express.text.js
This file
Path
test/express.text.js
578 lines · 17.1 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–82why, one step back13describe('express.text()', function () {14 before(function () {15 this.app = createApp()16 })1718 it('should parse text/plain', function (done) {19 request(this.app)20 .post('/')21 .set('Content-Type', 'text/plain')22 .send('user is tobi')23 .expect(200, '"user is tobi"', done)24 })2526 it('should 400 when invalid content-length', function (done) {27 var app = express()2829 app.use(function (req, res, next) {30 req.headers['content-length'] = '20' // bad length31 next()32 })3334 app.use(express.text())3536 app.post('/', function (req, res) {37 res.json(req.body)38 })3940 request(app)41 .post('/')42 .set('Content-Type', 'text/plain')43 .send('user')44 .expect(400, /content length/, done)45 })4647 it('should handle Content-Length: 0', function (done) {48 request(createApp({ limit: '1kb' }))49 .post('/')50 .set('Content-Type', 'text/plain')51 .set('Content-Length', '0')52 .expect(200, '""', done)53 })5455 it('should handle empty message-body', function (done) {56 request(createApp({ limit: '1kb' }))57 .post('/')58 .set('Content-Type', 'text/plain')59 .set('Transfer-Encoding', 'chunked')60 .send('')61 .expect(200, '""', done)62 })6364 it('should handle duplicated middleware', function (done) {65 var app = express()6667 app.use(express.text())68 app.use(express.text())6970 app.post('/', function (req, res) {71 res.json(req.body)72 })7374 request(app)75 .post('/')76 .set('Content-Type', 'text/plain')77 .send('user is tobi')78 .expect(200, '"user is tobi"', done)79 })8081 describe('with defaultCharset option', function () {82 it('should change default charset', function (done) {Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 83–84why, one step back83 var server = createApp({ defaultCharset: 'koi8-r' })84 var test = request(server).post('/')Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 85–90why, one step back85 test.set('Content-Type', 'text/plain')86 test.write(Buffer.from('6e616d6520697320cec5d4', 'hex'))87 test.expect(200, '"name is нет"', done)88 })8990 it('should honor content-type charset', function (done) {Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 91–92why, one step back91 var server = createApp({ defaultCharset: 'koi8-r' })92 var test = request(server).post('/')Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 93–111why, one step back93 test.set('Content-Type', 'text/plain; charset=utf-8')94 test.write(Buffer.from('6e616d6520697320e8aeba', 'hex'))95 test.expect(200, '"name is 论"', done)96 })97 })9899 describe('with limit option', function () {100 it('should 413 when over limit with Content-Length', function (done) {101 var buf = Buffer.alloc(1028, '.')102 request(createApp({ limit: '1kb' }))103 .post('/')104 .set('Content-Type', 'text/plain')105 .set('Content-Length', '1028')106 .send(buf.toString())107 .expect(413, done)108 })109110 it('should 413 when over limit with chunked encoding', function (done) {111 var app = createApp({ limit: '1kb' })Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 112why, one step back112 var buf = Buffer.alloc(1028, '.')Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 113–119why, one step back113 var test = request(app).post('/')114 test.set('Content-Type', 'text/plain')115 test.set('Transfer-Encoding', 'chunked')116 test.write(buf.toString())117 test.expect(413, done)118 })119Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 120–128why, one step back120 it('should 413 when inflated body over limit', function (done) {121 var app = createApp({ limit: '1kb' })122 var test = request(app).post('/')123 test.set('Content-Encoding', 'gzip')124 test.set('Content-Type', 'text/plain')125 test.write(Buffer.from('1f8b080000000000000ad3d31b05a360148c64000087e5a14704040000', 'hex'))126 test.expect(413, done)127 })128Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 129–153why, one step back129 it('should accept number of bytes', function (done) {130 var buf = Buffer.alloc(1028, '.')131 request(createApp({ limit: 1024 }))132 .post('/')133 .set('Content-Type', 'text/plain')134 .send(buf.toString())135 .expect(413, done)136 })137138 it('should not change when options altered', function (done) {139 var buf = Buffer.alloc(1028, '.')140 var options = { limit: '1kb' }141 var app = createApp(options)142143 options.limit = '100kb'144145 request(app)146 .post('/')147 .set('Content-Type', 'text/plain')148 .send(buf.toString())149 .expect(413, done)150 })151152 it('should not hang response', function (done) {153 var app = createApp({ limit: '8kb' })Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 154why, one step back154 var buf = Buffer.alloc(10240, '.')Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 155–161why, one step back155 var test = request(app).post('/')156 test.set('Content-Type', 'text/plain')157 test.write(buf)158 test.write(buf)159 test.write(buf)160 test.expect(413, done)161 })Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 162–172why, one step back162163 it('should not error when inflating', function (done) {164 var app = createApp({ limit: '1kb' })165 var test = request(app).post('/')166 test.set('Content-Encoding', 'gzip')167 test.set('Content-Type', 'text/plain')168 test.write(Buffer.from('1f8b080000000000000ad3d31b05a360148c64000087e5a1470404', 'hex'))169 setTimeout(function () {170 test.expect(413, done)171 }, 100)172 })Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 173–185why, one step back173 })174175 describe('with inflate option', function () {176 describe('when false', function () {177 before(function () {178 this.app = createApp({ inflate: false })179 })180181 it('should not accept content-encoding', function (done) {182 var test = request(this.app).post('/')183 test.set('Content-Encoding', 'gzip')184 test.set('Content-Type', 'text/plain')185 test.write(Buffer.from('1f8b080000000000000bcb4bcc4d55c82c5678b16e170072b3e0200b000000', 'hex'))Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 186why, one step back186 test.expect(415, '[encoding.unsupported] content encoding unsupported', done)Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 187–223why, one step back187 })188 })189190 describe('when true', function () {191 before(function () {192 this.app = createApp({ inflate: true })193 })194195 it('should accept content-encoding', function (done) {196 var test = request(this.app).post('/')197 test.set('Content-Encoding', 'gzip')198 test.set('Content-Type', 'text/plain')199 test.write(Buffer.from('1f8b080000000000000bcb4bcc4d55c82c5678b16e170072b3e0200b000000', 'hex'))200 test.expect(200, '"name is 论"', done)201 })202 })203 })204205 describe('with type option', function () {206 describe('when "text/html"', function () {207 before(function () {208 this.app = createApp({ type: 'text/html' })209 })210211 it('should parse for custom type', function (done) {212 request(this.app)213 .post('/')214 .set('Content-Type', 'text/html')215 .send('<b>tobi</b>')216 .expect(200, '"<b>tobi</b>"', done)217 })218219 it('should ignore standard type', function (done) {220 request(this.app)221 .post('/')222 .set('Content-Type', 'text/plain')223 .send('user is tobi')deps: body-parser@2.0.0-beta.1Douglas Christopher Wilsonbefore adoptionaf341b0
line 224why, one step back224 .expect(200, '', done)Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 225–253why, one step back225 })226 })227228 describe('when ["text/html", "text/plain"]', function () {229 before(function () {230 this.app = createApp({ type: ['text/html', 'text/plain'] })231 })232233 it('should parse "text/html"', function (done) {234 request(this.app)235 .post('/')236 .set('Content-Type', 'text/html')237 .send('<b>tobi</b>')238 .expect(200, '"<b>tobi</b>"', done)239 })240241 it('should parse "text/plain"', function (done) {242 request(this.app)243 .post('/')244 .set('Content-Type', 'text/plain')245 .send('tobi')246 .expect(200, '"tobi"', done)247 })248249 it('should ignore "text/xml"', function (done) {250 request(this.app)251 .post('/')252 .set('Content-Type', 'text/xml')253 .send('<user>tobi</user>')deps: body-parser@2.0.0-beta.1Douglas Christopher Wilsonbefore adoptionaf341b0
line 254why, one step back254 .expect(200, '', done)Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 255–305why, one step back255 })256 })257258 describe('when a function', function () {259 it('should parse when truthy value returned', function (done) {260 var app = createApp({ type: accept })261262 function accept (req) {263 return req.headers['content-type'] === 'text/vnd.something'264 }265266 request(app)267 .post('/')268 .set('Content-Type', 'text/vnd.something')269 .send('user is tobi')270 .expect(200, '"user is tobi"', done)271 })272273 it('should work without content-type', function (done) {274 var app = createApp({ type: accept })275276 function accept (req) {277 return true278 }279280 var test = request(app).post('/')281 test.write('user is tobi')282 test.expect(200, '"user is tobi"', done)283 })284285 it('should not invoke without a body', function (done) {286 var app = createApp({ type: accept })287288 function accept (req) {289 throw new Error('oops!')290 }291292 request(app)293 .get('/')294 .expect(404, done)295 })296 })297 })298299 describe('with verify option', function () {300 it('should assert value is function', function () {301 assert.throws(createApp.bind(null, { verify: 'lol' }),302 /TypeError: option verify must be function/)303 })304305 it('should error from verify', function (done) {Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 306–310why, one step back306 var app = createApp({307 verify: function (req, res, buf) {308 if (buf[0] === 0x20) throw new Error('no leading space')309 }310 })Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 311–315why, one step back311312 request(app)313 .post('/')314 .set('Content-Type', 'text/plain')315 .send(' user is tobi')Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 316why, one step back316 .expect(403, '[entity.verify.failed] no leading space', done)Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 317–319why, one step back317 })318319 it('should allow custom codes', function (done) {Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 320–327why, one step back320 var app = createApp({321 verify: function (req, res, buf) {322 if (buf[0] !== 0x20) return323 var err = new Error('no leading space')324 err.status = 400325 throw err326 }327 })Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 328–332why, one step back328329 request(app)330 .post('/')331 .set('Content-Type', 'text/plain')332 .send(' user is tobi')Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 333why, one step back333 .expect(400, '[entity.verify.failed] no leading space', done)Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 334–336why, one step back334 })335336 it('should allow pass-through', function (done) {Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 337–341why, one step back337 var app = createApp({338 verify: function (req, res, buf) {339 if (buf[0] === 0x20) throw new Error('no leading space')340 }341 })Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 342–350why, one step back342343 request(app)344 .post('/')345 .set('Content-Type', 'text/plain')346 .send('user is tobi')347 .expect(200, '"user is tobi"', done)348 })349350 it('should 415 on unknown charset prior to verify', function (done) {Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 351–355why, one step back351 var app = createApp({352 verify: function (req, res, buf) {353 throw new Error('unexpected verify call')354 }355 })Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 356–359why, one step back356357 var test = request(app).post('/')358 test.set('Content-Type', 'text/plain; charset=x-bogus')359 test.write(Buffer.from('00000000', 'hex'))Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 360why, one step back360 test.expect(415, '[charset.unsupported] unsupported charset "X-BOGUS"', done)Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 361–363why, one step back361 })362 })363Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 364–456why, one step back364 describeAsyncHooks('async local storage', function () {365 before(function () {366 var app = express()367 var store = { foo: 'bar' }368369 app.use(function (req, res, next) {370 req.asyncLocalStorage = new asyncHooks.AsyncLocalStorage()371 req.asyncLocalStorage.run(store, next)372 })373374 app.use(express.text())375376 app.use(function (req, res, next) {377 var local = req.asyncLocalStorage.getStore()378379 if (local) {380 res.setHeader('x-store-foo', String(local.foo))381 }382383 next()384 })385386 app.use(function (err, req, res, next) {387 var local = req.asyncLocalStorage.getStore()388389 if (local) {390 res.setHeader('x-store-foo', String(local.foo))391 }392393 res.status(err.status || 500)394 res.send('[' + err.type + '] ' + err.message)395 })396397 app.post('/', function (req, res) {398 res.json(req.body)399 })400401 this.app = app402 })403404 it('should presist store', function (done) {405 request(this.app)406 .post('/')407 .set('Content-Type', 'text/plain')408 .send('user is tobi')409 .expect(200)410 .expect('x-store-foo', 'bar')411 .expect('"user is tobi"')412 .end(done)413 })414415 it('should presist store when unmatched content-type', function (done) {416 request(this.app)417 .post('/')418 .set('Content-Type', 'application/fizzbuzz')419 .send('buzz')420 .expect(200)421 .expect('x-store-foo', 'bar')422 .end(done)423 })424425 it('should presist store when inflated', function (done) {426 var test = request(this.app).post('/')427 test.set('Content-Encoding', 'gzip')428 test.set('Content-Type', 'text/plain')429 test.write(Buffer.from('1f8b080000000000000bcb4bcc4d55c82c5678b16e170072b3e0200b000000', 'hex'))430 test.expect(200)431 test.expect('x-store-foo', 'bar')432 test.expect('"name is 论"')433 test.end(done)434 })435436 it('should presist store when inflate error', function (done) {437 var test = request(this.app).post('/')438 test.set('Content-Encoding', 'gzip')439 test.set('Content-Type', 'text/plain')440 test.write(Buffer.from('1f8b080000000000000bcb4bcc4d55c82c5678b16e170072b3e0200b0000', 'hex'))441 test.expect(400)442 test.expect('x-store-foo', 'bar')443 test.end(done)444 })445446 it('should presist store when limit exceeded', function (done) {447 request(this.app)448 .post('/')449 .set('Content-Type', 'text/plain')450 .send('user is ' + Buffer.alloc(1024 * 100, '.').toString())451 .expect(413)452 .expect('x-store-foo', 'bar')453 .end(done)454 })455 })456Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 457–494why, one step back457 describe('charset', function () {458 before(function () {459 this.app = createApp()460 })461462 it('should parse utf-8', function (done) {463 var test = request(this.app).post('/')464 test.set('Content-Type', 'text/plain; charset=utf-8')465 test.write(Buffer.from('6e616d6520697320e8aeba', 'hex'))466 test.expect(200, '"name is 论"', done)467 })468469 it('should parse codepage charsets', function (done) {470 var test = request(this.app).post('/')471 test.set('Content-Type', 'text/plain; charset=koi8-r')472 test.write(Buffer.from('6e616d6520697320cec5d4', 'hex'))473 test.expect(200, '"name is нет"', done)474 })475476 it('should parse when content-length != char length', function (done) {477 var test = request(this.app).post('/')478 test.set('Content-Type', 'text/plain; charset=utf-8')479 test.set('Content-Length', '11')480 test.write(Buffer.from('6e616d6520697320e8aeba', 'hex'))481 test.expect(200, '"name is 论"', done)482 })483484 it('should default to utf-8', function (done) {485 var test = request(this.app).post('/')486 test.set('Content-Type', 'text/plain')487 test.write(Buffer.from('6e616d6520697320e8aeba', 'hex'))488 test.expect(200, '"name is 论"', done)489 })490491 it('should 415 on unknown charset', function (done) {492 var test = request(this.app).post('/')493 test.set('Content-Type', 'text/plain; charset=x-bogus')494 test.write(Buffer.from('00000000', 'hex'))Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 495why, one step back495 test.expect(415, '[charset.unsupported] unsupported charset "X-BOGUS"', done)Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 496–542why, one step back496 })497 })498499 describe('encoding', function () {500 before(function () {501 this.app = createApp({ limit: '10kb' })502 })503504 it('should parse without encoding', function (done) {505 var test = request(this.app).post('/')506 test.set('Content-Type', 'text/plain')507 test.write(Buffer.from('6e616d6520697320e8aeba', 'hex'))508 test.expect(200, '"name is 论"', done)509 })510511 it('should support identity encoding', function (done) {512 var test = request(this.app).post('/')513 test.set('Content-Encoding', 'identity')514 test.set('Content-Type', 'text/plain')515 test.write(Buffer.from('6e616d6520697320e8aeba', 'hex'))516 test.expect(200, '"name is 论"', done)517 })518519 it('should support gzip encoding', function (done) {520 var test = request(this.app).post('/')521 test.set('Content-Encoding', 'gzip')522 test.set('Content-Type', 'text/plain')523 test.write(Buffer.from('1f8b080000000000000bcb4bcc4d55c82c5678b16e170072b3e0200b000000', 'hex'))524 test.expect(200, '"name is 论"', done)525 })526527 it('should support deflate encoding', function (done) {528 var test = request(this.app).post('/')529 test.set('Content-Encoding', 'deflate')530 test.set('Content-Type', 'text/plain')531 test.write(Buffer.from('789ccb4bcc4d55c82c5678b16e17001a6f050e', 'hex'))532 test.expect(200, '"name is 论"', done)533 })534535 it('should be case-insensitive', function (done) {536 var test = request(this.app).post('/')537 test.set('Content-Encoding', 'GZIP')538 test.set('Content-Type', 'text/plain')539 test.write(Buffer.from('1f8b080000000000000bcb4bcc4d55c82c5678b16e170072b3e0200b000000', 'hex'))540 test.expect(200, '"name is 论"', done)541 })542Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 543why, one step back543 it('should 415 on unknown encoding', function (done) {Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 544–547why, one step back544 var test = request(this.app).post('/')545 test.set('Content-Encoding', 'nulls')546 test.set('Content-Type', 'text/plain')547 test.write(Buffer.from('000000000000', 'hex'))Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 548why, one step back548 test.expect(415, '[encoding.unsupported] unsupported content encoding "nulls"', done)Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 549–559why, one step back549 })550 })551})552553function createApp (options) {554 var app = express()555556 app.use(express.text(options))557558 app.use(function (err, req, res, next) {559 res.status(err.status || 500)Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 560–562why, one step back560 res.send(String(req.headers['x-error-property']561 ? err[req.headers['x-error-property']]562 : ('[' + err.type + '] ' + err.message)))Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 563–569why, one step back563 })564565 app.post('/', function (req, res) {566 res.json(req.body)567 })568569 return appMerge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 570–577why, one step back570}571572function tryRequire (name) {573 try {574 return require(name)575 } catch (e) {576 return {}577 }Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
line 578why, one step back578}