Skip to content

Commit

Permalink
tests: assert err.type on most tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Feb 21, 2022
1 parent 705d024 commit 09dcbd9
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 135 deletions.
6 changes: 3 additions & 3 deletions test/body-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ describe('bodyParser()', function () {
.post('/')
.set('Content-Type', 'application/json')
.send(' {"user":"tobi"}')
.expect(403, 'no leading space', done)
.expect(403, '[entity.verify.failed] no leading space', done)
})

it('should apply to urlencoded', function (done) {
Expand All @@ -138,7 +138,7 @@ describe('bodyParser()', function () {
.post('/')
.set('Content-Type', 'application/x-www-form-urlencoded')
.send(' user=tobi')
.expect(403, 'no leading space', done)
.expect(403, '[entity.verify.failed] no leading space', done)
})
})
})
Expand All @@ -149,7 +149,7 @@ 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))
res.end(err ? ('[' + err.type + '] ' + err.message) : JSON.stringify(req.body))
})
})
}
92 changes: 16 additions & 76 deletions test/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,24 +79,15 @@ describe('bodyParser.json()', function () {
.post('/')
.set('Content-Type', 'application/json')
.send('{:')
.expect(400, parseError('{:'), done)
.expect(400, '[entity.parse.failed] ' + parseError('{:'), done)
})

it('should 400 for incomplete', function (done) {
request(this.server)
.post('/')
.set('Content-Type', 'application/json')
.send('{"user"')
.expect(400, parseError('{"user"'), done)
})

it('should error with type = "entity.parse.failed"', function (done) {
request(this.server)
.post('/')
.set('Content-Type', 'application/json')
.set('X-Error-Property', 'type')
.send(' {"user"')
.expect(400, 'entity.parse.failed', done)
.expect(400, '[entity.parse.failed] ' + parseError('{"user"'), done)
})

it('should include original body on error object', function (done) {
Expand All @@ -117,18 +108,7 @@ describe('bodyParser.json()', function () {
.set('Content-Type', 'application/json')
.set('Content-Length', '1034')
.send(JSON.stringify({ str: buf.toString() }))
.expect(413, done)
})

it('should error with type = "entity.too.large"', function (done) {
var buf = Buffer.alloc(1024, '.')
request(createServer({ limit: '1kb' }))
.post('/')
.set('Content-Type', 'application/json')
.set('Content-Length', '1034')
.set('X-Error-Property', 'type')
.send(JSON.stringify({ str: buf.toString() }))
.expect(413, 'entity.too.large', done)
.expect(413, '[entity.too.large] request entity too large', done)
})

it('should 413 when over limit with chunked encoding', function (done) {
Expand Down Expand Up @@ -197,7 +177,7 @@ describe('bodyParser.json()', function () {
test.set('Content-Encoding', 'gzip')
test.set('Content-Type', 'application/json')
test.write(Buffer.from('1f8b080000000000000bab56ca4bcc4d55b2527ab16e97522d00515be1cc0e000000', 'hex'))
test.expect(415, 'content encoding unsupported', done)
test.expect(415, '[encoding.unsupported] content encoding unsupported', done)
})
})

Expand Down Expand Up @@ -227,7 +207,7 @@ describe('bodyParser.json()', function () {
.post('/')
.set('Content-Type', 'application/json')
.send('true')
.expect(400, parseError('#rue').replace('#', 't'), done)
.expect(400, '[entity.parse.failed] ' + parseError('#rue').replace('#', 't'), done)
})
})

Expand Down Expand Up @@ -255,15 +235,15 @@ describe('bodyParser.json()', function () {
.post('/')
.set('Content-Type', 'application/json')
.send('true')
.expect(400, parseError('#rue').replace('#', 't'), done)
.expect(400, '[entity.parse.failed] ' + parseError('#rue').replace('#', 't'), done)
})

it('should not parse primitives with leading whitespaces', function (done) {
request(this.server)
.post('/')
.set('Content-Type', 'application/json')
.send(' true')
.expect(400, parseError(' #rue').replace('#', 't'), done)
.expect(400, '[entity.parse.failed] ' + parseError(' #rue').replace('#', 't'), done)
})

it('should allow leading whitespaces in JSON', function (done) {
Expand All @@ -274,15 +254,6 @@ describe('bodyParser.json()', function () {
.expect(200, '{"user":"tobi"}', done)
})

it('should error with type = "entity.parse.failed"', function (done) {
request(this.server)
.post('/')
.set('Content-Type', 'application/json')
.set('X-Error-Property', 'type')
.send('true')
.expect(400, 'entity.parse.failed', done)
})

it('should include correct message in stack trace', function (done) {
request(this.server)
.post('/')
Expand Down Expand Up @@ -409,22 +380,7 @@ describe('bodyParser.json()', function () {
.post('/')
.set('Content-Type', 'application/json')
.send('["tobi"]')
.expect(403, 'no arrays', done)
})

it('should error with type = "entity.verify.failed"', function (done) {
var server = createServer({
verify: function (req, res, buf) {
if (buf[0] === 0x5b) throw new Error('no arrays')
}
})

request(server)
.post('/')
.set('Content-Type', 'application/json')
.set('X-Error-Property', 'type')
.send('["tobi"]')
.expect(403, 'entity.verify.failed', done)
.expect(403, '[entity.verify.failed] no arrays', done)
})

it('should allow custom codes', function (done) {
Expand All @@ -441,7 +397,7 @@ describe('bodyParser.json()', function () {
.post('/')
.set('Content-Type', 'application/json')
.send('["tobi"]')
.expect(400, 'no arrays', done)
.expect(400, '[entity.verify.failed] no arrays', done)
})

it('should allow custom type', function (done) {
Expand All @@ -457,9 +413,8 @@ describe('bodyParser.json()', function () {
request(server)
.post('/')
.set('Content-Type', 'application/json')
.set('X-Error-Property', 'type')
.send('["tobi"]')
.expect(403, 'foo.bar', done)
.expect(403, '[foo.bar] no arrays', done)
})

it('should include original body on error object', function (done) {
Expand Down Expand Up @@ -514,7 +469,7 @@ describe('bodyParser.json()', function () {
var test = request(server).post('/')
test.set('Content-Type', 'application/json; charset=x-bogus')
test.write(Buffer.from('00000000', 'hex'))
test.expect(415, 'unsupported charset "X-BOGUS"', done)
test.expect(415, '[charset.unsupported] unsupported charset "X-BOGUS"', done)
})
})

Expand Down Expand Up @@ -556,15 +511,7 @@ describe('bodyParser.json()', function () {
var test = request(this.server).post('/')
test.set('Content-Type', 'application/json; charset=koi8-r')
test.write(Buffer.from('7b226e616d65223a22cec5d4227d', 'hex'))
test.expect(415, 'unsupported charset "KOI8-R"', done)
})

it('should error with type = "charset.unsupported"', function (done) {
var test = request(this.server).post('/')
test.set('Content-Type', 'application/json; charset=koi8-r')
test.set('X-Error-Property', 'type')
test.write(Buffer.from('7b226e616d65223a22cec5d4227d', 'hex'))
test.expect(415, 'charset.unsupported', done)
test.expect(415, '[charset.unsupported] unsupported charset "KOI8-R"', done)
})
})

Expand Down Expand Up @@ -617,16 +564,7 @@ describe('bodyParser.json()', function () {
test.set('Content-Encoding', 'nulls')
test.set('Content-Type', 'application/json')
test.write(Buffer.from('000000000000', 'hex'))
test.expect(415, 'unsupported content encoding "nulls"', done)
})

it('should error with type = "encoding.unsupported"', function (done) {
var test = request(this.server).post('/')
test.set('Content-Encoding', 'nulls')
test.set('Content-Type', 'application/json')
test.set('X-Error-Property', 'type')
test.write(Buffer.from('000000000000', 'hex'))
test.expect(415, 'encoding.unsupported', done)
test.expect(415, '[encoding.unsupported] unsupported content encoding "nulls"', done)
})

it('should 400 on malformed encoding', function (done) {
Expand Down Expand Up @@ -659,7 +597,9 @@ function createServer (opts) {
_bodyParser(req, res, function (err) {
if (err) {
res.statusCode = err.status || 500
res.end(err[req.headers['x-error-property'] || 'message'])
res.end(req.headers['x-error-property']
? err[req.headers['x-error-property']]
: ('[' + err.type + '] ' + err.message))
} else {
res.statusCode = 200
res.end(JSON.stringify(req.body))
Expand Down
10 changes: 5 additions & 5 deletions test/raw.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ describe('bodyParser.raw()', function () {
test.set('Content-Encoding', 'gzip')
test.set('Content-Type', 'application/octet-stream')
test.write(Buffer.from('1f8b080000000000000bcb4bcc4db57db16e170099a4bad608000000', 'hex'))
test.expect(415, 'content encoding unsupported', done)
test.expect(415, '[encoding.unsupported] content encoding unsupported', done)
})
})

Expand Down Expand Up @@ -266,7 +266,7 @@ describe('bodyParser.raw()', function () {
var test = request(server).post('/')
test.set('Content-Type', 'application/octet-stream')
test.write(Buffer.from('000102', 'hex'))
test.expect(403, 'no leading null', done)
test.expect(403, '[entity.verify.failed] no leading null', done)
})

it('should allow custom codes', function (done) {
Expand All @@ -282,7 +282,7 @@ describe('bodyParser.raw()', function () {
var test = request(server).post('/')
test.set('Content-Type', 'application/octet-stream')
test.write(Buffer.from('000102', 'hex'))
test.expect(400, 'no leading null', done)
test.expect(400, '[entity.verify.failed] no leading null', done)
})

it('should allow pass-through', function (done) {
Expand Down Expand Up @@ -361,7 +361,7 @@ describe('bodyParser.raw()', function () {
test.set('Content-Encoding', 'nulls')
test.set('Content-Type', 'application/octet-stream')
test.write(Buffer.from('000000000000', 'hex'))
test.expect(415, 'unsupported content encoding "nulls"', done)
test.expect(415, '[encoding.unsupported] unsupported content encoding "nulls"', done)
})
})
})
Expand All @@ -375,7 +375,7 @@ function createServer (opts) {
_bodyParser(req, res, function (err) {
if (err) {
res.statusCode = err.status || 500
res.end(err.message)
res.end('[' + err.type + '] ' + err.message)
return
}

Expand Down
14 changes: 7 additions & 7 deletions test/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ describe('bodyParser.text()', function () {
test.set('Content-Encoding', 'gzip')
test.set('Content-Type', 'text/plain')
test.write(Buffer.from('1f8b080000000000000bcb4bcc4d55c82c5678b16e170072b3e0200b000000', 'hex'))
test.expect(415, 'content encoding unsupported', done)
test.expect(415, '[encoding.unsupported] content encoding unsupported', done)
})
})

Expand Down Expand Up @@ -290,7 +290,7 @@ describe('bodyParser.text()', function () {
.post('/')
.set('Content-Type', 'text/plain')
.send(' user is tobi')
.expect(403, 'no leading space', done)
.expect(403, '[entity.verify.failed] no leading space', done)
})

it('should allow custom codes', function (done) {
Expand All @@ -307,7 +307,7 @@ describe('bodyParser.text()', function () {
.post('/')
.set('Content-Type', 'text/plain')
.send(' user is tobi')
.expect(400, 'no leading space', done)
.expect(400, '[entity.verify.failed] no leading space', done)
})

it('should allow pass-through', function (done) {
Expand All @@ -334,7 +334,7 @@ describe('bodyParser.text()', function () {
var test = request(server).post('/')
test.set('Content-Type', 'text/plain; charset=x-bogus')
test.write(Buffer.from('00000000', 'hex'))
test.expect(415, 'unsupported charset "X-BOGUS"', done)
test.expect(415, '[charset.unsupported] unsupported charset "X-BOGUS"', done)
})
})

Expand Down Expand Up @@ -376,7 +376,7 @@ describe('bodyParser.text()', function () {
var test = request(this.server).post('/')
test.set('Content-Type', 'text/plain; charset=x-bogus')
test.write(Buffer.from('00000000', 'hex'))
test.expect(415, 'unsupported charset "X-BOGUS"', done)
test.expect(415, '[charset.unsupported] unsupported charset "X-BOGUS"', done)
})
})

Expand Down Expand Up @@ -429,7 +429,7 @@ describe('bodyParser.text()', function () {
test.set('Content-Encoding', 'nulls')
test.set('Content-Type', 'text/plain')
test.write(Buffer.from('000000000000', 'hex'))
test.expect(415, 'unsupported content encoding "nulls"', done)
test.expect(415, '[encoding.unsupported] unsupported content encoding "nulls"', done)
})
})
})
Expand All @@ -442,7 +442,7 @@ 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))
res.end(err ? ('[' + err.type + '] ' + err.message) : JSON.stringify(req.body))
})
})
}
Loading

0 comments on commit 09dcbd9

Please sign in to comment.