| OLD | NEW |
| 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 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 provider_host()->provider_id()) | 188 provider_host()->provider_id()) |
| 189 .get(), | 189 .get(), |
| 190 "<../foo.js>; rel=serviceworker", context_wrapper()); | 190 "<../foo.js>; rel=serviceworker", context_wrapper()); |
| 191 base::RunLoop().RunUntilIdle(); | 191 base::RunLoop().RunUntilIdle(); |
| 192 | 192 |
| 193 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); | 193 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); |
| 194 ASSERT_EQ(1u, registrations.size()); | 194 ASSERT_EQ(1u, registrations.size()); |
| 195 EXPECT_EQ(GURL("https://example.com/foo/"), registrations[0].pattern); | 195 EXPECT_EQ(GURL("https://example.com/foo/"), registrations[0].pattern); |
| 196 EXPECT_EQ(GURL("https://example.com/foo/foo.js"), | 196 EXPECT_EQ(GURL("https://example.com/foo/foo.js"), |
| 197 registrations[0].active_version.script_url); | 197 registrations[0].active_version.script_url); |
| 198 EXPECT_EQ(blink::WebServiceWorkerUpdateViaCache::kImports, |
| 199 registrations[0].update_via_cache); |
| 198 } | 200 } |
| 199 | 201 |
| 200 TEST_F(LinkHeaderServiceWorkerTest, InstallServiceWorker_ScopeWithFragment) { | 202 TEST_F(LinkHeaderServiceWorkerTest, InstallServiceWorker_ScopeWithFragment) { |
| 201 CreateDocumentProviderHost(); | 203 CreateDocumentProviderHost(); |
| 202 ProcessLinkHeaderForRequest( | 204 ProcessLinkHeaderForRequest( |
| 203 CreateSubresourceRequest(GURL("https://example.com/foo/bar/"), | 205 CreateSubresourceRequest(GURL("https://example.com/foo/bar/"), |
| 204 provider_host()->provider_id()) | 206 provider_host()->provider_id()) |
| 205 .get(), | 207 .get(), |
| 206 "<../bar.js>; rel=serviceworker; scope=\"scope#ref\"", context_wrapper()); | 208 "<../bar.js>; rel=serviceworker; scope=\"scope#ref\"", context_wrapper()); |
| 207 base::RunLoop().RunUntilIdle(); | 209 base::RunLoop().RunUntilIdle(); |
| 208 | 210 |
| 209 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); | 211 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); |
| 210 ASSERT_EQ(1u, registrations.size()); | 212 ASSERT_EQ(1u, registrations.size()); |
| 211 EXPECT_EQ(GURL("https://example.com/foo/bar/scope"), | 213 EXPECT_EQ(GURL("https://example.com/foo/bar/scope"), |
| 212 registrations[0].pattern); | 214 registrations[0].pattern); |
| 213 EXPECT_EQ(GURL("https://example.com/foo/bar.js"), | 215 EXPECT_EQ(GURL("https://example.com/foo/bar.js"), |
| 214 registrations[0].active_version.script_url); | 216 registrations[0].active_version.script_url); |
| 217 EXPECT_EQ(blink::WebServiceWorkerUpdateViaCache::kImports, |
| 218 registrations[0].update_via_cache); |
| 215 } | 219 } |
| 216 | 220 |
| 217 TEST_F(LinkHeaderServiceWorkerTest, InstallServiceWorker_ScopeAbsoluteUrl) { | 221 TEST_F(LinkHeaderServiceWorkerTest, InstallServiceWorker_ScopeAbsoluteUrl) { |
| 218 CreateDocumentProviderHost(); | 222 CreateDocumentProviderHost(); |
| 219 ProcessLinkHeaderForRequest( | 223 ProcessLinkHeaderForRequest( |
| 220 CreateSubresourceRequest(GURL("https://example.com/foo/bar/"), | 224 CreateSubresourceRequest(GURL("https://example.com/foo/bar/"), |
| 221 provider_host()->provider_id()) | 225 provider_host()->provider_id()) |
| 222 .get(), | 226 .get(), |
| 223 "<bar.js>; rel=serviceworker; " | 227 "<bar.js>; rel=serviceworker; " |
| 224 "scope=\"https://example.com:443/foo/bar/scope\"", | 228 "scope=\"https://example.com:443/foo/bar/scope\"", |
| 225 context_wrapper()); | 229 context_wrapper()); |
| 226 base::RunLoop().RunUntilIdle(); | 230 base::RunLoop().RunUntilIdle(); |
| 227 | 231 |
| 228 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); | 232 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); |
| 229 ASSERT_EQ(1u, registrations.size()); | 233 ASSERT_EQ(1u, registrations.size()); |
| 230 EXPECT_EQ(GURL("https://example.com/foo/bar/scope"), | 234 EXPECT_EQ(GURL("https://example.com/foo/bar/scope"), |
| 231 registrations[0].pattern); | 235 registrations[0].pattern); |
| 232 EXPECT_EQ(GURL("https://example.com/foo/bar/bar.js"), | 236 EXPECT_EQ(GURL("https://example.com/foo/bar/bar.js"), |
| 233 registrations[0].active_version.script_url); | 237 registrations[0].active_version.script_url); |
| 238 EXPECT_EQ(blink::WebServiceWorkerUpdateViaCache::kImports, |
| 239 registrations[0].update_via_cache); |
| 234 } | 240 } |
| 235 | 241 |
| 236 TEST_F(LinkHeaderServiceWorkerTest, InstallServiceWorker_ScopeDifferentOrigin) { | 242 TEST_F(LinkHeaderServiceWorkerTest, InstallServiceWorker_ScopeDifferentOrigin) { |
| 237 CreateDocumentProviderHost(); | 243 CreateDocumentProviderHost(); |
| 238 ProcessLinkHeaderForRequest( | 244 ProcessLinkHeaderForRequest( |
| 239 CreateSubresourceRequest(GURL("https://example.com/foobar/"), | 245 CreateSubresourceRequest(GURL("https://example.com/foobar/"), |
| 240 provider_host()->provider_id()) | 246 provider_host()->provider_id()) |
| 241 .get(), | 247 .get(), |
| 242 "<bar.js>; rel=serviceworker; scope=\"https://google.com/scope\"", | 248 "<bar.js>; rel=serviceworker; scope=\"https://google.com/scope\"", |
| 243 context_wrapper()); | 249 context_wrapper()); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 282 .get(), | 288 .get(), |
| 283 "<https://example.com/bar.js>; rel=serviceworker; scope=foo", | 289 "<https://example.com/bar.js>; rel=serviceworker; scope=foo", |
| 284 context_wrapper()); | 290 context_wrapper()); |
| 285 base::RunLoop().RunUntilIdle(); | 291 base::RunLoop().RunUntilIdle(); |
| 286 | 292 |
| 287 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); | 293 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); |
| 288 ASSERT_EQ(1u, registrations.size()); | 294 ASSERT_EQ(1u, registrations.size()); |
| 289 EXPECT_EQ(GURL("https://example.com/foobar/foo"), registrations[0].pattern); | 295 EXPECT_EQ(GURL("https://example.com/foobar/foo"), registrations[0].pattern); |
| 290 EXPECT_EQ(GURL("https://example.com/bar.js"), | 296 EXPECT_EQ(GURL("https://example.com/bar.js"), |
| 291 registrations[0].active_version.script_url); | 297 registrations[0].active_version.script_url); |
| 298 EXPECT_EQ(blink::WebServiceWorkerUpdateViaCache::kImports, |
| 299 registrations[0].update_via_cache); |
| 292 } | 300 } |
| 293 | 301 |
| 294 TEST_F(LinkHeaderServiceWorkerTest, | 302 TEST_F(LinkHeaderServiceWorkerTest, |
| 295 InstallServiceWorker_ScriptDifferentOrigin) { | 303 InstallServiceWorker_ScriptDifferentOrigin) { |
| 296 CreateDocumentProviderHost(); | 304 CreateDocumentProviderHost(); |
| 297 ProcessLinkHeaderForRequest( | 305 ProcessLinkHeaderForRequest( |
| 298 CreateSubresourceRequest(GURL("https://example.com/foobar/"), | 306 CreateSubresourceRequest(GURL("https://example.com/foobar/"), |
| 299 provider_host()->provider_id()) | 307 provider_host()->provider_id()) |
| 300 .get(), | 308 .get(), |
| 301 "<https://google.com/bar.js>; rel=serviceworker; scope=foo", | 309 "<https://google.com/bar.js>; rel=serviceworker; scope=foo", |
| 302 context_wrapper()); | 310 context_wrapper()); |
| 303 base::RunLoop().RunUntilIdle(); | 311 base::RunLoop().RunUntilIdle(); |
| 304 | 312 |
| 305 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); | 313 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); |
| 306 ASSERT_EQ(0u, registrations.size()); | 314 ASSERT_EQ(0u, registrations.size()); |
| 307 } | 315 } |
| 308 | 316 |
| 317 TEST_F(LinkHeaderServiceWorkerTest, InstallServiceWorker_CacheNone) { |
| 318 CreateDocumentProviderHost(); |
| 319 ProcessLinkHeaderForRequest( |
| 320 CreateSubresourceRequest(GURL("https://example.com/foo/bar/"), |
| 321 provider_host()->provider_id()) |
| 322 .get(), |
| 323 "<../foo.js>; rel=serviceworker; updateviacache=none", context_wrapper()); |
| 324 base::RunLoop().RunUntilIdle(); |
| 325 |
| 326 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); |
| 327 ASSERT_EQ(1u, registrations.size()); |
| 328 EXPECT_EQ(GURL("https://example.com/foo/"), registrations[0].pattern); |
| 329 EXPECT_EQ(GURL("https://example.com/foo/foo.js"), |
| 330 registrations[0].active_version.script_url); |
| 331 EXPECT_EQ(blink::WebServiceWorkerUpdateViaCache::kNone, |
| 332 registrations[0].update_via_cache); |
| 333 } |
| 334 |
| 335 TEST_F(LinkHeaderServiceWorkerTest, InstallServiceWorker_CacheImports) { |
| 336 CreateDocumentProviderHost(); |
| 337 ProcessLinkHeaderForRequest( |
| 338 CreateSubresourceRequest(GURL("https://example.com/foo/bar/"), |
| 339 provider_host()->provider_id()) |
| 340 .get(), |
| 341 "<../foo.js>; rel=serviceworker; updateviacache=imports", |
| 342 context_wrapper()); |
| 343 base::RunLoop().RunUntilIdle(); |
| 344 |
| 345 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); |
| 346 ASSERT_EQ(1u, registrations.size()); |
| 347 EXPECT_EQ(GURL("https://example.com/foo/"), registrations[0].pattern); |
| 348 EXPECT_EQ(GURL("https://example.com/foo/foo.js"), |
| 349 registrations[0].active_version.script_url); |
| 350 EXPECT_EQ(blink::WebServiceWorkerUpdateViaCache::kImports, |
| 351 registrations[0].update_via_cache); |
| 352 } |
| 353 |
| 354 TEST_F(LinkHeaderServiceWorkerTest, InstallServiceWorker_CacheAll) { |
| 355 CreateDocumentProviderHost(); |
| 356 ProcessLinkHeaderForRequest( |
| 357 CreateSubresourceRequest(GURL("https://example.com/foo/bar/"), |
| 358 provider_host()->provider_id()) |
| 359 .get(), |
| 360 "<../foo.js>; rel=serviceworker; updateviacache=all", context_wrapper()); |
| 361 base::RunLoop().RunUntilIdle(); |
| 362 |
| 363 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); |
| 364 ASSERT_EQ(1u, registrations.size()); |
| 365 EXPECT_EQ(GURL("https://example.com/foo/"), registrations[0].pattern); |
| 366 EXPECT_EQ(GURL("https://example.com/foo/foo.js"), |
| 367 registrations[0].active_version.script_url); |
| 368 EXPECT_EQ(blink::WebServiceWorkerUpdateViaCache::kAll, |
| 369 registrations[0].update_via_cache); |
| 370 } |
| 371 |
| 372 TEST_F(LinkHeaderServiceWorkerTest, InstallServiceWorker_CacheInvalidValue) { |
| 373 CreateDocumentProviderHost(); |
| 374 ProcessLinkHeaderForRequest( |
| 375 CreateSubresourceRequest(GURL("https://example.com/foo/bar/"), |
| 376 provider_host()->provider_id()) |
| 377 .get(), |
| 378 "<../foo.js>; rel=serviceworker; updateviacache=invalidvalue", |
| 379 context_wrapper()); |
| 380 base::RunLoop().RunUntilIdle(); |
| 381 |
| 382 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); |
| 383 ASSERT_EQ(1u, registrations.size()); |
| 384 EXPECT_EQ(GURL("https://example.com/foo/"), registrations[0].pattern); |
| 385 EXPECT_EQ(GURL("https://example.com/foo/foo.js"), |
| 386 registrations[0].active_version.script_url); |
| 387 EXPECT_EQ(blink::WebServiceWorkerUpdateViaCache::kImports, |
| 388 registrations[0].update_via_cache); |
| 389 } |
| 390 |
| 309 TEST_F(LinkHeaderServiceWorkerTest, InstallServiceWorker_MultipleWorkers) { | 391 TEST_F(LinkHeaderServiceWorkerTest, InstallServiceWorker_MultipleWorkers) { |
| 310 CreateDocumentProviderHost(); | 392 CreateDocumentProviderHost(); |
| 311 ProcessLinkHeaderForRequest( | 393 ProcessLinkHeaderForRequest( |
| 312 CreateSubresourceRequest(GURL("https://example.com/foobar/"), | 394 CreateSubresourceRequest(GURL("https://example.com/foobar/"), |
| 313 provider_host()->provider_id()) | 395 provider_host()->provider_id()) |
| 314 .get(), | 396 .get(), |
| 315 "<bar.js>; rel=serviceworker; scope=foo, <baz.js>; " | 397 "<bar.js>; rel=serviceworker; scope=foo, <baz.js>; " |
| 316 "rel=serviceworker; scope=scope", | 398 "rel=serviceworker; scope=scope; updateviacache=all", |
| 317 context_wrapper()); | 399 context_wrapper()); |
| 318 base::RunLoop().RunUntilIdle(); | 400 base::RunLoop().RunUntilIdle(); |
| 319 | 401 |
| 320 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); | 402 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); |
| 321 ASSERT_EQ(2u, registrations.size()); | 403 ASSERT_EQ(2u, registrations.size()); |
| 322 EXPECT_EQ(GURL("https://example.com/foobar/foo"), registrations[0].pattern); | 404 EXPECT_EQ(GURL("https://example.com/foobar/foo"), registrations[0].pattern); |
| 323 EXPECT_EQ(GURL("https://example.com/foobar/bar.js"), | 405 EXPECT_EQ(GURL("https://example.com/foobar/bar.js"), |
| 324 registrations[0].active_version.script_url); | 406 registrations[0].active_version.script_url); |
| 407 EXPECT_EQ(blink::WebServiceWorkerUpdateViaCache::kAll, |
| 408 registrations[0].update_via_cache); |
| 325 EXPECT_EQ(GURL("https://example.com/foobar/scope"), registrations[1].pattern); | 409 EXPECT_EQ(GURL("https://example.com/foobar/scope"), registrations[1].pattern); |
| 326 EXPECT_EQ(GURL("https://example.com/foobar/baz.js"), | 410 EXPECT_EQ(GURL("https://example.com/foobar/baz.js"), |
| 327 registrations[1].active_version.script_url); | 411 registrations[1].active_version.script_url); |
| 412 EXPECT_EQ(blink::WebServiceWorkerUpdateViaCache::kAll, |
| 413 registrations[1].update_via_cache); |
| 328 } | 414 } |
| 329 | 415 |
| 330 TEST_F(LinkHeaderServiceWorkerTest, | 416 TEST_F(LinkHeaderServiceWorkerTest, |
| 331 InstallServiceWorker_ValidAndInvalidValues) { | 417 InstallServiceWorker_ValidAndInvalidValues) { |
| 332 CreateDocumentProviderHost(); | 418 CreateDocumentProviderHost(); |
| 333 ProcessLinkHeaderForRequest( | 419 ProcessLinkHeaderForRequest( |
| 334 CreateSubresourceRequest(GURL("https://example.com/foobar/"), | 420 CreateSubresourceRequest(GURL("https://example.com/foobar/"), |
| 335 provider_host()->provider_id()) | 421 provider_host()->provider_id()) |
| 336 .get(), | 422 .get(), |
| 337 "<https://google.com/bar.js>; rel=serviceworker; scope=foo, <baz.js>; " | 423 "<https://google.com/bar.js>; rel=serviceworker; scope=foo, <baz.js>; " |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 373 | 459 |
| 374 ProcessLinkHeaderForRequest(request.get(), "<../foo.js>; rel=serviceworker", | 460 ProcessLinkHeaderForRequest(request.get(), "<../foo.js>; rel=serviceworker", |
| 375 context_wrapper()); | 461 context_wrapper()); |
| 376 base::RunLoop().RunUntilIdle(); | 462 base::RunLoop().RunUntilIdle(); |
| 377 | 463 |
| 378 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); | 464 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); |
| 379 ASSERT_EQ(1u, registrations.size()); | 465 ASSERT_EQ(1u, registrations.size()); |
| 380 EXPECT_EQ(GURL("https://example.com/foo/"), registrations[0].pattern); | 466 EXPECT_EQ(GURL("https://example.com/foo/"), registrations[0].pattern); |
| 381 EXPECT_EQ(GURL("https://example.com/foo/foo.js"), | 467 EXPECT_EQ(GURL("https://example.com/foo/foo.js"), |
| 382 registrations[0].active_version.script_url); | 468 registrations[0].active_version.script_url); |
| 469 EXPECT_EQ(blink::WebServiceWorkerUpdateViaCache::kImports, |
| 470 registrations[0].update_via_cache); |
| 383 } | 471 } |
| 384 | 472 |
| 385 TEST_F(LinkHeaderServiceWorkerTest, | 473 TEST_F(LinkHeaderServiceWorkerTest, |
| 386 InstallServiceWorker_NavigationToInsecureContext) { | 474 InstallServiceWorker_NavigationToInsecureContext) { |
| 387 CreateDocumentProviderHost(); | 475 CreateDocumentProviderHost(); |
| 388 provider_host()->SetDocumentUrl(GURL("http://example.com/foo/bar/")); | 476 provider_host()->SetDocumentUrl(GURL("http://example.com/foo/bar/")); |
| 389 ProcessLinkHeaderForRequest( | 477 ProcessLinkHeaderForRequest( |
| 390 CreateRequest(GURL("http://example.com/foo/bar/"), | 478 CreateRequest(GURL("http://example.com/foo/bar/"), |
| 391 RESOURCE_TYPE_MAIN_FRAME, provider_host()->provider_id()) | 479 RESOURCE_TYPE_MAIN_FRAME, provider_host()->provider_id()) |
| 392 .get(), | 480 .get(), |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 443 provider_host()->provider_id()) | 531 provider_host()->provider_id()) |
| 444 .get(), | 532 .get(), |
| 445 "<../foo.js>; rel=serviceworker", context_wrapper()); | 533 "<../foo.js>; rel=serviceworker", context_wrapper()); |
| 446 base::RunLoop().RunUntilIdle(); | 534 base::RunLoop().RunUntilIdle(); |
| 447 | 535 |
| 448 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); | 536 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); |
| 449 ASSERT_EQ(1u, registrations.size()); | 537 ASSERT_EQ(1u, registrations.size()); |
| 450 } | 538 } |
| 451 | 539 |
| 452 } // namespace content | 540 } // namespace content |
| OLD | NEW |