Bug 1931425 - Limit how often moz-label's #setStyles runs r=reusable-components-revie...
[gecko.git] / netwerk / test / unit / test_http_408_retry.js
blob16392ab4bfc8bb2117e4d1832d072f344690d575
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 async function loadURL(uri, flags) {
8 let chan = NetUtil.newChannel({
9 uri,
10 loadUsingSystemPrincipal: true,
11 }).QueryInterface(Ci.nsIHttpChannel);
12 chan.loadFlags = Ci.nsIChannel.LOAD_INITIAL_DOCUMENT_URI;
14 return new Promise(resolve => {
15 chan.asyncOpen(
16 new ChannelListener((req, buff) => resolve({ req, buff }), null, flags)
18 });
21 add_task(async function test() {
22 let certdb = Cc["@mozilla.org/security/x509certdb;1"].getService(
23 Ci.nsIX509CertDB
25 addCertFromFile(certdb, "http2-ca.pem", "CTu,u,u");
27 async function check408retry(server) {
28 info(`Testing ${server.constructor.name}`);
29 await server.execute(`global.server_name = "${server.constructor.name}";`);
30 if (
31 server.constructor.name == "NodeHTTPServer" ||
32 server.constructor.name == "NodeHTTPSServer"
33 ) {
34 await server.registerPathHandler("/test", (req, resp) => {
35 let oldSock = global.socket;
36 global.socket = resp.socket;
37 if (global.socket == oldSock) {
38 // This function is handled within the httpserver where setTimeout is
39 // available.
40 // eslint-disable-next-line mozilla/no-arbitrary-setTimeout, no-undef
41 setTimeout(
42 arg => {
43 arg.writeHead(408);
44 arg.end("stuff");
46 1100,
47 resp
49 return;
51 resp.writeHead(200);
52 resp.end(global.server_name);
53 });
54 } else {
55 await server.registerPathHandler("/test", (req, resp) => {
56 global.socket = resp.socket;
57 if (!global.sent408) {
58 global.sent408 = true;
59 resp.writeHead(408);
60 resp.end("stuff");
61 return;
63 resp.writeHead(200);
64 resp.end(global.server_name);
65 });
68 async function load() {
69 let { req, buff } = await loadURL(
70 `${server.origin()}/test`,
71 CL_ALLOW_UNKNOWN_CL
73 equal(req.status, Cr.NS_OK);
74 equal(req.QueryInterface(Ci.nsIHttpChannel).responseStatus, 200);
75 equal(buff, server.constructor.name);
76 equal(
77 req.QueryInterface(Ci.nsIHttpChannel).protocolVersion,
78 server.constructor.name == "NodeHTTP2Server" ? "h2" : "http/1.1"
82 info("first load");
83 await load();
84 info("second load");
85 await load();
88 await with_node_servers(
89 [NodeHTTPServer, NodeHTTPSServer, NodeHTTP2Server],
90 check408retry
92 });