#include "content/browser/service_worker/service_worker_controllee_request_handler.h"
#include <memory>
#include <utility>
#include <vector>
#include "base/files/scoped_temp_dir.h"
#include "base/functional/callback_helpers.h"
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "components/offline_pages/buildflags/buildflags.h"
#include "content/browser/loader/response_head_update_params.h"
#include "content/browser/service_worker/embedded_worker_test_helper.h"
#include "content/browser/service_worker/service_worker_client.h"
#include "content/browser/service_worker/service_worker_context_core.h"
#include "content/browser/service_worker/service_worker_context_wrapper.h"
#include "content/browser/service_worker/service_worker_host.h"
#include "content/browser/service_worker/service_worker_registration.h"
#include "content/browser/service_worker/service_worker_test_utils.h"
#include "content/public/common/content_client.h"
#include "content/public/common/content_features.h"
#include "content/public/test/browser_task_environment.h"
#include "content/test/test_content_browser_client.h"
#include "net/cookies/site_for_cookies.h"
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_builder.h"
#include "net/url_request/url_request_test_util.h"
#include "services/network/public/cpp/resource_request_body.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/service_worker/embedded_worker_status.h"
#include "third_party/blink/public/common/storage_key/storage_key.h"
#include "third_party/blink/public/mojom/fetch/fetch_api_request.mojom.h"
#include "third_party/blink/public/mojom/service_worker/service_worker_registration.mojom.h"
#include "third_party/blink/public/mojom/service_worker/service_worker_registration_options.mojom.h"
#include "url/origin.h"
namespace content {
namespace service_worker_controllee_request_handler_unittest {
namespace {
class DeleteAndStartOverWaiter : public ServiceWorkerContextCoreObserver { … };
}
class ServiceWorkerControlleeRequestHandlerTest : public testing::Test { … };
class ServiceWorkerTestContentBrowserClient : public TestContentBrowserClient { … };
TEST_F(ServiceWorkerControlleeRequestHandlerTest, Basic) { … }
TEST_F(ServiceWorkerControlleeRequestHandlerTest, DoesNotExist) { … }
TEST_F(ServiceWorkerControlleeRequestHandlerTest, Error) { … }
TEST_F(ServiceWorkerControlleeRequestHandlerTest, DisallowServiceWorker) { … }
TEST_F(ServiceWorkerControlleeRequestHandlerTest, InsecureContext) { … }
TEST_F(ServiceWorkerControlleeRequestHandlerTest, ActivateWaitingVersion) { … }
TEST_F(ServiceWorkerControlleeRequestHandlerTest, InstallingRegistration) { … }
TEST_F(ServiceWorkerControlleeRequestHandlerTest, DeletedContainerHost) { … }
TEST_F(ServiceWorkerControlleeRequestHandlerTest, SkipServiceWorker) { … }
TEST_F(ServiceWorkerControlleeRequestHandlerTest, NullContext) { … }
#if BUILDFLAG(ENABLE_OFFLINE_PAGES)
TEST_F(ServiceWorkerControlleeRequestHandlerTest, FallbackWithOfflineHeader) {
version_->set_fetch_handler_type(
ServiceWorkerVersion::FetchHandlerType::kNotSkippable);
version_->SetStatus(ServiceWorkerVersion::ACTIVATED);
registration_->SetActiveVersion(version_);
base::RunLoop loop;
context()->registry()->StoreRegistration(
registration_.get(), version_.get(),
base::BindLambdaForTesting(
[&loop](blink::ServiceWorkerStatusCode status) { loop.Quit(); }));
loop.Run();
version_.reset();
registration_.reset();
ServiceWorkerRequestTestResources test_resources(
this, GURL("https://host/scope/doc"),
network::mojom::RequestDestination::kDocument);
test_resources.request()->SetExtraRequestHeaderByName(
"X-Chrome-offline", "reason=download", true);
test_resources.MaybeCreateLoader();
test_resources.WaitLoader();
EXPECT_FALSE(test_resources.loader());
}
TEST_F(ServiceWorkerControlleeRequestHandlerTest, FallbackWithNoOfflineHeader) {
version_->set_fetch_handler_type(
ServiceWorkerVersion::FetchHandlerType::kNotSkippable);
version_->SetStatus(ServiceWorkerVersion::ACTIVATED);
registration_->SetActiveVersion(version_);
base::RunLoop loop;
context()->registry()->StoreRegistration(
registration_.get(), version_.get(),
base::BindLambdaForTesting(
[&loop](blink::ServiceWorkerStatusCode status) { loop.Quit(); }));
loop.Run();
version_.reset();
registration_.reset();
ServiceWorkerRequestTestResources test_resources(
this, GURL("https://host/scope/doc"),
network::mojom::RequestDestination::kDocument);
test_resources.request()->SetExtraRequestHeaderByName("X-Chrome-offline", "",
true);
test_resources.MaybeCreateLoader();
test_resources.WaitLoader();
EXPECT_TRUE(test_resources.loader());
}
#endif
}
}