Skip to content

Commit

Permalink
tests: add test for err.body on json parse error
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Sep 8, 2017
1 parent 4e15325 commit c659e8a
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions test/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ describe('bodyParser.json()', function () {
.send('{"user"')
.expect(400, parseError('{"user"'), done)
})

it('should include original body on error object', function (done) {
request(this.server)
.post('/')
.set('Content-Type', 'application/json')
.set('X-Error-Property', 'body')
.send(' {"user"')
.expect(400, ' {"user"', done)
})
})

describe('with limit option', function () {
Expand Down Expand Up @@ -492,8 +501,13 @@ function createServer (opts) {

return http.createServer(function (req, res) {
_bodyParser(req, res, function (err) {
res.statusCode = err ? (err.status || 500) : 200
res.end(err ? err.message : JSON.stringify(req.body))
if (err) {
res.statusCode = err.status || 500
res.end(err[req.headers['x-error-property'] || 'message'])
} else {
res.statusCode = 200
res.end(JSON.stringify(req.body))
}
})
})
}
Expand Down

0 comments on commit c659e8a

Please sign in to comment.