8eacce6542facc2f98b330f82c4bbd1149aebc41
[WebKit-https.git] / LayoutTests / imported / w3c / web-platform-tests / url / url-constructor.html
1 <!doctype html>
2 <meta charset=utf-8>
3 <script src=/resources/testharness.js></script>
4 <script src=/resources/testharnessreport.js></script>
5 <div id=log></div>
6 <script>
7 function runURLConstructorTests() {
8   var setup = async_test("Loading data‚Ķ")
9   setup.step(function() {
10     var request = new XMLHttpRequest()
11     request.open("GET", "urltestdata.json")
12     request.send()
13     request.responseType = "json"
14     request.onload = setup.step_func(function() {
15       runURLTests(request.response)
16       setup.done()
17     })
18   })
19 }
20
21 function bURL(url, base) {
22   return new URL(url, base || "about:blank")
23 }
24
25 function runURLTests(urltests) {
26   for(var i = 0, l = urltests.length; i < l; i++) {
27     var expected = urltests[i]
28     if (typeof expected === "string") continue // skip comments
29
30     test(function() {
31       if (expected.failure) {
32         assert_throws(new TypeError(), function() {
33           bURL(expected.input, expected.base)
34         })
35         return
36       }
37
38       var url = bURL(expected.input, expected.base)
39       assert_equals(url.href, expected.href, "href")
40       if ("origin" in expected) {
41         assert_equals(url.origin, expected.origin, "origin")
42       }
43       assert_equals(url.protocol, expected.protocol, "protocol")
44       assert_equals(url.username, expected.username, "username")
45       assert_equals(url.password, expected.password, "password")
46       assert_equals(url.host, expected.host, "host")
47       assert_equals(url.hostname, expected.hostname, "hostname")
48       assert_equals(url.port, expected.port, "port")
49       assert_equals(url.pathname, expected.pathname, "pathname")
50       assert_equals(url.search, expected.search, "search")
51       if ("searchParams" in expected) {
52         assert_true("searchParams" in url)
53         assert_equals(url.searchParams.toString(), expected.searchParams, "searchParams")
54       }
55       assert_equals(url.hash, expected.hash, "hash")
56     }, "Parsing: <" + expected.input + "> against <" + expected.base + ">")
57   }
58 }
59
60 function runURLSearchParamTests() {
61   test(function() {
62     var url = bURL('http://example.org/?a=b')
63     assert_true("searchParams" in url)
64     var searchParams = url.searchParams
65     assert_true(url.searchParams === searchParams, 'Object identity should hold.')
66   }, 'URL.searchParams getter')
67
68   test(function() {
69     var url = bURL('http://example.org/?a=b')
70     assert_true("searchParams" in url)
71     var searchParams = url.searchParams
72     assert_equals(searchParams.toString(), 'a=b')
73
74     searchParams.set('a', 'b')
75     assert_equals(url.searchParams.toString(), 'a=b')
76     assert_equals(url.search, '?a=b')
77     url.search = ''
78     assert_equals(url.searchParams.toString(), '')
79     assert_equals(url.search, '')
80     assert_equals(searchParams.toString(), '')
81   }, 'URL.searchParams updating, clearing')
82
83   test(function() {
84     'use strict'
85     var urlString = 'http://example.org'
86     var url = bURL(urlString)
87     assert_throws(TypeError(), function() { url.searchParams = new URLSearchParams(urlString) })
88   }, 'URL.searchParams setter, invalid values')
89
90   test(function() {
91     var url = bURL('http://example.org/file?a=b&c=d')
92     assert_true("searchParams" in url)
93     var searchParams = url.searchParams
94     assert_equals(url.search, '?a=b&c=d')
95     assert_equals(searchParams.toString(), 'a=b&c=d')
96
97     // Test that setting 'search' propagates to the URL object's query object.
98     url.search = 'e=f&g=h'
99     assert_equals(url.search, '?e=f&g=h')
100     assert_equals(searchParams.toString(), 'e=f&g=h')
101
102     // ..and same but with a leading '?'.
103     url.search = '?e=f&g=h'
104     assert_equals(url.search, '?e=f&g=h')
105     assert_equals(searchParams.toString(), 'e=f&g=h')
106
107     // And in the other direction, altering searchParams propagates
108     // back to 'search'.
109     searchParams.append('i', ' j ')
110     assert_equals(url.search, '?e=f&g=h&i=+j+')
111     assert_equals(url.searchParams.toString(), 'e=f&g=h&i=+j+')
112     assert_equals(searchParams.get('i'), ' j ')
113
114     searchParams.set('e', 'updated')
115     assert_equals(url.search, '?e=updated&g=h&i=+j+')
116     assert_equals(searchParams.get('e'), 'updated')
117
118     var url2 = bURL('http://example.org/file??a=b&c=d')
119     assert_equals(url2.search, '??a=b&c=d')
120     assert_equals(url2.searchParams.toString(), '%3Fa=b&c=d')
121
122     url2.href = 'http://example.org/file??a=b'
123     assert_equals(url2.search, '??a=b')
124     assert_equals(url2.searchParams.toString(), '%3Fa=b')
125   }, 'URL.searchParams and URL.search setters, update propagation')
126 }
127 runURLSearchParamTests()
128 runURLConstructorTests()
129 </script>