Bug 1931425 - Limit how often moz-label's #setStyles runs r=reusable-components-revie...
[gecko.git] / netwerk / test / unit / test_brotli_http.js
blobadf5915b76b464d3df5dd301399ee79f4e6a2c7e
1 // This test exists mostly as documentation that
2 // Firefox can load brotli files over HTTP if we set the proper pref.
4 "use strict";
6 function contentHandler(metadata, response) {
7 response.setHeader("Content-Type", "text/plain", false);
8 response.setHeader("Content-Encoding", "br", false);
9 response.write("\x0b\x02\x80hello\x03");
12 const { HttpServer } = ChromeUtils.importESModule(
13 "resource://testing-common/httpd.sys.mjs"
16 ChromeUtils.defineLazyGetter(this, "URL", function () {
17 return "http://localhost:" + httpServer.identity.primaryPort + "/content";
18 });
20 var httpServer = null;
22 add_task(async function check_brotli() {
23 httpServer = new HttpServer();
24 httpServer.registerPathHandler("/content", contentHandler);
25 httpServer.start(-1);
27 async function test() {
28 let chan = NetUtil.newChannel({ uri: URL, loadUsingSystemPrincipal: true });
29 let [, buff] = await new Promise(resolve => {
30 chan.asyncOpen(
31 new ChannelListener(
32 (req, buff1) => {
33 resolve([req, buff1]);
35 null,
36 CL_IGNORE_CL
39 });
40 return buff;
43 Services.prefs.setBoolPref(
44 "network.http.encoding.trustworthy_is_https",
45 true
47 equal(
48 await test(),
49 "hello",
50 "Should decode brotli when trustworthy_is_https=true"
52 Services.prefs.setBoolPref(
53 "network.http.encoding.trustworthy_is_https",
54 false
56 equal(
57 await test(),
58 "\x0b\x02\x80hello\x03",
59 "Should not decode brotli when trustworthy_is_https=false"
61 Services.prefs.setCharPref(
62 "network.http.accept-encoding",
63 "gzip, deflate, br"
65 equal(
66 await test(),
67 "hello",
68 "Should decode brotli if we set the HTTP accept encoding to include brotli"
70 Services.prefs.clearUserPref("network.http.accept-encoding");
71 Services.prefs.clearUserPref("network.http.encoding.trustworthy_is_https");
72 await httpServer.stop();
73 });
75 // Make sure we still decode brotli on HTTPS
76 // Node server doesn't work on Android yet.
77 add_task(
78 { skip_if: () => AppConstants.platform == "android" },
79 async function check_https() {
80 Services.prefs.setBoolPref(
81 "network.http.encoding.trustworthy_is_https",
82 true
84 let certdb = Cc["@mozilla.org/security/x509certdb;1"].getService(
85 Ci.nsIX509CertDB
87 addCertFromFile(certdb, "http2-ca.pem", "CTu,u,u");
89 let server = new NodeHTTPSServer();
90 await server.start();
91 registerCleanupFunction(async () => {
92 await server.stop();
93 });
94 await server.registerPathHandler("/brotli", (req, resp) => {
95 resp.setHeader("Content-Type", "text/plain");
96 resp.setHeader("Content-Encoding", "br");
97 let output = "\x0b\x02\x80hello\x03";
98 resp.writeHead(200);
99 resp.end(output, "binary");
101 equal(
102 Services.prefs.getCharPref("network.http.accept-encoding.secure"),
103 "gzip, deflate, br, zstd"
105 let { req, buff } = await new Promise(resolve => {
106 let chan = NetUtil.newChannel({
107 uri: `${server.origin()}/brotli`,
108 loadUsingSystemPrincipal: true,
110 chan.asyncOpen(
111 new ChannelListener(
112 (req1, buff1) => resolve({ req: req1, buff: buff1 }),
113 null,
114 CL_ALLOW_UNKNOWN_CL
118 equal(req.status, Cr.NS_OK);
119 equal(req.QueryInterface(Ci.nsIHttpChannel).responseStatus, 200);
120 equal(buff, "hello");