Backed out changeset b71c8c052463 (bug 1943846) for causing mass failures. CLOSED...
[gecko.git] / netwerk / test / unit / test_bug669001.js
blobc402a6e975d2c5384f08e53f8dbe2de586c5b41f
1 "use strict";
3 const { HttpServer } = ChromeUtils.importESModule(
4 "resource://testing-common/httpd.sys.mjs"
5 );
7 var httpServer = null;
8 var path = "/bug699001";
10 ChromeUtils.defineLazyGetter(this, "URI", function () {
11 return "http://localhost:" + httpServer.identity.primaryPort + path;
12 });
14 function make_channel(url) {
15 return NetUtil.newChannel({ uri: url, loadUsingSystemPrincipal: true });
18 var fetched;
20 // The test loads a resource that expires in one year, has an etag and varies only by User-Agent
21 // First we load it, then check we load it only from the cache w/o even checking with the server
22 // Then we modify our User-Agent and try it again
23 // We have to get a new content (even though with the same etag) and again on next load only from
24 // cache w/o accessing the server
25 // Goal is to check we've updated User-Agent request header in cache after we've got 304 response
26 // from the server
28 var tests = [
30 prepare() {},
31 test() {
32 Assert.ok(fetched);
36 prepare() {},
37 test() {
38 Assert.ok(!fetched);
42 prepare() {
43 setUA("A different User Agent");
45 test() {
46 Assert.ok(fetched);
50 prepare() {},
51 test() {
52 Assert.ok(!fetched);
56 prepare() {
57 setUA("And another User Agent");
59 test() {
60 Assert.ok(fetched);
64 prepare() {},
65 test() {
66 Assert.ok(!fetched);
71 function handler(metadata, response) {
72 if (metadata.hasHeader("If-None-Match")) {
73 response.setStatusLine(metadata.httpVersion, 304, "Not modified");
74 } else {
75 response.setStatusLine(metadata.httpVersion, 200, "OK");
76 response.setHeader("Content-Type", "text/plain");
78 var body = "body";
79 response.bodyOutputStream.write(body, body.length);
82 fetched = true;
84 response.setHeader("Expires", getDateString(+1));
85 response.setHeader("Cache-Control", "private");
86 response.setHeader("Vary", "User-Agent");
87 response.setHeader("ETag", "1234");
90 function run_test() {
91 httpServer = new HttpServer();
92 httpServer.registerPathHandler(path, handler);
93 httpServer.start(-1);
95 do_test_pending();
97 nextTest();
100 function nextTest() {
101 fetched = false;
102 tests[0].prepare();
104 dump("Testing with User-Agent: " + getUA() + "\n");
105 var chan = make_channel(URI);
107 // Give the old channel a chance to close the cache entry first.
108 // XXX This is actually a race condition that might be considered a bug...
109 executeSoon(function () {
110 chan.asyncOpen(new ChannelListener(checkAndShiftTest, null));
114 function checkAndShiftTest(request, response) {
115 tests[0].test(response);
117 tests.shift();
118 if (!tests.length) {
119 httpServer.stop(tearDown);
120 return;
123 nextTest();
126 function tearDown() {
127 setUA("");
128 do_test_finished();
131 // Helpers
133 function getUA() {
134 var httphandler = Cc["@mozilla.org/network/protocol;1?name=http"].getService(
135 Ci.nsIHttpProtocolHandler
137 return httphandler.userAgent;
140 function setUA(value) {
141 Services.prefs.setCharPref("general.useragent.override", value);
144 function getDateString(yearDelta) {
145 var months = [
146 "Jan",
147 "Feb",
148 "Mar",
149 "Apr",
150 "May",
151 "Jun",
152 "Jul",
153 "Aug",
154 "Sep",
155 "Oct",
156 "Nov",
157 "Dec",
159 var days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
161 var d = new Date();
162 return (
163 days[d.getUTCDay()] +
164 ", " +
165 d.getUTCDate() +
166 " " +
167 months[d.getUTCMonth()] +
168 " " +
169 (d.getUTCFullYear() + yearDelta) +
170 " " +
171 d.getUTCHours() +
172 ":" +
173 d.getUTCMinutes() +
174 ":" +
175 d.getUTCSeconds() +
176 " UTC"