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

Side by Side Diff: content/browser/service_worker/link_header_support_unittest.cc

Issue 2771823002: Implement updateViaCache flag and no-cache by default for main service worker scripts
Patch Set: fix tests Created 3 years, 8 months 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
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/service_worker/link_header_support.h" 5 #include "content/browser/service_worker/link_header_support.h"
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/run_loop.h" 9 #include "base/run_loop.h"
10 #include "content/browser/loader/resource_request_info_impl.h" 10 #include "content/browser/loader/resource_request_info_impl.h"
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
104 std::unique_ptr<ServiceWorkerProviderHost> host = 104 std::unique_ptr<ServiceWorkerProviderHost> host =
105 CreateProviderHostForServiceWorkerContext( 105 CreateProviderHostForServiceWorkerContext(
106 render_process_id(), kMockProviderId, 106 render_process_id(), kMockProviderId,
107 true /* is_parent_frame_secure */, context()->AsWeakPtr()); 107 true /* is_parent_frame_secure */, context()->AsWeakPtr());
108 provider_host_ = host->AsWeakPtr(); 108 provider_host_ = host->AsWeakPtr();
109 EXPECT_FALSE( 109 EXPECT_FALSE(
110 context()->GetProviderHost(host->process_id(), host->provider_id())); 110 context()->GetProviderHost(host->process_id(), host->provider_id()));
111 context()->AddProviderHost(std::move(host)); 111 context()->AddProviderHost(std::move(host));
112 112
113 scoped_refptr<ServiceWorkerRegistration> registration = 113 scoped_refptr<ServiceWorkerRegistration> registration =
114 new ServiceWorkerRegistration(GURL("https://host/scope"), 1L, 114 new ServiceWorkerRegistration(GURL("https://host/scope"),
115 false /* use_cache* */, 1L,
nhiroki 2017/03/28 06:49:51 There is an extra * ("use_cache*")
yuryu 2017/07/20 10:15:12 Acknowledged.
115 context()->AsWeakPtr()); 116 context()->AsWeakPtr());
116 scoped_refptr<ServiceWorkerVersion> version = new ServiceWorkerVersion( 117 scoped_refptr<ServiceWorkerVersion> version = new ServiceWorkerVersion(
117 registration.get(), GURL("https://host/script.js"), 1L, 118 registration.get(), GURL("https://host/script.js"), 1L,
118 context()->AsWeakPtr()); 119 context()->AsWeakPtr());
119 120
120 provider_host_->running_hosted_version_ = version; 121 provider_host_->running_hosted_version_ = version;
121 } 122 }
122 123
123 std::unique_ptr<net::URLRequest> CreateRequest(const GURL& request_url, 124 std::unique_ptr<net::URLRequest> CreateRequest(const GURL& request_url,
124 ResourceType resource_type) { 125 ResourceType resource_type) {
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
174 ProcessLinkHeaderForRequest( 175 ProcessLinkHeaderForRequest(
175 CreateSubresourceRequest(GURL("https://example.com/foo/bar/")).get(), 176 CreateSubresourceRequest(GURL("https://example.com/foo/bar/")).get(),
176 "<../foo.js>; rel=serviceworker", context_wrapper()); 177 "<../foo.js>; rel=serviceworker", context_wrapper());
177 base::RunLoop().RunUntilIdle(); 178 base::RunLoop().RunUntilIdle();
178 179
179 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); 180 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations();
180 ASSERT_EQ(1u, registrations.size()); 181 ASSERT_EQ(1u, registrations.size());
181 EXPECT_EQ(GURL("https://example.com/foo/"), registrations[0].pattern); 182 EXPECT_EQ(GURL("https://example.com/foo/"), registrations[0].pattern);
182 EXPECT_EQ(GURL("https://example.com/foo/foo.js"), 183 EXPECT_EQ(GURL("https://example.com/foo/foo.js"),
183 registrations[0].active_version.script_url); 184 registrations[0].active_version.script_url);
185 EXPECT_FALSE(registrations[0].use_cache);
184 } 186 }
185 187
186 TEST_F(LinkHeaderServiceWorkerTest, InstallServiceWorker_ScopeWithFragment) { 188 TEST_F(LinkHeaderServiceWorkerTest, InstallServiceWorker_ScopeWithFragment) {
187 CreateDocumentProviderHost(); 189 CreateDocumentProviderHost();
188 ProcessLinkHeaderForRequest( 190 ProcessLinkHeaderForRequest(
189 CreateSubresourceRequest(GURL("https://example.com/foo/bar/")).get(), 191 CreateSubresourceRequest(GURL("https://example.com/foo/bar/")).get(),
190 "<../bar.js>; rel=serviceworker; scope=\"scope#ref\"", context_wrapper()); 192 "<../bar.js>; rel=serviceworker; scope=\"scope#ref\"", context_wrapper());
191 base::RunLoop().RunUntilIdle(); 193 base::RunLoop().RunUntilIdle();
192 194
193 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); 195 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations();
194 ASSERT_EQ(1u, registrations.size()); 196 ASSERT_EQ(1u, registrations.size());
195 EXPECT_EQ(GURL("https://example.com/foo/bar/scope"), 197 EXPECT_EQ(GURL("https://example.com/foo/bar/scope"),
196 registrations[0].pattern); 198 registrations[0].pattern);
197 EXPECT_EQ(GURL("https://example.com/foo/bar.js"), 199 EXPECT_EQ(GURL("https://example.com/foo/bar.js"),
198 registrations[0].active_version.script_url); 200 registrations[0].active_version.script_url);
201 EXPECT_FALSE(registrations[0].use_cache);
199 } 202 }
200 203
201 TEST_F(LinkHeaderServiceWorkerTest, InstallServiceWorker_ScopeAbsoluteUrl) { 204 TEST_F(LinkHeaderServiceWorkerTest, InstallServiceWorker_ScopeAbsoluteUrl) {
202 CreateDocumentProviderHost(); 205 CreateDocumentProviderHost();
203 ProcessLinkHeaderForRequest( 206 ProcessLinkHeaderForRequest(
204 CreateSubresourceRequest(GURL("https://example.com/foo/bar/")).get(), 207 CreateSubresourceRequest(GURL("https://example.com/foo/bar/")).get(),
205 "<bar.js>; rel=serviceworker; " 208 "<bar.js>; rel=serviceworker; "
206 "scope=\"https://example.com:443/foo/bar/scope\"", 209 "scope=\"https://example.com:443/foo/bar/scope\"",
207 context_wrapper()); 210 context_wrapper());
208 base::RunLoop().RunUntilIdle(); 211 base::RunLoop().RunUntilIdle();
209 212
210 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); 213 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations();
211 ASSERT_EQ(1u, registrations.size()); 214 ASSERT_EQ(1u, registrations.size());
212 EXPECT_EQ(GURL("https://example.com/foo/bar/scope"), 215 EXPECT_EQ(GURL("https://example.com/foo/bar/scope"),
213 registrations[0].pattern); 216 registrations[0].pattern);
214 EXPECT_EQ(GURL("https://example.com/foo/bar/bar.js"), 217 EXPECT_EQ(GURL("https://example.com/foo/bar/bar.js"),
215 registrations[0].active_version.script_url); 218 registrations[0].active_version.script_url);
219 EXPECT_FALSE(registrations[0].use_cache);
216 } 220 }
217 221
218 TEST_F(LinkHeaderServiceWorkerTest, InstallServiceWorker_ScopeDifferentOrigin) { 222 TEST_F(LinkHeaderServiceWorkerTest, InstallServiceWorker_ScopeDifferentOrigin) {
219 CreateDocumentProviderHost(); 223 CreateDocumentProviderHost();
220 ProcessLinkHeaderForRequest( 224 ProcessLinkHeaderForRequest(
221 CreateSubresourceRequest(GURL("https://example.com/foobar/")).get(), 225 CreateSubresourceRequest(GURL("https://example.com/foobar/")).get(),
222 "<bar.js>; rel=serviceworker; scope=\"https://google.com/scope\"", 226 "<bar.js>; rel=serviceworker; scope=\"https://google.com/scope\"",
223 context_wrapper()); 227 context_wrapper());
224 base::RunLoop().RunUntilIdle(); 228 base::RunLoop().RunUntilIdle();
225 229
(...skipping 30 matching lines...) Expand all
256 CreateSubresourceRequest(GURL("https://example.com/foobar/")).get(), 260 CreateSubresourceRequest(GURL("https://example.com/foobar/")).get(),
257 "<https://example.com/bar.js>; rel=serviceworker; scope=foo", 261 "<https://example.com/bar.js>; rel=serviceworker; scope=foo",
258 context_wrapper()); 262 context_wrapper());
259 base::RunLoop().RunUntilIdle(); 263 base::RunLoop().RunUntilIdle();
260 264
261 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); 265 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations();
262 ASSERT_EQ(1u, registrations.size()); 266 ASSERT_EQ(1u, registrations.size());
263 EXPECT_EQ(GURL("https://example.com/foobar/foo"), registrations[0].pattern); 267 EXPECT_EQ(GURL("https://example.com/foobar/foo"), registrations[0].pattern);
264 EXPECT_EQ(GURL("https://example.com/bar.js"), 268 EXPECT_EQ(GURL("https://example.com/bar.js"),
265 registrations[0].active_version.script_url); 269 registrations[0].active_version.script_url);
270 EXPECT_FALSE(registrations[0].use_cache);
266 } 271 }
267 272
268 TEST_F(LinkHeaderServiceWorkerTest, 273 TEST_F(LinkHeaderServiceWorkerTest,
269 InstallServiceWorker_ScriptDifferentOrigin) { 274 InstallServiceWorker_ScriptDifferentOrigin) {
270 CreateDocumentProviderHost(); 275 CreateDocumentProviderHost();
271 ProcessLinkHeaderForRequest( 276 ProcessLinkHeaderForRequest(
272 CreateSubresourceRequest(GURL("https://example.com/foobar/")).get(), 277 CreateSubresourceRequest(GURL("https://example.com/foobar/")).get(),
273 "<https://google.com/bar.js>; rel=serviceworker; scope=foo", 278 "<https://google.com/bar.js>; rel=serviceworker; scope=foo",
274 context_wrapper()); 279 context_wrapper());
275 base::RunLoop().RunUntilIdle(); 280 base::RunLoop().RunUntilIdle();
276 281
277 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); 282 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations();
278 ASSERT_EQ(0u, registrations.size()); 283 ASSERT_EQ(0u, registrations.size());
279 } 284 }
280 285
286 TEST_F(LinkHeaderServiceWorkerTest, InstallServiceWorker_UseCache) {
287 CreateDocumentProviderHost();
288 ProcessLinkHeaderForRequest(
289 CreateSubresourceRequest(GURL("https://example.com/foo/bar/")).get(),
290 "<../foo.js>; rel=serviceworker; usecache", context_wrapper());
291 base::RunLoop().RunUntilIdle();
292
293 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations();
294 ASSERT_EQ(1u, registrations.size());
295 EXPECT_EQ(GURL("https://example.com/foo/"), registrations[0].pattern);
296 EXPECT_EQ(GURL("https://example.com/foo/foo.js"),
297 registrations[0].active_version.script_url);
298 EXPECT_TRUE(registrations[0].use_cache);
299 }
300
281 TEST_F(LinkHeaderServiceWorkerTest, InstallServiceWorker_MultipleWorkers) { 301 TEST_F(LinkHeaderServiceWorkerTest, InstallServiceWorker_MultipleWorkers) {
282 CreateDocumentProviderHost(); 302 CreateDocumentProviderHost();
283 ProcessLinkHeaderForRequest( 303 ProcessLinkHeaderForRequest(
284 CreateSubresourceRequest(GURL("https://example.com/foobar/")).get(), 304 CreateSubresourceRequest(GURL("https://example.com/foobar/")).get(),
285 "<bar.js>; rel=serviceworker; scope=foo, <baz.js>; " 305 "<bar.js>; rel=serviceworker; scope=foo, <baz.js>; "
286 "rel=serviceworker; scope=scope", 306 "rel=serviceworker; scope=scope; usecache",
287 context_wrapper()); 307 context_wrapper());
288 base::RunLoop().RunUntilIdle(); 308 base::RunLoop().RunUntilIdle();
289 309
290 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); 310 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations();
291 ASSERT_EQ(2u, registrations.size()); 311 ASSERT_EQ(2u, registrations.size());
292 EXPECT_EQ(GURL("https://example.com/foobar/foo"), registrations[0].pattern); 312 EXPECT_EQ(GURL("https://example.com/foobar/foo"), registrations[0].pattern);
293 EXPECT_EQ(GURL("https://example.com/foobar/bar.js"), 313 EXPECT_EQ(GURL("https://example.com/foobar/bar.js"),
294 registrations[0].active_version.script_url); 314 registrations[0].active_version.script_url);
315 EXPECT_FALSE(registrations[0].use_cache);
295 EXPECT_EQ(GURL("https://example.com/foobar/scope"), registrations[1].pattern); 316 EXPECT_EQ(GURL("https://example.com/foobar/scope"), registrations[1].pattern);
296 EXPECT_EQ(GURL("https://example.com/foobar/baz.js"), 317 EXPECT_EQ(GURL("https://example.com/foobar/baz.js"),
297 registrations[1].active_version.script_url); 318 registrations[1].active_version.script_url);
319 EXPECT_TRUE(registrations[1].use_cache);
298 } 320 }
299 321
300 TEST_F(LinkHeaderServiceWorkerTest, 322 TEST_F(LinkHeaderServiceWorkerTest,
301 InstallServiceWorker_ValidAndInvalidValues) { 323 InstallServiceWorker_ValidAndInvalidValues) {
302 CreateDocumentProviderHost(); 324 CreateDocumentProviderHost();
303 ProcessLinkHeaderForRequest( 325 ProcessLinkHeaderForRequest(
304 CreateSubresourceRequest(GURL("https://example.com/foobar/")).get(), 326 CreateSubresourceRequest(GURL("https://example.com/foobar/")).get(),
305 "<https://google.com/bar.js>; rel=serviceworker; scope=foo, <baz.js>; " 327 "<https://google.com/bar.js>; rel=serviceworker; scope=foo, <baz.js>; "
306 "rel=serviceworker; scope=scope", 328 "rel=serviceworker; scope=scope",
307 context_wrapper()); 329 context_wrapper());
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
340 362
341 ProcessLinkHeaderForRequest(request.get(), "<../foo.js>; rel=serviceworker", 363 ProcessLinkHeaderForRequest(request.get(), "<../foo.js>; rel=serviceworker",
342 context_wrapper()); 364 context_wrapper());
343 base::RunLoop().RunUntilIdle(); 365 base::RunLoop().RunUntilIdle();
344 366
345 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); 367 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations();
346 ASSERT_EQ(1u, registrations.size()); 368 ASSERT_EQ(1u, registrations.size());
347 EXPECT_EQ(GURL("https://example.com/foo/"), registrations[0].pattern); 369 EXPECT_EQ(GURL("https://example.com/foo/"), registrations[0].pattern);
348 EXPECT_EQ(GURL("https://example.com/foo/foo.js"), 370 EXPECT_EQ(GURL("https://example.com/foo/foo.js"),
349 registrations[0].active_version.script_url); 371 registrations[0].active_version.script_url);
372 EXPECT_FALSE(registrations[0].use_cache);
350 } 373 }
351 374
352 TEST_F(LinkHeaderServiceWorkerTest, 375 TEST_F(LinkHeaderServiceWorkerTest,
353 InstallServiceWorker_NavigationToInsecureContext) { 376 InstallServiceWorker_NavigationToInsecureContext) {
354 CreateDocumentProviderHost(); 377 CreateDocumentProviderHost();
355 provider_host()->SetDocumentUrl(GURL("http://example.com/foo/bar/")); 378 provider_host()->SetDocumentUrl(GURL("http://example.com/foo/bar/"));
356 ProcessLinkHeaderForRequest(CreateRequest(GURL("http://example.com/foo/bar/"), 379 ProcessLinkHeaderForRequest(CreateRequest(GURL("http://example.com/foo/bar/"),
357 RESOURCE_TYPE_MAIN_FRAME) 380 RESOURCE_TYPE_MAIN_FRAME)
358 .get(), 381 .get(),
359 "<../foo.js>; rel=serviceworker", 382 "<../foo.js>; rel=serviceworker",
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
404 ProcessLinkHeaderForRequest( 427 ProcessLinkHeaderForRequest(
405 CreateSubresourceRequest(GURL("https://example.com/foo/bar/")).get(), 428 CreateSubresourceRequest(GURL("https://example.com/foo/bar/")).get(),
406 "<../foo.js>; rel=serviceworker", context_wrapper()); 429 "<../foo.js>; rel=serviceworker", context_wrapper());
407 base::RunLoop().RunUntilIdle(); 430 base::RunLoop().RunUntilIdle();
408 431
409 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); 432 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations();
410 ASSERT_EQ(1u, registrations.size()); 433 ASSERT_EQ(1u, registrations.size());
411 } 434 }
412 435
413 } // namespace content 436 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698