pax_global_header00006660000000000000000000000064124066617700014523gustar00rootroot0000000000000052 comment=9775242fa57cbfa4db62e4b0aa4f82b23e2ce6af qs-2.2.4/000077500000000000000000000000001240666177000121535ustar00rootroot00000000000000qs-2.2.4/.gitignore000066400000000000000000000002771240666177000141510ustar00rootroot00000000000000.idea *.iml npm-debug.log dump.rdb node_modules results.tap results.xml npm-shrinkwrap.json config.json .DS_Store */.DS_Store */*/.DS_Store ._* */._* */*/._* coverage.* lib-cov complexity.md qs-2.2.4/.jshintignore000066400000000000000000000000151240666177000146530ustar00rootroot00000000000000node_modules qs-2.2.4/.jshintrc000066400000000000000000000002031240666177000137730ustar00rootroot00000000000000{ "node": true, "curly": true, "latedef": true, "quotmark": true, "undef": true, "unused": true, "trailing": true } qs-2.2.4/.travis.yml000066400000000000000000000000441240666177000142620ustar00rootroot00000000000000language: node_js node_js: - 0.10qs-2.2.4/CHANGELOG.md000066400000000000000000000057701240666177000137750ustar00rootroot00000000000000 ## [**2.2.3**](https://github.com/hapijs/qs/issues?milestone=12&state=closed) - [**#37**](https://github.com/hapijs/qs/issues/37) parser discards first empty value in array - [**#36**](https://github.com/hapijs/qs/issues/36) Update to lab 4.x ## [**2.2.2**](https://github.com/hapijs/qs/issues?milestone=11&state=closed) - [**#33**](https://github.com/hapijs/qs/issues/33) Error when plain object in a value - [**#34**](https://github.com/hapijs/qs/issues/34) use Object.prototype.hasOwnProperty.call instead of obj.hasOwnProperty - [**#24**](https://github.com/hapijs/qs/issues/24) Changelog? Semver? ## [**2.2.1**](https://github.com/hapijs/qs/issues?milestone=10&state=closed) - [**#32**](https://github.com/hapijs/qs/issues/32) account for circular references properly, closes #31 - [**#31**](https://github.com/hapijs/qs/issues/31) qs.parse stackoverflow on circular objects ## [**2.2.0**](https://github.com/hapijs/qs/issues?milestone=9&state=closed) - [**#26**](https://github.com/hapijs/qs/issues/26) Don't use Buffer global if it's not present - [**#30**](https://github.com/hapijs/qs/issues/30) Bug when merging non-object values into arrays - [**#29**](https://github.com/hapijs/qs/issues/29) Don't call Utils.clone at the top of Utils.merge - [**#23**](https://github.com/hapijs/qs/issues/23) Ability to not limit parameters? ## [**2.1.0**](https://github.com/hapijs/qs/issues?milestone=8&state=closed) - [**#22**](https://github.com/hapijs/qs/issues/22) Enable using a RegExp as delimiter ## [**2.0.0**](https://github.com/hapijs/qs/issues?milestone=7&state=closed) - [**#18**](https://github.com/hapijs/qs/issues/18) Why is there arrayLimit? - [**#20**](https://github.com/hapijs/qs/issues/20) Configurable parametersLimit - [**#21**](https://github.com/hapijs/qs/issues/21) make all limits optional, for #18, for #20 ## [**1.2.2**](https://github.com/hapijs/qs/issues?milestone=6&state=closed) - [**#19**](https://github.com/hapijs/qs/issues/19) Don't overwrite null values ## [**1.2.1**](https://github.com/hapijs/qs/issues?milestone=5&state=closed) - [**#16**](https://github.com/hapijs/qs/issues/16) ignore non-string delimiters - [**#15**](https://github.com/hapijs/qs/issues/15) Close code block ## [**1.2.0**](https://github.com/hapijs/qs/issues?milestone=4&state=closed) - [**#12**](https://github.com/hapijs/qs/issues/12) Add optional delim argument - [**#13**](https://github.com/hapijs/qs/issues/13) fix #11: flattened keys in array are now correctly parsed ## [**1.1.0**](https://github.com/hapijs/qs/issues?milestone=3&state=closed) - [**#7**](https://github.com/hapijs/qs/issues/7) Empty values of a POST array disappear after being submitted - [**#9**](https://github.com/hapijs/qs/issues/9) Should not omit equals signs (=) when value is null - [**#6**](https://github.com/hapijs/qs/issues/6) Minor grammar fix in README ## [**1.0.2**](https://github.com/hapijs/qs/issues?milestone=2&state=closed) - [**#5**](https://github.com/hapijs/qs/issues/5) array holes incorrectly copied into object on large index qs-2.2.4/CONTRIBUTING.md000066400000000000000000000001511240666177000144010ustar00rootroot00000000000000Please view our [hapijs contributing guide](https://github.com/hapijs/hapi/blob/master/CONTRIBUTING.md). qs-2.2.4/LICENSE000077500000000000000000000031661240666177000131710ustar00rootroot00000000000000Copyright (c) 2014 Nathan LaFreniere and other contributors. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * The names of any contributors may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * * The complete list of contributors can be found at: https://github.com/hapijs/qs/graphs/contributors qs-2.2.4/Makefile000066400000000000000000000003031240666177000136070ustar00rootroot00000000000000test: @node node_modules/lab/bin/lab test-cov: @node node_modules/lab/bin/lab -t 100 test-cov-html: @node node_modules/lab/bin/lab -r html -o coverage.html .PHONY: test test-cov test-cov-htmlqs-2.2.4/README.md000077500000000000000000000112171240666177000134370ustar00rootroot00000000000000# qs A querystring parsing and stringifying library with some added security. [![Build Status](https://secure.travis-ci.org/hapijs/qs.svg)](http://travis-ci.org/hapijs/qs) Lead Maintainer: [Nathan LaFreniere](https://github.com/nlf) The **qs** module was originally created and maintained by [TJ Holowaychuk](https://github.com/visionmedia/node-querystring). ## Usage ```javascript var Qs = require('qs'); var obj = Qs.parse('a=c'); // { a: 'c' } var str = Qs.stringify(obj); // 'a=c' ``` ### Parsing Objects ```javascript Qs.parse(string, [options]); ``` **qs** allows you to create nested objects within your query strings, by surrounding the name of sub-keys with square brackets `[]`. For example, the string `'foo[bar]=baz'` converts to: ```javascript { foo: { bar: 'baz' } } ``` URI encoded strings work too: ```javascript Qs.parse('a%5Bb%5D=c'); // { a: { b: 'c' } } ``` You can also nest your objects, like `'foo[bar][baz]=foobarbaz'`: ```javascript { foo: { bar: { baz: 'foobarbaz' } } } ``` By default, when nesting objects **qs** will only parse up to 5 children deep. This means if you attempt to parse a string like `'a[b][c][d][e][f][g][h][i]=j'` your resulting object will be: ```javascript { a: { b: { c: { d: { e: { f: { '[g][h][i]': 'j' } } } } } } } ``` This depth can be overridden by passing a `depth` option to `Qs.parse(string, [options])`: ```javascript Qs.parse('a[b][c][d][e][f][g][h][i]=j', { depth: 1 }); // { a: { b: { '[c][d][e][f][g][h][i]': 'j' } } } ``` The depth limit helps mitigate abuse when **qs** is used to parse user input, and it is recommended to keep it a reasonably small number. For similar reasons, by default **qs** will only parse up to 1000 parameters. This can be overridden by passing a `parameterLimit` option: ```javascript Qs.parse('a=b&c=d', { parameterLimit: 1 }); // { a: 'b' } ``` An optional delimiter can also be passed: ```javascript Qs.parse('a=b;c=d', { delimiter: ';' }); // { a: 'b', c: 'd' } ``` Delimiters can be a regular expression too: ```javascript Qs.parse('a=b;c=d,e=f', { delimiter: /[;,]/ }); // { a: 'b', c: 'd', e: 'f' } ``` ### Parsing Arrays **qs** can also parse arrays using a similar `[]` notation: ```javascript Qs.parse('a[]=b&a[]=c'); // { a: ['b', 'c'] } ``` You may specify an index as well: ```javascript Qs.parse('a[1]=c&a[0]=b'); // { a: ['b', 'c'] } ``` Note that the only difference between an index in an array and a key in an object is that the value between the brackets must be a number to create an array. When creating arrays with specific indices, **qs** will compact a sparse array to only the existing values preserving their order: ```javascript Qs.parse('a[1]=b&a[15]=c'); // { a: ['b', 'c'] } ``` Note that an empty string is also a value, and will be preserved: ```javascript Qs.parse('a[]=&a[]=b'); // { a: ['', 'b'] } Qs.parse('a[0]=b&a[1]=&a[2]=c'); // { a: ['b', '', 'c'] } ``` **qs** will also limit specifying indices in an array to a maximum index of `20`. Any array members with an index of greater than `20` will instead be converted to an object with the index as the key: ```javascript Qs.parse('a[100]=b'); // { a: { '100': 'b' } } ``` This limit can be overridden by passing an `arrayLimit` option: ```javascript Qs.parse('a[1]=b', { arrayLimit: 0 }); // { a: { '1': 'b' } } ``` If you mix notations, **qs** will merge the two items into an object: ```javascript Qs.parse('a[0]=b&a[b]=c'); // { a: { '0': 'b', b: 'c' } } ``` You can also create arrays of objects: ```javascript Qs.parse('a[][b]=c'); // { a: [{ b: 'c' }] } ``` ### Stringifying ```javascript Qs.stringify(object, [options]); ``` When stringifying, **qs** always URI encodes output. Objects are stringified as you would expect: ```javascript Qs.stringify({ a: 'b' }); // 'a=b' Qs.stringify({ a: { b: 'c' } }); // 'a%5Bb%5D=c' ``` Examples beyond this point will be shown as though the output is not URI encoded for clarity. Please note that the return values in these cases *will* be URI encoded during real usage. When arrays are stringified, they are always given explicit indices: ```javascript Qs.stringify({ a: ['b', 'c', 'd'] }); // 'a[0]=b&a[1]=c&a[2]=d' ``` Empty strings and null values will omit the value, but the equals sign (=) remains in place: ```javascript Qs.stringify({ a: '' }); // 'a=' ``` Properties that are set to `undefined` will be omitted entirely: ```javascript Qs.stringify({ a: null, b: undefined }); // 'a=' ``` The delimiter may be overridden with stringify as well: ```javascript Qs.stringify({ a: 'b', c: 'd' }, { delimiter: ';' }); // 'a=b;c=d' ``` qs-2.2.4/index.js000066400000000000000000000000431240666177000136150ustar00rootroot00000000000000module.exports = require('./lib'); qs-2.2.4/lib/000077500000000000000000000000001240666177000127215ustar00rootroot00000000000000qs-2.2.4/lib/index.js000077500000000000000000000003101240666177000143630ustar00rootroot00000000000000// Load modules var Stringify = require('./stringify'); var Parse = require('./parse'); // Declare internals var internals = {}; module.exports = { stringify: Stringify, parse: Parse }; qs-2.2.4/lib/parse.js000077500000000000000000000075211240666177000144010ustar00rootroot00000000000000// Load modules var Utils = require('./utils'); // Declare internals var internals = { delimiter: '&', depth: 5, arrayLimit: 20, parameterLimit: 1000 }; internals.parseValues = function (str, options) { var obj = {}; var parts = str.split(options.delimiter, options.parameterLimit === Infinity ? undefined : options.parameterLimit); for (var i = 0, il = parts.length; i < il; ++i) { var part = parts[i]; var pos = part.indexOf(']=') === -1 ? part.indexOf('=') : part.indexOf(']=') + 1; if (pos === -1) { obj[Utils.decode(part)] = ''; } else { var key = Utils.decode(part.slice(0, pos)); var val = Utils.decode(part.slice(pos + 1)); if (!obj.hasOwnProperty(key)) { obj[key] = val; } else { obj[key] = [].concat(obj[key]).concat(val); } } } return obj; }; internals.parseObject = function (chain, val, options) { if (!chain.length) { return val; } var root = chain.shift(); var obj = {}; if (root === '[]') { obj = []; obj = obj.concat(internals.parseObject(chain, val, options)); } else { var cleanRoot = root[0] === '[' && root[root.length - 1] === ']' ? root.slice(1, root.length - 1) : root; var index = parseInt(cleanRoot, 10); var indexString = '' + index; if (!isNaN(index) && root !== cleanRoot && indexString === cleanRoot && index <= options.arrayLimit) { obj = []; obj[index] = internals.parseObject(chain, val, options); } else { obj[cleanRoot] = internals.parseObject(chain, val, options); } } return obj; }; internals.parseKeys = function (key, val, options) { if (!key) { return; } // The regex chunks var parent = /^([^\[\]]*)/; var child = /(\[[^\[\]]*\])/g; // Get the parent var segment = parent.exec(key); // Don't allow them to overwrite object prototype properties if (Object.prototype.hasOwnProperty(segment[1])) { return; } // Stash the parent if it exists var keys = []; if (segment[1]) { keys.push(segment[1]); } // Loop through children appending to the array until we hit depth var i = 0; while ((segment = child.exec(key)) !== null && i < options.depth) { ++i; if (!Object.prototype.hasOwnProperty(segment[1].replace(/\[|\]/g, ''))) { keys.push(segment[1]); } } // If there's a remainder, just add whatever is left if (segment) { keys.push('[' + key.slice(segment.index) + ']'); } return internals.parseObject(keys, val, options); }; module.exports = function (str, options) { if (str === '' || str === null || typeof str === 'undefined') { return {}; } options = options || {}; options.delimiter = typeof options.delimiter === 'string' || Utils.isRegExp(options.delimiter) ? options.delimiter : internals.delimiter; options.depth = typeof options.depth === 'number' ? options.depth : internals.depth; options.arrayLimit = typeof options.arrayLimit === 'number' ? options.arrayLimit : internals.arrayLimit; options.parameterLimit = typeof options.parameterLimit === 'number' ? options.parameterLimit : internals.parameterLimit; var tempObj = typeof str === 'string' ? internals.parseValues(str, options) : str; var obj = {}; // Iterate over the keys and setup the new object var keys = Object.keys(tempObj); for (var i = 0, il = keys.length; i < il; ++i) { var key = keys[i]; var newObj = internals.parseKeys(key, tempObj[key], options); obj = Utils.merge(obj, newObj); } return Utils.compact(obj); }; qs-2.2.4/lib/stringify.js000077500000000000000000000022051240666177000152770ustar00rootroot00000000000000// Load modules var Utils = require('./utils'); // Declare internals var internals = { delimiter: '&' }; internals.stringify = function (obj, prefix) { if (Utils.isBuffer(obj)) { obj = obj.toString(); } else if (obj instanceof Date) { obj = obj.toISOString(); } else if (obj === null) { obj = ''; } if (typeof obj === 'string' || typeof obj === 'number' || typeof obj === 'boolean') { return [encodeURIComponent(prefix) + '=' + encodeURIComponent(obj)]; } var values = []; for (var key in obj) { if (obj.hasOwnProperty(key)) { values = values.concat(internals.stringify(obj[key], prefix + '[' + key + ']')); } } return values; }; module.exports = function (obj, options) { options = options || {}; var delimiter = typeof options.delimiter === 'undefined' ? internals.delimiter : options.delimiter; var keys = []; for (var key in obj) { if (obj.hasOwnProperty(key)) { keys = keys.concat(internals.stringify(obj[key], key)); } } return keys.join(delimiter); }; qs-2.2.4/lib/utils.js000077500000000000000000000052011240666177000144200ustar00rootroot00000000000000// Load modules // Declare internals var internals = {}; exports.arrayToObject = function (source) { var obj = {}; for (var i = 0, il = source.length; i < il; ++i) { if (typeof source[i] !== 'undefined') { obj[i] = source[i]; } } return obj; }; exports.merge = function (target, source) { if (!source) { return target; } if (Array.isArray(source)) { for (var i = 0, il = source.length; i < il; ++i) { if (typeof source[i] !== 'undefined') { if (typeof target[i] === 'object') { target[i] = exports.merge(target[i], source[i]); } else { target[i] = source[i]; } } } return target; } if (Array.isArray(target)) { if (typeof source !== 'object') { target.push(source); return target; } else { target = exports.arrayToObject(target); } } var keys = Object.keys(source); for (var k = 0, kl = keys.length; k < kl; ++k) { var key = keys[k]; var value = source[key]; if (value && typeof value === 'object') { if (!target[key]) { target[key] = value; } else { target[key] = exports.merge(target[key], value); } } else { target[key] = value; } } return target; }; exports.decode = function (str) { try { return decodeURIComponent(str.replace(/\+/g, ' ')); } catch (e) { return str; } }; exports.compact = function (obj, refs) { if (typeof obj !== 'object' || obj === null) { return obj; } refs = refs || []; var lookup = refs.indexOf(obj); if (lookup !== -1) { return refs[lookup]; } refs.push(obj); if (Array.isArray(obj)) { var compacted = []; for (var i = 0, l = obj.length; i < l; ++i) { if (typeof obj[i] !== 'undefined') { compacted.push(obj[i]); } } return compacted; } var keys = Object.keys(obj); for (var i = 0, il = keys.length; i < il; ++i) { var key = keys[i]; obj[key] = exports.compact(obj[key], refs); } return obj; }; exports.isRegExp = function (obj) { return Object.prototype.toString.call(obj) === '[object RegExp]'; }; exports.isBuffer = function (obj) { if (typeof Buffer !== 'undefined') { return Buffer.isBuffer(obj); } else { return false; } }; qs-2.2.4/package.json000077500000000000000000000011641240666177000144460ustar00rootroot00000000000000{ "name": "qs", "version": "2.2.4", "description": "A querystring parser that supports nesting and arrays, with a depth limit", "homepage": "https://github.com/hapijs/qs", "main": "index.js", "dependencies": {}, "devDependencies": { "lab": "4.x.x" }, "scripts": { "test": "make test-cov" }, "repository": { "type": "git", "url": "https://github.com/hapijs/qs.git" }, "keywords": [ "querystring", "qs" ], "author": "Nathan LaFreniere ", "licenses": [ { "type": "BSD", "url": "http://github.com/hapijs/qs/raw/master/LICENSE" } ] } qs-2.2.4/test/000077500000000000000000000000001240666177000131325ustar00rootroot00000000000000qs-2.2.4/test/parse.js000077500000000000000000000302071240666177000146070ustar00rootroot00000000000000// Load modules var Lab = require('lab'); var Qs = require('../'); // Declare internals var internals = {}; // Test shortcuts var lab = exports.lab = Lab.script(); var expect = Lab.expect; var before = lab.before; var after = lab.after; var describe = lab.experiment; var it = lab.test; describe('#parse', function () { it('parses a simple string', function (done) { expect(Qs.parse('0=foo')).to.deep.equal({ '0': 'foo' }); expect(Qs.parse('foo=c++')).to.deep.equal({ foo: 'c ' }); expect(Qs.parse('a[>=]=23')).to.deep.equal({ a: { '>=': '23' } }); expect(Qs.parse('a[<=>]==23')).to.deep.equal({ a: { '<=>': '=23' } }); expect(Qs.parse('a[==]=23')).to.deep.equal({ a: { '==': '23' } }); expect(Qs.parse('foo')).to.deep.equal({ foo: '' }); expect(Qs.parse('foo=bar')).to.deep.equal({ foo: 'bar' }); expect(Qs.parse(' foo = bar = baz ')).to.deep.equal({ ' foo ': ' bar = baz ' }); expect(Qs.parse('foo=bar=baz')).to.deep.equal({ foo: 'bar=baz' }); expect(Qs.parse('foo=bar&bar=baz')).to.deep.equal({ foo: 'bar', bar: 'baz' }); expect(Qs.parse('foo=bar&baz')).to.deep.equal({ foo: 'bar', baz: '' }); expect(Qs.parse('cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World')).to.deep.equal({ cht: 'p3', chd: 't:60,40', chs: '250x100', chl: 'Hello|World' }); done(); }); it('parses a single nested string', function (done) { expect(Qs.parse('a[b]=c')).to.deep.equal({ a: { b: 'c' } }); done(); }); it('parses a double nested string', function (done) { expect(Qs.parse('a[b][c]=d')).to.deep.equal({ a: { b: { c: 'd' } } }); done(); }); it('defaults to a depth of 5', function (done) { expect(Qs.parse('a[b][c][d][e][f][g][h]=i')).to.deep.equal({ a: { b: { c: { d: { e: { f: { '[g][h]': 'i' } } } } } } }); done(); }); it('only parses one level when depth = 1', function (done) { expect(Qs.parse('a[b][c]=d', { depth: 1 })).to.deep.equal({ a: { b: { '[c]': 'd' } } }); expect(Qs.parse('a[b][c][d]=e', { depth: 1 })).to.deep.equal({ a: { b: { '[c][d]': 'e' } } }); done(); }); it('parses a simple array', function (done) { expect(Qs.parse('a=b&a=c')).to.deep.equal({ a: ['b', 'c'] }); done(); }); it('parses an explicit array', function (done) { expect(Qs.parse('a[]=b')).to.deep.equal({ a: ['b'] }); expect(Qs.parse('a[]=b&a[]=c')).to.deep.equal({ a: ['b', 'c'] }); expect(Qs.parse('a[]=b&a[]=c&a[]=d')).to.deep.equal({ a: ['b', 'c', 'd'] }); done(); }); it('parses a nested array', function (done) { expect(Qs.parse('a[b][]=c&a[b][]=d')).to.deep.equal({ a: { b: ['c', 'd'] } }); expect(Qs.parse('a[>=]=25')).to.deep.equal({ a: { '>=': '25' } }); done(); }); it('allows to specify array indices', function (done) { expect(Qs.parse('a[1]=c&a[0]=b&a[2]=d')).to.deep.equal({ a: ['b', 'c', 'd'] }); expect(Qs.parse('a[1]=c&a[0]=b')).to.deep.equal({ a: ['b', 'c'] }); expect(Qs.parse('a[1]=c')).to.deep.equal({ a: ['c'] }); done(); }); it('limits specific array indices to 20', function (done) { expect(Qs.parse('a[20]=a')).to.deep.equal({ a: ['a'] }); expect(Qs.parse('a[21]=a')).to.deep.equal({ a: { '21': 'a' } }); done(); }); it('supports keys that begin with a number', function (done) { expect(Qs.parse('a[12b]=c')).to.deep.equal({ a: { '12b': 'c' } }); done(); }); it('supports encoded = signs', function (done) { expect(Qs.parse('he%3Dllo=th%3Dere')).to.deep.equal({ 'he=llo': 'th=ere' }); done(); }); it('is ok with url encoded strings', function (done) { expect(Qs.parse('a[b%20c]=d')).to.deep.equal({ a: { 'b c': 'd' } }); expect(Qs.parse('a[b]=c%20d')).to.deep.equal({ a: { b: 'c d' } }); done(); }); it('allows brackets in the value', function (done) { expect(Qs.parse('pets=["tobi"]')).to.deep.equal({ pets: '["tobi"]' }); expect(Qs.parse('operators=[">=", "<="]')).to.deep.equal({ operators: '[">=", "<="]' }); done(); }); it('allows empty values', function (done) { expect(Qs.parse('')).to.deep.equal({}); expect(Qs.parse(null)).to.deep.equal({}); expect(Qs.parse(undefined)).to.deep.equal({}); done(); }); it('transforms arrays to objects', function (done) { expect(Qs.parse('foo[0]=bar&foo[bad]=baz')).to.deep.equal({ foo: { '0': 'bar', bad: 'baz' } }); expect(Qs.parse('foo[bad]=baz&foo[0]=bar')).to.deep.equal({ foo: { bad: 'baz', '0': 'bar' } }); expect(Qs.parse('foo[bad]=baz&foo[]=bar')).to.deep.equal({ foo: { bad: 'baz', '0': 'bar' } }); expect(Qs.parse('foo[]=bar&foo[bad]=baz')).to.deep.equal({ foo: { '0': 'bar', bad: 'baz' } }); expect(Qs.parse('foo[bad]=baz&foo[]=bar&foo[]=foo')).to.deep.equal({ foo: { bad: 'baz', '0': 'bar', '1': 'foo' } }); expect(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'} ]}); done(); }); it('correctly prunes undefined values when converting an array to an object', function (done) { expect(Qs.parse('a[2]=b&a[99999999]=c')).to.deep.equal({ a: { '2': 'b', '99999999': 'c' } }); done(); }); it('supports malformed uri characters', function (done) { expect(Qs.parse('{%:%}')).to.deep.equal({ '{%:%}': '' }); expect(Qs.parse('foo=%:%}')).to.deep.equal({ foo: '%:%}' }); done(); }); it('doesn\'t produce empty keys', function (done) { expect(Qs.parse('_r=1&')).to.deep.equal({ '_r': '1' }); done(); }); it('cannot override prototypes', function (done) { var obj = Qs.parse('toString=bad&bad[toString]=bad&constructor=bad'); expect(typeof obj.toString).to.equal('function'); expect(typeof obj.bad.toString).to.equal('function'); expect(typeof obj.constructor).to.equal('function'); done(); }); it('cannot access Object prototype', function (done) { Qs.parse('constructor[prototype][bad]=bad'); Qs.parse('bad[constructor][prototype][bad]=bad'); expect(typeof Object.prototype.bad).to.equal('undefined'); done(); }); it('parses arrays of objects', function (done) { expect(Qs.parse('a[][b]=c')).to.deep.equal({ a: [{ b: 'c' }] }); expect(Qs.parse('a[0][b]=c')).to.deep.equal({ a: [{ b: 'c' }] }); done(); }); it('allows for empty strings in arrays', function (done) { expect(Qs.parse('a[]=b&a[]=&a[]=c')).to.deep.equal({ a: ['b', '', 'c'] }); expect(Qs.parse('a[0]=b&a[1]=&a[2]=c&a[19]=')).to.deep.equal({ a: ['b', '', 'c', ''] }); expect(Qs.parse('a[]=&a[]=b&a[]=c')).to.deep.equal({ a: ['', 'b', 'c'] }); done(); }); it('compacts sparse arrays', function (done) { expect(Qs.parse('a[10]=1&a[2]=2')).to.deep.equal({ a: ['2', '1'] }); done(); }); it('parses semi-parsed strings', function (done) { expect(Qs.parse({ 'a[b]': 'c' })).to.deep.equal({ a: { b: 'c' } }); expect(Qs.parse({ 'a[b]': 'c', 'a[d]': 'e' })).to.deep.equal({ a: { b: 'c', d: 'e' } }); done(); }); it('parses buffers correctly', function (done) { var b = new Buffer('test'); expect(Qs.parse({ a: b })).to.deep.equal({ a: b }); done(); }); it('continues parsing when no parent is found', function (done) { expect(Qs.parse('[]&a=b')).to.deep.equal({ '0': '', a: 'b' }); expect(Qs.parse('[foo]=bar')).to.deep.equal({ foo: 'bar' }); done(); }); it('does not error when parsing a very long array', function (done) { var str = 'a[]=a'; while (Buffer.byteLength(str) < 128 * 1024) { str += '&' + str; } expect(function () { Qs.parse(str); }).to.not.throw(); done(); }); it('should not throw when a native prototype has an enumerable property', { parallel: false }, function (done) { Object.prototype.crash = ''; Array.prototype.crash = ''; expect(Qs.parse.bind(null, 'a=b')).to.not.throw(); expect(Qs.parse('a=b')).to.deep.equal({ a: 'b' }); expect(Qs.parse.bind(null, 'a[][b]=c')).to.not.throw(); expect(Qs.parse('a[][b]=c')).to.deep.equal({ a: [{ b: 'c' }] }); delete Object.prototype.crash; delete Array.prototype.crash; done(); }); it('parses a string with an alternative string delimiter', function (done) { expect(Qs.parse('a=b;c=d', { delimiter: ';' })).to.deep.equal({ a: 'b', c: 'd' }); done(); }); it('parses a string with an alternative RegExp delimiter', function (done) { expect(Qs.parse('a=b; c=d', { delimiter: /[;,] */ })).to.deep.equal({ a: 'b', c: 'd' }); done(); }); it('does not use non-splittable objects as delimiters', function (done) { expect(Qs.parse('a=b&c=d', { delimiter: true })).to.deep.equal({ a: 'b', c: 'd' }); done(); }); it('allows overriding parameter limit', function (done) { expect(Qs.parse('a=b&c=d', { parameterLimit: 1 })).to.deep.equal({ a: 'b' }); done(); }); it('allows setting the parameter limit to Infinity', function (done) { expect(Qs.parse('a=b&c=d', { parameterLimit: Infinity })).to.deep.equal({ a: 'b', c: 'd' }); done(); }); it('allows overriding array limit', function (done) { expect(Qs.parse('a[0]=b&a[1]=c', { arrayLimit: 0 })).to.deep.equal({ a: { '0': 'b', '1': 'c' } }); done(); }); it('parses an object', function (done) { var input = { "user[name]": {"pop[bob]": 3}, "user[email]": null }; var expected = { "user": { "name": {"pop[bob]": 3}, "email": null } }; var result = Qs.parse(input); expect(result).to.deep.equal(expected); done(); }); it('parses an object and not child values', function (done) { var input = { "user[name]": {"pop[bob]": { "test": 3 }}, "user[email]": null }; var expected = { "user": { "name": {"pop[bob]": { "test": 3 }}, "email": null } }; var result = Qs.parse(input); expect(result).to.deep.equal(expected); done(); }); it('does not blow up when Buffer global is missing', function (done) { var tempBuffer = global.Buffer; delete global.Buffer; expect(Qs.parse('a=b&c=d')).to.deep.equal({ a: 'b', c: 'd' }); global.Buffer = tempBuffer; done(); }); it('does not crash when using invalid dot notation', function (done) { expect(Qs.parse('roomInfoList[0].childrenAges[0]=15&roomInfoList[0].numberOfAdults=2')).to.deep.equal({ roomInfoList: [['15', '2']] }); done(); }); it('does not crash when parsing circular references', function (done) { var a = {}; a.b = a; var parsed; expect(function () { parsed = Qs.parse({ 'foo[bar]': 'baz', 'foo[baz]': a }); }).to.not.throw(Error); expect(parsed).to.have.key('foo'); expect(parsed.foo).to.have.keys('bar', 'baz'); expect(parsed.foo.bar).to.equal('baz'); expect(parsed.foo.baz).to.deep.equal(a); done(); }); it('parses plain objects correctly', function (done) { var a = Object.create(null); a.b = 'c'; expect(Qs.parse(a)).to.deep.equal({ b: 'c' }); expect(Qs.parse({ a: a })).to.deep.equal({ a: { b: 'c' } }); done(); }); it('parses dates correctly', function (done) { var now = new Date(); expect(Qs.parse({ a: now })).to.deep.equal({ a: now }); done(); }); it('parses regular expressions correctly', function (done) { var re = /^test$/; expect(Qs.parse({ a: re })).to.deep.equal({ a: re }); done(); }); }); qs-2.2.4/test/stringify.js000077500000000000000000000102131240666177000155060ustar00rootroot00000000000000// Load modules var Lab = require('lab'); var Qs = require('../'); // Declare internals var internals = {}; // Test shortcuts var lab = exports.lab = Lab.script(); var expect = Lab.expect; var before = lab.before; var after = lab.after; var describe = lab.experiment; var it = lab.test; describe('#stringify', function () { it('stringifies a querystring object', function (done) { expect(Qs.stringify({ a: 'b' })).to.equal('a=b'); expect(Qs.stringify({ a: 1 })).to.equal('a=1'); expect(Qs.stringify({ a: 1, b: 2 })).to.equal('a=1&b=2'); done(); }); it('stringifies a nested object', function (done) { expect(Qs.stringify({ a: { b: 'c' } })).to.equal('a%5Bb%5D=c'); expect(Qs.stringify({ a: { b: { c: { d: 'e' } } } })).to.equal('a%5Bb%5D%5Bc%5D%5Bd%5D=e'); done(); }); it('stringifies an array value', function (done) { expect(Qs.stringify({ a: ['b', 'c', 'd'] })).to.equal('a%5B0%5D=b&a%5B1%5D=c&a%5B2%5D=d'); done(); }); it('stringifies a nested array value', function (done) { expect(Qs.stringify({ a: { b: ['c', 'd'] } })).to.equal('a%5Bb%5D%5B0%5D=c&a%5Bb%5D%5B1%5D=d'); done(); }); it('stringifies an object inside an array', function (done) { expect(Qs.stringify({ a: [{ b: 'c' }] })).to.equal('a%5B0%5D%5Bb%5D=c'); expect(Qs.stringify({ a: [{ b: { c: [1] } }] })).to.equal('a%5B0%5D%5Bb%5D%5Bc%5D%5B0%5D=1'); done(); }); it('stringifies a complicated object', function (done) { expect(Qs.stringify({ a: { b: 'c', d: 'e' } })).to.equal('a%5Bb%5D=c&a%5Bd%5D=e'); done(); }); it('stringifies an empty value', function (done) { expect(Qs.stringify({ a: '' })).to.equal('a='); expect(Qs.stringify({ a: '', b: '' })).to.equal('a=&b='); expect(Qs.stringify({ a: null })).to.equal('a='); expect(Qs.stringify({ a: { b: null } })).to.equal('a%5Bb%5D='); done(); }); it('drops keys with a value of undefined', function (done) { expect(Qs.stringify({ a: undefined })).to.equal(''); expect(Qs.stringify({ a: { b: undefined, c: null } })).to.equal('a%5Bc%5D='); done(); }); it('url encodes values', function (done) { expect(Qs.stringify({ a: 'b c' })).to.equal('a=b%20c'); done(); }); it('stringifies a date', function (done) { var now = new Date(); var str = 'a=' + encodeURIComponent(now.toISOString()); expect(Qs.stringify({ a: now })).to.equal(str); done(); }); it('stringifies the weird object from qs', function (done) { expect(Qs.stringify({ 'my weird field': 'q1!2"\'w$5&7/z8)?' })).to.equal('my%20weird%20field=q1!2%22\'w%245%267%2Fz8)%3F'); done(); }); it('skips properties that are part of the object prototype', function (done) { Object.prototype.crash = 'test'; expect(Qs.stringify({ a: 'b'})).to.equal('a=b'); expect(Qs.stringify({ a: { b: 'c' } })).to.equal('a%5Bb%5D=c'); delete Object.prototype.crash; done(); }); it('stringifies boolean values', function (done) { expect(Qs.stringify({ a: true })).to.equal('a=true'); expect(Qs.stringify({ a: { b: true } })).to.equal('a%5Bb%5D=true'); expect(Qs.stringify({ b: false })).to.equal('b=false'); expect(Qs.stringify({ b: { c: false } })).to.equal('b%5Bc%5D=false'); done(); }); it('stringifies buffer values', function (done) { expect(Qs.stringify({ a: new Buffer('test') })).to.equal('a=test'); expect(Qs.stringify({ a: { b: new Buffer('test') } })).to.equal('a%5Bb%5D=test'); done(); }); it('stringifies an object using an alternative delimiter', function (done) { expect(Qs.stringify({ a: 'b', c: 'd' }, { delimiter: ';' })).to.equal('a=b;c=d'); done(); }); it('doesn\'t blow up when Buffer global is missing', function (done) { var tempBuffer = global.Buffer; delete global.Buffer; expect(Qs.stringify({ a: 'b', c: 'd' })).to.equal('a=b&c=d'); global.Buffer = tempBuffer; done(); }); });