1 /* import-globals-from urlSearchParams_worker.js */
3 // This file gets included into a worker which doesn't have any
4 // assertion methods besides `ok` and `is`.
5 /* eslint-disable mozilla/no-comparison-or-assignment-inside-ok */
7 function testSimpleURLSearchParams() {
8 var u = new URLSearchParams();
9 ok(u, "URLSearchParams created");
10 is(u.has("foo"), false, "URLSearchParams.has(foo)");
11 is(u.get("foo"), null, "URLSearchParams.get(foo)");
12 is(u.getAll("foo").length, 0, "URLSearchParams.getAll(foo)");
14 u.append("foo", "bar");
15 is(u.has("foo"), true, "URLSearchParams.has(foo)");
16 is(u.get("foo"), "bar", "URLSearchParams.get(foo)");
17 is(u.getAll("foo").length, 1, "URLSearchParams.getAll(foo)");
20 is(u.get("foo"), "bar2", "URLSearchParams.get(foo)");
21 is(u.getAll("foo").length, 1, "URLSearchParams.getAll(foo)");
23 is(u + "", "foo=bar2", "stringifier");
30 function testCopyURLSearchParams() {
31 var u = new URLSearchParams();
32 ok(u, "URLSearchParams created");
33 u.append("foo", "bar");
35 var uu = new URLSearchParams(u);
36 is(uu.get("foo"), "bar", "uu.get()");
38 u.append("foo", "bar2");
39 is(u.getAll("foo").length, 2, "u.getAll()");
40 is(uu.getAll("foo").length, 1, "uu.getAll()");
46 var url = new URL("http://www.example.net?a=b&c=d");
47 ok(url.searchParams, "URL searchParams exists!");
48 ok(url.searchParams.has("a"), "URL.searchParams.has('a')");
49 is(url.searchParams.get("a"), "b", "URL.searchParams.get('a')");
50 ok(url.searchParams.has("c"), "URL.searchParams.has('c')");
51 is(url.searchParams.get("c"), "d", "URL.searchParams.get('c')");
53 url.searchParams.set("e", "f");
54 ok(url.href.indexOf("e=f") != 1, "URL right");
56 url = new URL("mailto:a@b.com?subject=Hi");
57 ok(url.searchParams, "URL searchParams exists!");
58 ok(url.searchParams.has("subject"), "Hi");
63 function testParserURLSearchParams() {
65 { input: "", data: {} },
66 { input: "a", data: { a: [""] } },
67 { input: "a=b", data: { a: ["b"] } },
68 { input: "a=", data: { a: [""] } },
69 { input: "=b", data: { "": ["b"] } },
70 { input: "&", data: {} },
71 { input: "&a", data: { a: [""] } },
72 { input: "a&", data: { a: [""] } },
73 { input: "a&a", data: { a: ["", ""] } },
74 { input: "a&b&c", data: { a: [""], b: [""], c: [""] } },
75 { input: "a=b&c=d", data: { a: ["b"], c: ["d"] } },
76 { input: "a=b&c=d&", data: { a: ["b"], c: ["d"] } },
77 { input: "&&&a=b&&&&c=d&", data: { a: ["b"], c: ["d"] } },
78 { input: "a=a&a=b&a=c", data: { a: ["a", "b", "c"] } },
79 { input: "a==a", data: { a: ["=a"] } },
80 { input: "a=a+b+c+d", data: { a: ["a b c d"] } },
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: "%=a", data: { "%": ["a"] } },
86 { input: "%a=a", data: { "%a": ["a"] } },
87 { input: "%a_=a", data: { "%a_": ["a"] } },
88 { input: "%61=a", data: { a: ["a"] } },
89 { input: "%61+%4d%4D=", data: { "a MM": [""] } },
90 { input: "?a=1", data: { a: ["1"] } },
91 { input: "?", data: {} },
92 { input: "?=b", data: { "": ["b"] } },
95 for (var i = 0; i < checks.length; ++i) {
96 var u = new URLSearchParams(checks[i].input);
99 for (var key in checks[i].data) {
101 ok(u.has(key), "key " + key + " found");
103 var all = u.getAll(key);
104 is(all.length, checks[i].data[key].length, "same number of elements");
106 for (var k = 0; k < all.length; ++k) {
107 is(all[k], checks[i].data[key][k], "value matches");
115 function testEncoding() {
120 ["\u0541", "%D5%81"],
123 for (var i = 0; i < encoding.length; ++i) {
124 var url = new URL("http://www.example.net");
125 url.searchParams.set("a", encoding[i][0]);
126 is(url.href, "http://www.example.net/?a=" + encoding[i][1]);
128 var url2 = new URL(url.href);
129 is(url2.searchParams.get("a"), encoding[i][0], "a is still there");
135 function testCopyConstructor() {
136 var url = new URL("http://example.com/");
137 var p = url.searchParams;
138 var q = new URLSearchParams(p);
142 "http://example.com/",
143 "Messing with copy of URLSearchParams should not affect URL"
148 "http://example.com/?c=d",
149 "Messing with URLSearchParams should affect URL"
155 function testOrdering() {
156 var a = new URLSearchParams("a=1&a=2&b=3&c=4&c=5&a=6");
157 is(a.toString(), "a=1&a=2&b=3&c=4&c=5&a=6", "Order is correct");
158 is(a.getAll("a").length, 3, "Correct length of getAll()");
160 var b = new URLSearchParams();
164 is(b.toString(), "a=1&b=2&a=3", "Order is correct");
165 is(b.getAll("a").length, 2, "Correct length of getAll()");
170 function testDelete() {
171 var a = new URLSearchParams("a=1&a=2&b=3&c=4&c=5&a=6");
172 is(a.toString(), "a=1&a=2&b=3&c=4&c=5&a=6", "Order is correct");
173 is(a.getAll("a").length, 3, "Correct length of getAll()");
176 is(a.getAll("a").length, 0, "Correct length of getAll()");
177 is(a.toString(), "b=3&c=4&c=5", "Order is correct");
182 function testGetNULL() {
183 var u = new URLSearchParams();
184 is(typeof u.get(""), "object", "typeof URL.searchParams.get('')");
185 is(u.get(""), null, "URL.searchParams.get('') should be null");
187 var url = new URL("http://www.example.net?a=b");
189 url.searchParams.get("b"),
191 "URL.searchParams.get('b') should be null"
193 is(url.searchParams.get("a"), "b", "URL.searchParams.get('a')");
199 var u = new URLSearchParams();
206 is(u.get("a"), "b", "URL.searchParams.get('a') should return b");
207 is(u.getAll("a").length, 1, "URLSearchParams.getAll('a').length should be 1");
213 is(u.get("a"), "h4", "URL.searchParams.get('a') should return h4");
214 is(u.getAll("a").length, 1, "URLSearchParams.getAll('a').length should be 1");
216 is(u.get("e"), "c", "URL.searchParams.get('e') should return c");
217 is(u.get("i"), "d", "URL.searchParams.get('i') should return d");
218 is(u.get("o"), "f", "URL.searchParams.get('o') should return f");
219 is(u.get("u"), "g", "URL.searchParams.get('u') should return g");
221 is(u.getAll("e").length, 1, "URLSearchParams.getAll('e').length should be 1");
222 is(u.getAll("i").length, 1, "URLSearchParams.getAll('i').length should be 1");
223 is(u.getAll("o").length, 1, "URLSearchParams.getAll('o').length should be 1");
224 is(u.getAll("u").length, 1, "URLSearchParams.getAll('u').length should be 1");
226 u = new URLSearchParams("name1=value1&name1=value2&name1=value3");
230 "URL.searchParams.get('name1') should return value1"
233 u.getAll("name1").length,
235 "URLSearchParams.getAll('name1').length should be 3"
237 u.set("name1", "firstPair");
241 "URL.searchParams.get('name1') should return firstPair"
244 u.getAll("name1").length,
246 "URLSearchParams.getAll('name1').length should be 1"
252 function testIterable() {
253 var u = new URLSearchParams();
260 var key_iter = u.keys();
261 var value_iter = u.values();
262 var entries_iter = u.entries();
263 for (var i = 0; i < 5; ++i) {
265 var key = key_iter.next();
266 var value = value_iter.next();
267 var entry = entries_iter.next();
268 is(key.value, v.toString(), "Correct Key iterator: " + v.toString());
269 ok(!key.done, "Key.done is false");
273 "Correct Value iterator: " + (v * 2).toString()
275 ok(!value.done, "Value.done is false");
279 "Correct Entry 0 iterator: " + v.toString()
284 "Correct Entry 1 iterator: " + (v * 2).toString()
286 ok(!entry.done, "Entry.done is false");
289 var last = key_iter.next();
290 ok(last.done, "Nothing more to read.");
291 is(last.value, undefined, "Undefined is the last key");
293 last = value_iter.next();
294 ok(last.done, "Nothing more to read.");
295 is(last.value, undefined, "Undefined is the last value");
297 last = entries_iter.next();
298 ok(last.done, "Nothing more to read.");
309 last = key_iter.next();
310 ok(last.done, "Nothing more to read.");
311 is(last.value, undefined, "Undefined is the last key");
315 function testZeroHandling() {
316 var u = new URLSearchParams();
319 u.set("g\0h", "i\0j");
322 "a=b%00c&d%00e=f&g%00h=i%00j",
323 "Should encode U+0000 as %00"
329 function testCTORs() {
330 var a = new URLSearchParams("a=b");
331 is(a.get("a"), "b", "CTOR with string");
333 var b = new URLSearchParams([
337 is(b.get("a"), "b", "CTOR with sequence");
338 is(b.get("c"), "d", "CTOR with sequence");
340 ok(new URLSearchParams([]), "CTOR with empty sequence");
344 result = new URLSearchParams([[1]]);
352 "CTOR throws if the sequence doesn't contain exactly 2 elements"
356 result = new URLSearchParams([[1, 2, 3]]);
363 "CTOR throws if the sequence doesn't contain exactly 2 elements"
366 var c = new URLSearchParams({
373 is(c.get("a"), "b", "CTOR with record<>");
374 is(c.get("c"), "42", "CTOR with record<>");
375 is(c.get("d"), "null", "CTOR with record<>");
376 is(c.get("e"), [1, 2, 3].toString(), "CTOR with record<>");
377 is(c.get("f"), { a: 42 }.toString(), "CTOR with record<>");