summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/url/url-setters-stripping.any.js
blob: ac90cc17e0bfd5d26adb329ce1a506d14cbb3698 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
function urlString({ scheme = "https",
                     username = "username",
                     password = "password",
                     host = "host",
                     port = "8000",
                     pathname = "path",
                     search = "query",
                     hash = "fragment" }) {
  return `${scheme}://${username}:${password}@${host}:${port}/${pathname}?${search}#${hash}`;
}

function urlRecord(scheme) {
  return new URL(urlString({ scheme }));
}

for(const scheme of ["https", "wpt++"]) {
  for(let i = 0; i < 0x20; i++) {
    const stripped = i === 0x09 || i === 0x0A || i === 0x0D;

    // It turns out that user agents are surprisingly similar for these ranges so generate fewer
    // tests. If this is changed also change the logic for host below.
    if (i !== 0 && i !== 0x1F && !stripped) {
      continue;
    }

    const cpString = String.fromCodePoint(i);
    const cpReference = "U+" + i.toString(16).toUpperCase().padStart(4, "0");

    test(() => {
      const expected = scheme === "https" ? (stripped ? "http" : "https") : (stripped ? "wpt--" : "wpt++");
      const url = urlRecord(scheme);
      url.protocol = String.fromCodePoint(i) + (scheme === "https" ? "http" : "wpt--");
      assert_equals(url.protocol, expected + ":", "property");
      assert_equals(url.href, urlString({ scheme: expected }), "href");
    }, `Setting protocol with leading ${cpReference} (${scheme}:)`);

    test(() => {
      const expected = scheme === "https" ? (stripped ? "http" : "https") : (stripped ? "wpt--" : "wpt++");
      const url = urlRecord(scheme);
      url.protocol = (scheme === "https" ? "http" : "wpt--") + String.fromCodePoint(i);
      assert_equals(url.protocol, expected + ":", "property");
      assert_equals(url.href, urlString({ scheme: expected }), "href");
    }, `Setting protocol with ${cpReference} before inserted colon (${scheme}:)`);

    // Cannot test protocol with trailing as the algorithm inserts a colon before proceeding

    // These do no stripping
    for (const property of ["username", "password"]) {
      for (const [type, expected, input] of [
        ["leading", encodeURIComponent(cpString) + "test", String.fromCodePoint(i) + "test"],
        ["middle", "te" + encodeURIComponent(cpString) + "st", "te" + String.fromCodePoint(i) + "st"],
        ["trailing", "test" + encodeURIComponent(cpString), "test" + String.fromCodePoint(i)]
      ]) {
        test(() => {
          const url = urlRecord(scheme);
          url[property] = input;
          assert_equals(url[property], expected, "property");
          assert_equals(url.href, urlString({ scheme, [property]: expected }), "href");
        }, `Setting ${property} with ${type} ${cpReference} (${scheme}:)`);
      }
    }

    for (const [type, expectedPart, input] of [
      ["leading", (scheme === "https" ? cpString : encodeURIComponent(cpString)) + "test", String.fromCodePoint(i) + "test"],
      ["middle", "te" + (scheme === "https" ? cpString : encodeURIComponent(cpString)) + "st", "te" + String.fromCodePoint(i) + "st"],
      ["trailing", "test" + (scheme === "https" ? cpString : encodeURIComponent(cpString)), "test" + String.fromCodePoint(i)]
    ]) {
      test(() => {
        const expected = i === 0x00 || (scheme === "https" && i === 0x1F) ? "host" : stripped ? "test" : expectedPart;
        const url = urlRecord(scheme);
        url.host = input;
        assert_equals(url.host, expected + ":8000", "property");
        assert_equals(url.href, urlString({ scheme, host: expected }), "href");
      }, `Setting host with ${type} ${cpReference} (${scheme}:)`);

      test(() => {
        const expected = i === 0x00 || (scheme === "https" && i === 0x1F) ? "host" : stripped ? "test" : expectedPart;
        const url = urlRecord(scheme);
        url.hostname = input;
        assert_equals(url.hostname, expected, "property");
        assert_equals(url.href, urlString({ scheme, host: expected }), "href");
      }, `Setting hostname with ${type} ${cpReference} (${scheme}:)`);
    }

    test(() => {
      const expected = stripped ? "9000" : "8000";
      const url = urlRecord(scheme);
      url.port = String.fromCodePoint(i) + "9000";
      assert_equals(url.port, expected, "property");
      assert_equals(url.href, urlString({ scheme, port: expected }), "href");
    }, `Setting port with leading ${cpReference} (${scheme}:)`);

    test(() => {
      const expected = stripped ? "9000" : "90";
      const url = urlRecord(scheme);
      url.port = "90" + String.fromCodePoint(i) + "00";
      assert_equals(url.port, expected, "property");
      assert_equals(url.href, urlString({ scheme, port: expected }), "href");
    }, `Setting port with middle ${cpReference} (${scheme}:)`);

    test(() => {
      const expected = "9000";
      const url = urlRecord(scheme);
      url.port = "9000" + String.fromCodePoint(i);
      assert_equals(url.port, expected, "property");
      assert_equals(url.href, urlString({ scheme, port: expected }), "href");
    }, `Setting port with trailing ${cpReference} (${scheme}:)`);

    for (const [property, separator] of [["pathname", "/"], ["search", "?"], ["hash", "#"]]) {
      for (const [type, expectedPart, input] of [
        ["leading", encodeURIComponent(cpString) + "test", String.fromCodePoint(i) + "test"],
        ["middle", "te" + encodeURIComponent(cpString) + "st", "te" + String.fromCodePoint(i) + "st"],
        ["trailing", "test" + encodeURIComponent(cpString), "test" + String.fromCodePoint(i)]
      ]) {
        test(() => {
          const expected = stripped ? "test" : expectedPart;
          const url = urlRecord(scheme);
          url[property] = input;
          assert_equals(url[property], separator + expected, "property");
          assert_equals(url.href, urlString({ scheme, [property]: expected }), "href");
        }, `Setting ${property} with ${type} ${cpReference} (${scheme}:)`);
      }
    }
  }
}