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(
56 SERVICE_WORKER_PROVIDER_FOR_CONTROLLEE
,
57 context()->AsWeakPtr(),
59 provider_host_
= host
->AsWeakPtr();
60 context()->AddProviderHost(host
.Pass());
62 context()->storage()->LazyInitialize(base::Bind(&EmptyCallback
));
63 base::RunLoop().RunUntilIdle();
65 version_
->SetStatus(ServiceWorkerVersion::ACTIVATED
);
66 registration_
->SetActiveVersion(version_
.get());
67 context()->storage()->StoreRegistration(
70 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback
));
71 provider_host_
->AssociateRegistration(registration_
.get());
72 base::RunLoop().RunUntilIdle();
75 void TearDown() override
{
77 registration_
= nullptr;
81 ServiceWorkerContextCore
* context() const { return helper_
->context(); }
82 ServiceWorkerContextWrapper
* context_wrapper() const {
83 return helper_
->context_wrapper();
86 bool InitializeHandlerCheck(const std::string
& url
,
87 const std::string
& method
,
88 bool skip_service_worker
,
89 ResourceType resource_type
) {
90 const GURL
kDocUrl(url
);
91 scoped_ptr
<net::URLRequest
> request
= url_request_context_
.CreateRequest(
92 kDocUrl
, net::DEFAULT_PRIORITY
, &url_request_delegate_
, nullptr);
93 request
->set_method(method
);
94 ServiceWorkerRequestHandler::InitializeHandler(
97 &blob_storage_context_
,
101 FETCH_REQUEST_MODE_NO_CORS
,
102 FETCH_CREDENTIALS_MODE_OMIT
,
104 REQUEST_CONTEXT_TYPE_HYPERLINK
,
105 REQUEST_CONTEXT_FRAME_TYPE_TOP_LEVEL
,
107 return ServiceWorkerRequestHandler::GetHandler(request
.get()) != nullptr;
111 TestBrowserThreadBundle browser_thread_bundle_
;
112 scoped_ptr
<EmbeddedWorkerTestHelper
> helper_
;
113 scoped_refptr
<ServiceWorkerRegistration
> registration_
;
114 scoped_refptr
<ServiceWorkerVersion
> version_
;
115 base::WeakPtr
<ServiceWorkerProviderHost
> provider_host_
;
116 net::URLRequestContext url_request_context_
;
117 MockURLRequestDelegate url_request_delegate_
;
118 storage::BlobStorageContext blob_storage_context_
;
121 TEST_F(ServiceWorkerRequestHandlerTest
, InitializeHandler
) {
122 EXPECT_TRUE(InitializeHandlerCheck(
123 "http://host/scope/doc", "GET", false, RESOURCE_TYPE_MAIN_FRAME
));
124 EXPECT_TRUE(InitializeHandlerCheck(
125 "https://host/scope/doc", "GET", false, RESOURCE_TYPE_MAIN_FRAME
));
126 EXPECT_FALSE(InitializeHandlerCheck(
127 "ftp://host/scope/doc", "GET", false, RESOURCE_TYPE_MAIN_FRAME
));
129 EXPECT_TRUE(InitializeHandlerCheck(
130 "http://host/scope/doc", "OPTIONS", false, RESOURCE_TYPE_MAIN_FRAME
));
131 EXPECT_TRUE(InitializeHandlerCheck(
132 "https://host/scope/doc", "OPTIONS", false, RESOURCE_TYPE_MAIN_FRAME
));
134 provider_host_
->SetDocumentUrl(GURL(""));
135 EXPECT_FALSE(InitializeHandlerCheck(
136 "http://host/scope/doc", "GET", true, RESOURCE_TYPE_MAIN_FRAME
));
137 EXPECT_STREQ("http://host/scope/doc",
138 provider_host_
->document_url().spec().c_str());
139 EXPECT_FALSE(InitializeHandlerCheck(
140 "https://host/scope/doc", "GET", true, RESOURCE_TYPE_MAIN_FRAME
));
141 EXPECT_STREQ("https://host/scope/doc",
142 provider_host_
->document_url().spec().c_str());
144 provider_host_
->SetDocumentUrl(GURL(""));
145 EXPECT_FALSE(InitializeHandlerCheck(
146 "http://host/scope/doc", "GET", true, RESOURCE_TYPE_SUB_FRAME
));
147 EXPECT_STREQ("http://host/scope/doc",
148 provider_host_
->document_url().spec().c_str());
149 EXPECT_FALSE(InitializeHandlerCheck(
150 "https://host/scope/doc", "GET", true, RESOURCE_TYPE_SUB_FRAME
));
151 EXPECT_STREQ("https://host/scope/doc",
152 provider_host_
->document_url().spec().c_str());
154 provider_host_
->SetDocumentUrl(GURL(""));
155 EXPECT_FALSE(InitializeHandlerCheck(
156 "http://host/scope/doc", "GET", true, RESOURCE_TYPE_IMAGE
));
157 EXPECT_STREQ("", provider_host_
->document_url().spec().c_str());
158 EXPECT_FALSE(InitializeHandlerCheck(
159 "https://host/scope/doc", "GET", true, RESOURCE_TYPE_IMAGE
));
160 EXPECT_STREQ("", provider_host_
->document_url().spec().c_str());
163 } // namespace content