Skip to content

Commit

Permalink
feat(middleware): add methods option (options.methods) (#319)
Browse files Browse the repository at this point in the history
  • Loading branch information
ferdinando-ferreira authored and michael-ciniawsky committed Aug 23, 2018
1 parent ad88fed commit fe6bb86
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
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

0 comments on commit fe6bb86

Please sign in to comment.