diff --git a/README.md b/README.md index 6bb9013..c156e80 100644 --- a/README.md +++ b/README.md @@ -424,8 +424,16 @@ With that option, you can mock only specific urls simply. ## Body 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. +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', + 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); + }); +};