1 importScripts('worker-testharness.js');
2 importScripts('/resources/testharness-helpers.js');
4 var test_url = 'https://example.com/foo';
6 // Construct a generic Request object. The URL is |test_url|. All other fields
8 function new_test_request() {
9 return new Request(test_url);
12 // Construct a generic Response object.
13 function new_test_response() {
14 return new Response('Hello world!', { status: 200 });
17 cache_test(function(cache) {
18 return assert_promise_rejects(
21 'Cache.delete should reject with a TypeError when called with no ' +
23 }, 'Cache.delete with no arguments');
25 cache_test(function(cache) {
26 return cache.put(new_test_request(), new_test_response())
28 return cache.delete(test_url);
30 .then(function(result) {
32 'Cache.delete should resolve with "true" if an entry ' +
33 'was successfully deleted.');
36 assert_promise_rejects(
37 cache.match(test_url),
39 'Cache.delete should remove matching entries from cache.');
41 }, 'Cache.delete called with a string URL');
43 cache_test(function(cache) {
44 var request = new Request(test_url, { body: 'Abc' });
45 return cache.put(request.clone(), new_test_response())
47 return cache.delete(request);
49 .then(function(result) {
51 'Cache.delete should resolve with "true" if an entry ' +
52 'was successfully deleted.');
53 assert_false(request.bodyUsed,
54 'Cache.delete should not consume request body.');
56 }, 'Cache.delete called with a Request object');
58 cache_test(function(cache) {
59 var request = new Request(test_url, { body: 'Abc' });
60 return cache.put(request.clone(), new_test_response())
62 return request.text();
65 assert_true(request.bodyUsed,
66 '[https://fetch.spec.whatwg.org/#body-mixin] ' +
67 'Request.bodyUsed should be true after text() method ' +
71 return cache.delete(request);
73 .then(function(result) {
75 'Cache.delete should resolve with "true" if an entry ' +
76 'was successfully deleted.');
78 }, 'Cache.delete with a Request object containing used body');
80 cache_test(function(cache) {
81 return cache.delete(test_url)
82 .then(function(result) {
84 'Cache.delete should resolve with "false" if there ' +
85 'are no matching entries.');
87 }, 'Cache.delete with a non-existent entry');
91 request: new Request('http://example.com/abc'),
92 response: new Response('')
96 request: new Request('http://example.com/b'),
97 response: new Response('')
101 request: new Request('http://example.com/abc?q=r'),
102 response: new Response('')
106 function prepopulated_cache_test(test_function, description) {
107 cache_test(function(cache) {
108 return Promise.all(Object.keys(cache_entries).map(function(k) {
109 return cache.put(cache_entries[k].request.clone(),
110 cache_entries[k].response.clone());
113 return test_function(cache);
118 // Note that these tests don't exercise the full gamut of CacheQueryOptions.
119 // That's left for the Cache.match() and Cache.matchAll() tests. The objective
120 // is to test that CacheQueryOptions, if specified, are used.
122 prepopulated_cache_test(function(cache) {
123 return cache.delete('http://example.com/a', { prefixMatch: true })
124 .then(function(result) {
126 'Cache.delete should resolve with "true" if an entry ' +
127 'was successfully deleted.');
132 // The entries 'a' and 'a_with_query' should have been deleted.
133 ['a', 'a_with_query'].map(function(k) {
134 return assert_promise_rejects(
135 cache.match(cache_entries[k].request.url),
137 'Cache.delete should respect "prefixMatch" option.');
139 // The entry 'b' should still be in the cache.
140 cache.match(cache_entries.b.request.url)
142 assert_unreached('Cache.delete should respect ' +
143 '"prefixMatch" option.');
147 }, 'Cache.delete with CacheQueryOptions.*');
149 prepopulated_cache_test(function(cache) {
150 return cache.delete('http://example.com/ac', { prefixMatch: true })
151 .then(function(result) {
153 'Cache.delete should resolve with "false" if there ' +
154 'are no matching entries.');
156 }, 'Cache.delete with CacheQueryOptions.* that don\'t match');