1 /* Any copyright is dedicated to the Public Domain.
2 * http://creativecommons.org/publicdomain/zero/1.0/
7 const { HttpServer } = ChromeUtils.importESModule(
8 "resource://testing-common/httpd.sys.mjs"
11 var httpserver = new HttpServer();
13 const PORT = httpserver.identity.primaryPort;
15 function make_channel(url) {
16 return NetUtil.newChannel({
18 loadUsingSystemPrincipal: true,
19 }).QueryInterface(Ci.nsIHttpChannel);
22 let gFirstResponseBody = "first version";
23 let gSecondResponseBody = "second version";
24 let gRequestCounter = 0;
26 function test_handler(metadata, response) {
27 response.setHeader("Content-Type", "text/plain");
28 response.setHeader("Cache-Control", "max-age=3600");
29 response.setHeader("ETag", "test-etag1");
31 switch (gRequestCounter) {
33 response.bodyOutputStream.write(
35 gFirstResponseBody.length
39 response.bodyOutputStream.write(
41 gSecondResponseBody.length
45 do_throw("Unexpected request");
47 response.setStatusLine(metadata.httpVersion, 200, "OK");
50 function checkContent(request, buffer, context, isFromCache) {
51 let isRacing = request.QueryInterface(Ci.nsICacheInfoChannel).isRacing();
52 switch (gRequestCounter) {
54 Assert.equal(buffer, gFirstResponseBody);
55 Assert.ok(!isFromCache);
59 Assert.equal(buffer, gSecondResponseBody);
60 Assert.ok(!isFromCache);
64 Assert.equal(buffer, gSecondResponseBody);
65 Assert.ok(isFromCache);
69 do_throw("Unexpected request");
80 // In this test, we race the requests manually using |TriggerNetwork|,
81 // therefore we should disable racing in the HttpChannel first.
82 Services.prefs.setBoolPref("network.http.rcwn.enabled", false);
83 httpserver.registerPathHandler("/rcwn", test_handler);
88 let testGenerator = testSteps();
89 function* testSteps() {
90 // Store first version of the content in the cache.
91 let channel = make_channel("http://localhost:" + PORT + "/rcwn");
92 channel.asyncOpen(new ChannelListener(checkContent, null));
94 equal(gRequestCounter, 1);
96 // Simulate the network victory by setting high delay for the cache fetch and
97 // triggering the network.
98 channel = make_channel("http://localhost:" + PORT + "/rcwn");
100 .QueryInterface(Ci.nsIRaceCacheWithNetwork)
101 .test_delayCacheEntryOpeningBy(100000);
102 // Trigger network after 50 ms.
103 channel.QueryInterface(Ci.nsIRaceCacheWithNetwork).test_triggerNetwork(50);
104 channel.asyncOpen(new ChannelListener(checkContent, null));
106 equal(gRequestCounter, 2);
108 // Simulate navigation back by specifying VALIDATE_NEVER flag.
109 channel = make_channel("http://localhost:" + PORT + "/rcwn");
110 channel.loadFlags = Ci.nsIRequest.VALIDATE_NEVER;
111 channel.asyncOpen(new ChannelListener(checkContent, null));
113 equal(gRequestCounter, 3);
115 httpserver.stop(do_test_finished);