|
1 Cu.import("resource://testing-common/httpd.js"); |
|
2 |
|
3 const VALUE_HDR_NAME = "X-HTTP-VALUE-HEADER"; |
|
4 const VARY_HDR_NAME = "X-HTTP-VARY-HEADER"; |
|
5 const CACHECTRL_HDR_NAME = "X-CACHE-CONTROL-HEADER"; |
|
6 |
|
7 var httpserver = null; |
|
8 |
|
9 function make_channel(flags, vary, value) { |
|
10 var ios = Cc["@mozilla.org/network/io-service;1"]. |
|
11 getService(Ci.nsIIOService); |
|
12 var chan = ios.newChannel("http://localhost:" + |
|
13 httpserver.identity.primaryPort + |
|
14 "/bug633743", null, null); |
|
15 return chan.QueryInterface(Ci.nsIHttpChannel); |
|
16 } |
|
17 |
|
18 function Test(flags, varyHdr, sendValue, expectValue, cacheHdr) { |
|
19 this._flags = flags; |
|
20 this._varyHdr = varyHdr; |
|
21 this._sendVal = sendValue; |
|
22 this._expectVal = expectValue; |
|
23 this._cacheHdr = cacheHdr; |
|
24 } |
|
25 |
|
26 Test.prototype = { |
|
27 _buffer: "", |
|
28 _flags: null, |
|
29 _varyHdr: null, |
|
30 _sendVal: null, |
|
31 _expectVal: null, |
|
32 _cacheHdr: null, |
|
33 |
|
34 QueryInterface: function(iid) { |
|
35 if (iid.equals(Ci.nsIStreamListener) || |
|
36 iid.equals(Ci.nsIRequestObserver) || |
|
37 iid.equals(Ci.nsISupports)) |
|
38 return this; |
|
39 throw Cr.NS_ERROR_NO_INTERFACE; |
|
40 }, |
|
41 |
|
42 onStartRequest: function(request, context) { }, |
|
43 |
|
44 onDataAvailable: function(request, context, stream, offset, count) { |
|
45 this._buffer = this._buffer.concat(read_stream(stream, count)); |
|
46 }, |
|
47 |
|
48 onStopRequest: function(request, context, status) { |
|
49 do_check_eq(this._buffer, this._expectVal); |
|
50 do_timeout(0, run_next_test); |
|
51 }, |
|
52 |
|
53 run: function() { |
|
54 var channel = make_channel(); |
|
55 channel.loadFlags = this._flags; |
|
56 channel.setRequestHeader(VALUE_HDR_NAME, this._sendVal, false); |
|
57 channel.setRequestHeader(VARY_HDR_NAME, this._varyHdr, false); |
|
58 if (this._cacheHdr) |
|
59 channel.setRequestHeader(CACHECTRL_HDR_NAME, this._cacheHdr, false); |
|
60 |
|
61 channel.asyncOpen(this, null); |
|
62 } |
|
63 }; |
|
64 |
|
65 var gTests = [ |
|
66 // Test LOAD_FROM_CACHE: Load cache-entry |
|
67 new Test(Ci.nsIRequest.LOAD_NORMAL, |
|
68 "entity-initial", // hdr-value used to vary |
|
69 "request1", // echoed by handler |
|
70 "request1" // value expected to receive in channel |
|
71 ), |
|
72 // Verify that it was cached |
|
73 new Test(Ci.nsIRequest.LOAD_NORMAL, |
|
74 "entity-initial", // hdr-value used to vary |
|
75 "fresh value with LOAD_NORMAL", // echoed by handler |
|
76 "request1" // value expected to receive in channel |
|
77 ), |
|
78 // Load same entity with LOAD_FROM_CACHE-flag |
|
79 new Test(Ci.nsIRequest.LOAD_FROM_CACHE, |
|
80 "entity-initial", // hdr-value used to vary |
|
81 "fresh value with LOAD_FROM_CACHE", // echoed by handler |
|
82 "request1" // value expected to receive in channel |
|
83 ), |
|
84 // Load different entity with LOAD_FROM_CACHE-flag |
|
85 new Test(Ci.nsIRequest.LOAD_FROM_CACHE, |
|
86 "entity-l-f-c", // hdr-value used to vary |
|
87 "request2", // echoed by handler |
|
88 "request2" // value expected to receive in channel |
|
89 ), |
|
90 // Verify that new value was cached |
|
91 new Test(Ci.nsIRequest.LOAD_NORMAL, |
|
92 "entity-l-f-c", // hdr-value used to vary |
|
93 "fresh value with LOAD_NORMAL", // echoed by handler |
|
94 "request2" // value expected to receive in channel |
|
95 ), |
|
96 |
|
97 // Test VALIDATE_NEVER: Note previous cache-entry |
|
98 new Test(Ci.nsIRequest.VALIDATE_NEVER, |
|
99 "entity-v-n", // hdr-value used to vary |
|
100 "request3", // echoed by handler |
|
101 "request3" // value expected to receive in channel |
|
102 ), |
|
103 // Verify that cache-entry was replaced |
|
104 new Test(Ci.nsIRequest.LOAD_NORMAL, |
|
105 "entity-v-n", // hdr-value used to vary |
|
106 "fresh value with LOAD_NORMAL", // echoed by handler |
|
107 "request3" // value expected to receive in channel |
|
108 ), |
|
109 |
|
110 // Test combination VALIDATE_NEVER && no-store: Load new cache-entry |
|
111 new Test(Ci.nsIRequest.LOAD_NORMAL, |
|
112 "entity-2",// hdr-value used to vary |
|
113 "request4", // echoed by handler |
|
114 "request4", // value expected to receive in channel |
|
115 "no-store" // set no-store on response |
|
116 ), |
|
117 // Ensure we validate without IMS header in this case (verified in handler) |
|
118 new Test(Ci.nsIRequest.VALIDATE_NEVER, |
|
119 "entity-2-v-n",// hdr-value used to vary |
|
120 "request5", // echoed by handler |
|
121 "request5" // value expected to receive in channel |
|
122 ), |
|
123 |
|
124 // Test VALIDATE-ALWAYS: Load new entity |
|
125 new Test(Ci.nsIRequest.LOAD_NORMAL, |
|
126 "entity-3",// hdr-value used to vary |
|
127 "request6", // echoed by handler |
|
128 "request6", // value expected to receive in channel |
|
129 "no-cache" // set no-cache on response |
|
130 ), |
|
131 // Ensure we don't send IMS header also in this case (verified in handler) |
|
132 new Test(Ci.nsIRequest.VALIDATE_ALWAYS, |
|
133 "entity-3-v-a",// hdr-value used to vary |
|
134 "request7", // echoed by handler |
|
135 "request7" // value expected to receive in channel |
|
136 ), |
|
137 ]; |
|
138 |
|
139 function run_next_test() |
|
140 { |
|
141 if (gTests.length == 0) { |
|
142 httpserver.stop(do_test_finished); |
|
143 return; |
|
144 } |
|
145 |
|
146 var test = gTests.shift(); |
|
147 test.run(); |
|
148 } |
|
149 |
|
150 function handler(metadata, response) { |
|
151 |
|
152 // None of the tests above should send an IMS |
|
153 do_check_false(metadata.hasHeader("If-Modified-Since")); |
|
154 |
|
155 // Pick up requested value to echo |
|
156 var hdr = "default value"; |
|
157 try { |
|
158 hdr = metadata.getHeader(VALUE_HDR_NAME); |
|
159 } catch(ex) { } |
|
160 |
|
161 // Pick up requested cache-control header-value |
|
162 var cctrlVal = "max-age=10000"; |
|
163 try { |
|
164 cctrlVal = metadata.getHeader(CACHECTRL_HDR_NAME); |
|
165 } catch(ex) { } |
|
166 |
|
167 response.setStatusLine(metadata.httpVersion, 200, "OK"); |
|
168 response.setHeader("Content-Type", "text/plain", false); |
|
169 response.setHeader("Cache-Control", cctrlVal, false); |
|
170 response.setHeader("Vary", VARY_HDR_NAME, false); |
|
171 response.setHeader("Last-Modified", "Tue, 15 Nov 1994 12:45:26 GMT", false); |
|
172 response.bodyOutputStream.write(hdr, hdr.length); |
|
173 } |
|
174 |
|
175 function run_test() { |
|
176 |
|
177 // clear the cache |
|
178 evict_cache_entries(); |
|
179 |
|
180 httpserver = new HttpServer(); |
|
181 httpserver.registerPathHandler("/bug633743", handler); |
|
182 httpserver.start(-1); |
|
183 |
|
184 run_next_test(); |
|
185 do_test_pending(); |
|
186 } |