Browse Source

Allow optionally disabling body parser (#32)

* Use default function paramter for options object

* Don't set body parsing middleware if opts.bodyParser is false

* Only test against Node.js versions 8 and 10

* Test if opts.bodyParser is false body parsing middleware is disabled

* Document disabling body parsing middleware
pull/33/head
Luke Childs 6 years ago
committed by GitHub
parent
commit
6af11837ea
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      .travis.yml
  2. 8
      README.md
  3. 14
      src/index.js
  4. 15
      test/create-test-server.js
  5. 5643
      yarn.lock

3
.travis.yml

@ -1,8 +1,7 @@
language: node_js language: node_js
node_js: node_js:
- '10'
- '8' - '8'
- '6'
- '4'
script: npm test script: npm test
after_success: npm run coverage after_success: npm run coverage
notifications: notifications:

8
README.md

@ -53,6 +53,8 @@ The following `Content-Type` headers will be parsed and exposed via `req.body`:
- URL-encoded form (`application/x-www-form-urlencoded`) - URL-encoded form (`application/x-www-form-urlencoded`)
- Buffer (`application/octet-stream`) - Buffer (`application/octet-stream`)
You can change body parsing behaviour with the [`bodyParser`](#optionsbodyparser) option.
`createTestServer()` has a Promise based API that pairs well with a modern asynchronous test runner such as [AVA](https://github.com/avajs/ava). `createTestServer()` has a Promise based API that pairs well with a modern asynchronous test runner such as [AVA](https://github.com/avajs/ava).
You can create a separate server per test: You can create a separate server per test:
@ -166,10 +168,12 @@ SSL certificate options to be passed to [`createCert()`](https://github.com/luke
##### options.bodyParser ##### options.bodyParser
Type: `object` Type: `object | boolean`<br>
Default: `undefined` Default: `undefined`
Body parser options to be passed to [`body-parser`](https://github.com/expressjs/body-parser) methods. Body parser options object to be passed to [`body-parser`](https://github.com/expressjs/body-parser) methods.
If set to `false` then all body parsing middleware will be disabled.
### server ### server

14
src/index.js

@ -7,7 +7,7 @@ const pify = require('pify');
const createCert = require('create-cert'); const createCert = require('create-cert');
const bodyParser = require('body-parser'); const bodyParser = require('body-parser');
const createTestServer = opts => createCert(opts && opts.certificate) const createTestServer = (opts = {}) => createCert(opts.certificate)
.then(keys => { .then(keys => {
const app = express(); const app = express();
const get = app.get.bind(app); const get = app.get.bind(app);
@ -24,10 +24,14 @@ const createTestServer = opts => createCert(opts && opts.certificate)
}; };
app.set('etag', false); app.set('etag', false);
app.use(bodyParser.json(Object.assign({ limit: '1mb', type: 'application/json' }, opts && opts.bodyParser)));
app.use(bodyParser.text(Object.assign({ limit: '1mb', type: 'text/plain' }, opts && opts.bodyParser))); if (opts.bodyParser !== false) {
app.use(bodyParser.urlencoded(Object.assign({ limit: '1mb', type: 'application/x-www-form-urlencoded', extended: true }, opts && opts.bodyParser))); app.use(bodyParser.json(Object.assign({ limit: '1mb', type: 'application/json' }, opts.bodyParser)));
app.use(bodyParser.raw(Object.assign({ limit: '1mb', type: 'application/octet-stream' }, opts && opts.bodyParser))); app.use(bodyParser.text(Object.assign({ limit: '1mb', type: 'text/plain' }, opts.bodyParser)));
app.use(bodyParser.urlencoded(Object.assign({ limit: '1mb', type: 'application/x-www-form-urlencoded', extended: true }, opts.bodyParser)));
app.use(bodyParser.raw(Object.assign({ limit: '1mb', type: 'application/octet-stream' }, opts.bodyParser)));
}
app.caCert = keys.caCert; app.caCert = keys.caCert;
app.listen = () => Promise.all([ app.listen = () => Promise.all([

15
test/create-test-server.js

@ -186,6 +186,21 @@ test('opts.bodyParser is passed through to bodyParser', async t => {
})); }));
}); });
test('if opts.bodyParser is false body parsing middleware is disabled', async t => {
const server = await createTestServer({ bodyParser: false });
const text = 'foo';
server.post('/echo', (req, res) => {
t.deepEqual(req.body, undefined);
res.end();
});
await got.post(server.url + '/echo', {
headers: { 'content-type': 'text/plain' },
body: text
});
});
test('support returning body directly', async t => { test('support returning body directly', async t => {
const server = await createTestServer(); const server = await createTestServer();

5643
yarn.lock

File diff suppressed because it is too large
Loading…
Cancel
Save