summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_bug203271.js
blob: ea260bc0b49a469c3bb71f3856fbb49cda5fde91 (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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
//
// Tests if a response with an Expires-header in the past
// and Cache-Control: max-age  in the future works as
// specified in RFC 2616 section 14.9.3 by letting max-age
// take precedence

"use strict";

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

var httpserver = new HttpServer();
var index = 0;
var tests = [
  // original problem described in bug#203271
  {
    url: "/precedence",
    server: "0",
    expected: "0",
    responseheader: [
      "Expires: " + getDateString(-1),
      "Cache-Control: max-age=3600",
    ],
  },

  {
    url: "/precedence?0",
    server: "0",
    expected: "0",
    responseheader: [
      "Cache-Control: max-age=3600",
      "Expires: " + getDateString(-1),
    ],
  },

  // max-age=1s, expires=1 year from now
  {
    url: "/precedence?1",
    server: "0",
    expected: "0",
    responseheader: [
      "Expires: " + getDateString(1),
      "Cache-Control: max-age=1",
    ],
  },

  // expires=now
  {
    url: "/precedence?2",
    server: "0",
    expected: "0",
    responseheader: ["Expires: " + getDateString(0)],
  },

  // max-age=1s
  {
    url: "/precedence?3",
    server: "0",
    expected: "0",
    responseheader: ["Cache-Control: max-age=1"],
  },

  // The test below is the example from
  //
  //         https://bugzilla.mozilla.org/show_bug.cgi?id=203271#c27
  //
  //  max-age=2592000s (1 month), expires=1 year from now, date=1 year ago
  {
    url: "/precedence?4",
    server: "0",
    expected: "0",
    responseheader: [
      "Cache-Control: private, max-age=2592000",
      "Expires: " + getDateString(+1),
    ],
    explicitDate: getDateString(-1),
  },

  // The two tests below are also examples of clocks really out of synch
  // max-age=1s, date=1 year from now
  {
    url: "/precedence?5",
    server: "0",
    expected: "0",
    responseheader: ["Cache-Control: max-age=1"],
    explicitDate: getDateString(1),
  },

  // max-age=60s, date=1 year from now
  {
    url: "/precedence?6",
    server: "0",
    expected: "0",
    responseheader: ["Cache-Control: max-age=60"],
    explicitDate: getDateString(1),
  },

  // this is just to get a pause of 3s to allow cache-entries to expire
  { url: "/precedence?999", server: "0", expected: "0", delay: "3000" },

  // Below are the cases which actually matters
  { url: "/precedence", server: "1", expected: "0" }, // should be cached

  { url: "/precedence?0", server: "1", expected: "0" }, // should be cached

  { url: "/precedence?1", server: "1", expected: "1" }, // should have expired

  { url: "/precedence?2", server: "1", expected: "1" }, // should have expired

  { url: "/precedence?3", server: "1", expected: "1" }, // should have expired

  { url: "/precedence?4", server: "1", expected: "1" }, // should have expired

  { url: "/precedence?5", server: "1", expected: "1" }, // should have expired

  { url: "/precedence?6", server: "1", expected: "0" }, // should be cached
];

function logit(i, data, ctx) {
  dump(
    "requested [" +
      tests[i].server +
      "] " +
      "got [" +
      data +
      "] " +
      "expected [" +
      tests[i].expected +
      "]"
  );

  if (tests[i].responseheader) {
    dump("\t[" + tests[i].responseheader + "]");
  }
  dump("\n");
  // Dump all response-headers
  dump("\n===================================\n");
  ctx.visitResponseHeaders({
    visitHeader(key, val) {
      dump("\t" + key + ":" + val + "\n");
    },
  });
  dump("===================================\n");
}

function setupChannel(suffix, value) {
  var chan = NetUtil.newChannel({
    uri: "http://localhost:" + httpserver.identity.primaryPort + suffix,
    loadUsingSystemPrincipal: true,
  });
  var httpChan = chan.QueryInterface(Ci.nsIHttpChannel);
  httpChan.requestMethod = "GET"; // default value, just being paranoid...
  httpChan.setRequestHeader("x-request", value, false);
  return httpChan;
}

function triggerNextTest() {
  var channel = setupChannel(tests[index].url, tests[index].server);
  channel.asyncOpen(new ChannelListener(checkValueAndTrigger, channel));
}

function checkValueAndTrigger(request, data, ctx) {
  logit(index, data, ctx);
  Assert.equal(tests[index].expected, data);

  if (index < tests.length - 1) {
    var delay = tests[index++].delay;
    if (delay) {
      do_timeout(delay, triggerNextTest);
    } else {
      triggerNextTest();
    }
  } else {
    httpserver.stop(do_test_finished);
  }
}

function run_test() {
  httpserver.registerPathHandler("/precedence", handler);
  httpserver.start(-1);

  // clear cache
  evict_cache_entries();

  triggerNextTest();
  do_test_pending();
}

function handler(metadata, response) {
  var body = metadata.getHeader("x-request");
  response.setHeader("Content-Type", "text/plain", false);

  var date = tests[index].explicitDate;
  if (date == undefined) {
    response.setHeader("Date", getDateString(0), false);
  } else {
    response.setHeader("Date", date, false);
  }

  var header = tests[index].responseheader;
  if (header == undefined) {
    response.setHeader("Last-Modified", getDateString(-1), false);
  } else {
    for (var i = 0; i < header.length; i++) {
      var splitHdr = header[i].split(": ");
      response.setHeader(splitHdr[0], splitHdr[1], false);
    }
  }

  response.setStatusLine(metadata.httpVersion, 200, "OK");
  response.bodyOutputStream.write(body, body.length);
}

function getDateString(yearDelta) {
  var months = [
    "Jan",
    "Feb",
    "Mar",
    "Apr",
    "May",
    "Jun",
    "Jul",
    "Aug",
    "Sep",
    "Oct",
    "Nov",
    "Dec",
  ];
  var days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];

  var d = new Date();
  return (
    days[d.getUTCDay()] +
    ", " +
    d.getUTCDate() +
    " " +
    months[d.getUTCMonth()] +
    " " +
    (d.getUTCFullYear() + yearDelta) +
    " " +
    d.getUTCHours() +
    ":" +
    d.getUTCMinutes() +
    ":" +
    d.getUTCSeconds() +
    " UTC"
  );
}