Skip to content

Commit

Permalink
feat: Making body parser optional and configurable.
Browse files Browse the repository at this point in the history
Thanks to @vudodov
  • Loading branch information
muratcorlu authored Sep 26, 2019
2 parents bc5ca52 + ecae19f commit 9d14f87
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 11 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
22 changes: 13 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,23 +173,27 @@ 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];
}
// flatten middlewares
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';

Expand Down
13 changes: 13 additions & 0 deletions test/api-mocker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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')
Expand Down
10 changes: 10 additions & 0 deletions test/mocks/bodyParser/disabled/POST.js
Original file line number Diff line number Diff line change
@@ -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);
});
};

0 comments on commit 9d14f87

Please sign in to comment.