From 5b8609939799a500cdaa1335a02769a2054cb560 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Vasseur?= Date: Thu, 26 Dec 2019 14:05:09 +0100 Subject: [PATCH] fix(server): fix POST request forward to routes defined in after option Fix a bug introduced in cee700d59aff644a499ee310c4a32d5c5693e559 where the serveIndex feature where always replying instead of forwarding requests to the next middleware. --- lib/Server.js | 18 ++++++++++++++++-- test/server/after-option.test.js | 14 ++++++++++++++ .../contentBasePublicPath-option.test.js | 8 ++++---- 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/lib/Server.js b/lib/Server.js index 4edba202de..dff653aceb 100644 --- a/lib/Server.js +++ b/lib/Server.js @@ -390,13 +390,27 @@ class Server { if (Array.isArray(contentBase)) { contentBase.forEach((item) => { - this.app.use(contentBasePublicPath, serveIndex(item)); + this.app.use(contentBasePublicPath, (req, res, next) => { + // serve-index doesn't fallthrough non-get/head request to next middleware + if (req.method !== 'GET' && req.method !== 'HEAD') { + return next(); + } + + serveIndex(item)(req, res, next); + }); }); } else if ( typeof contentBase !== 'number' && !isAbsoluteUrl(String(contentBase)) ) { - this.app.use(contentBasePublicPath, serveIndex(contentBase)); + this.app.use(contentBasePublicPath, (req, res, next) => { + // serve-index doesn't fallthrough non-get/head request to next middleware + if (req.method !== 'GET' && req.method !== 'HEAD') { + return next(); + } + + serveIndex(contentBase)(req, res, next); + }); } } diff --git a/test/server/after-option.test.js b/test/server/after-option.test.js index b9d57616d0..9a0b07ea5e 100644 --- a/test/server/after-option.test.js +++ b/test/server/after-option.test.js @@ -29,6 +29,10 @@ describe('after option', () => { appArg.get('/after/some/path', (_, response) => { response.send('after'); }); + + appArg.post('/after/some/path', (_, response) => { + response.send('after POST'); + }); }, port, }, @@ -48,4 +52,14 @@ describe('after option', () => { expect(response.text).toBe('after'); }); }); + + it('should handle POST requests to after route', () => { + return req + .post('/after/some/path') + .expect('Content-Type', 'text/html; charset=utf-8') + .expect(200) + .then((response) => { + expect(response.text).toBe('after POST'); + }); + }); }); diff --git a/test/server/contentBasePublicPath-option.test.js b/test/server/contentBasePublicPath-option.test.js index b9eb5c3fa6..752002fcde 100644 --- a/test/server/contentBasePublicPath-option.test.js +++ b/test/server/contentBasePublicPath-option.test.js @@ -277,19 +277,19 @@ describe('contentBasePublicPath option', () => { }); it('POST request', (done) => { - req.post(`${contentBasePublicPath}/`).expect(405, done); + req.post(`${contentBasePublicPath}/`).expect(404, done); }); it('PUT request', (done) => { - req.put(`${contentBasePublicPath}/`).expect(405, done); + req.put(`${contentBasePublicPath}/`).expect(404, done); }); it('DELETE request', (done) => { - req.delete(`${contentBasePublicPath}/`).expect(405, done); + req.delete(`${contentBasePublicPath}/`).expect(404, done); }); it('PATCH request', (done) => { - req.patch(`${contentBasePublicPath}/`).expect(405, done); + req.patch(`${contentBasePublicPath}/`).expect(404, done); }); }); });