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