summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_localhost_offline.js
blob: de0ff6df4efb052b9c9f2982d296d41bbeef141f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"use strict";

const { HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js");
var httpServer = null;
const body = "Hello";

function makeChan(url) {
  let chan = NetUtil.newChannel({
    uri: url,
    loadUsingSystemPrincipal: true,
  }).QueryInterface(Ci.nsIHttpChannel);
  chan.loadFlags |= Ci.nsIRequest.LOAD_BYPASS_CACHE;
  chan.loadFlags |= Ci.nsIRequest.INHIBIT_CACHING;
  return chan;
}

function channelOpenPromise(chan, flags) {
  return new Promise(resolve => {
    function finish(req, buffer) {
      resolve([req, buffer]);
    }
    chan.asyncOpen(new ChannelListener(finish, null, flags));
  });
}

function makeURL(host) {
  return `http://${host}:${httpServer.identity.primaryPort}/`;
}

add_task(async function test_localhost_offline() {
  Services.io.offline = true;
  Services.prefs.setBoolPref("network.disable-localhost-when-offline", false);
  let chan = makeChan(makeURL("127.0.0.1"));
  let [, resp] = await channelOpenPromise(chan);
  Assert.equal(resp, body, "Should get correct response");

  chan = makeChan(makeURL("localhost"));
  [, resp] = await channelOpenPromise(chan);
  Assert.equal(resp, body, "Should get response");

  Services.prefs.setBoolPref("network.disable-localhost-when-offline", true);

  chan = makeChan(makeURL("127.0.0.1"));
  let [req] = await channelOpenPromise(
    chan,
    CL_ALLOW_UNKNOWN_CL | CL_EXPECT_FAILURE
  );
  req.QueryInterface(Ci.nsIHttpChannel);
  Assert.equal(req.status, Cr.NS_ERROR_OFFLINE);

  chan = makeChan(makeURL("localhost"));
  [req] = await channelOpenPromise(
    chan,
    CL_ALLOW_UNKNOWN_CL | CL_EXPECT_FAILURE
  );
  req.QueryInterface(Ci.nsIHttpChannel);
  Assert.equal(req.status, Cr.NS_ERROR_OFFLINE);

  Services.prefs.clearUserPref("network.disable-localhost-when-offline");
  Services.io.offline = false;
});

function run_test() {
  httpServer = new HttpServer();
  httpServer.registerPathHandler("/", (request, response) => {
    response.seizePower();
    response.write("HTTP/1.1 200 OK\r\n");
    response.write("Content-Length: " + body.length + "\r\n");
    response.write("\r\n");
    response.write(body);
    response.finish();
  });
  httpServer.start(-1);
  run_next_test();
}