3 const { HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js");
13 var httpserver = new HttpServer();
16 var base_url = "http://localhost:" + httpserver.identity.primaryPort;
17 var resource = "/resource";
18 var resource_url = base_url + resource;
21 var hit_server = false;
23 function make_channel(aUrl) {
24 // Reset test global status
27 var req = NetUtil.newChannel({ uri: aUrl, loadUsingSystemPrincipal: true });
28 req.QueryInterface(Ci.nsIHttpChannel);
29 req.setRequestHeader("If-Modified-Since", request_time, false);
33 function make_uri(aUrl) {
34 return Services.io.newURI(aUrl);
37 function resource_handler(aMetadata, aResponse) {
39 Assert.ok(aMetadata.hasHeader("If-Modified-Since"));
40 Assert.equal(aMetadata.getHeader("If-Modified-Since"), request_time);
42 if (response_code == "200") {
43 aResponse.setStatusLine(aMetadata.httpVersion, 200, "OK");
44 aResponse.setHeader("Content-Type", "text/plain", false);
45 aResponse.setHeader("Last-Modified", response_time, false);
47 aResponse.bodyOutputStream.write(response_body, response_body.length);
48 } else if (response_code == "304") {
49 aResponse.setStatusLine(aMetadata.httpVersion, 304, "Not Modified");
50 aResponse.setHeader("Returned-From-Handler", "1");
54 function check_cached_data(aCachedData, aCallback) {
58 Ci.nsICacheStorage.OPEN_READONLY,
60 function(aStatus, aEntry) {
61 Assert.equal(aStatus, Cr.NS_OK);
62 pumpReadStream(aEntry.openInputStream(0), function(aData) {
63 Assert.equal(aData, aCachedData);
72 evict_cache_entries();
76 cache_storage = getCacheStorage("disk");
77 httpserver.registerPathHandler(resource, resource_handler);
79 wait_for_cache_index(run_next_test);
82 // 1. send custom conditional request when we don't have an entry
83 // server returns 304 -> client receives 304
85 response_code = "304";
87 request_time = "Thu, 1 Jan 2009 00:00:00 GMT";
88 response_time = "Thu, 1 Jan 2009 00:00:00 GMT";
90 var ch = make_channel(resource_url);
92 new ChannelListener(function(aRequest, aData) {
93 syncWithCacheIOThread(() => {
94 Assert.ok(hit_server);
96 aRequest.QueryInterface(Ci.nsIHttpChannel).responseStatus,
99 Assert.ok(!cache_storage.exists(make_uri(resource_url), ""));
100 Assert.equal(aRequest.getResponseHeader("Returned-From-Handler"), "1");
108 // 2. send custom conditional request when we don't have an entry
109 // server returns 200 -> result is cached
111 response_code = "200";
112 response_body = "content_body";
113 request_time = "Thu, 1 Jan 2009 00:00:00 GMT";
114 response_time = "Fri, 2 Jan 2009 00:00:00 GMT";
116 var ch = make_channel(resource_url);
118 new ChannelListener(function(aRequest, aData) {
119 syncWithCacheIOThread(() => {
120 Assert.ok(hit_server);
122 aRequest.QueryInterface(Ci.nsIHttpChannel).responseStatus,
125 Assert.ok(cache_storage.exists(make_uri(resource_url), ""));
127 check_cached_data(response_body, run_next_test);
133 // 3. send custom conditional request when we have an entry
134 // server returns 304 -> client receives 304 and cached entry is unchanged
136 response_code = "304";
137 var cached_body = response_body;
139 request_time = "Fri, 2 Jan 2009 00:00:00 GMT";
140 response_time = "Fri, 2 Jan 2009 00:00:00 GMT";
142 var ch = make_channel(resource_url);
144 new ChannelListener(function(aRequest, aData) {
145 syncWithCacheIOThread(() => {
146 Assert.ok(hit_server);
148 aRequest.QueryInterface(Ci.nsIHttpChannel).responseStatus,
151 Assert.ok(cache_storage.exists(make_uri(resource_url), ""));
152 Assert.equal(aRequest.getResponseHeader("Returned-From-Handler"), "1");
153 Assert.equal(aData, "");
155 // Check the cache data is not changed
156 check_cached_data(cached_body, run_next_test);
162 // 4. send custom conditional request when we have an entry
163 // server returns 200 -> result is cached
165 response_code = "200";
166 response_body = "updated_content_body";
167 request_time = "Fri, 2 Jan 2009 00:00:00 GMT";
168 response_time = "Sat, 3 Jan 2009 00:00:00 GMT";
169 var ch = make_channel(resource_url);
171 new ChannelListener(function(aRequest, aData) {
172 syncWithCacheIOThread(() => {
173 Assert.ok(hit_server);
175 aRequest.QueryInterface(Ci.nsIHttpChannel).responseStatus,
178 Assert.ok(cache_storage.exists(make_uri(resource_url), ""));
180 // Check the cache data is updated
181 check_cached_data(response_body, () => {
183 httpserver.stop(do_test_finished);