summaryrefslogtreecommitdiffstats
path: root/toolkit/modules/tests/xpcshell/test_BrowserUtils_urlFormatting.js
blob: 53fe88397e39bc318e9f5a3b722dbba897b5fb64 (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const { FileUtils } = ChromeUtils.importESModule(
  "resource://gre/modules/FileUtils.sys.mjs"
);

let tempFile = new FileUtils.File(PathUtils.tempDir);
const TEST_LOCAL_FILE_NAME = "hello.txt";
tempFile.append(TEST_LOCAL_FILE_NAME);

const DATA_URL_EXPECTED_STRING = new Localization(
  ["toolkit/global/browser-utils.ftl"],
  true
).formatValueSync("browser-utils-url-data");

// http/https tests first. These are split out from TESTS for the benefit of reuse by the
// blob: tests further down.
const HTTP_TESTS = [
  // Simple http/https examples with/without `www.`.
  {
    input: "https://example.com",
    output: "example.com",
  },
  {
    input: "https://example.com/path/?query#hash",
    output: "example.com",
  },
  {
    input: "https://www.example.com",
    output: "example.com",
  },
  {
    input: "https://www.example.com/path/?query#hash",
    output: "example.com",
  },
  {
    input: "http://example.com",
    output: "example.com",
  },
  {
    input: "http://www.example.com",
    output: "example.com",
  },

  // We shouldn't drop `www.` if that's the domain:
  {
    input: "https://www.com",
    output: "www.com",
  },

  // Multilevel TLDs should work:
  {
    input: "https://www.example.co.uk",
    output: "example.co.uk",
  },
  {
    input: "https://www.co.uk",
    output: "www.co.uk",
  },

  // Other sudomains should be kept:
  {
    input: "https://webmail.example.co.uk",
    output: "webmail.example.co.uk",
  },
  {
    input: "https://webmail.example.com",
    output: "webmail.example.com",
  },

  // IP addresses should work:
  {
    input: "http://[::1]/foo/bar?baz=bax#quux",
    output: "[::1]",
  },
  {
    input: "http://127.0.0.1/foo/bar?baz=bax#quux",
    output: "127.0.0.1",
  },
];

const TESTS = [
  ...HTTP_TESTS,

  // about URIs:
  {
    input: "about:config",
    output: "about:config",
  },
  {
    input: "about:config?foo#bar",
    output: "about:config",
  },

  // file URI directory:
  {
    input: Services.io.newFileURI(new FileUtils.File(PathUtils.tempDir)).spec,
    output: PathUtils.filename(PathUtils.tempDir),
  },
  // file URI directory that ends in slash:
  {
    input:
      Services.io.newFileURI(new FileUtils.File(PathUtils.tempDir)).spec + "/",
    output: PathUtils.filename(PathUtils.tempDir),
  },
  // file URI individual file:
  {
    input: Services.io.newFileURI(tempFile).spec,
    output: tempFile.leafName,
  },

  // As above but for chrome URIs:
  {
    input: "chrome://global/content/blah",
    output: "blah",
  },
  {
    input: "chrome://global/content/blah//",
    output: "blah",
  },
  {
    input: "chrome://global/content/foo.txt",
    output: "foo.txt",
  },

  // Also check data URIs:
  {
    input: "data:text/html,42",
    output: DATA_URL_EXPECTED_STRING,
  },
];

const { BrowserUtils } = ChromeUtils.importESModule(
  "resource://gre/modules/BrowserUtils.sys.mjs"
);

add_task(async function test_checkStringFormatting() {
  for (let { input, output } of TESTS) {
    Assert.equal(
      BrowserUtils.formatURIStringForDisplay(input),
      output,
      `String ${input} formatted for output should match`
    );
  }
});

add_task(async function test_checkURIFormatting() {
  for (let { input, output } of TESTS) {
    let uri = Services.io.newURI(input);
    Assert.equal(
      BrowserUtils.formatURIForDisplay(uri),
      output,
      `URI ${input} formatted for output should match`
    );
  }
});

add_task(async function test_checkViewSourceFormatting() {
  for (let { input, output } of HTTP_TESTS) {
    Assert.equal(
      BrowserUtils.formatURIStringForDisplay("view-source:" + input),
      output,
      `String view-source:${input} formatted for output should match`
    );
    let uri = Services.io.newURI("view-source:" + input);
    Assert.equal(
      BrowserUtils.formatURIForDisplay(uri),
      output,
      `URI view-source:${input} formatted for output should match`
    );
  }
});

function createBlobURLWithSandbox(origin) {
  let sb = new Cu.Sandbox(origin, { wantGlobalProperties: ["Blob", "URL"] });
  // Need to pass 'false' for the validate filename param or this throws
  // exceptions. I'm not sure why...
  return Cu.evalInSandbox(
    'URL.createObjectURL(new Blob(["text"], { type: "text/plain" }))',
    sb,
    "",
    null,
    0,
    false
  );
}

add_task(async function test_checkBlobURIs() {
  // These don't just live in the TESTS array because creating a valid
  // blob URI is a bit more involved...
  let blob = new Blob(["test"], { type: "text/plain" });
  let url = URL.createObjectURL(blob);
  Assert.equal(
    BrowserUtils.formatURIStringForDisplay(url),
    DATA_URL_EXPECTED_STRING,
    `Blob url string without origin should be represented as (data)`
  );

  // Now with a null principal:
  url = createBlobURLWithSandbox(null);
  Assert.equal(
    BrowserUtils.formatURIStringForDisplay(url),
    DATA_URL_EXPECTED_STRING,
    `Blob url string with null principal origin should be represented as (data)`
  );

  // And some http url principals:
  for (let { input, output } of HTTP_TESTS) {
    url = createBlobURLWithSandbox(input);
    Assert.equal(
      BrowserUtils.formatURIStringForDisplay(url),
      output,
      `Blob url string with principal from ${input} should show principal URI`
    );
  }
});