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