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 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 }); | 178 }); |
179 })) | 179 })) |
180 .then(function() { | 180 .then(function() { |
181 assert_equals(Object.keys(hash).length, entries.length); | 181 assert_equals(Object.keys(hash).length, entries.length); |
182 }) | 182 }) |
183 .then(function() { | 183 .then(function() { |
184 return test_function(cache, hash); | 184 return test_function(cache, hash); |
185 }); | 185 }); |
186 }, description); | 186 }, description); |
187 } | 187 } |
| 188 |
| 189 // Helper for testing with Headers objects. Compares Headers instances |
| 190 // by serializing |expected| and |actual| to arrays and comparing. |
| 191 function assert_header_equals(actual, expected, description) { |
| 192 assert_class_string(actual, "Headers", description); |
| 193 var header; |
| 194 var actual_headers = []; |
| 195 var expected_headers = []; |
| 196 for (header of actual) |
| 197 actual_headers.push(header[0] + ": " + header[1]); |
| 198 for (header of expected) |
| 199 expected_headers.push(header[0] + ": " + header[1]); |
| 200 assert_array_equals(actual_headers, expected_headers, |
| 201 description + " Headers differ."); |
| 202 } |
| 203 |
| 204 // Helper for testing with Response objects. Compares simple |
| 205 // attributes defined on the interfaces, as well as the headers. It |
| 206 // does not compare the response bodies. |
| 207 function assert_response_equals(actual, expected, description) { |
| 208 assert_class_string(actual, "Response", description); |
| 209 ["type", "url", "status", "ok", "statusText"].forEach(function(attribute) { |
| 210 assert_equals(actual[attribute], expected[attribute], |
| 211 description + " Attributes differ: " + attribute + "."); |
| 212 }); |
| 213 assert_header_equals(actual.headers, expected.headers, description); |
| 214 } |
| 215 |
| 216 // Assert that the two arrays |actual| and |expected| contain the same |
| 217 // set of Responses as determined by assert_response_equals. The order |
| 218 // is not significant. |
| 219 // |
| 220 // |expected| is assumed to not contain any duplicates. |
| 221 function assert_response_array_equivalent(actual, expected, description) { |
| 222 assert_true(Array.isArray(actual), description); |
| 223 assert_equals(actual.length, expected.length, description); |
| 224 expected.forEach(function(expected_element) { |
| 225 // assert_response_in_array treats the first argument as being |
| 226 // 'actual', and the second as being 'expected array'. We are |
| 227 // switching them around because we want to be resilient |
| 228 // against the |actual| array containing duplicates. |
| 229 assert_response_in_array(expected_element, actual, description); |
| 230 }); |
| 231 } |
| 232 |
| 233 // Asserts that two arrays |actual| and |expected| contain the same |
| 234 // set of Responses as determined by assert_response_equals(). The |
| 235 // corresponding elements must occupy corresponding indices in their |
| 236 // respective arrays. |
| 237 function assert_response_array_equals(actual, expected, description) { |
| 238 assert_true(Array.isArray(actual), description); |
| 239 assert_equals(actual.length, expected.length, description); |
| 240 actual.forEach(function(value, index) { |
| 241 assert_response_equals(value, expected[index], |
| 242 description + " : object[" + index + "]"); |
| 243 }); |
| 244 } |
| 245 |
| 246 // Equivalent to assert_in_array, but uses assert_response_equals. |
| 247 function assert_response_in_array(actual, expected_array, description) { |
| 248 assert_true(expected_array.some(function(element) { |
| 249 try { |
| 250 assert_response_equals(actual, element); |
| 251 return true; |
| 252 } catch (e) { |
| 253 return false; |
| 254 } |
| 255 }), description); |
| 256 } |
OLD | NEW |