summaryrefslogtreecommitdiffstats
path: root/docshell/test/unit/test_URIFixup.js
blob: 794ee8cfa240e2e393f0c5033ef7a947f807dd54 (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
160
161
162
163
164
165
166
167
168
169
var pref = "browser.fixup.typo.scheme";

var data = [
  {
    // ttp -> http.
    wrong: "ttp://www.example.com/",
    fixed: "http://www.example.com/",
  },
  {
    // htp -> http.
    wrong: "htp://www.example.com/",
    fixed: "http://www.example.com/",
  },
  {
    // ttps -> https.
    wrong: "ttps://www.example.com/",
    fixed: "https://www.example.com/",
  },
  {
    // tps -> https.
    wrong: "tps://www.example.com/",
    fixed: "https://www.example.com/",
  },
  {
    // ps -> https.
    wrong: "ps://www.example.com/",
    fixed: "https://www.example.com/",
  },
  {
    // htps -> https.
    wrong: "htps://www.example.com/",
    fixed: "https://www.example.com/",
  },
  {
    // ile -> file.
    wrong: "ile:///this/is/a/test.html",
    fixed: "file:///this/is/a/test.html",
  },
  {
    // le -> file.
    wrong: "le:///this/is/a/test.html",
    fixed: "file:///this/is/a/test.html",
  },
  {
    // Replace ';' with ':'.
    wrong: "http;//www.example.com/",
    fixed: "http://www.example.com/",
    noPrefValue: "http://http;//www.example.com/",
  },
  {
    // Missing ':'.
    wrong: "https//www.example.com/",
    fixed: "https://www.example.com/",
    noPrefValue: "http://https//www.example.com/",
  },
  {
    // Missing ':' for file scheme.
    wrong: "file///this/is/a/test.html",
    fixed: "file:///this/is/a/test.html",
    noPrefValue: "http://file///this/is/a/test.html",
  },
  {
    // Valid should not be changed.
    wrong: "https://example.com/this/is/a/test.html",
    fixed: "https://example.com/this/is/a/test.html",
  },
  {
    // Unmatched should not be changed.
    wrong: "whatever://this/is/a/test.html",
    fixed: "whatever://this/is/a/test.html",
  },
  {
    // Spaces before @ are valid if it appears after the domain.
    wrong: "example.com/ @test.com",
    fixed: "http://example.com/%20@test.com",
    noPrefValue: "http://example.com/%20@test.com",
  },
];

var dontFixURIs = [
  {
    input: " leadingSpaceUsername@example.com/",
    testInfo: "dont fix usernames with leading space",
  },
  {
    input: "trailingSpacerUsername @example.com/",
    testInfo: "dont fix usernames with trailing space",
  },
  {
    input: "multiple words username@example.com/",
    testInfo: "dont fix usernames with multiple spaces",
  },
  {
    input: "one spaceTwo  SpacesThree   Spaces@example.com/",
    testInfo: "dont match multiple consecutive spaces",
  },
  {
    input: " dontMatchCredentialsWithSpaces: secret password @example.com/",
    testInfo: "dont fix credentials with spaces",
  },
];

var len = data.length;

add_task(async function setup() {
  await setupSearchService();
  // Now we've initialised the search service, we force remove the engines
  // it has, so they don't interfere with this test.
  // Search engine integration is tested in test_URIFixup_search.js.
  Services.search.wrappedJSObject._engines.clear();
});

// Make sure we fix what needs fixing when there is no pref set.
add_task(function test_unset_pref_fixes_typos() {
  Services.prefs.clearUserPref(pref);
  for (let i = 0; i < len; ++i) {
    let item = data[i];
    let { preferredURI } = Services.uriFixup.getFixupURIInfo(
      item.wrong,
      Services.uriFixup.FIXUP_FLAG_FIX_SCHEME_TYPOS
    );
    Assert.equal(preferredURI.spec, item.fixed);
  }
});

// Make sure we don't do anything when the pref is explicitly
// set to false.
add_task(function test_false_pref_keeps_typos() {
  Services.prefs.setBoolPref(pref, false);
  for (let i = 0; i < len; ++i) {
    let item = data[i];
    let { preferredURI } = Services.uriFixup.getFixupURIInfo(
      item.wrong,
      Services.uriFixup.FIXUP_FLAG_FIX_SCHEME_TYPOS
    );
    Assert.equal(preferredURI.spec, item.noPrefValue || item.wrong);
  }
});

// Finally, make sure we still fix what needs fixing if the pref is
// explicitly set to true.
add_task(function test_true_pref_fixes_typos() {
  Services.prefs.setBoolPref(pref, true);
  for (let i = 0; i < len; ++i) {
    let item = data[i];
    let { preferredURI } = Services.uriFixup.getFixupURIInfo(
      item.wrong,
      Services.uriFixup.FIXUP_FLAG_FIX_SCHEME_TYPOS
    );
    Assert.equal(preferredURI.spec, item.fixed);
  }
});

add_task(function test_dont_fix_uris() {
  let dontFixLength = dontFixURIs.length;
  for (let i = 0; i < dontFixLength; i++) {
    let testCase = dontFixURIs[i];
    Assert.throws(
      () => {
        Services.uriFixup.getFixupURIInfo(
          testCase.input,
          Services.uriFixup.FIXUP_FLAG_FIX_SCHEME_TYPOS
        );
      },
      /NS_ERROR_MALFORMED_URI/,
      testCase.testInfo
    );
  }
});