react / wstein / node_modules / jest-cli / node_modules / jsdom / node_modules / request / node_modules / qs / test / parse.js
81146 views/* eslint no-extend-native:0 */1// Load modules23var Code = require('code');4var Lab = require('lab');5var Qs = require('../');678// Declare internals910var internals = {};111213// Test shortcuts1415var lab = exports.lab = Lab.script();16var expect = Code.expect;17var describe = lab.experiment;18var it = lab.test;192021describe('parse()', function () {2223it('parses a simple string', function (done) {2425expect(Qs.parse('0=foo')).to.deep.equal({ '0': 'foo' }, { prototype: false });26expect(Qs.parse('foo=c++')).to.deep.equal({ foo: 'c ' }, { prototype: false });27expect(Qs.parse('a[>=]=23')).to.deep.equal({ a: { '>=': '23' } }, { prototype: false });28expect(Qs.parse('a[<=>]==23')).to.deep.equal({ a: { '<=>': '=23' } }, { prototype: false });29expect(Qs.parse('a[==]=23')).to.deep.equal({ a: { '==': '23' } }, { prototype: false });30expect(Qs.parse('foo', {strictNullHandling: true})).to.deep.equal({ foo: null }, { prototype: false });31expect(Qs.parse('foo' )).to.deep.equal({ foo: '' }, { prototype: false });32expect(Qs.parse('foo=')).to.deep.equal({ foo: '' }, { prototype: false });33expect(Qs.parse('foo=bar')).to.deep.equal({ foo: 'bar' }, { prototype: false });34expect(Qs.parse(' foo = bar = baz ')).to.deep.equal({ ' foo ': ' bar = baz ' }, { prototype: false });35expect(Qs.parse('foo=bar=baz')).to.deep.equal({ foo: 'bar=baz' }, { prototype: false });36expect(Qs.parse('foo=bar&bar=baz')).to.deep.equal({ foo: 'bar', bar: 'baz' }, { prototype: false });37expect(Qs.parse('foo2=bar2&baz2=')).to.deep.equal({ foo2: 'bar2', baz2: '' }, { prototype: false });38expect(Qs.parse('foo=bar&baz', {strictNullHandling: true})).to.deep.equal({ foo: 'bar', baz: null }, { prototype: false });39expect(Qs.parse('foo=bar&baz')).to.deep.equal({ foo: 'bar', baz: '' }, { prototype: false });40expect(Qs.parse('cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World')).to.deep.equal({41cht: 'p3',42chd: 't:60,40',43chs: '250x100',44chl: 'Hello|World'45}, { prototype: false });46done();47});4849it('allows disabling dot notation', function (done) {5051expect(Qs.parse('a.b=c')).to.deep.equal({ a: { b: 'c' } }, { prototype: false });52expect(Qs.parse('a.b=c', { allowDots: false })).to.deep.equal({ 'a.b': 'c' }, { prototype: false });53done();54});5556it('parses a single nested string', function (done) {5758expect(Qs.parse('a[b]=c')).to.deep.equal({ a: { b: 'c' } }, { prototype: false });59done();60});6162it('parses a double nested string', function (done) {6364expect(Qs.parse('a[b][c]=d')).to.deep.equal({ a: { b: { c: 'd' } } }, { prototype: false });65done();66});6768it('defaults to a depth of 5', function (done) {6970expect(Qs.parse('a[b][c][d][e][f][g][h]=i')).to.deep.equal({ a: { b: { c: { d: { e: { f: { '[g][h]': 'i' } } } } } } }, { prototype: false });71done();72});7374it('only parses one level when depth = 1', function (done) {7576expect(Qs.parse('a[b][c]=d', { depth: 1 })).to.deep.equal({ a: { b: { '[c]': 'd' } } }, { prototype: false });77expect(Qs.parse('a[b][c][d]=e', { depth: 1 })).to.deep.equal({ a: { b: { '[c][d]': 'e' } } }, { prototype: false });78done();79});8081it('parses a simple array', function (done) {8283expect(Qs.parse('a=b&a=c')).to.deep.equal({ a: ['b', 'c'] }, { prototype: false });84done();85});8687it('parses an explicit array', function (done) {8889expect(Qs.parse('a[]=b')).to.deep.equal({ a: ['b'] }, { prototype: false });90expect(Qs.parse('a[]=b&a[]=c')).to.deep.equal({ a: ['b', 'c'] }, { prototype: false });91expect(Qs.parse('a[]=b&a[]=c&a[]=d')).to.deep.equal({ a: ['b', 'c', 'd'] }, { prototype: false });92done();93});9495it('parses a mix of simple and explicit arrays', function (done) {9697expect(Qs.parse('a=b&a[]=c')).to.deep.equal({ a: ['b', 'c'] }, { prototype: false });98expect(Qs.parse('a[]=b&a=c')).to.deep.equal({ a: ['b', 'c'] }, { prototype: false });99expect(Qs.parse('a[0]=b&a=c')).to.deep.equal({ a: ['b', 'c'] }, { prototype: false });100expect(Qs.parse('a=b&a[0]=c')).to.deep.equal({ a: ['b', 'c'] }, { prototype: false });101expect(Qs.parse('a[1]=b&a=c')).to.deep.equal({ a: ['b', 'c'] }, { prototype: false });102expect(Qs.parse('a=b&a[1]=c')).to.deep.equal({ a: ['b', 'c'] }, { prototype: false });103done();104});105106it('parses a nested array', function (done) {107108expect(Qs.parse('a[b][]=c&a[b][]=d')).to.deep.equal({ a: { b: ['c', 'd'] } }, { prototype: false });109expect(Qs.parse('a[>=]=25')).to.deep.equal({ a: { '>=': '25' } }, { prototype: false });110done();111});112113it('allows to specify array indices', function (done) {114115expect(Qs.parse('a[1]=c&a[0]=b&a[2]=d')).to.deep.equal({ a: ['b', 'c', 'd'] }, { prototype: false });116expect(Qs.parse('a[1]=c&a[0]=b')).to.deep.equal({ a: ['b', 'c'] }, { prototype: false });117expect(Qs.parse('a[1]=c')).to.deep.equal({ a: ['c'] }, { prototype: false });118done();119});120121it('limits specific array indices to 20', function (done) {122123expect(Qs.parse('a[20]=a')).to.deep.equal({ a: ['a'] }, { prototype: false });124expect(Qs.parse('a[21]=a')).to.deep.equal({ a: { '21': 'a' } }, { prototype: false });125done();126});127128it('supports keys that begin with a number', function (done) {129130expect(Qs.parse('a[12b]=c')).to.deep.equal({ a: { '12b': 'c' } }, { prototype: false });131done();132});133134it('supports encoded = signs', function (done) {135136expect(Qs.parse('he%3Dllo=th%3Dere')).to.deep.equal({ 'he=llo': 'th=ere' }, { prototype: false });137done();138});139140it('is ok with url encoded strings', function (done) {141142expect(Qs.parse('a[b%20c]=d')).to.deep.equal({ a: { 'b c': 'd' } }, { prototype: false });143expect(Qs.parse('a[b]=c%20d')).to.deep.equal({ a: { b: 'c d' } }, { prototype: false });144done();145});146147it('allows brackets in the value', function (done) {148149expect(Qs.parse('pets=["tobi"]')).to.deep.equal({ pets: '["tobi"]' }, { prototype: false });150expect(Qs.parse('operators=[">=", "<="]')).to.deep.equal({ operators: '[">=", "<="]' }, { prototype: false });151done();152});153154it('allows empty values', function (done) {155156expect(Qs.parse('')).to.deep.equal({}, { prototype: false });157expect(Qs.parse(null)).to.deep.equal({}, { prototype: false });158expect(Qs.parse(undefined)).to.deep.equal({}, { prototype: false });159done();160});161162it('transforms arrays to objects', function (done) {163164expect(Qs.parse('foo[0]=bar&foo[bad]=baz')).to.deep.equal({ foo: { '0': 'bar', bad: 'baz' } }, { prototype: false });165expect(Qs.parse('foo[bad]=baz&foo[0]=bar')).to.deep.equal({ foo: { bad: 'baz', '0': 'bar' } }, { prototype: false });166expect(Qs.parse('foo[bad]=baz&foo[]=bar')).to.deep.equal({ foo: { bad: 'baz', '0': 'bar' } }, { prototype: false });167expect(Qs.parse('foo[]=bar&foo[bad]=baz')).to.deep.equal({ foo: { '0': 'bar', bad: 'baz' } }, { prototype: false });168expect(Qs.parse('foo[bad]=baz&foo[]=bar&foo[]=foo')).to.deep.equal({ foo: { bad: 'baz', '0': 'bar', '1': 'foo' } }, { prototype: false });169expect(Qs.parse('foo[0][a]=a&foo[0][b]=b&foo[1][a]=aa&foo[1][b]=bb')).to.deep.equal({foo: [ {a: 'a', b: 'b'}, {a: 'aa', b: 'bb'} ]}, { prototype: false });170expect(Qs.parse('a[]=b&a[t]=u&a[hasOwnProperty]=c')).to.deep.equal({ a: { '0': 'b', t: 'u', hasOwnProperty: 'c' } }, { prototype: false });171expect(Qs.parse('a[]=b&a[hasOwnProperty]=c&a[x]=y')).to.deep.equal({ a: { '0': 'b', hasOwnProperty: 'c', x: 'y' } }, { prototype: false });172done();173});174175it('transforms arrays to objects (dot notation)', function (done) {176177expect(Qs.parse('foo[0].baz=bar&fool.bad=baz')).to.deep.equal({ foo: [ { baz: 'bar'} ], fool: { bad: 'baz' } }, { prototype: false });178expect(Qs.parse('foo[0].baz=bar&fool.bad.boo=baz')).to.deep.equal({ foo: [ { baz: 'bar'} ], fool: { bad: { boo: 'baz' } } }, { prototype: false });179expect(Qs.parse('foo[0][0].baz=bar&fool.bad=baz')).to.deep.equal({ foo: [[ { baz: 'bar'} ]], fool: { bad: 'baz' } }, { prototype: false });180expect(Qs.parse('foo[0].baz[0]=15&foo[0].bar=2')).to.deep.equal({ foo: [{ baz: ['15'], bar: '2' }] }, { prototype: false });181expect(Qs.parse('foo[0].baz[0]=15&foo[0].baz[1]=16&foo[0].bar=2')).to.deep.equal({ foo: [{ baz: ['15', '16'], bar: '2' }] }, { prototype: false });182expect(Qs.parse('foo.bad=baz&foo[0]=bar')).to.deep.equal({ foo: { bad: 'baz', '0': 'bar' } }, { prototype: false });183expect(Qs.parse('foo.bad=baz&foo[]=bar')).to.deep.equal({ foo: { bad: 'baz', '0': 'bar' } }, { prototype: false });184expect(Qs.parse('foo[]=bar&foo.bad=baz')).to.deep.equal({ foo: { '0': 'bar', bad: 'baz' } }, { prototype: false });185expect(Qs.parse('foo.bad=baz&foo[]=bar&foo[]=foo')).to.deep.equal({ foo: { bad: 'baz', '0': 'bar', '1': 'foo' } }, { prototype: false });186expect(Qs.parse('foo[0].a=a&foo[0].b=b&foo[1].a=aa&foo[1].b=bb')).to.deep.equal({foo: [ {a: 'a', b: 'b'}, {a: 'aa', b: 'bb'} ]}, { prototype: false });187done();188});189190it('can add keys to objects', function (done) {191192expect(Qs.parse('a[b]=c&a=d')).to.deep.equal({ a: { b: 'c', d: true } }, { prototype: false });193done();194});195196it('correctly prunes undefined values when converting an array to an object', function (done) {197198expect(Qs.parse('a[2]=b&a[99999999]=c')).to.deep.equal({ a: { '2': 'b', '99999999': 'c' } }, { prototype: false });199done();200});201202it('supports malformed uri characters', function (done) {203204expect(Qs.parse('{%:%}', {strictNullHandling: true})).to.deep.equal({ '{%:%}': null }, { prototype: false });205expect(Qs.parse('{%:%}=')).to.deep.equal({ '{%:%}': '' }, { prototype: false });206expect(Qs.parse('foo=%:%}')).to.deep.equal({ foo: '%:%}' }, { prototype: false });207done();208});209210it('doesn\'t produce empty keys', function (done) {211212expect(Qs.parse('_r=1&')).to.deep.equal({ '_r': '1' }, { prototype: false });213done();214});215216it('cannot access Object prototype', function (done) {217218Qs.parse('constructor[prototype][bad]=bad');219Qs.parse('bad[constructor][prototype][bad]=bad');220expect(typeof Object.prototype.bad).to.equal('undefined');221done();222});223224it('parses arrays of objects', function (done) {225226expect(Qs.parse('a[][b]=c')).to.deep.equal({ a: [{ b: 'c' }] }, { prototype: false });227expect(Qs.parse('a[0][b]=c')).to.deep.equal({ a: [{ b: 'c' }] }, { prototype: false });228done();229});230231it('allows for empty strings in arrays', function (done) {232233expect(Qs.parse('a[]=b&a[]=&a[]=c')).to.deep.equal({ a: ['b', '', 'c'] }, { prototype: false });234expect(Qs.parse('a[0]=b&a[1]&a[2]=c&a[19]=', {strictNullHandling: true})).to.deep.equal({ a: ['b', null, 'c', ''] }, { prototype: false });235expect(Qs.parse('a[0]=b&a[1]=&a[2]=c&a[19]', {strictNullHandling: true})).to.deep.equal({ a: ['b', '', 'c', null] }, { prototype: false });236expect(Qs.parse('a[]=&a[]=b&a[]=c')).to.deep.equal({ a: ['', 'b', 'c'] }, { prototype: false });237done();238});239240it('compacts sparse arrays', function (done) {241242expect(Qs.parse('a[10]=1&a[2]=2')).to.deep.equal({ a: ['2', '1'] }, { prototype: false });243done();244});245246it('parses semi-parsed strings', function (done) {247248expect(Qs.parse({ 'a[b]': 'c' })).to.deep.equal({ a: { b: 'c' } }, { prototype: false });249expect(Qs.parse({ 'a[b]': 'c', 'a[d]': 'e' })).to.deep.equal({ a: { b: 'c', d: 'e' } }, { prototype: false });250done();251});252253it('parses buffers correctly', function (done) {254255var b = new Buffer('test');256expect(Qs.parse({ a: b })).to.deep.equal({ a: b }, { prototype: false });257done();258});259260it('continues parsing when no parent is found', function (done) {261262expect(Qs.parse('[]=&a=b')).to.deep.equal({ '0': '', a: 'b' }, { prototype: false });263expect(Qs.parse('[]&a=b', {strictNullHandling: true})).to.deep.equal({ '0': null, a: 'b' }, { prototype: false });264expect(Qs.parse('[foo]=bar')).to.deep.equal({ foo: 'bar' }, { prototype: false });265done();266});267268it('does not error when parsing a very long array', function (done) {269270var str = 'a[]=a';271while (Buffer.byteLength(str) < 128 * 1024) {272str += '&' + str;273}274275expect(function () {276277Qs.parse(str);278}).to.not.throw();279280done();281});282283it('should not throw when a native prototype has an enumerable property', { parallel: false }, function (done) {284285Object.prototype.crash = '';286Array.prototype.crash = '';287expect(Qs.parse.bind(null, 'a=b')).to.not.throw();288expect(Qs.parse('a=b')).to.deep.equal({ a: 'b' }, { prototype: false });289expect(Qs.parse.bind(null, 'a[][b]=c')).to.not.throw();290expect(Qs.parse('a[][b]=c')).to.deep.equal({ a: [{ b: 'c' }] }, { prototype: false });291delete Object.prototype.crash;292delete Array.prototype.crash;293done();294});295296it('parses a string with an alternative string delimiter', function (done) {297298expect(Qs.parse('a=b;c=d', { delimiter: ';' })).to.deep.equal({ a: 'b', c: 'd' }, { prototype: false });299done();300});301302it('parses a string with an alternative RegExp delimiter', function (done) {303304expect(Qs.parse('a=b; c=d', { delimiter: /[;,] */ })).to.deep.equal({ a: 'b', c: 'd' }, { prototype: false });305done();306});307308it('does not use non-splittable objects as delimiters', function (done) {309310expect(Qs.parse('a=b&c=d', { delimiter: true })).to.deep.equal({ a: 'b', c: 'd' }, { prototype: false });311done();312});313314it('allows overriding parameter limit', function (done) {315316expect(Qs.parse('a=b&c=d', { parameterLimit: 1 })).to.deep.equal({ a: 'b' }, { prototype: false });317done();318});319320it('allows setting the parameter limit to Infinity', function (done) {321322expect(Qs.parse('a=b&c=d', { parameterLimit: Infinity })).to.deep.equal({ a: 'b', c: 'd' }, { prototype: false });323done();324});325326it('allows overriding array limit', function (done) {327328expect(Qs.parse('a[0]=b', { arrayLimit: -1 })).to.deep.equal({ a: { '0': 'b' } }, { prototype: false });329expect(Qs.parse('a[-1]=b', { arrayLimit: -1 })).to.deep.equal({ a: { '-1': 'b' } }, { prototype: false });330expect(Qs.parse('a[0]=b&a[1]=c', { arrayLimit: 0 })).to.deep.equal({ a: { '0': 'b', '1': 'c' } }, { prototype: false });331done();332});333334it('allows disabling array parsing', function (done) {335336expect(Qs.parse('a[0]=b&a[1]=c', { parseArrays: false })).to.deep.equal({ a: { '0': 'b', '1': 'c' } }, { prototype: false });337done();338});339340it('parses an object', function (done) {341342var input = {343'user[name]': {'pop[bob]': 3},344'user[email]': null345};346347var expected = {348'user': {349'name': {'pop[bob]': 3},350'email': null351}352};353354var result = Qs.parse(input);355356expect(result).to.deep.equal(expected, { prototype: false });357done();358});359360it('parses an object in dot notation', function (done) {361362var input = {363'user.name': {'pop[bob]': 3},364'user.email.': null365};366367var expected = {368'user': {369'name': {'pop[bob]': 3},370'email': null371}372};373374var result = Qs.parse(input);375376expect(result).to.deep.equal(expected, { prototype: false });377done();378});379380it('parses an object and not child values', function (done) {381382var input = {383'user[name]': {'pop[bob]': { 'test': 3 }},384'user[email]': null385};386387var expected = {388'user': {389'name': {'pop[bob]': { 'test': 3 }},390'email': null391}392};393394var result = Qs.parse(input);395396expect(result).to.deep.equal(expected, { prototype: false });397done();398});399400it('does not blow up when Buffer global is missing', function (done) {401402var tempBuffer = global.Buffer;403delete global.Buffer;404var result = Qs.parse('a=b&c=d');405global.Buffer = tempBuffer;406expect(result).to.deep.equal({ a: 'b', c: 'd' }, { prototype: false });407done();408});409410it('does not crash when parsing circular references', function (done) {411412var a = {};413a.b = a;414415var parsed;416417expect(function () {418419parsed = Qs.parse({ 'foo[bar]': 'baz', 'foo[baz]': a });420}).to.not.throw();421422expect(parsed).to.contain('foo');423expect(parsed.foo).to.contain('bar', 'baz');424expect(parsed.foo.bar).to.equal('baz');425expect(parsed.foo.baz).to.deep.equal(a, { prototype: false });426done();427});428429it('parses plain objects correctly', function (done) {430431var a = Object.create(null);432a.b = 'c';433434expect(Qs.parse(a)).to.deep.equal({ b: 'c' }, { prototype: false });435var result = Qs.parse({ a: a });436expect(result).to.contain('a');437expect(result.a).to.deep.equal(a, { prototype: false });438done();439});440441it('parses dates correctly', function (done) {442443var now = new Date();444expect(Qs.parse({ a: now })).to.deep.equal({ a: now }, { prototype: false });445done();446});447448it('parses regular expressions correctly', function (done) {449450var re = /^test$/;451expect(Qs.parse({ a: re })).to.deep.equal({ a: re }, { prototype: false });452done();453});454});455456457