Skip to content

Commit fd61145

Browse files
committed
feat: making body-parser optional and configurable.
1 parent bc5ca52 commit fd61145

File tree

4 files changed

+47
-11
lines changed

4 files changed

+47
-11
lines changed

README.md

+11-2
Original file line numberDiff line numberDiff line change
@@ -423,9 +423,18 @@ apiMocker('/api', {
423423
With that option, you can mock only specific urls simply.
424424
425425
## Body parser
426+
By default request body is pre-processed with [body-parser](https://github.com/expressjs/body-parser).
427+
Default body-parser configuration uses JSON parser. Example belows configures usage of `json` (default) parser
426428
427-
By default request body is parsed with help of [body-parser](https://github.com/expressjs/body-parser).
428-
Default configuration uses JSON parser, however you can modify it with help of `bodyParser` object.
429+
In order to disable dfault pre-processing set `bodyParser` option to `false`.
430+
```js
431+
apiMocker('/text', {
432+
target: 'test/mocks',
433+
bodyParser: false
434+
})
435+
```
436+
437+
In order to modify default body-parser behaviour use `bodyParser` object.
429438
`bodyParser` object supports configuration of
430439
431440
- parser type via `type` setting.

index.js

+13-9
Original file line numberDiff line numberDiff line change
@@ -173,23 +173,27 @@ module.exports = function (urlRoot, pathRoot) {
173173
if (requestParams) {
174174
req.params = requestParams;
175175
}
176-
let bodyParserType = 'json';
177-
let bodyParserOptions = {};
178-
if(typeof config.bodyParser != "undefined"){
179-
bodyParserType = config.bodyParser.type || 'json';
180-
bodyParserOptions = config.bodyParser.options || {};
181-
}
182-
bodyParser[bodyParserType](bodyParserOptions)(req, res, () => {
176+
177+
const executeMiddleware = (request, response) => {
183178
if (typeof customMiddleware === 'function') {
184179
customMiddleware = [customMiddleware];
185180
}
186181
// flatten middlewares
187182
customMiddleware = [].concat(...customMiddleware);
188183

189184
customMiddleware.reduce((chain, middleware) => chain.then(
190-
() => new Promise(resolve => middleware(req, res, resolve))
185+
() => new Promise(resolve => middleware(request, response, resolve))
191186
), Promise.resolve()).then(next);
192-
});
187+
};
188+
189+
if (config.bodyParser === false) {
190+
executeMiddleware(req, res);
191+
} else {
192+
const bodyParserType = (config.bodyParser && config.bodyParser.type) || 'json';
193+
const bodyParserOptions = (config.bodyParser && config.bodyParser.options) || {};
194+
195+
bodyParser[bodyParserType](bodyParserOptions)(req, res, () => executeMiddleware(req, res));
196+
}
193197
} else {
194198
let fileType = config.type || 'json';
195199

test/api-mocker.spec.js

+13
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ app.use(apiMocker('/text', {
5353
options: { type: 'application/vnd.custom-type' }
5454
}
5555
}));
56+
app.use('/disable-body-parser', apiMocker({
57+
target: 'test/mocks/bodyParser/disabled',
58+
bodyParser: false
59+
}));
60+
5661

5762
describe('Simple configuration with baseUrl', () => {
5863
it('responds for simple GET request', (done) => {
@@ -250,6 +255,14 @@ describe('Handling request body', () => {
250255
}, done);
251256
});
252257

258+
it('should work with disabled request body', (done) => {
259+
request(app)
260+
.post('/disable-body-parser')
261+
.send({ todo: 'buy milk' })
262+
.expect(201)
263+
.expect('buy milk', done);
264+
});
265+
253266
it('should work with request body raw', (done) => {
254267
request(app)
255268
.post('/text/bodyParser')
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = function (req, res) {
2+
const data = [];
3+
req
4+
.on('data', (chunk) => {
5+
data.push(chunk);
6+
})
7+
.on('end', () => {
8+
res.status(201).send(JSON.parse(data).todo);
9+
});
10+
};

0 commit comments

Comments
 (0)