Ignore URL.origin in URL web-platform-tests
[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       assert_equals(url.protocol, expected.protocol, "protocol")
41       assert_equals(url.username, expected.username, "username")
42       assert_equals(url.password, expected.password, "password")
43       assert_equals(url.host, expected.host, "host")
44       assert_equals(url.hostname, expected.hostname, "hostname")
45       assert_equals(url.port, expected.port, "port")
46       assert_equals(url.pathname, expected.pathname, "pathname")
47       assert_equals(url.search, expected.search, "search")
48       if ("searchParams" in expected) {
49         assert_true("searchParams" in url)
50         assert_equals(url.searchParams.toString(), expected.searchParams, "searchParams")
51       }
52       assert_equals(url.hash, expected.hash, "hash")
53     }, "Parsing: <" + expected.input + "> against <" + expected.base + ">")
54   }
55 }
56
57 function runURLSearchParamTests() {
58   test(function() {
59     var url = bURL('http://example.org/?a=b')
60     assert_true("searchParams" in url)
61     var searchParams = url.searchParams
62     assert_true(url.searchParams === searchParams, 'Object identity should hold.')
63   }, 'URL.searchParams getter')
64
65   test(function() {
66     var url = bURL('http://example.org/?a=b')
67     assert_true("searchParams" in url)
68     var searchParams = url.searchParams
69     assert_equals(searchParams.toString(), 'a=b')
70
71     searchParams.set('a', 'b')
72     assert_equals(url.searchParams.toString(), 'a=b')
73     assert_equals(url.search, '?a=b')
74     url.search = ''
75     assert_equals(url.searchParams.toString(), '')
76     assert_equals(url.search, '')
77     assert_equals(searchParams.toString(), '')
78   }, 'URL.searchParams updating, clearing')
79
80   test(function() {
81     'use strict'
82     var urlString = 'http://example.org'
83     var url = bURL(urlString)
84     assert_throws(TypeError(), function() { url.searchParams = new URLSearchParams(urlString) })
85   }, 'URL.searchParams setter, invalid values')
86
87   test(function() {
88     var url = bURL('http://example.org/file?a=b&c=d')
89     assert_true("searchParams" in url)
90     var searchParams = url.searchParams
91     assert_equals(url.search, '?a=b&c=d')
92     assert_equals(searchParams.toString(), 'a=b&c=d')
93
94     // Test that setting 'search' propagates to the URL object's query object.
95     url.search = 'e=f&g=h'
96     assert_equals(url.search, '?e=f&g=h')
97     assert_equals(searchParams.toString(), 'e=f&g=h')
98
99     // ..and same but with a leading '?'.
100     url.search = '?e=f&g=h'
101     assert_equals(url.search, '?e=f&g=h')
102     assert_equals(searchParams.toString(), 'e=f&g=h')
103
104     // And in the other direction, altering searchParams propagates
105     // back to 'search'.
106     searchParams.append('i', ' j ')
107     assert_equals(url.search, '?e=f&g=h&i=+j+')
108     assert_equals(url.searchParams.toString(), 'e=f&g=h&i=+j+')
109     assert_equals(searchParams.get('i'), ' j ')
110
111     searchParams.set('e', 'updated')
112     assert_equals(url.search, '?e=updated&g=h&i=+j+')
113     assert_equals(searchParams.get('e'), 'updated')
114
115     var url2 = bURL('http://example.org/file??a=b&c=d')
116     assert_equals(url2.search, '??a=b&c=d')
117     assert_equals(url2.searchParams.toString(), '%3Fa=b&c=d')
118
119     url2.href = 'http://example.org/file??a=b'
120     assert_equals(url2.search, '??a=b')
121     assert_equals(url2.searchParams.toString(), '%3Fa=b')
122   }, 'URL.searchParams and URL.search setters, update propagation')
123 }
124 runURLSearchParamTests()
125 runURLConstructorTests()
126 </script>