Skip to content
Myr

express.json.js

This file

Path

test/express.json.js

754 lines · 23.1 KiB · JavaScript

Last changed by

fix(refactor): prefix built-in node module importsSzymon Łągiewkabefore adoption4111359

History of this file

Take it

Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b

line 1why, one step back
1'use strict'

Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99

line 2why, one step back
2

fix(refactor): prefix built-in node module importsSzymon Łągiewkabefore adoption4111359

lines 3–4why, one step back
3var assert = require('node:assert')
4var AsyncLocalStorage = require('node:async_hooks').AsyncLocalStorage

cleanup: remove AsyncLocalStorage check from tests (#6147)Phillip Bartabefore adoption4e92ac9

line 5why, one step back
5

Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99

lines 6–41why, one step back
6var express = require('..')
7var request = require('supertest')89describe('express.json()', function () {10 it('should parse JSON', function (done) {11 request(createApp())12 .post('/')13 .set('Content-Type', 'application/json')14 .send('{"user":"tobi"}')15 .expect(200, '{"user":"tobi"}', done)16 })1718 it('should handle Content-Length: 0', function (done) {19 request(createApp())20 .post('/')21 .set('Content-Type', 'application/json')22 .set('Content-Length', '0')23 .expect(200, '{}', done)24 })2526 it('should handle empty message-body', function (done) {27 request(createApp())28 .post('/')29 .set('Content-Type', 'application/json')30 .set('Transfer-Encoding', 'chunked')31 .expect(200, '{}', done)32 })3334 it('should handle no message-body', function (done) {35 request(createApp())36 .post('/')37 .set('Content-Type', 'application/json')38 .unset('Transfer-Encoding')39 .expect(200, '{}', done)40 })41

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 42–50why, one step back
42 // The old node error message modification in body parser is catching this
43 it('should 400 when only whitespace', function (done) {44 request(createApp())45 .post('/')46 .set('Content-Type', 'application/json')47 .send(' \n')48 .expect(400, '[entity.parse.failed] ' + parseError(' \n'), done)49 })50

Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99

lines 51–98why, one step back
51 it('should 400 when invalid content-length', function (done) {
52 var app = express()5354 app.use(function (req, res, next) {55 req.headers['content-length'] = '20' // bad length56 next()57 })5859 app.use(express.json())6061 app.post('/', function (req, res) {62 res.json(req.body)63 })6465 request(app)66 .post('/')67 .set('Content-Type', 'application/json')68 .send('{"str":')69 .expect(400, /content length/, done)70 })7172 it('should handle duplicated middleware', function (done) {73 var app = express()7475 app.use(express.json())76 app.use(express.json())7778 app.post('/', function (req, res) {79 res.json(req.body)80 })8182 request(app)83 .post('/')84 .set('Content-Type', 'application/json')85 .send('{"user":"tobi"}')86 .expect(200, '{"user":"tobi"}', done)87 })8889 describe('when JSON is invalid', function () {90 before(function () {91 this.app = createApp()92 })9394 it('should 400 for bad token', function (done) {95 request(this.app)96 .post('/')97 .set('Content-Type', 'application/json')98 .send('{:')

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

line 99why, one step back
99 .expect(400, '[entity.parse.failed] ' + parseError('{:'), done)

Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99

lines 100–106why, one step back
100 })
101102 it('should 400 for incomplete', function (done) {103 request(this.app)104 .post('/')105 .set('Content-Type', 'application/json')106 .send('{"user"')

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

line 107why, one step back
107 .expect(400, '[entity.parse.failed] ' + parseError('{"user"'), done)

Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99

lines 108–127why, one step back
108 })
109110 it('should include original body on error object', function (done) {111 request(this.app)112 .post('/')113 .set('Content-Type', 'application/json')114 .set('X-Error-Property', 'body')115 .send(' {"user"')116 .expect(400, ' {"user"', done)117 })118 })119120 describe('with limit option', function () {121 it('should 413 when over limit with Content-Length', function (done) {122 var buf = Buffer.alloc(1024, '.')123 request(createApp({ limit: '1kb' }))124 .post('/')125 .set('Content-Type', 'application/json')126 .set('Content-Length', '1034')127 .send(JSON.stringify({ str: buf.toString() }))

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

line 128why, one step back
128 .expect(413, '[entity.too.large] request entity too large', done)

Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99

lines 129–131why, one step back
129 })
130131 it('should 413 when over limit with chunked encoding', function (done) {

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

line 132why, one step back
132 var app = createApp({ limit: '1kb' })

Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99

line 133why, one step back
133 var buf = Buffer.alloc(1024, '.')

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

line 134why, one step back
134 var test = request(app).post('/')

Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99

lines 135–141why, one step back
135 test.set('Content-Type', 'application/json')
136 test.set('Transfer-Encoding', 'chunked')137 test.write('{"str":')138 test.write('"' + buf.toString() + '"}')139 test.expect(413, done)140 })141

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 142–150why, one step back
142 it('should 413 when inflated body over limit', function (done) {
143 var app = createApp({ limit: '1kb' })144 var test = request(app).post('/')145 test.set('Content-Encoding', 'gzip')146 test.set('Content-Type', 'application/json')147 test.write(Buffer.from('1f8b080000000000000aab562a2e2952b252d21b05a360148c58a0540b0066f7ce1e0a040000', 'hex'))148 test.expect(413, done)149 })150

Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99

lines 151–162why, one step back
151 it('should accept number of bytes', function (done) {
152 var buf = Buffer.alloc(1024, '.')153 request(createApp({ limit: 1024 }))154 .post('/')155 .set('Content-Type', 'application/json')156 .send(JSON.stringify({ str: buf.toString() }))157 .expect(413, done)158 })159160 it('should not change when options altered', function (done) {161 var buf = Buffer.alloc(1024, '.')162 var options = { limit: '1kb' }

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

line 163why, one step back
163 var app = createApp(options)

Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99

lines 164–166why, one step back
164
165 options.limit = '100kb'166

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

line 167why, one step back
167 request(app)

Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99

lines 168–175why, one step back
168 .post('/')
169 .set('Content-Type', 'application/json')170 .send(JSON.stringify({ str: buf.toString() }))171 .expect(413, done)172 })173174 it('should not hang response', function (done) {175 var buf = Buffer.alloc(10240, '.')

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 176–177why, one step back
176 var app = createApp({ limit: '8kb' })
177 var test = request(app).post('/')

Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99

lines 178–183why, one step back
178 test.set('Content-Type', 'application/json')
179 test.write(buf)180 test.write(buf)181 test.write(buf)182 test.expect(413, done)183 })

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 184–192why, one step back
184
185 it('should not error when inflating', function (done) {186 var app = createApp({ limit: '1kb' })187 var test = request(app).post('/')188 test.set('Content-Encoding', 'gzip')189 test.set('Content-Type', 'application/json')190 test.write(Buffer.from('1f8b080000000000000aab562a2e2952b252d21b05a360148c58a0540b0066f7ce1e0a0400', 'hex'))191 test.expect(413, done)192 })

Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99

lines 193–205why, one step back
193 })
194195 describe('with inflate option', function () {196 describe('when false', function () {197 before(function () {198 this.app = createApp({ inflate: false })199 })200201 it('should not accept content-encoding', function (done) {202 var test = request(this.app).post('/')203 test.set('Content-Encoding', 'gzip')204 test.set('Content-Type', 'application/json')205 test.write(Buffer.from('1f8b080000000000000bab56ca4bcc4d55b2527ab16e97522d00515be1cc0e000000', 'hex'))

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

line 206why, one step back
206 test.expect(415, '[encoding.unsupported] content encoding unsupported', done)

Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99

lines 207–235why, one step back
207 })
208 })209210 describe('when true', function () {211 before(function () {212 this.app = createApp({ inflate: true })213 })214215 it('should accept content-encoding', function (done) {216 var test = request(this.app).post('/')217 test.set('Content-Encoding', 'gzip')218 test.set('Content-Type', 'application/json')219 test.write(Buffer.from('1f8b080000000000000bab56ca4bcc4d55b2527ab16e97522d00515be1cc0e000000', 'hex'))220 test.expect(200, '{"name":"论"}', done)221 })222 })223 })224225 describe('with strict option', function () {226 describe('when undefined', function () {227 before(function () {228 this.app = createApp()229 })230231 it('should 400 on primitives', function (done) {232 request(this.app)233 .post('/')234 .set('Content-Type', 'application/json')235 .send('true')

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

line 236why, one step back
236 .expect(400, '[entity.parse.failed] ' + parseError('#rue').replace(/#/g, 't'), done)

Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99

lines 237–263why, one step back
237 })
238 })239240 describe('when false', function () {241 before(function () {242 this.app = createApp({ strict: false })243 })244245 it('should parse primitives', function (done) {246 request(this.app)247 .post('/')248 .set('Content-Type', 'application/json')249 .send('true')250 .expect(200, 'true', done)251 })252 })253254 describe('when true', function () {255 before(function () {256 this.app = createApp({ strict: true })257 })258259 it('should not parse primitives', function (done) {260 request(this.app)261 .post('/')262 .set('Content-Type', 'application/json')263 .send('true')

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

line 264why, one step back
264 .expect(400, '[entity.parse.failed] ' + parseError('#rue').replace(/#/g, 't'), done)

Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99

lines 265–271why, one step back
265 })
266267 it('should not parse primitives with leading whitespaces', function (done) {268 request(this.app)269 .post('/')270 .set('Content-Type', 'application/json')271 .send(' true')

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

line 272why, one step back
272 .expect(400, '[entity.parse.failed] ' + parseError(' #rue').replace(/#/g, 't'), done)

Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99

lines 273–289why, one step back
273 })
274275 it('should allow leading whitespaces in JSON', function (done) {276 request(this.app)277 .post('/')278 .set('Content-Type', 'application/json')279 .send(' { "user": "tobi" }')280 .expect(200, '{"user":"tobi"}', done)281 })282283 it('should include correct message in stack trace', function (done) {284 request(this.app)285 .post('/')286 .set('Content-Type', 'application/json')287 .set('X-Error-Property', 'stack')288 .send('true')289 .expect(400)

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

line 290why, one step back
290 .expect(shouldContainInBody(parseError('#rue').replace(/#/g, 't')))

Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99

lines 291–314why, one step back
291 .end(done)
292 })293 })294 })295296 describe('with type option', function () {297 describe('when "application/vnd.api+json"', function () {298 before(function () {299 this.app = createApp({ type: 'application/vnd.api+json' })300 })301302 it('should parse JSON for custom type', function (done) {303 request(this.app)304 .post('/')305 .set('Content-Type', 'application/vnd.api+json')306 .send('{"user":"tobi"}')307 .expect(200, '{"user":"tobi"}', done)308 })309310 it('should ignore standard type', function (done) {311 request(this.app)312 .post('/')313 .set('Content-Type', 'application/json')314 .send('{"user":"tobi"}')

deps: body-parser@2.0.0-beta.1Douglas Christopher Wilsonbefore adoptionaf341b0

line 315why, one step back
315 .expect(200, '', done)

Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99

lines 316–346why, one step back
316 })
317 })318319 describe('when ["application/json", "application/vnd.api+json"]', function () {320 before(function () {321 this.app = createApp({322 type: ['application/json', 'application/vnd.api+json']323 })324 })325326 it('should parse JSON for "application/json"', function (done) {327 request(this.app)328 .post('/')329 .set('Content-Type', 'application/json')330 .send('{"user":"tobi"}')331 .expect(200, '{"user":"tobi"}', done)332 })333334 it('should parse JSON for "application/vnd.api+json"', function (done) {335 request(this.app)336 .post('/')337 .set('Content-Type', 'application/vnd.api+json')338 .send('{"user":"tobi"}')339 .expect(200, '{"user":"tobi"}', done)340 })341342 it('should ignore "application/x-json"', function (done) {343 request(this.app)344 .post('/')345 .set('Content-Type', 'application/x-json')346 .send('{"user":"tobi"}')

deps: body-parser@2.0.0-beta.1Douglas Christopher Wilsonbefore adoptionaf341b0

line 347why, one step back
347 .expect(200, '', done)

Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99

lines 348–398why, one step back
348 })
349 })350351 describe('when a function', function () {352 it('should parse when truthy value returned', function (done) {353 var app = createApp({ type: accept })354355 function accept (req) {356 return req.headers['content-type'] === 'application/vnd.api+json'357 }358359 request(app)360 .post('/')361 .set('Content-Type', 'application/vnd.api+json')362 .send('{"user":"tobi"}')363 .expect(200, '{"user":"tobi"}', done)364 })365366 it('should work without content-type', function (done) {367 var app = createApp({ type: accept })368369 function accept (req) {370 return true371 }372373 var test = request(app).post('/')374 test.write('{"user":"tobi"}')375 test.expect(200, '{"user":"tobi"}', done)376 })377378 it('should not invoke without a body', function (done) {379 var app = createApp({ type: accept })380381 function accept (req) {382 throw new Error('oops!')383 }384385 request(app)386 .get('/')387 .expect(404, done)388 })389 })390 })391392 describe('with verify option', function () {393 it('should assert value if function', function () {394 assert.throws(createApp.bind(null, { verify: 'lol' }),395 /TypeError: option verify must be function/)396 })397398 it('should error from verify', function (done) {

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 399–403why, one step back
399 var app = createApp({
400 verify: function (req, res, buf) {401 if (buf[0] === 0x5b) throw new Error('no arrays')402 }403 })

Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99

lines 404–408why, one step back
404
405 request(app)406 .post('/')407 .set('Content-Type', 'application/json')408 .send('["tobi"]')

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

line 409why, one step back
409 .expect(403, '[entity.verify.failed] no arrays', done)

Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99

lines 410–412why, one step back
410 })
411412 it('should allow custom codes', function (done) {

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 413–420why, one step back
413 var app = createApp({
414 verify: function (req, res, buf) {415 if (buf[0] !== 0x5b) return416 var err = new Error('no arrays')417 err.status = 400418 throw err419 }420 })

Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99

lines 421–425why, one step back
421
422 request(app)423 .post('/')424 .set('Content-Type', 'application/json')425 .send('["tobi"]')

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

line 426why, one step back
426 .expect(400, '[entity.verify.failed] no arrays', done)

Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99

lines 427–429why, one step back
427 })
428429 it('should allow custom type', function (done) {

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 430–437why, one step back
430 var app = createApp({
431 verify: function (req, res, buf) {432 if (buf[0] !== 0x5b) return433 var err = new Error('no arrays')434 err.type = 'foo.bar'435 throw err436 }437 })

Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99

lines 438–442why, one step back
438
439 request(app)440 .post('/')441 .set('Content-Type', 'application/json')442 .send('["tobi"]')

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

line 443why, one step back
443 .expect(403, '[foo.bar] no arrays', done)

Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99

lines 444–446why, one step back
444 })
445446 it('should include original body on error object', function (done) {

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 447–451why, one step back
447 var app = createApp({
448 verify: function (req, res, buf) {449 if (buf[0] === 0x5b) throw new Error('no arrays')450 }451 })

Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99

lines 452–461why, one step back
452
453 request(app)454 .post('/')455 .set('Content-Type', 'application/json')456 .set('X-Error-Property', 'body')457 .send('["tobi"]')458 .expect(403, '["tobi"]', done)459 })460461 it('should allow pass-through', function (done) {

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 462–466why, one step back
462 var app = createApp({
463 verify: function (req, res, buf) {464 if (buf[0] === 0x5b) throw new Error('no arrays')465 }466 })

Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99

lines 467–475why, one step back
467
468 request(app)469 .post('/')470 .set('Content-Type', 'application/json')471 .send('{"user":"tobi"}')472 .expect(200, '{"user":"tobi"}', done)473 })474475 it('should work with different charsets', function (done) {

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 476–480why, one step back
476 var app = createApp({
477 verify: function (req, res, buf) {478 if (buf[0] === 0x5b) throw new Error('no arrays')479 }480 })

Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99

lines 481–488why, one step back
481
482 var test = request(app).post('/')483 test.set('Content-Type', 'application/json; charset=utf-16')484 test.write(Buffer.from('feff007b0022006e0061006d00650022003a00228bba0022007d', 'hex'))485 test.expect(200, '{"name":"论"}', done)486 })487488 it('should 415 on unknown charset prior to verify', function (done) {

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 489–493why, one step back
489 var app = createApp({
490 verify: function (req, res, buf) {491 throw new Error('unexpected verify call')492 }493 })

Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99

lines 494–497why, one step back
494
495 var test = request(app).post('/')496 test.set('Content-Type', 'application/json; charset=x-bogus')497 test.write(Buffer.from('00000000', 'hex'))

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

line 498why, one step back
498 test.expect(415, '[charset.unsupported] unsupported charset "X-BOGUS"', done)

Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99

lines 499–501why, one step back
499 })
500 })501

cleanup: remove AsyncLocalStorage check from tests (#6147)Phillip Bartabefore adoption4e92ac9

line 502why, one step back
502 describe('async local storage', function () {

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 503–507why, one step back
503 before(function () {
504 var app = express()505 var store = { foo: 'bar' }506507 app.use(function (req, res, next) {

cleanup: remove AsyncLocalStorage check from tests (#6147)Phillip Bartabefore adoption4e92ac9

line 508why, one step back
508 req.asyncLocalStorage = new AsyncLocalStorage()

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 509–605why, one step back
509 req.asyncLocalStorage.run(store, next)
510 })511512 app.use(express.json())513514 app.use(function (req, res, next) {515 var local = req.asyncLocalStorage.getStore()516517 if (local) {518 res.setHeader('x-store-foo', String(local.foo))519 }520521 next()522 })523524 app.use(function (err, req, res, next) {525 var local = req.asyncLocalStorage.getStore()526527 if (local) {528 res.setHeader('x-store-foo', String(local.foo))529 }530531 res.status(err.status || 500)532 res.send('[' + err.type + '] ' + err.message)533 })534535 app.post('/', function (req, res) {536 res.json(req.body)537 })538539 this.app = app540 })541542 it('should presist store', function (done) {543 request(this.app)544 .post('/')545 .set('Content-Type', 'application/json')546 .send('{"user":"tobi"}')547 .expect(200)548 .expect('x-store-foo', 'bar')549 .expect('{"user":"tobi"}')550 .end(done)551 })552553 it('should persist store when unmatched content-type', function (done) {554 request(this.app)555 .post('/')556 .set('Content-Type', 'application/fizzbuzz')557 .send('buzz')558 .expect(200)559 .expect('x-store-foo', 'bar')560 .expect('')561 .end(done)562 })563564 it('should presist store when inflated', function (done) {565 var test = request(this.app).post('/')566 test.set('Content-Encoding', 'gzip')567 test.set('Content-Type', 'application/json')568 test.write(Buffer.from('1f8b080000000000000bab56ca4bcc4d55b2527ab16e97522d00515be1cc0e000000', 'hex'))569 test.expect(200)570 test.expect('x-store-foo', 'bar')571 test.expect('{"name":"论"}')572 test.end(done)573 })574575 it('should presist store when inflate error', function (done) {576 var test = request(this.app).post('/')577 test.set('Content-Encoding', 'gzip')578 test.set('Content-Type', 'application/json')579 test.write(Buffer.from('1f8b080000000000000bab56cc4d55b2527ab16e97522d00515be1cc0e000000', 'hex'))580 test.expect(400)581 test.expect('x-store-foo', 'bar')582 test.end(done)583 })584585 it('should presist store when parse error', function (done) {586 request(this.app)587 .post('/')588 .set('Content-Type', 'application/json')589 .send('{"user":')590 .expect(400)591 .expect('x-store-foo', 'bar')592 .end(done)593 })594595 it('should presist store when limit exceeded', function (done) {596 request(this.app)597 .post('/')598 .set('Content-Type', 'application/json')599 .send('{"user":"' + Buffer.alloc(1024 * 100, '.').toString() + '"}')600 .expect(413)601 .expect('x-store-foo', 'bar')602 .end(done)603 })604 })605

Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99

lines 606–643why, one step back
606 describe('charset', function () {
607 before(function () {608 this.app = createApp()609 })610611 it('should parse utf-8', function (done) {612 var test = request(this.app).post('/')613 test.set('Content-Type', 'application/json; charset=utf-8')614 test.write(Buffer.from('7b226e616d65223a22e8aeba227d', 'hex'))615 test.expect(200, '{"name":"论"}', done)616 })617618 it('should parse utf-16', function (done) {619 var test = request(this.app).post('/')620 test.set('Content-Type', 'application/json; charset=utf-16')621 test.write(Buffer.from('feff007b0022006e0061006d00650022003a00228bba0022007d', 'hex'))622 test.expect(200, '{"name":"论"}', done)623 })624625 it('should parse when content-length != char length', function (done) {626 var test = request(this.app).post('/')627 test.set('Content-Type', 'application/json; charset=utf-8')628 test.set('Content-Length', '13')629 test.write(Buffer.from('7b2274657374223a22c3a5227d', 'hex'))630 test.expect(200, '{"test":"å"}', done)631 })632633 it('should default to utf-8', function (done) {634 var test = request(this.app).post('/')635 test.set('Content-Type', 'application/json')636 test.write(Buffer.from('7b226e616d65223a22e8aeba227d', 'hex'))637 test.expect(200, '{"name":"论"}', done)638 })639640 it('should fail on unknown charset', function (done) {641 var test = request(this.app).post('/')642 test.set('Content-Type', 'application/json; charset=koi8-r')643 test.write(Buffer.from('7b226e616d65223a22cec5d4227d', 'hex'))

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

line 644why, one step back
644 test.expect(415, '[charset.unsupported] unsupported charset "KOI8-R"', done)

Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99

lines 645–696why, one step back
645 })
646 })647648 describe('encoding', function () {649 before(function () {650 this.app = createApp({ limit: '1kb' })651 })652653 it('should parse without encoding', function (done) {654 var test = request(this.app).post('/')655 test.set('Content-Type', 'application/json')656 test.write(Buffer.from('7b226e616d65223a22e8aeba227d', 'hex'))657 test.expect(200, '{"name":"论"}', done)658 })659660 it('should support identity encoding', function (done) {661 var test = request(this.app).post('/')662 test.set('Content-Encoding', 'identity')663 test.set('Content-Type', 'application/json')664 test.write(Buffer.from('7b226e616d65223a22e8aeba227d', 'hex'))665 test.expect(200, '{"name":"论"}', done)666 })667668 it('should support gzip encoding', function (done) {669 var test = request(this.app).post('/')670 test.set('Content-Encoding', 'gzip')671 test.set('Content-Type', 'application/json')672 test.write(Buffer.from('1f8b080000000000000bab56ca4bcc4d55b2527ab16e97522d00515be1cc0e000000', 'hex'))673 test.expect(200, '{"name":"论"}', done)674 })675676 it('should support deflate encoding', function (done) {677 var test = request(this.app).post('/')678 test.set('Content-Encoding', 'deflate')679 test.set('Content-Type', 'application/json')680 test.write(Buffer.from('789cab56ca4bcc4d55b2527ab16e97522d00274505ac', 'hex'))681 test.expect(200, '{"name":"论"}', done)682 })683684 it('should be case-insensitive', function (done) {685 var test = request(this.app).post('/')686 test.set('Content-Encoding', 'GZIP')687 test.set('Content-Type', 'application/json')688 test.write(Buffer.from('1f8b080000000000000bab56ca4bcc4d55b2527ab16e97522d00515be1cc0e000000', 'hex'))689 test.expect(200, '{"name":"论"}', done)690 })691692 it('should 415 on unknown encoding', function (done) {693 var test = request(this.app).post('/')694 test.set('Content-Encoding', 'nulls')695 test.set('Content-Type', 'application/json')696 test.write(Buffer.from('000000000000', 'hex'))

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

line 697why, one step back
697 test.expect(415, '[encoding.unsupported] unsupported content encoding "nulls"', done)

Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99

lines 698–726why, one step back
698 })
699700 it('should 400 on malformed encoding', function (done) {701 var test = request(this.app).post('/')702 test.set('Content-Encoding', 'gzip')703 test.set('Content-Type', 'application/json')704 test.write(Buffer.from('1f8b080000000000000bab56cc4d55b2527ab16e97522d00515be1cc0e000000', 'hex'))705 test.expect(400, done)706 })707708 it('should 413 when inflated value exceeds limit', function (done) {709 // gzip'd data exceeds 1kb, but deflated below 1kb710 var test = request(this.app).post('/')711 test.set('Content-Encoding', 'gzip')712 test.set('Content-Type', 'application/json')713 test.write(Buffer.from('1f8b080000000000000bedc1010d000000c2a0f74f6d0f071400000000000000', 'hex'))714 test.write(Buffer.from('0000000000000000000000000000000000000000000000000000000000000000', 'hex'))715 test.write(Buffer.from('0000000000000000004f0625b3b71650c30000', 'hex'))716 test.expect(413, done)717 })718 })719})720721function createApp (options) {722 var app = express()723724 app.use(express.json(options))725726 app.use(function (err, req, res, next) {

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

line 727why, one step back
727 // console.log(err)

Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99

line 728why, one step back
728 res.status(err.status || 500)

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 729–731why, one step back
729 res.send(String(req.headers['x-error-property']
730 ? err[req.headers['x-error-property']]731 : ('[' + err.type + '] ' + err.message)))

Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99

lines 732–754why, one step back
732 })
733734 app.post('/', function (req, res) {735 res.json(req.body)736 })737738 return app739}740741function parseError (str) {742 try {743 JSON.parse(str); throw new SyntaxError('strict violation')744 } catch (e) {745 return e.message746 }747}748749function shouldContainInBody (str) {750 return function (res) {751 assert.ok(res.text.indexOf(str) !== -1,752 'expected \'' + res.text + '\' to contain \'' + str + '\'')753 }754}