@@ -90,6 +90,15 @@ describe('bodyParser.json()', function () {
9090 . expect ( 400 , parseError ( '{"user"' ) , done )
9191 } )
9292
93+ it ( 'should error with type = "entity.parse.failed"' , function ( done ) {
94+ request ( this . server )
95+ . post ( '/' )
96+ . set ( 'Content-Type' , 'application/json' )
97+ . set ( 'X-Error-Property' , 'type' )
98+ . send ( ' {"user"' )
99+ . expect ( 400 , 'entity.parse.failed' , done )
100+ } )
101+
93102 it ( 'should include original body on error object' , function ( done ) {
94103 request ( this . server )
95104 . post ( '/' )
@@ -111,6 +120,17 @@ describe('bodyParser.json()', function () {
111120 . expect ( 413 , done )
112121 } )
113122
123+ it ( 'should error with type = "entity.too.large"' , function ( done ) {
124+ var buf = Buffer . alloc ( 1024 , '.' )
125+ request ( createServer ( { limit : '1kb' } ) )
126+ . post ( '/' )
127+ . set ( 'Content-Type' , 'application/json' )
128+ . set ( 'Content-Length' , '1034' )
129+ . set ( 'X-Error-Property' , 'type' )
130+ . send ( JSON . stringify ( { str : buf . toString ( ) } ) )
131+ . expect ( 413 , 'entity.too.large' , done )
132+ } )
133+
114134 it ( 'should 413 when over limit with chunked encoding' , function ( done ) {
115135 var buf = Buffer . alloc ( 1024 , '.' )
116136 var server = createServer ( { limit : '1kb' } )
@@ -244,6 +264,15 @@ describe('bodyParser.json()', function () {
244264 . send ( ' { "user": "tobi" }' )
245265 . expect ( 200 , '{"user":"tobi"}' , done )
246266 } )
267+
268+ it ( 'should error with type = "entity.parse.failed"' , function ( done ) {
269+ request ( this . server )
270+ . post ( '/' )
271+ . set ( 'Content-Type' , 'application/json' )
272+ . set ( 'X-Error-Property' , 'type' )
273+ . send ( 'true' )
274+ . expect ( 400 , 'entity.parse.failed' , done )
275+ } )
247276 } )
248277 } )
249278
@@ -329,6 +358,19 @@ describe('bodyParser.json()', function () {
329358 . expect ( 403 , 'no arrays' , done )
330359 } )
331360
361+ it ( 'should error with type = "entity.verify.failed"' , function ( done ) {
362+ var server = createServer ( { verify : function ( req , res , buf ) {
363+ if ( buf [ 0 ] === 0x5b ) throw new Error ( 'no arrays' )
364+ } } )
365+
366+ request ( server )
367+ . post ( '/' )
368+ . set ( 'Content-Type' , 'application/json' )
369+ . set ( 'X-Error-Property' , 'type' )
370+ . send ( '["tobi"]' )
371+ . expect ( 403 , 'entity.verify.failed' , done )
372+ } )
373+
332374 it ( 'should allow custom codes' , function ( done ) {
333375 var server = createServer ( { verify : function ( req , res , buf ) {
334376 if ( buf [ 0 ] !== 0x5b ) return
@@ -344,6 +386,22 @@ describe('bodyParser.json()', function () {
344386 . expect ( 400 , 'no arrays' , done )
345387 } )
346388
389+ it ( 'should allow custom type' , function ( done ) {
390+ var server = createServer ( { verify : function ( req , res , buf ) {
391+ if ( buf [ 0 ] !== 0x5b ) return
392+ var err = new Error ( 'no arrays' )
393+ err . type = 'foo.bar'
394+ throw err
395+ } } )
396+
397+ request ( server )
398+ . post ( '/' )
399+ . set ( 'Content-Type' , 'application/json' )
400+ . set ( 'X-Error-Property' , 'type' )
401+ . send ( '["tobi"]' )
402+ . expect ( 403 , 'foo.bar' , done )
403+ } )
404+
347405 it ( 'should include original body on error object' , function ( done ) {
348406 var server = createServer ( { verify : function ( req , res , buf ) {
349407 if ( buf [ 0 ] === 0x5b ) throw new Error ( 'no arrays' )
@@ -432,6 +490,14 @@ describe('bodyParser.json()', function () {
432490 test . write ( Buffer . from ( '7b226e616d65223a22cec5d4227d' , 'hex' ) )
433491 test . expect ( 415 , 'unsupported charset "KOI8-R"' , done )
434492 } )
493+
494+ it ( 'should error with type = "charset.unsupported"' , function ( done ) {
495+ var test = request ( this . server ) . post ( '/' )
496+ test . set ( 'Content-Type' , 'application/json; charset=koi8-r' )
497+ test . set ( 'X-Error-Property' , 'type' )
498+ test . write ( Buffer . from ( '7b226e616d65223a22cec5d4227d' , 'hex' ) )
499+ test . expect ( 415 , 'charset.unsupported' , done )
500+ } )
435501 } )
436502
437503 describe ( 'encoding' , function ( ) {
@@ -486,6 +552,15 @@ describe('bodyParser.json()', function () {
486552 test . expect ( 415 , 'unsupported content encoding "nulls"' , done )
487553 } )
488554
555+ it ( 'should error with type = "encoding.unsupported"' , function ( done ) {
556+ var test = request ( this . server ) . post ( '/' )
557+ test . set ( 'Content-Encoding' , 'nulls' )
558+ test . set ( 'Content-Type' , 'application/json' )
559+ test . set ( 'X-Error-Property' , 'type' )
560+ test . write ( Buffer . from ( '000000000000' , 'hex' ) )
561+ test . expect ( 415 , 'encoding.unsupported' , done )
562+ } )
563+
489564 it ( 'should 400 on malformed encoding' , function ( done ) {
490565 var test = request ( this . server ) . post ( '/' )
491566 test . set ( 'Content-Encoding' , 'gzip' )
0 commit comments