1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/service_worker/service_worker_request_handler.h"
7 #include "base/run_loop.h"
8 #include "content/browser/fileapi/mock_url_request_delegate.h"
9 #include "content/browser/service_worker/embedded_worker_test_helper.h"
10 #include "content/browser/service_worker/service_worker_context_core.h"
11 #include "content/browser/service_worker/service_worker_provider_host.h"
12 #include "content/browser/service_worker/service_worker_registration.h"
13 #include "content/browser/service_worker/service_worker_utils.h"
14 #include "content/common/resource_request_body.h"
15 #include "content/public/common/request_context_frame_type.h"
16 #include "content/public/common/request_context_type.h"
17 #include "content/public/common/resource_type.h"
18 #include "content/public/test/test_browser_thread_bundle.h"
19 #include "net/url_request/url_request_context.h"
20 #include "storage/browser/blob/blob_storage_context.h"
21 #include "testing/gtest/include/gtest/gtest.h"
27 int kMockRenderProcessId = 1224;
28 int kMockProviderId = 1;
30 void EmptyCallback() {
35 class ServiceWorkerRequestHandlerTest : public testing::Test {
37 ServiceWorkerRequestHandlerTest()
38 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {}
40 void SetUp() override {
41 helper_.reset(new EmbeddedWorkerTestHelper(kMockRenderProcessId));
43 // A new unstored registration/version.
44 registration_ = new ServiceWorkerRegistration(
45 GURL("http://host/scope/"), 1L, context()->AsWeakPtr());
46 version_ = new ServiceWorkerVersion(registration_.get(),
47 GURL("http://host/script.js"),
49 context()->AsWeakPtr());
52 scoped_ptr<ServiceWorkerProviderHost> host(new ServiceWorkerProviderHost(
53 kMockRenderProcessId, kMockProviderId, context()->AsWeakPtr(), NULL));
54 provider_host_ = host->AsWeakPtr();
55 context()->AddProviderHost(host.Pass());
57 context()->storage()->LazyInitialize(base::Bind(&EmptyCallback));
58 base::RunLoop().RunUntilIdle();
60 version_->SetStatus(ServiceWorkerVersion::ACTIVATED);
61 registration_->SetActiveVersion(version_.get());
62 context()->storage()->StoreRegistration(
65 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback));
66 provider_host_->AssociateRegistration(registration_.get());
67 base::RunLoop().RunUntilIdle();
70 void TearDown() override {
76 ServiceWorkerContextCore* context() const { return helper_->context(); }
77 ServiceWorkerContextWrapper* context_wrapper() const {
78 return helper_->context_wrapper();
81 bool InitializeHandlerCheck(const std::string& url,
82 const std::string& method,
83 bool skip_service_worker,
84 ResourceType resource_type) {
85 const GURL kDocUrl(url);
86 scoped_ptr<net::URLRequest> request = url_request_context_.CreateRequest(
87 kDocUrl, net::DEFAULT_PRIORITY, &url_request_delegate_, NULL);
88 request->set_method(method);
89 ServiceWorkerRequestHandler::InitializeHandler(
92 &blob_storage_context_,
96 FETCH_REQUEST_MODE_NO_CORS,
97 FETCH_CREDENTIALS_MODE_OMIT,
99 REQUEST_CONTEXT_TYPE_HYPERLINK,
100 REQUEST_CONTEXT_FRAME_TYPE_TOP_LEVEL,
102 return ServiceWorkerRequestHandler::GetHandler(request.get()) != NULL;
106 TestBrowserThreadBundle browser_thread_bundle_;
107 scoped_ptr<EmbeddedWorkerTestHelper> helper_;
108 scoped_refptr<ServiceWorkerRegistration> registration_;
109 scoped_refptr<ServiceWorkerVersion> version_;
110 base::WeakPtr<ServiceWorkerProviderHost> provider_host_;
111 net::URLRequestContext url_request_context_;
112 MockURLRequestDelegate url_request_delegate_;
113 storage::BlobStorageContext blob_storage_context_;
116 TEST_F(ServiceWorkerRequestHandlerTest, InitializeHandler) {
117 EXPECT_TRUE(InitializeHandlerCheck(
118 "http://host/scope/doc", "GET", false, RESOURCE_TYPE_MAIN_FRAME));
119 EXPECT_TRUE(InitializeHandlerCheck(
120 "https://host/scope/doc", "GET", false, RESOURCE_TYPE_MAIN_FRAME));
121 EXPECT_FALSE(InitializeHandlerCheck(
122 "ftp://host/scope/doc", "GET", false, RESOURCE_TYPE_MAIN_FRAME));
124 EXPECT_FALSE(InitializeHandlerCheck(
125 "http://host/scope/doc", "OPTIONS", false, RESOURCE_TYPE_MAIN_FRAME));
126 EXPECT_FALSE(InitializeHandlerCheck(
127 "https://host/scope/doc", "OPTIONS", false, RESOURCE_TYPE_MAIN_FRAME));
129 provider_host_->SetDocumentUrl(GURL(""));
130 EXPECT_FALSE(InitializeHandlerCheck(
131 "http://host/scope/doc", "GET", true, RESOURCE_TYPE_MAIN_FRAME));
132 EXPECT_STREQ("http://host/scope/doc",
133 provider_host_->document_url().spec().c_str());
134 EXPECT_FALSE(InitializeHandlerCheck(
135 "https://host/scope/doc", "GET", true, RESOURCE_TYPE_MAIN_FRAME));
136 EXPECT_STREQ("https://host/scope/doc",
137 provider_host_->document_url().spec().c_str());
139 provider_host_->SetDocumentUrl(GURL(""));
140 EXPECT_FALSE(InitializeHandlerCheck(
141 "http://host/scope/doc", "GET", true, RESOURCE_TYPE_SUB_FRAME));
142 EXPECT_STREQ("http://host/scope/doc",
143 provider_host_->document_url().spec().c_str());
144 EXPECT_FALSE(InitializeHandlerCheck(
145 "https://host/scope/doc", "GET", true, RESOURCE_TYPE_SUB_FRAME));
146 EXPECT_STREQ("https://host/scope/doc",
147 provider_host_->document_url().spec().c_str());
149 provider_host_->SetDocumentUrl(GURL(""));
150 EXPECT_FALSE(InitializeHandlerCheck(
151 "http://host/scope/doc", "GET", true, RESOURCE_TYPE_IMAGE));
152 EXPECT_STREQ("", provider_host_->document_url().spec().c_str());
153 EXPECT_FALSE(InitializeHandlerCheck(
154 "https://host/scope/doc", "GET", true, RESOURCE_TYPE_IMAGE));
155 EXPECT_STREQ("", provider_host_->document_url().spec().c_str());
158 } // namespace content