test_brotli_http.js (3504B)
1 // This test exists mostly as documentation that 2 // Firefox can load brotli files over HTTP if we set the proper pref. 3 4 "use strict"; 5 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"); 10 } 11 12 const { HttpServer } = ChromeUtils.importESModule( 13 "resource://testing-common/httpd.sys.mjs" 14 ); 15 16 const { NodeHTTPSServer } = ChromeUtils.importESModule( 17 "resource://testing-common/NodeServer.sys.mjs" 18 ); 19 20 const { AppConstants } = ChromeUtils.importESModule( 21 "resource://gre/modules/AppConstants.sys.mjs" 22 ); 23 24 ChromeUtils.defineLazyGetter(this, "URL", function () { 25 return "http://localhost:" + httpServer.identity.primaryPort + "/content"; 26 }); 27 28 var httpServer = null; 29 30 add_task(async function check_brotli() { 31 httpServer = new HttpServer(); 32 httpServer.registerPathHandler("/content", contentHandler); 33 httpServer.start(-1); 34 35 async function test() { 36 let chan = NetUtil.newChannel({ uri: URL, loadUsingSystemPrincipal: true }); 37 let [, buff] = await new Promise(resolve => { 38 chan.asyncOpen( 39 new ChannelListener( 40 (req, buff1) => { 41 resolve([req, buff1]); 42 }, 43 null, 44 CL_IGNORE_CL 45 ) 46 ); 47 }); 48 return buff; 49 } 50 51 Services.prefs.setBoolPref( 52 "network.http.encoding.trustworthy_is_https", 53 true 54 ); 55 equal( 56 await test(), 57 "hello", 58 "Should decode brotli when trustworthy_is_https=true" 59 ); 60 Services.prefs.setBoolPref( 61 "network.http.encoding.trustworthy_is_https", 62 false 63 ); 64 equal( 65 await test(), 66 "\x0b\x02\x80hello\x03", 67 "Should not decode brotli when trustworthy_is_https=false" 68 ); 69 Services.prefs.setCharPref( 70 "network.http.accept-encoding", 71 "gzip, deflate, br" 72 ); 73 equal( 74 await test(), 75 "hello", 76 "Should decode brotli if we set the HTTP accept encoding to include brotli" 77 ); 78 Services.prefs.clearUserPref("network.http.accept-encoding"); 79 Services.prefs.clearUserPref("network.http.encoding.trustworthy_is_https"); 80 await httpServer.stop(); 81 }); 82 83 // Make sure we still decode brotli on HTTPS 84 // Node server doesn't work on Android yet. 85 add_task( 86 { skip_if: () => AppConstants.platform == "android" }, 87 async function check_https() { 88 Services.prefs.setBoolPref( 89 "network.http.encoding.trustworthy_is_https", 90 true 91 ); 92 93 let server = new NodeHTTPSServer(); 94 await server.start(); 95 registerCleanupFunction(async () => { 96 await server.stop(); 97 }); 98 await server.registerPathHandler("/brotli", (req, resp) => { 99 resp.setHeader("Content-Type", "text/plain"); 100 resp.setHeader("Content-Encoding", "br"); 101 let output = "\x0b\x02\x80hello\x03"; 102 resp.writeHead(200); 103 resp.end(output, "binary"); 104 }); 105 equal( 106 Services.prefs.getCharPref("network.http.accept-encoding.secure"), 107 "gzip, deflate, br, zstd" 108 ); 109 let { req, buff } = await new Promise(resolve => { 110 let chan = NetUtil.newChannel({ 111 uri: `${server.origin()}/brotli`, 112 loadUsingSystemPrincipal: true, 113 }); 114 chan.asyncOpen( 115 new ChannelListener( 116 (req1, buff1) => resolve({ req: req1, buff: buff1 }), 117 null, 118 CL_ALLOW_UNKNOWN_CL 119 ) 120 ); 121 }); 122 equal(req.status, Cr.NS_OK); 123 equal(req.QueryInterface(Ci.nsIHttpChannel).responseStatus, 200); 124 equal(buff, "hello"); 125 } 126 );