test_http_408_retry.js (2865B)
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 const { NodeHTTPServer, NodeHTTPSServer, NodeHTTP2Server, with_node_servers } = 8 ChromeUtils.importESModule("resource://testing-common/NodeServer.sys.mjs"); 9 10 async function loadURL(uri, flags) { 11 let chan = NetUtil.newChannel({ 12 uri, 13 loadUsingSystemPrincipal: true, 14 }).QueryInterface(Ci.nsIHttpChannel); 15 chan.loadFlags = Ci.nsIChannel.LOAD_INITIAL_DOCUMENT_URI; 16 17 return new Promise(resolve => { 18 chan.asyncOpen( 19 new ChannelListener((req, buff) => resolve({ req, buff }), null, flags) 20 ); 21 }); 22 } 23 24 add_task(async function test() { 25 let certdb = Cc["@mozilla.org/security/x509certdb;1"].getService( 26 Ci.nsIX509CertDB 27 ); 28 addCertFromFile(certdb, "http2-ca.pem", "CTu,u,u"); 29 30 async function check408retry(server) { 31 info(`Testing ${server.constructor.name}`); 32 await server.execute(`global.server_name = "${server.constructor.name}";`); 33 if ( 34 server.constructor.name == "NodeHTTPServer" || 35 server.constructor.name == "NodeHTTPSServer" 36 ) { 37 await server.registerPathHandler("/test", (req, resp) => { 38 let oldSock = global.socket; 39 global.socket = resp.socket; 40 if (global.socket == oldSock) { 41 // This function is handled within the httpserver where setTimeout is 42 // available. 43 // eslint-disable-next-line mozilla/no-arbitrary-setTimeout, no-undef 44 setTimeout( 45 arg => { 46 arg.writeHead(408); 47 arg.end("stuff"); 48 }, 49 1100, 50 resp 51 ); 52 return; 53 } 54 resp.writeHead(200); 55 resp.end(global.server_name); 56 }); 57 } else { 58 await server.registerPathHandler("/test", (req, resp) => { 59 global.socket = resp.socket; 60 if (!global.sent408) { 61 global.sent408 = true; 62 resp.writeHead(408); 63 resp.end("stuff"); 64 return; 65 } 66 resp.writeHead(200); 67 resp.end(global.server_name); 68 }); 69 } 70 71 async function load() { 72 let { req, buff } = await loadURL( 73 `${server.origin()}/test`, 74 CL_ALLOW_UNKNOWN_CL 75 ); 76 equal(req.status, Cr.NS_OK); 77 equal(req.QueryInterface(Ci.nsIHttpChannel).responseStatus, 200); 78 equal(buff, server.constructor.name); 79 equal( 80 req.QueryInterface(Ci.nsIHttpChannel).protocolVersion, 81 server.constructor.name == "NodeHTTP2Server" ? "h2" : "http/1.1" 82 ); 83 } 84 85 info("first load"); 86 await load(); 87 info("second load"); 88 await load(); 89 } 90 91 await with_node_servers( 92 [NodeHTTPServer, NodeHTTPSServer, NodeHTTP2Server], 93 check408retry 94 ); 95 });