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

Unified Diff: content/browser/background_fetch/background_fetch_service_unittest.cc

Issue 2816403004: Fire the `backgroundfetchfail` event for failed fetches (Closed)
Patch Set: 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « content/browser/background_fetch/background_fetch_data_manager.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: content/browser/background_fetch/background_fetch_service_unittest.cc
diff --git a/content/browser/background_fetch/background_fetch_service_unittest.cc b/content/browser/background_fetch/background_fetch_service_unittest.cc
index db4111194c8acef674d35be7377549552ca5b80d..51aadc5d7117de17418bc62d5b827067101ed0c2 100644
--- a/content/browser/background_fetch/background_fetch_service_unittest.cc
+++ b/content/browser/background_fetch/background_fetch_service_unittest.cc
@@ -333,6 +333,7 @@ TEST_F(BackgroundFetchServiceTest, FetchSuccessEventDispatch) {
constexpr int kFirstResponseCode = 200;
constexpr int kSecondResponseCode = 201;
+ constexpr int kThirdResponseCode = 200;
requests.push_back(CreateRequestWithProvidedResponse(
"GET", "https://example.com/funny_cat.txt",
@@ -351,6 +352,14 @@ TEST_F(BackgroundFetchServiceTest, FetchSuccessEventDispatch) {
.AddResponseHeader("Content-Type", "text/plain")
.Build()));
+ requests.push_back(CreateRequestWithProvidedResponse(
+ "GET", "https://chrome.com/accessible_cross_origin_cat.txt",
+ TestResponseBuilder(kThirdResponseCode)
+ .SetResponseData("This cat originates from another origin.")
+ .AddResponseHeader("Access-Control-Allow-Origin", "*")
+ .AddResponseHeader("Content-Type", "text/plain")
+ .Build()));
+
// Create the registration with the given |requests|.
{
BackgroundFetchOptions options;
@@ -395,6 +404,11 @@ TEST_F(BackgroundFetchServiceTest, FetchSuccessEventDispatch) {
EXPECT_EQ(fetches[i].response.headers.count("Content-Type"), 1u);
EXPECT_EQ(fetches[i].response.headers.count("X-Cat"), 0u);
break;
+ case 2:
+ EXPECT_EQ(fetches[i].response.status_code, kThirdResponseCode);
+ EXPECT_EQ(fetches[i].response.headers.count("Content-Type"), 1u);
+ EXPECT_EQ(fetches[i].response.headers.count("X-Cat"), 0u);
+ break;
default:
NOTREACHED();
}
@@ -413,6 +427,92 @@ TEST_F(BackgroundFetchServiceTest, FetchSuccessEventDispatch) {
}
}
+TEST_F(BackgroundFetchServiceTest, FetchFailEventDispatch) {
+ // This test verifies that the fail event will be fired when a response either
+ // has a non-OK status code, or the response cannot be accessed due to CORS.
+
+ BackgroundFetchRegistrationId registration_id;
+ ASSERT_TRUE(CreateRegistrationId(kExampleTag, &registration_id));
+
+ // base::RunLoop that we'll run until the event has been dispatched. If this
+ // test times out, it means that the event could not be dispatched.
+ base::RunLoop event_dispatched_loop;
+ embedded_worker_test_helper()->set_fetch_fail_event_closure(
+ event_dispatched_loop.QuitClosure());
+
+ std::vector<ServiceWorkerFetchRequest> requests;
+
+ constexpr int kFirstResponseCode = 404;
+ constexpr int kSecondResponseCode = 200;
+
+ requests.push_back(CreateRequestWithProvidedResponse(
+ "GET", "https://example.com/not_existing_cat.txt",
+ TestResponseBuilder(kFirstResponseCode).Build()));
+
+ requests.push_back(CreateRequestWithProvidedResponse(
+ "GET", "https://chrome.com/inaccessible_cross_origin_cat.txt",
+ TestResponseBuilder(kSecondResponseCode)
+ .SetResponseData(
+ "This is a cross-origin response not accessible to the reader.")
+ .AddResponseHeader("Content-Type", "text/plain")
+ .Build()));
+
+ // Create the registration with the given |requests|.
+ {
+ BackgroundFetchOptions options;
+
+ blink::mojom::BackgroundFetchError error;
+ BackgroundFetchRegistration registration;
+
+ // Create the first registration. This must succeed.
+ ASSERT_NO_FATAL_FAILURE(
+ Fetch(registration_id, requests, options, &error, &registration));
+ ASSERT_EQ(error, blink::mojom::BackgroundFetchError::NONE);
+ }
+
+ // Spin the |event_dispatched_loop| to wait for the dispatched event.
+ event_dispatched_loop.Run();
+
+ ASSERT_TRUE(embedded_worker_test_helper()->last_tag().has_value());
+ EXPECT_EQ(kExampleTag, embedded_worker_test_helper()->last_tag().value());
+
+ ASSERT_TRUE(embedded_worker_test_helper()->last_fetches().has_value());
+
+ std::vector<BackgroundFetchSettledFetch> fetches =
+ embedded_worker_test_helper()->last_fetches().value();
+ ASSERT_EQ(fetches.size(), requests.size());
+
+ for (size_t i = 0; i < fetches.size(); ++i) {
+ ASSERT_EQ(fetches[i].request.url, requests[i].url);
+ EXPECT_EQ(fetches[i].request.method, requests[i].method);
+
+ EXPECT_EQ(fetches[i].response.url_list[0], fetches[i].request.url);
+ EXPECT_EQ(fetches[i].response.response_type,
+ blink::kWebServiceWorkerResponseTypeDefault);
+
+ switch (i) {
+ case 0:
+ EXPECT_EQ(fetches[i].response.status_code, 404);
+ break;
+ case 1:
+ EXPECT_EQ(fetches[i].response.status_code, 0);
+ break;
+ default:
+ NOTREACHED();
+ }
+
+ EXPECT_TRUE(fetches[i].response.headers.empty());
+ EXPECT_TRUE(fetches[i].response.blob_uuid.empty());
+ EXPECT_EQ(fetches[i].response.blob_size, 0u);
+ EXPECT_FALSE(fetches[i].response.response_time.is_null());
+
+ // TODO(peter): change-detector tests for unsupported properties.
+ EXPECT_EQ(fetches[i].response.error,
+ blink::kWebServiceWorkerResponseErrorUnknown);
+ EXPECT_TRUE(fetches[i].response.cors_exposed_header_names.empty());
+ }
+}
+
TEST_F(BackgroundFetchServiceTest, Abort) {
// This test starts a new Background Fetch, completes the registration, and
// then aborts the Background Fetch mid-process. Tests all of StartFetch(),
« no previous file with comments | « content/browser/background_fetch/background_fetch_data_manager.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698