Properly support all HTTP methods

This commit is contained in:
Douglas Christopher Wilson
2014-05-28 17:13:22 -04:00
parent db91ccac66
commit c00618fa62
3 changed files with 44 additions and 1 deletions
+1
View File
@@ -1,6 +1,7 @@
unreleased
==========
* Properly support all HTTP methods
* Support vanilla node.js http servers
1.0.3 / 2014-05-20
+6 -1
View File
@@ -85,7 +85,12 @@ exports = module.exports = function serveIndex(root, options){
, stylesheet = options.stylesheet || defaultStylesheet;
return function serveIndex(req, res, next) {
if ('GET' != req.method && 'HEAD' != req.method) return next();
if (req.method !== 'GET' && req.method !== 'HEAD') {
var status = 'OPTIONS' === req.method ? 200 : 405;
res.writeHead(status, {'Allow': 'GET, HEAD, OPTIONS'});
res.end();
return;
}
var url = parse(req.url)
, dir = decodeURIComponent(url.pathname)
+37
View File
@@ -9,6 +9,43 @@ describe('serveIndex(root)', function () {
serveIndex.should.throw(/root path required/)
})
it('should serve a directory index', function (done) {
var server = createServer()
request(server)
.get('/')
.set('Accept', 'text/html, */*;q=0.5')
.expect(200, /todo\.txt/, done)
})
it('should work with HEAD requests', function (done) {
var server = createServer()
request(server)
.head('/')
.set('Accept', 'text/html, */*;q=0.5')
.expect(200, '', done)
})
it('should work with OPTIONS requests', function (done) {
var server = createServer()
request(server)
.options('/')
.set('Accept', 'text/html, */*;q=0.5')
.expect('Allow', 'GET, HEAD, OPTIONS')
.expect(200, done)
})
it('should deny POST requests', function (done) {
var server = createServer()
request(server)
.post('/')
.set('Accept', 'text/html, */*;q=0.5')
.expect(405, done)
})
describe('when given Accept: header', function () {
describe('when Accept: application/json is given', function () {
it('should respond with json', function (done) {