dom/workers/test/urlSearchParams_worker.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 function ok(a, msg) {
michael@0 2 dump("OK: " + !!a + " => " + a + " " + msg + "\n");
michael@0 3 postMessage({type: 'status', status: !!a, msg: a + ": " + msg });
michael@0 4 }
michael@0 5
michael@0 6 function is(a, b, msg) {
michael@0 7 dump("IS: " + (a===b) + " => " + a + " | " + b + " " + msg + "\n");
michael@0 8 postMessage({type: 'status', status: a === b, msg: a + " === " + b + ": " + msg });
michael@0 9 }
michael@0 10
michael@0 11 onmessage = function() {
michael@0 12 status = false;
michael@0 13 try {
michael@0 14 if ((URLSearchParams instanceof Object)) {
michael@0 15 status = true;
michael@0 16 }
michael@0 17 } catch(e) {
michael@0 18 }
michael@0 19 ok(status, "URLSearchParams in workers \\o/");
michael@0 20
michael@0 21 function testSimpleURLSearchParams() {
michael@0 22 var u = new URLSearchParams();
michael@0 23 ok(u, "URLSearchParams created");
michael@0 24 is(u.has('foo'), false, 'URLSearchParams.has(foo)');
michael@0 25 is(u.get('foo'), '', 'URLSearchParams.get(foo)');
michael@0 26 is(u.getAll('foo').length, 0, 'URLSearchParams.getAll(foo)');
michael@0 27
michael@0 28 u.append('foo', 'bar');
michael@0 29 is(u.has('foo'), true, 'URLSearchParams.has(foo)');
michael@0 30 is(u.get('foo'), 'bar', 'URLSearchParams.get(foo)');
michael@0 31 is(u.getAll('foo').length, 1, 'URLSearchParams.getAll(foo)');
michael@0 32
michael@0 33 u.set('foo', 'bar2');
michael@0 34 is(u.get('foo'), 'bar2', 'URLSearchParams.get(foo)');
michael@0 35 is(u.getAll('foo').length, 1, 'URLSearchParams.getAll(foo)');
michael@0 36
michael@0 37 is(u + "", "foo=bar2", "stringify");
michael@0 38
michael@0 39 u.delete('foo');
michael@0 40
michael@0 41 runTest();
michael@0 42 }
michael@0 43
michael@0 44 function testCopyURLSearchParams() {
michael@0 45 var u = new URLSearchParams();
michael@0 46 ok(u, "URLSearchParams created");
michael@0 47 u.append('foo', 'bar');
michael@0 48
michael@0 49 var uu = new URLSearchParams(u);
michael@0 50 is(uu.get('foo'), 'bar', 'uu.get()');
michael@0 51
michael@0 52 u.append('foo', 'bar2');
michael@0 53 is(u.getAll('foo').length, 2, "u.getAll()");
michael@0 54 is(uu.getAll('foo').length, 1, "uu.getAll()");
michael@0 55
michael@0 56 runTest();
michael@0 57 }
michael@0 58
michael@0 59 function testParserURLSearchParams() {
michael@0 60 var checks = [
michael@0 61 { input: '', data: {} },
michael@0 62 { input: 'a', data: { 'a' : [''] } },
michael@0 63 { input: 'a=b', data: { 'a' : ['b'] } },
michael@0 64 { input: 'a=', data: { 'a' : [''] } },
michael@0 65 { input: '=b', data: { '' : ['b'] } },
michael@0 66 { input: '&', data: {} },
michael@0 67 { input: '&a', data: { 'a' : [''] } },
michael@0 68 { input: 'a&', data: { 'a' : [''] } },
michael@0 69 { input: 'a&a', data: { 'a' : ['', ''] } },
michael@0 70 { input: 'a&b&c', data: { 'a' : [''], 'b' : [''], 'c' : [''] } },
michael@0 71 { input: 'a=b&c=d', data: { 'a' : ['b'], 'c' : ['d'] } },
michael@0 72 { input: 'a=b&c=d&', data: { 'a' : ['b'], 'c' : ['d'] } },
michael@0 73 { input: '&&&a=b&&&&c=d&', data: { 'a' : ['b'], 'c' : ['d'] } },
michael@0 74 { input: 'a=a&a=b&a=c', data: { 'a' : ['a', 'b', 'c'] } },
michael@0 75 { input: 'a==a', data: { 'a' : ['=a'] } },
michael@0 76 { input: 'a=a+b+c+d', data: { 'a' : ['a b c d'] } },
michael@0 77 { input: '%=a', data: { '%' : ['a'] } },
michael@0 78 { input: '%a=a', data: { '%a' : ['a'] } },
michael@0 79 { input: '%a_=a', data: { '%a_' : ['a'] } },
michael@0 80 { input: '%61=a', data: { 'a' : ['a'] } },
michael@0 81 { input: '%=a', data: { '%' : ['a'] } },
michael@0 82 { input: '%a=a', data: { '%a' : ['a'] } },
michael@0 83 { input: '%a_=a', data: { '%a_' : ['a'] } },
michael@0 84 { input: '%61=a', data: { 'a' : ['a'] } },
michael@0 85 { input: '%61+%4d%4D=', data: { 'a MM' : [''] } },
michael@0 86 ];
michael@0 87
michael@0 88 for (var i = 0; i < checks.length; ++i) {
michael@0 89 var u = new URLSearchParams(checks[i].input);
michael@0 90
michael@0 91 var count = 0;
michael@0 92 for (var key in checks[i].data) {
michael@0 93 ++count;
michael@0 94 ok(u.has(key), "key " + key + " found");
michael@0 95
michael@0 96 var all = u.getAll(key);
michael@0 97 is(all.length, checks[i].data[key].length, "same number of elements");
michael@0 98
michael@0 99 for (var k = 0; k < all.length; ++k) {
michael@0 100 is(all[k], checks[i].data[key][k], "value matches");
michael@0 101 }
michael@0 102 }
michael@0 103 }
michael@0 104
michael@0 105 runTest();
michael@0 106 }
michael@0 107
michael@0 108 function testURL() {
michael@0 109 var url = new URL('http://www.example.net?a=b&c=d');
michael@0 110 ok(url.searchParams, "URL searchParams exists!");
michael@0 111 ok(url.searchParams.has('a'), "URL.searchParams.has('a')");
michael@0 112 is(url.searchParams.get('a'), 'b', "URL.searchParams.get('a')");
michael@0 113 ok(url.searchParams.has('c'), "URL.searchParams.has('c')");
michael@0 114 is(url.searchParams.get('c'), 'd', "URL.searchParams.get('c')");
michael@0 115
michael@0 116 url.searchParams.set('e', 'f');
michael@0 117 ok(url.href.indexOf('e=f') != 1, 'URL right');
michael@0 118
michael@0 119 var u = new URLSearchParams();
michael@0 120 u.append('foo', 'bar');
michael@0 121 url.searchParams = u;
michael@0 122 is(url.searchParams, u, "URL.searchParams is the same object");
michael@0 123 is(url.searchParams.get('foo'), 'bar', "URL.searchParams.get('foo')");
michael@0 124 is(url.href, 'http://www.example.net/?foo=bar', 'URL right');
michael@0 125
michael@0 126 try {
michael@0 127 url.searchParams = null;
michael@0 128 ok(false, "URLSearchParams is not nullable");
michael@0 129 } catch(e) {
michael@0 130 ok(true, "URLSearchParams is not nullable");
michael@0 131 }
michael@0 132
michael@0 133 var url2 = new URL('http://www.example.net?e=f');
michael@0 134 url.searchParams = url2.searchParams;
michael@0 135 is(url.searchParams, url2.searchParams, "URL.searchParams is not the same object");
michael@0 136 is(url.searchParams.get('e'), 'f', "URL.searchParams.get('e')");
michael@0 137
michael@0 138 url.href = "http://www.example.net?bar=foo";
michael@0 139 is(url.searchParams.get('bar'), 'foo', "URL.searchParams.get('bar')");
michael@0 140
michael@0 141 runTest();
michael@0 142 }
michael@0 143
michael@0 144 function testEncoding() {
michael@0 145 var encoding = [ [ '1', '1' ],
michael@0 146 [ 'a b', 'a+b' ],
michael@0 147 [ '<>', '%3C%3E' ],
michael@0 148 [ '\u0541', '%D5%81'] ];
michael@0 149
michael@0 150 for (var i = 0; i < encoding.length; ++i) {
michael@0 151 var a = new URLSearchParams();
michael@0 152 a.set('a', encoding[i][0]);
michael@0 153
michael@0 154 var url = new URL('http://www.example.net');
michael@0 155 url.searchParams = a;
michael@0 156 is(url.href, 'http://www.example.net/?a=' + encoding[i][1]);
michael@0 157
michael@0 158 var url2 = new URL(url.href);
michael@0 159 is(url2.searchParams.get('a'), encoding[i][0], 'a is still there');
michael@0 160 }
michael@0 161
michael@0 162 runTest();
michael@0 163 }
michael@0 164
michael@0 165 function testMultiURL() {
michael@0 166 var a = new URL('http://www.example.net?a=b&c=d');
michael@0 167 var b = new URL('http://www.example.net?e=f');
michael@0 168 ok(a.searchParams.has('a'), "a.searchParams.has('a')");
michael@0 169 ok(a.searchParams.has('c'), "a.searchParams.has('c')");
michael@0 170 ok(b.searchParams.has('e'), "b.searchParams.has('e')");
michael@0 171
michael@0 172 var u = new URLSearchParams();
michael@0 173 a.searchParams = b.searchParams = u;
michael@0 174 is(a.searchParams, u, "a.searchParams === u");
michael@0 175 is(b.searchParams, u, "b.searchParams === u");
michael@0 176 ok(!a.searchParams.has('a'), "!a.searchParams.has('a')");
michael@0 177 ok(!a.searchParams.has('c'), "!a.searchParams.has('c')");
michael@0 178 ok(!b.searchParams.has('e'), "!b.searchParams.has('e')");
michael@0 179
michael@0 180 u.append('foo', 'bar');
michael@0 181 is(a.searchParams.get('foo'), 'bar', "a has foo=bar");
michael@0 182 is(b.searchParams.get('foo'), 'bar', "b has foo=bar");
michael@0 183 is(a + "", b + "", "stringify a == b");
michael@0 184
michael@0 185 a.search = "?bar=foo";
michael@0 186 is(a.searchParams.get('bar'), 'foo', "a has bar=foo");
michael@0 187 is(b.searchParams.get('bar'), 'foo', "b has bar=foo");
michael@0 188
michael@0 189 runTest();
michael@0 190 }
michael@0 191 var tests = [
michael@0 192 testSimpleURLSearchParams,
michael@0 193 testCopyURLSearchParams,
michael@0 194 testParserURLSearchParams,
michael@0 195 testURL,
michael@0 196 testEncoding,
michael@0 197 testMultiURL
michael@0 198 ];
michael@0 199
michael@0 200 function runTest() {
michael@0 201 if (!tests.length) {
michael@0 202 postMessage({type: 'finish' });
michael@0 203 return;
michael@0 204 }
michael@0 205
michael@0 206 var test = tests.shift();
michael@0 207 test();
michael@0 208 }
michael@0 209
michael@0 210 runTest();
michael@0 211 }

mercurial