summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_bug633743.js
blob: 6e49751c789dd7352752cb6f503788b81df4052a (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
"use strict";

const { HttpServer } = ChromeUtils.importESModule(
  "resource://testing-common/httpd.sys.mjs"
);

const VALUE_HDR_NAME = "X-HTTP-VALUE-HEADER";
const VARY_HDR_NAME = "X-HTTP-VARY-HEADER";
const CACHECTRL_HDR_NAME = "X-CACHE-CONTROL-HEADER";

var httpserver = null;

function make_channel(flags, vary, value) {
  var chan = NetUtil.newChannel({
    uri: "http://localhost:" + httpserver.identity.primaryPort + "/bug633743",
    loadUsingSystemPrincipal: true,
  }).QueryInterface(Ci.nsIHttpChannel);
  return chan.QueryInterface(Ci.nsIHttpChannel);
}

function Test(flags, varyHdr, sendValue, expectValue, cacheHdr) {
  this._flags = flags;
  this._varyHdr = varyHdr;
  this._sendVal = sendValue;
  this._expectVal = expectValue;
  this._cacheHdr = cacheHdr;
}

Test.prototype = {
  _buffer: "",
  _flags: null,
  _varyHdr: null,
  _sendVal: null,
  _expectVal: null,
  _cacheHdr: null,

  QueryInterface: ChromeUtils.generateQI([
    "nsIStreamListener",
    "nsIRequestObserver",
  ]),

  onStartRequest(request) {},

  onDataAvailable(request, stream, offset, count) {
    this._buffer = this._buffer.concat(read_stream(stream, count));
  },

  onStopRequest(request, status) {
    Assert.equal(this._buffer, this._expectVal);
    do_timeout(0, run_next_test);
  },

  run() {
    var channel = make_channel();
    channel.loadFlags = this._flags;
    channel.setRequestHeader(VALUE_HDR_NAME, this._sendVal, false);
    channel.setRequestHeader(VARY_HDR_NAME, this._varyHdr, false);
    if (this._cacheHdr) {
      channel.setRequestHeader(CACHECTRL_HDR_NAME, this._cacheHdr, false);
    }

    channel.asyncOpen(this);
  },
};

var gTests = [
  // Test LOAD_FROM_CACHE: Load cache-entry
  new Test(
    Ci.nsIRequest.LOAD_NORMAL,
    "entity-initial", // hdr-value used to vary
    "request1", // echoed by handler
    "request1" // value expected to receive in channel
  ),
  // Verify that it was cached
  new Test(
    Ci.nsIRequest.LOAD_NORMAL,
    "entity-initial", // hdr-value used to vary
    "fresh value with LOAD_NORMAL", // echoed by handler
    "request1" // value expected to receive in channel
  ),
  // Load same entity with LOAD_FROM_CACHE-flag
  new Test(
    Ci.nsIRequest.LOAD_FROM_CACHE,
    "entity-initial", // hdr-value used to vary
    "fresh value with LOAD_FROM_CACHE", // echoed by handler
    "request1" // value expected to receive in channel
  ),
  // Load different entity with LOAD_FROM_CACHE-flag
  new Test(
    Ci.nsIRequest.LOAD_FROM_CACHE,
    "entity-l-f-c", // hdr-value used to vary
    "request2", // echoed by handler
    "request2" // value expected to receive in channel
  ),
  // Verify that new value was cached
  new Test(
    Ci.nsIRequest.LOAD_NORMAL,
    "entity-l-f-c", // hdr-value used to vary
    "fresh value with LOAD_NORMAL", // echoed by handler
    "request2" // value expected to receive in channel
  ),

  // Test VALIDATE_NEVER: Note previous cache-entry
  new Test(
    Ci.nsIRequest.VALIDATE_NEVER,
    "entity-v-n", // hdr-value used to vary
    "request3", // echoed by handler
    "request3" // value expected to receive in channel
  ),
  // Verify that cache-entry was replaced
  new Test(
    Ci.nsIRequest.LOAD_NORMAL,
    "entity-v-n", // hdr-value used to vary
    "fresh value with LOAD_NORMAL", // echoed by handler
    "request3" // value expected to receive in channel
  ),

  // Test combination VALIDATE_NEVER && no-store: Load new cache-entry
  new Test(
    Ci.nsIRequest.LOAD_NORMAL,
    "entity-2", // hdr-value used to vary
    "request4", // echoed by handler
    "request4", // value expected to receive in channel
    "no-store" // set no-store on response
  ),
  // Ensure we validate without IMS header in this case (verified in handler)
  new Test(
    Ci.nsIRequest.VALIDATE_NEVER,
    "entity-2-v-n", // hdr-value used to vary
    "request5", // echoed by handler
    "request5" // value expected to receive in channel
  ),

  // Test VALIDATE-ALWAYS: Load new entity
  new Test(
    Ci.nsIRequest.LOAD_NORMAL,
    "entity-3", // hdr-value used to vary
    "request6", // echoed by handler
    "request6", // value expected to receive in channel
    "no-cache" // set no-cache on response
  ),
  // Ensure we don't send IMS header also in this case (verified in handler)
  new Test(
    Ci.nsIRequest.VALIDATE_ALWAYS,
    "entity-3-v-a", // hdr-value used to vary
    "request7", // echoed by handler
    "request7" // value expected to receive in channel
  ),
];

function run_next_test() {
  if (!gTests.length) {
    httpserver.stop(do_test_finished);
    return;
  }

  var test = gTests.shift();
  test.run();
}

function handler(metadata, response) {
  // None of the tests above should send an IMS
  Assert.ok(!metadata.hasHeader("If-Modified-Since"));

  // Pick up requested value to echo
  var hdr = "default value";
  try {
    hdr = metadata.getHeader(VALUE_HDR_NAME);
  } catch (ex) {}

  // Pick up requested cache-control header-value
  var cctrlVal = "max-age=10000";
  try {
    cctrlVal = metadata.getHeader(CACHECTRL_HDR_NAME);
  } catch (ex) {}

  response.setStatusLine(metadata.httpVersion, 200, "OK");
  response.setHeader("Content-Type", "text/plain", false);
  response.setHeader("Cache-Control", cctrlVal, false);
  response.setHeader("Vary", VARY_HDR_NAME, false);
  response.setHeader("Last-Modified", "Tue, 15 Nov 1994 12:45:26 GMT", false);
  response.bodyOutputStream.write(hdr, hdr.length);
}

function run_test() {
  // clear the cache
  evict_cache_entries();

  httpserver = new HttpServer();
  httpserver.registerPathHandler("/bug633743", handler);
  httpserver.start(-1);

  run_next_test();
  do_test_pending();
}