Bug 1931425 - Limit how often moz-label's #setStyles runs r=reusable-components-revie...
[gecko.git] / netwerk / test / unit / test_bug482934.js
blob8388c35d2b3ecfe3c093a2794cddbe5dc3d7ff38
1 "use strict";
3 const { HttpServer } = ChromeUtils.importESModule(
4 "resource://testing-common/httpd.sys.mjs"
5 );
7 var response_code;
8 var response_body;
10 var request_time;
11 var response_time;
13 var cache_storage;
15 var httpserver = new HttpServer();
16 httpserver.start(-1);
18 var base_url = "http://localhost:" + httpserver.identity.primaryPort;
19 var resource = "/resource";
20 var resource_url = base_url + resource;
22 // Test flags
23 var hit_server = false;
25 function make_channel(aUrl) {
26 // Reset test global status
27 hit_server = false;
29 var req = NetUtil.newChannel({ uri: aUrl, loadUsingSystemPrincipal: true });
30 req.QueryInterface(Ci.nsIHttpChannel);
31 req.setRequestHeader("If-Modified-Since", request_time, false);
32 return req;
35 function make_uri(aUrl) {
36 return Services.io.newURI(aUrl);
39 function resource_handler(aMetadata, aResponse) {
40 hit_server = true;
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) {
57 asyncOpenCacheEntry(
58 resource_url,
59 "disk",
60 Ci.nsICacheStorage.OPEN_READONLY,
61 null,
62 function (aStatus, aEntry) {
63 Assert.equal(aStatus, Cr.NS_OK);
64 pumpReadStream(aEntry.openInputStream(0), function (aData) {
65 Assert.equal(aData, aCachedData);
66 aCallback();
67 });
72 function run_test() {
73 do_get_profile();
74 evict_cache_entries();
76 do_test_pending();
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
86 add_test(() => {
87 response_code = "304";
88 response_body = "";
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);
93 ch.asyncOpen(
94 new ChannelListener(function (aRequest) {
95 syncWithCacheIOThread(() => {
96 Assert.ok(hit_server);
97 Assert.equal(
98 aRequest.QueryInterface(Ci.nsIHttpChannel).responseStatus,
99 304
101 Assert.ok(!cache_storage.exists(make_uri(resource_url), ""));
102 Assert.equal(aRequest.getResponseHeader("Returned-From-Handler"), "1");
104 run_next_test();
105 }, true);
106 }, null)
110 // 2. send custom conditional request when we don't have an entry
111 // server returns 200 -> result is cached
112 add_test(() => {
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);
119 ch.asyncOpen(
120 new ChannelListener(function (aRequest) {
121 syncWithCacheIOThread(() => {
122 Assert.ok(hit_server);
123 Assert.equal(
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);
130 }, true);
131 }, null)
135 // 3. send custom conditional request when we have an entry
136 // server returns 304 -> client receives 304 and cached entry is unchanged
137 add_test(() => {
138 response_code = "304";
139 var cached_body = response_body;
140 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);
145 ch.asyncOpen(
146 new ChannelListener(function (aRequest, aData) {
147 syncWithCacheIOThread(() => {
148 Assert.ok(hit_server);
149 Assert.equal(
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);
159 }, true);
160 }, null)
164 // 4. send custom conditional request when we have an entry
165 // server returns 200 -> result is cached
166 add_test(() => {
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);
172 ch.asyncOpen(
173 new ChannelListener(function (aRequest) {
174 syncWithCacheIOThread(() => {
175 Assert.ok(hit_server);
176 Assert.equal(
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, () => {
184 run_next_test();
185 httpserver.stop(do_test_finished);
187 }, true);
188 }, null)