1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/addon-sdk/source/test/test-querystring.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,205 @@ 1.4 +// Copyright Joyent, Inc. and other Node contributors. 1.5 +// 1.6 +// Permission is hereby granted, free of charge, to any person obtaining a 1.7 +// copy of this software and associated documentation files (the 1.8 +// "Software"), to deal in the Software without restriction, including 1.9 +// without limitation the rights to use, copy, modify, merge, publish, 1.10 +// distribute, sublicense, and/or sell copies of the Software, and to permit 1.11 +// persons to whom the Software is furnished to do so, subject to the 1.12 +// following conditions: 1.13 +// 1.14 +// The above copyright notice and this permission notice shall be included 1.15 +// in all copies or substantial portions of the Software. 1.16 +// 1.17 +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 1.18 +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 1.19 +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 1.20 +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 1.21 +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 1.22 +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 1.23 +// USE OR OTHER DEALINGS IN THE SOFTWARE. 1.24 + 1.25 +"use strict"; 1.26 + 1.27 +// test using assert 1.28 +var qs = require('sdk/querystring'); 1.29 + 1.30 +// folding block, commented to pass gjslint 1.31 +// {{{ 1.32 +// [ wonkyQS, canonicalQS, obj ] 1.33 +var qsTestCases = [ 1.34 + ['foo=918854443121279438895193', 1.35 + 'foo=918854443121279438895193', 1.36 + {'foo': '918854443121279438895193'}], 1.37 + ['foo=bar', 'foo=bar', {'foo': 'bar'}], 1.38 + //['foo=bar&foo=quux', 'foo=bar&foo=quux', {'foo': ['bar', 'quux']}], 1.39 + ['foo=1&bar=2', 'foo=1&bar=2', {'foo': '1', 'bar': '2'}], 1.40 + // ['my+weird+field=q1%212%22%27w%245%267%2Fz8%29%3F', 1.41 + // 'my%20weird%20field=q1!2%22\'w%245%267%2Fz8)%3F', 1.42 + // {'my weird field': 'q1!2"\'w$5&7/z8)?' }], 1.43 + ['foo%3Dbaz=bar', 'foo%3Dbaz=bar', {'foo=baz': 'bar'}], 1.44 + ['foo=baz=bar', 'foo=baz%3Dbar', {'foo': 'baz=bar'}], 1.45 + /* 1.46 + ['str=foo&arr=1&arr=2&arr=3&somenull=&undef=', 1.47 + 'str=foo&arr=1&arr=2&arr=3&somenull=&undef=', 1.48 + { 'str': 'foo', 1.49 + 'arr': ['1', '2', '3'], 1.50 + 'somenull': '', 1.51 + 'undef': ''}], 1.52 + */ 1.53 + //[' foo = bar ', '%20foo%20=%20bar%20', {' foo ': ' bar '}], 1.54 + // disable test that fails ['foo=%zx', 'foo=%25zx', {'foo': '%zx'}], 1.55 + ['foo=%EF%BF%BD', 'foo=%EF%BF%BD', {'foo': '\ufffd' }] 1.56 +]; 1.57 + 1.58 +// [ wonkyQS, canonicalQS, obj ] 1.59 +var qsColonTestCases = [ 1.60 + ['foo:bar', 'foo:bar', {'foo': 'bar'}], 1.61 + //['foo:bar;foo:quux', 'foo:bar;foo:quux', {'foo': ['bar', 'quux']}], 1.62 + ['foo:1&bar:2;baz:quux', 1.63 + 'foo:1%26bar%3A2;baz:quux', 1.64 + {'foo': '1&bar:2', 'baz': 'quux'}], 1.65 + ['foo%3Abaz:bar', 'foo%3Abaz:bar', {'foo:baz': 'bar'}], 1.66 + ['foo:baz:bar', 'foo:baz%3Abar', {'foo': 'baz:bar'}] 1.67 +]; 1.68 + 1.69 +// [wonkyObj, qs, canonicalObj] 1.70 +var extendedFunction = function() {}; 1.71 +extendedFunction.prototype = {a: 'b'}; 1.72 +var qsWeirdObjects = [ 1.73 + //[{regexp: /./g}, 'regexp=', {'regexp': ''}], 1.74 + //[{regexp: new RegExp('.', 'g')}, 'regexp=', {'regexp': ''}], 1.75 + //[{fn: function() {}}, 'fn=', {'fn': ''}], 1.76 + //[{fn: new Function('')}, 'fn=', {'fn': ''}], 1.77 + //[{math: Math}, 'math=', {'math': ''}], 1.78 + //[{e: extendedFunction}, 'e=', {'e': ''}], 1.79 + //[{d: new Date()}, 'd=', {'d': ''}], 1.80 + //[{d: Date}, 'd=', {'d': ''}], 1.81 + //[{f: new Boolean(false), t: new Boolean(true)}, 'f=&t=', {'f': '', 't': ''}], 1.82 + [{f: false, t: true}, 'f=false&t=true', {'f': 'false', 't': 'true'}], 1.83 + //[{n: null}, 'n=', {'n': ''}], 1.84 + //[{nan: NaN}, 'nan=', {'nan': ''}], 1.85 + //[{inf: Infinity}, 'inf=', {'inf': ''}] 1.86 +]; 1.87 +// }}} 1.88 + 1.89 +var qsNoMungeTestCases = [ 1.90 + ['', {}], 1.91 + //['foo=bar&foo=baz', {'foo': ['bar', 'baz']}], 1.92 + ['blah=burp', {'blah': 'burp'}], 1.93 + //['gragh=1&gragh=3&goo=2', {'gragh': ['1', '3'], 'goo': '2'}], 1.94 + ['frappucino=muffin&goat%5B%5D=scone&pond=moose', 1.95 + {'frappucino': 'muffin', 'goat[]': 'scone', 'pond': 'moose'}], 1.96 + ['trololol=yes&lololo=no', {'trololol': 'yes', 'lololo': 'no'}] 1.97 +]; 1.98 + 1.99 +exports['test basic'] = function(assert) { 1.100 + assert.strictEqual('918854443121279438895193', 1.101 + qs.parse('id=918854443121279438895193').id, 1.102 + 'prase id=918854443121279438895193'); 1.103 +}; 1.104 + 1.105 +exports['test that the canonical qs is parsed properly'] = function(assert) { 1.106 + qsTestCases.forEach(function(testCase) { 1.107 + assert.deepEqual(testCase[2], qs.parse(testCase[0]), 1.108 + 'parse ' + testCase[0]); 1.109 + }); 1.110 +}; 1.111 + 1.112 + 1.113 +exports['test that the colon test cases can do the same'] = function(assert) { 1.114 + qsColonTestCases.forEach(function(testCase) { 1.115 + assert.deepEqual(testCase[2], qs.parse(testCase[0], ';', ':'), 1.116 + 'parse ' + testCase[0] + ' -> ; :'); 1.117 + }); 1.118 +}; 1.119 + 1.120 +exports['test the weird objects, that they get parsed properly'] = function(assert) { 1.121 + qsWeirdObjects.forEach(function(testCase) { 1.122 + assert.deepEqual(testCase[2], qs.parse(testCase[1]), 1.123 + 'parse ' + testCase[1]); 1.124 + }); 1.125 +}; 1.126 + 1.127 +exports['test non munge test cases'] = function(assert) { 1.128 + qsNoMungeTestCases.forEach(function(testCase) { 1.129 + assert.deepEqual(testCase[0], qs.stringify(testCase[1], '&', '=', false), 1.130 + 'stringify ' + JSON.stringify(testCase[1]) + ' -> & ='); 1.131 + }); 1.132 +}; 1.133 + 1.134 +exports['test the nested qs-in-qs case'] = function(assert) { 1.135 + var f = qs.parse('a=b&q=x%3Dy%26y%3Dz'); 1.136 + f.q = qs.parse(f.q); 1.137 + assert.deepEqual(f, { a: 'b', q: { x: 'y', y: 'z' } }, 1.138 + 'parse a=b&q=x%3Dy%26y%3Dz'); 1.139 +}; 1.140 + 1.141 +exports['test nested in colon'] = function(assert) { 1.142 + var f = qs.parse('a:b;q:x%3Ay%3By%3Az', ';', ':'); 1.143 + f.q = qs.parse(f.q, ';', ':'); 1.144 + assert.deepEqual(f, { a: 'b', q: { x: 'y', y: 'z' } }, 1.145 + 'parse a:b;q:x%3Ay%3By%3Az -> ; :'); 1.146 +}; 1.147 + 1.148 +exports['test stringifying'] = function(assert) { 1.149 + qsTestCases.forEach(function(testCase) { 1.150 + assert.equal(testCase[1], qs.stringify(testCase[2]), 1.151 + 'stringify ' + JSON.stringify(testCase[2])); 1.152 + }); 1.153 + 1.154 + qsColonTestCases.forEach(function(testCase) { 1.155 + assert.equal(testCase[1], qs.stringify(testCase[2], ';', ':'), 1.156 + 'stringify ' + JSON.stringify(testCase[2]) + ' -> ; :'); 1.157 + }); 1.158 + 1.159 + qsWeirdObjects.forEach(function(testCase) { 1.160 + assert.equal(testCase[1], qs.stringify(testCase[0]), 1.161 + 'stringify ' + JSON.stringify(testCase[0])); 1.162 + }); 1.163 +}; 1.164 + 1.165 +exports['test stringifying nested'] = function(assert) { 1.166 + var f = qs.stringify({ 1.167 + a: 'b', 1.168 + q: qs.stringify({ 1.169 + x: 'y', 1.170 + y: 'z' 1.171 + }) 1.172 + }); 1.173 + assert.equal(f, 'a=b&q=x%3Dy%26y%3Dz', 1.174 + JSON.stringify({ 1.175 + a: 'b', 1.176 + 'qs.stringify -> q': { 1.177 + x: 'y', 1.178 + y: 'z' 1.179 + } 1.180 + })); 1.181 + 1.182 + var threw = false; 1.183 + try { qs.parse(undefined); } catch(error) { threw = true; } 1.184 + assert.ok(!threw, "does not throws on undefined"); 1.185 +}; 1.186 + 1.187 +exports['test nested in colon'] = function(assert) { 1.188 + var f = qs.stringify({ 1.189 + a: 'b', 1.190 + q: qs.stringify({ 1.191 + x: 'y', 1.192 + y: 'z' 1.193 + }, ';', ':') 1.194 + }, ';', ':'); 1.195 + assert.equal(f, 'a:b;q:x%3Ay%3By%3Az', 1.196 + 'stringify ' + JSON.stringify({ 1.197 + a: 'b', 1.198 + 'qs.stringify -> q': { 1.199 + x: 'y', 1.200 + y: 'z' 1.201 + } 1.202 + }) + ' -> ; : '); 1.203 + 1.204 + 1.205 + assert.deepEqual({}, qs.parse(), 'parse undefined'); 1.206 +}; 1.207 + 1.208 +require("test").run(exports);