1 if (self.importScripts) {
2 importScripts('/resources/testharness.js');
3 importScripts('/resources/testharness-helpers.js');
4 importScripts('../resources/test-helpers.js');
7 prepopulated_cache_test(simple_entries, function(cache, entries) {
8 return cache.match('not-present-in-the-cache')
9 .then(function(result) {
10 assert_equals(result, undefined,
11 'Cache.match failures should resolve with undefined.');
13 }, 'Cache.match with no matching entries');
15 prepopulated_cache_test(simple_entries, function(cache, entries) {
16 return cache.match(entries.a.request.url)
17 .then(function(result) {
18 assert_response_equals(result, entries.a.response,
19 'Cache.match should match by URL.');
21 }, 'Cache.match with URL');
23 prepopulated_cache_test(simple_entries, function(cache, entries) {
24 return cache.match(entries.a.request)
25 .then(function(result) {
26 assert_response_equals(result, entries.a.response,
27 'Cache.match should match by Request.');
29 }, 'Cache.match with Request');
31 prepopulated_cache_test(simple_entries, function(cache, entries) {
32 return cache.match(new Request(entries.a.request.url))
33 .then(function(result) {
34 assert_response_equals(result, entries.a.response,
35 'Cache.match should match by Request.');
37 }, 'Cache.match with new Request');
39 prepopulated_cache_test(simple_entries, function(cache, entries) {
40 return cache.match(entries.a.request,
42 .then(function(result) {
43 assert_response_in_array(
47 entries.a_with_query.response
49 'Cache.match with ignoreSearch should ignore the ' +
50 'search parameters of cached request.');
53 'Cache.match with ignoreSearch option (request with no search ' +
56 prepopulated_cache_test(simple_entries, function(cache, entries) {
57 return cache.match(entries.a_with_query.request,
59 .then(function(result) {
60 assert_response_in_array(
64 entries.a_with_query.response
66 'Cache.match with ignoreSearch should ignore the ' +
67 'search parameters of request.');
70 'Cache.match with ignoreSearch option (request with search parameter)');
72 prepopulated_cache_test(simple_entries, function(cache, entries) {
73 return cache.match(entries.cat.request.url + '#mouse')
74 .then(function(result) {
75 assert_response_equals(result, entries.cat.response,
76 'Cache.match should ignore URL fragment.');
78 }, 'Cache.match with URL containing fragment');
80 prepopulated_cache_test(simple_entries, function(cache, entries) {
81 return cache.match('http')
82 .then(function(result) {
85 'Cache.match should treat query as a URL and not ' +
86 'just a string fragment.');
88 }, 'Cache.match with string fragment "http" as query');
90 prepopulated_cache_test(simple_entries, function(cache, entries) {
91 return cache.match(entries.secret_cat.request.url)
92 .then(function(result) {
93 assert_response_equals(
94 result, entries.secret_cat.response,
95 'Cache.match should not ignore embedded credentials');
97 }, 'Cache.match with URL containing credentials');
99 prepopulated_cache_test(vary_entries, function(cache, entries) {
100 return cache.match('http://example.com/c')
101 .then(function(result) {
102 assert_response_in_array(
105 entries.vary_wildcard.response,
106 entries.vary_cookie_absent.response
108 'Cache.match should honor "Vary" header.');
110 }, 'Cache.match with responses containing "Vary" header');
112 cache_test(function(cache) {
113 var request = new Request('http://example.com');
115 var request_url = new URL('../resources/simple.txt', location.href).href;
116 return fetch(request_url)
117 .then(function(fetch_result) {
118 response = fetch_result;
120 response.url, request_url,
121 '[https://fetch.spec.whatwg.org/#dom-response-url] ' +
122 'Reponse.url should return the URL of the response.');
123 return cache.put(request, response.clone());
126 return cache.match(request.url);
128 .then(function(result) {
129 assert_response_equals(
131 'Cache.match should return a Response object that has the same ' +
132 'properties as the stored response.');
133 return cache.match(response.url);
135 .then(function(result) {
138 'Cache.match should not match cache entry based on response URL.');
140 }, 'Cache.match with Request and Response objects with different URLs');
142 cache_test(function(cache) {
143 var request_url = new URL('../resources/simple.txt', location.href).href;
144 return fetch(request_url)
145 .then(function(fetch_result) {
146 return cache.put(new Request(request_url), fetch_result);
149 return cache.match(request_url);
151 .then(function(result) {
152 return result.text();
154 .then(function(body_text) {
155 assert_equals(body_text, 'a simple text file\n',
156 'Cache.match should return a Response object with a ' +
160 return cache.match(request_url);
162 .then(function(result) {
163 return result.text();
165 .then(function(body_text) {
166 assert_equals(body_text, 'a simple text file\n',
167 'Cache.match should return a Response object with a ' +
168 'valid body each time it is called.');
170 }, 'Cache.match invoked multiple times for the same Request/Response');
172 prepopulated_cache_test(simple_entries, function(cache, entries) {
173 var request = new Request(entries.a.request.clone(), {method: 'POST'});
174 return cache.match(request)
175 .then(function(result) {
176 assert_equals(result, undefined,
177 'Cache.match should not find a match');
179 }, 'Cache.match with POST Request');
181 prepopulated_cache_test(simple_entries, function(cache, entries) {
182 var response = entries.non_2xx_response.response;
183 return cache.match(entries.non_2xx_response.request.url)
184 .then(function(result) {
185 assert_response_equals(
186 result, entries.non_2xx_response.response,
187 'Cache.match should return a Response object that has the ' +
188 'same properties as a stored non-2xx response.');
190 }, 'Cache.match with a non-2xx Response');
192 prepopulated_cache_test(simple_entries, function(cache, entries) {
193 var response = entries.error_response.response;
194 return cache.match(entries.error_response.request.url)
195 .then(function(result) {
196 assert_response_equals(
197 result, entries.error_response.response,
198 'Cache.match should return a Response object that has the ' +
199 'same properties as a stored network error response.');
201 }, 'Cache.match with a network error Response');