diff --git a/lib/types/json.js b/lib/types/json.js index a7bc838c..5f2bee13 100644 --- a/lib/types/json.js +++ b/lib/types/json.js @@ -57,6 +57,7 @@ function json (options) { var reviver = opts.reviver var strict = opts.strict !== false var type = opts.type || 'application/json' + var strictType = !!opts.strictType var verify = opts.verify || false if (verify !== false && typeof verify !== 'function') { @@ -114,6 +115,14 @@ function json (options) { // determine if request should be parsed if (!shouldParse(req)) { + if (strictType) { + debug('invalid content-type') + next(createError(415, 'Unsupported Content-Type: "' + req.headers['content-type'] + '"', { + // charset: charset, + type: 'content-type.unsupported' + })) + } + debug('skip parsing') next() return diff --git a/test/json.js b/test/json.js index 5cc10cf3..cdb1580a 100644 --- a/test/json.js +++ b/test/json.js @@ -372,6 +372,20 @@ describe('bodyParser.json()', function () { }) }) + describe('with strictType option', function () { + before(function () { + this.server = createServer({ strictType: true }) + }) + + it('should error when given wrong content type', function (done) { + request(this.server) + .post('/') + .set('Content-Type', 'application/x-www-form-urlencoded') + .send('') + .expect(415, 'Unsupported Content-Type: "application/x-www-form-urlencoded"', done) + }) + }) + describe('with verify option', function () { it('should assert value if function', function () { assert.throws(createServer.bind(null, { verify: 'lol' }),