Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / http / tests / serviceworker / resources / cache-delete-worker.js
1 importScripts('worker-testharness.js');
2 importScripts('/resources/testharness-helpers.js');
3
4 var test_url = 'https://example.com/foo';
5
6 // Construct a generic Request object. The URL is |test_url|. All other fields
7 // are defaults.
8 function new_test_request() {
9   return new Request(test_url);
10 }
11
12 // Construct a generic Response object.
13 function new_test_response() {
14   return new Response('Hello world!', { status: 200 });
15 }
16
17 cache_test(function(cache) {
18     return assert_promise_rejects(
19       cache.delete(),
20       new TypeError(),
21       'Cache.delete should reject with a TypeError when called with no ' +
22       'arguments.');
23   }, 'Cache.delete with no arguments');
24
25 cache_test(function(cache) {
26     return cache.put(new_test_request(), new_test_response())
27       .then(function() {
28           return cache.delete(test_url);
29         })
30       .then(function(result) {
31           assert_true(result,
32                       'Cache.delete should resolve with "true" if an entry ' +
33                       'was successfully deleted.');
34         })
35       .then(function() {
36           assert_promise_rejects(
37             cache.match(test_url),
38             'NotFoundError',
39             'Cache.delete should remove matching entries from cache.');
40         });
41   }, 'Cache.delete called with a string URL');
42
43 cache_test(function(cache) {
44     var request = new Request(test_url, { body: 'Abc' });
45     return cache.put(request.clone(), new_test_response())
46       .then(function() {
47           return cache.delete(request);
48         })
49       .then(function(result) {
50           assert_true(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.');
55         });
56   }, 'Cache.delete called with a Request object');
57
58 cache_test(function(cache) {
59     var request = new Request(test_url, { body: 'Abc' });
60     return cache.put(request.clone(), new_test_response())
61       .then(function() {
62           return request.text();
63         })
64       .then(function() {
65           assert_true(request.bodyUsed,
66                       '[https://fetch.spec.whatwg.org/#body-mixin] ' +
67                       'Request.bodyUsed should be true after text() method ' +
68                       'resolves.');
69         })
70       .then(function() {
71           return cache.delete(request);
72         })
73       .then(function(result) {
74           assert_true(result,
75                       'Cache.delete should resolve with "true" if an entry ' +
76                       'was successfully deleted.');
77         });
78   }, 'Cache.delete with a Request object containing used body');
79
80 cache_test(function(cache) {
81     return cache.delete(test_url)
82       .then(function(result) {
83           assert_false(result,
84                        'Cache.delete should resolve with "false" if there ' +
85                        'are no matching entries.');
86         });
87   }, 'Cache.delete with a non-existent entry');
88
89 var cache_entries = {
90   a: {
91     request: new Request('http://example.com/abc'),
92     response: new Response('')
93   },
94
95   b: {
96     request: new Request('http://example.com/b'),
97     response: new Response('')
98   },
99
100   a_with_query: {
101     request: new Request('http://example.com/abc?q=r'),
102     response: new Response('')
103   }
104 };
105
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());
111         }))
112         .then(function() {
113             return test_function(cache);
114           });
115     }, description);
116 }
117
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.
121
122 prepopulated_cache_test(function(cache) {
123     return cache.delete('http://example.com/a', { prefixMatch: true })
124       .then(function(result) {
125           assert_true(result,
126                       'Cache.delete should resolve with "true" if an entry ' +
127                       'was successfully deleted.');
128         })
129       .then(function() {
130           return Promise.all(
131             [].concat(
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),
136                     'NotFoundError',
137                     'Cache.delete should respect "prefixMatch" option.');
138                 }),
139               // The entry 'b' should still be in the cache.
140               cache.match(cache_entries.b.request.url)
141               .catch(function() {
142                   assert_unreached('Cache.delete should respect ' +
143                                    '"prefixMatch" option.');
144                 })
145             ));
146         });
147   }, 'Cache.delete with CacheQueryOptions.*');
148
149 prepopulated_cache_test(function(cache) {
150     return cache.delete('http://example.com/ac', { prefixMatch: true })
151       .then(function(result) {
152           assert_false(result,
153                        'Cache.delete should resolve with "false" if there ' +
154                        'are no matching entries.');
155         });
156   }, 'Cache.delete with CacheQueryOptions.* that don\'t match');
157