Skip to content

Commit

Permalink
feature: BodyParser settings are now configurable
Browse files Browse the repository at this point in the history
Unlocked usage of all parsers and options of `body-parser`
  • Loading branch information
muratcorlu authored Aug 8, 2019
2 parents f45222e + 8376c48 commit 47880ef
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
language: node_js
install:
- npm install
cache:
directories:
- ~/.npm
Expand Down
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,28 @@ apiMocker('/api', {
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.
`bodyParser` object supports configuration of
- parser type via `type` setting.
- parser options via `options` setting.
Supported parsers and corresponding options can be found [here](https://github.com/expressjs/body-parser#bodyparserjsonoptions)
Example belows configures usage of `text` parser for requests with `content-type=application/vnd.custom-type`
```js
apiMocker('/text', {
target: 'test/mocks',
bodyParser: {
type: 'text',
options: { type: 'application/vnd.custom-type' }
}
})
```
## Logging
If you want to see which requests are being mocked, set the `verbose` option either to `true` or provide your own function.
Expand Down
8 changes: 7 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,13 @@ module.exports = function (urlRoot, pathRoot) {
if (requestParams) {
req.params = requestParams;
}
bodyParser.json()(req, res, () => {
let bodyParserType = 'json';
let bodyParserOptions = {};
if(typeof config.bodyParser != "undefined"){
bodyParserType = config.bodyParser.type || 'json';
bodyParserOptions = config.bodyParser.options || {};
}
bodyParser[bodyParserType](bodyParserOptions)(req, res, () => {
if (typeof customMiddleware === 'function') {
customMiddleware = [customMiddleware];
}
Expand Down
16 changes: 16 additions & 0 deletions test/api-mocker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ app.use(apiMocker('/dyn', {
target: 'test/mocks',
type: 'auto'
}));
app.use(apiMocker('/text', {
target: 'test/mocks',
bodyParser: {
type: 'text',
options: { type: 'application/vnd.custom-type' }
}
}));

describe('Simple configuration with baseUrl', () => {
it('responds for simple GET request', (done) => {
Expand Down Expand Up @@ -243,6 +250,15 @@ describe('Handling request body', () => {
}, done);
});

it('should work with request body raw', (done) => {
request(app)
.post('/text/bodyParser')
.set('Content-Type', 'application/vnd.custom-type')
.send('plain text')
.expect(201)
.expect('plain text', done);
});

it('shouldnt break to capability of reading raw request body', (done) => {
request(app)
.patch('/api/users')
Expand Down
3 changes: 3 additions & 0 deletions test/mocks/bodyParser/POST.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function (req, res) {
res.status(201).send(req.body);
};

0 comments on commit 47880ef

Please sign in to comment.