summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_rcwn_always_cache_new_content.js
blob: 8244265088341e80c08984e8f713a106a439782d (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

"use strict";

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

var httpserver = new HttpServer();
httpserver.start(-1);
const PORT = httpserver.identity.primaryPort;

function make_channel(url) {
  return NetUtil.newChannel({
    uri: url,
    loadUsingSystemPrincipal: true,
  }).QueryInterface(Ci.nsIHttpChannel);
}

let gFirstResponseBody = "first version";
let gSecondResponseBody = "second version";
let gRequestCounter = 0;

function test_handler(metadata, response) {
  response.setHeader("Content-Type", "text/plain");
  response.setHeader("Cache-Control", "max-age=3600");
  response.setHeader("ETag", "test-etag1");

  switch (gRequestCounter) {
    case 0:
      response.bodyOutputStream.write(
        gFirstResponseBody,
        gFirstResponseBody.length
      );
      break;
    case 1:
      response.bodyOutputStream.write(
        gSecondResponseBody,
        gSecondResponseBody.length
      );
      break;
    default:
      do_throw("Unexpected request");
  }
  response.setStatusLine(metadata.httpVersion, 200, "OK");
}

function checkContent(request, buffer, context, isFromCache) {
  let isRacing = request.QueryInterface(Ci.nsICacheInfoChannel).isRacing();
  switch (gRequestCounter) {
    case 0:
      Assert.equal(buffer, gFirstResponseBody);
      Assert.ok(!isFromCache);
      Assert.ok(!isRacing);
      break;
    case 1:
      Assert.equal(buffer, gSecondResponseBody);
      Assert.ok(!isFromCache);
      Assert.ok(isRacing);
      break;
    case 2:
      Assert.equal(buffer, gSecondResponseBody);
      Assert.ok(isFromCache);
      Assert.ok(!isRacing);
      break;
    default:
      do_throw("Unexpected request");
  }

  gRequestCounter++;
  executeSoon(() => {
    testGenerator.next();
  });
}

function run_test() {
  do_get_profile();
  // In this test, we race the requests manually using |TriggerNetwork|,
  // therefore we should disable racing in the HttpChannel first.
  Services.prefs.setBoolPref("network.http.rcwn.enabled", false);
  httpserver.registerPathHandler("/rcwn", test_handler);
  testGenerator.next();
  do_test_pending();
}

let testGenerator = testSteps();
function* testSteps() {
  // Store first version of the content in the cache.
  let channel = make_channel("http://localhost:" + PORT + "/rcwn");
  channel.asyncOpen(new ChannelListener(checkContent, null));
  yield undefined;
  equal(gRequestCounter, 1);

  // Simulate the network victory by setting high delay for the cache fetch and
  // triggering the network.
  channel = make_channel("http://localhost:" + PORT + "/rcwn");
  channel
    .QueryInterface(Ci.nsIRaceCacheWithNetwork)
    .test_delayCacheEntryOpeningBy(100000);
  // Trigger network after 50 ms.
  channel.QueryInterface(Ci.nsIRaceCacheWithNetwork).test_triggerNetwork(50);
  channel.asyncOpen(new ChannelListener(checkContent, null));
  yield undefined;
  equal(gRequestCounter, 2);

  // Simulate navigation back by specifying VALIDATE_NEVER flag.
  channel = make_channel("http://localhost:" + PORT + "/rcwn");
  channel.loadFlags = Ci.nsIRequest.VALIDATE_NEVER;
  channel.asyncOpen(new ChannelListener(checkContent, null));
  yield undefined;
  equal(gRequestCounter, 3);

  httpserver.stop(do_test_finished);
}