Skip to content

Commit

Permalink
Merge d8ea63b into a4ed25d
Browse files Browse the repository at this point in the history
  • Loading branch information
lukechilds committed Apr 3, 2019
2 parents a4ed25d + d8ea63b commit f21d0d7
Show file tree
Hide file tree
Showing 5 changed files with 5,674 additions and 9 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
@@ -1,8 +1,7 @@
language: node_js
node_js:
- '10'
- '8'
- '6'
- '4'
script: npm test
after_success: npm run coverage
notifications:
Expand Down
8 changes: 6 additions & 2 deletions README.md
Expand Up @@ -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`)
- 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).

You can create a separate server per test:
Expand Down Expand Up @@ -166,10 +168,12 @@ SSL certificate options to be passed to [`createCert()`](https://github.com/luke

##### options.bodyParser

Type: `object`
Type: `object | boolean`<br>
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

Expand Down
14 changes: 9 additions & 5 deletions src/index.js
Expand Up @@ -7,7 +7,7 @@ const pify = require('pify');
const createCert = require('create-cert');
const bodyParser = require('body-parser');

const createTestServer = opts => createCert(opts && opts.certificate)
const createTestServer = (opts = {}) => createCert(opts.certificate)
.then(keys => {
const app = express();
const get = app.get.bind(app);
Expand All @@ -24,10 +24,14 @@ const createTestServer = opts => createCert(opts && opts.certificate)
};

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)));
app.use(bodyParser.urlencoded(Object.assign({ limit: '1mb', type: 'application/x-www-form-urlencoded', extended: true }, opts && opts.bodyParser)));
app.use(bodyParser.raw(Object.assign({ limit: '1mb', type: 'application/octet-stream' }, opts && opts.bodyParser)));

if (opts.bodyParser !== false) {
app.use(bodyParser.json(Object.assign({ limit: '1mb', type: 'application/json' }, 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.listen = () => Promise.all([
Expand Down
15 changes: 15 additions & 0 deletions test/create-test-server.js
Expand Up @@ -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 => {
const server = await createTestServer();

Expand Down

0 comments on commit f21d0d7

Please sign in to comment.