From 504171a8a8008e5bdc6addd3f823befae9bf1eda Mon Sep 17 00:00:00 2001 From: ferdinando-ferreira Date: Tue, 21 Aug 2018 10:55:21 +0100 Subject: [PATCH] feat: methods option (#319) * Add the option `acceptedMethods` Add the option `acceptedMethods` to allow for requests with method other than "GET" to be processed * Update README.md Update README.md documenting the addition of the option `acceptedMethods` * Tests for the option `acceptedMethods` Add a set of testes for the option `acceptedMethods` * Tests for the option `acceptedMethods` : smallfix, line ending * Changed option name from acceptedMethods to methods * Update README.md * Changed option name from acceptedMethods to methods --- README.md | 7 +++++++ lib/middleware.js | 3 ++- test/tests/server.js | 28 ++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6b883a42e..321cc1b81 100644 --- a/README.md +++ b/README.md @@ -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` diff --git a/lib/middleware.js b/lib/middleware.js index dd720729e..23e2220cf 100644 --- a/lib/middleware.js +++ b/lib/middleware.js @@ -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(); } diff --git a/test/tests/server.js b/test/tests/server.js index af851daf4..ba9ede7ed 100644 --- a/test/tests/server.js +++ b/test/tests/server.js @@ -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();