test_brotli_decoding.js (4737B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 "use strict"; 6 7 /* import-globals-from head_cache.js */ 8 /* import-globals-from head_cookies.js */ 9 /* import-globals-from head_channels.js */ 10 11 const { NodeHTTP2Server } = ChromeUtils.importESModule( 12 "resource://testing-common/NodeServer.sys.mjs" 13 ); 14 15 let endChunk2ReceivedInTime = false; 16 17 function makeChan(uri) { 18 let chan = NetUtil.newChannel({ 19 uri, 20 loadUsingSystemPrincipal: true, 21 }).QueryInterface(Ci.nsIHttpChannel); 22 chan.loadFlags = Ci.nsIChannel.LOAD_INITIAL_DOCUMENT_URI; 23 return chan; 24 } 25 26 let channelListener = function (closure) { 27 this._closure = closure; 28 this._start = Date.now(); 29 }; 30 31 channelListener.prototype = { 32 onStartRequest: function testOnStartRequest() {}, 33 34 onDataAvailable: function testOnDataAvailable(request, stream, off, cnt) { 35 let data = read_stream(stream, cnt); 36 let current = Date.now(); 37 let elapsed = current - this._start; 38 dump("data:" + data.slice(-10) + "\n"); 39 dump("elapsed=" + elapsed + "\n"); 40 if (elapsed < 2500 && data[data.length - 1] == "E") { 41 endChunk2ReceivedInTime = true; 42 } 43 }, 44 45 onStopRequest: function testOnStopRequest() { 46 this._closure(); 47 }, 48 }; 49 50 add_task(async function test_http2() { 51 let certdb = Cc["@mozilla.org/security/x509certdb;1"].getService( 52 Ci.nsIX509CertDB 53 ); 54 addCertFromFile(certdb, "http2-ca.pem", "CTu,u,u"); 55 56 let server = new NodeHTTP2Server(); 57 await server.start(); 58 registerCleanupFunction(async () => { 59 await server.stop(); 60 }); 61 let chan = makeChan(`https://localhost:${server.port()}/test`); 62 let req = await new Promise(resolve => { 63 chan.asyncOpen(new ChannelListener(resolve, null, CL_ALLOW_UNKNOWN_CL)); 64 }); 65 equal(req.status, Cr.NS_OK); 66 equal(req.QueryInterface(Ci.nsIHttpChannel).responseStatus, 404); 67 await server.registerPathHandler("/test", (req1, resp) => { 68 resp.writeHead(200, { 69 "content-type": "text/html; charset=utf-8", 70 "content-encoding": "br", 71 }); 72 resp.write( 73 Buffer.from([ 74 0x8b, 0x2a, 0x80, 0x3c, 0x64, 0x69, 0x76, 0x3e, 0x73, 0x74, 0x61, 0x72, 75 0x74, 0x3c, 0x2f, 0x64, 0x69, 0x76, 0x3e, 0x3c, 0x64, 0x69, 0x76, 0x20, 76 0x73, 0x74, 0x79, 0x6c, 0x65, 0x3d, 0x22, 0x74, 0x65, 0x78, 0x74, 0x2d, 77 0x6f, 0x76, 0x65, 0x72, 0x66, 0x6c, 0x6f, 0x77, 0x3a, 0x20, 0x65, 0x6c, 78 0x6c, 0x69, 0x70, 0x73, 0x69, 0x73, 0x3b, 0x6f, 0x76, 0x65, 0x72, 0x66, 79 0x6c, 0x6f, 0x77, 0x3a, 0x20, 0x68, 0x69, 0x64, 0x64, 0x65, 0x6e, 0x3b, 80 0x64, 0x69, 0x72, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x72, 81 0x74, 0x6c, 0x3b, 0x22, 0x3e, 82 ]) 83 ); 84 85 // This function is handled within the httpserver where setTimeout is 86 // available. 87 // eslint-disable-next-line mozilla/no-arbitrary-setTimeout, no-undef 88 setTimeout(function () { 89 resp.write( 90 Buffer.from([ 91 0xfa, 0xff, 0x0b, 0x00, 0x80, 0xaa, 0xaa, 0xaa, 0xea, 0x3f, 0x72, 92 0x59, 0xd6, 0x05, 0x73, 0x5b, 0xb6, 0x75, 0xea, 0xe6, 0xfd, 0xa8, 93 0x54, 0xc7, 0x62, 0xd8, 0x18, 0x86, 0x61, 0x18, 0x86, 0x63, 0xa9, 94 0x86, 0x61, 0x18, 0x86, 0xe1, 0x63, 0x8e, 0x63, 0xa9, 0x86, 0x61, 95 0x18, 0x86, 0x61, 0xd8, 0xe0, 0xbc, 0x85, 0x48, 0x1f, 0xa0, 0x05, 96 0xda, 0x6f, 0xef, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0xaa, 0xaa, 97 0xaa, 0xaa, 0xff, 0xc8, 0x65, 0x59, 0x17, 0xcc, 0x6d, 0xd9, 0xd6, 98 0xa9, 0x9b, 0xf7, 0xff, 0x0d, 0xd5, 0xb1, 0x18, 0xe6, 0x63, 0x18, 99 0x86, 0x61, 0x18, 0x8e, 0xa5, 0x1a, 0x86, 0x61, 0x18, 0x86, 0x61, 100 0x8e, 0xed, 0x57, 0x0d, 0xc3, 0x30, 0x0c, 0xc3, 0xb0, 0xc1, 0x79, 101 0x0b, 0x91, 0x3e, 0x40, 0x6c, 0x1c, 0x00, 0x90, 0xd6, 0x3b, 0x00, 102 0x50, 0x96, 0x31, 0x53, 0xe6, 0x2c, 0x59, 0xb3, 0x65, 0xcf, 0x91, 103 0x33, 0x57, 0x31, 0x03, 104 ]) 105 ); 106 }, 100); 107 108 // This function is handled within the httpserver where setTimeout is 109 // available. 110 // eslint-disable-next-line mozilla/no-arbitrary-setTimeout, no-undef 111 setTimeout(function () { 112 resp.end( 113 Buffer.from([ 114 0x98, 0x00, 0x08, 0x3c, 0x2f, 0x64, 0x69, 0x76, 0x3e, 0x3c, 0x64, 115 0x69, 0x76, 0x3e, 0x65, 0x6e, 0x64, 0x3c, 0x2f, 0x64, 0x69, 0x76, 116 0x3e, 0x03, 117 ]) 118 ); 119 }, 2500); 120 }); 121 chan = makeChan(`https://localhost:${server.port()}/test`); 122 await new Promise(resolve => { 123 chan.asyncOpen(new channelListener(() => resolve())); 124 }); 125 126 equal( 127 endChunk2ReceivedInTime, 128 true, 129 "End of chunk 2 not received before chunk 3 was sent" 130 ); 131 });