Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP Add way to reject body with the wrong content type #274

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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