Skip to content

Commit 1581e09

Browse files
authoredOct 26, 2017
Merge pull request #376 from jbielick/allow-header-list
Comma-separate list of Allow header methods (#273)
2 parents afb542b + 9c3dec9 commit 1581e09

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed
 

‎lib/router.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -426,13 +426,13 @@ Router.prototype.allowedMethods = function (options) {
426426
throw notImplementedThrowable;
427427
} else {
428428
ctx.status = 501;
429-
ctx.set('Allow', allowedArr);
429+
ctx.set('Allow', allowedArr.join(', '));
430430
}
431431
} else if (allowedArr.length) {
432432
if (ctx.method === 'OPTIONS') {
433433
ctx.status = 200;
434434
ctx.body = '';
435-
ctx.set('Allow', allowedArr);
435+
ctx.set('Allow', allowedArr.join(', '));
436436
} else if (!allowed[ctx.method]) {
437437
if (options.throw) {
438438
var notAllowedThrowable;
@@ -444,7 +444,7 @@ Router.prototype.allowedMethods = function (options) {
444444
throw notAllowedThrowable;
445445
} else {
446446
ctx.status = 405;
447-
ctx.set('Allow', allowedArr);
447+
ctx.set('Allow', allowedArr.join(', '));
448448
}
449449
}
450450
}

‎test/lib/router.js

+22
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,28 @@ describe('Router', function () {
754754
done();
755755
});
756756
});
757+
758+
it('sets the allowed methods to a single Allow header #273', function (done) {
759+
// https://tools.ietf.org/html/rfc7231#section-7.4.1
760+
var app = new Koa();
761+
var router = new Router();
762+
app.use(router.routes());
763+
app.use(router.allowedMethods());
764+
765+
router.get('/', function (ctx, next) {});
766+
767+
request(http.createServer(app.callback()))
768+
.options('/')
769+
.expect(200)
770+
.end(function (err, res) {
771+
if (err) return done(err);
772+
res.header.should.have.property('allow', 'HEAD, GET');
773+
let allowHeaders = res.res.rawHeaders.filter((item) => item == 'Allow');
774+
expect(allowHeaders.length).to.eql(1);
775+
done();
776+
});
777+
});
778+
757779
});
758780

759781
it('supports custom routing detect path: ctx.routerPath', function (done) {

0 commit comments

Comments
 (0)
Please sign in to comment.