| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 <map> | 5 #include <map> |
| 6 #include <memory> | 6 #include <memory> |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| (...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 326 // base::RunLoop that we'll run until the event has been dispatched. If this | 326 // base::RunLoop that we'll run until the event has been dispatched. If this |
| 327 // test times out, it means that the event could not be dispatched. | 327 // test times out, it means that the event could not be dispatched. |
| 328 base::RunLoop event_dispatched_loop; | 328 base::RunLoop event_dispatched_loop; |
| 329 embedded_worker_test_helper()->set_fetched_event_closure( | 329 embedded_worker_test_helper()->set_fetched_event_closure( |
| 330 event_dispatched_loop.QuitClosure()); | 330 event_dispatched_loop.QuitClosure()); |
| 331 | 331 |
| 332 std::vector<ServiceWorkerFetchRequest> requests; | 332 std::vector<ServiceWorkerFetchRequest> requests; |
| 333 | 333 |
| 334 constexpr int kFirstResponseCode = 200; | 334 constexpr int kFirstResponseCode = 200; |
| 335 constexpr int kSecondResponseCode = 201; | 335 constexpr int kSecondResponseCode = 201; |
| 336 constexpr int kThirdResponseCode = 200; |
| 336 | 337 |
| 337 requests.push_back(CreateRequestWithProvidedResponse( | 338 requests.push_back(CreateRequestWithProvidedResponse( |
| 338 "GET", "https://example.com/funny_cat.txt", | 339 "GET", "https://example.com/funny_cat.txt", |
| 339 TestResponseBuilder(kFirstResponseCode) | 340 TestResponseBuilder(kFirstResponseCode) |
| 340 .SetResponseData( | 341 .SetResponseData( |
| 341 "This text describes a scenario involving a funny cat.") | 342 "This text describes a scenario involving a funny cat.") |
| 342 .AddResponseHeader("Content-Type", "text/plain") | 343 .AddResponseHeader("Content-Type", "text/plain") |
| 343 .AddResponseHeader("X-Cat", "yes") | 344 .AddResponseHeader("X-Cat", "yes") |
| 344 .Build())); | 345 .Build())); |
| 345 | 346 |
| 346 requests.push_back(CreateRequestWithProvidedResponse( | 347 requests.push_back(CreateRequestWithProvidedResponse( |
| 347 "GET", "https://example.com/crazy_cat.txt", | 348 "GET", "https://example.com/crazy_cat.txt", |
| 348 TestResponseBuilder(kSecondResponseCode) | 349 TestResponseBuilder(kSecondResponseCode) |
| 349 .SetResponseData( | 350 .SetResponseData( |
| 350 "This text describes another scenario that involves a crazy cat.") | 351 "This text describes another scenario that involves a crazy cat.") |
| 351 .AddResponseHeader("Content-Type", "text/plain") | 352 .AddResponseHeader("Content-Type", "text/plain") |
| 352 .Build())); | 353 .Build())); |
| 353 | 354 |
| 355 requests.push_back(CreateRequestWithProvidedResponse( |
| 356 "GET", "https://chrome.com/accessible_cross_origin_cat.txt", |
| 357 TestResponseBuilder(kThirdResponseCode) |
| 358 .SetResponseData("This cat originates from another origin.") |
| 359 .AddResponseHeader("Access-Control-Allow-Origin", "*") |
| 360 .AddResponseHeader("Content-Type", "text/plain") |
| 361 .Build())); |
| 362 |
| 354 // Create the registration with the given |requests|. | 363 // Create the registration with the given |requests|. |
| 355 { | 364 { |
| 356 BackgroundFetchOptions options; | 365 BackgroundFetchOptions options; |
| 357 | 366 |
| 358 blink::mojom::BackgroundFetchError error; | 367 blink::mojom::BackgroundFetchError error; |
| 359 BackgroundFetchRegistration registration; | 368 BackgroundFetchRegistration registration; |
| 360 | 369 |
| 361 // Create the first registration. This must succeed. | 370 // Create the first registration. This must succeed. |
| 362 ASSERT_NO_FATAL_FAILURE( | 371 ASSERT_NO_FATAL_FAILURE( |
| 363 Fetch(registration_id, requests, options, &error, ®istration)); | 372 Fetch(registration_id, requests, options, &error, ®istration)); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 388 case 0: | 397 case 0: |
| 389 EXPECT_EQ(fetches[i].response.status_code, kFirstResponseCode); | 398 EXPECT_EQ(fetches[i].response.status_code, kFirstResponseCode); |
| 390 EXPECT_EQ(fetches[i].response.headers.count("Content-Type"), 1u); | 399 EXPECT_EQ(fetches[i].response.headers.count("Content-Type"), 1u); |
| 391 EXPECT_EQ(fetches[i].response.headers.count("X-Cat"), 1u); | 400 EXPECT_EQ(fetches[i].response.headers.count("X-Cat"), 1u); |
| 392 break; | 401 break; |
| 393 case 1: | 402 case 1: |
| 394 EXPECT_EQ(fetches[i].response.status_code, kSecondResponseCode); | 403 EXPECT_EQ(fetches[i].response.status_code, kSecondResponseCode); |
| 395 EXPECT_EQ(fetches[i].response.headers.count("Content-Type"), 1u); | 404 EXPECT_EQ(fetches[i].response.headers.count("Content-Type"), 1u); |
| 396 EXPECT_EQ(fetches[i].response.headers.count("X-Cat"), 0u); | 405 EXPECT_EQ(fetches[i].response.headers.count("X-Cat"), 0u); |
| 397 break; | 406 break; |
| 407 case 2: |
| 408 EXPECT_EQ(fetches[i].response.status_code, kThirdResponseCode); |
| 409 EXPECT_EQ(fetches[i].response.headers.count("Content-Type"), 1u); |
| 410 EXPECT_EQ(fetches[i].response.headers.count("X-Cat"), 0u); |
| 411 break; |
| 398 default: | 412 default: |
| 399 NOTREACHED(); | 413 NOTREACHED(); |
| 400 } | 414 } |
| 401 | 415 |
| 402 // TODO(peter): change-detector tests for unsupported properties. | 416 // TODO(peter): change-detector tests for unsupported properties. |
| 403 EXPECT_EQ(fetches[i].response.error, | 417 EXPECT_EQ(fetches[i].response.error, |
| 404 blink::kWebServiceWorkerResponseErrorUnknown); | 418 blink::kWebServiceWorkerResponseErrorUnknown); |
| 405 | 419 |
| 406 // Verify that all properties have a sensible value. | 420 // Verify that all properties have a sensible value. |
| 407 EXPECT_FALSE(fetches[i].response.response_time.is_null()); | 421 EXPECT_FALSE(fetches[i].response.response_time.is_null()); |
| 408 | 422 |
| 409 // Verify that the response blobs have been populated. We cannot consume | 423 // Verify that the response blobs have been populated. We cannot consume |
| 410 // their data here since the handles have already been released. | 424 // their data here since the handles have already been released. |
| 411 ASSERT_FALSE(fetches[i].response.blob_uuid.empty()); | 425 ASSERT_FALSE(fetches[i].response.blob_uuid.empty()); |
| 412 ASSERT_GT(fetches[i].response.blob_size, 0u); | 426 ASSERT_GT(fetches[i].response.blob_size, 0u); |
| 413 } | 427 } |
| 414 } | 428 } |
| 415 | 429 |
| 430 TEST_F(BackgroundFetchServiceTest, FetchFailEventDispatch) { |
| 431 // This test verifies that the fail event will be fired when a response either |
| 432 // has a non-OK status code, or the response cannot be accessed due to CORS. |
| 433 |
| 434 BackgroundFetchRegistrationId registration_id; |
| 435 ASSERT_TRUE(CreateRegistrationId(kExampleTag, ®istration_id)); |
| 436 |
| 437 // base::RunLoop that we'll run until the event has been dispatched. If this |
| 438 // test times out, it means that the event could not be dispatched. |
| 439 base::RunLoop event_dispatched_loop; |
| 440 embedded_worker_test_helper()->set_fetch_fail_event_closure( |
| 441 event_dispatched_loop.QuitClosure()); |
| 442 |
| 443 std::vector<ServiceWorkerFetchRequest> requests; |
| 444 |
| 445 constexpr int kFirstResponseCode = 404; |
| 446 constexpr int kSecondResponseCode = 200; |
| 447 |
| 448 requests.push_back(CreateRequestWithProvidedResponse( |
| 449 "GET", "https://example.com/not_existing_cat.txt", |
| 450 TestResponseBuilder(kFirstResponseCode).Build())); |
| 451 |
| 452 requests.push_back(CreateRequestWithProvidedResponse( |
| 453 "GET", "https://chrome.com/inaccessible_cross_origin_cat.txt", |
| 454 TestResponseBuilder(kSecondResponseCode) |
| 455 .SetResponseData( |
| 456 "This is a cross-origin response not accessible to the reader.") |
| 457 .AddResponseHeader("Content-Type", "text/plain") |
| 458 .Build())); |
| 459 |
| 460 // Create the registration with the given |requests|. |
| 461 { |
| 462 BackgroundFetchOptions options; |
| 463 |
| 464 blink::mojom::BackgroundFetchError error; |
| 465 BackgroundFetchRegistration registration; |
| 466 |
| 467 // Create the first registration. This must succeed. |
| 468 ASSERT_NO_FATAL_FAILURE( |
| 469 Fetch(registration_id, requests, options, &error, ®istration)); |
| 470 ASSERT_EQ(error, blink::mojom::BackgroundFetchError::NONE); |
| 471 } |
| 472 |
| 473 // Spin the |event_dispatched_loop| to wait for the dispatched event. |
| 474 event_dispatched_loop.Run(); |
| 475 |
| 476 ASSERT_TRUE(embedded_worker_test_helper()->last_tag().has_value()); |
| 477 EXPECT_EQ(kExampleTag, embedded_worker_test_helper()->last_tag().value()); |
| 478 |
| 479 ASSERT_TRUE(embedded_worker_test_helper()->last_fetches().has_value()); |
| 480 |
| 481 std::vector<BackgroundFetchSettledFetch> fetches = |
| 482 embedded_worker_test_helper()->last_fetches().value(); |
| 483 ASSERT_EQ(fetches.size(), requests.size()); |
| 484 |
| 485 for (size_t i = 0; i < fetches.size(); ++i) { |
| 486 ASSERT_EQ(fetches[i].request.url, requests[i].url); |
| 487 EXPECT_EQ(fetches[i].request.method, requests[i].method); |
| 488 |
| 489 EXPECT_EQ(fetches[i].response.url_list[0], fetches[i].request.url); |
| 490 EXPECT_EQ(fetches[i].response.response_type, |
| 491 blink::kWebServiceWorkerResponseTypeDefault); |
| 492 |
| 493 switch (i) { |
| 494 case 0: |
| 495 EXPECT_EQ(fetches[i].response.status_code, 404); |
| 496 break; |
| 497 case 1: |
| 498 EXPECT_EQ(fetches[i].response.status_code, 0); |
| 499 break; |
| 500 default: |
| 501 NOTREACHED(); |
| 502 } |
| 503 |
| 504 EXPECT_TRUE(fetches[i].response.headers.empty()); |
| 505 EXPECT_TRUE(fetches[i].response.blob_uuid.empty()); |
| 506 EXPECT_EQ(fetches[i].response.blob_size, 0u); |
| 507 EXPECT_FALSE(fetches[i].response.response_time.is_null()); |
| 508 |
| 509 // TODO(peter): change-detector tests for unsupported properties. |
| 510 EXPECT_EQ(fetches[i].response.error, |
| 511 blink::kWebServiceWorkerResponseErrorUnknown); |
| 512 EXPECT_TRUE(fetches[i].response.cors_exposed_header_names.empty()); |
| 513 } |
| 514 } |
| 515 |
| 416 TEST_F(BackgroundFetchServiceTest, Abort) { | 516 TEST_F(BackgroundFetchServiceTest, Abort) { |
| 417 // This test starts a new Background Fetch, completes the registration, and | 517 // This test starts a new Background Fetch, completes the registration, and |
| 418 // then aborts the Background Fetch mid-process. Tests all of StartFetch(), | 518 // then aborts the Background Fetch mid-process. Tests all of StartFetch(), |
| 419 // GetActiveFetches() and GetActiveTagsForServiceWorkerRegistration(). | 519 // GetActiveFetches() and GetActiveTagsForServiceWorkerRegistration(). |
| 420 | 520 |
| 421 BackgroundFetchRegistrationId registration_id; | 521 BackgroundFetchRegistrationId registration_id; |
| 422 ASSERT_TRUE(CreateRegistrationId(kExampleTag, ®istration_id)); | 522 ASSERT_TRUE(CreateRegistrationId(kExampleTag, ®istration_id)); |
| 423 | 523 |
| 424 std::vector<ServiceWorkerFetchRequest> requests; | 524 std::vector<ServiceWorkerFetchRequest> requests; |
| 425 requests.emplace_back(); // empty, but valid | 525 requests.emplace_back(); // empty, but valid |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 551 const bool has_alternative_tag = | 651 const bool has_alternative_tag = |
| 552 tags[0] == kAlternativeTag || tags[1] == kAlternativeTag; | 652 tags[0] == kAlternativeTag || tags[1] == kAlternativeTag; |
| 553 | 653 |
| 554 EXPECT_TRUE(has_example_tag); | 654 EXPECT_TRUE(has_example_tag); |
| 555 EXPECT_TRUE(has_alternative_tag); | 655 EXPECT_TRUE(has_alternative_tag); |
| 556 } | 656 } |
| 557 } | 657 } |
| 558 | 658 |
| 559 } // namespace | 659 } // namespace |
| 560 } // namespace content | 660 } // namespace content |
| OLD | NEW |