OLD | NEW |
1 (function() { | 1 (function() { |
2 var next_cache_index = 1; | 2 var next_cache_index = 1; |
3 | 3 |
4 // Returns a promise that resolves to a newly created Cache object. The | 4 // Returns a promise that resolves to a newly created Cache object. The |
5 // returned Cache will be destroyed when |test| completes. | 5 // returned Cache will be destroyed when |test| completes. |
6 function create_temporary_cache(test) { | 6 function create_temporary_cache(test) { |
7 var uniquifier = String(++next_cache_index); | 7 var uniquifier = String(++next_cache_index); |
8 var cache_name = self.location.pathname + '/' + uniquifier; | 8 var cache_name = self.location.pathname + '/' + uniquifier; |
9 | 9 |
10 test.add_cleanup(function() { | 10 test.add_cleanup(function() { |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 name: 'vary_cookie_absent', | 134 name: 'vary_cookie_absent', |
135 request: new Request('http://example.com/c'), | 135 request: new Request('http://example.com/c'), |
136 response: new Response('', | 136 response: new Response('', |
137 {headers: {'Vary': 'Cookies'}}) | 137 {headers: {'Vary': 'Cookies'}}) |
138 } | 138 } |
139 ]; | 139 ]; |
140 | 140 |
141 // Run |test_function| with a Cache object and a map of entries. Prior to the | 141 // Run |test_function| with a Cache object and a map of entries. Prior to the |
142 // call, the Cache is populated by cache entries from |entries|. The latter is | 142 // call, the Cache is populated by cache entries from |entries|. The latter is |
143 // expected to be an Object mapping arbitrary keys to objects of the form | 143 // expected to be an Object mapping arbitrary keys to objects of the form |
144 // {request: <Request object>, response: <Response object>}. There's no | 144 // {request: <Request object>, response: <Response object>}. The entries are |
145 // guarantee on the order in which entries will be added to the cache. | 145 // added sequentially so that tests can verify the ordering of the cache |
| 146 // methods. |
146 // | 147 // |
147 // |test_function| should return a Promise that can be used with promise_test. | 148 // |test_function| should return a Promise that can be used with promise_test. |
148 function prepopulated_cache_test(entries, test_function, description) { | 149 function prepopulated_cache_test(entries, test_function, description) { |
149 cache_test(function(cache) { | 150 cache_test(function(cache) { |
150 var p = Promise.resolve(); | |
151 var hash = {}; | 151 var hash = {}; |
152 return Promise.all(entries.map(function(entry) { | 152 var resolveMethod = null; |
| 153 |
| 154 var promise = new Promise(function(resolve, reject) { |
| 155 resolveMethod = resolve; |
| 156 }) |
| 157 .then(function() { |
| 158 assert_equals(Object.keys(hash).length, entries.length); |
| 159 return test_function(cache, hash); |
| 160 }); |
| 161 |
| 162 // Add the entries to the cache sequentially. |
| 163 // TODO(jkarlin): Once async/await is available use it to prettify this |
| 164 // code. |
| 165 var i = 0; |
| 166 var processNextEntry = function(i) { |
| 167 if (i == entries.length) { |
| 168 resolveMethod(); |
| 169 return; |
| 170 } |
| 171 entry = entries[i]; |
153 hash[entry.name] = entry; | 172 hash[entry.name] = entry; |
154 return cache.put(entry.request.clone(), | 173 cache.put(entry.request.clone(), entry.response.clone()) |
155 entry.response.clone()) | 174 .then(function() { |
156 .catch(function(e) { | 175 processNextEntry(i+1); |
157 assert_unreached( | 176 }) |
158 'Test setup failed for entry ' + entry.name + ': ' + e); | 177 .catch(function(e) { |
159 }); | 178 assert_unreached( |
160 })) | 179 'Test setup failed for entry ' + entry.name + ': ' + e); |
161 .then(function() { | 180 }) |
162 assert_equals(Object.keys(hash).length, entries.length); | 181 } |
163 }) | 182 |
164 .then(function() { | 183 processNextEntry(0); |
165 return test_function(cache, hash); | 184 return promise; |
166 }); | |
167 }, description); | 185 }, description); |
168 } | 186 } |
169 | 187 |
170 // Helper for testing with Headers objects. Compares Headers instances | 188 // Helper for testing with Headers objects. Compares Headers instances |
171 // by serializing |expected| and |actual| to arrays and comparing. | 189 // by serializing |expected| and |actual| to arrays and comparing. |
172 function assert_header_equals(actual, expected, description) { | 190 function assert_header_equals(actual, expected, description) { |
173 assert_class_string(actual, "Headers", description); | 191 assert_class_string(actual, "Headers", description); |
174 var header; | 192 var header; |
175 var actual_headers = []; | 193 var actual_headers = []; |
176 var expected_headers = []; | 194 var expected_headers = []; |
(...skipping 10 matching lines...) Expand all Loading... |
187 // does not compare the response bodies. | 205 // does not compare the response bodies. |
188 function assert_response_equals(actual, expected, description) { | 206 function assert_response_equals(actual, expected, description) { |
189 assert_class_string(actual, "Response", description); | 207 assert_class_string(actual, "Response", description); |
190 ["type", "url", "status", "ok", "statusText"].forEach(function(attribute) { | 208 ["type", "url", "status", "ok", "statusText"].forEach(function(attribute) { |
191 assert_equals(actual[attribute], expected[attribute], | 209 assert_equals(actual[attribute], expected[attribute], |
192 description + " Attributes differ: " + attribute + "."); | 210 description + " Attributes differ: " + attribute + "."); |
193 }); | 211 }); |
194 assert_header_equals(actual.headers, expected.headers, description); | 212 assert_header_equals(actual.headers, expected.headers, description); |
195 } | 213 } |
196 | 214 |
197 // Assert that the two arrays |actual| and |expected| contain the same | |
198 // set of Responses as determined by assert_response_equals. The order | |
199 // is not significant. | |
200 // | |
201 // |expected| is assumed to not contain any duplicates. | |
202 function assert_response_array_equivalent(actual, expected, description) { | |
203 assert_true(Array.isArray(actual), description); | |
204 assert_equals(actual.length, expected.length, description); | |
205 expected.forEach(function(expected_element) { | |
206 // assert_response_in_array treats the first argument as being | |
207 // 'actual', and the second as being 'expected array'. We are | |
208 // switching them around because we want to be resilient | |
209 // against the |actual| array containing duplicates. | |
210 assert_response_in_array(expected_element, actual, description); | |
211 }); | |
212 } | |
213 | |
214 // Asserts that two arrays |actual| and |expected| contain the same | 215 // Asserts that two arrays |actual| and |expected| contain the same |
215 // set of Responses as determined by assert_response_equals(). The | 216 // set of Responses as determined by assert_response_equals(). The |
216 // corresponding elements must occupy corresponding indices in their | 217 // corresponding elements must occupy corresponding indices in their |
217 // respective arrays. | 218 // respective arrays. |
218 function assert_response_array_equals(actual, expected, description) { | 219 function assert_response_array_equals(actual, expected, description) { |
219 assert_true(Array.isArray(actual), description); | 220 assert_true(Array.isArray(actual), description); |
220 assert_equals(actual.length, expected.length, description); | 221 assert_equals(actual.length, expected.length, description); |
221 actual.forEach(function(value, index) { | 222 actual.forEach(function(value, index) { |
222 assert_response_equals(value, expected[index], | 223 assert_response_equals(value, expected[index], |
223 description + " : object[" + index + "]"); | 224 description + " : object[" + index + "]"); |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
284 function assert_request_in_array(actual, expected_array, description) { | 285 function assert_request_in_array(actual, expected_array, description) { |
285 assert_true(expected_array.some(function(element) { | 286 assert_true(expected_array.some(function(element) { |
286 try { | 287 try { |
287 assert_request_equals(actual, element); | 288 assert_request_equals(actual, element); |
288 return true; | 289 return true; |
289 } catch (e) { | 290 } catch (e) { |
290 return false; | 291 return false; |
291 } | 292 } |
292 }), description); | 293 }), description); |
293 } | 294 } |
OLD | NEW |