Chromium Code Reviews| Index: LayoutTests/http/tests/cachestorage/resources/test-helpers.js |
| diff --git a/LayoutTests/http/tests/cachestorage/resources/test-helpers.js b/LayoutTests/http/tests/cachestorage/resources/test-helpers.js |
| index 91110950d568d5cb7b1808fa2c6cb2de947a02de..a616a436175a64fe31390f86a7a9547af7cbc00e 100644 |
| --- a/LayoutTests/http/tests/cachestorage/resources/test-helpers.js |
| +++ b/LayoutTests/http/tests/cachestorage/resources/test-helpers.js |
| @@ -35,3 +35,70 @@ function cache_test(test_function, description) { |
| .then(test_function); |
| }, description); |
| } |
| + |
| +// Helper for testing with Headers objects. Compares Headers instances |
| +// by serializing |expected| and |actual| to arrays and comparing. |
| +function assert_header_equals(actual, expected, description) { |
| + assert_class_string(actual, "Headers", description); |
| + var header, actual_headers = [], expected_headers = []; |
|
jkarlin
2015/08/21 14:50:44
Should these be on separate lines?
jsbell
2015/08/21 18:05:50
Done. (I'll try to remember to tweak that upstream
|
| + for (header of actual) |
| + actual_headers.push(header[0] + ": " + header[1]); |
| + for (header of expected) |
| + expected_headers.push(header[0] + ": " + header[1]); |
| + assert_array_equals(actual_headers, expected_headers, |
| + description + " Headers differ."); |
| +} |
| + |
| +// Helper for testing with Response objects. Compares simple |
| +// attributes defined on the interfaces, as well as the headers. It |
| +// does not compare the response bodies. |
| +function assert_response_equals(actual, expected, description) { |
| + assert_class_string(actual, "Response", description); |
| + ["type", "url", "status", "ok", "statusText"].forEach(function(attribute) { |
| + assert_equals(actual[attribute], expected[attribute], |
| + description + " Attributes differ: " + attribute + "."); |
| + }); |
| + assert_header_equals(actual.headers, expected.headers, description); |
| +} |
| + |
| +// Assert that the two arrays |actual| and |expected| contain the same |
| +// set of Responses as determined by assert_response_equals. The order |
| +// is not significant. |
| +// |
| +// |expected| is assumed to not contain any duplicates. |
| +function assert_response_array_equivalent(actual, expected, description) { |
| + assert_true(Array.isArray(actual), description); |
| + assert_equals(actual.length, expected.length, description); |
| + expected.forEach(function(expected_element) { |
| + // assert_response_in_array treats the first argument as being |
| + // 'actual', and the second as being 'expected array'. We are |
| + // switching them around because we want to be resilient |
| + // against the |actual| array containing duplicates. |
| + assert_response_in_array(expected_element, actual, description); |
| + }); |
| +} |
| + |
| +// Asserts that two arrays |actual| and |expected| contain the same |
| +// set of Responses as determined by assert_response_equals(). The |
| +// corresponding elements must occupy corresponding indices in their |
| +// respective arrays. |
| +function assert_response_array_equals(actual, expected, description) { |
| + assert_true(Array.isArray(actual), description); |
| + assert_equals(actual.length, expected.length, description); |
| + actual.forEach(function(value, index) { |
| + assert_response_equals(value, expected[index], |
| + description + " : object[" + index + "]"); |
| + }); |
| +} |
| + |
| +// Equivalent to assert_in_array, but uses assert_response_equals. |
| +function assert_response_in_array(actual, expected_array, description) { |
| + assert_true(expected_array.some(function(element) { |
| + try { |
| + assert_response_equals(actual, element); |
| + return true; |
| + } catch (e) { |
| + return false; |
| + } |
| + }), description); |
| +} |