1 if (self
.importScripts
) {
2 importScripts('/resources/testharness.js');
3 importScripts('/resources/testharness-helpers.js');
4 importScripts('../resources/test-helpers.js');
7 cache_test(function(cache
) {
8 return assert_promise_rejects(
11 'Cache.add should throw a TypeError when no arguments are given.');
12 }, 'Cache.add called with no arguments');
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');
21 .then(function(response
) {
22 assert_class_string(response
, 'Response',
23 'Cache.add should put a resource in the cache.');
24 return response
.text();
26 .then(function(body
) {
27 assert_equals(body
, 'a simple text file\n',
28 'Cache.add should retrieve the correct body.');
30 }, 'Cache.add called with relative URL specified as a string');
32 cache_test(function(cache
) {
33 return assert_promise_rejects(
34 cache
.add('javascript://this-is-not-http-mmkay'),
36 'Cache.add should throw a TypeError for non-HTTP/HTTPS URLs.');
37 }, 'Cache.add called with non-HTTP/HTTPS URL');
39 cache_test(function(cache
) {
40 var request
= new Request('../resources/simple.txt');
41 return cache
.add(request
)
42 .then(function(result
) {
43 assert_equals(result
, undefined,
44 'Cache.add should resolve with undefined on success.');
46 }, 'Cache.add called with Request object');
48 cache_test(function(cache
) {
49 var request
= new Request('../resources/simple.txt',
50 {method
: 'POST', body
: 'This is a body.'});
51 return assert_promise_rejects(
54 'Cache.add should throw a TypeError for non-GET requests.');
55 }, 'Cache.add called with POST request');
57 cache_test(function(cache
) {
58 var request
= new Request('../resources/simple.txt');
61 assert_false(request
.bodyUsed
);
64 return cache
.add(request
);
66 }, 'Cache.add called with Request object with a used body');
68 cache_test(function(cache
) {
69 return cache
.add('this-does-not-exist-please-dont-create-it')
70 .then(function(result
) {
71 assert_equals(result
, undefined,
72 'Cache.add should resolve with undefined on success.');
74 }, 'Cache.add with request that results in a status of 404');
76 cache_test(function(cache
) {
77 return cache
.add('../resources/fetch-status.php?status=500')
78 .then(function(result
) {
79 assert_equals(result
, undefined,
80 'Cache.add should resolve with undefined on success.');
82 }, 'Cache.add with request that results in a status of 500');
84 cache_test(function(cache
) {
85 return assert_promise_rejects(
88 'Cache.addAll with no arguments should throw TypeError.');
89 }, 'Cache.addAll with no arguments');
91 cache_test(function(cache
) {
92 // Assumes the existence of ../resources/simple.txt and ../resources/blank.html
93 var urls
= ['../resources/simple.txt', undefined, '../resources/blank.html'];
94 return assert_promise_rejects(
97 'Cache.addAll should throw TypeError for an undefined argument.');
98 }, 'Cache.addAll with a mix of valid and undefined arguments');
100 cache_test(function(cache
) {
101 return cache
.addAll([])
102 .then(function(result
) {
103 assert_equals(result
, undefined,
104 'Cache.addAll should resolve with undefined on ' +
108 .then(function(result
) {
109 assert_equals(result
.length
, 0,
110 'There should be no entry in the cache.');
112 }, 'Cache.addAll with an empty array');
114 cache_test(function(cache
) {
115 // Assumes the existence of ../resources/simple.txt and
116 // ../resources/blank.html
117 var urls
= ['../resources/simple.txt',
119 '../resources/blank.html'];
120 return cache
.addAll(urls
)
121 .then(function(result
) {
122 assert_equals(result
, undefined,
123 'Cache.addAll should resolve with undefined on ' +
126 urls
.map(function(url
) { return cache
.match(url
); }));
128 .then(function(responses
) {
130 responses
[0], 'Response',
131 'Cache.addAll should put a resource in the cache.');
133 responses
[1], 'Response',
134 'Cache.addAll should put a resource in the cache.');
136 responses
[2], 'Response',
137 'Cache.addAll should put a resource in the cache.');
139 responses
.map(function(response
) { return response
.text(); }));
141 .then(function(bodies
) {
143 bodies
[0], 'a simple text file\n',
144 'Cache.add should retrieve the correct body.');
146 bodies
[2], '<!DOCTYPE html>\n<title>Empty doc</title>\n',
147 'Cache.add should retrieve the correct body.');
149 }, 'Cache.addAll with string URL arguments');
151 cache_test(function(cache
) {
152 // Assumes the existence of ../resources/simple.txt and
153 // ../resources/blank.html
154 var urls
= ['../resources/simple.txt',
156 '../resources/blank.html'];
157 var requests
= urls
.map(function(url
) { return new Request(url
); });
158 return cache
.addAll(requests
)
159 .then(function(result
) {
160 assert_equals(result
, undefined,
161 'Cache.addAll should resolve with undefined on ' +
164 urls
.map(function(url
) { return cache
.match(url
); }));
166 .then(function(responses
) {
168 responses
[0], 'Response',
169 'Cache.addAll should put a resource in the cache.');
171 responses
[1], 'Response',
172 'Cache.addAll should put a resource in the cache.');
174 responses
[2], 'Response',
175 'Cache.addAll should put a resource in the cache.');
177 responses
.map(function(response
) { return response
.text(); }));
179 .then(function(bodies
) {
181 bodies
[0], 'a simple text file\n',
182 'Cache.add should retrieve the correct body.');
184 bodies
[2], '<!DOCTYPE html>\n<title>Empty doc</title>\n',
185 'Cache.add should retrieve the correct body.');
187 }, 'Cache.addAll with Request arguments');
189 cache_test(function(cache
) {
190 // Assumes that ../resources/simple.txt and ../resources/blank.html exist.
191 // The second resource does not.
192 var urls
= ['../resources/simple.txt',
193 'this-resource-should-not-exist',
194 '../resources/blank.html'];
195 var requests
= urls
.map(function(url
) { return new Request(url
); });
196 return cache
.addAll(requests
)
197 .then(function(result
) {
198 assert_equals(result
, undefined,
199 'Cache.addAll should resolve with undefined on ' +
202 urls
.map(function(url
) { return cache
.match(url
); }));
204 .then(function(responses
) {
206 responses
[0], 'Response',
207 'Cache.addAll should put a resource in the cache.');
209 responses
[1], 'Response',
210 'Cache.addAll should put a resource in the cache.');
212 responses
[1].status
, 404,
213 'Cache.addAll should put a 404 resource in the cache.');
215 responses
[2], 'Response',
216 'Cache.addAll should put a resource in the cache.');
218 responses
.map(function(response
) { return response
.text(); }));
220 .then(function(bodies
) {
222 bodies
[0], 'a simple text file\n',
223 'Cache.add should retrieve the correct body.');
225 bodies
[2], '<!DOCTYPE html>\n<title>Empty doc</title>\n',
226 'Cache.add should retrieve the correct body.');
228 }, 'Cache.addAll with a mix of succeeding and failing requests');