Skip to content

feat(middleware): add methods option (options.methods) #319

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

Merged
merged 7 commits into from
Aug 21, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ for the Object.

_Note: The `publicPath` property is required, whereas all other options are optional_

### methods

Type: `Array`
Default: `[ 'GET' ]`

This property allows a user to pass the list of HTTP request methods accepted by the server.

### headers

Type: `Object`
Expand Down
3 changes: 2 additions & 1 deletion lib/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ module.exports = function wrapper(context) {
}));
}

if (req.method !== 'GET') {
const acceptedMethods = context.options.methods || ['GET'];
if (acceptedMethods.indexOf(req.method) === -1) {
return goNext();
}

Expand Down
28 changes: 28 additions & 0 deletions test/tests/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,34 @@ describe('Server', () => {
});
});

describe('accepted methods', () => {
before((done) => {
app = express();
const compiler = webpack(webpackConfig);
instance = middleware(compiler, {
stats: 'errors-only',
methods: ['POST'],
logLevel,
publicPath: '/public/'
});
app.use(instance);
listen = listenShorthand(done);
});
after(close);

it('POST request to bundle file with methods set to [\'POST\']', (done) => {
request(app).post('/public/bundle.js')
.expect('Content-Type', 'application/javascript; charset=UTF-8')
.expect('Content-Length', '3645')
.expect(200, /console\.log\('Hey\.'\)/, done);
});

it('GET request to bundle file with methods set to [\'POST\']', (done) => {
request(app).get('/public/bundle.js')
.expect(404, done);
});
});

describe('no index mode', () => {
before((done) => {
app = express();
Expand Down