Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / http / tests / cachestorage / script-tests / cache-match.js
blob6f1cfac1ca123c099978185e62c749cf9aaf84bf
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.');
12         });
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.');
20         });
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.');
28         });
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.');
36         });
37   }, 'Cache.match with new Request');
39 prepopulated_cache_test(simple_entries, function(cache, entries) {
40     return cache.match(entries.a.request,
41                        {ignoreSearch: true})
42       .then(function(result) {
43           assert_response_in_array(
44             result,
45             [
46               entries.a.response,
47               entries.a_with_query.response
48             ],
49             'Cache.match with ignoreSearch should ignore the ' +
50             'search parameters of cached request.');
51         });
52   },
53   'Cache.match with ignoreSearch option (request with no search ' +
54   'parameters)');
56 prepopulated_cache_test(simple_entries, function(cache, entries) {
57     return cache.match(entries.a_with_query.request,
58                        {ignoreSearch: true})
59       .then(function(result) {
60           assert_response_in_array(
61             result,
62             [
63               entries.a.response,
64               entries.a_with_query.response
65             ],
66             'Cache.match with ignoreSearch should ignore the ' +
67             'search parameters of request.');
68         });
69   },
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.');
77         });
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) {
83           assert_equals(
84             result, undefined,
85             'Cache.match should treat query as a URL and not ' +
86             'just a string fragment.');
87         });
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');
96         });
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(
103             result,
104             [
105               entries.vary_wildcard.response,
106               entries.vary_cookie_absent.response
107             ],
108             'Cache.match should honor "Vary" header.');
109         });
110   }, 'Cache.match with responses containing "Vary" header');
112 cache_test(function(cache) {
113     var request = new Request('http://example.com');
114     var response;
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;
119           assert_equals(
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());
124         })
125       .then(function() {
126           return cache.match(request.url);
127         })
128       .then(function(result) {
129           assert_response_equals(
130             result, response,
131             'Cache.match should return a Response object that has the same ' +
132             'properties as the stored response.');
133           return cache.match(response.url);
134         })
135       .then(function(result) {
136           assert_equals(
137             result, undefined,
138             'Cache.match should not match cache entry based on response URL.');
139         });
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);
147         })
148       .then(function() {
149           return cache.match(request_url);
150         })
151       .then(function(result) {
152           return result.text();
153         })
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 ' +
157                         'valid body.');
158         })
159       .then(function() {
160           return cache.match(request_url);
161         })
162       .then(function(result) {
163           return result.text();
164         })
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.');
169         });
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');
178       });
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.');
189         });
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.');
200         });
201   }, 'Cache.match with a network error Response');
203 done();