deps: body-parser@1.20.0
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Douglas Christopher Wilson · committed by Douglas Christopher Wilson · parent a107702 · Browse files · 6 files·+817 added−221 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+817 added−221 removed
- tests:+806 added−220 removed
- other:+11 added−1 removed
Provenance
- Commit
32c558d- Tree
f84cd46- Author
Douglas Christopher Wilson <doug@somethingdoug.com>- Committer
Douglas Christopher Wilson <doug@somethingdoug.com>- Parent
a107702
6 files changed+817 added−221 removed
modified:test/express.json.js+232 added-107 removed
1 'use strict'2 3 var assert = require('assert')4+var asyncHooks = tryRequire('async_hooks')5 var Buffer = require('safe-buffer').Buffer6 var express = require('..')7 var request = require('supertest')8 9+var describeAsyncHooks = typeof asyncHooks.AsyncLocalStorage === 'function'10+ ? describe11+ : describe.skip12+13 describe('express.json()', function () {14 it('should parse JSON', function (done) {15 request(createApp())16 .post('/')25 unmodified lines42 .unset('Transfer-Encoding')43 .expect(200, '{}', done)44 })45 46+ it('should 400 when only whitespace', function (done) {47+ request(createApp())48+ .post('/')49+ .set('Content-Type', 'application/json')50+ .send(' \n')51+ .expect(400, '[entity.parse.failed] ' + parseError(' '), done)52+ })53+54 it('should 400 when invalid content-length', function (done) {55 var app = express()56 57 app.use(function (req, res, next) {13 unmodified lines71 .send('{"str":')72 .expect(400, /content length/, done)73 })74 75+ it('should 500 if stream not readable', function (done) {76+ var app = express()77+78+ app.use(function (req, res, next) {79+ req.on('end', next)80+ req.resume()81+ })82+83+ app.use(express.json())84+85+ app.use(function (err, req, res, next) {86+ res.status(err.status || 500)87+ res.send('[' + err.type + '] ' + err.message)88+ })89+90+ app.post('/', function (req, res) {91+ res.json(req.body)92+ })93+94+ request(app)95+ .post('/')96+ .set('Content-Type', 'application/json')97+ .send('{"user":"tobi"}')98+ .expect(500, '[stream.not.readable] stream is not readable', done)99+ })100+101 it('should handle duplicated middleware', function (done) {102 var app = express()103 104 app.use(express.json())19 unmodified lines124 request(this.app)125 .post('/')126 .set('Content-Type', 'application/json')127 .send('{:')89- .expect(400, parseError('{:'), done)128+ .expect(400, '[entity.parse.failed] ' + parseError('{:'), done)129 })130 131 it('should 400 for incomplete', function (done) {132 request(this.app)133 .post('/')134 .set('Content-Type', 'application/json')135 .send('{"user"')97- .expect(400, parseError('{"user"'), done)136+ .expect(400, '[entity.parse.failed] ' + parseError('{"user"'), done)137 })138 100- it('should error with type = "entity.parse.failed"', function (done) {101- request(this.app)102- .post('/')103- .set('Content-Type', 'application/json')104- .set('X-Error-Property', 'type')105- .send(' {"user"')106- .expect(400, 'entity.parse.failed', done)107- })108-139 it('should include original body on error object', function (done) {140 request(this.app)141 .post('/')142 .set('Content-Type', 'application/json')10 unmodified lines153 .post('/')154 .set('Content-Type', 'application/json')155 .set('Content-Length', '1034')156 .send(JSON.stringify({ str: buf.toString() }))127- .expect(413, done)157+ .expect(413, '[entity.too.large] request entity too large', done)158 })159 130- it('should error with type = "entity.too.large"', function (done) {131- var buf = Buffer.alloc(1024, '.')132- request(createApp({ limit: '1kb' }))133- .post('/')134- .set('Content-Type', 'application/json')135- .set('Content-Length', '1034')136- .set('X-Error-Property', 'type')137- .send(JSON.stringify({ str: buf.toString() }))138- .expect(413, 'entity.too.large', done)139- })140-160 it('should 413 when over limit with chunked encoding', function (done) {161+ var app = createApp({ limit: '1kb' })162 var buf = Buffer.alloc(1024, '.')143- var server = createApp({ limit: '1kb' })144- var test = request(server).post('/')163+ var test = request(app).post('/')164 test.set('Content-Type', 'application/json')165 test.set('Transfer-Encoding', 'chunked')166 test.write('{"str":')167 test.write('"' + buf.toString() + '"}')168 test.expect(413, done)169 })170 171+ it('should 413 when inflated body over limit', function (done) {172+ var app = createApp({ limit: '1kb' })173+ var test = request(app).post('/')174+ test.set('Content-Encoding', 'gzip')175+ test.set('Content-Type', 'application/json')176+ test.write(Buffer.from('1f8b080000000000000aab562a2e2952b252d21b05a360148c58a0540b0066f7ce1e0a040000', 'hex'))177+ test.expect(413, done)178+ })179+180 it('should accept number of bytes', function (done) {181 var buf = Buffer.alloc(1024, '.')182 request(createApp({ limit: 1024 }))183 .post('/')4 unmodified lines188 189 it('should not change when options altered', function (done) {190 var buf = Buffer.alloc(1024, '.')191 var options = { limit: '1kb' }164- var server = createApp(options)192+ var app = createApp(options)193 194 options.limit = '100kb'195 168- request(server)196+ request(app)197 .post('/')198 .set('Content-Type', 'application/json')199 .send(JSON.stringify({ str: buf.toString() }))200 .expect(413, done)201 })202 203 it('should not hang response', function (done) {204 var buf = Buffer.alloc(10240, '.')177- var server = createApp({ limit: '8kb' })178- var test = request(server).post('/')205+ var app = createApp({ limit: '8kb' })206+ var test = request(app).post('/')207 test.set('Content-Type', 'application/json')208 test.write(buf)209 test.write(buf)210 test.write(buf)211 test.expect(413, done)212 })213+214+ it('should not error when inflating', function (done) {215+ var app = createApp({ limit: '1kb' })216+ var test = request(app).post('/')217+ test.set('Content-Encoding', 'gzip')218+ test.set('Content-Type', 'application/json')219+ test.write(Buffer.from('1f8b080000000000000aab562a2e2952b252d21b05a360148c58a0540b0066f7ce1e0a0400', 'hex'))220+ test.expect(413, done)221+ })222 })223 224 describe('with inflate option', function () {225 describe('when false', function () {5 unmodified lines231 var test = request(this.app).post('/')232 test.set('Content-Encoding', 'gzip')233 test.set('Content-Type', 'application/json')234 test.write(Buffer.from('1f8b080000000000000bab56ca4bcc4d55b2527ab16e97522d00515be1cc0e000000', 'hex'))198- test.expect(415, 'content encoding unsupported', done)235+ test.expect(415, '[encoding.unsupported] content encoding unsupported', done)236 })237 })238 239 describe('when true', function () {21 unmodified lines261 request(this.app)262 .post('/')263 .set('Content-Type', 'application/json')264 .send('true')228- .expect(400, parseError('#rue').replace('#', 't'), done)265+ .expect(400, '[entity.parse.failed] ' + parseError('#rue').replace('#', 't'), done)266 })267 })268 269 describe('when false', function () {19 unmodified lines289 request(this.app)290 .post('/')291 .set('Content-Type', 'application/json')292 .send('true')256- .expect(400, parseError('#rue').replace('#', 't'), done)293+ .expect(400, '[entity.parse.failed] ' + parseError('#rue').replace('#', 't'), done)294 })295 296 it('should not parse primitives with leading whitespaces', function (done) {297 request(this.app)298 .post('/')299 .set('Content-Type', 'application/json')300 .send(' true')264- .expect(400, parseError(' #rue').replace('#', 't'), done)301+ .expect(400, '[entity.parse.failed] ' + parseError(' #rue').replace('#', 't'), done)302 })303 304 it('should allow leading whitespaces in JSON', function (done) {305 request(this.app)2 unmodified lines308 .send(' { "user": "tobi" }')309 .expect(200, '{"user":"tobi"}', done)310 })311 275- it('should error with type = "entity.parse.failed"', function (done) {276- request(this.app)277- .post('/')278- .set('Content-Type', 'application/json')279- .set('X-Error-Property', 'type')280- .send('true')281- .expect(400, 'entity.parse.failed', done)282- })283-312 it('should include correct message in stack trace', function (done) {313 request(this.app)314 .post('/')315 .set('Content-Type', 'application/json')108 unmodified lines424 /TypeError: option verify must be function/)425 })426 427 it('should error from verify', function (done) {428+ var app = createApp({400- var app = createApp({ verify: function (req, res, buf) {401- if (buf[0] === 0x5b) throw new Error('no arrays')402- } })429+ verify: function (req, res, buf) {430+ if (buf[0] === 0x5b) throw new Error('no arrays')431+ }432+ })433 434 request(app)435 .post('/')436 .set('Content-Type', 'application/json')437 .send('["tobi"]')408- .expect(403, 'no arrays', done)438+ .expect(403, '[entity.verify.failed] no arrays', done)439 })440 411- it('should error with type = "entity.verify.failed"', function (done) {412- var app = createApp({ verify: function (req, res, buf) {413- if (buf[0] === 0x5b) throw new Error('no arrays')414- } })415-416- request(app)417- .post('/')418- .set('Content-Type', 'application/json')419- .set('X-Error-Property', 'type')420- .send('["tobi"]')421- .expect(403, 'entity.verify.failed', done)422- })423-441 it('should allow custom codes', function (done) {442+ var app = createApp({425- var app = createApp({ verify: function (req, res, buf) {426- if (buf[0] !== 0x5b) return427- var err = new Error('no arrays')428- err.status = 400429- throw err430- } })443+ verify: function (req, res, buf) {444+ if (buf[0] !== 0x5b) return445+ var err = new Error('no arrays')446+ err.status = 400447+ throw err448+ }449+ })450 451 request(app)452 .post('/')453 .set('Content-Type', 'application/json')454 .send('["tobi"]')436- .expect(400, 'no arrays', done)455+ .expect(400, '[entity.verify.failed] no arrays', done)456 })457 458 it('should allow custom type', function (done) {459+ var app = createApp({440- var app = createApp({ verify: function (req, res, buf) {441- if (buf[0] !== 0x5b) return442- var err = new Error('no arrays')443- err.type = 'foo.bar'444- throw err445- } })460+ verify: function (req, res, buf) {461+ if (buf[0] !== 0x5b) return462+ var err = new Error('no arrays')463+ err.type = 'foo.bar'464+ throw err465+ }466+ })467 468 request(app)469 .post('/')470 .set('Content-Type', 'application/json')450- .set('X-Error-Property', 'type')471 .send('["tobi"]')452- .expect(403, 'foo.bar', done)472+ .expect(403, '[foo.bar] no arrays', done)473 })474 475 it('should include original body on error object', function (done) {476+ var app = createApp({456- var app = createApp({ verify: function (req, res, buf) {457- if (buf[0] === 0x5b) throw new Error('no arrays')458- } })477+ verify: function (req, res, buf) {478+ if (buf[0] === 0x5b) throw new Error('no arrays')479+ }480+ })481 482 request(app)483 .post('/')484 .set('Content-Type', 'application/json')2 unmodified lines487 .expect(403, '["tobi"]', done)488 })489 490 it('should allow pass-through', function (done) {491+ var app = createApp({469- var app = createApp({ verify: function (req, res, buf) {470- if (buf[0] === 0x5b) throw new Error('no arrays')471- } })492+ verify: function (req, res, buf) {493+ if (buf[0] === 0x5b) throw new Error('no arrays')494+ }495+ })496 497 request(app)498 .post('/')499 .set('Content-Type', 'application/json')1 unmodified line501 .expect(200, '{"user":"tobi"}', done)502 })503 504 it('should work with different charsets', function (done) {505+ var app = createApp({481- var app = createApp({ verify: function (req, res, buf) {482- if (buf[0] === 0x5b) throw new Error('no arrays')483- } })506+ verify: function (req, res, buf) {507+ if (buf[0] === 0x5b) throw new Error('no arrays')508+ }509+ })510 511 var test = request(app).post('/')512 test.set('Content-Type', 'application/json; charset=utf-16')513 test.write(Buffer.from('feff007b0022006e0061006d00650022003a00228bba0022007d', 'hex'))514 test.expect(200, '{"name":"论"}', done)515 })516 517 it('should 415 on unknown charset prior to verify', function (done) {518+ var app = createApp({492- var app = createApp({ verify: function (req, res, buf) {493- throw new Error('unexpected verify call')494- } })519+ verify: function (req, res, buf) {520+ throw new Error('unexpected verify call')521+ }522+ })523 524 var test = request(app).post('/')525 test.set('Content-Type', 'application/json; charset=x-bogus')526 test.write(Buffer.from('00000000', 'hex'))499- test.expect(415, 'unsupported charset "X-BOGUS"', done)527+ test.expect(415, '[charset.unsupported] unsupported charset "X-BOGUS"', done)528 })529 })530 531+ describeAsyncHooks('async local storage', function () {532+ before(function () {533+ var app = express()534+ var store = { foo: 'bar' }535+536+ app.use(function (req, res, next) {537+ req.asyncLocalStorage = new asyncHooks.AsyncLocalStorage()538+ req.asyncLocalStorage.run(store, next)539+ })540+541+ app.use(express.json())542+543+ app.use(function (req, res, next) {544+ var local = req.asyncLocalStorage.getStore()545+546+ if (local) {547+ res.setHeader('x-store-foo', String(local.foo))548+ }549+550+ next()551+ })552+553+ app.use(function (err, req, res, next) {554+ var local = req.asyncLocalStorage.getStore()555+556+ if (local) {557+ res.setHeader('x-store-foo', String(local.foo))558+ }559+560+ res.status(err.status || 500)561+ res.send('[' + err.type + '] ' + err.message)562+ })563+564+ app.post('/', function (req, res) {565+ res.json(req.body)566+ })567+568+ this.app = app569+ })570+571+ it('should presist store', function (done) {572+ request(this.app)573+ .post('/')574+ .set('Content-Type', 'application/json')575+ .send('{"user":"tobi"}')576+ .expect(200)577+ .expect('x-store-foo', 'bar')578+ .expect('{"user":"tobi"}')579+ .end(done)580+ })581+582+ it('should presist store when unmatched content-type', function (done) {583+ request(this.app)584+ .post('/')585+ .set('Content-Type', 'application/fizzbuzz')586+ .send('buzz')587+ .expect(200)588+ .expect('x-store-foo', 'bar')589+ .expect('{}')590+ .end(done)591+ })592+593+ it('should presist store when inflated', function (done) {594+ var test = request(this.app).post('/')595+ test.set('Content-Encoding', 'gzip')596+ test.set('Content-Type', 'application/json')597+ test.write(Buffer.from('1f8b080000000000000bab56ca4bcc4d55b2527ab16e97522d00515be1cc0e000000', 'hex'))598+ test.expect(200)599+ test.expect('x-store-foo', 'bar')600+ test.expect('{"name":"论"}')601+ test.end(done)602+ })603+604+ it('should presist store when inflate error', function (done) {605+ var test = request(this.app).post('/')606+ test.set('Content-Encoding', 'gzip')607+ test.set('Content-Type', 'application/json')608+ test.write(Buffer.from('1f8b080000000000000bab56cc4d55b2527ab16e97522d00515be1cc0e000000', 'hex'))609+ test.expect(400)610+ test.expect('x-store-foo', 'bar')611+ test.end(done)612+ })613+614+ it('should presist store when parse error', function (done) {615+ request(this.app)616+ .post('/')617+ .set('Content-Type', 'application/json')618+ .send('{"user":')619+ .expect(400)620+ .expect('x-store-foo', 'bar')621+ .end(done)622+ })623+624+ it('should presist store when limit exceeded', function (done) {625+ request(this.app)626+ .post('/')627+ .set('Content-Type', 'application/json')628+ .send('{"user":"' + Buffer.alloc(1024 * 100, '.').toString() + '"}')629+ .expect(413)630+ .expect('x-store-foo', 'bar')631+ .end(done)632+ })633+ })634+635 describe('charset', function () {636 before(function () {637 this.app = createApp()638 })30 unmodified lines669 it('should fail on unknown charset', function (done) {670 var test = request(this.app).post('/')671 test.set('Content-Type', 'application/json; charset=koi8-r')672 test.write(Buffer.from('7b226e616d65223a22cec5d4227d', 'hex'))541- test.expect(415, 'unsupported charset "KOI8-R"', done)673+ test.expect(415, '[charset.unsupported] unsupported charset "KOI8-R"', done)674 })543-544- it('should error with type = "charset.unsupported"', function (done) {545- var test = request(this.app).post('/')546- test.set('Content-Type', 'application/json; charset=koi8-r')547- test.set('X-Error-Property', 'type')548- test.write(Buffer.from('7b226e616d65223a22cec5d4227d', 'hex'))549- test.expect(415, 'charset.unsupported', done)550- })675 })676 677 describe('encoding', function () {678 before(function () {43 unmodified lines722 var test = request(this.app).post('/')723 test.set('Content-Encoding', 'nulls')724 test.set('Content-Type', 'application/json')725 test.write(Buffer.from('000000000000', 'hex'))602- test.expect(415, 'unsupported content encoding "nulls"', done)726+ test.expect(415, '[encoding.unsupported] unsupported content encoding "nulls"', done)727 })728 605- it('should error with type = "encoding.unsupported"', function (done) {606- var test = request(this.app).post('/')607- test.set('Content-Encoding', 'nulls')608- test.set('Content-Type', 'application/json')609- test.set('X-Error-Property', 'type')610- test.write(Buffer.from('000000000000', 'hex'))611- test.expect(415, 'encoding.unsupported', done)612- })613-729 it('should 400 on malformed encoding', function (done) {730 var test = request(this.app).post('/')731 test.set('Content-Encoding', 'gzip')732 test.set('Content-Type', 'application/json')20 unmodified lines753 app.use(express.json(options))754 755 app.use(function (err, req, res, next) {756 res.status(err.status || 500)642- res.send(String(err[req.headers['x-error-property'] || 'message']))757+ res.send(String(req.headers['x-error-property']758+ ? err[req.headers['x-error-property']]759+ : ('[' + err.type + '] ' + err.message)))760 })761 762 app.post('/', function (req, res) {763 res.json(req.body)15 unmodified lines779 assert.ok(res.text.indexOf(str) !== -1,780 'expected \'' + res.text + '\' to contain \'' + str + '\'')781 }782 }783+784+function tryRequire (name) {785+ try {786+ return require(name)787+ } catch (e) {788+ return {}789+ }790+}modified:test/express.raw.js+185 added-18 removed
1 'use strict'2 3 var assert = require('assert')4+var asyncHooks = tryRequire('async_hooks')5 var Buffer = require('safe-buffer').Buffer6 var express = require('..')7 var request = require('supertest')8 9+var describeAsyncHooks = typeof asyncHooks.AsyncLocalStorage === 'function'10+ ? describe11+ : describe.skip12+13 describe('express.raw()', function () {14 before(function () {15 this.app = createApp()16 })47 unmodified lines64 .send('')65 .expect(200, { buf: '' }, done)66 })67 68+ it('should 500 if stream not readable', function (done) {69+ var app = express()70+71+ app.use(function (req, res, next) {72+ req.on('end', next)73+ req.resume()74+ })75+76+ app.use(express.raw())77+78+ app.use(function (err, req, res, next) {79+ res.status(err.status || 500)80+ res.send('[' + err.type + '] ' + err.message)81+ })82+83+ app.post('/', function (req, res) {84+ if (Buffer.isBuffer(req.body)) {85+ res.json({ buf: req.body.toString('hex') })86+ } else {87+ res.json(req.body)88+ }89+ })90+91+ request(app)92+ .post('/')93+ .set('Content-Type', 'application/octet-stream')94+ .send('the user is tobi')95+ .expect(500, '[stream.not.readable] stream is not readable', done)96+ })97+98 it('should handle duplicated middleware', function (done) {99 var app = express()100 101 app.use(express.raw())34 unmodified lines136 test.write(buf)137 test.expect(413, done)138 })139 140+ it('should 413 when inflated body over limit', function (done) {141+ var app = createApp({ limit: '1kb' })142+ var test = request(app).post('/')143+ test.set('Content-Encoding', 'gzip')144+ test.set('Content-Type', 'application/octet-stream')145+ test.write(Buffer.from('1f8b080000000000000ad3d31b05a360148c64000087e5a14704040000', 'hex'))146+ test.expect(413, done)147+ })148+149 it('should accept number of bytes', function (done) {150 var buf = Buffer.alloc(1028, '.')151 var app = createApp({ limit: 1024 })152 var test = request(app).post('/')24 unmodified lines177 test.write(buf)178 test.write(buf)179 test.expect(413, done)180 })181+182+ it('should not error when inflating', function (done) {183+ var app = createApp({ limit: '1kb' })184+ var test = request(app).post('/')185+ test.set('Content-Encoding', 'gzip')186+ test.set('Content-Type', 'application/octet-stream')187+ test.write(Buffer.from('1f8b080000000000000ad3d31b05a360148c64000087e5a147040400', 'hex'))188+ test.expect(413, done)189+ })190 })191 192 describe('with inflate option', function () {193 describe('when false', function () {5 unmodified lines199 var test = request(this.app).post('/')200 test.set('Content-Encoding', 'gzip')201 test.set('Content-Type', 'application/octet-stream')202 test.write(Buffer.from('1f8b080000000000000bcb4bcc4db57db16e170099a4bad608000000', 'hex'))150- test.expect(415, 'content encoding unsupported', done)203+ test.expect(415, '[encoding.unsupported] content encoding unsupported', done)204 })205 })206 207 describe('when true', function () {107 unmodified lines315 /TypeError: option verify must be function/)316 })317 318 it('should error from verify', function (done) {319+ var app = createApp({266- var app = createApp({ verify: function (req, res, buf) {267- if (buf[0] === 0x00) throw new Error('no leading null')268- } })320+ verify: function (req, res, buf) {321+ if (buf[0] === 0x00) throw new Error('no leading null')322+ }323+ })324 325 var test = request(app).post('/')326 test.set('Content-Type', 'application/octet-stream')327 test.write(Buffer.from('000102', 'hex'))273- test.expect(403, 'no leading null', done)328+ test.expect(403, '[entity.verify.failed] no leading null', done)329 })330 331 it('should allow custom codes', function (done) {332+ var app = createApp({277- var app = createApp({ verify: function (req, res, buf) {278- if (buf[0] !== 0x00) return279- var err = new Error('no leading null')280- err.status = 400281- throw err282- } })333+ verify: function (req, res, buf) {334+ if (buf[0] !== 0x00) return335+ var err = new Error('no leading null')336+ err.status = 400337+ throw err338+ }339+ })340 341 var test = request(app).post('/')342 test.set('Content-Type', 'application/octet-stream')343 test.write(Buffer.from('000102', 'hex'))287- test.expect(400, 'no leading null', done)344+ test.expect(400, '[entity.verify.failed] no leading null', done)345 })346 347 it('should allow pass-through', function (done) {348+ var app = createApp({291- var app = createApp({ verify: function (req, res, buf) {292- if (buf[0] === 0x00) throw new Error('no leading null')293- } })349+ verify: function (req, res, buf) {350+ if (buf[0] === 0x00) throw new Error('no leading null')351+ }352+ })353 354 var test = request(app).post('/')355 test.set('Content-Type', 'application/octet-stream')356 test.write(Buffer.from('0102', 'hex'))357 test.expect(200, { buf: '0102' }, done)358 })359 })360 361+ describeAsyncHooks('async local storage', function () {362+ before(function () {363+ var app = express()364+ var store = { foo: 'bar' }365+366+ app.use(function (req, res, next) {367+ req.asyncLocalStorage = new asyncHooks.AsyncLocalStorage()368+ req.asyncLocalStorage.run(store, next)369+ })370+371+ app.use(express.raw())372+373+ app.use(function (req, res, next) {374+ var local = req.asyncLocalStorage.getStore()375+376+ if (local) {377+ res.setHeader('x-store-foo', String(local.foo))378+ }379+380+ next()381+ })382+383+ app.use(function (err, req, res, next) {384+ var local = req.asyncLocalStorage.getStore()385+386+ if (local) {387+ res.setHeader('x-store-foo', String(local.foo))388+ }389+390+ res.status(err.status || 500)391+ res.send('[' + err.type + '] ' + err.message)392+ })393+394+ app.post('/', function (req, res) {395+ if (Buffer.isBuffer(req.body)) {396+ res.json({ buf: req.body.toString('hex') })397+ } else {398+ res.json(req.body)399+ }400+ })401+402+ this.app = app403+ })404+405+ it('should presist store', function (done) {406+ request(this.app)407+ .post('/')408+ .set('Content-Type', 'application/octet-stream')409+ .send('the user is tobi')410+ .expect(200)411+ .expect('x-store-foo', 'bar')412+ .expect({ buf: '746865207573657220697320746f6269' })413+ .end(done)414+ })415+416+ it('should presist store when unmatched content-type', function (done) {417+ request(this.app)418+ .post('/')419+ .set('Content-Type', 'application/fizzbuzz')420+ .send('buzz')421+ .expect(200)422+ .expect('x-store-foo', 'bar')423+ .expect('{}')424+ .end(done)425+ })426+427+ it('should presist store when inflated', function (done) {428+ var test = request(this.app).post('/')429+ test.set('Content-Encoding', 'gzip')430+ test.set('Content-Type', 'application/octet-stream')431+ test.write(Buffer.from('1f8b080000000000000bcb4bcc4db57db16e170099a4bad608000000', 'hex'))432+ test.expect(200)433+ test.expect('x-store-foo', 'bar')434+ test.expect({ buf: '6e616d653de8aeba' })435+ test.end(done)436+ })437+438+ it('should presist store when inflate error', function (done) {439+ var test = request(this.app).post('/')440+ test.set('Content-Encoding', 'gzip')441+ test.set('Content-Type', 'application/octet-stream')442+ test.write(Buffer.from('1f8b080000000000000bcb4bcc4db57db16e170099a4bad6080000', 'hex'))443+ test.expect(400)444+ test.expect('x-store-foo', 'bar')445+ test.end(done)446+ })447+448+ it('should presist store when limit exceeded', function (done) {449+ request(this.app)450+ .post('/')451+ .set('Content-Type', 'application/octet-stream')452+ .send('the user is ' + Buffer.alloc(1024 * 100, '.').toString())453+ .expect(413)454+ .expect('x-store-foo', 'bar')455+ .end(done)456+ })457+ })458+459 describe('charset', function () {460 before(function () {461 this.app = createApp()462 })49 unmodified lines512 test.write(Buffer.from('1f8b080000000000000bcb4bcc4db57db16e170099a4bad608000000', 'hex'))513 test.expect(200, { buf: '6e616d653de8aeba' }, done)514 })515 359- it('should fail on unknown encoding', function (done) {516+ it('should 415 on unknown encoding', function (done) {517 var test = request(this.app).post('/')518 test.set('Content-Encoding', 'nulls')519 test.set('Content-Type', 'application/octet-stream')520 test.write(Buffer.from('000000000000', 'hex'))364- test.expect(415, 'unsupported content encoding "nulls"', done)521+ test.expect(415, '[encoding.unsupported] unsupported content encoding "nulls"', done)522 })523 })524 })525 3 unmodified lines529 app.use(express.raw(options))530 531 app.use(function (err, req, res, next) {532 res.status(err.status || 500)376- res.send(String(err[req.headers['x-error-property'] || 'message']))533+ res.send(String(req.headers['x-error-property']534+ ? err[req.headers['x-error-property']]535+ : ('[' + err.type + '] ' + err.message)))536 })537 538 app.post('/', function (req, res) {539 if (Buffer.isBuffer(req.body)) {4 unmodified lines544 })545 546 return app547 }548+549+function tryRequire (name) {550+ try {551+ return require(name)552+ } catch (e) {553+ return {}554+ }555+}modified:test/express.text.js+192 added-29 removed
1 'use strict'2 3 var assert = require('assert')4+var asyncHooks = tryRequire('async_hooks')5 var Buffer = require('safe-buffer').Buffer6 var express = require('..')7 var request = require('supertest')8 9+var describeAsyncHooks = typeof asyncHooks.AsyncLocalStorage === 'function'10+ ? describe11+ : describe.skip12+13 describe('express.text()', function () {14 before(function () {15 this.app = createApp()16 })43 unmodified lines60 .send('')61 .expect(200, '""', done)62 })63 64+ it('should 500 if stream not readable', function (done) {65+ var app = express()66+67+ app.use(function (req, res, next) {68+ req.on('end', next)69+ req.resume()70+ })71+72+ app.use(express.text())73+74+ app.use(function (err, req, res, next) {75+ res.status(err.status || 500)76+ res.send('[' + err.type + '] ' + err.message)77+ })78+79+ app.post('/', function (req, res) {80+ res.json(req.body)81+ })82+83+ request(app)84+ .post('/')85+ .set('Content-Type', 'text/plain')86+ .send('user is tobi')87+ .expect(500, '[stream.not.readable] stream is not readable', done)88+ })89+90 it('should handle duplicated middleware', function (done) {91 var app = express()92 93 app.use(express.text())11 unmodified lines105 })106 107 describe('with defaultCharset option', function () {108 it('should change default charset', function (done) {78- var app = createApp({ defaultCharset: 'koi8-r' })79- var test = request(app).post('/')109+ var server = createApp({ defaultCharset: 'koi8-r' })110+ var test = request(server).post('/')111 test.set('Content-Type', 'text/plain')112 test.write(Buffer.from('6e616d6520697320cec5d4', 'hex'))113 test.expect(200, '"name is нет"', done)114 })115 116 it('should honor content-type charset', function (done) {86- var app = createApp({ defaultCharset: 'koi8-r' })87- var test = request(app).post('/')117+ var server = createApp({ defaultCharset: 'koi8-r' })118+ var test = request(server).post('/')119 test.set('Content-Type', 'text/plain; charset=utf-8')120 test.write(Buffer.from('6e616d6520697320e8aeba', 'hex'))121 test.expect(200, '"name is 论"', done)122 })10 unmodified lines133 .expect(413, done)134 })135 136 it('should 413 when over limit with chunked encoding', function (done) {106- var buf = Buffer.alloc(1028, '.')137 var app = createApp({ limit: '1kb' })138+ var buf = Buffer.alloc(1028, '.')139 var test = request(app).post('/')140 test.set('Content-Type', 'text/plain')141 test.set('Transfer-Encoding', 'chunked')142 test.write(buf.toString())143 test.expect(413, done)144 })145 146+ 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', 'text/plain')151+ test.write(Buffer.from('1f8b080000000000000ad3d31b05a360148c64000087e5a14704040000', 'hex'))152+ test.expect(413, done)153+ })154+155 it('should accept number of bytes', function (done) {156 var buf = Buffer.alloc(1028, '.')157 request(createApp({ limit: 1024 }))158 .post('/')16 unmodified lines175 .expect(413, done)176 })177 178 it('should not hang response', function (done) {139- var buf = Buffer.alloc(10240, '.')179 var app = createApp({ limit: '8kb' })180+ var buf = Buffer.alloc(10240, '.')181 var test = request(app).post('/')182 test.set('Content-Type', 'text/plain')183 test.write(buf)184 test.write(buf)185 test.write(buf)186 test.expect(413, done)187 })188+189+ 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', 'text/plain')194+ test.write(Buffer.from('1f8b080000000000000ad3d31b05a360148c64000087e5a1470404', 'hex'))195+ setTimeout(function () {196+ test.expect(413, done)197+ }, 100)198+ })199 })200 201 describe('with inflate option', function () {202 describe('when false', function () {5 unmodified lines208 var test = request(this.app).post('/')209 test.set('Content-Encoding', 'gzip')210 test.set('Content-Type', 'text/plain')211 test.write(Buffer.from('1f8b080000000000000bcb4bcc4d55c82c5678b16e170072b3e0200b000000', 'hex'))161- test.expect(415, 'content encoding unsupported', done)212+ test.expect(415, '[encoding.unsupported] content encoding unsupported', done)213 })214 })215 216 describe('when true', function () {111 unmodified lines328 /TypeError: option verify must be function/)329 })330 331 it('should error from verify', function (done) {332+ var app = createApp({281- var app = createApp({ verify: function (req, res, buf) {282- if (buf[0] === 0x20) throw new Error('no leading space')283- } })333+ verify: function (req, res, buf) {334+ if (buf[0] === 0x20) throw new Error('no leading space')335+ }336+ })337 338 request(app)339 .post('/')340 .set('Content-Type', 'text/plain')341 .send(' user is tobi')289- .expect(403, 'no leading space', done)342+ .expect(403, '[entity.verify.failed] no leading space', done)343 })344 345 it('should allow custom codes', function (done) {346+ var app = createApp({293- var app = createApp({ verify: function (req, res, buf) {294- if (buf[0] !== 0x20) return295- var err = new Error('no leading space')296- err.status = 400297- throw err298- } })347+ verify: function (req, res, buf) {348+ if (buf[0] !== 0x20) return349+ var err = new Error('no leading space')350+ err.status = 400351+ throw err352+ }353+ })354 355 request(app)356 .post('/')357 .set('Content-Type', 'text/plain')358 .send(' user is tobi')304- .expect(400, 'no leading space', done)359+ .expect(400, '[entity.verify.failed] no leading space', done)360 })361 362 it('should allow pass-through', function (done) {363+ var app = createApp({308- var app = createApp({ verify: function (req, res, buf) {309- if (buf[0] === 0x20) throw new Error('no leading space')310- } })364+ verify: function (req, res, buf) {365+ if (buf[0] === 0x20) throw new Error('no leading space')366+ }367+ })368 369 request(app)370 .post('/')371 .set('Content-Type', 'text/plain')1 unmodified line373 .expect(200, '"user is tobi"', done)374 })375 376 it('should 415 on unknown charset prior to verify', function (done) {377+ var app = createApp({320- var app = createApp({ verify: function (req, res, buf) {321- throw new Error('unexpected verify call')322- } })378+ verify: function (req, res, buf) {379+ throw new Error('unexpected verify call')380+ }381+ })382 383 var test = request(app).post('/')384 test.set('Content-Type', 'text/plain; charset=x-bogus')385 test.write(Buffer.from('00000000', 'hex'))327- test.expect(415, 'unsupported charset "X-BOGUS"', done)386+ test.expect(415, '[charset.unsupported] unsupported charset "X-BOGUS"', done)387 })388 })389 390+ describeAsyncHooks('async local storage', function () {391+ before(function () {392+ var app = express()393+ var store = { foo: 'bar' }394+395+ app.use(function (req, res, next) {396+ req.asyncLocalStorage = new asyncHooks.AsyncLocalStorage()397+ req.asyncLocalStorage.run(store, next)398+ })399+400+ app.use(express.text())401+402+ app.use(function (req, res, next) {403+ var local = req.asyncLocalStorage.getStore()404+405+ if (local) {406+ res.setHeader('x-store-foo', String(local.foo))407+ }408+409+ next()410+ })411+412+ app.use(function (err, req, res, next) {413+ var local = req.asyncLocalStorage.getStore()414+415+ if (local) {416+ res.setHeader('x-store-foo', String(local.foo))417+ }418+419+ res.status(err.status || 500)420+ res.send('[' + err.type + '] ' + err.message)421+ })422+423+ app.post('/', function (req, res) {424+ res.json(req.body)425+ })426+427+ this.app = app428+ })429+430+ it('should presist store', function (done) {431+ request(this.app)432+ .post('/')433+ .set('Content-Type', 'text/plain')434+ .send('user is tobi')435+ .expect(200)436+ .expect('x-store-foo', 'bar')437+ .expect('"user is tobi"')438+ .end(done)439+ })440+441+ it('should presist store when unmatched content-type', function (done) {442+ request(this.app)443+ .post('/')444+ .set('Content-Type', 'application/fizzbuzz')445+ .send('buzz')446+ .expect(200)447+ .expect('x-store-foo', 'bar')448+ .expect('{}')449+ .end(done)450+ })451+452+ it('should presist store when inflated', function (done) {453+ var test = request(this.app).post('/')454+ test.set('Content-Encoding', 'gzip')455+ test.set('Content-Type', 'text/plain')456+ test.write(Buffer.from('1f8b080000000000000bcb4bcc4d55c82c5678b16e170072b3e0200b000000', 'hex'))457+ test.expect(200)458+ test.expect('x-store-foo', 'bar')459+ test.expect('"name is 论"')460+ test.end(done)461+ })462+463+ it('should presist store when inflate error', function (done) {464+ var test = request(this.app).post('/')465+ test.set('Content-Encoding', 'gzip')466+ test.set('Content-Type', 'text/plain')467+ test.write(Buffer.from('1f8b080000000000000bcb4bcc4d55c82c5678b16e170072b3e0200b0000', 'hex'))468+ test.expect(400)469+ test.expect('x-store-foo', 'bar')470+ test.end(done)471+ })472+473+ it('should presist store when limit exceeded', function (done) {474+ request(this.app)475+ .post('/')476+ .set('Content-Type', 'text/plain')477+ .send('user is ' + Buffer.alloc(1024 * 100, '.').toString())478+ .expect(413)479+ .expect('x-store-foo', 'bar')480+ .end(done)481+ })482+ })483+484 describe('charset', function () {485 before(function () {486 this.app = createApp()487 })30 unmodified lines518 it('should 415 on unknown charset', function (done) {519 var test = request(this.app).post('/')520 test.set('Content-Type', 'text/plain; charset=x-bogus')521 test.write(Buffer.from('00000000', 'hex'))369- test.expect(415, 'unsupported charset "X-BOGUS"', done)522+ test.expect(415, '[charset.unsupported] unsupported charset "X-BOGUS"', done)523 })524 })525 526 describe('encoding', function () {39 unmodified lines566 test.write(Buffer.from('1f8b080000000000000bcb4bcc4d55c82c5678b16e170072b3e0200b000000', 'hex'))567 test.expect(200, '"name is 论"', done)568 })569 417- it('should fail on unknown encoding', function (done) {570+ it('should 415 on unknown encoding', function (done) {571 var test = request(this.app).post('/')572 test.set('Content-Encoding', 'nulls')573 test.set('Content-Type', 'text/plain')574 test.write(Buffer.from('000000000000', 'hex'))422- test.expect(415, 'unsupported content encoding "nulls"', done)575+ test.expect(415, '[encoding.unsupported] unsupported content encoding "nulls"', done)576 })577 })578 })579 3 unmodified lines583 app.use(express.text(options))584 585 app.use(function (err, req, res, next) {586 res.status(err.status || 500)434- res.send(err.message)587+ res.send(String(req.headers['x-error-property']588+ ? err[req.headers['x-error-property']]589+ : ('[' + err.type + '] ' + err.message)))590 })591 592 app.post('/', function (req, res) {593 res.json(req.body)594 })595 596 return app597 }598+599+function tryRequire (name) {600+ try {601+ return require(name)602+ } catch (e) {603+ return {}604+ }605+}modified:test/express.urlencoded.js+197 added-66 removed
1 'use strict'2 3 var assert = require('assert')4+var asyncHooks = tryRequire('async_hooks')5 var Buffer = require('safe-buffer').Buffer6 var express = require('..')7 var request = require('supertest')8 9+var describeAsyncHooks = typeof asyncHooks.AsyncLocalStorage === 'function'10+ ? describe11+ : describe.skip12+13 describe('express.urlencoded()', function () {14 before(function () {15 this.app = createApp()16 })44 unmodified lines61 .send('')62 .expect(200, '{}', done)63 })64 65+ it('should 500 if stream not readable', function (done) {66+ var app = express()67+68+ app.use(function (req, res, next) {69+ req.on('end', next)70+ req.resume()71+ })72+73+ app.use(express.urlencoded())74+75+ app.use(function (err, req, res, next) {76+ res.status(err.status || 500)77+ res.send('[' + err.type + '] ' + err.message)78+ })79+80+ app.post('/', function (req, res) {81+ res.json(req.body)82+ })83+84+ request(app)85+ .post('/')86+ .set('Content-Type', 'application/x-www-form-urlencoded')87+ .send('user=tobi')88+ .expect(500, '[stream.not.readable] stream is not readable', done)89+ })90+91 it('should handle duplicated middleware', function (done) {92 var app = express()93 94 app.use(express.urlencoded())152 unmodified lines247 var test = request(this.app).post('/')248 test.set('Content-Encoding', 'gzip')249 test.set('Content-Type', 'application/x-www-form-urlencoded')250 test.write(Buffer.from('1f8b080000000000000bcb4bcc4db57db16e170099a4bad608000000', 'hex'))220- test.expect(415, 'content encoding unsupported', done)251+ test.expect(415, '[encoding.unsupported] content encoding unsupported', done)252 })253 })254 255 describe('when true', function () {22 unmodified lines278 .expect(413, done)279 })280 281 it('should 413 when over limit with chunked encoding', function (done) {251- var buf = Buffer.alloc(1024, '.')282 var app = createApp({ limit: '1kb' })283+ var buf = Buffer.alloc(1024, '.')284 var test = request(app).post('/')285 test.set('Content-Type', 'application/x-www-form-urlencoded')286 test.set('Transfer-Encoding', 'chunked')287 test.write('str=')288 test.write(buf.toString())289 test.expect(413, done)290 })291 292+ it('should 413 when inflated body over limit', function (done) {293+ var app = createApp({ limit: '1kb' })294+ var test = request(app).post('/')295+ test.set('Content-Encoding', 'gzip')296+ test.set('Content-Type', 'application/x-www-form-urlencoded')297+ test.write(Buffer.from('1f8b080000000000000a2b2e29b2d51b05a360148c580000a0351f9204040000', 'hex'))298+ test.expect(413, done)299+ })300+301 it('should accept number of bytes', function (done) {302 var buf = Buffer.alloc(1024, '.')303 request(createApp({ limit: 1024 }))304 .post('/')16 unmodified lines321 .expect(413, done)322 })323 324 it('should not hang response', function (done) {285- var buf = Buffer.alloc(10240, '.')325 var app = createApp({ limit: '8kb' })326+ var buf = Buffer.alloc(10240, '.')327 var test = request(app).post('/')328 test.set('Content-Type', 'application/x-www-form-urlencoded')329 test.write(buf)330 test.write(buf)331 test.write(buf)332 test.expect(413, done)333 })334+335+ it('should not error when inflating', function (done) {336+ var app = createApp({ limit: '1kb' })337+ var test = request(app).post('/')338+ test.set('Content-Encoding', 'gzip')339+ test.set('Content-Type', 'application/x-www-form-urlencoded')340+ test.write(Buffer.from('1f8b080000000000000a2b2e29b2d51b05a360148c580000a0351f92040400', 'hex'))341+ test.expect(413, done)342+ })343 })344 345 describe('with parameterLimit option', function () {346 describe('with extended: false', function () {11 unmodified lines358 request(createApp({ extended: false, parameterLimit: 10 }))359 .post('/')360 .set('Content-Type', 'application/x-www-form-urlencoded')361 .send(createManyParams(11))313- .expect(413, /too many parameters/, done)362+ .expect(413, '[parameters.too.many] too many parameters', done)363 })364 316- it('should error with type = "parameters.too.many"', function (done) {317- request(createApp({ extended: false, parameterLimit: 10 }))318- .post('/')319- .set('Content-Type', 'application/x-www-form-urlencoded')320- .set('X-Error-Property', 'type')321- .send(createManyParams(11))322- .expect(413, 'parameters.too.many', done)323- })324-365 it('should work when at the limit', function (done) {366 request(createApp({ extended: false, parameterLimit: 10 }))367 .post('/')368 .set('Content-Type', 'application/x-www-form-urlencoded')44 unmodified lines413 request(createApp({ extended: true, parameterLimit: 10 }))414 .post('/')415 .set('Content-Type', 'application/x-www-form-urlencoded')416 .send(createManyParams(11))377- .expect(413, /too many parameters/, done)417+ .expect(413, '[parameters.too.many] too many parameters', done)418 })419 380- it('should error with type = "parameters.too.many"', function (done) {381- request(createApp({ extended: true, parameterLimit: 10 }))382- .post('/')383- .set('Content-Type', 'application/x-www-form-urlencoded')384- .set('X-Error-Property', 'type')385- .send(createManyParams(11))386- .expect(413, 'parameters.too.many', done)387- })388-420 it('should work when at the limit', function (done) {421 request(createApp({ extended: true, parameterLimit: 10 }))422 .post('/')423 .set('Content-Type', 'application/x-www-form-urlencoded')132 unmodified lines556 /TypeError: option verify must be function/)557 })558 559 it('should error from verify', function (done) {560+ var app = createApp({529- var app = createApp({ verify: function (req, res, buf) {530- if (buf[0] === 0x20) throw new Error('no leading space')531- } })561+ verify: function (req, res, buf) {562+ if (buf[0] === 0x20) throw new Error('no leading space')563+ }564+ })565 566 request(app)567 .post('/')568 .set('Content-Type', 'application/x-www-form-urlencoded')569 .send(' user=tobi')537- .expect(403, 'no leading space', done)570+ .expect(403, '[entity.verify.failed] no leading space', done)571 })572 540- it('should error with type = "entity.verify.failed"', function (done) {541- var app = createApp({ verify: function (req, res, buf) {542- if (buf[0] === 0x20) throw new Error('no leading space')543- } })544-545- request(app)546- .post('/')547- .set('Content-Type', 'application/x-www-form-urlencoded')548- .set('X-Error-Property', 'type')549- .send(' user=tobi')550- .expect(403, 'entity.verify.failed', done)551- })552-573 it('should allow custom codes', function (done) {574+ var app = createApp({554- var app = createApp({ verify: function (req, res, buf) {555- if (buf[0] !== 0x20) return556- var err = new Error('no leading space')557- err.status = 400558- throw err559- } })575+ verify: function (req, res, buf) {576+ if (buf[0] !== 0x20) return577+ var err = new Error('no leading space')578+ err.status = 400579+ throw err580+ }581+ })582 583 request(app)584 .post('/')585 .set('Content-Type', 'application/x-www-form-urlencoded')586 .send(' user=tobi')565- .expect(400, 'no leading space', done)587+ .expect(400, '[entity.verify.failed] no leading space', done)588 })589 590 it('should allow custom type', function (done) {591+ var app = createApp({569- var app = createApp({ verify: function (req, res, buf) {570- if (buf[0] !== 0x20) return571- var err = new Error('no leading space')572- err.type = 'foo.bar'573- throw err574- } })592+ verify: function (req, res, buf) {593+ if (buf[0] !== 0x20) return594+ var err = new Error('no leading space')595+ err.type = 'foo.bar'596+ throw err597+ }598+ })599 600 request(app)601 .post('/')602 .set('Content-Type', 'application/x-www-form-urlencoded')579- .set('X-Error-Property', 'type')603 .send(' user=tobi')581- .expect(403, 'foo.bar', done)604+ .expect(403, '[foo.bar] no leading space', done)605 })606 607 it('should allow pass-through', function (done) {608+ var app = createApp({585- var app = createApp({ verify: function (req, res, buf) {586- if (buf[0] === 0x5b) throw new Error('no arrays')587- } })609+ verify: function (req, res, buf) {610+ if (buf[0] === 0x5b) throw new Error('no arrays')611+ }612+ })613 614 request(app)615 .post('/')616 .set('Content-Type', 'application/x-www-form-urlencoded')1 unmodified line618 .expect(200, '{"user":"tobi"}', done)619 })620 621 it('should 415 on unknown charset prior to verify', function (done) {622+ var app = createApp({597- var app = createApp({ verify: function (req, res, buf) {598- throw new Error('unexpected verify call')599- } })623+ verify: function (req, res, buf) {624+ throw new Error('unexpected verify call')625+ }626+ })627 628 var test = request(app).post('/')629 test.set('Content-Type', 'application/x-www-form-urlencoded; charset=x-bogus')630 test.write(Buffer.from('00000000', 'hex'))604- test.expect(415, 'unsupported charset "X-BOGUS"', done)631+ test.expect(415, '[charset.unsupported] unsupported charset "X-BOGUS"', done)632 })633 })634 635+ describeAsyncHooks('async local storage', function () {636+ before(function () {637+ var app = express()638+ var store = { foo: 'bar' }639+640+ app.use(function (req, res, next) {641+ req.asyncLocalStorage = new asyncHooks.AsyncLocalStorage()642+ req.asyncLocalStorage.run(store, next)643+ })644+645+ app.use(express.urlencoded())646+647+ app.use(function (req, res, next) {648+ var local = req.asyncLocalStorage.getStore()649+650+ if (local) {651+ res.setHeader('x-store-foo', String(local.foo))652+ }653+654+ next()655+ })656+657+ app.use(function (err, req, res, next) {658+ var local = req.asyncLocalStorage.getStore()659+660+ if (local) {661+ res.setHeader('x-store-foo', String(local.foo))662+ }663+664+ res.status(err.status || 500)665+ res.send('[' + err.type + '] ' + err.message)666+ })667+668+ app.post('/', function (req, res) {669+ res.json(req.body)670+ })671+672+ this.app = app673+ })674+675+ it('should presist store', function (done) {676+ request(this.app)677+ .post('/')678+ .set('Content-Type', 'application/x-www-form-urlencoded')679+ .send('user=tobi')680+ .expect(200)681+ .expect('x-store-foo', 'bar')682+ .expect('{"user":"tobi"}')683+ .end(done)684+ })685+686+ it('should presist store when unmatched content-type', function (done) {687+ request(this.app)688+ .post('/')689+ .set('Content-Type', 'application/fizzbuzz')690+ .send('buzz')691+ .expect(200)692+ .expect('x-store-foo', 'bar')693+ .expect('{}')694+ .end(done)695+ })696+697+ it('should presist store when inflated', function (done) {698+ var test = request(this.app).post('/')699+ test.set('Content-Encoding', 'gzip')700+ test.set('Content-Type', 'application/x-www-form-urlencoded')701+ test.write(Buffer.from('1f8b080000000000000bcb4bcc4db57db16e170099a4bad608000000', 'hex'))702+ test.expect(200)703+ test.expect('x-store-foo', 'bar')704+ test.expect('{"name":"论"}')705+ test.end(done)706+ })707+708+ it('should presist store when inflate error', function (done) {709+ var test = request(this.app).post('/')710+ test.set('Content-Encoding', 'gzip')711+ test.set('Content-Type', 'application/x-www-form-urlencoded')712+ test.write(Buffer.from('1f8b080000000000000bcb4bcc4db57db16e170099a4bad6080000', 'hex'))713+ test.expect(400)714+ test.expect('x-store-foo', 'bar')715+ test.end(done)716+ })717+718+ it('should presist store when limit exceeded', function (done) {719+ request(this.app)720+ .post('/')721+ .set('Content-Type', 'application/x-www-form-urlencoded')722+ .send('user=' + Buffer.alloc(1024 * 100, '.').toString())723+ .expect(413)724+ .expect('x-store-foo', 'bar')725+ .end(done)726+ })727+ })728+729 describe('charset', function () {730 before(function () {731 this.app = createApp()732 })23 unmodified lines756 it('should fail on unknown charset', function (done) {757 var test = request(this.app).post('/')758 test.set('Content-Type', 'application/x-www-form-urlencoded; charset=koi8-r')759 test.write(Buffer.from('6e616d653dcec5d4', 'hex'))639- test.expect(415, 'unsupported charset "KOI8-R"', done)760+ test.expect(415, '[charset.unsupported] unsupported charset "KOI8-R"', done)761 })762 })763 764 describe('encoding', function () {39 unmodified lines804 test.write(Buffer.from('1f8b080000000000000bcb4bcc4db57db16e170099a4bad608000000', 'hex'))805 test.expect(200, '{"name":"论"}', done)806 })807 687- it('should fail on unknown encoding', function (done) {808+ it('should 415 on unknown encoding', function (done) {809 var test = request(this.app).post('/')810 test.set('Content-Encoding', 'nulls')811 test.set('Content-Type', 'application/x-www-form-urlencoded')812 test.write(Buffer.from('000000000000', 'hex'))692- test.expect(415, 'unsupported content encoding "nulls"', done)813+ test.expect(415, '[encoding.unsupported] unsupported content encoding "nulls"', done)814 })815 })816 })817 20 unmodified lines838 app.use(express.urlencoded(options))839 840 app.use(function (err, req, res, next) {841 res.status(err.status || 500)721- res.send(String(err[req.headers['x-error-property'] || 'message']))842+ res.send(String(req.headers['x-error-property']843+ ? err[req.headers['x-error-property']]844+ : ('[' + err.type + '] ' + err.message)))845 })846 847 app.post('/', function (req, res) {848 res.json(req.body)6 unmodified lines855 return function (res) {856 assert.strictEqual(Object.keys(JSON.parse(res.text)).length, count)857 }858 }859+860+function tryRequire (name) {861+ try {862+ return require(name)863+ } catch (e) {864+ return {}865+ }866+}modified:History.md+10 added
7 unmodified lines8 * Ignore `Object.prototype` values in settings through `app.set`/`app.get`9 * Invoke `default` with same arguments as types in `res.format`10 * Support proper 205 responses using `res.send`11 * Use `http-errors` for `res.format` error12+ * deps: body-parser@1.20.013+ - Fix error message for json parse whitespace in `strict`14+ - Fix internal error when inflated body exceeds limit15+ - Prevent loss of async hooks context16+ - Prevent hanging when request already read17+ - deps: depd@2.0.018+ - deps: http-errors@2.0.019+ - deps: on-finished@2.4.120+ - deps: qs@6.10.321+ - deps: raw-body@2.5.122 * deps: depd@2.0.023 - Replace internal `eval` usage with `Function` constructor24 - Use instance methods on `process` to check for listeners25 * deps: finalhandler@1.2.03539 unmodified linesmodified:package.json+1 added-1 removed
28 unmodified lines2930313233333435363762 unmodified lines28 unmodified lines ], "dependencies": { "accepts": "~1.3.8", "array-flatten": "1.1.1", "body-parser": "1.19.2", "body-parser": "1.20.0", "content-disposition": "0.5.4", "content-type": "~1.0.4", "cookie": "0.4.2", "cookie-signature": "1.0.6",62 unmodified lines