summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/extensions/test/xpcshell/test_AddonRepository_langpacks.js
blob: e84ce4ea30c36102f7ee2ba1b0c77e4062939058 (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
const PREF_GET_LANGPACKS = "extensions.getAddons.langpacks.url";

let server = AddonTestUtils.createHttpServer({ hosts: ["example.com"] });
Services.prefs.setStringPref(
  PREF_GET_LANGPACKS,
  "http://example.com/langpacks.json"
);

add_task(async function test_getlangpacks() {
  function setData(data) {
    if (typeof data != "string") {
      data = JSON.stringify(data);
    }

    server.registerPathHandler("/langpacks.json", (request, response) => {
      response.setHeader("content-type", "application/json");
      response.write(data);
    });
  }

  const EXPECTED = [
    {
      target_locale: "kl",
      url: "http://example.com/langpack1.xpi",
      hash: "sha256:0123456789abcdef",
    },
    {
      target_locale: "fo",
      url: "http://example.com/langpack2.xpi",
      hash: "sha256:fedcba9876543210",
    },
  ];

  setData({
    results: [
      // A simple entry
      {
        target_locale: EXPECTED[0].target_locale,
        current_compatible_version: {
          files: [
            {
              platform: "all",
              url: EXPECTED[0].url,
              hash: EXPECTED[0].hash,
            },
          ],
        },
      },

      // An entry with multiple supported platforms
      {
        target_locale: EXPECTED[1].target_locale,
        current_compatible_version: {
          files: [
            {
              platform: "somethingelse",
              url: "http://example.com/bogus.xpi",
              hash: "sha256:abcd",
            },
            {
              platform: Services.appinfo.OS.toLowerCase(),
              url: EXPECTED[1].url,
              hash: EXPECTED[1].hash,
            },
          ],
        },
      },

      // An entry with no matching platform
      {
        target_locale: "bla",
        current_compatible_version: {
          files: [
            {
              platform: "unsupportedplatform",
              url: "http://example.com/bogus2.xpi",
              hash: "sha256:1234",
            },
          ],
        },
      },
    ],
  });

  let result = await AddonRepository.getAvailableLangpacks();
  equal(result.length, 2, "Got 2 results");

  deepEqual(result[0], EXPECTED[0], "Got expected result for simple entry");
  deepEqual(
    result[1],
    EXPECTED[1],
    "Got expected result for multi-platform entry"
  );

  setData("not valid json");
  await Assert.rejects(
    AddonRepository.getAvailableLangpacks(),
    /SyntaxError/,
    "Got parse error on invalid JSON"
  );
});

// Tests that cookies are not sent with langpack requests.
add_task(async function test_cookies() {
  let lastRequest = null;
  server.registerPathHandler("/langpacks.json", (request, response) => {
    lastRequest = request;
    response.write(JSON.stringify({ results: [] }));
  });

  const COOKIE = "test";
  let expiration = Date.now() / 1000 + 60 * 60;
  Services.cookies.add(
    "example.com",
    "/",
    COOKIE,
    "testing",
    false,
    false,
    false,
    expiration,
    {},
    Ci.nsICookie.SAMESITE_NONE,
    Ci.nsICookie.SCHEME_HTTP
  );

  await AddonRepository.getAvailableLangpacks();

  notEqual(lastRequest, null, "Received langpack request");
  equal(
    lastRequest.hasHeader("Cookie"),
    false,
    "Langpack request has no cookies"
  );
});