1 if (self
.importScripts
) {
2 importScripts('/resources/testharness.js');
3 importScripts('/resources/testharness-helpers.js');
4 importScripts('../resources/test-helpers.js');
7 var test_url
= 'https://example.com/foo';
8 var test_body
= 'Hello world!';
10 cache_test(function(cache
) {
11 var request
= new Request(test_url
);
12 var response
= new Response(test_body
);
13 return cache
.put(request
, response
)
14 .then(function(result
) {
15 assert_equals(result
, undefined,
16 'Cache.put should resolve with undefined on success.');
18 }, 'Cache.put called with simple Request and Response');
20 cache_test(function(cache
) {
21 var test_url
= new URL('../resources/simple.txt', location
.href
).href
;
22 var request
= new Request(test_url
);
24 return fetch(test_url
)
25 .then(function(fetch_result
) {
26 response
= fetch_result
.clone();
27 return cache
.put(request
, fetch_result
);
30 return cache
.match(test_url
);
32 .then(function(result
) {
33 assert_response_equals(result
, response
,
34 'Cache.put should update the cache with ' +
35 'new request and response.');
38 .then(function(body
) {
39 assert_equals(body
, 'a simple text file\n',
40 'Cache.put should store response body.');
42 }, 'Cache.put called with Request and Response from fetch()');
44 cache_test(function(cache
) {
45 var request
= new Request(test_url
);
46 var response
= new Response(test_body
);
47 assert_false(request
.bodyUsed
,
48 '[https://fetch.spec.whatwg.org/#dom-body-bodyused] ' +
49 'Request.bodyUsed should be initially false.');
50 return cache
.put(request
, response
)
52 assert_false(request
.bodyUsed
,
53 'Cache.put should not mark empty request\'s body used');
55 }, 'Cache.put with Request without a body');
57 cache_test(function(cache
) {
58 var request
= new Request(test_url
);
59 var response
= new Response();
60 assert_false(response
.bodyUsed
,
61 '[https://fetch.spec.whatwg.org/#dom-body-bodyused] ' +
62 'Response.bodyUsed should be initially false.');
63 return cache
.put(request
, response
)
65 assert_false(response
.bodyUsed
,
66 'Cache.put should not mark empty response\'s body used');
68 }, 'Cache.put with Response without a body');
70 cache_test(function(cache
) {
71 var request
= new Request(test_url
);
72 var response
= new Response(test_body
);
73 return cache
.put(request
, response
.clone())
75 return cache
.match(test_url
);
77 .then(function(result
) {
78 assert_response_equals(result
, response
,
79 'Cache.put should update the cache with ' +
80 'new Request and Response.');
82 }, 'Cache.put with a Response containing an empty URL');
84 cache_test(function(cache
) {
85 var request
= new Request(test_url
);
86 var response
= new Response('', {
88 headers
: [['Content-Type', 'text/plain']]
90 return cache
.put(request
, response
)
92 return cache
.match(test_url
);
94 .then(function(result
) {
95 assert_equals(result
.status
, 200, 'Cache.put should store status.');
96 assert_equals(result
.headers
.get('Content-Type'), 'text/plain',
97 'Cache.put should store headers.');
100 .then(function(body
) {
101 assert_equals(body
, '',
102 'Cache.put should store response body.');
104 }, 'Cache.put with an empty response body');
106 cache_test(function(cache
) {
107 var test_url
= new URL('../resources/fetch-status.php?status=500', location
.href
).href
;
108 var request
= new Request(test_url
);
110 return fetch(test_url
)
111 .then(function(fetch_result
) {
112 assert_equals(fetch_result
.status
, 500,
113 'Test framework error: The status code should be 500.');
114 response
= fetch_result
.clone();
115 return cache
.put(request
, fetch_result
);
118 return cache
.match(test_url
);
120 .then(function(result
) {
121 assert_response_equals(result
, response
,
122 'Cache.put should update the cache with ' +
123 'new request and response.');
124 return result
.text();
126 .then(function(body
) {
127 assert_equals(body
, '',
128 'Cache.put should store response body.');
130 }, 'Cache.put with HTTP 500 response');
132 cache_test(function(cache
) {
133 var alternate_response_body
= 'New body';
134 var alternate_response
= new Response(alternate_response_body
,
135 { statusText
: 'New status' });
136 return cache
.put(new Request(test_url
),
137 new Response('Old body', { statusText
: 'Old status' }))
139 return cache
.put(new Request(test_url
), alternate_response
.clone());
142 return cache
.match(test_url
);
144 .then(function(result
) {
145 assert_response_equals(result
, alternate_response
,
146 'Cache.put should replace existing ' +
147 'response with new response.');
148 return result
.text();
150 .then(function(body
) {
151 assert_equals(body
, alternate_response_body
,
152 'Cache put should store new response body.');
154 }, 'Cache.put called twice with matching Requests and different Responses');
156 cache_test(function(cache
) {
157 var first_url
= test_url
;
158 var second_url
= first_url
+ '#(O_o)';
159 var alternate_response_body
= 'New body';
160 var alternate_response
= new Response(alternate_response_body
,
161 { statusText
: 'New status' });
162 return cache
.put(new Request(first_url
),
163 new Response('Old body', { statusText
: 'Old status' }))
165 return cache
.put(new Request(second_url
), alternate_response
.clone());
168 return cache
.match(test_url
);
170 .then(function(result
) {
171 assert_response_equals(result
, alternate_response
,
172 'Cache.put should replace existing ' +
173 'response with new response.');
174 return result
.text();
176 .then(function(body
) {
177 assert_equals(body
, alternate_response_body
,
178 'Cache put should store new response body.');
180 }, 'Cache.put called twice with request URLs that differ only by a fragment');
182 cache_test(function(cache
) {
185 url
: 'http://darkhelmet:12345@example.com/spaceballs',
190 url
: 'http://skroob:12345@example.com/spaceballs',
195 url
: 'http://example.com/spaceballs',
200 return Promise
.all(Object
.keys(entries
).map(function(key
) {
201 return cache
.put(new Request(entries
[key
].url
),
202 new Response(entries
[key
].body
));
205 return Promise
.all(Object
.keys(entries
).map(function(key
) {
206 return cache
.match(entries
[key
].url
)
207 .then(function(result
) {
208 return result
.text();
210 .then(function(body
) {
211 assert_equals(body
, entries
[key
].body
,
212 'Cache put should store response body.');
216 }, 'Cache.put with request URLs containing embedded credentials');
218 cache_test(function(cache
) {
219 var url
= 'http://example.com/foo';
220 return cache
.put(url
, new Response('some body'))
221 .then(function() { return cache
.match(url
); })
222 .then(function(response
) { return response
.text(); })
223 .then(function(body
) {
224 assert_equals(body
, 'some body',
225 'Cache.put should accept a string as request.');
227 }, 'Cache.put with a string request');
229 cache_test(function(cache
) {
230 return assert_promise_rejects(
231 cache
.put(new Request(test_url
), 'Hello world!'),
233 'Cache.put should only accept a Response object as the response.');
234 }, 'Cache.put with an invalid response');
236 cache_test(function(cache
) {
237 return assert_promise_rejects(
238 cache
.put(new Request('file:///etc/passwd'),
239 new Response(test_body
)),
241 'Cache.put should reject non-HTTP/HTTPS requests with a TypeError.');
242 }, 'Cache.put with a non-HTTP/HTTPS request');
244 cache_test(function(cache
) {
245 var response
= new Response(test_body
);
246 return cache
.put(new Request('relative-url'), response
.clone())
248 return cache
.match(new URL('relative-url', location
.href
).href
);
250 .then(function(result
) {
251 assert_response_equals(result
, response
,
252 'Cache.put should accept a relative URL ' +
255 }, 'Cache.put with a relative URL');
257 cache_test(function(cache
) {
258 var request
= new Request('http://example.com/foo', { method
: 'HEAD' });
259 return assert_promise_rejects(
260 cache
.put(request
, new Response(test_body
)),
262 'Cache.put should throw a TypeError for non-GET requests.');
263 }, 'Cache.put with a non-GET request');
265 cache_test(function(cache
) {
266 return assert_promise_rejects(
267 cache
.put(new Request(test_url
), null),
269 'Cache.put should throw a TypeError for a null response.');
270 }, 'Cache.put with a null response');
272 cache_test(function(cache
) {
273 var request
= new Request(test_url
, {method
: 'POST', body
: test_body
});
274 return assert_promise_rejects(
275 cache
.put(request
, new Response(test_body
)),
277 'Cache.put should throw a TypeError for a POST request.');
278 }, 'Cache.put with a POST request');
280 cache_test(function(cache
) {
281 var response
= new Response(test_body
);
282 assert_false(response
.bodyUsed
,
283 '[https://fetch.spec.whatwg.org/#dom-body-bodyused] ' +
284 'Response.bodyUsed should be initially false.');
285 return response
.text().then(function() {
288 '[https://fetch.spec.whatwg.org/#concept-body-consume-body] ' +
289 'The text() method should not set "body passed" flag.');
290 return cache
.put(new Request(test_url
), response
);
292 }, 'Cache.put with a used response body');
294 cache_test(function(cache
) {
295 var response
= new Response(test_body
);
296 return cache
.put(new Request(test_url
), response
)
298 return response
.body
.getReader().closed
;
300 }, 'getReader() after Cache.put');