michael@0: // Copyright Joyent, Inc. and other Node contributors. michael@0: // michael@0: // Permission is hereby granted, free of charge, to any person obtaining a michael@0: // copy of this software and associated documentation files (the michael@0: // "Software"), to deal in the Software without restriction, including michael@0: // without limitation the rights to use, copy, modify, merge, publish, michael@0: // distribute, sublicense, and/or sell copies of the Software, and to permit michael@0: // persons to whom the Software is furnished to do so, subject to the michael@0: // following conditions: michael@0: // michael@0: // The above copyright notice and this permission notice shall be included michael@0: // in all copies or substantial portions of the Software. michael@0: // michael@0: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS michael@0: // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF michael@0: // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN michael@0: // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, michael@0: // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR michael@0: // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE michael@0: // USE OR OTHER DEALINGS IN THE SOFTWARE. michael@0: michael@0: "use strict"; michael@0: michael@0: // test using assert michael@0: var qs = require('sdk/querystring'); michael@0: michael@0: // folding block, commented to pass gjslint michael@0: // {{{ michael@0: // [ wonkyQS, canonicalQS, obj ] michael@0: var qsTestCases = [ michael@0: ['foo=918854443121279438895193', michael@0: 'foo=918854443121279438895193', michael@0: {'foo': '918854443121279438895193'}], michael@0: ['foo=bar', 'foo=bar', {'foo': 'bar'}], michael@0: //['foo=bar&foo=quux', 'foo=bar&foo=quux', {'foo': ['bar', 'quux']}], michael@0: ['foo=1&bar=2', 'foo=1&bar=2', {'foo': '1', 'bar': '2'}], michael@0: // ['my+weird+field=q1%212%22%27w%245%267%2Fz8%29%3F', michael@0: // 'my%20weird%20field=q1!2%22\'w%245%267%2Fz8)%3F', michael@0: // {'my weird field': 'q1!2"\'w$5&7/z8)?' }], michael@0: ['foo%3Dbaz=bar', 'foo%3Dbaz=bar', {'foo=baz': 'bar'}], michael@0: ['foo=baz=bar', 'foo=baz%3Dbar', {'foo': 'baz=bar'}], michael@0: /* michael@0: ['str=foo&arr=1&arr=2&arr=3&somenull=&undef=', michael@0: 'str=foo&arr=1&arr=2&arr=3&somenull=&undef=', michael@0: { 'str': 'foo', michael@0: 'arr': ['1', '2', '3'], michael@0: 'somenull': '', michael@0: 'undef': ''}], michael@0: */ michael@0: //[' foo = bar ', '%20foo%20=%20bar%20', {' foo ': ' bar '}], michael@0: // disable test that fails ['foo=%zx', 'foo=%25zx', {'foo': '%zx'}], michael@0: ['foo=%EF%BF%BD', 'foo=%EF%BF%BD', {'foo': '\ufffd' }] michael@0: ]; michael@0: michael@0: // [ wonkyQS, canonicalQS, obj ] michael@0: var qsColonTestCases = [ michael@0: ['foo:bar', 'foo:bar', {'foo': 'bar'}], michael@0: //['foo:bar;foo:quux', 'foo:bar;foo:quux', {'foo': ['bar', 'quux']}], michael@0: ['foo:1&bar:2;baz:quux', michael@0: 'foo:1%26bar%3A2;baz:quux', michael@0: {'foo': '1&bar:2', 'baz': 'quux'}], michael@0: ['foo%3Abaz:bar', 'foo%3Abaz:bar', {'foo:baz': 'bar'}], michael@0: ['foo:baz:bar', 'foo:baz%3Abar', {'foo': 'baz:bar'}] michael@0: ]; michael@0: michael@0: // [wonkyObj, qs, canonicalObj] michael@0: var extendedFunction = function() {}; michael@0: extendedFunction.prototype = {a: 'b'}; michael@0: var qsWeirdObjects = [ michael@0: //[{regexp: /./g}, 'regexp=', {'regexp': ''}], michael@0: //[{regexp: new RegExp('.', 'g')}, 'regexp=', {'regexp': ''}], michael@0: //[{fn: function() {}}, 'fn=', {'fn': ''}], michael@0: //[{fn: new Function('')}, 'fn=', {'fn': ''}], michael@0: //[{math: Math}, 'math=', {'math': ''}], michael@0: //[{e: extendedFunction}, 'e=', {'e': ''}], michael@0: //[{d: new Date()}, 'd=', {'d': ''}], michael@0: //[{d: Date}, 'd=', {'d': ''}], michael@0: //[{f: new Boolean(false), t: new Boolean(true)}, 'f=&t=', {'f': '', 't': ''}], michael@0: [{f: false, t: true}, 'f=false&t=true', {'f': 'false', 't': 'true'}], michael@0: //[{n: null}, 'n=', {'n': ''}], michael@0: //[{nan: NaN}, 'nan=', {'nan': ''}], michael@0: //[{inf: Infinity}, 'inf=', {'inf': ''}] michael@0: ]; michael@0: // }}} michael@0: michael@0: var qsNoMungeTestCases = [ michael@0: ['', {}], michael@0: //['foo=bar&foo=baz', {'foo': ['bar', 'baz']}], michael@0: ['blah=burp', {'blah': 'burp'}], michael@0: //['gragh=1&gragh=3&goo=2', {'gragh': ['1', '3'], 'goo': '2'}], michael@0: ['frappucino=muffin&goat%5B%5D=scone&pond=moose', michael@0: {'frappucino': 'muffin', 'goat[]': 'scone', 'pond': 'moose'}], michael@0: ['trololol=yes&lololo=no', {'trololol': 'yes', 'lololo': 'no'}] michael@0: ]; michael@0: michael@0: exports['test basic'] = function(assert) { michael@0: assert.strictEqual('918854443121279438895193', michael@0: qs.parse('id=918854443121279438895193').id, michael@0: 'prase id=918854443121279438895193'); michael@0: }; michael@0: michael@0: exports['test that the canonical qs is parsed properly'] = function(assert) { michael@0: qsTestCases.forEach(function(testCase) { michael@0: assert.deepEqual(testCase[2], qs.parse(testCase[0]), michael@0: 'parse ' + testCase[0]); michael@0: }); michael@0: }; michael@0: michael@0: michael@0: exports['test that the colon test cases can do the same'] = function(assert) { michael@0: qsColonTestCases.forEach(function(testCase) { michael@0: assert.deepEqual(testCase[2], qs.parse(testCase[0], ';', ':'), michael@0: 'parse ' + testCase[0] + ' -> ; :'); michael@0: }); michael@0: }; michael@0: michael@0: exports['test the weird objects, that they get parsed properly'] = function(assert) { michael@0: qsWeirdObjects.forEach(function(testCase) { michael@0: assert.deepEqual(testCase[2], qs.parse(testCase[1]), michael@0: 'parse ' + testCase[1]); michael@0: }); michael@0: }; michael@0: michael@0: exports['test non munge test cases'] = function(assert) { michael@0: qsNoMungeTestCases.forEach(function(testCase) { michael@0: assert.deepEqual(testCase[0], qs.stringify(testCase[1], '&', '=', false), michael@0: 'stringify ' + JSON.stringify(testCase[1]) + ' -> & ='); michael@0: }); michael@0: }; michael@0: michael@0: exports['test the nested qs-in-qs case'] = function(assert) { michael@0: var f = qs.parse('a=b&q=x%3Dy%26y%3Dz'); michael@0: f.q = qs.parse(f.q); michael@0: assert.deepEqual(f, { a: 'b', q: { x: 'y', y: 'z' } }, michael@0: 'parse a=b&q=x%3Dy%26y%3Dz'); michael@0: }; michael@0: michael@0: exports['test nested in colon'] = function(assert) { michael@0: var f = qs.parse('a:b;q:x%3Ay%3By%3Az', ';', ':'); michael@0: f.q = qs.parse(f.q, ';', ':'); michael@0: assert.deepEqual(f, { a: 'b', q: { x: 'y', y: 'z' } }, michael@0: 'parse a:b;q:x%3Ay%3By%3Az -> ; :'); michael@0: }; michael@0: michael@0: exports['test stringifying'] = function(assert) { michael@0: qsTestCases.forEach(function(testCase) { michael@0: assert.equal(testCase[1], qs.stringify(testCase[2]), michael@0: 'stringify ' + JSON.stringify(testCase[2])); michael@0: }); michael@0: michael@0: qsColonTestCases.forEach(function(testCase) { michael@0: assert.equal(testCase[1], qs.stringify(testCase[2], ';', ':'), michael@0: 'stringify ' + JSON.stringify(testCase[2]) + ' -> ; :'); michael@0: }); michael@0: michael@0: qsWeirdObjects.forEach(function(testCase) { michael@0: assert.equal(testCase[1], qs.stringify(testCase[0]), michael@0: 'stringify ' + JSON.stringify(testCase[0])); michael@0: }); michael@0: }; michael@0: michael@0: exports['test stringifying nested'] = function(assert) { michael@0: var f = qs.stringify({ michael@0: a: 'b', michael@0: q: qs.stringify({ michael@0: x: 'y', michael@0: y: 'z' michael@0: }) michael@0: }); michael@0: assert.equal(f, 'a=b&q=x%3Dy%26y%3Dz', michael@0: JSON.stringify({ michael@0: a: 'b', michael@0: 'qs.stringify -> q': { michael@0: x: 'y', michael@0: y: 'z' michael@0: } michael@0: })); michael@0: michael@0: var threw = false; michael@0: try { qs.parse(undefined); } catch(error) { threw = true; } michael@0: assert.ok(!threw, "does not throws on undefined"); michael@0: }; michael@0: michael@0: exports['test nested in colon'] = function(assert) { michael@0: var f = qs.stringify({ michael@0: a: 'b', michael@0: q: qs.stringify({ michael@0: x: 'y', michael@0: y: 'z' michael@0: }, ';', ':') michael@0: }, ';', ':'); michael@0: assert.equal(f, 'a:b;q:x%3Ay%3By%3Az', michael@0: 'stringify ' + JSON.stringify({ michael@0: a: 'b', michael@0: 'qs.stringify -> q': { michael@0: x: 'y', michael@0: y: 'z' michael@0: } michael@0: }) + ' -> ; : '); michael@0: michael@0: michael@0: assert.deepEqual({}, qs.parse(), 'parse undefined'); michael@0: }; michael@0: michael@0: require("test").run(exports);