summaryrefslogtreecommitdiffstats
path: root/docshell/test/unit/test_URIFixup_forced.js
blob: 1d4dfd94a71c7b9c3b775692faa9ed309116841f (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

var data = [
  {
    // singleword.
    wrong: "http://example/",
    fixed: "https://www.example.com/",
  },
  {
    // upgrade protocol.
    wrong: "http://www.example.com/",
    fixed: "https://www.example.com/",
    noAlternateURI: true,
  },
  {
    // no difference.
    wrong: "https://www.example.com/",
    fixed: "https://www.example.com/",
    noProtocolFixup: true,
    noAlternateURI: true,
  },
  {
    // don't add prefix when there's more than one dot.
    wrong: "http://www.example.abc.def/",
    fixed: "https://www.example.abc.def/",
    noAlternateURI: true,
  },
  {
    // http -> https.
    wrong: "http://www.example/",
    fixed: "https://www.example/",
    noAlternateURI: true,
  },
  {
    // domain.com -> https://www.domain.com.
    wrong: "http://example.com/",
    fixed: "https://www.example.com/",
  },
  {
    // example/example... -> https://www.example.com/example/
    wrong: "http://example/example/",
    fixed: "https://www.example.com/example/",
  },
  {
    // example/example/s#q -> www.example.com/example/s#q.
    wrong: "http://example/example/s#q",
    fixed: "https://www.example.com/example/s#q",
  },
  {
    wrong: "http://モジラ.org",
    fixed: "https://www.xn--yck6dwa.org/",
  },
  {
    wrong: "http://モジラ",
    fixed: "https://www.xn--yck6dwa.com/",
  },
  {
    wrong: "http://xn--yck6dwa",
    fixed: "https://www.xn--yck6dwa.com/",
  },
  {
    wrong: "https://モジラ.org",
    fixed: "https://www.xn--yck6dwa.org/",
    noProtocolFixup: true,
  },
  {
    wrong: "https://モジラ",
    fixed: "https://www.xn--yck6dwa.com/",
    noProtocolFixup: true,
  },
  {
    wrong: "https://xn--yck6dwa",
    fixed: "https://www.xn--yck6dwa.com/",
    noProtocolFixup: true,
  },
  {
    // Host is https -> fixup typos of protocol
    wrong: "htp://https://mozilla.org",
    fixed: "http://https//mozilla.org",
    noAlternateURI: true,
  },
  {
    // Host is http -> fixup typos of protocol
    wrong: "ttp://http://mozilla.org",
    fixed: "http://http//mozilla.org",
    noAlternateURI: true,
  },
  {
    // Host is localhost -> fixup typos of protocol
    wrong: "htps://localhost://mozilla.org",
    fixed: "https://localhost//mozilla.org",
    noAlternateURI: true,
  },
  {
    // view-source is not http/https -> error
    wrong: "view-source:http://example/example/example/example",
    reject: true,
    comment: "Scheme should be either http or https",
  },
  {
    // file is not http/https -> error
    wrong: "file://http://example/example/example/example",
    reject: true,
    comment: "Scheme should be either http or https",
  },
  {
    // Protocol is missing -> error
    wrong: "example.com",
    reject: true,
    comment: "Scheme should be either http or https",
  },
  {
    // Empty input -> error
    wrong: "",
    reject: true,
    comment: "Should pass a non-null uri",
  },
];

add_task(async function setup() {
  Services.prefs.setStringPref("browser.fixup.alternate.prefix", "www.");
  Services.prefs.setStringPref("browser.fixup.alternate.suffix", ".com");
  Services.prefs.setStringPref("browser.fixup.alternate.protocol", "https");
  registerCleanupFunction(function () {
    Services.prefs.clearUserPref("browser.fixup.alternate.prefix");
    Services.prefs.clearUserPref("browser.fixup.alternate.suffix");
    Services.prefs.clearUserPref("browser.fixup.alternate.protocol");
  });
});

add_task(function test_default_https_pref() {
  for (let item of data) {
    if (item.reject) {
      Assert.throws(
        () => Services.uriFixup.forceHttpFixup(item.wrong),
        /NS_ERROR_FAILURE/,
        item.comment
      );
    } else {
      let { fixupChangedProtocol, fixupCreatedAlternateURI, fixedURI } =
        Services.uriFixup.forceHttpFixup(item.wrong);
      Assert.equal(fixedURI.spec, item.fixed, "Specs should be the same");
      Assert.equal(
        fixupChangedProtocol,
        !item.noProtocolFixup,
        `fixupChangedProtocol should be ${!item.noAlternateURI}`
      );
      Assert.equal(
        fixupCreatedAlternateURI,
        !item.noAlternateURI,
        `fixupCreatedAlternateURI should be ${!item.limitedFixup}`
      );
    }
  }
});