Skip to content
Myr

tests: add express.urlencoded test suite

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 955f2a5 · Browse files · 3 files+740 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+740 added−1 removed

  • tests:+740 added−1 removed

3 files+740 added−1 removed

modified:test/support/env.js+1 added-1 removed
modified:test/exports.js+5 added
added:test/express.urlencoded.js+734 added
12var assert = require('assert')3var Buffer = require('safe-buffer').Buffer4var express = require('..')5var request = require('supertest')67describe('express.urlencoded()', function () {8 before(function () {9 this.app = createApp()10 })1112 it('should parse x-www-form-urlencoded', function (done) {13 request(this.app)14 .post('/')15 .set('Content-Type', 'application/x-www-form-urlencoded')16 .send('user=tobi')17 .expect(200, '{"user":"tobi"}', done)18 })1920 it('should 400 when invalid content-length', function (done) {21 var app = express()2223 app.use(function (req, res, next) {24 req.headers['content-length'] = '20' // bad length25 next()26 })2728 app.use(express.urlencoded())2930 app.post('/', function (req, res) {31 res.json(req.body)32 })3334 request(app)35 .post('/')36 .set('Content-Type', 'application/x-www-form-urlencoded')37 .send('str=')38 .expect(400, /content length/, done)39 })4041 it('should handle Content-Length: 0', function (done) {42 request(this.app)43 .post('/')44 .set('Content-Type', 'application/x-www-form-urlencoded')45 .set('Content-Length', '0')46 .send('')47 .expect(200, '{}', done)48 })4950 it('should handle empty message-body', function (done) {51 request(createApp({ limit: '1kb' }))52 .post('/')53 .set('Content-Type', 'application/x-www-form-urlencoded')54 .set('Transfer-Encoding', 'chunked')55 .send('')56 .expect(200, '{}', done)57 })5859 it('should handle duplicated middleware', function (done) {60 var app = express()6162 app.use(express.urlencoded())63 app.use(express.urlencoded())6465 app.post('/', function (req, res) {66 res.json(req.body)67 })6869 request(app)70 .post('/')71 .set('Content-Type', 'application/x-www-form-urlencoded')72 .send('user=tobi')73 .expect(200, '{"user":"tobi"}', done)74 })7576 it('should parse extended syntax', function (done) {77 request(this.app)78 .post('/')79 .set('Content-Type', 'application/x-www-form-urlencoded')80 .send('user[name][first]=Tobi')81 .expect(200, '{"user":{"name":{"first":"Tobi"}}}', done)82 })8384 describe('with extended option', function () {85 describe('when false', function () {86 before(function () {87 this.app = createApp({ extended: false })88 })8990 it('should not parse extended syntax', function (done) {91 request(this.app)92 .post('/')93 .set('Content-Type', 'application/x-www-form-urlencoded')94 .send('user[name][first]=Tobi')95 .expect(200, '{"user[name][first]":"Tobi"}', done)96 })9798 it('should parse multiple key instances', function (done) {99 request(this.app)100 .post('/')101 .set('Content-Type', 'application/x-www-form-urlencoded')102 .send('user=Tobi&user=Loki')103 .expect(200, '{"user":["Tobi","Loki"]}', done)104 })105 })106107 describe('when true', function () {108 before(function () {109 this.app = createApp({ extended: true })110 })111112 it('should parse multiple key instances', function (done) {113 request(this.app)114 .post('/')115 .set('Content-Type', 'application/x-www-form-urlencoded')116 .send('user=Tobi&user=Loki')117 .expect(200, '{"user":["Tobi","Loki"]}', done)118 })119120 it('should parse extended syntax', function (done) {121 request(this.app)122 .post('/')123 .set('Content-Type', 'application/x-www-form-urlencoded')124 .send('user[name][first]=Tobi')125 .expect(200, '{"user":{"name":{"first":"Tobi"}}}', done)126 })127128 it('should parse parameters with dots', function (done) {129 request(this.app)130 .post('/')131 .set('Content-Type', 'application/x-www-form-urlencoded')132 .send('user.name=Tobi')133 .expect(200, '{"user.name":"Tobi"}', done)134 })135136 it('should parse fully-encoded extended syntax', function (done) {137 request(this.app)138 .post('/')139 .set('Content-Type', 'application/x-www-form-urlencoded')140 .send('user%5Bname%5D%5Bfirst%5D=Tobi')141 .expect(200, '{"user":{"name":{"first":"Tobi"}}}', done)142 })143144 it('should parse array index notation', function (done) {145 request(this.app)146 .post('/')147 .set('Content-Type', 'application/x-www-form-urlencoded')148 .send('foo[0]=bar&foo[1]=baz')149 .expect(200, '{"foo":["bar","baz"]}', done)150 })151152 it('should parse array index notation with large array', function (done) {153 var str = 'f[0]=0'154155 for (var i = 1; i < 500; i++) {156 str += '&f[' + i + ']=' + i.toString(16)157 }158159 request(this.app)160 .post('/')161 .set('Content-Type', 'application/x-www-form-urlencoded')162 .send(str)163 .expect(function (res) {164 var obj = JSON.parse(res.text)165 assert.strictEqual(Object.keys(obj).length, 1)166 assert.strictEqual(Array.isArray(obj.f), true)167 assert.strictEqual(obj.f.length, 500)168 })169 .expect(200, done)170 })171172 it('should parse array of objects syntax', function (done) {173 request(this.app)174 .post('/')175 .set('Content-Type', 'application/x-www-form-urlencoded')176 .send('foo[0][bar]=baz&foo[0][fizz]=buzz&foo[]=done!')177 .expect(200, '{"foo":[{"bar":"baz","fizz":"buzz"},"done!"]}', done)178 })179180 it('should parse deep object', function (done) {181 var str = 'foo'182183 for (var i = 0; i < 500; i++) {184 str += '[p]'185 }186187 str += '=bar'188189 request(this.app)190 .post('/')191 .set('Content-Type', 'application/x-www-form-urlencoded')192 .send(str)193 .expect(function (res) {194 var obj = JSON.parse(res.text)195 assert.strictEqual(Object.keys(obj).length, 1)196 assert.strictEqual(typeof obj.foo, 'object')197198 var depth = 0199 var ref = obj.foo200 while ((ref = ref.p)) { depth++ }201 assert.strictEqual(depth, 500)202 })203 .expect(200, done)204 })205 })206 })207208 describe('with inflate option', function () {209 describe('when false', function () {210 before(function () {211 this.app = createApp({ inflate: false })212 })213214 it('should not accept content-encoding', function (done) {215 var test = request(this.app).post('/')216 test.set('Content-Encoding', 'gzip')217 test.set('Content-Type', 'application/x-www-form-urlencoded')218 test.write(Buffer.from('1f8b080000000000000bcb4bcc4db57db16e170099a4bad608000000', 'hex'))219 test.expect(415, 'content encoding unsupported', done)220 })221 })222223 describe('when true', function () {224 before(function () {225 this.app = createApp({ inflate: true })226 })227228 it('should accept content-encoding', function (done) {229 var test = request(this.app).post('/')230 test.set('Content-Encoding', 'gzip')231 test.set('Content-Type', 'application/x-www-form-urlencoded')232 test.write(Buffer.from('1f8b080000000000000bcb4bcc4db57db16e170099a4bad608000000', 'hex'))233 test.expect(200, '{"name":"论"}', done)234 })235 })236 })237238 describe('with limit option', function () {239 it('should 413 when over limit with Content-Length', function (done) {240 var buf = Buffer.alloc(1024, '.')241 request(createApp({ limit: '1kb' }))242 .post('/')243 .set('Content-Type', 'application/x-www-form-urlencoded')244 .set('Content-Length', '1028')245 .send('str=' + buf.toString())246 .expect(413, done)247 })248249 it('should 413 when over limit with chunked encoding', function (done) {250 var buf = Buffer.alloc(1024, '.')251 var app = createApp({ limit: '1kb' })252 var test = request(app).post('/')253 test.set('Content-Type', 'application/x-www-form-urlencoded')254 test.set('Transfer-Encoding', 'chunked')255 test.write('str=')256 test.write(buf.toString())257 test.expect(413, done)258 })259260 it('should accept number of bytes', function (done) {261 var buf = Buffer.alloc(1024, '.')262 request(createApp({ limit: 1024 }))263 .post('/')264 .set('Content-Type', 'application/x-www-form-urlencoded')265 .send('str=' + buf.toString())266 .expect(413, done)267 })268269 it('should not change when options altered', function (done) {270 var buf = Buffer.alloc(1024, '.')271 var options = { limit: '1kb' }272 var app = createApp(options)273274 options.limit = '100kb'275276 request(app)277 .post('/')278 .set('Content-Type', 'application/x-www-form-urlencoded')279 .send('str=' + buf.toString())280 .expect(413, done)281 })282283 it('should not hang response', function (done) {284 var buf = Buffer.alloc(10240, '.')285 var app = createApp({ limit: '8kb' })286 var test = request(app).post('/')287 test.set('Content-Type', 'application/x-www-form-urlencoded')288 test.write(buf)289 test.write(buf)290 test.write(buf)291 test.expect(413, done)292 })293 })294295 describe('with parameterLimit option', function () {296 describe('with extended: false', function () {297 it('should reject 0', function () {298 assert.throws(createApp.bind(null, { extended: false, parameterLimit: 0 }),299 /TypeError: option parameterLimit must be a positive number/)300 })301302 it('should reject string', function () {303 assert.throws(createApp.bind(null, { extended: false, parameterLimit: 'beep' }),304 /TypeError: option parameterLimit must be a positive number/)305 })306307 it('should 413 if over limit', function (done) {308 request(createApp({ extended: false, parameterLimit: 10 }))309 .post('/')310 .set('Content-Type', 'application/x-www-form-urlencoded')311 .send(createManyParams(11))312 .expect(413, /too many parameters/, done)313 })314315 it('should error with type = "parameters.too.many"', function (done) {316 request(createApp({ extended: false, parameterLimit: 10 }))317 .post('/')318 .set('Content-Type', 'application/x-www-form-urlencoded')319 .set('X-Error-Property', 'type')320 .send(createManyParams(11))321 .expect(413, 'parameters.too.many', done)322 })323324 it('should work when at the limit', function (done) {325 request(createApp({ extended: false, parameterLimit: 10 }))326 .post('/')327 .set('Content-Type', 'application/x-www-form-urlencoded')328 .send(createManyParams(10))329 .expect(expectKeyCount(10))330 .expect(200, done)331 })332333 it('should work if number is floating point', function (done) {334 request(createApp({ extended: false, parameterLimit: 10.1 }))335 .post('/')336 .set('Content-Type', 'application/x-www-form-urlencoded')337 .send(createManyParams(11))338 .expect(413, /too many parameters/, done)339 })340341 it('should work with large limit', function (done) {342 request(createApp({ extended: false, parameterLimit: 5000 }))343 .post('/')344 .set('Content-Type', 'application/x-www-form-urlencoded')345 .send(createManyParams(5000))346 .expect(expectKeyCount(5000))347 .expect(200, done)348 })349350 it('should work with Infinity limit', function (done) {351 request(createApp({ extended: false, parameterLimit: Infinity }))352 .post('/')353 .set('Content-Type', 'application/x-www-form-urlencoded')354 .send(createManyParams(10000))355 .expect(expectKeyCount(10000))356 .expect(200, done)357 })358 })359360 describe('with extended: true', function () {361 it('should reject 0', function () {362 assert.throws(createApp.bind(null, { extended: true, parameterLimit: 0 }),363 /TypeError: option parameterLimit must be a positive number/)364 })365366 it('should reject string', function () {367 assert.throws(createApp.bind(null, { extended: true, parameterLimit: 'beep' }),368 /TypeError: option parameterLimit must be a positive number/)369 })370371 it('should 413 if over limit', function (done) {372 request(createApp({ extended: true, parameterLimit: 10 }))373 .post('/')374 .set('Content-Type', 'application/x-www-form-urlencoded')375 .send(createManyParams(11))376 .expect(413, /too many parameters/, done)377 })378379 it('should error with type = "parameters.too.many"', function (done) {380 request(createApp({ extended: true, parameterLimit: 10 }))381 .post('/')382 .set('Content-Type', 'application/x-www-form-urlencoded')383 .set('X-Error-Property', 'type')384 .send(createManyParams(11))385 .expect(413, 'parameters.too.many', done)386 })387388 it('should work when at the limit', function (done) {389 request(createApp({ extended: true, parameterLimit: 10 }))390 .post('/')391 .set('Content-Type', 'application/x-www-form-urlencoded')392 .send(createManyParams(10))393 .expect(expectKeyCount(10))394 .expect(200, done)395 })396397 it('should work if number is floating point', function (done) {398 request(createApp({ extended: true, parameterLimit: 10.1 }))399 .post('/')400 .set('Content-Type', 'application/x-www-form-urlencoded')401 .send(createManyParams(11))402 .expect(413, /too many parameters/, done)403 })404405 it('should work with large limit', function (done) {406 request(createApp({ extended: true, parameterLimit: 5000 }))407 .post('/')408 .set('Content-Type', 'application/x-www-form-urlencoded')409 .send(createManyParams(5000))410 .expect(expectKeyCount(5000))411 .expect(200, done)412 })413414 it('should work with Infinity limit', function (done) {415 request(createApp({ extended: true, parameterLimit: Infinity }))416 .post('/')417 .set('Content-Type', 'application/x-www-form-urlencoded')418 .send(createManyParams(10000))419 .expect(expectKeyCount(10000))420 .expect(200, done)421 })422 })423 })424425 describe('with type option', function () {426 describe('when "application/vnd.x-www-form-urlencoded"', function () {427 before(function () {428 this.app = createApp({ type: 'application/vnd.x-www-form-urlencoded' })429 })430431 it('should parse for custom type', function (done) {432 request(this.app)433 .post('/')434 .set('Content-Type', 'application/vnd.x-www-form-urlencoded')435 .send('user=tobi')436 .expect(200, '{"user":"tobi"}', done)437 })438439 it('should ignore standard type', function (done) {440 request(this.app)441 .post('/')442 .set('Content-Type', 'application/x-www-form-urlencoded')443 .send('user=tobi')444 .expect(200, '{}', done)445 })446 })447448 describe('when ["urlencoded", "application/x-pairs"]', function () {449 before(function () {450 this.app = createApp({451 type: ['urlencoded', 'application/x-pairs']452 })453 })454455 it('should parse "application/x-www-form-urlencoded"', function (done) {456 request(this.app)457 .post('/')458 .set('Content-Type', 'application/x-www-form-urlencoded')459 .send('user=tobi')460 .expect(200, '{"user":"tobi"}', done)461 })462463 it('should parse "application/x-pairs"', function (done) {464 request(this.app)465 .post('/')466 .set('Content-Type', 'application/x-pairs')467 .send('user=tobi')468 .expect(200, '{"user":"tobi"}', done)469 })470471 it('should ignore application/x-foo', function (done) {472 request(this.app)473 .post('/')474 .set('Content-Type', 'application/x-foo')475 .send('user=tobi')476 .expect(200, '{}', done)477 })478 })479480 describe('when a function', function () {481 it('should parse when truthy value returned', function (done) {482 var app = createApp({ type: accept })483484 function accept (req) {485 return req.headers['content-type'] === 'application/vnd.something'486 }487488 request(app)489 .post('/')490 .set('Content-Type', 'application/vnd.something')491 .send('user=tobi')492 .expect(200, '{"user":"tobi"}', done)493 })494495 it('should work without content-type', function (done) {496 var app = createApp({ type: accept })497498 function accept (req) {499 return true500 }501502 var test = request(app).post('/')503 test.write('user=tobi')504 test.expect(200, '{"user":"tobi"}', done)505 })506507 it('should not invoke without a body', function (done) {508 var app = createApp({ type: accept })509510 function accept (req) {511 throw new Error('oops!')512 }513514 request(app)515 .get('/')516 .expect(404, done)517 })518 })519 })520521 describe('with verify option', function () {522 it('should assert value if function', function () {523 assert.throws(createApp.bind(null, { verify: 'lol' }),524 /TypeError: option verify must be function/)525 })526527 it('should error from verify', function (done) {528 var app = createApp({ verify: function (req, res, buf) {529 if (buf[0] === 0x20) throw new Error('no leading space')530 } })531532 request(app)533 .post('/')534 .set('Content-Type', 'application/x-www-form-urlencoded')535 .send(' user=tobi')536 .expect(403, 'no leading space', done)537 })538539 it('should error with type = "entity.verify.failed"', function (done) {540 var app = createApp({ verify: function (req, res, buf) {541 if (buf[0] === 0x20) throw new Error('no leading space')542 } })543544 request(app)545 .post('/')546 .set('Content-Type', 'application/x-www-form-urlencoded')547 .set('X-Error-Property', 'type')548 .send(' user=tobi')549 .expect(403, 'entity.verify.failed', done)550 })551552 it('should allow custom codes', function (done) {553 var app = createApp({ verify: function (req, res, buf) {554 if (buf[0] !== 0x20) return555 var err = new Error('no leading space')556 err.status = 400557 throw err558 } })559560 request(app)561 .post('/')562 .set('Content-Type', 'application/x-www-form-urlencoded')563 .send(' user=tobi')564 .expect(400, 'no leading space', done)565 })566567 it('should allow custom type', function (done) {568 var app = createApp({ verify: function (req, res, buf) {569 if (buf[0] !== 0x20) return570 var err = new Error('no leading space')571 err.type = 'foo.bar'572 throw err573 } })574575 request(app)576 .post('/')577 .set('Content-Type', 'application/x-www-form-urlencoded')578 .set('X-Error-Property', 'type')579 .send(' user=tobi')580 .expect(403, 'foo.bar', done)581 })582583 it('should allow pass-through', function (done) {584 var app = createApp({ verify: function (req, res, buf) {585 if (buf[0] === 0x5b) throw new Error('no arrays')586 } })587588 request(app)589 .post('/')590 .set('Content-Type', 'application/x-www-form-urlencoded')591 .send('user=tobi')592 .expect(200, '{"user":"tobi"}', done)593 })594595 it('should 415 on unknown charset prior to verify', function (done) {596 var app = createApp({ verify: function (req, res, buf) {597 throw new Error('unexpected verify call')598 } })599600 var test = request(app).post('/')601 test.set('Content-Type', 'application/x-www-form-urlencoded; charset=x-bogus')602 test.write(Buffer.from('00000000', 'hex'))603 test.expect(415, 'unsupported charset "X-BOGUS"', done)604 })605 })606607 describe('charset', function () {608 before(function () {609 this.app = createApp()610 })611612 it('should parse utf-8', function (done) {613 var test = request(this.app).post('/')614 test.set('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8')615 test.write(Buffer.from('6e616d653de8aeba', 'hex'))616 test.expect(200, '{"name":"论"}', done)617 })618619 it('should parse when content-length != char length', function (done) {620 var test = request(this.app).post('/')621 test.set('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8')622 test.set('Content-Length', '7')623 test.write(Buffer.from('746573743dc3a5', 'hex'))624 test.expect(200, '{"test":"å"}', done)625 })626627 it('should default to utf-8', function (done) {628 var test = request(this.app).post('/')629 test.set('Content-Type', 'application/x-www-form-urlencoded')630 test.write(Buffer.from('6e616d653de8aeba', 'hex'))631 test.expect(200, '{"name":"论"}', done)632 })633634 it('should fail on unknown charset', function (done) {635 var test = request(this.app).post('/')636 test.set('Content-Type', 'application/x-www-form-urlencoded; charset=koi8-r')637 test.write(Buffer.from('6e616d653dcec5d4', 'hex'))638 test.expect(415, 'unsupported charset "KOI8-R"', done)639 })640 })641642 describe('encoding', function () {643 before(function () {644 this.app = createApp({ limit: '10kb' })645 })646647 it('should parse without encoding', function (done) {648 var test = request(this.app).post('/')649 test.set('Content-Type', 'application/x-www-form-urlencoded')650 test.write(Buffer.from('6e616d653de8aeba', 'hex'))651 test.expect(200, '{"name":"论"}', done)652 })653654 it('should support identity encoding', function (done) {655 var test = request(this.app).post('/')656 test.set('Content-Encoding', 'identity')657 test.set('Content-Type', 'application/x-www-form-urlencoded')658 test.write(Buffer.from('6e616d653de8aeba', 'hex'))659 test.expect(200, '{"name":"论"}', done)660 })661662 it('should support gzip encoding', function (done) {663 var test = request(this.app).post('/')664 test.set('Content-Encoding', 'gzip')665 test.set('Content-Type', 'application/x-www-form-urlencoded')666 test.write(Buffer.from('1f8b080000000000000bcb4bcc4db57db16e170099a4bad608000000', 'hex'))667 test.expect(200, '{"name":"论"}', done)668 })669670 it('should support deflate encoding', function (done) {671 var test = request(this.app).post('/')672 test.set('Content-Encoding', 'deflate')673 test.set('Content-Type', 'application/x-www-form-urlencoded')674 test.write(Buffer.from('789ccb4bcc4db57db16e17001068042f', 'hex'))675 test.expect(200, '{"name":"论"}', done)676 })677678 it('should be case-insensitive', function (done) {679 var test = request(this.app).post('/')680 test.set('Content-Encoding', 'GZIP')681 test.set('Content-Type', 'application/x-www-form-urlencoded')682 test.write(Buffer.from('1f8b080000000000000bcb4bcc4db57db16e170099a4bad608000000', 'hex'))683 test.expect(200, '{"name":"论"}', done)684 })685686 it('should fail on unknown encoding', function (done) {687 var test = request(this.app).post('/')688 test.set('Content-Encoding', 'nulls')689 test.set('Content-Type', 'application/x-www-form-urlencoded')690 test.write(Buffer.from('000000000000', 'hex'))691 test.expect(415, 'unsupported content encoding "nulls"', done)692 })693 })694})695696function createManyParams (count) {697 var str = ''698699 if (count === 0) {700 return str701 }702703 str += '0=0'704705 for (var i = 1; i < count; i++) {706 var n = i.toString(36)707 str += '&' + n + '=' + n708 }709710 return str711}712713function createApp (options) {714 var app = express()715716 app.use(express.urlencoded(options))717718 app.use(function (err, req, res, next) {719 res.status(err.status || 500)720 res.send(String(err[req.headers['x-error-property'] || 'message']))721 })722723 app.post('/', function (req, res) {724 res.json(req.body)725 })726727 return app728}729730function expectKeyCount (count) {731 return function (res) {732 assert.strictEqual(Object.keys(JSON.parse(res.text)).length, count)733 }734}