test_content_encoding_gzip.js (4677B)
1 "use strict"; 2 3 const { HttpServer } = ChromeUtils.importESModule( 4 "resource://testing-common/httpd.sys.mjs" 5 ); 6 7 var httpserver = new HttpServer(); 8 var index = 0; 9 var tests = [ 10 { 11 url: "/test/cegzip1", 12 flags: CL_EXPECT_GZIP, 13 ce: "gzip", 14 body: [ 15 0x1f, 0x8b, 0x08, 0x08, 0x5a, 0xa0, 0x31, 0x4f, 0x00, 0x03, 0x74, 0x78, 16 0x74, 0x00, 0x2b, 0xc9, 0xc8, 0x2c, 0x56, 0x00, 0xa2, 0x92, 0xd4, 0xe2, 17 0x12, 0x43, 0x2e, 0x00, 0xb9, 0x23, 0xd7, 0x3b, 0x0e, 0x00, 0x00, 0x00, 18 ], 19 datalen: 14, // the data length of the uncompressed document 20 }, 21 22 { 23 url: "/test/cegzip2", 24 flags: CL_EXPECT_GZIP, 25 ce: "gzip, gzip", 26 body: [ 27 0x1f, 0x8b, 0x08, 0x00, 0x72, 0xa1, 0x31, 0x4f, 0x00, 0x03, 0x93, 0xef, 28 0xe6, 0xe0, 0x88, 0x5a, 0x60, 0xe8, 0xcf, 0xc0, 0x5c, 0x52, 0x51, 0xc2, 29 0xa0, 0x7d, 0xf2, 0x84, 0x4e, 0x18, 0xc3, 0xa2, 0x49, 0x57, 0x1e, 0x09, 30 0x39, 0xeb, 0x31, 0xec, 0x54, 0xbe, 0x6e, 0xcd, 0xc7, 0xc0, 0xc0, 0x00, 31 0x00, 0x6e, 0x90, 0x7a, 0x85, 0x24, 0x00, 0x00, 0x00, 32 ], 33 datalen: 14, // the data length of the uncompressed document 34 }, 35 36 { 37 url: "/test/cebrotli1", 38 flags: CL_EXPECT_GZIP, 39 ce: "br", 40 body: [0x0b, 0x02, 0x80, 0x74, 0x65, 0x73, 0x74, 0x0a, 0x03], 41 42 datalen: 5, // the data length of the uncompressed document 43 }, 44 45 // this is a truncated brotli document, producing no output bytes 46 { 47 url: "/test/cebrotli2", 48 flags: CL_EXPECT_GZIP, 49 ce: "br", 50 body: [0x0b, 0x0a, 0x09], 51 datalen: 0, 52 }, 53 54 // this is brotli but should come through as identity due to prefs 55 { 56 url: "/test/cebrotli3", 57 flags: 0, 58 ce: "br", 59 body: [0x0b, 0x02, 0x80, 0x74, 0x65, 0x73, 0x74, 0x0a, 0x03], 60 61 datalen: 9, 62 }, 63 64 // this is not a brotli document 65 { 66 url: "/test/cebrotli4", 67 flags: CL_EXPECT_GZIP | CL_EXPECT_FAILURE, 68 ce: "br", 69 body: [ 70 0x01, 0xe6, 0x00, 0x76, 0x42, 0x10, 0x01, 0x1c, 0x24, 0x24, 0x3c, 0xd7, 71 0xd7, 0xd7, 0x01, 0x1c, 72 ], 73 datalen: 16, 74 }, 75 76 // this is a brotli document 77 { 78 url: "/test/cebrotli5", 79 flags: CL_EXPECT_GZIP, 80 ce: "br", 81 body: [0x0b, 0x00, 0x80, 0x4e, 0x03], 82 datalen: 1, 83 }, 84 85 // this a truncated brotli document (missing the end marker) 86 // producing one output byte 87 { 88 url: "/test/cebrotli6", 89 flags: CL_EXPECT_GZIP, 90 ce: "br", 91 body: [0x0b, 0x00, 0x80, 0x4e], 92 datalen: 1, 93 }, 94 ]; 95 96 function setupChannel(url) { 97 return NetUtil.newChannel({ 98 uri: "http://localhost:" + httpserver.identity.primaryPort + url, 99 loadUsingSystemPrincipal: true, 100 }); 101 } 102 103 function startIter() { 104 if (tests[index].url === "/test/cebrotli3") { 105 // this test wants to make sure we don't do brotli when not in a-e 106 prefs.setCharPref("network.http.accept-encoding", "gzip, deflate"); 107 } else { 108 prefs.setCharPref("network.http.accept-encoding", "gzip, deflate, br"); 109 } 110 var channel = setupChannel(tests[index].url); 111 channel.asyncOpen( 112 new ChannelListener(completeIter, channel, tests[index].flags) 113 ); 114 } 115 116 function completeIter(request, data) { 117 if (!(tests[index].flags & CL_EXPECT_FAILURE)) { 118 Assert.equal(data.length, tests[index].datalen, "test " + index); 119 } 120 if (++index < tests.length) { 121 startIter(); 122 } else { 123 httpserver.stop(do_test_finished); 124 prefs.setCharPref("network.http.accept-encoding", cePref); 125 } 126 } 127 128 var prefs; 129 var cePref; 130 function run_test() { 131 prefs = Services.prefs; 132 cePref = prefs.getCharPref("network.http.accept-encoding"); 133 prefs.setBoolPref("network.http.encoding.trustworthy_is_https", false); 134 135 httpserver.registerPathHandler("/test/cegzip1", handler); 136 httpserver.registerPathHandler("/test/cegzip2", handler); 137 httpserver.registerPathHandler("/test/cebrotli1", handler); 138 httpserver.registerPathHandler("/test/cebrotli2", handler); 139 httpserver.registerPathHandler("/test/cebrotli3", handler); 140 httpserver.registerPathHandler("/test/cebrotli4", handler); 141 httpserver.registerPathHandler("/test/cebrotli5", handler); 142 httpserver.registerPathHandler("/test/cebrotli6", handler); 143 httpserver.start(-1); 144 145 startIter(); 146 do_test_pending(); 147 } 148 149 function handler(metadata, response) { 150 response.setStatusLine(metadata.httpVersion, 200, "OK"); 151 response.setHeader("Content-Type", "text/plain", false); 152 response.setHeader("Content-Encoding", tests[index].ce, false); 153 response.setHeader("Content-Length", "" + tests[index].body.length, false); 154 155 var bos = Cc["@mozilla.org/binaryoutputstream;1"].createInstance( 156 Ci.nsIBinaryOutputStream 157 ); 158 bos.setOutputStream(response.bodyOutputStream); 159 160 response.processAsync(); 161 bos.writeByteArray(tests[index].body); 162 response.finish(); 163 }