2 var next_cache_index
= 1;
4 // Returns a promise that resolves to a newly created Cache object. The
5 // returned Cache will be destroyed when |test| completes.
6 function create_temporary_cache(test
) {
7 var uniquifier
= String(++next_cache_index
);
8 var cache_name
= self
.location
.pathname
+ '/' + uniquifier
;
10 test
.add_cleanup(function() {
11 self
.caches
.delete(cache_name
);
14 return self
.caches
.delete(cache_name
)
16 return self
.caches
.open(cache_name
);
20 self
.create_temporary_cache
= create_temporary_cache
;
23 // Runs |test_function| with a temporary unique Cache passed in as the only
24 // argument. The function is run as a part of Promise chain owned by
25 // promise_test(). As such, it is expected to behave in a manner identical (with
26 // the exception of the argument) to a function passed into promise_test().
29 // cache_test(function(cache) {
30 // // Do something with |cache|, which is a Cache object.
31 // }, "Some Cache test");
32 function cache_test(test_function
, description
) {
33 promise_test(function(test
) {
34 return create_temporary_cache(test
)
39 // A set of Request/Response pairs to be used with prepopulated_cache_test().
40 var simple_entries
= [
43 request
: new Request('http://example.com/a'),
44 response
: new Response('')
49 request
: new Request('http://example.com/b'),
50 response
: new Response('')
55 request
: new Request('http://example.com/a?q=r'),
56 response
: new Response('')
61 request
: new Request('http://example.com/A'),
62 response
: new Response('')
67 request
: new Request('https://example.com/a'),
68 response
: new Response('')
73 request
: new Request('http://example.org/a'),
74 response
: new Response('')
79 request
: new Request('http://example.com/cat'),
80 response
: new Response('')
85 request
: new Request('http://example.com/catmandu'),
86 response
: new Response('')
90 name
: 'cat_num_lives',
91 request
: new Request('http://example.com/cat?lives=9'),
92 response
: new Response('')
96 name
: 'cat_in_the_hat',
97 request
: new Request('http://example.com/cat/in/the/hat'),
98 response
: new Response('')
103 request
: new Request('http://tom:jerry@example.com/cat'),
104 response
: new Response('')
108 name
: 'top_secret_cat',
109 request
: new Request('http://tom:j3rry@example.com/cat'),
110 response
: new Response('')
113 name
: 'non_2xx_response',
114 request
: new Request('http://example.com/non2xx'),
115 response
: new Response('', {status
: 404, statusText
: 'nope'})
119 name
: 'error_response',
120 request
: new Request('http://example.com/error'),
121 response
: Response
.error()
125 // A set of Request/Response pairs to be used with prepopulated_cache_test().
126 // These contain a mix of test cases that use Vary headers.
129 name
: 'vary_cookie_is_cookie',
130 request
: new Request('http://example.com/c',
131 {headers
: {'Cookies': 'is-for-cookie'}}),
132 response
: new Response('',
133 {headers
: {'Vary': 'Cookies'}})
137 name
: 'vary_cookie_is_good',
138 request
: new Request('http://example.com/c',
139 {headers
: {'Cookies': 'is-good-enough-for-me'}}),
140 response
: new Response('',
141 {headers
: {'Vary': 'Cookies'}})
145 name
: 'vary_cookie_absent',
146 request
: new Request('http://example.com/c'),
147 response
: new Response('',
148 {headers
: {'Vary': 'Cookies'}})
152 name
: 'vary_wildcard',
153 request
: new Request('http://example.com/c',
154 {headers
: {'Cookies': 'x', 'X-Key': '1'}}),
155 response
: new Response('',
156 {headers
: {'Vary': '*'}})
160 // Run |test_function| with a Cache object and a map of entries. Prior to the
161 // call, the Cache is populated by cache entries from |entries|. The latter is
162 // expected to be an Object mapping arbitrary keys to objects of the form
163 // {request: <Request object>, response: <Response object>}. There's no
164 // guarantee on the order in which entries will be added to the cache.
166 // |test_function| should return a Promise that can be used with promise_test.
167 function prepopulated_cache_test(entries
, test_function
, description
) {
168 cache_test(function(cache
) {
169 var p
= Promise
.resolve();
171 return Promise
.all(entries
.map(function(entry
) {
172 hash
[entry
.name
] = entry
;
173 return cache
.put(entry
.request
.clone(),
174 entry
.response
.clone())
177 'Test setup failed for entry ' + entry
.name
+ ': ' + e
);
181 assert_equals(Object
.keys(hash
).length
, entries
.length
);
184 return test_function(cache
, hash
);
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
);
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.");
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
+ ".");
213 assert_header_equals(actual
.headers
, expected
.headers
, description
);
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.
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
);
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
+ "]");
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
) {
250 assert_response_equals(actual
, element
);