Browse Source

Allow options to be passed to `body-parser`. (#22)

pull/25/head
wtgtybhertgeghgtwtg 6 years ago
committed by Luke Childs
parent
commit
36d4b66e2f
  1. 7
      README.md
  2. 9
      src/index.js
  3. 33
      test/create-test-server.js

7
README.md

@ -164,6 +164,13 @@ Default: `undefined`
SSL certificate options to be passed to [`createCert()`](https://github.com/lukechilds/create-cert).
##### options.bodyParser
Type: `object`
Default: `undefined`
Body parser options to be passed to [`body-parser`](https://github.com/expressjs/body-parser) methods.
### server
Express instance resolved from `createTestServer()`

9
src/index.js

@ -24,11 +24,10 @@ const createTestServer = opts => createCert(opts && opts.certificate)
};
app.set('etag', false);
app.use(bodyParser.json({ type: 'application/json' }));
app.use(bodyParser.text({ type: 'text/plain' }));
app.use(bodyParser.urlencoded({ type: 'application/x-www-form-urlencoded', extended: true }));
app.use(bodyParser.raw({ type: 'application/octet-stream' }));
app.use(bodyParser.json(Object.assign({ type: 'application/json' }, opts && opts.bodyParser)));
app.use(bodyParser.text(Object.assign({ type: 'text/plain' }, opts && opts.bodyParser)));
app.use(bodyParser.urlencoded(Object.assign({ type: 'application/x-www-form-urlencoded', extended: true }, opts && opts.bodyParser)));
app.use(bodyParser.raw(Object.assign({ type: 'application/octet-stream' }, opts && opts.bodyParser)));
app.caCert = keys.caCert;
app.listen = () => Promise.all([

33
test/create-test-server.js

@ -153,6 +153,39 @@ test('opts.certificate is passed through to createCert()', async t => {
t.is(body, 'bar');
});
test('opts.bodyParser is passed through to bodyParser', async t => {
const smallServer = await createTestServer({ bodyParser: { limit: '100kb' } });
const bigServer = await createTestServer({ bodyParser: { limit: '200kb' } });
const buf = Buffer.alloc(150 * 1024);
// Custom error handler so we don't dump the stack trace in the test output
smallServer.use((err, req, res, next) => { // eslint-disable-line no-unused-vars
res.status(500).end();
});
t.plan(3);
smallServer.post('/', (req, res) => {
t.fail();
res.end();
});
bigServer.post('/', (req, res) => {
t.true(req.body.length === buf.length);
res.end();
});
await t.throws(got.post(smallServer.url, {
headers: { 'content-type': 'application/octet-stream' },
body: buf
}));
await t.notThrows(got.post(bigServer.url, {
headers: { 'content-type': 'application/octet-stream' },
body: buf
}));
});
test('support returning body directly', async t => {
const server = await createTestServer();

Loading…
Cancel
Save