express.json.js
This file
Path
test/express.json.js
766 lines · 23.3 KiB · JavaScript
Last changed by
Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
History of this fileTake it
Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
line 1why, one step back1'use strict'Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 2–3why, one step back23var assert = require('assert')Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 4why, one step back4var asyncHooks = tryRequire('async_hooks')Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 5–8why, one step back5var Buffer = require('safe-buffer').Buffer6var express = require('..')7var request = require('supertest')8Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 9–12why, one step back9var describeAsyncHooks = typeof asyncHooks.AsyncLocalStorage === 'function'10 ? describe11 : describe.skip12Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 13–45why, one step back13describe('express.json()', function () {14 it('should parse JSON', function (done) {15 request(createApp())16 .post('/')17 .set('Content-Type', 'application/json')18 .send('{"user":"tobi"}')19 .expect(200, '{"user":"tobi"}', done)20 })2122 it('should handle Content-Length: 0', function (done) {23 request(createApp())24 .post('/')25 .set('Content-Type', 'application/json')26 .set('Content-Length', '0')27 .expect(200, '{}', done)28 })2930 it('should handle empty message-body', function (done) {31 request(createApp())32 .post('/')33 .set('Content-Type', 'application/json')34 .set('Transfer-Encoding', 'chunked')35 .expect(200, '{}', done)36 })3738 it('should handle no message-body', function (done) {39 request(createApp())40 .post('/')41 .set('Content-Type', 'application/json')42 .unset('Transfer-Encoding')43 .expect(200, '{}', done)44 })45Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 46–54why, one step back46 // The old node error message modification in body parser is catching this47 it('should 400 when only whitespace', function (done) {48 request(createApp())49 .post('/')50 .set('Content-Type', 'application/json')51 .send(' \n')52 .expect(400, '[entity.parse.failed] ' + parseError(' \n'), done)53 })54Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 55–102why, one step back55 it('should 400 when invalid content-length', function (done) {56 var app = express()5758 app.use(function (req, res, next) {59 req.headers['content-length'] = '20' // bad length60 next()61 })6263 app.use(express.json())6465 app.post('/', function (req, res) {66 res.json(req.body)67 })6869 request(app)70 .post('/')71 .set('Content-Type', 'application/json')72 .send('{"str":')73 .expect(400, /content length/, done)74 })7576 it('should handle duplicated middleware', function (done) {77 var app = express()7879 app.use(express.json())80 app.use(express.json())8182 app.post('/', function (req, res) {83 res.json(req.body)84 })8586 request(app)87 .post('/')88 .set('Content-Type', 'application/json')89 .send('{"user":"tobi"}')90 .expect(200, '{"user":"tobi"}', done)91 })9293 describe('when JSON is invalid', function () {94 before(function () {95 this.app = createApp()96 })9798 it('should 400 for bad token', function (done) {99 request(this.app)100 .post('/')101 .set('Content-Type', 'application/json')102 .send('{:')Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 103why, one step back103 .expect(400, '[entity.parse.failed] ' + parseError('{:'), done)Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 104–110why, one step back104 })105106 it('should 400 for incomplete', function (done) {107 request(this.app)108 .post('/')109 .set('Content-Type', 'application/json')110 .send('{"user"')Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 111why, one step back111 .expect(400, '[entity.parse.failed] ' + parseError('{"user"'), done)Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 112–131why, one step back112 })113114 it('should include original body on error object', function (done) {115 request(this.app)116 .post('/')117 .set('Content-Type', 'application/json')118 .set('X-Error-Property', 'body')119 .send(' {"user"')120 .expect(400, ' {"user"', done)121 })122 })123124 describe('with limit option', function () {125 it('should 413 when over limit with Content-Length', function (done) {126 var buf = Buffer.alloc(1024, '.')127 request(createApp({ limit: '1kb' }))128 .post('/')129 .set('Content-Type', 'application/json')130 .set('Content-Length', '1034')131 .send(JSON.stringify({ str: buf.toString() }))Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 132why, one step back132 .expect(413, '[entity.too.large] request entity too large', done)Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 133–135why, one step back133 })134135 it('should 413 when over limit with chunked encoding', function (done) {Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 136why, one step back136 var app = createApp({ limit: '1kb' })Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
line 137why, one step back137 var buf = Buffer.alloc(1024, '.')Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 138why, one step back138 var test = request(app).post('/')Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 139–145why, one step back139 test.set('Content-Type', 'application/json')140 test.set('Transfer-Encoding', 'chunked')141 test.write('{"str":')142 test.write('"' + buf.toString() + '"}')143 test.expect(413, done)144 })145Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 146–154why, one step back146 it('should 413 when inflated body over limit', function (done) {147 var app = createApp({ limit: '1kb' })148 var test = request(app).post('/')149 test.set('Content-Encoding', 'gzip')150 test.set('Content-Type', 'application/json')151 test.write(Buffer.from('1f8b080000000000000aab562a2e2952b252d21b05a360148c58a0540b0066f7ce1e0a040000', 'hex'))152 test.expect(413, done)153 })154Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 155–166why, one step back155 it('should accept number of bytes', function (done) {156 var buf = Buffer.alloc(1024, '.')157 request(createApp({ limit: 1024 }))158 .post('/')159 .set('Content-Type', 'application/json')160 .send(JSON.stringify({ str: buf.toString() }))161 .expect(413, done)162 })163164 it('should not change when options altered', function (done) {165 var buf = Buffer.alloc(1024, '.')166 var options = { limit: '1kb' }Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 167why, one step back167 var app = createApp(options)Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 168–170why, one step back168169 options.limit = '100kb'170Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 171why, one step back171 request(app)Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 172–179why, one step back172 .post('/')173 .set('Content-Type', 'application/json')174 .send(JSON.stringify({ str: buf.toString() }))175 .expect(413, done)176 })177178 it('should not hang response', function (done) {179 var buf = Buffer.alloc(10240, '.')Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 180–181why, one step back180 var app = createApp({ limit: '8kb' })181 var test = request(app).post('/')Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 182–187why, one step back182 test.set('Content-Type', 'application/json')183 test.write(buf)184 test.write(buf)185 test.write(buf)186 test.expect(413, done)187 })Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 188–196why, one step back188189 it('should not error when inflating', function (done) {190 var app = createApp({ limit: '1kb' })191 var test = request(app).post('/')192 test.set('Content-Encoding', 'gzip')193 test.set('Content-Type', 'application/json')194 test.write(Buffer.from('1f8b080000000000000aab562a2e2952b252d21b05a360148c58a0540b0066f7ce1e0a0400', 'hex'))195 test.expect(413, done)196 })Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 197–209why, one step back197 })198199 describe('with inflate option', function () {200 describe('when false', function () {201 before(function () {202 this.app = createApp({ inflate: false })203 })204205 it('should not accept content-encoding', function (done) {206 var test = request(this.app).post('/')207 test.set('Content-Encoding', 'gzip')208 test.set('Content-Type', 'application/json')209 test.write(Buffer.from('1f8b080000000000000bab56ca4bcc4d55b2527ab16e97522d00515be1cc0e000000', 'hex'))Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 210why, one step back210 test.expect(415, '[encoding.unsupported] content encoding unsupported', done)Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 211–239why, one step back211 })212 })213214 describe('when true', function () {215 before(function () {216 this.app = createApp({ inflate: true })217 })218219 it('should accept content-encoding', function (done) {220 var test = request(this.app).post('/')221 test.set('Content-Encoding', 'gzip')222 test.set('Content-Type', 'application/json')223 test.write(Buffer.from('1f8b080000000000000bab56ca4bcc4d55b2527ab16e97522d00515be1cc0e000000', 'hex'))224 test.expect(200, '{"name":"论"}', done)225 })226 })227 })228229 describe('with strict option', function () {230 describe('when undefined', function () {231 before(function () {232 this.app = createApp()233 })234235 it('should 400 on primitives', function (done) {236 request(this.app)237 .post('/')238 .set('Content-Type', 'application/json')239 .send('true')Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 240why, one step back240 .expect(400, '[entity.parse.failed] ' + parseError('#rue').replace(/#/g, 't'), done)Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 241–267why, one step back241 })242 })243244 describe('when false', function () {245 before(function () {246 this.app = createApp({ strict: false })247 })248249 it('should parse primitives', function (done) {250 request(this.app)251 .post('/')252 .set('Content-Type', 'application/json')253 .send('true')254 .expect(200, 'true', done)255 })256 })257258 describe('when true', function () {259 before(function () {260 this.app = createApp({ strict: true })261 })262263 it('should not parse primitives', function (done) {264 request(this.app)265 .post('/')266 .set('Content-Type', 'application/json')267 .send('true')Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 268why, one step back268 .expect(400, '[entity.parse.failed] ' + parseError('#rue').replace(/#/g, 't'), done)Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 269–275why, one step back269 })270271 it('should not parse primitives with leading whitespaces', function (done) {272 request(this.app)273 .post('/')274 .set('Content-Type', 'application/json')275 .send(' true')Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 276why, one step back276 .expect(400, '[entity.parse.failed] ' + parseError(' #rue').replace(/#/g, 't'), done)Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 277–293why, one step back277 })278279 it('should allow leading whitespaces in JSON', function (done) {280 request(this.app)281 .post('/')282 .set('Content-Type', 'application/json')283 .send(' { "user": "tobi" }')284 .expect(200, '{"user":"tobi"}', done)285 })286287 it('should include correct message in stack trace', function (done) {288 request(this.app)289 .post('/')290 .set('Content-Type', 'application/json')291 .set('X-Error-Property', 'stack')292 .send('true')293 .expect(400)Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 294why, one step back294 .expect(shouldContainInBody(parseError('#rue').replace(/#/g, 't')))Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 295–318why, one step back295 .end(done)296 })297 })298 })299300 describe('with type option', function () {301 describe('when "application/vnd.api+json"', function () {302 before(function () {303 this.app = createApp({ type: 'application/vnd.api+json' })304 })305306 it('should parse JSON for custom type', function (done) {307 request(this.app)308 .post('/')309 .set('Content-Type', 'application/vnd.api+json')310 .send('{"user":"tobi"}')311 .expect(200, '{"user":"tobi"}', done)312 })313314 it('should ignore standard type', function (done) {315 request(this.app)316 .post('/')317 .set('Content-Type', 'application/json')318 .send('{"user":"tobi"}')deps: body-parser@2.0.0-beta.1Douglas Christopher Wilsonbefore adoptionaf341b0
line 319why, one step back319 .expect(200, '', done)Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 320–350why, one step back320 })321 })322323 describe('when ["application/json", "application/vnd.api+json"]', function () {324 before(function () {325 this.app = createApp({326 type: ['application/json', 'application/vnd.api+json']327 })328 })329330 it('should parse JSON for "application/json"', function (done) {331 request(this.app)332 .post('/')333 .set('Content-Type', 'application/json')334 .send('{"user":"tobi"}')335 .expect(200, '{"user":"tobi"}', done)336 })337338 it('should parse JSON for "application/vnd.api+json"', function (done) {339 request(this.app)340 .post('/')341 .set('Content-Type', 'application/vnd.api+json')342 .send('{"user":"tobi"}')343 .expect(200, '{"user":"tobi"}', done)344 })345346 it('should ignore "application/x-json"', function (done) {347 request(this.app)348 .post('/')349 .set('Content-Type', 'application/x-json')350 .send('{"user":"tobi"}')deps: body-parser@2.0.0-beta.1Douglas Christopher Wilsonbefore adoptionaf341b0
line 351why, one step back351 .expect(200, '', done)Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 352–402why, one step back352 })353 })354355 describe('when a function', function () {356 it('should parse when truthy value returned', function (done) {357 var app = createApp({ type: accept })358359 function accept (req) {360 return req.headers['content-type'] === 'application/vnd.api+json'361 }362363 request(app)364 .post('/')365 .set('Content-Type', 'application/vnd.api+json')366 .send('{"user":"tobi"}')367 .expect(200, '{"user":"tobi"}', done)368 })369370 it('should work without content-type', function (done) {371 var app = createApp({ type: accept })372373 function accept (req) {374 return true375 }376377 var test = request(app).post('/')378 test.write('{"user":"tobi"}')379 test.expect(200, '{"user":"tobi"}', done)380 })381382 it('should not invoke without a body', function (done) {383 var app = createApp({ type: accept })384385 function accept (req) {386 throw new Error('oops!')387 }388389 request(app)390 .get('/')391 .expect(404, done)392 })393 })394 })395396 describe('with verify option', function () {397 it('should assert value if function', function () {398 assert.throws(createApp.bind(null, { verify: 'lol' }),399 /TypeError: option verify must be function/)400 })401402 it('should error from verify', function (done) {Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 403–407why, one step back403 var app = createApp({404 verify: function (req, res, buf) {405 if (buf[0] === 0x5b) throw new Error('no arrays')406 }407 })Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 408–412why, one step back408409 request(app)410 .post('/')411 .set('Content-Type', 'application/json')412 .send('["tobi"]')Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 413why, one step back413 .expect(403, '[entity.verify.failed] no arrays', done)Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 414–416why, one step back414 })415416 it('should allow custom codes', function (done) {Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 417–424why, one step back417 var app = createApp({418 verify: function (req, res, buf) {419 if (buf[0] !== 0x5b) return420 var err = new Error('no arrays')421 err.status = 400422 throw err423 }424 })Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 425–429why, one step back425426 request(app)427 .post('/')428 .set('Content-Type', 'application/json')429 .send('["tobi"]')Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 430why, one step back430 .expect(400, '[entity.verify.failed] no arrays', done)Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 431–433why, one step back431 })432433 it('should allow custom type', function (done) {Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 434–441why, one step back434 var app = createApp({435 verify: function (req, res, buf) {436 if (buf[0] !== 0x5b) return437 var err = new Error('no arrays')438 err.type = 'foo.bar'439 throw err440 }441 })Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 442–446why, one step back442443 request(app)444 .post('/')445 .set('Content-Type', 'application/json')446 .send('["tobi"]')Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 447why, one step back447 .expect(403, '[foo.bar] no arrays', done)Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 448–450why, one step back448 })449450 it('should include original body on error object', function (done) {Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 451–455why, one step back451 var app = createApp({452 verify: function (req, res, buf) {453 if (buf[0] === 0x5b) throw new Error('no arrays')454 }455 })Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 456–465why, one step back456457 request(app)458 .post('/')459 .set('Content-Type', 'application/json')460 .set('X-Error-Property', 'body')461 .send('["tobi"]')462 .expect(403, '["tobi"]', done)463 })464465 it('should allow pass-through', function (done) {Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 466–470why, one step back466 var app = createApp({467 verify: function (req, res, buf) {468 if (buf[0] === 0x5b) throw new Error('no arrays')469 }470 })Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 471–479why, one step back471472 request(app)473 .post('/')474 .set('Content-Type', 'application/json')475 .send('{"user":"tobi"}')476 .expect(200, '{"user":"tobi"}', done)477 })478479 it('should work with different charsets', function (done) {Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 480–484why, one step back480 var app = createApp({481 verify: function (req, res, buf) {482 if (buf[0] === 0x5b) throw new Error('no arrays')483 }484 })Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 485–492why, one step back485486 var test = request(app).post('/')487 test.set('Content-Type', 'application/json; charset=utf-16')488 test.write(Buffer.from('feff007b0022006e0061006d00650022003a00228bba0022007d', 'hex'))489 test.expect(200, '{"name":"论"}', done)490 })491492 it('should 415 on unknown charset prior to verify', function (done) {Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 493–497why, one step back493 var app = createApp({494 verify: function (req, res, buf) {495 throw new Error('unexpected verify call')496 }497 })Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 498–501why, one step back498499 var test = request(app).post('/')500 test.set('Content-Type', 'application/json; charset=x-bogus')501 test.write(Buffer.from('00000000', 'hex'))Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 502why, one step back502 test.expect(415, '[charset.unsupported] unsupported charset "X-BOGUS"', done)Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 503–505why, one step back503 })504 })505Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 506–609why, one step back506 describeAsyncHooks('async local storage', function () {507 before(function () {508 var app = express()509 var store = { foo: 'bar' }510511 app.use(function (req, res, next) {512 req.asyncLocalStorage = new asyncHooks.AsyncLocalStorage()513 req.asyncLocalStorage.run(store, next)514 })515516 app.use(express.json())517518 app.use(function (req, res, next) {519 var local = req.asyncLocalStorage.getStore()520521 if (local) {522 res.setHeader('x-store-foo', String(local.foo))523 }524525 next()526 })527528 app.use(function (err, req, res, next) {529 var local = req.asyncLocalStorage.getStore()530531 if (local) {532 res.setHeader('x-store-foo', String(local.foo))533 }534535 res.status(err.status || 500)536 res.send('[' + err.type + '] ' + err.message)537 })538539 app.post('/', function (req, res) {540 res.json(req.body)541 })542543 this.app = app544 })545546 it('should presist store', function (done) {547 request(this.app)548 .post('/')549 .set('Content-Type', 'application/json')550 .send('{"user":"tobi"}')551 .expect(200)552 .expect('x-store-foo', 'bar')553 .expect('{"user":"tobi"}')554 .end(done)555 })556557 it('should persist store when unmatched content-type', function (done) {558 request(this.app)559 .post('/')560 .set('Content-Type', 'application/fizzbuzz')561 .send('buzz')562 .expect(200)563 .expect('x-store-foo', 'bar')564 .expect('')565 .end(done)566 })567568 it('should presist store when inflated', function (done) {569 var test = request(this.app).post('/')570 test.set('Content-Encoding', 'gzip')571 test.set('Content-Type', 'application/json')572 test.write(Buffer.from('1f8b080000000000000bab56ca4bcc4d55b2527ab16e97522d00515be1cc0e000000', 'hex'))573 test.expect(200)574 test.expect('x-store-foo', 'bar')575 test.expect('{"name":"论"}')576 test.end(done)577 })578579 it('should presist store when inflate error', function (done) {580 var test = request(this.app).post('/')581 test.set('Content-Encoding', 'gzip')582 test.set('Content-Type', 'application/json')583 test.write(Buffer.from('1f8b080000000000000bab56cc4d55b2527ab16e97522d00515be1cc0e000000', 'hex'))584 test.expect(400)585 test.expect('x-store-foo', 'bar')586 test.end(done)587 })588589 it('should presist store when parse error', function (done) {590 request(this.app)591 .post('/')592 .set('Content-Type', 'application/json')593 .send('{"user":')594 .expect(400)595 .expect('x-store-foo', 'bar')596 .end(done)597 })598599 it('should presist store when limit exceeded', function (done) {600 request(this.app)601 .post('/')602 .set('Content-Type', 'application/json')603 .send('{"user":"' + Buffer.alloc(1024 * 100, '.').toString() + '"}')604 .expect(413)605 .expect('x-store-foo', 'bar')606 .end(done)607 })608 })609Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 610–647why, one step back610 describe('charset', function () {611 before(function () {612 this.app = createApp()613 })614615 it('should parse utf-8', function (done) {616 var test = request(this.app).post('/')617 test.set('Content-Type', 'application/json; charset=utf-8')618 test.write(Buffer.from('7b226e616d65223a22e8aeba227d', 'hex'))619 test.expect(200, '{"name":"论"}', done)620 })621622 it('should parse utf-16', function (done) {623 var test = request(this.app).post('/')624 test.set('Content-Type', 'application/json; charset=utf-16')625 test.write(Buffer.from('feff007b0022006e0061006d00650022003a00228bba0022007d', 'hex'))626 test.expect(200, '{"name":"论"}', done)627 })628629 it('should parse when content-length != char length', function (done) {630 var test = request(this.app).post('/')631 test.set('Content-Type', 'application/json; charset=utf-8')632 test.set('Content-Length', '13')633 test.write(Buffer.from('7b2274657374223a22c3a5227d', 'hex'))634 test.expect(200, '{"test":"å"}', done)635 })636637 it('should default to utf-8', function (done) {638 var test = request(this.app).post('/')639 test.set('Content-Type', 'application/json')640 test.write(Buffer.from('7b226e616d65223a22e8aeba227d', 'hex'))641 test.expect(200, '{"name":"论"}', done)642 })643644 it('should fail on unknown charset', function (done) {645 var test = request(this.app).post('/')646 test.set('Content-Type', 'application/json; charset=koi8-r')647 test.write(Buffer.from('7b226e616d65223a22cec5d4227d', 'hex'))Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 648why, one step back648 test.expect(415, '[charset.unsupported] unsupported charset "KOI8-R"', done)Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 649–700why, one step back649 })650 })651652 describe('encoding', function () {653 before(function () {654 this.app = createApp({ limit: '1kb' })655 })656657 it('should parse without encoding', function (done) {658 var test = request(this.app).post('/')659 test.set('Content-Type', 'application/json')660 test.write(Buffer.from('7b226e616d65223a22e8aeba227d', 'hex'))661 test.expect(200, '{"name":"论"}', done)662 })663664 it('should support identity encoding', function (done) {665 var test = request(this.app).post('/')666 test.set('Content-Encoding', 'identity')667 test.set('Content-Type', 'application/json')668 test.write(Buffer.from('7b226e616d65223a22e8aeba227d', 'hex'))669 test.expect(200, '{"name":"论"}', done)670 })671672 it('should support gzip encoding', function (done) {673 var test = request(this.app).post('/')674 test.set('Content-Encoding', 'gzip')675 test.set('Content-Type', 'application/json')676 test.write(Buffer.from('1f8b080000000000000bab56ca4bcc4d55b2527ab16e97522d00515be1cc0e000000', 'hex'))677 test.expect(200, '{"name":"论"}', done)678 })679680 it('should support deflate encoding', function (done) {681 var test = request(this.app).post('/')682 test.set('Content-Encoding', 'deflate')683 test.set('Content-Type', 'application/json')684 test.write(Buffer.from('789cab56ca4bcc4d55b2527ab16e97522d00274505ac', 'hex'))685 test.expect(200, '{"name":"论"}', done)686 })687688 it('should be case-insensitive', function (done) {689 var test = request(this.app).post('/')690 test.set('Content-Encoding', 'GZIP')691 test.set('Content-Type', 'application/json')692 test.write(Buffer.from('1f8b080000000000000bab56ca4bcc4d55b2527ab16e97522d00515be1cc0e000000', 'hex'))693 test.expect(200, '{"name":"论"}', done)694 })695696 it('should 415 on unknown encoding', function (done) {697 var test = request(this.app).post('/')698 test.set('Content-Encoding', 'nulls')699 test.set('Content-Type', 'application/json')700 test.write(Buffer.from('000000000000', 'hex'))Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 701why, one step back701 test.expect(415, '[encoding.unsupported] unsupported content encoding "nulls"', done)Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 702–730why, one step back702 })703704 it('should 400 on malformed encoding', function (done) {705 var test = request(this.app).post('/')706 test.set('Content-Encoding', 'gzip')707 test.set('Content-Type', 'application/json')708 test.write(Buffer.from('1f8b080000000000000bab56cc4d55b2527ab16e97522d00515be1cc0e000000', 'hex'))709 test.expect(400, done)710 })711712 it('should 413 when inflated value exceeds limit', function (done) {713 // gzip'd data exceeds 1kb, but deflated below 1kb714 var test = request(this.app).post('/')715 test.set('Content-Encoding', 'gzip')716 test.set('Content-Type', 'application/json')717 test.write(Buffer.from('1f8b080000000000000bedc1010d000000c2a0f74f6d0f071400000000000000', 'hex'))718 test.write(Buffer.from('0000000000000000000000000000000000000000000000000000000000000000', 'hex'))719 test.write(Buffer.from('0000000000000000004f0625b3b71650c30000', 'hex'))720 test.expect(413, done)721 })722 })723})724725function createApp (options) {726 var app = express()727728 app.use(express.json(options))729730 app.use(function (err, req, res, next) {Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 731why, one step back731 // console.log(err)Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
line 732why, one step back732 res.status(err.status || 500)Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 733–735why, one step back733 res.send(String(req.headers['x-error-property']734 ? err[req.headers['x-error-property']]735 : ('[' + err.type + '] ' + err.message)))Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 736–756why, one step back736 })737738 app.post('/', function (req, res) {739 res.json(req.body)740 })741742 return app743}744745function parseError (str) {746 try {747 JSON.parse(str); throw new SyntaxError('strict violation')748 } catch (e) {749 return e.message750 }751}752753function shouldContainInBody (str) {754 return function (res) {755 assert.ok(res.text.indexOf(str) !== -1,756 'expected \'' + res.text + '\' to contain \'' + str + '\'')Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 757–764why, one step back757 }758}759760function tryRequire (name) {761 try {762 return require(name)763 } catch (e) {764 return {}Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 765–766why, one step back765 }766}