Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/types/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions test/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' }),
Expand Down