Skip to content

Commit

Permalink
use text/plain as default content-type in .json
Browse files Browse the repository at this point in the history
  • Loading branch information
floatdrop committed Mar 27, 2017
1 parent 8012c9e commit 96d3249
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ exports.json = (req, { limit = '1mb' } = {}) =>
Promise.resolve().then(() => {
const type = req.headers['content-type'];
const length = req.headers['content-length'];
const encoding = typer.parse(type).parameters.charset;
const encoding = type === undefined ? 'text/plain' : typer.parse(type).parameters.charset;

let str = rawBodyMap.get(req);

Expand Down
13 changes: 13 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -586,3 +586,16 @@ test('res.end is working', async t => {

t.deepEqual(res, 'woot');
});

test('json should throw 400 on empty body with no headers', async t => {
const fn = async req => json(req);

const url = await getUrl(fn);

try {
await request(url);
} catch (err) {
t.is(err.message, '400 - "Invalid body"');
t.is(err.statusCode, 400);
}
});

0 comments on commit 96d3249

Please sign in to comment.