3 const { HttpServer
} = ChromeUtils
.importESModule(
4 "resource://testing-common/httpd.sys.mjs"
15 var httpserver
= new HttpServer();
18 var base_url
= "http://localhost:" + httpserver
.identity
.primaryPort
;
19 var resource
= "/resource";
20 var resource_url
= base_url
+ resource
;
23 var hit_server
= false;
25 function make_channel(aUrl
) {
26 // Reset test global status
29 var req
= NetUtil
.newChannel({ uri
: aUrl
, loadUsingSystemPrincipal
: true });
30 req
.QueryInterface(Ci
.nsIHttpChannel
);
31 req
.setRequestHeader("If-Modified-Since", request_time
, false);
35 function make_uri(aUrl
) {
36 return Services
.io
.newURI(aUrl
);
39 function resource_handler(aMetadata
, aResponse
) {
41 Assert
.ok(aMetadata
.hasHeader("If-Modified-Since"));
42 Assert
.equal(aMetadata
.getHeader("If-Modified-Since"), request_time
);
44 if (response_code
== "200") {
45 aResponse
.setStatusLine(aMetadata
.httpVersion
, 200, "OK");
46 aResponse
.setHeader("Content-Type", "text/plain", false);
47 aResponse
.setHeader("Last-Modified", response_time
, false);
49 aResponse
.bodyOutputStream
.write(response_body
, response_body
.length
);
50 } else if (response_code
== "304") {
51 aResponse
.setStatusLine(aMetadata
.httpVersion
, 304, "Not Modified");
52 aResponse
.setHeader("Returned-From-Handler", "1");
56 function check_cached_data(aCachedData
, aCallback
) {
60 Ci
.nsICacheStorage
.OPEN_READONLY
,
62 function (aStatus
, aEntry
) {
63 Assert
.equal(aStatus
, Cr
.NS_OK
);
64 pumpReadStream(aEntry
.openInputStream(0), function (aData
) {
65 Assert
.equal(aData
, aCachedData
);
74 evict_cache_entries();
78 cache_storage
= getCacheStorage("disk");
79 httpserver
.registerPathHandler(resource
, resource_handler
);
81 wait_for_cache_index(run_next_test
);
84 // 1. send custom conditional request when we don't have an entry
85 // server returns 304 -> client receives 304
87 response_code
= "304";
89 request_time
= "Thu, 1 Jan 2009 00:00:00 GMT";
90 response_time
= "Thu, 1 Jan 2009 00:00:00 GMT";
92 var ch
= make_channel(resource_url
);
94 new ChannelListener(function (aRequest
) {
95 syncWithCacheIOThread(() => {
96 Assert
.ok(hit_server
);
98 aRequest
.QueryInterface(Ci
.nsIHttpChannel
).responseStatus
,
101 Assert
.ok(!cache_storage
.exists(make_uri(resource_url
), ""));
102 Assert
.equal(aRequest
.getResponseHeader("Returned-From-Handler"), "1");
110 // 2. send custom conditional request when we don't have an entry
111 // server returns 200 -> result is cached
113 response_code
= "200";
114 response_body
= "content_body";
115 request_time
= "Thu, 1 Jan 2009 00:00:00 GMT";
116 response_time
= "Fri, 2 Jan 2009 00:00:00 GMT";
118 var ch
= make_channel(resource_url
);
120 new ChannelListener(function (aRequest
) {
121 syncWithCacheIOThread(() => {
122 Assert
.ok(hit_server
);
124 aRequest
.QueryInterface(Ci
.nsIHttpChannel
).responseStatus
,
127 Assert
.ok(cache_storage
.exists(make_uri(resource_url
), ""));
129 check_cached_data(response_body
, run_next_test
);
135 // 3. send custom conditional request when we have an entry
136 // server returns 304 -> client receives 304 and cached entry is unchanged
138 response_code
= "304";
139 var cached_body
= response_body
;
141 request_time
= "Fri, 2 Jan 2009 00:00:00 GMT";
142 response_time
= "Fri, 2 Jan 2009 00:00:00 GMT";
144 var ch
= make_channel(resource_url
);
146 new ChannelListener(function (aRequest
, aData
) {
147 syncWithCacheIOThread(() => {
148 Assert
.ok(hit_server
);
150 aRequest
.QueryInterface(Ci
.nsIHttpChannel
).responseStatus
,
153 Assert
.ok(cache_storage
.exists(make_uri(resource_url
), ""));
154 Assert
.equal(aRequest
.getResponseHeader("Returned-From-Handler"), "1");
155 Assert
.equal(aData
, "");
157 // Check the cache data is not changed
158 check_cached_data(cached_body
, run_next_test
);
164 // 4. send custom conditional request when we have an entry
165 // server returns 200 -> result is cached
167 response_code
= "200";
168 response_body
= "updated_content_body";
169 request_time
= "Fri, 2 Jan 2009 00:00:00 GMT";
170 response_time
= "Sat, 3 Jan 2009 00:00:00 GMT";
171 var ch
= make_channel(resource_url
);
173 new ChannelListener(function (aRequest
) {
174 syncWithCacheIOThread(() => {
175 Assert
.ok(hit_server
);
177 aRequest
.QueryInterface(Ci
.nsIHttpChannel
).responseStatus
,
180 Assert
.ok(cache_storage
.exists(make_uri(resource_url
), ""));
182 // Check the cache data is updated
183 check_cached_data(response_body
, () => {
185 httpserver
.stop(do_test_finished
);