OLD | NEW |
| (Empty) |
1 if (self.importScripts) { | |
2 importScripts('/resources/testharness.js'); | |
3 importScripts('../resources/test-helpers.js'); | |
4 } | |
5 | |
6 cache_test(function(cache, test) { | |
7 return promise_rejects( | |
8 test, | |
9 new TypeError(), | |
10 cache.add(), | |
11 'Cache.add should throw a TypeError when no arguments are given.'); | |
12 }, 'Cache.add called with no arguments'); | |
13 | |
14 cache_test(function(cache) { | |
15 return cache.add('../resources/simple.txt') | |
16 .then(function(result) { | |
17 assert_equals(result, undefined, | |
18 'Cache.add should resolve with undefined on success.'); | |
19 return cache.match('../resources/simple.txt'); | |
20 }) | |
21 .then(function(response) { | |
22 assert_class_string(response, 'Response', | |
23 'Cache.add should put a resource in the cache.'); | |
24 return response.text(); | |
25 }) | |
26 .then(function(body) { | |
27 assert_equals(body, 'a simple text file\n', | |
28 'Cache.add should retrieve the correct body.'); | |
29 }); | |
30 }, 'Cache.add called with relative URL specified as a string'); | |
31 | |
32 cache_test(function(cache, test) { | |
33 return promise_rejects( | |
34 test, | |
35 new TypeError(), | |
36 cache.add('javascript://this-is-not-http-mmkay'), | |
37 'Cache.add should throw a TypeError for non-HTTP/HTTPS URLs.'); | |
38 }, 'Cache.add called with non-HTTP/HTTPS URL'); | |
39 | |
40 cache_test(function(cache) { | |
41 var request = new Request('../resources/simple.txt'); | |
42 return cache.add(request) | |
43 .then(function(result) { | |
44 assert_equals(result, undefined, | |
45 'Cache.add should resolve with undefined on success.'); | |
46 }); | |
47 }, 'Cache.add called with Request object'); | |
48 | |
49 cache_test(function(cache, test) { | |
50 var request = new Request('../resources/simple.txt', | |
51 {method: 'POST', body: 'This is a body.'}); | |
52 return promise_rejects( | |
53 test, | |
54 new TypeError(), | |
55 cache.add(request), | |
56 'Cache.add should throw a TypeError for non-GET requests.'); | |
57 }, 'Cache.add called with POST request'); | |
58 | |
59 cache_test(function(cache) { | |
60 var request = new Request('../resources/simple.txt'); | |
61 return cache.add(request) | |
62 .then(function(result) { | |
63 assert_equals(result, undefined, | |
64 'Cache.add should resolve with undefined on success.'); | |
65 }) | |
66 .then(function() { | |
67 return cache.add(request); | |
68 }) | |
69 .then(function(result) { | |
70 assert_equals(result, undefined, | |
71 'Cache.add should resolve with undefined on success.'); | |
72 }); | |
73 }, 'Cache.add called twice with the same Request object'); | |
74 | |
75 cache_test(function(cache) { | |
76 var request = new Request('../resources/simple.txt'); | |
77 return request.text() | |
78 .then(function() { | |
79 assert_false(request.bodyUsed); | |
80 }) | |
81 .then(function() { | |
82 return cache.add(request); | |
83 }); | |
84 }, 'Cache.add called with Request object with a used body'); | |
85 | |
86 cache_test(function(cache, test) { | |
87 return promise_rejects( | |
88 test, | |
89 new TypeError(), | |
90 cache.add('this-does-not-exist-please-dont-create-it'), | |
91 'Cache.add should reject if response is !ok'); | |
92 }, 'Cache.add with request that results in a status of 404'); | |
93 | |
94 cache_test(function(cache, test) { | |
95 return promise_rejects( | |
96 test, | |
97 new TypeError(), | |
98 cache.add('../resources/fetch-status.php?status=500'), | |
99 'Cache.add should reject if response is !ok'); | |
100 }, 'Cache.add with request that results in a status of 500'); | |
101 | |
102 cache_test(function(cache, test) { | |
103 return promise_rejects( | |
104 test, | |
105 new TypeError(), | |
106 cache.add('../resources/fetch-status.php?status=206'), | |
107 'Cache.add should reject on partial response'); | |
108 }, 'Cache.add with request that results in a status of 206'); | |
109 | |
110 cache_test(function(cache, test) { | |
111 return promise_rejects( | |
112 test, | |
113 new TypeError(), | |
114 cache.addAll(['../resources/simple.txt', | |
115 '../resources/fetch-status.php?status=206']), | |
116 'Cache.addAll should reject on partial response'); | |
117 }, 'Cache.addAll with request that results in a status of 206'); | |
118 | |
119 cache_test(function(cache, test) { | |
120 return promise_rejects( | |
121 test, | |
122 new TypeError(), | |
123 cache.addAll(), | |
124 'Cache.addAll with no arguments should throw TypeError.'); | |
125 }, 'Cache.addAll with no arguments'); | |
126 | |
127 cache_test(function(cache, test) { | |
128 // Assumes the existence of ../resources/simple.txt and ../resources/blank.h
tml | |
129 var urls = ['../resources/simple.txt', undefined, '../resources/blank.html']
; | |
130 return promise_rejects( | |
131 test, | |
132 new TypeError(), | |
133 cache.addAll(), | |
134 'Cache.addAll should throw TypeError for an undefined argument.'); | |
135 }, 'Cache.addAll with a mix of valid and undefined arguments'); | |
136 | |
137 cache_test(function(cache) { | |
138 return cache.addAll([]) | |
139 .then(function(result) { | |
140 assert_equals(result, undefined, | |
141 'Cache.addAll should resolve with undefined on ' + | |
142 'success.'); | |
143 return cache.keys(); | |
144 }) | |
145 .then(function(result) { | |
146 assert_equals(result.length, 0, | |
147 'There should be no entry in the cache.'); | |
148 }); | |
149 }, 'Cache.addAll with an empty array'); | |
150 | |
151 cache_test(function(cache) { | |
152 // Assumes the existence of ../resources/simple.txt and | |
153 // ../resources/blank.html | |
154 var urls = ['../resources/simple.txt', | |
155 self.location.href, | |
156 '../resources/blank.html']; | |
157 return cache.addAll(urls) | |
158 .then(function(result) { | |
159 assert_equals(result, undefined, | |
160 'Cache.addAll should resolve with undefined on ' + | |
161 'success.'); | |
162 return Promise.all( | |
163 urls.map(function(url) { return cache.match(url); })); | |
164 }) | |
165 .then(function(responses) { | |
166 assert_class_string( | |
167 responses[0], 'Response', | |
168 'Cache.addAll should put a resource in the cache.'); | |
169 assert_class_string( | |
170 responses[1], 'Response', | |
171 'Cache.addAll should put a resource in the cache.'); | |
172 assert_class_string( | |
173 responses[2], 'Response', | |
174 'Cache.addAll should put a resource in the cache.'); | |
175 return Promise.all( | |
176 responses.map(function(response) { return response.text(); })); | |
177 }) | |
178 .then(function(bodies) { | |
179 assert_equals( | |
180 bodies[0], 'a simple text file\n', | |
181 'Cache.add should retrieve the correct body.'); | |
182 assert_equals( | |
183 bodies[2], '<!DOCTYPE html>\n<title>Empty doc</title>\n', | |
184 'Cache.add should retrieve the correct body.'); | |
185 }); | |
186 }, 'Cache.addAll with string URL arguments'); | |
187 | |
188 cache_test(function(cache) { | |
189 // Assumes the existence of ../resources/simple.txt and | |
190 // ../resources/blank.html | |
191 var urls = ['../resources/simple.txt', | |
192 self.location.href, | |
193 '../resources/blank.html']; | |
194 var requests = urls.map(function(url) { | |
195 return new Request(url); | |
196 }); | |
197 return cache.addAll(requests) | |
198 .then(function(result) { | |
199 assert_equals(result, undefined, | |
200 'Cache.addAll should resolve with undefined on ' + | |
201 'success.'); | |
202 return Promise.all( | |
203 urls.map(function(url) { return cache.match(url); })); | |
204 }) | |
205 .then(function(responses) { | |
206 assert_class_string( | |
207 responses[0], 'Response', | |
208 'Cache.addAll should put a resource in the cache.'); | |
209 assert_class_string( | |
210 responses[1], 'Response', | |
211 'Cache.addAll should put a resource in the cache.'); | |
212 assert_class_string( | |
213 responses[2], 'Response', | |
214 'Cache.addAll should put a resource in the cache.'); | |
215 return Promise.all( | |
216 responses.map(function(response) { return response.text(); })); | |
217 }) | |
218 .then(function(bodies) { | |
219 assert_equals( | |
220 bodies[0], 'a simple text file\n', | |
221 'Cache.add should retrieve the correct body.'); | |
222 assert_equals( | |
223 bodies[2], '<!DOCTYPE html>\n<title>Empty doc</title>\n', | |
224 'Cache.add should retrieve the correct body.'); | |
225 }); | |
226 }, 'Cache.addAll with Request arguments'); | |
227 | |
228 cache_test(function(cache, test) { | |
229 // Assumes that ../resources/simple.txt and ../resources/blank.html exist. | |
230 // The second resource does not. | |
231 var urls = ['../resources/simple.txt', | |
232 'this-resource-should-not-exist', | |
233 '../resources/blank.html']; | |
234 var requests = urls.map(function(url) { | |
235 return new Request(url); | |
236 }); | |
237 return promise_rejects( | |
238 test, | |
239 new TypeError(), | |
240 cache.addAll(requests), | |
241 'Cache.addAll should reject with TypeError if any request fails') | |
242 .then(function() { | |
243 return Promise.all(urls.map(function(url) { | |
244 return cache.match(url); | |
245 })); | |
246 }) | |
247 .then(function(matches) { | |
248 assert_array_equals( | |
249 matches, | |
250 [undefined, undefined, undefined], | |
251 'If any response fails, no response should be added to cache'); | |
252 }); | |
253 }, 'Cache.addAll with a mix of succeeding and failing requests'); | |
254 | |
255 cache_test(function(cache, test) { | |
256 var request = new Request('../resources/simple.txt'); | |
257 return promise_rejects( | |
258 test, | |
259 'InvalidStateError', | |
260 cache.addAll([request, request]), | |
261 'Cache.addAll should throw InvalidStateError if the same request is added
' + | |
262 'twice.'); | |
263 }, 'Cache.addAll called with the same Request object specified twice'); | |
264 | |
265 done(); | |
OLD | NEW |