react / wstein / node_modules / browserify / node_modules / url / node_modules / querystring / test / index.js
80540 views// Copyright Joyent, Inc. and other Node contributors.1//2// Permission is hereby granted, free of charge, to any person obtaining a3// copy of this software and associated documentation files (the4// "Software"), to deal in the Software without restriction, including5// without limitation the rights to use, copy, modify, merge, publish,6// distribute, sublicense, and/or sell copies of the Software, and to permit7// persons to whom the Software is furnished to do so, subject to the8// following conditions:9//10// The above copyright notice and this permission notice shall be included11// in all copies or substantial portions of the Software.12//13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS14// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF15// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN16// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,17// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR18// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE19// USE OR OTHER DEALINGS IN THE SOFTWARE.2021"use strict";2223// test using assert24var qs = require('../');2526// folding block, commented to pass gjslint27// {{{28// [ wonkyQS, canonicalQS, obj ]29var qsTestCases = [30['foo=918854443121279438895193',31'foo=918854443121279438895193',32{'foo': '918854443121279438895193'}],33['foo=bar', 'foo=bar', {'foo': 'bar'}],34['foo=bar&foo=quux', 'foo=bar&foo=quux', {'foo': ['bar', 'quux']}],35['foo=1&bar=2', 'foo=1&bar=2', {'foo': '1', 'bar': '2'}],36['my+weird+field=q1%212%22%27w%245%267%2Fz8%29%3F',37'my%20weird%20field=q1!2%22\'w%245%267%2Fz8)%3F',38{'my weird field': 'q1!2"\'w$5&7/z8)?' }],39['foo%3Dbaz=bar', 'foo%3Dbaz=bar', {'foo=baz': 'bar'}],40['foo=baz=bar', 'foo=baz%3Dbar', {'foo': 'baz=bar'}],41['str=foo&arr=1&arr=2&arr=3&somenull=&undef=',42'str=foo&arr=1&arr=2&arr=3&somenull=&undef=',43{ 'str': 'foo',44'arr': ['1', '2', '3'],45'somenull': '',46'undef': ''}],47[' foo = bar ', '%20foo%20=%20bar%20', {' foo ': ' bar '}],48// disable test that fails ['foo=%zx', 'foo=%25zx', {'foo': '%zx'}],49['foo=%EF%BF%BD', 'foo=%EF%BF%BD', {'foo': '\ufffd' }],50// See: https://github.com/joyent/node/issues/170751['hasOwnProperty=x&toString=foo&valueOf=bar&__defineGetter__=baz',52'hasOwnProperty=x&toString=foo&valueOf=bar&__defineGetter__=baz',53{ hasOwnProperty: 'x',54toString: 'foo',55valueOf: 'bar',56__defineGetter__: 'baz' }],57// See: https://github.com/joyent/node/issues/305858['foo&bar=baz', 'foo=&bar=baz', { foo: '', bar: 'baz' }]59];6061// [ wonkyQS, canonicalQS, obj ]62var qsColonTestCases = [63['foo:bar', 'foo:bar', {'foo': 'bar'}],64['foo:bar;foo:quux', 'foo:bar;foo:quux', {'foo': ['bar', 'quux']}],65['foo:1&bar:2;baz:quux',66'foo:1%26bar%3A2;baz:quux',67{'foo': '1&bar:2', 'baz': 'quux'}],68['foo%3Abaz:bar', 'foo%3Abaz:bar', {'foo:baz': 'bar'}],69['foo:baz:bar', 'foo:baz%3Abar', {'foo': 'baz:bar'}]70];7172// [wonkyObj, qs, canonicalObj]73var extendedFunction = function() {};74extendedFunction.prototype = {a: 'b'};75var qsWeirdObjects = [76[{regexp: /./g}, 'regexp=', {'regexp': ''}],77[{regexp: new RegExp('.', 'g')}, 'regexp=', {'regexp': ''}],78[{fn: function() {}}, 'fn=', {'fn': ''}],79[{fn: new Function('')}, 'fn=', {'fn': ''}],80[{math: Math}, 'math=', {'math': ''}],81[{e: extendedFunction}, 'e=', {'e': ''}],82[{d: new Date()}, 'd=', {'d': ''}],83[{d: Date}, 'd=', {'d': ''}],84[{f: new Boolean(false), t: new Boolean(true)}, 'f=&t=', {'f': '', 't': ''}],85[{f: false, t: true}, 'f=false&t=true', {'f': 'false', 't': 'true'}],86[{n: null}, 'n=', {'n': ''}],87[{nan: NaN}, 'nan=', {'nan': ''}],88[{inf: Infinity}, 'inf=', {'inf': ''}]89];90// }}}9192var qsNoMungeTestCases = [93['', {}],94['foo=bar&foo=baz', {'foo': ['bar', 'baz']}],95['blah=burp', {'blah': 'burp'}],96['gragh=1&gragh=3&goo=2', {'gragh': ['1', '3'], 'goo': '2'}],97['frappucino=muffin&goat%5B%5D=scone&pond=moose',98{'frappucino': 'muffin', 'goat[]': 'scone', 'pond': 'moose'}],99['trololol=yes&lololo=no', {'trololol': 'yes', 'lololo': 'no'}]100];101102exports['test basic'] = function(assert) {103assert.strictEqual('918854443121279438895193',104qs.parse('id=918854443121279438895193').id,105'prase id=918854443121279438895193');106};107108exports['test that the canonical qs is parsed properly'] = function(assert) {109qsTestCases.forEach(function(testCase) {110assert.deepEqual(testCase[2], qs.parse(testCase[0]),111'parse ' + testCase[0]);112});113};114115116exports['test that the colon test cases can do the same'] = function(assert) {117qsColonTestCases.forEach(function(testCase) {118assert.deepEqual(testCase[2], qs.parse(testCase[0], ';', ':'),119'parse ' + testCase[0] + ' -> ; :');120});121};122123exports['test the weird objects, that they get parsed properly'] = function(assert) {124qsWeirdObjects.forEach(function(testCase) {125assert.deepEqual(testCase[2], qs.parse(testCase[1]),126'parse ' + testCase[1]);127});128};129130exports['test non munge test cases'] = function(assert) {131qsNoMungeTestCases.forEach(function(testCase) {132assert.deepEqual(testCase[0], qs.stringify(testCase[1], '&', '=', false),133'stringify ' + JSON.stringify(testCase[1]) + ' -> & =');134});135};136137exports['test the nested qs-in-qs case'] = function(assert) {138var f = qs.parse('a=b&q=x%3Dy%26y%3Dz');139f.q = qs.parse(f.q);140assert.deepEqual(f, { a: 'b', q: { x: 'y', y: 'z' } },141'parse a=b&q=x%3Dy%26y%3Dz');142};143144exports['test nested in colon'] = function(assert) {145var f = qs.parse('a:b;q:x%3Ay%3By%3Az', ';', ':');146f.q = qs.parse(f.q, ';', ':');147assert.deepEqual(f, { a: 'b', q: { x: 'y', y: 'z' } },148'parse a:b;q:x%3Ay%3By%3Az -> ; :');149};150151exports['test stringifying'] = function(assert) {152qsTestCases.forEach(function(testCase) {153assert.equal(testCase[1], qs.stringify(testCase[2]),154'stringify ' + JSON.stringify(testCase[2]));155});156157qsColonTestCases.forEach(function(testCase) {158assert.equal(testCase[1], qs.stringify(testCase[2], ';', ':'),159'stringify ' + JSON.stringify(testCase[2]) + ' -> ; :');160});161162qsWeirdObjects.forEach(function(testCase) {163assert.equal(testCase[1], qs.stringify(testCase[0]),164'stringify ' + JSON.stringify(testCase[0]));165});166};167168exports['test stringifying nested'] = function(assert) {169var f = qs.stringify({170a: 'b',171q: qs.stringify({172x: 'y',173y: 'z'174})175});176assert.equal(f, 'a=b&q=x%3Dy%26y%3Dz',177JSON.stringify({178a: 'b',179'qs.stringify -> q': {180x: 'y',181y: 'z'182}183}));184185var threw = false;186try { qs.parse(undefined); } catch(error) { threw = true; }187assert.ok(!threw, "does not throws on undefined");188};189190exports['test nested in colon'] = function(assert) {191var f = qs.stringify({192a: 'b',193q: qs.stringify({194x: 'y',195y: 'z'196}, ';', ':')197}, ';', ':');198assert.equal(f, 'a:b;q:x%3Ay%3By%3Az',199'stringify ' + JSON.stringify({200a: 'b',201'qs.stringify -> q': {202x: 'y',203y: 'z'204}205}) + ' -> ; : ');206207208assert.deepEqual({}, qs.parse(), 'parse undefined');209};210211212