gecko-dev/dom/base/test/test_urlSearchParams.html

307 строки
10 KiB
HTML

<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=887836
-->
<head>
<meta charset="utf-8">
<title>Test for Bug 887836</title>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=887836">Mozilla Bug 887836</a>
<p id="display"></p>
<div id="content" style="display: none">
<iframe name="x" id="x"></iframe>
<iframe name="y" id="y"></iframe>
</div>
<pre id="test">
</pre>
<a href="http://www.example.net?a=b&c=d" id="anchor">foobar</a>
<area href="http://www.example.net?a=b&c=d" id="area">foobar</area>
<script type="application/javascript">
/** Test for Bug 887836 **/
ok("URLSearchParams" in window, "window.URLSearchParams exists");
function testSimpleURLSearchParams() {
var u = new URLSearchParams();
ok(u, "URLSearchParams created");
is(u.has('foo'), false, 'URLSearchParams.has(foo)');
is(u.get('foo'), '', 'URLSearchParams.get(foo)');
is(u.getAll('foo').length, 0, 'URLSearchParams.getAll(foo)');
u.append('foo', 'bar');
is(u.has('foo'), true, 'URLSearchParams.has(foo)');
is(u.get('foo'), 'bar', 'URLSearchParams.get(foo)');
is(u.getAll('foo').length, 1, 'URLSearchParams.getAll(foo)');
u.set('foo', 'bar2');
is(u.get('foo'), 'bar2', 'URLSearchParams.get(foo)');
is(u.getAll('foo').length, 1, 'URLSearchParams.getAll(foo)');
is(u + "", "foo=bar2", "stringifier");
u.delete('foo');
runTest();
}
function testCopyURLSearchParams() {
var u = new URLSearchParams();
ok(u, "URLSearchParams created");
u.append('foo', 'bar');
var uu = new URLSearchParams(u);
is(uu.get('foo'), 'bar', 'uu.get()');
u.append('foo', 'bar2');
is(u.getAll('foo').length, 2, "u.getAll()");
is(uu.getAll('foo').length, 1, "uu.getAll()");
runTest();
}
function testParserURLSearchParams() {
var checks = [
{ input: '', data: {} },
{ input: 'a', data: { 'a' : [''] } },
{ input: 'a=b', data: { 'a' : ['b'] } },
{ input: 'a=', data: { 'a' : [''] } },
{ input: '=b', data: { '' : ['b'] } },
{ input: '&', data: {} },
{ input: '&a', data: { 'a' : [''] } },
{ input: 'a&', data: { 'a' : [''] } },
{ input: 'a&a', data: { 'a' : ['', ''] } },
{ input: 'a&b&c', data: { 'a' : [''], 'b' : [''], 'c' : [''] } },
{ input: 'a=b&c=d', data: { 'a' : ['b'], 'c' : ['d'] } },
{ input: 'a=b&c=d&', data: { 'a' : ['b'], 'c' : ['d'] } },
{ input: '&&&a=b&&&&c=d&', data: { 'a' : ['b'], 'c' : ['d'] } },
{ input: 'a=a&a=b&a=c', data: { 'a' : ['a', 'b', 'c'] } },
{ input: 'a==a', data: { 'a' : ['=a'] } },
{ input: 'a=a+b+c+d', data: { 'a' : ['a b c d'] } },
{ input: '%=a', data: { '%' : ['a'] } },
{ input: '%a=a', data: { '%a' : ['a'] } },
{ input: '%a_=a', data: { '%a_' : ['a'] } },
{ input: '%61=a', data: { 'a' : ['a'] } },
{ input: '%=a', data: { '%' : ['a'] } },
{ input: '%a=a', data: { '%a' : ['a'] } },
{ input: '%a_=a', data: { '%a_' : ['a'] } },
{ input: '%61=a', data: { 'a' : ['a'] } },
{ input: '%61+%4d%4D=', data: { 'a MM' : [''] } },
];
for (var i = 0; i < checks.length; ++i) {
var u = new URLSearchParams(checks[i].input);
var count = 0;
for (var key in checks[i].data) {
++count;
ok(u.has(key), "key " + key + " found");
var all = u.getAll(key);
is(all.length, checks[i].data[key].length, "same number of elements");
for (var k = 0; k < all.length; ++k) {
is(all[k], checks[i].data[key][k], "value matches");
}
}
}
runTest();
}
function testURL() {
var url = new URL('http://www.example.net?a=b&c=d');
ok(url.searchParams, "URL searchParams exists!");
ok(url.searchParams.has('a'), "URL.searchParams.has('a')");
is(url.searchParams.get('a'), 'b', "URL.searchParams.get('a')");
ok(url.searchParams.has('c'), "URL.searchParams.has('c')");
is(url.searchParams.get('c'), 'd', "URL.searchParams.get('c')");
url.searchParams.set('e', 'f');
ok(url.href.indexOf('e=f') != 1, 'URL right');
var u = new URLSearchParams();
u.append('foo', 'bar');
url.searchParams = u;
is(url.searchParams, u, "URL.searchParams is the same object");
is(url.searchParams.get('foo'), 'bar', "URL.searchParams.get('foo')");
is(url.href, 'http://www.example.net/?foo=bar', 'URL right');
try {
url.searchParams = null;
ok(false, "URLSearchParams is not nullable");
} catch(e) {
ok(true, "URLSearchParams is not nullable");
}
var url2 = new URL('http://www.example.net?e=f');
url.searchParams = url2.searchParams;
is(url.searchParams, url2.searchParams, "URL.searchParams is not the same object");
is(url.searchParams.get('e'), 'f', "URL.searchParams.get('e')");
url.href = "http://www.example.net?bar=foo";
is(url.searchParams.get('bar'), 'foo', "URL.searchParams.get('bar')");
runTest();
}
function testElement(e) {
ok(e, 'element exists');
ok(e.searchParams, "e.searchParams exists!");
ok(e.searchParams.has('a'), "e.searchParams.has('a')");
is(e.searchParams.get('a'), 'b', "e.searchParams.get('a')");
ok(e.searchParams.has('c'), "e.searchParams.has('c')");
is(e.searchParams.get('c'), 'd', "e.searchParams.get('c')");
e.searchParams.set('e', 'f');
ok(e.href.indexOf('e=f') != 1, 'e is right');
var u = new URLSearchParams();
u.append('foo', 'bar');
e.searchParams = u;
is(e.searchParams, u, "e.searchParams is the same object");
is(e.searchParams.get('foo'), 'bar', "e.searchParams.get('foo')");
is(e.href, 'http://www.example.net/?foo=bar', 'e is right');
try {
e.searchParams = null;
ok(false, "URLSearchParams is not nullable");
} catch(e) {
ok(true, "URLSearchParams is not nullable");
}
var url2 = new URL('http://www.example.net?e=f');
e.searchParams = url2.searchParams;
is(e.searchParams, url2.searchParams, "e.searchParams is not the same object");
is(e.searchParams.get('e'), 'f', "e.searchParams.get('e')");
e.href = "http://www.example.net?bar=foo";
is(e.searchParams.get('bar'), 'foo', "e.searchParams.get('bar')");
e.setAttribute('href', "http://www.example.net?bar2=foo2");
is(e.searchParams.get('bar2'), 'foo2', "e.searchParams.get('bar2')");
runTest();
}
function testEncoding() {
var encoding = [ [ '1', '1' ],
[ 'a b', 'a+b' ],
[ '<>', '%3C%3E' ],
[ '\u0541', '%D5%81'] ];
for (var i = 0; i < encoding.length; ++i) {
var a = new URLSearchParams();
a.set('a', encoding[i][0]);
var url = new URL('http://www.example.net');
url.searchParams = a;
is(url.href, 'http://www.example.net/?a=' + encoding[i][1]);
var url2 = new URL(url.href);
is(url2.searchParams.get('a'), encoding[i][0], 'a is still there');
}
runTest();
}
function testMultiURL() {
var a = new URL('http://www.example.net?a=b&c=d');
var b = new URL('http://www.example.net?e=f');
var c = document.createElement('a');
var d = document.createElement('area');
ok(a.searchParams.has('a'), "a.searchParams.has('a')");
ok(a.searchParams.has('c'), "a.searchParams.has('c')");
ok(b.searchParams.has('e'), "b.searchParams.has('e')");
ok(c.searchParams, "c.searchParams");
ok(d.searchParams, "d.searchParams");
var u = new URLSearchParams();
a.searchParams = b.searchParams = c.searchParams = d.searchParams = u;
is(a.searchParams, u, "a.searchParams === u");
is(b.searchParams, u, "b.searchParams === u");
is(c.searchParams, u, "c.searchParams === u");
is(d.searchParams, u, "d.searchParams === u");
ok(!a.searchParams.has('a'), "!a.searchParams.has('a')");
ok(!a.searchParams.has('c'), "!a.searchParams.has('c')");
ok(!b.searchParams.has('e'), "!b.searchParams.has('e')");
u.append('foo', 'bar');
is(a.searchParams.get('foo'), 'bar', "a has foo=bar");
is(b.searchParams.get('foo'), 'bar', "b has foo=bar");
is(c.searchParams.get('foo'), 'bar', "c has foo=bar");
is(d.searchParams.get('foo'), 'bar', "d has foo=bar");
is(a + "", b + "", "stringify a == b");
is(c.searchParams + "", b.searchParams + "", "stringify c.searchParams == b.searchParams");
is(d.searchParams + "", b.searchParams + "", "stringify d.searchParams == b.searchParams");
a.search = "?bar=foo";
is(a.searchParams.get('bar'), 'foo', "a has bar=foo");
is(b.searchParams.get('bar'), 'foo', "b has bar=foo");
is(c.searchParams.get('bar'), 'foo', "c has bar=foo");
is(d.searchParams.get('bar'), 'foo', "d has bar=foo");
runTest();
}
function testOrdering() {
var a = new URLSearchParams("a=1&a=2&b=3&c=4&c=5&a=6");
is(a.toString(), "a=1&a=2&b=3&c=4&c=5&a=6", "Order is correct");
is(a.getAll('a').length, 3, "Correct length of getAll()");
var b = new URLSearchParams();
b.append('a', '1');
b.append('b', '2');
b.append('a', '3');
is(b.toString(), "a=1&b=2&a=3", "Order is correct");
is(b.getAll('a').length, 2, "Correct length of getAll()");
runTest();
}
function testDelete() {
var a = new URLSearchParams("a=1&a=2&b=3&c=4&c=5&a=6");
is(a.toString(), "a=1&a=2&b=3&c=4&c=5&a=6", "Order is correct");
is(a.getAll('a').length, 3, "Correct length of getAll()");
a.delete('a');
is(a.getAll('a').length, 0, "Correct length of getAll()");
is(a.toString(), "b=3&c=4&c=5", "Order is correct");
runTest();
}
var tests = [
testSimpleURLSearchParams,
testCopyURLSearchParams,
testParserURLSearchParams,
testURL,
function() { testElement(document.getElementById('anchor')) },
function() { testElement(document.getElementById('area')) },
testEncoding,
testMultiURL,
testOrdering,
testDelete
];
function runTest() {
if (!tests.length) {
SimpleTest.finish();
return;
}
var test = tests.shift();
test();
}
SimpleTest.waitForExplicitFinish();
runTest();
</script>
</body>
</html>