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.

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

mercurial