|
1 Cu.import("resource://testing-common/httpd.js"); |
|
2 |
|
3 var httpServer = null; |
|
4 var path = "/bug699001"; |
|
5 |
|
6 XPCOMUtils.defineLazyGetter(this, "URI", function() { |
|
7 return "http://localhost:" + httpServer.identity.primaryPort + path; |
|
8 }); |
|
9 |
|
10 function make_channel(url) { |
|
11 var ios = Cc["@mozilla.org/network/io-service;1"]. |
|
12 getService(Ci.nsIIOService); |
|
13 return ios.newChannel(url, "", null); |
|
14 } |
|
15 |
|
16 var fetched; |
|
17 |
|
18 // The test loads a resource that expires in one year, has an etag and varies only by User-Agent |
|
19 // First we load it, then check we load it only from the cache w/o even checking with the server |
|
20 // Then we modify our User-Agent and try it again |
|
21 // We have to get a new content (even though with the same etag) and again on next load only from |
|
22 // cache w/o accessing the server |
|
23 // Goal is to check we've updated User-Agent request header in cache after we've got 304 response |
|
24 // from the server |
|
25 |
|
26 var tests = [ |
|
27 { |
|
28 prepare: function() { }, |
|
29 test: function(response) { |
|
30 do_check_true(fetched); |
|
31 } |
|
32 }, |
|
33 { |
|
34 prepare: function() { }, |
|
35 test: function(response) { |
|
36 do_check_false(fetched); |
|
37 } |
|
38 }, |
|
39 { |
|
40 prepare: function() { |
|
41 setUA("A different User Agent"); |
|
42 }, |
|
43 test: function(response) { |
|
44 do_check_true(fetched); |
|
45 } |
|
46 }, |
|
47 { |
|
48 prepare: function() { }, |
|
49 test: function(response) { |
|
50 do_check_false(fetched); |
|
51 } |
|
52 }, |
|
53 { |
|
54 prepare: function() { |
|
55 setUA("And another User Agent"); |
|
56 }, |
|
57 test: function(response) { |
|
58 do_check_true(fetched); |
|
59 } |
|
60 }, |
|
61 { |
|
62 prepare: function() { }, |
|
63 test: function(response) { |
|
64 do_check_false(fetched); |
|
65 } |
|
66 } |
|
67 ]; |
|
68 |
|
69 function handler(metadata, response) |
|
70 { |
|
71 if (metadata.hasHeader("If-None-Match")) { |
|
72 response.setStatusLine(metadata.httpVersion, 304, "Not modified"); |
|
73 } |
|
74 else { |
|
75 response.setStatusLine(metadata.httpVersion, 200, "OK"); |
|
76 response.setHeader("Content-Type", "text/plain"); |
|
77 |
|
78 var body = "body"; |
|
79 response.bodyOutputStream.write(body, body.length); |
|
80 } |
|
81 |
|
82 fetched = true; |
|
83 |
|
84 response.setHeader("Expires", getDateString(+1)); |
|
85 response.setHeader("Cache-Control", "private"); |
|
86 response.setHeader("Vary", "User-Agent"); |
|
87 response.setHeader("ETag", "1234"); |
|
88 } |
|
89 |
|
90 function run_test() |
|
91 { |
|
92 httpServer = new HttpServer(); |
|
93 httpServer.registerPathHandler(path, handler); |
|
94 httpServer.start(-1); |
|
95 |
|
96 do_test_pending(); |
|
97 |
|
98 nextTest(); |
|
99 } |
|
100 |
|
101 function nextTest() |
|
102 { |
|
103 fetched = false; |
|
104 tests[0].prepare(); |
|
105 |
|
106 dump("Testing with User-Agent: " + getUA() + "\n"); |
|
107 var chan = make_channel(URI); |
|
108 |
|
109 // Give the old channel a chance to close the cache entry first. |
|
110 // XXX This is actually a race condition that might be considered a bug... |
|
111 do_execute_soon(function() { |
|
112 chan.asyncOpen(new ChannelListener(checkAndShiftTest, null), null); |
|
113 }); |
|
114 } |
|
115 |
|
116 function checkAndShiftTest(request, response) |
|
117 { |
|
118 tests[0].test(response); |
|
119 |
|
120 tests.shift(); |
|
121 if (tests.length == 0) { |
|
122 httpServer.stop(tearDown); |
|
123 return; |
|
124 } |
|
125 |
|
126 nextTest(); |
|
127 } |
|
128 |
|
129 function tearDown() |
|
130 { |
|
131 setUA(""); |
|
132 do_test_finished(); |
|
133 } |
|
134 |
|
135 // Helpers |
|
136 |
|
137 function getUA() |
|
138 { |
|
139 var httphandler = Cc["@mozilla.org/network/protocol;1?name=http"]. |
|
140 getService(Ci.nsIHttpProtocolHandler); |
|
141 return httphandler.userAgent; |
|
142 } |
|
143 |
|
144 function setUA(value) |
|
145 { |
|
146 var prefs = Cc["@mozilla.org/preferences-service;1"]. |
|
147 getService(Ci.nsIPrefBranch); |
|
148 prefs.setCharPref("general.useragent.override", value); |
|
149 } |
|
150 |
|
151 function getDateString(yearDelta) { |
|
152 var months = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', |
|
153 'Sep', 'Oct', 'Nov', 'Dec' ]; |
|
154 var days = [ 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' ]; |
|
155 |
|
156 var d = new Date(); |
|
157 return days[d.getUTCDay()] + ", " + d.getUTCDate() + " " |
|
158 + months[d.getUTCMonth()] + " " + (d.getUTCFullYear() + yearDelta) |
|
159 + " " + d.getUTCHours() + ":" + d.getUTCMinutes() + ":" |
|
160 + d.getUTCSeconds() + " UTC"; |
|
161 } |