@@ -86,8 +86,12 @@ describe('bodyParser.json()', function () {
86
86
} )
87
87
88
88
describe ( 'when strict is false' , function ( ) {
89
+ before ( function ( ) {
90
+ this . server = createServer ( { strict : false } )
91
+ } )
92
+
89
93
it ( 'should parse primitives' , function ( done ) {
90
- request ( createServer ( { strict : false } ) )
94
+ request ( this . server )
91
95
. post ( '/' )
92
96
. set ( 'Content-Type' , 'application/json' )
93
97
. send ( 'true' )
@@ -96,29 +100,28 @@ describe('bodyParser.json()', function () {
96
100
} )
97
101
98
102
describe ( 'when strict is true' , function ( ) {
99
- var server
100
103
before ( function ( ) {
101
- server = createServer ( { strict : true } )
104
+ this . server = createServer ( { strict : true } )
102
105
} )
103
106
104
107
it ( 'should not parse primitives' , function ( done ) {
105
- request ( server )
108
+ request ( this . server )
106
109
. post ( '/' )
107
110
. set ( 'Content-Type' , 'application/json' )
108
111
. send ( 'true' )
109
112
. expect ( 400 , parseError ( '#rue' ) . replace ( '#' , 't' ) , done )
110
113
} )
111
114
112
115
it ( 'should not parse primitives with leading whitespaces' , function ( done ) {
113
- request ( server )
116
+ request ( this . server )
114
117
. post ( '/' )
115
118
. set ( 'Content-Type' , 'application/json' )
116
119
. send ( ' true' )
117
120
. expect ( 400 , parseError ( ' #rue' ) . replace ( '#' , 't' ) , done )
118
121
} )
119
122
120
123
it ( 'should allow leading whitespaces in JSON' , function ( done ) {
121
- request ( server )
124
+ request ( this . server )
122
125
. post ( '/' )
123
126
. set ( 'Content-Type' , 'application/json' )
124
127
. send ( ' { "user": "tobi" }' )
@@ -127,8 +130,12 @@ describe('bodyParser.json()', function () {
127
130
} )
128
131
129
132
describe ( 'by default' , function ( ) {
133
+ before ( function ( ) {
134
+ this . server = createServer ( )
135
+ } )
136
+
130
137
it ( 'should 400 on primitives' , function ( done ) {
131
- request ( createServer ( ) )
138
+ request ( this . server )
132
139
. post ( '/' )
133
140
. set ( 'Content-Type' , 'application/json' )
134
141
. send ( 'true' )
@@ -195,13 +202,12 @@ describe('bodyParser.json()', function () {
195
202
196
203
describe ( 'with inflate option' , function ( ) {
197
204
describe ( 'when false' , function ( ) {
198
- var server
199
205
before ( function ( ) {
200
- server = createServer ( { inflate : false } )
206
+ this . server = createServer ( { inflate : false } )
201
207
} )
202
208
203
209
it ( 'should not accept content-encoding' , function ( done ) {
204
- var test = request ( server ) . post ( '/' )
210
+ var test = request ( this . server ) . post ( '/' )
205
211
test . set ( 'Content-Encoding' , 'gzip' )
206
212
test . set ( 'Content-Type' , 'application/json' )
207
213
test . write ( Buffer . from ( '1f8b080000000000000bab56ca4bcc4d55b2527ab16e97522d00515be1cc0e000000' , 'hex' ) )
@@ -210,13 +216,12 @@ describe('bodyParser.json()', function () {
210
216
} )
211
217
212
218
describe ( 'when true' , function ( ) {
213
- var server
214
219
before ( function ( ) {
215
- server = createServer ( { inflate : true } )
220
+ this . server = createServer ( { inflate : true } )
216
221
} )
217
222
218
223
it ( 'should accept content-encoding' , function ( done ) {
219
- var test = request ( server ) . post ( '/' )
224
+ var test = request ( this . server ) . post ( '/' )
220
225
test . set ( 'Content-Encoding' , 'gzip' )
221
226
test . set ( 'Content-Type' , 'application/json' )
222
227
test . write ( Buffer . from ( '1f8b080000000000000bab56ca4bcc4d55b2527ab16e97522d00515be1cc0e000000' , 'hex' ) )
@@ -227,21 +232,20 @@ describe('bodyParser.json()', function () {
227
232
228
233
describe ( 'with type option' , function ( ) {
229
234
describe ( 'when "application/vnd.api+json"' , function ( ) {
230
- var server
231
235
before ( function ( ) {
232
- server = createServer ( { type : 'application/vnd.api+json' } )
236
+ this . server = createServer ( { type : 'application/vnd.api+json' } )
233
237
} )
234
238
235
239
it ( 'should parse JSON for custom type' , function ( done ) {
236
- request ( server )
240
+ request ( this . server )
237
241
. post ( '/' )
238
242
. set ( 'Content-Type' , 'application/vnd.api+json' )
239
243
. send ( '{"user":"tobi"}' )
240
244
. expect ( 200 , '{"user":"tobi"}' , done )
241
245
} )
242
246
243
247
it ( 'should ignore standard type' , function ( done ) {
244
- request ( server )
248
+ request ( this . server )
245
249
. post ( '/' )
246
250
. set ( 'Content-Type' , 'application/json' )
247
251
. send ( '{"user":"tobi"}' )
@@ -359,103 +363,101 @@ describe('bodyParser.json()', function () {
359
363
} )
360
364
361
365
describe ( 'charset' , function ( ) {
362
- var server
363
366
before ( function ( ) {
364
- server = createServer ( )
367
+ this . server = createServer ( )
365
368
} )
366
369
367
370
it ( 'should parse utf-8' , function ( done ) {
368
- var test = request ( server ) . post ( '/' )
371
+ var test = request ( this . server ) . post ( '/' )
369
372
test . set ( 'Content-Type' , 'application/json; charset=utf-8' )
370
373
test . write ( Buffer . from ( '7b226e616d65223a22e8aeba227d' , 'hex' ) )
371
374
test . expect ( 200 , '{"name":"论"}' , done )
372
375
} )
373
376
374
377
it ( 'should parse utf-16' , function ( done ) {
375
- var test = request ( server ) . post ( '/' )
378
+ var test = request ( this . server ) . post ( '/' )
376
379
test . set ( 'Content-Type' , 'application/json; charset=utf-16' )
377
380
test . write ( Buffer . from ( 'feff007b0022006e0061006d00650022003a00228bba0022007d' , 'hex' ) )
378
381
test . expect ( 200 , '{"name":"论"}' , done )
379
382
} )
380
383
381
384
it ( 'should parse when content-length != char length' , function ( done ) {
382
- var test = request ( server ) . post ( '/' )
385
+ var test = request ( this . server ) . post ( '/' )
383
386
test . set ( 'Content-Type' , 'application/json; charset=utf-8' )
384
387
test . set ( 'Content-Length' , '13' )
385
388
test . write ( Buffer . from ( '7b2274657374223a22c3a5227d' , 'hex' ) )
386
389
test . expect ( 200 , '{"test":"å"}' , done )
387
390
} )
388
391
389
392
it ( 'should default to utf-8' , function ( done ) {
390
- var test = request ( server ) . post ( '/' )
393
+ var test = request ( this . server ) . post ( '/' )
391
394
test . set ( 'Content-Type' , 'application/json' )
392
395
test . write ( Buffer . from ( '7b226e616d65223a22e8aeba227d' , 'hex' ) )
393
396
test . expect ( 200 , '{"name":"论"}' , done )
394
397
} )
395
398
396
399
it ( 'should fail on unknown charset' , function ( done ) {
397
- var test = request ( server ) . post ( '/' )
400
+ var test = request ( this . server ) . post ( '/' )
398
401
test . set ( 'Content-Type' , 'application/json; charset=koi8-r' )
399
402
test . write ( Buffer . from ( '7b226e616d65223a22cec5d4227d' , 'hex' ) )
400
403
test . expect ( 415 , 'unsupported charset "KOI8-R"' , done )
401
404
} )
402
405
} )
403
406
404
407
describe ( 'encoding' , function ( ) {
405
- var server
406
408
before ( function ( ) {
407
- server = createServer ( { limit : '1kb' } )
409
+ this . server = createServer ( { limit : '1kb' } )
408
410
} )
409
411
410
412
it ( 'should parse without encoding' , function ( done ) {
411
- var test = request ( server ) . post ( '/' )
413
+ var test = request ( this . server ) . post ( '/' )
412
414
test . set ( 'Content-Type' , 'application/json' )
413
415
test . write ( Buffer . from ( '7b226e616d65223a22e8aeba227d' , 'hex' ) )
414
416
test . expect ( 200 , '{"name":"论"}' , done )
415
417
} )
416
418
417
419
it ( 'should support identity encoding' , function ( done ) {
418
- var test = request ( server ) . post ( '/' )
420
+ var test = request ( this . server ) . post ( '/' )
419
421
test . set ( 'Content-Encoding' , 'identity' )
420
422
test . set ( 'Content-Type' , 'application/json' )
421
423
test . write ( Buffer . from ( '7b226e616d65223a22e8aeba227d' , 'hex' ) )
422
424
test . expect ( 200 , '{"name":"论"}' , done )
423
425
} )
424
426
425
427
it ( 'should support gzip encoding' , function ( done ) {
426
- var test = request ( server ) . post ( '/' )
428
+ var test = request ( this . server ) . post ( '/' )
427
429
test . set ( 'Content-Encoding' , 'gzip' )
428
430
test . set ( 'Content-Type' , 'application/json' )
429
431
test . write ( Buffer . from ( '1f8b080000000000000bab56ca4bcc4d55b2527ab16e97522d00515be1cc0e000000' , 'hex' ) )
430
432
test . expect ( 200 , '{"name":"论"}' , done )
431
433
} )
432
434
433
435
it ( 'should support deflate encoding' , function ( done ) {
434
- var test = request ( server ) . post ( '/' )
436
+ var test = request ( this . server ) . post ( '/' )
435
437
test . set ( 'Content-Encoding' , 'deflate' )
436
438
test . set ( 'Content-Type' , 'application/json' )
437
439
test . write ( Buffer . from ( '789cab56ca4bcc4d55b2527ab16e97522d00274505ac' , 'hex' ) )
438
440
test . expect ( 200 , '{"name":"论"}' , done )
439
441
} )
440
442
441
443
it ( 'should be case-insensitive' , function ( done ) {
442
- var test = request ( server ) . post ( '/' )
444
+ var test = request ( this . server ) . post ( '/' )
443
445
test . set ( 'Content-Encoding' , 'GZIP' )
444
446
test . set ( 'Content-Type' , 'application/json' )
445
447
test . write ( Buffer . from ( '1f8b080000000000000bab56ca4bcc4d55b2527ab16e97522d00515be1cc0e000000' , 'hex' ) )
446
448
test . expect ( 200 , '{"name":"论"}' , done )
447
449
} )
448
450
449
451
it ( 'should 415 on unknown encoding' , function ( done ) {
450
- var test = request ( server ) . post ( '/' )
452
+ var test = request ( this . server ) . post ( '/' )
451
453
test . set ( 'Content-Encoding' , 'nulls' )
452
454
test . set ( 'Content-Type' , 'application/json' )
453
455
test . write ( Buffer . from ( '000000000000' , 'hex' ) )
454
456
test . expect ( 415 , 'unsupported content encoding "nulls"' , done )
455
457
} )
456
458
457
459
it ( 'should 400 on malformed encoding' , function ( done ) {
458
- var test = request ( server ) . post ( '/' )
460
+ var test = request ( this . server ) . post ( '/' )
459
461
test . set ( 'Content-Encoding' , 'gzip' )
460
462
test . set ( 'Content-Type' , 'application/json' )
461
463
test . write ( Buffer . from ( '1f8b080000000000000bab56cc4d55b2527ab16e97522d00515be1cc0e000000' , 'hex' ) )
@@ -464,7 +466,7 @@ describe('bodyParser.json()', function () {
464
466
465
467
it ( 'should 413 when inflated value exceeds limit' , function ( done ) {
466
468
// gzip'd data exceeds 1kb, but deflated below 1kb
467
- var test = request ( server ) . post ( '/' )
469
+ var test = request ( this . server ) . post ( '/' )
468
470
test . set ( 'Content-Encoding' , 'gzip' )
469
471
test . set ( 'Content-Type' , 'application/json' )
470
472
test . write ( Buffer . from ( '1f8b080000000000000bedc1010d000000c2a0f74f6d0f071400000000000000' , 'hex' ) )
0 commit comments