addon-sdk/source/test/test-querystring.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // Copyright Joyent, Inc. and other Node contributors.
michael@0 2 //
michael@0 3 // Permission is hereby granted, free of charge, to any person obtaining a
michael@0 4 // copy of this software and associated documentation files (the
michael@0 5 // "Software"), to deal in the Software without restriction, including
michael@0 6 // without limitation the rights to use, copy, modify, merge, publish,
michael@0 7 // distribute, sublicense, and/or sell copies of the Software, and to permit
michael@0 8 // persons to whom the Software is furnished to do so, subject to the
michael@0 9 // following conditions:
michael@0 10 //
michael@0 11 // The above copyright notice and this permission notice shall be included
michael@0 12 // in all copies or substantial portions of the Software.
michael@0 13 //
michael@0 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
michael@0 15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
michael@0 16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
michael@0 17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
michael@0 18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
michael@0 19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
michael@0 20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
michael@0 21
michael@0 22 "use strict";
michael@0 23
michael@0 24 // test using assert
michael@0 25 var qs = require('sdk/querystring');
michael@0 26
michael@0 27 // folding block, commented to pass gjslint
michael@0 28 // {{{
michael@0 29 // [ wonkyQS, canonicalQS, obj ]
michael@0 30 var qsTestCases = [
michael@0 31 ['foo=918854443121279438895193',
michael@0 32 'foo=918854443121279438895193',
michael@0 33 {'foo': '918854443121279438895193'}],
michael@0 34 ['foo=bar', 'foo=bar', {'foo': 'bar'}],
michael@0 35 //['foo=bar&foo=quux', 'foo=bar&foo=quux', {'foo': ['bar', 'quux']}],
michael@0 36 ['foo=1&bar=2', 'foo=1&bar=2', {'foo': '1', 'bar': '2'}],
michael@0 37 // ['my+weird+field=q1%212%22%27w%245%267%2Fz8%29%3F',
michael@0 38 // 'my%20weird%20field=q1!2%22\'w%245%267%2Fz8)%3F',
michael@0 39 // {'my weird field': 'q1!2"\'w$5&7/z8)?' }],
michael@0 40 ['foo%3Dbaz=bar', 'foo%3Dbaz=bar', {'foo=baz': 'bar'}],
michael@0 41 ['foo=baz=bar', 'foo=baz%3Dbar', {'foo': 'baz=bar'}],
michael@0 42 /*
michael@0 43 ['str=foo&arr=1&arr=2&arr=3&somenull=&undef=',
michael@0 44 'str=foo&arr=1&arr=2&arr=3&somenull=&undef=',
michael@0 45 { 'str': 'foo',
michael@0 46 'arr': ['1', '2', '3'],
michael@0 47 'somenull': '',
michael@0 48 'undef': ''}],
michael@0 49 */
michael@0 50 //[' foo = bar ', '%20foo%20=%20bar%20', {' foo ': ' bar '}],
michael@0 51 // disable test that fails ['foo=%zx', 'foo=%25zx', {'foo': '%zx'}],
michael@0 52 ['foo=%EF%BF%BD', 'foo=%EF%BF%BD', {'foo': '\ufffd' }]
michael@0 53 ];
michael@0 54
michael@0 55 // [ wonkyQS, canonicalQS, obj ]
michael@0 56 var qsColonTestCases = [
michael@0 57 ['foo:bar', 'foo:bar', {'foo': 'bar'}],
michael@0 58 //['foo:bar;foo:quux', 'foo:bar;foo:quux', {'foo': ['bar', 'quux']}],
michael@0 59 ['foo:1&bar:2;baz:quux',
michael@0 60 'foo:1%26bar%3A2;baz:quux',
michael@0 61 {'foo': '1&bar:2', 'baz': 'quux'}],
michael@0 62 ['foo%3Abaz:bar', 'foo%3Abaz:bar', {'foo:baz': 'bar'}],
michael@0 63 ['foo:baz:bar', 'foo:baz%3Abar', {'foo': 'baz:bar'}]
michael@0 64 ];
michael@0 65
michael@0 66 // [wonkyObj, qs, canonicalObj]
michael@0 67 var extendedFunction = function() {};
michael@0 68 extendedFunction.prototype = {a: 'b'};
michael@0 69 var qsWeirdObjects = [
michael@0 70 //[{regexp: /./g}, 'regexp=', {'regexp': ''}],
michael@0 71 //[{regexp: new RegExp('.', 'g')}, 'regexp=', {'regexp': ''}],
michael@0 72 //[{fn: function() {}}, 'fn=', {'fn': ''}],
michael@0 73 //[{fn: new Function('')}, 'fn=', {'fn': ''}],
michael@0 74 //[{math: Math}, 'math=', {'math': ''}],
michael@0 75 //[{e: extendedFunction}, 'e=', {'e': ''}],
michael@0 76 //[{d: new Date()}, 'd=', {'d': ''}],
michael@0 77 //[{d: Date}, 'd=', {'d': ''}],
michael@0 78 //[{f: new Boolean(false), t: new Boolean(true)}, 'f=&t=', {'f': '', 't': ''}],
michael@0 79 [{f: false, t: true}, 'f=false&t=true', {'f': 'false', 't': 'true'}],
michael@0 80 //[{n: null}, 'n=', {'n': ''}],
michael@0 81 //[{nan: NaN}, 'nan=', {'nan': ''}],
michael@0 82 //[{inf: Infinity}, 'inf=', {'inf': ''}]
michael@0 83 ];
michael@0 84 // }}}
michael@0 85
michael@0 86 var qsNoMungeTestCases = [
michael@0 87 ['', {}],
michael@0 88 //['foo=bar&foo=baz', {'foo': ['bar', 'baz']}],
michael@0 89 ['blah=burp', {'blah': 'burp'}],
michael@0 90 //['gragh=1&gragh=3&goo=2', {'gragh': ['1', '3'], 'goo': '2'}],
michael@0 91 ['frappucino=muffin&goat%5B%5D=scone&pond=moose',
michael@0 92 {'frappucino': 'muffin', 'goat[]': 'scone', 'pond': 'moose'}],
michael@0 93 ['trololol=yes&lololo=no', {'trololol': 'yes', 'lololo': 'no'}]
michael@0 94 ];
michael@0 95
michael@0 96 exports['test basic'] = function(assert) {
michael@0 97 assert.strictEqual('918854443121279438895193',
michael@0 98 qs.parse('id=918854443121279438895193').id,
michael@0 99 'prase id=918854443121279438895193');
michael@0 100 };
michael@0 101
michael@0 102 exports['test that the canonical qs is parsed properly'] = function(assert) {
michael@0 103 qsTestCases.forEach(function(testCase) {
michael@0 104 assert.deepEqual(testCase[2], qs.parse(testCase[0]),
michael@0 105 'parse ' + testCase[0]);
michael@0 106 });
michael@0 107 };
michael@0 108
michael@0 109
michael@0 110 exports['test that the colon test cases can do the same'] = function(assert) {
michael@0 111 qsColonTestCases.forEach(function(testCase) {
michael@0 112 assert.deepEqual(testCase[2], qs.parse(testCase[0], ';', ':'),
michael@0 113 'parse ' + testCase[0] + ' -> ; :');
michael@0 114 });
michael@0 115 };
michael@0 116
michael@0 117 exports['test the weird objects, that they get parsed properly'] = function(assert) {
michael@0 118 qsWeirdObjects.forEach(function(testCase) {
michael@0 119 assert.deepEqual(testCase[2], qs.parse(testCase[1]),
michael@0 120 'parse ' + testCase[1]);
michael@0 121 });
michael@0 122 };
michael@0 123
michael@0 124 exports['test non munge test cases'] = function(assert) {
michael@0 125 qsNoMungeTestCases.forEach(function(testCase) {
michael@0 126 assert.deepEqual(testCase[0], qs.stringify(testCase[1], '&', '=', false),
michael@0 127 'stringify ' + JSON.stringify(testCase[1]) + ' -> & =');
michael@0 128 });
michael@0 129 };
michael@0 130
michael@0 131 exports['test the nested qs-in-qs case'] = function(assert) {
michael@0 132 var f = qs.parse('a=b&q=x%3Dy%26y%3Dz');
michael@0 133 f.q = qs.parse(f.q);
michael@0 134 assert.deepEqual(f, { a: 'b', q: { x: 'y', y: 'z' } },
michael@0 135 'parse a=b&q=x%3Dy%26y%3Dz');
michael@0 136 };
michael@0 137
michael@0 138 exports['test nested in colon'] = function(assert) {
michael@0 139 var f = qs.parse('a:b;q:x%3Ay%3By%3Az', ';', ':');
michael@0 140 f.q = qs.parse(f.q, ';', ':');
michael@0 141 assert.deepEqual(f, { a: 'b', q: { x: 'y', y: 'z' } },
michael@0 142 'parse a:b;q:x%3Ay%3By%3Az -> ; :');
michael@0 143 };
michael@0 144
michael@0 145 exports['test stringifying'] = function(assert) {
michael@0 146 qsTestCases.forEach(function(testCase) {
michael@0 147 assert.equal(testCase[1], qs.stringify(testCase[2]),
michael@0 148 'stringify ' + JSON.stringify(testCase[2]));
michael@0 149 });
michael@0 150
michael@0 151 qsColonTestCases.forEach(function(testCase) {
michael@0 152 assert.equal(testCase[1], qs.stringify(testCase[2], ';', ':'),
michael@0 153 'stringify ' + JSON.stringify(testCase[2]) + ' -> ; :');
michael@0 154 });
michael@0 155
michael@0 156 qsWeirdObjects.forEach(function(testCase) {
michael@0 157 assert.equal(testCase[1], qs.stringify(testCase[0]),
michael@0 158 'stringify ' + JSON.stringify(testCase[0]));
michael@0 159 });
michael@0 160 };
michael@0 161
michael@0 162 exports['test stringifying nested'] = function(assert) {
michael@0 163 var f = qs.stringify({
michael@0 164 a: 'b',
michael@0 165 q: qs.stringify({
michael@0 166 x: 'y',
michael@0 167 y: 'z'
michael@0 168 })
michael@0 169 });
michael@0 170 assert.equal(f, 'a=b&q=x%3Dy%26y%3Dz',
michael@0 171 JSON.stringify({
michael@0 172 a: 'b',
michael@0 173 'qs.stringify -> q': {
michael@0 174 x: 'y',
michael@0 175 y: 'z'
michael@0 176 }
michael@0 177 }));
michael@0 178
michael@0 179 var threw = false;
michael@0 180 try { qs.parse(undefined); } catch(error) { threw = true; }
michael@0 181 assert.ok(!threw, "does not throws on undefined");
michael@0 182 };
michael@0 183
michael@0 184 exports['test nested in colon'] = function(assert) {
michael@0 185 var f = qs.stringify({
michael@0 186 a: 'b',
michael@0 187 q: qs.stringify({
michael@0 188 x: 'y',
michael@0 189 y: 'z'
michael@0 190 }, ';', ':')
michael@0 191 }, ';', ':');
michael@0 192 assert.equal(f, 'a:b;q:x%3Ay%3By%3Az',
michael@0 193 'stringify ' + JSON.stringify({
michael@0 194 a: 'b',
michael@0 195 'qs.stringify -> q': {
michael@0 196 x: 'y',
michael@0 197 y: 'z'
michael@0 198 }
michael@0 199 }) + ' -> ; : ');
michael@0 200
michael@0 201
michael@0 202 assert.deepEqual({}, qs.parse(), 'parse undefined');
michael@0 203 };
michael@0 204
michael@0 205 require("test").run(exports);

mercurial