• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3if (!common.hasIntl) {
4  // A handful of the tests fail when ICU is not included.
5  common.skip('missing Intl');
6}
7
8const fixtures = require('../common/fixtures');
9const { test, assert_equals, assert_true, assert_throws } =
10  require('../common/wpt').harness;
11
12const request = {
13  response: require(
14    fixtures.path('wpt', 'url', 'resources', 'urltestdata.json')
15  )
16};
17
18// The following tests are copied from WPT. Modifications to them should be
19// upstreamed first.
20// Refs: https://github.com/w3c/web-platform-tests/blob/8791bed/url/url-constructor.html
21// License: http://www.w3.org/Consortium/Legal/2008/04-testsuite-copyright.html
22
23/* eslint-disable */
24function runURLConstructorTests() {
25  // var setup = async_test("Loading data…")
26  // setup.step(function() {
27  //   var request = new XMLHttpRequest()
28  //   request.open("GET", "urltestdata.json")
29  //   request.send()
30  //   request.responseType = "json"
31  //   request.onload = setup.step_func(function() {
32         runURLTests(request.response)
33  //     setup.done()
34  //   })
35  // })
36}
37
38function bURL(url, base) {
39  return new URL(url, base || "about:blank")
40}
41
42
43function runURLTests(urltests) {
44  for(var i = 0, l = urltests.length; i < l; i++) {
45    var expected = urltests[i]
46    if (typeof expected === "string") continue // skip comments
47
48    test(function() {
49      if (expected.failure) {
50        assert_throws(new TypeError(), function() {
51          bURL(expected.input, expected.base)
52        })
53        return
54      }
55
56      var url = bURL(expected.input, expected.base)
57      assert_equals(url.href, expected.href, "href")
58      assert_equals(url.protocol, expected.protocol, "protocol")
59      assert_equals(url.username, expected.username, "username")
60      assert_equals(url.password, expected.password, "password")
61      assert_equals(url.host, expected.host, "host")
62      assert_equals(url.hostname, expected.hostname, "hostname")
63      assert_equals(url.port, expected.port, "port")
64      assert_equals(url.pathname, expected.pathname, "pathname")
65      assert_equals(url.search, expected.search, "search")
66      if ("searchParams" in expected) {
67        assert_true("searchParams" in url)
68        assert_equals(url.searchParams.toString(), expected.searchParams, "searchParams")
69      }
70      assert_equals(url.hash, expected.hash, "hash")
71    }, "Parsing: <" + expected.input + "> against <" + expected.base + ">")
72  }
73}
74
75function runURLSearchParamTests() {
76  test(function() {
77    var url = bURL('http://example.org/?a=b')
78    assert_true("searchParams" in url)
79    var searchParams = url.searchParams
80    assert_true(url.searchParams === searchParams, 'Object identity should hold.')
81  }, 'URL.searchParams getter')
82
83  test(function() {
84    var url = bURL('http://example.org/?a=b')
85    assert_true("searchParams" in url)
86    var searchParams = url.searchParams
87    assert_equals(searchParams.toString(), 'a=b')
88
89    searchParams.set('a', 'b')
90    assert_equals(url.searchParams.toString(), 'a=b')
91    assert_equals(url.search, '?a=b')
92    url.search = ''
93    assert_equals(url.searchParams.toString(), '')
94    assert_equals(url.search, '')
95    assert_equals(searchParams.toString(), '')
96  }, 'URL.searchParams updating, clearing')
97
98  test(function() {
99    'use strict'
100    var urlString = 'http://example.org'
101    var url = bURL(urlString)
102    assert_throws(TypeError(), function() { url.searchParams = new URLSearchParams(urlString) })
103  }, 'URL.searchParams setter, invalid values')
104
105  test(function() {
106    var url = bURL('http://example.org/file?a=b&c=d')
107    assert_true("searchParams" in url)
108    var searchParams = url.searchParams
109    assert_equals(url.search, '?a=b&c=d')
110    assert_equals(searchParams.toString(), 'a=b&c=d')
111
112    // Test that setting 'search' propagates to the URL object's query object.
113    url.search = 'e=f&g=h'
114    assert_equals(url.search, '?e=f&g=h')
115    assert_equals(searchParams.toString(), 'e=f&g=h')
116
117    // ..and same but with a leading '?'.
118    url.search = '?e=f&g=h'
119    assert_equals(url.search, '?e=f&g=h')
120    assert_equals(searchParams.toString(), 'e=f&g=h')
121
122    // And in the other direction, altering searchParams propagates
123    // back to 'search'.
124    searchParams.append('i', ' j ')
125    assert_equals(url.search, '?e=f&g=h&i=+j+')
126    assert_equals(url.searchParams.toString(), 'e=f&g=h&i=+j+')
127    assert_equals(searchParams.get('i'), ' j ')
128
129    searchParams.set('e', 'updated')
130    assert_equals(url.search, '?e=updated&g=h&i=+j+')
131    assert_equals(searchParams.get('e'), 'updated')
132
133    var url2 = bURL('http://example.org/file??a=b&c=d')
134    assert_equals(url2.search, '??a=b&c=d')
135    assert_equals(url2.searchParams.toString(), '%3Fa=b&c=d')
136
137    url2.href = 'http://example.org/file??a=b'
138    assert_equals(url2.search, '??a=b')
139    assert_equals(url2.searchParams.toString(), '%3Fa=b')
140  }, 'URL.searchParams and URL.search setters, update propagation')
141}
142runURLSearchParamTests()
143runURLConstructorTests()
144/* eslint-enable */
145