summaryrefslogtreecommitdiffstats
path: root/intl/uconv/tests/unit/test_unEscapeNonAsciiURI.js
blob: f4479592444de601f61f392a05d7d41834b62c90 (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
// Tests for nsITextToSubURI.unEscapeNonAsciiURI
function run_test() {
  // Tests whether nsTextToSubURI does UTF-16 unescaping (it shouldn't)
  const testURI = "data:text/html,%FE%FF";
  Assert.equal(
    Services.textToSubURI.unEscapeNonAsciiURI("UTF-16", testURI),
    testURI
  );

  // Tests whether incomplete multibyte sequences throw.
  const tests = [
    {
      input: "http://example.com/?p=%E9",
      throws: Cr.NS_ERROR_ILLEGAL_INPUT,
    },
    {
      input: "http://example.com/?p=%E9%80",
      throws: Cr.NS_ERROR_ILLEGAL_INPUT,
    },
    {
      input: "http://example.com/?p=%E9%80%80",
      expected: "http://example.com/?p=\u9000",
    },
    {
      input: "http://example.com/?p=%E9e",
      throws: Cr.NS_ERROR_ILLEGAL_INPUT,
    },
    {
      input: "http://example.com/?p=%E9%E9",
      throws: Cr.NS_ERROR_ILLEGAL_INPUT,
    },
    {
      input: "http://example.com/?name=M%FCller/",
      throws: Cr.NS_ERROR_ILLEGAL_INPUT,
    },
    {
      input: "http://example.com/?name=M%C3%BCller/",
      expected: "http://example.com/?name=Müller/",
    },
  ];

  for (const t of tests) {
    if (t.throws !== undefined) {
      let thrown = undefined;
      try {
        Services.textToSubURI.unEscapeNonAsciiURI("UTF-8", t.input);
      } catch (e) {
        thrown = e.result;
      }
      Assert.equal(thrown, t.throws);
    } else {
      Assert.equal(
        Services.textToSubURI.unEscapeNonAsciiURI("UTF-8", t.input),
        t.expected
      );
    }
  }
}