From fd611454ee252725bcb7a82385e4debe40e6bba1 Mon Sep 17 00:00:00 2001 From: Valerii Udodov Date: Wed, 14 Aug 2019 09:54:29 +1000 Subject: [PATCH 1/2] feat: making body-parser optional and configurable. --- README.md | 13 +++++++++++-- index.js | 22 +++++++++++++--------- test/api-mocker.spec.js | 13 +++++++++++++ test/mocks/bodyParser/disabled/POST.js | 10 ++++++++++ 4 files changed, 47 insertions(+), 11 deletions(-) create mode 100644 test/mocks/bodyParser/disabled/POST.js diff --git a/README.md b/README.md index 6bb9013..57bb318 100644 --- a/README.md +++ b/README.md @@ -423,9 +423,18 @@ apiMocker('/api', { With that option, you can mock only specific urls simply. ## Body parser +By default request body is pre-processed with [body-parser](https://github.com/expressjs/body-parser). +Default body-parser configuration uses JSON parser. Example belows configures usage of `json` (default) parser -By default request body is parsed with help of [body-parser](https://github.com/expressjs/body-parser). -Default configuration uses JSON parser, however you can modify it with help of `bodyParser` object. +In order to disable dfault pre-processing set `bodyParser` option to `false`. +```js +apiMocker('/text', { + target: 'test/mocks', + bodyParser: false +}) +``` + +In order to modify default body-parser behaviour use `bodyParser` object. `bodyParser` object supports configuration of - parser type via `type` setting. diff --git a/index.js b/index.js index e66b1fa..1371d40 100644 --- a/index.js +++ b/index.js @@ -173,13 +173,8 @@ module.exports = function (urlRoot, pathRoot) { if (requestParams) { req.params = requestParams; } - let bodyParserType = 'json'; - let bodyParserOptions = {}; - if(typeof config.bodyParser != "undefined"){ - bodyParserType = config.bodyParser.type || 'json'; - bodyParserOptions = config.bodyParser.options || {}; - } - bodyParser[bodyParserType](bodyParserOptions)(req, res, () => { + + const executeMiddleware = (request, response) => { if (typeof customMiddleware === 'function') { customMiddleware = [customMiddleware]; } @@ -187,9 +182,18 @@ module.exports = function (urlRoot, pathRoot) { customMiddleware = [].concat(...customMiddleware); customMiddleware.reduce((chain, middleware) => chain.then( - () => new Promise(resolve => middleware(req, res, resolve)) + () => new Promise(resolve => middleware(request, response, resolve)) ), Promise.resolve()).then(next); - }); + }; + + if (config.bodyParser === false) { + executeMiddleware(req, res); + } else { + const bodyParserType = (config.bodyParser && config.bodyParser.type) || 'json'; + const bodyParserOptions = (config.bodyParser && config.bodyParser.options) || {}; + + bodyParser[bodyParserType](bodyParserOptions)(req, res, () => executeMiddleware(req, res)); + } } else { let fileType = config.type || 'json'; diff --git a/test/api-mocker.spec.js b/test/api-mocker.spec.js index 64f29dd..c30a470 100644 --- a/test/api-mocker.spec.js +++ b/test/api-mocker.spec.js @@ -53,6 +53,11 @@ app.use(apiMocker('/text', { options: { type: 'application/vnd.custom-type' } } })); +app.use('/disable-body-parser', apiMocker({ + target: 'test/mocks/bodyParser/disabled', + bodyParser: false +})); + describe('Simple configuration with baseUrl', () => { it('responds for simple GET request', (done) => { @@ -250,6 +255,14 @@ describe('Handling request body', () => { }, done); }); + it('should work with disabled request body', (done) => { + request(app) + .post('/disable-body-parser') + .send({ todo: 'buy milk' }) + .expect(201) + .expect('buy milk', done); + }); + it('should work with request body raw', (done) => { request(app) .post('/text/bodyParser') diff --git a/test/mocks/bodyParser/disabled/POST.js b/test/mocks/bodyParser/disabled/POST.js new file mode 100644 index 0000000..068eb9e --- /dev/null +++ b/test/mocks/bodyParser/disabled/POST.js @@ -0,0 +1,10 @@ +module.exports = function (req, res) { + const data = []; + req + .on('data', (chunk) => { + data.push(chunk); + }) + .on('end', () => { + res.status(201).send(JSON.parse(data).todo); + }); +}; From ecae19ffea4b65d074d12454ef14e16672a770d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Murat=20=C3=87orlu?= Date: Thu, 26 Sep 2019 10:41:43 +0200 Subject: [PATCH 2/2] docs: Fixed typo and paragraphs --- README.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 57bb318..c156e80 100644 --- a/README.md +++ b/README.md @@ -423,10 +423,9 @@ apiMocker('/api', { With that option, you can mock only specific urls simply. ## Body parser -By default request body is pre-processed with [body-parser](https://github.com/expressjs/body-parser). -Default body-parser configuration uses JSON parser. Example belows configures usage of `json` (default) parser -In order to disable dfault pre-processing set `bodyParser` option to `false`. +By default request body is pre-processed with [body-parser](https://github.com/expressjs/body-parser). Default body-parser configuration uses JSON parser. Example belows configures usage of `json` (default) parser. In order to disable default pre-processing set `bodyParser` option to `false`. + ```js apiMocker('/text', { target: 'test/mocks',