Chromium Code Reviews| 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 p = Promise.resolve(); |
|
nhiroki
2016/09/08 03:54:56
Is this used?
jkarlin
2016/09/08 12:03:13
Nope, thanks!
| |
| 151 var hash = {}; | 152 var hash = {}; |
| 152 return Promise.all(entries.map(function(entry) { | 153 var resolveMethod = null; |
| 154 | |
| 155 var promise = new Promise(function(resolve, reject) { | |
| 156 resolveMethod = resolve; | |
| 157 }) | |
| 158 .then(function() { | |
| 159 assert_equals(Object.keys(hash).length, entries.length); | |
| 160 }) | |
| 161 .then(function() { | |
| 162 return test_function(cache, hash); | |
|
nhiroki
2016/09/08 03:54:56
You could combine this '.then' with the previous '
jkarlin
2016/09/08 12:03:13
Done.
| |
| 163 }); | |
| 164 | |
| 165 // Add the entries to the cache sequentially. | |
| 166 // TODO(jkarlin): Once async/await is available use it to prettify this | |
| 167 // code. | |
| 168 var i = 0; | |
| 169 var processNextEntry = function(i) { | |
| 170 if (i == entries.length) { | |
| 171 resolveMethod(); | |
| 172 return; | |
| 173 } | |
| 174 entry = entries[i]; | |
| 153 hash[entry.name] = entry; | 175 hash[entry.name] = entry; |
| 154 return cache.put(entry.request.clone(), | 176 cache.put(entry.request.clone(), entry.response.clone()) |
| 155 entry.response.clone()) | 177 .then(function() { |
| 156 .catch(function(e) { | 178 processNextEntry(i+1); |
| 157 assert_unreached( | 179 }) |
| 158 'Test setup failed for entry ' + entry.name + ': ' + e); | 180 .catch(function(e) { |
| 159 }); | 181 assert_unreached( |
| 160 })) | 182 'Test setup failed for entry ' + entry.name + ': ' + e); |
|
nhiroki
2016/09/08 03:54:56
+2-spaces indent for an argument?
jkarlin
2016/09/08 12:03:13
Done.
| |
| 161 .then(function() { | 183 }) |
| 162 assert_equals(Object.keys(hash).length, entries.length); | 184 } |
| 163 }) | 185 |
| 164 .then(function() { | 186 processNextEntry(0); |
| 165 return test_function(cache, hash); | 187 return promise; |
| 166 }); | |
| 167 }, description); | 188 }, description); |
| 168 } | 189 } |
| 169 | 190 |
| 170 // Helper for testing with Headers objects. Compares Headers instances | 191 // Helper for testing with Headers objects. Compares Headers instances |
| 171 // by serializing |expected| and |actual| to arrays and comparing. | 192 // by serializing |expected| and |actual| to arrays and comparing. |
| 172 function assert_header_equals(actual, expected, description) { | 193 function assert_header_equals(actual, expected, description) { |
| 173 assert_class_string(actual, "Headers", description); | 194 assert_class_string(actual, "Headers", description); |
| 174 var header; | 195 var header; |
| 175 var actual_headers = []; | 196 var actual_headers = []; |
| 176 var expected_headers = []; | 197 var expected_headers = []; |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 187 // does not compare the response bodies. | 208 // does not compare the response bodies. |
| 188 function assert_response_equals(actual, expected, description) { | 209 function assert_response_equals(actual, expected, description) { |
| 189 assert_class_string(actual, "Response", description); | 210 assert_class_string(actual, "Response", description); |
| 190 ["type", "url", "status", "ok", "statusText"].forEach(function(attribute) { | 211 ["type", "url", "status", "ok", "statusText"].forEach(function(attribute) { |
| 191 assert_equals(actual[attribute], expected[attribute], | 212 assert_equals(actual[attribute], expected[attribute], |
| 192 description + " Attributes differ: " + attribute + "."); | 213 description + " Attributes differ: " + attribute + "."); |
| 193 }); | 214 }); |
| 194 assert_header_equals(actual.headers, expected.headers, description); | 215 assert_header_equals(actual.headers, expected.headers, description); |
| 195 } | 216 } |
| 196 | 217 |
| 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 | 218 // Asserts that two arrays |actual| and |expected| contain the same |
| 215 // set of Responses as determined by assert_response_equals(). The | 219 // set of Responses as determined by assert_response_equals(). The |
| 216 // corresponding elements must occupy corresponding indices in their | 220 // corresponding elements must occupy corresponding indices in their |
| 217 // respective arrays. | 221 // respective arrays. |
| 218 function assert_response_array_equals(actual, expected, description) { | 222 function assert_response_array_equals(actual, expected, description) { |
| 219 assert_true(Array.isArray(actual), description); | 223 assert_true(Array.isArray(actual), description); |
| 220 assert_equals(actual.length, expected.length, description); | 224 assert_equals(actual.length, expected.length, description); |
| 221 actual.forEach(function(value, index) { | 225 actual.forEach(function(value, index) { |
| 222 assert_response_equals(value, expected[index], | 226 assert_response_equals(value, expected[index], |
| 223 description + " : object[" + index + "]"); | 227 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) { | 288 function assert_request_in_array(actual, expected_array, description) { |
| 285 assert_true(expected_array.some(function(element) { | 289 assert_true(expected_array.some(function(element) { |
| 286 try { | 290 try { |
| 287 assert_request_equals(actual, element); | 291 assert_request_equals(actual, element); |
| 288 return true; | 292 return true; |
| 289 } catch (e) { | 293 } catch (e) { |
| 290 return false; | 294 return false; |
| 291 } | 295 } |
| 292 }), description); | 296 }), description); |
| 293 } | 297 } |
| OLD | NEW |