dom/base/test/test_urlSearchParams.html

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
michael@0 2 <!DOCTYPE HTML>
michael@0 3 <html>
michael@0 4 <!--
michael@0 5 https://bugzilla.mozilla.org/show_bug.cgi?id=887836
michael@0 6 -->
michael@0 7 <head>
michael@0 8 <meta charset="utf-8">
michael@0 9 <title>Test for Bug 887836</title>
michael@0 10 <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
michael@0 11 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
michael@0 12 </head>
michael@0 13 <body>
michael@0 14 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=887836">Mozilla Bug 887836</a>
michael@0 15 <p id="display"></p>
michael@0 16 <div id="content" style="display: none">
michael@0 17 <iframe name="x" id="x"></iframe>
michael@0 18 <iframe name="y" id="y"></iframe>
michael@0 19 </div>
michael@0 20 <pre id="test">
michael@0 21 </pre>
michael@0 22 <a href="http://www.example.net?a=b&c=d" id="anchor">foobar</a>
michael@0 23 <area href="http://www.example.net?a=b&c=d" id="area">foobar</area>
michael@0 24 <script type="application/javascript">
michael@0 25
michael@0 26 /** Test for Bug 887836 **/
michael@0 27 ok("URLSearchParams" in window, "window.URLSearchParams exists");
michael@0 28
michael@0 29 function testSimpleURLSearchParams() {
michael@0 30 var u = new URLSearchParams();
michael@0 31 ok(u, "URLSearchParams created");
michael@0 32 is(u.has('foo'), false, 'URLSearchParams.has(foo)');
michael@0 33 is(u.get('foo'), '', 'URLSearchParams.get(foo)');
michael@0 34 is(u.getAll('foo').length, 0, 'URLSearchParams.getAll(foo)');
michael@0 35
michael@0 36 u.append('foo', 'bar');
michael@0 37 is(u.has('foo'), true, 'URLSearchParams.has(foo)');
michael@0 38 is(u.get('foo'), 'bar', 'URLSearchParams.get(foo)');
michael@0 39 is(u.getAll('foo').length, 1, 'URLSearchParams.getAll(foo)');
michael@0 40
michael@0 41 u.set('foo', 'bar2');
michael@0 42 is(u.get('foo'), 'bar2', 'URLSearchParams.get(foo)');
michael@0 43 is(u.getAll('foo').length, 1, 'URLSearchParams.getAll(foo)');
michael@0 44
michael@0 45 is(u + "", "foo=bar2", "stringifier");
michael@0 46
michael@0 47 u.delete('foo');
michael@0 48
michael@0 49 runTest();
michael@0 50 }
michael@0 51
michael@0 52 function testCopyURLSearchParams() {
michael@0 53 var u = new URLSearchParams();
michael@0 54 ok(u, "URLSearchParams created");
michael@0 55 u.append('foo', 'bar');
michael@0 56
michael@0 57 var uu = new URLSearchParams(u);
michael@0 58 is(uu.get('foo'), 'bar', 'uu.get()');
michael@0 59
michael@0 60 u.append('foo', 'bar2');
michael@0 61 is(u.getAll('foo').length, 2, "u.getAll()");
michael@0 62 is(uu.getAll('foo').length, 1, "uu.getAll()");
michael@0 63
michael@0 64 runTest();
michael@0 65 }
michael@0 66
michael@0 67 function testParserURLSearchParams() {
michael@0 68 var checks = [
michael@0 69 { input: '', data: {} },
michael@0 70 { input: 'a', data: { 'a' : [''] } },
michael@0 71 { input: 'a=b', data: { 'a' : ['b'] } },
michael@0 72 { input: 'a=', data: { 'a' : [''] } },
michael@0 73 { input: '=b', data: { '' : ['b'] } },
michael@0 74 { input: '&', data: {} },
michael@0 75 { input: '&a', data: { 'a' : [''] } },
michael@0 76 { input: 'a&', data: { 'a' : [''] } },
michael@0 77 { input: 'a&a', data: { 'a' : ['', ''] } },
michael@0 78 { input: 'a&b&c', data: { 'a' : [''], 'b' : [''], 'c' : [''] } },
michael@0 79 { input: 'a=b&c=d', data: { 'a' : ['b'], 'c' : ['d'] } },
michael@0 80 { input: 'a=b&c=d&', data: { 'a' : ['b'], 'c' : ['d'] } },
michael@0 81 { input: '&&&a=b&&&&c=d&', data: { 'a' : ['b'], 'c' : ['d'] } },
michael@0 82 { input: 'a=a&a=b&a=c', data: { 'a' : ['a', 'b', 'c'] } },
michael@0 83 { input: 'a==a', data: { 'a' : ['=a'] } },
michael@0 84 { input: 'a=a+b+c+d', data: { 'a' : ['a b c d'] } },
michael@0 85 { input: '%=a', data: { '%' : ['a'] } },
michael@0 86 { input: '%a=a', data: { '%a' : ['a'] } },
michael@0 87 { input: '%a_=a', data: { '%a_' : ['a'] } },
michael@0 88 { input: '%61=a', data: { 'a' : ['a'] } },
michael@0 89 { input: '%=a', data: { '%' : ['a'] } },
michael@0 90 { input: '%a=a', data: { '%a' : ['a'] } },
michael@0 91 { input: '%a_=a', data: { '%a_' : ['a'] } },
michael@0 92 { input: '%61=a', data: { 'a' : ['a'] } },
michael@0 93 { input: '%61+%4d%4D=', data: { 'a MM' : [''] } },
michael@0 94 ];
michael@0 95
michael@0 96 for (var i = 0; i < checks.length; ++i) {
michael@0 97 var u = new URLSearchParams(checks[i].input);
michael@0 98
michael@0 99 var count = 0;
michael@0 100 for (var key in checks[i].data) {
michael@0 101 ++count;
michael@0 102 ok(u.has(key), "key " + key + " found");
michael@0 103
michael@0 104 var all = u.getAll(key);
michael@0 105 is(all.length, checks[i].data[key].length, "same number of elements");
michael@0 106
michael@0 107 for (var k = 0; k < all.length; ++k) {
michael@0 108 is(all[k], checks[i].data[key][k], "value matches");
michael@0 109 }
michael@0 110 }
michael@0 111 }
michael@0 112
michael@0 113 runTest();
michael@0 114 }
michael@0 115
michael@0 116 function testURL() {
michael@0 117 var url = new URL('http://www.example.net?a=b&c=d');
michael@0 118 ok(url.searchParams, "URL searchParams exists!");
michael@0 119 ok(url.searchParams.has('a'), "URL.searchParams.has('a')");
michael@0 120 is(url.searchParams.get('a'), 'b', "URL.searchParams.get('a')");
michael@0 121 ok(url.searchParams.has('c'), "URL.searchParams.has('c')");
michael@0 122 is(url.searchParams.get('c'), 'd', "URL.searchParams.get('c')");
michael@0 123
michael@0 124 url.searchParams.set('e', 'f');
michael@0 125 ok(url.href.indexOf('e=f') != 1, 'URL right');
michael@0 126
michael@0 127 var u = new URLSearchParams();
michael@0 128 u.append('foo', 'bar');
michael@0 129 url.searchParams = u;
michael@0 130 is(url.searchParams, u, "URL.searchParams is the same object");
michael@0 131 is(url.searchParams.get('foo'), 'bar', "URL.searchParams.get('foo')");
michael@0 132 is(url.href, 'http://www.example.net/?foo=bar', 'URL right');
michael@0 133
michael@0 134 try {
michael@0 135 url.searchParams = null;
michael@0 136 ok(false, "URLSearchParams is not nullable");
michael@0 137 } catch(e) {
michael@0 138 ok(true, "URLSearchParams is not nullable");
michael@0 139 }
michael@0 140
michael@0 141 var url2 = new URL('http://www.example.net?e=f');
michael@0 142 url.searchParams = url2.searchParams;
michael@0 143 is(url.searchParams, url2.searchParams, "URL.searchParams is not the same object");
michael@0 144 is(url.searchParams.get('e'), 'f', "URL.searchParams.get('e')");
michael@0 145
michael@0 146 url.href = "http://www.example.net?bar=foo";
michael@0 147 is(url.searchParams.get('bar'), 'foo', "URL.searchParams.get('bar')");
michael@0 148
michael@0 149 runTest();
michael@0 150 }
michael@0 151
michael@0 152 function testElement(e) {
michael@0 153 ok(e, 'element exists');
michael@0 154 ok(e.searchParams, "e.searchParams exists!");
michael@0 155 ok(e.searchParams.has('a'), "e.searchParams.has('a')");
michael@0 156 is(e.searchParams.get('a'), 'b', "e.searchParams.get('a')");
michael@0 157 ok(e.searchParams.has('c'), "e.searchParams.has('c')");
michael@0 158 is(e.searchParams.get('c'), 'd', "e.searchParams.get('c')");
michael@0 159
michael@0 160 e.searchParams.set('e', 'f');
michael@0 161 ok(e.href.indexOf('e=f') != 1, 'e is right');
michael@0 162
michael@0 163 var u = new URLSearchParams();
michael@0 164 u.append('foo', 'bar');
michael@0 165 e.searchParams = u;
michael@0 166 is(e.searchParams, u, "e.searchParams is the same object");
michael@0 167 is(e.searchParams.get('foo'), 'bar', "e.searchParams.get('foo')");
michael@0 168 is(e.href, 'http://www.example.net/?foo=bar', 'e is right');
michael@0 169
michael@0 170 try {
michael@0 171 e.searchParams = null;
michael@0 172 ok(false, "URLSearchParams is not nullable");
michael@0 173 } catch(e) {
michael@0 174 ok(true, "URLSearchParams is not nullable");
michael@0 175 }
michael@0 176
michael@0 177 var url2 = new URL('http://www.example.net?e=f');
michael@0 178 e.searchParams = url2.searchParams;
michael@0 179 is(e.searchParams, url2.searchParams, "e.searchParams is not the same object");
michael@0 180 is(e.searchParams.get('e'), 'f', "e.searchParams.get('e')");
michael@0 181
michael@0 182 e.href = "http://www.example.net?bar=foo";
michael@0 183 is(e.searchParams.get('bar'), 'foo', "e.searchParams.get('bar')");
michael@0 184
michael@0 185 e.setAttribute('href', "http://www.example.net?bar2=foo2");
michael@0 186 is(e.searchParams.get('bar2'), 'foo2', "e.searchParams.get('bar2')");
michael@0 187
michael@0 188 runTest();
michael@0 189 }
michael@0 190
michael@0 191 function testEncoding() {
michael@0 192 var encoding = [ [ '1', '1' ],
michael@0 193 [ 'a b', 'a+b' ],
michael@0 194 [ '<>', '%3C%3E' ],
michael@0 195 [ '\u0541', '%D5%81'] ];
michael@0 196
michael@0 197 for (var i = 0; i < encoding.length; ++i) {
michael@0 198 var a = new URLSearchParams();
michael@0 199 a.set('a', encoding[i][0]);
michael@0 200
michael@0 201 var url = new URL('http://www.example.net');
michael@0 202 url.searchParams = a;
michael@0 203 is(url.href, 'http://www.example.net/?a=' + encoding[i][1]);
michael@0 204
michael@0 205 var url2 = new URL(url.href);
michael@0 206 is(url2.searchParams.get('a'), encoding[i][0], 'a is still there');
michael@0 207 }
michael@0 208
michael@0 209 runTest();
michael@0 210 }
michael@0 211
michael@0 212 function testMultiURL() {
michael@0 213 var a = new URL('http://www.example.net?a=b&c=d');
michael@0 214 var b = new URL('http://www.example.net?e=f');
michael@0 215 var c = document.createElement('a');
michael@0 216 var d = document.createElement('area');
michael@0 217 ok(a.searchParams.has('a'), "a.searchParams.has('a')");
michael@0 218 ok(a.searchParams.has('c'), "a.searchParams.has('c')");
michael@0 219 ok(b.searchParams.has('e'), "b.searchParams.has('e')");
michael@0 220 ok(c.searchParams, "c.searchParams");
michael@0 221 ok(d.searchParams, "d.searchParams");
michael@0 222
michael@0 223 var u = new URLSearchParams();
michael@0 224 a.searchParams = b.searchParams = c.searchParams = d.searchParams = u;
michael@0 225 is(a.searchParams, u, "a.searchParams === u");
michael@0 226 is(b.searchParams, u, "b.searchParams === u");
michael@0 227 is(c.searchParams, u, "c.searchParams === u");
michael@0 228 is(d.searchParams, u, "d.searchParams === u");
michael@0 229 ok(!a.searchParams.has('a'), "!a.searchParams.has('a')");
michael@0 230 ok(!a.searchParams.has('c'), "!a.searchParams.has('c')");
michael@0 231 ok(!b.searchParams.has('e'), "!b.searchParams.has('e')");
michael@0 232
michael@0 233 u.append('foo', 'bar');
michael@0 234 is(a.searchParams.get('foo'), 'bar', "a has foo=bar");
michael@0 235 is(b.searchParams.get('foo'), 'bar', "b has foo=bar");
michael@0 236 is(c.searchParams.get('foo'), 'bar', "c has foo=bar");
michael@0 237 is(d.searchParams.get('foo'), 'bar', "d has foo=bar");
michael@0 238 is(a + "", b + "", "stringify a == b");
michael@0 239 is(c.searchParams + "", b.searchParams + "", "stringify c.searchParams == b.searchParams");
michael@0 240 is(d.searchParams + "", b.searchParams + "", "stringify d.searchParams == b.searchParams");
michael@0 241
michael@0 242 a.search = "?bar=foo";
michael@0 243 is(a.searchParams.get('bar'), 'foo', "a has bar=foo");
michael@0 244 is(b.searchParams.get('bar'), 'foo', "b has bar=foo");
michael@0 245 is(c.searchParams.get('bar'), 'foo', "c has bar=foo");
michael@0 246 is(d.searchParams.get('bar'), 'foo', "d has bar=foo");
michael@0 247
michael@0 248 runTest();
michael@0 249 }
michael@0 250
michael@0 251 var tests = [
michael@0 252 testSimpleURLSearchParams,
michael@0 253 testCopyURLSearchParams,
michael@0 254 testParserURLSearchParams,
michael@0 255 testURL,
michael@0 256 function() { testElement(document.getElementById('anchor')) },
michael@0 257 function() { testElement(document.getElementById('area')) },
michael@0 258 testEncoding,
michael@0 259 testMultiURL
michael@0 260 ];
michael@0 261
michael@0 262 function runTest() {
michael@0 263 if (!tests.length) {
michael@0 264 SimpleTest.finish();
michael@0 265 return;
michael@0 266 }
michael@0 267
michael@0 268 var test = tests.shift();
michael@0 269 test();
michael@0 270 }
michael@0 271
michael@0 272 SimpleTest.waitForExplicitFinish();
michael@0 273 runTest();
michael@0 274
michael@0 275 </script>
michael@0 276 </body>
michael@0 277 </html>

mercurial