Skip to content

Commit

Permalink
Merge 43417e2 into a4ed25d
Browse files Browse the repository at this point in the history
  • Loading branch information
lukechilds committed Apr 3, 2019
2 parents a4ed25d + 43417e2 commit a1a9a00
Show file tree
Hide file tree
Showing 4 changed files with 5,668 additions and 7 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
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 a1a9a00

Please sign in to comment.