Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(server): fallthrough non GET and HEAD request to routes defined in after option #2374

Merged
merged 1 commit into from Dec 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 16 additions & 2 deletions lib/Server.js
Expand Up @@ -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);
});
}
}

Expand Down
14 changes: 14 additions & 0 deletions test/server/after-option.test.js
Expand Up @@ -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,
},
Expand All @@ -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');
});
});
});
8 changes: 4 additions & 4 deletions test/server/contentBasePublicPath-option.test.js
Expand Up @@ -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);
});
});
});