Skip to content

Commit 395a31d

Browse files
committed
chore: Merge branch 'master' of github.com:muratcorlu/connect-api-mocker
2 parents e737d9c + 9d14f87 commit 395a31d

File tree

4 files changed

+46
-11
lines changed

4 files changed

+46
-11
lines changed

README.md

+10-2
Original file line numberDiff line numberDiff line change
@@ -437,8 +437,16 @@ With that option, you can mock only specific urls simply.
437437
438438
## Body parser
439439
440-
By default request body is parsed with help of [body-parser](https://github.com/expressjs/body-parser).
441-
Default configuration uses JSON parser, however you can modify it with help of `bodyParser` object.
440+
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`.
441+
442+
```js
443+
apiMocker('/text', {
444+
target: 'test/mocks',
445+
bodyParser: false
446+
})
447+
```
448+
449+
In order to modify default body-parser behaviour use `bodyParser` object.
442450
`bodyParser` object supports configuration of
443451
444452
- 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)