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

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

Issue 706073002: [ServiceWorker] Add Cache.put tests for URL fragment handling. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@cleanup-formatting
Patch Set: 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
« no previous file with comments | « LayoutTests/http/tests/serviceworker/cache-put-expected.txt ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 5
6 // Construct a generic Request object. The URL is |test_url|. All other fields 6 // Construct a generic Request object. The URL is |test_url|. All other fields
jsbell 2014/11/06 22:48:37 Nit: adjust comment But... what's the advantage f
asanka 2014/11/06 23:42:00 None whatsoever. Removed!
7 // are defaults. 7 // are defaults.
8 function new_test_request() { 8 function new_test_request(url) {
9 return new Request(test_url); 9 url = url || test_url;
10 return new Request(url);
10 } 11 }
11 12
12 // Construct a generic Response object. The URL is empty. If specified |body| 13 // Construct a generic Response object. The URL is empty. If specified |body|
13 // will be set as the response body string. 14 // will be set as the response body string.
14 function new_test_response(body) { 15 function new_test_response(body) {
15 body = body || 'Hello world!'; 16 body = body || 'Hello world!';
16 return new Response(body, { 17 return new Response(body, {
17 status: 200, 18 status: 200,
18 statusText: 'OK', 19 statusText: 'OK',
19 headers: [['Content-Type', 'text/plain']] 20 headers: [['Content-Type', 'text/plain']]
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
112 'Cache.put should store response body.'); 113 'Cache.put should store response body.');
113 }); 114 });
114 }, 'Cache.put with an empty response body'); 115 }, 'Cache.put with an empty response body');
115 116
116 cache_test(function(cache) { 117 cache_test(function(cache) {
117 var test_url = new URL('fetch-status.php?status=500', location.href).href; 118 var test_url = new URL('fetch-status.php?status=500', location.href).href;
118 var request = new Request(test_url); 119 var request = new Request(test_url);
119 var response; 120 var response;
120 return fetch(test_url) 121 return fetch(test_url)
121 .then(function(fetch_result) { 122 .then(function(fetch_result) {
123 assert_equals(fetch_result.status, 500,
124 'Test framework error: The status code should be 500.');
122 response = fetch_result.clone(); 125 response = fetch_result.clone();
123 return cache.put(request, fetch_result); 126 return cache.put(request, fetch_result);
124 }) 127 })
125 .then(function() { 128 .then(function() {
126 return cache.match(test_url); 129 return cache.match(test_url);
127 }) 130 })
128 .then(function(result) { 131 .then(function(result) {
129 assert_object_equals(result, response, 132 assert_object_equals(result, response,
130 'Cache.put should update the cache with ' + 133 'Cache.put should update the cache with ' +
131 'new request and response.'); 134 'new request and response.');
132 return result.text(); 135 return result.text();
133 }) 136 })
134 .then(function(body) { 137 .then(function(body) {
135 assert_equals(body, '', 138 assert_equals(body, '',
136 'Cache.put should store response body.'); 139 'Cache.put should store response body.');
137 }); 140 });
138 }, 'Cache.put with HTTP 500 response'); 141 }, 'Cache.put with HTTP 500 response');
139 142
140 cache_test(function(cache) { 143 cache_test(function(cache) {
141 var alternate_response = new_test_response('Lorem ipsum'); 144 var alternate_response_body = 'New body';
142 return cache.put(new_test_request(), new_test_response()) 145 var alternate_response = new_test_response(alternate_response_body);
146 return cache.put(new_test_request(),
147 new_test_response('Old body'))
143 .then(function() { 148 .then(function() {
144 return cache.put(new_test_request(), alternate_response); 149 return cache.put(new_test_request(), alternate_response);
145 }) 150 })
146 .then(function() { 151 .then(function() {
147 return cache.match(test_url); 152 return cache.match(test_url);
148 }) 153 })
149 .then(function(result) { 154 .then(function(result) {
150 assert_object_equals(result, alternate_response, 155 assert_object_equals(result, alternate_response,
151 'Cache.put should replace existing ' + 156 'Cache.put should replace existing ' +
152 'response with new response.'); 157 'response with new response.');
158 return result.text();
159 })
160 .then(function(body) {
161 assert_equals(body, alternate_response_body,
162 'Cache put should store new response body.');
153 }); 163 });
154 }, 'Cache.put called twice with same Request and different Responses'); 164 }, 'Cache.put called twice with matching Requests and different Responses');
165
166 cache_test(function(cache) {
167 var first_url = test_url;
168 var second_url = first_url + '#(O_o)';
169 var alternate_response_body = 'New body';
170 var alternate_response = new_test_response(alternate_response_body);
171 return cache.put(new_test_request(first_url),
172 new_test_response('Old body'))
173 .then(function() {
174 return cache.put(new_test_request(second_url), alternate_response);
175 })
176 .then(function() {
177 return cache.match(test_url);
178 })
179 .then(function(result) {
180 assert_object_equals(result, alternate_response,
jsbell 2014/11/06 22:48:37 It's a bit weird since this assertion currently pa
asanka 2014/11/06 23:42:00 Yeah. I removed the new_test_response() function a
181 'Cache.put should replace existing ' +
182 'response with new response.');
183 return result.text();
184 })
185 .then(function(body) {
186 assert_equals(body, alternate_response_body,
187 'Cache put should store new response body.');
188 });
189 }, 'Cache.put called twice with request URLs that differ only by a fragment');
190
191 cache_test(function(cache) {
192 var entries = {
193 dark: {
194 url: 'http://darkhelmet:12345@example.com/spaceballs',
195 body: 'Moranis'
196 },
197
198 skroob: {
199 url: 'http://skroob:12345@example.com/spaceballs',
200 body: 'Brooks'
201 },
202
203 control: {
204 url: 'http://example.com/spaceballs',
205 body: 'v(o.o)v'
206 }
207 };
208
209 return Promise.all(Object.keys(entries).map(function(key) {
210 return cache.put(new_test_request(entries[key].url),
211 new_test_response(entries[key].body));
212 }))
213 .then(function() {
214 return Promise.all(Object.keys(entries).map(function(key) {
215 return cache.match(entries[key].url)
216 .then(function(result) {
217 return result.text();
218 })
219 .then(function(body) {
220 assert_equals(body, entries[key].body,
221 'Cache put should store response body.');
222 });
223 }));
224 });
225 }, 'Cache.put with request URLs containing embedded credentials');
155 226
156 cache_test(function(cache) { 227 cache_test(function(cache) {
157 var url = 'http://example.com/foo'; 228 var url = 'http://example.com/foo';
158 return cache.put(url, new_test_response('some body')) 229 return cache.put(url, new_test_response('some body'))
159 .then(function() { return cache.match(url); }) 230 .then(function() { return cache.match(url); })
160 .then(function(response) { return response.text(); }) 231 .then(function(response) { return response.text(); })
161 .then(function(body) { 232 .then(function(body) {
162 assert_equals(body, 'some body', 233 assert_equals(body, 'some body',
163 'Cache.put should accept a string as request.'); 234 'Cache.put should accept a string as request.');
164 }); 235 });
165 }, 'Cache.put with an string request'); 236 }, 'Cache.put with a string request');
166 237
167 cache_test(function(cache) { 238 cache_test(function(cache) {
168 return assert_promise_rejects( 239 return assert_promise_rejects(
169 cache.put(new_test_request(), 'Hello world!'), 240 cache.put(new_test_request(), 'Hello world!'),
170 new TypeError(), 241 new TypeError(),
171 'Cache.put should only accept a Response object as the response.'); 242 'Cache.put should only accept a Response object as the response.');
172 }, 'Cache.put with an invalid response'); 243 }, 'Cache.put with an invalid response');
173 244
174 cache_test(function(cache) { 245 cache_test(function(cache) {
175 return assert_promise_rejects( 246 return assert_promise_rejects(
(...skipping 22 matching lines...) Expand all
198 new TypeError(), 269 new TypeError(),
199 'Cache.put should throw a TypeError for non-GET requests.'); 270 'Cache.put should throw a TypeError for non-GET requests.');
200 }, 'Cache.put with a non-GET request'); 271 }, 'Cache.put with a non-GET request');
201 272
202 cache_test(function(cache) { 273 cache_test(function(cache) {
203 return assert_promise_rejects( 274 return assert_promise_rejects(
204 cache.put(new_test_request(), null), 275 cache.put(new_test_request(), null),
205 new TypeError(), 276 new TypeError(),
206 'Cache.put should throw a TypeError for an empty response.'); 277 'Cache.put should throw a TypeError for an empty response.');
207 }, 'Cache.put with an empty response'); 278 }, 'Cache.put with an empty response');
OLDNEW
« no previous file with comments | « LayoutTests/http/tests/serviceworker/cache-put-expected.txt ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698