Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(762)

Side by Side Diff: LayoutTests/http/tests/serviceworker/resources/cache-put-worker.js

Issue 708703002: Service Worker: Cache.put() consumes request/response bodies (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Inline hasBody methods Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 importScripts('worker-testharness.js'); 1 importScripts('worker-testharness.js');
2 importScripts('/resources/testharness-helpers.js'); 2 importScripts('/resources/testharness-helpers.js');
3 3
4 var test_url = 'https://example.com/foo'; 4 var test_url = 'https://example.com/foo';
5 var test_response_body = 'Hello world!'; 5 var test_body = 'Hello world!';
6 6
7 cache_test(function(cache) { 7 cache_test(function(cache) {
8 var request = new Request(test_url); 8 var request = new Request(test_url);
9 var response = new Response(test_response_body); 9 var response = new Response(test_body);
10 return cache.put(request, response) 10 return cache.put(request, response)
11 .then(function(result) { 11 .then(function(result) {
12 assert_equals(result, undefined, 12 assert_equals(result, undefined,
13 'Cache.put should resolve with undefined on success.'); 13 'Cache.put should resolve with undefined on success.');
14 }); 14 });
15 }, 'Cache.put called with simple Request and Response'); 15 }, 'Cache.put called with simple Request and Response');
16 16
17 cache_test(function(cache) { 17 cache_test(function(cache) {
18 var test_url = new URL('simple.txt', location.href).href; 18 var test_url = new URL('simple.txt', location.href).href;
19 var request = new Request(test_url); 19 var request = new Request(test_url);
(...skipping 12 matching lines...) Expand all
32 'new request and response.'); 32 'new request and response.');
33 return result.text(); 33 return result.text();
34 }) 34 })
35 .then(function(body) { 35 .then(function(body) {
36 assert_equals(body, 'a simple text file\n', 36 assert_equals(body, 'a simple text file\n',
37 'Cache.put should store response body.'); 37 'Cache.put should store response body.');
38 }); 38 });
39 }, 'Cache.put called with Request and Response from fetch()'); 39 }, 'Cache.put called with Request and Response from fetch()');
40 40
41 cache_test(function(cache) { 41 cache_test(function(cache) {
42 var request = new Request(test_url);
43 var response = new Response(test_body);
44 assert_false(request.bodyUsed,
45 '[https://fetch.spec.whatwg.org/#dom-body-bodyused] ' +
46 'Request.bodyUsed should be initially false.');
47 return cache.put(request, response)
48 .then(function() {
49 assert_false(request.bodyUsed,
50 'Cache.put should not mark empty request\'s body used');
51 });
52 }, 'Cache.put with Request without a body');
53
54 cache_test(function(cache) {
55 var request = new Request(test_url);
56 var response = new Response();
57 assert_false(response.bodyUsed,
58 '[https://fetch.spec.whatwg.org/#dom-body-bodyused] ' +
59 'Response.bodyUsed should be initially false.');
60 return cache.put(request, response)
61 .then(function() {
62 assert_false(response.bodyUsed,
63 'Cache.put should not mark empty response\'s body used');
64 });
65 }, 'Cache.put with Response without a body');
66
67 cache_test(function(cache) {
42 var request = new Request(test_url, { 68 var request = new Request(test_url, {
43 method: 'GET', 69 method: 'GET',
44 body: 'Hello' 70 body: 'Hello'
45 }); 71 });
46 var response = new Response(test_response_body); 72 var response = new Response(test_body);
47 assert_false(request.bodyUsed, 73 assert_false(request.bodyUsed,
48 '[https://fetch.spec.whatwg.org/#dom-body-bodyused] ' + 74 '[https://fetch.spec.whatwg.org/#dom-body-bodyused] ' +
49 'Request.bodyUsed should be initially false.'); 75 'Request.bodyUsed should be initially false.');
50 return cache.put(request, response) 76 return cache.put(request, response.clone())
51 .then(function() { 77 .then(function() {
52 assert_false(request.bodyUsed, 78 assert_true(request.bodyUsed,
53 'Cache.put should not consume Request body.'); 79 'Cache.put should consume Request body.');
54 }) 80 })
55 .then(function() { 81 .then(function() {
56 return cache.match(request); 82 return cache.match(request);
57 }) 83 })
58 .then(function(result) { 84 .then(function(result) {
59 assert_object_equals(result, response, 85 assert_object_equals(result, response,
60 'Cache.put should store response body.'); 86 'Cache.put should store response body.');
61 }); 87 });
62 }, 'Cache.put with Request containing a body'); 88 }, 'Cache.put with Request containing a body');
63 89
64 cache_test(function(cache) { 90 cache_test(function(cache) {
65 var request = new Request(test_url); 91 var request = new Request(test_url);
66 var response = new Response(test_response_body); 92 var response = new Response(test_body);
67 return cache.put(request, response) 93 return cache.put(request, response.clone())
68 .then(function() { 94 .then(function() {
69 return cache.match(test_url); 95 return cache.match(test_url);
70 }) 96 })
71 .then(function(result) { 97 .then(function(result) {
72 assert_object_equals(result, response, 98 assert_object_equals(result, response,
73 'Cache.put should update the cache with ' + 99 'Cache.put should update the cache with ' +
74 'new Request and Response.'); 100 'new Request and Response.');
75 }); 101 });
76 }, 'Cache.put with a Response containing an empty URL'); 102 }, 'Cache.put with a Response containing an empty URL');
77 103
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 }); 149 });
124 }, 'Cache.put with HTTP 500 response'); 150 }, 'Cache.put with HTTP 500 response');
125 151
126 cache_test(function(cache) { 152 cache_test(function(cache) {
127 var alternate_response_body = 'New body'; 153 var alternate_response_body = 'New body';
128 var alternate_response = new Response(alternate_response_body, 154 var alternate_response = new Response(alternate_response_body,
129 { statusText: 'New status' }); 155 { statusText: 'New status' });
130 return cache.put(new Request(test_url), 156 return cache.put(new Request(test_url),
131 new Response('Old body', { statusText: 'Old status' })) 157 new Response('Old body', { statusText: 'Old status' }))
132 .then(function() { 158 .then(function() {
133 return cache.put(new Request(test_url), alternate_response); 159 return cache.put(new Request(test_url), alternate_response.clone());
134 }) 160 })
135 .then(function() { 161 .then(function() {
136 return cache.match(test_url); 162 return cache.match(test_url);
137 }) 163 })
138 .then(function(result) { 164 .then(function(result) {
139 assert_object_equals(result, alternate_response, 165 assert_object_equals(result, alternate_response,
140 'Cache.put should replace existing ' + 166 'Cache.put should replace existing ' +
141 'response with new response.'); 167 'response with new response.');
142 return result.text(); 168 return result.text();
143 }) 169 })
144 .then(function(body) { 170 .then(function(body) {
145 assert_equals(body, alternate_response_body, 171 assert_equals(body, alternate_response_body,
146 'Cache put should store new response body.'); 172 'Cache put should store new response body.');
147 }); 173 });
148 }, 'Cache.put called twice with matching Requests and different Responses'); 174 }, 'Cache.put called twice with matching Requests and different Responses');
149 175
150 cache_test(function(cache) { 176 cache_test(function(cache) {
151 var first_url = test_url; 177 var first_url = test_url;
152 var second_url = first_url + '#(O_o)'; 178 var second_url = first_url + '#(O_o)';
153 var alternate_response_body = 'New body'; 179 var alternate_response_body = 'New body';
154 var alternate_response = new Response(alternate_response_body, 180 var alternate_response = new Response(alternate_response_body,
155 { statusText: 'New status' }); 181 { statusText: 'New status' });
156 return cache.put(new Request(first_url), 182 return cache.put(new Request(first_url),
157 new Response('Old body', { statusText: 'Old status' })) 183 new Response('Old body', { statusText: 'Old status' }))
158 .then(function() { 184 .then(function() {
159 return cache.put(new Request(second_url), alternate_response); 185 return cache.put(new Request(second_url), alternate_response.clone());
160 }) 186 })
161 .then(function() { 187 .then(function() {
162 return cache.match(test_url); 188 return cache.match(test_url);
163 }) 189 })
164 .then(function(result) { 190 .then(function(result) {
165 assert_object_equals(result, alternate_response, 191 assert_object_equals(result, alternate_response,
166 'Cache.put should replace existing ' + 192 'Cache.put should replace existing ' +
167 'response with new response.'); 193 'response with new response.');
168 return result.text(); 194 return result.text();
169 }) 195 })
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
223 cache_test(function(cache) { 249 cache_test(function(cache) {
224 return assert_promise_rejects( 250 return assert_promise_rejects(
225 cache.put(new Request(test_url), 'Hello world!'), 251 cache.put(new Request(test_url), 'Hello world!'),
226 new TypeError(), 252 new TypeError(),
227 'Cache.put should only accept a Response object as the response.'); 253 'Cache.put should only accept a Response object as the response.');
228 }, 'Cache.put with an invalid response'); 254 }, 'Cache.put with an invalid response');
229 255
230 cache_test(function(cache) { 256 cache_test(function(cache) {
231 return assert_promise_rejects( 257 return assert_promise_rejects(
232 cache.put(new Request('file:///etc/passwd'), 258 cache.put(new Request('file:///etc/passwd'),
233 new Response(test_response_body)), 259 new Response(test_body)),
234 new TypeError(), 260 new TypeError(),
235 'Cache.put should reject non-HTTP/HTTPS requests with a TypeError.'); 261 'Cache.put should reject non-HTTP/HTTPS requests with a TypeError.');
236 }, 'Cache.put with a non-HTTP/HTTPS request'); 262 }, 'Cache.put with a non-HTTP/HTTPS request');
237 263
238 cache_test(function(cache) { 264 cache_test(function(cache) {
239 var response = new Response(test_response_body); 265 var response = new Response(test_body);
240 return cache.put(new Request('relative-url'), response) 266 return cache.put(new Request('relative-url'), response.clone())
241 .then(function() { 267 .then(function() {
242 return cache.match(new URL('relative-url', location.href).href); 268 return cache.match(new URL('relative-url', location.href).href);
243 }) 269 })
244 .then(function(result) { 270 .then(function(result) {
245 assert_object_equals(result, response, 271 assert_object_equals(result, response,
246 'Cache.put should accept a relative URL ' + 272 'Cache.put should accept a relative URL ' +
247 'as the request.'); 273 'as the request.');
248 }); 274 });
249 }, 'Cache.put with a relative URL'); 275 }, 'Cache.put with a relative URL');
250 276
251 cache_test(function(cache) { 277 cache_test(function(cache) {
252 var request = new Request('http://example.com/foo', { method: 'HEAD' }); 278 var request = new Request('http://example.com/foo', { method: 'HEAD' });
253 return assert_promise_rejects( 279 return assert_promise_rejects(
254 cache.put(request, new Response(test_response_body)), 280 cache.put(request, new Response(test_body)),
255 new TypeError(), 281 new TypeError(),
256 'Cache.put should throw a TypeError for non-GET requests.'); 282 'Cache.put should throw a TypeError for non-GET requests.');
257 }, 'Cache.put with a non-GET request'); 283 }, 'Cache.put with a non-GET request');
258 284
259 cache_test(function(cache) { 285 cache_test(function(cache) {
260 return assert_promise_rejects( 286 return assert_promise_rejects(
261 cache.put(new Request(test_url), null), 287 cache.put(new Request(test_url), null),
262 new TypeError(), 288 new TypeError(),
263 'Cache.put should throw a TypeError for an empty response.'); 289 'Cache.put should throw a TypeError for a null response.');
264 }, 'Cache.put with an empty response'); 290 }, 'Cache.put with a null response');
291
292 cache_test(function(cache) {
293 var request = new Request(test_url, {body: test_body});
294 assert_false(request.bodyUsed,
295 '[https://fetch.spec.whatwg.org/#dom-body-bodyused] ' +
296 'Request.bodyUsed should be initially false.');
297 var copy = new Request(request);
298 assert_true(request.bodyUsed,
299 '[https://fetch.spec.whatwg.org/#dom-request] ' +
300 'Request constructor should set input\'s used flag.');
301 return assert_promise_rejects(
302 cache.put(request, new Response(test_body)),
303 new TypeError(),
304 'Cache.put should throw a TypeError for a request with used body.');
305 }, 'Cache.put with a used request body');
306
307 cache_test(function(cache) {
308 var response = new Response(test_body);
309 assert_false(response.bodyUsed,
310 '[https://fetch.spec.whatwg.org/#dom-body-bodyused] ' +
311 'Response.bodyUsed should be initially false.');
312 response.text().then(function() {
313 assert_true(
314 response.bodyUsed,
315 '[https://fetch.spec.whatwg.org/#concept-body-consume-body] ' +
316 'The text() method should consume the body of the response.');
317 return assert_promise_rejects(
318 cache.put(new Request(test_url), response),
319 new TypeError(),
320 'Cache.put should throw a TypeError for a response with used body.');
321 });
322 }, 'Cache.put with a used response body');
OLDNEW
« no previous file with comments | « LayoutTests/http/tests/serviceworker/resources/cache-match-worker.js ('k') | Source/modules/serviceworkers/Body.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698