Bug 1857386 [wpt PR 42383] - Update wpt metadata, a=testonly
[gecko.git] / netwerk / test / unit / test_redirect_failure.js
blob28b466858074bd7bc52074daf2152c8cac2b4b42
1 "use strict";
3 const { HttpServer } = ChromeUtils.importESModule(
4   "resource://testing-common/httpd.sys.mjs"
5 );
7 /*
8  * The test is checking async redirect code path that is loading a
9  * redirect.  But creation of the target channel fails before we even try
10  * to do async open on it. We force the creation error by forbidding
11  * the port number the URI contains.
12  */
14 function inChildProcess() {
15   return Services.appinfo.processType != Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT;
18 ChromeUtils.defineLazyGetter(this, "URL", function () {
19   return "http://localhost:" + httpServer.identity.primaryPort;
20 });
22 var httpServer = null;
23 // Need to randomize, because apparently no one clears our cache
24 var randomPath = "/redirect/" + Math.random();
26 ChromeUtils.defineLazyGetter(this, "randomURI", function () {
27   return URL + randomPath;
28 });
30 function make_channel(url, callback, ctx) {
31   return NetUtil.newChannel({ uri: url, loadUsingSystemPrincipal: true });
34 function redirectHandler(metadata, response) {
35   response.setStatusLine(metadata.httpVersion, 301, "Moved");
36   response.setHeader("Location", "http://non-existent.tld:65400", false);
37   response.setHeader("Cache-Control", "no-cache", false);
40 function finish_test(request, buffer) {
41   Assert.equal(request.status, Cr.NS_ERROR_PORT_ACCESS_NOT_ALLOWED);
43   Assert.equal(buffer, "");
44   httpServer.stop(do_test_finished);
47 function run_test() {
48   httpServer = new HttpServer();
49   httpServer.registerPathHandler(randomPath, redirectHandler);
50   httpServer.start(-1);
52   if (!inChildProcess()) {
53     Services.prefs.setCharPref("network.security.ports.banned", "65400");
54   }
56   var chan = make_channel(randomURI);
57   chan.asyncOpen(new ChannelListener(finish_test, null, CL_EXPECT_FAILURE));
58   do_test_pending();