diff --git a/History.md b/History.md index b38715d..97de9c7 100644 --- a/History.md +++ b/History.md @@ -1,6 +1,7 @@ unreleased ========== + * Properly support all HTTP methods * Support vanilla node.js http servers 1.0.3 / 2014-05-20 diff --git a/index.js b/index.js index 8fa74f4..7e6247c 100644 --- a/index.js +++ b/index.js @@ -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) diff --git a/test/test.js b/test/test.js index 271921d..17c74be 100644 --- a/test/test.js +++ b/test/test.js @@ -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) {