michael@0: function ok(a, msg) { michael@0: dump("OK: " + !!a + " => " + a + " " + msg + "\n"); michael@0: postMessage({type: 'status', status: !!a, msg: a + ": " + msg }); michael@0: } michael@0: michael@0: function is(a, b, msg) { michael@0: dump("IS: " + (a===b) + " => " + a + " | " + b + " " + msg + "\n"); michael@0: postMessage({type: 'status', status: a === b, msg: a + " === " + b + ": " + msg }); michael@0: } michael@0: michael@0: onmessage = function() { michael@0: status = false; michael@0: try { michael@0: if ((URLSearchParams instanceof Object)) { michael@0: status = true; michael@0: } michael@0: } catch(e) { michael@0: } michael@0: ok(status, "URLSearchParams in workers \\o/"); michael@0: michael@0: function testSimpleURLSearchParams() { michael@0: var u = new URLSearchParams(); michael@0: ok(u, "URLSearchParams created"); michael@0: is(u.has('foo'), false, 'URLSearchParams.has(foo)'); michael@0: is(u.get('foo'), '', 'URLSearchParams.get(foo)'); michael@0: is(u.getAll('foo').length, 0, 'URLSearchParams.getAll(foo)'); michael@0: michael@0: u.append('foo', 'bar'); michael@0: is(u.has('foo'), true, 'URLSearchParams.has(foo)'); michael@0: is(u.get('foo'), 'bar', 'URLSearchParams.get(foo)'); michael@0: is(u.getAll('foo').length, 1, 'URLSearchParams.getAll(foo)'); michael@0: michael@0: u.set('foo', 'bar2'); michael@0: is(u.get('foo'), 'bar2', 'URLSearchParams.get(foo)'); michael@0: is(u.getAll('foo').length, 1, 'URLSearchParams.getAll(foo)'); michael@0: michael@0: is(u + "", "foo=bar2", "stringify"); michael@0: michael@0: u.delete('foo'); michael@0: michael@0: runTest(); michael@0: } michael@0: michael@0: function testCopyURLSearchParams() { michael@0: var u = new URLSearchParams(); michael@0: ok(u, "URLSearchParams created"); michael@0: u.append('foo', 'bar'); michael@0: michael@0: var uu = new URLSearchParams(u); michael@0: is(uu.get('foo'), 'bar', 'uu.get()'); michael@0: michael@0: u.append('foo', 'bar2'); michael@0: is(u.getAll('foo').length, 2, "u.getAll()"); michael@0: is(uu.getAll('foo').length, 1, "uu.getAll()"); michael@0: michael@0: runTest(); michael@0: } michael@0: michael@0: function testParserURLSearchParams() { michael@0: var checks = [ michael@0: { input: '', data: {} }, michael@0: { input: 'a', data: { 'a' : [''] } }, michael@0: { input: 'a=b', data: { 'a' : ['b'] } }, michael@0: { input: 'a=', data: { 'a' : [''] } }, michael@0: { input: '=b', data: { '' : ['b'] } }, michael@0: { input: '&', data: {} }, michael@0: { input: '&a', data: { 'a' : [''] } }, michael@0: { input: 'a&', data: { 'a' : [''] } }, michael@0: { input: 'a&a', data: { 'a' : ['', ''] } }, michael@0: { input: 'a&b&c', data: { 'a' : [''], 'b' : [''], 'c' : [''] } }, michael@0: { input: 'a=b&c=d', data: { 'a' : ['b'], 'c' : ['d'] } }, michael@0: { input: 'a=b&c=d&', data: { 'a' : ['b'], 'c' : ['d'] } }, michael@0: { input: '&&&a=b&&&&c=d&', data: { 'a' : ['b'], 'c' : ['d'] } }, michael@0: { input: 'a=a&a=b&a=c', data: { 'a' : ['a', 'b', 'c'] } }, michael@0: { input: 'a==a', data: { 'a' : ['=a'] } }, michael@0: { input: 'a=a+b+c+d', data: { 'a' : ['a b c d'] } }, michael@0: { input: '%=a', data: { '%' : ['a'] } }, michael@0: { input: '%a=a', data: { '%a' : ['a'] } }, michael@0: { input: '%a_=a', data: { '%a_' : ['a'] } }, michael@0: { input: '%61=a', data: { 'a' : ['a'] } }, michael@0: { input: '%=a', data: { '%' : ['a'] } }, michael@0: { input: '%a=a', data: { '%a' : ['a'] } }, michael@0: { input: '%a_=a', data: { '%a_' : ['a'] } }, michael@0: { input: '%61=a', data: { 'a' : ['a'] } }, michael@0: { input: '%61+%4d%4D=', data: { 'a MM' : [''] } }, michael@0: ]; michael@0: michael@0: for (var i = 0; i < checks.length; ++i) { michael@0: var u = new URLSearchParams(checks[i].input); michael@0: michael@0: var count = 0; michael@0: for (var key in checks[i].data) { michael@0: ++count; michael@0: ok(u.has(key), "key " + key + " found"); michael@0: michael@0: var all = u.getAll(key); michael@0: is(all.length, checks[i].data[key].length, "same number of elements"); michael@0: michael@0: for (var k = 0; k < all.length; ++k) { michael@0: is(all[k], checks[i].data[key][k], "value matches"); michael@0: } michael@0: } michael@0: } michael@0: michael@0: runTest(); michael@0: } michael@0: michael@0: function testURL() { michael@0: var url = new URL('http://www.example.net?a=b&c=d'); michael@0: ok(url.searchParams, "URL searchParams exists!"); michael@0: ok(url.searchParams.has('a'), "URL.searchParams.has('a')"); michael@0: is(url.searchParams.get('a'), 'b', "URL.searchParams.get('a')"); michael@0: ok(url.searchParams.has('c'), "URL.searchParams.has('c')"); michael@0: is(url.searchParams.get('c'), 'd', "URL.searchParams.get('c')"); michael@0: michael@0: url.searchParams.set('e', 'f'); michael@0: ok(url.href.indexOf('e=f') != 1, 'URL right'); michael@0: michael@0: var u = new URLSearchParams(); michael@0: u.append('foo', 'bar'); michael@0: url.searchParams = u; michael@0: is(url.searchParams, u, "URL.searchParams is the same object"); michael@0: is(url.searchParams.get('foo'), 'bar', "URL.searchParams.get('foo')"); michael@0: is(url.href, 'http://www.example.net/?foo=bar', 'URL right'); michael@0: michael@0: try { michael@0: url.searchParams = null; michael@0: ok(false, "URLSearchParams is not nullable"); michael@0: } catch(e) { michael@0: ok(true, "URLSearchParams is not nullable"); michael@0: } michael@0: michael@0: var url2 = new URL('http://www.example.net?e=f'); michael@0: url.searchParams = url2.searchParams; michael@0: is(url.searchParams, url2.searchParams, "URL.searchParams is not the same object"); michael@0: is(url.searchParams.get('e'), 'f', "URL.searchParams.get('e')"); michael@0: michael@0: url.href = "http://www.example.net?bar=foo"; michael@0: is(url.searchParams.get('bar'), 'foo', "URL.searchParams.get('bar')"); michael@0: michael@0: runTest(); michael@0: } michael@0: michael@0: function testEncoding() { michael@0: var encoding = [ [ '1', '1' ], michael@0: [ 'a b', 'a+b' ], michael@0: [ '<>', '%3C%3E' ], michael@0: [ '\u0541', '%D5%81'] ]; michael@0: michael@0: for (var i = 0; i < encoding.length; ++i) { michael@0: var a = new URLSearchParams(); michael@0: a.set('a', encoding[i][0]); michael@0: michael@0: var url = new URL('http://www.example.net'); michael@0: url.searchParams = a; michael@0: is(url.href, 'http://www.example.net/?a=' + encoding[i][1]); michael@0: michael@0: var url2 = new URL(url.href); michael@0: is(url2.searchParams.get('a'), encoding[i][0], 'a is still there'); michael@0: } michael@0: michael@0: runTest(); michael@0: } michael@0: michael@0: function testMultiURL() { michael@0: var a = new URL('http://www.example.net?a=b&c=d'); michael@0: var b = new URL('http://www.example.net?e=f'); michael@0: ok(a.searchParams.has('a'), "a.searchParams.has('a')"); michael@0: ok(a.searchParams.has('c'), "a.searchParams.has('c')"); michael@0: ok(b.searchParams.has('e'), "b.searchParams.has('e')"); michael@0: michael@0: var u = new URLSearchParams(); michael@0: a.searchParams = b.searchParams = u; michael@0: is(a.searchParams, u, "a.searchParams === u"); michael@0: is(b.searchParams, u, "b.searchParams === u"); michael@0: ok(!a.searchParams.has('a'), "!a.searchParams.has('a')"); michael@0: ok(!a.searchParams.has('c'), "!a.searchParams.has('c')"); michael@0: ok(!b.searchParams.has('e'), "!b.searchParams.has('e')"); michael@0: michael@0: u.append('foo', 'bar'); michael@0: is(a.searchParams.get('foo'), 'bar', "a has foo=bar"); michael@0: is(b.searchParams.get('foo'), 'bar', "b has foo=bar"); michael@0: is(a + "", b + "", "stringify a == b"); michael@0: michael@0: a.search = "?bar=foo"; michael@0: is(a.searchParams.get('bar'), 'foo', "a has bar=foo"); michael@0: is(b.searchParams.get('bar'), 'foo', "b has bar=foo"); michael@0: michael@0: runTest(); michael@0: } michael@0: var tests = [ michael@0: testSimpleURLSearchParams, michael@0: testCopyURLSearchParams, michael@0: testParserURLSearchParams, michael@0: testURL, michael@0: testEncoding, michael@0: testMultiURL michael@0: ]; michael@0: michael@0: function runTest() { michael@0: if (!tests.length) { michael@0: postMessage({type: 'finish' }); michael@0: return; michael@0: } michael@0: michael@0: var test = tests.shift(); michael@0: test(); michael@0: } michael@0: michael@0: runTest(); michael@0: }