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
{
42 new EmbeddedWorkerTestHelper(base::FilePath(), kMockRenderProcessId
));
44 // A new unstored registration/version.
45 registration_
= new ServiceWorkerRegistration(
46 GURL("http://host/scope/"), 1L, context()->AsWeakPtr());
47 version_
= new ServiceWorkerVersion(registration_
.get(),
48 GURL("http://host/script.js"),
50 context()->AsWeakPtr());
53 scoped_ptr
<ServiceWorkerProviderHost
> host(new ServiceWorkerProviderHost(
57 SERVICE_WORKER_PROVIDER_FOR_CONTROLLEE
,
58 context()->AsWeakPtr(),
60 host
->SetDocumentUrl(GURL("http://host/scope/"));
61 provider_host_
= host
->AsWeakPtr();
62 context()->AddProviderHost(host
.Pass());
64 context()->storage()->LazyInitialize(base::Bind(&EmptyCallback
));
65 base::RunLoop().RunUntilIdle();
67 version_
->SetStatus(ServiceWorkerVersion::ACTIVATED
);
68 registration_
->SetActiveVersion(version_
.get());
69 context()->storage()->StoreRegistration(
72 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback
));
73 provider_host_
->AssociateRegistration(registration_
.get());
74 base::RunLoop().RunUntilIdle();
77 void TearDown() override
{
79 registration_
= nullptr;
83 ServiceWorkerContextCore
* context() const { return helper_
->context(); }
84 ServiceWorkerContextWrapper
* context_wrapper() const {
85 return helper_
->context_wrapper();
88 bool InitializeHandlerCheck(const std::string
& url
,
89 const std::string
& method
,
90 bool skip_service_worker
,
91 ResourceType resource_type
) {
92 const GURL
kDocUrl(url
);
93 scoped_ptr
<net::URLRequest
> request
= url_request_context_
.CreateRequest(
94 kDocUrl
, net::DEFAULT_PRIORITY
, &url_request_delegate_
);
95 request
->set_method(method
);
96 ServiceWorkerRequestHandler::InitializeHandler(
99 &blob_storage_context_
,
100 kMockRenderProcessId
,
103 FETCH_REQUEST_MODE_NO_CORS
,
104 FETCH_CREDENTIALS_MODE_OMIT
,
106 REQUEST_CONTEXT_TYPE_HYPERLINK
,
107 REQUEST_CONTEXT_FRAME_TYPE_TOP_LEVEL
,
109 return ServiceWorkerRequestHandler::GetHandler(request
.get()) != nullptr;
113 TestBrowserThreadBundle browser_thread_bundle_
;
114 scoped_ptr
<EmbeddedWorkerTestHelper
> helper_
;
115 scoped_refptr
<ServiceWorkerRegistration
> registration_
;
116 scoped_refptr
<ServiceWorkerVersion
> version_
;
117 base::WeakPtr
<ServiceWorkerProviderHost
> provider_host_
;
118 net::URLRequestContext url_request_context_
;
119 MockURLRequestDelegate url_request_delegate_
;
120 storage::BlobStorageContext blob_storage_context_
;
123 TEST_F(ServiceWorkerRequestHandlerTest
, InitializeHandler
) {
124 EXPECT_TRUE(InitializeHandlerCheck(
125 "http://host/scope/doc", "GET", false, RESOURCE_TYPE_MAIN_FRAME
));
126 EXPECT_TRUE(InitializeHandlerCheck(
127 "https://host/scope/doc", "GET", false, RESOURCE_TYPE_MAIN_FRAME
));
128 EXPECT_FALSE(InitializeHandlerCheck(
129 "ftp://host/scope/doc", "GET", false, RESOURCE_TYPE_MAIN_FRAME
));
131 EXPECT_TRUE(InitializeHandlerCheck(
132 "http://host/scope/doc", "OPTIONS", false, RESOURCE_TYPE_MAIN_FRAME
));
133 EXPECT_TRUE(InitializeHandlerCheck(
134 "https://host/scope/doc", "OPTIONS", false, RESOURCE_TYPE_MAIN_FRAME
));
136 provider_host_
->SetDocumentUrl(GURL(""));
137 EXPECT_FALSE(InitializeHandlerCheck(
138 "http://host/scope/doc", "GET", true, RESOURCE_TYPE_MAIN_FRAME
));
139 EXPECT_STREQ("http://host/scope/doc",
140 provider_host_
->document_url().spec().c_str());
141 EXPECT_FALSE(InitializeHandlerCheck(
142 "https://host/scope/doc", "GET", true, RESOURCE_TYPE_MAIN_FRAME
));
143 EXPECT_STREQ("https://host/scope/doc",
144 provider_host_
->document_url().spec().c_str());
146 provider_host_
->SetDocumentUrl(GURL(""));
147 EXPECT_FALSE(InitializeHandlerCheck(
148 "http://host/scope/doc", "GET", true, RESOURCE_TYPE_SUB_FRAME
));
149 EXPECT_STREQ("http://host/scope/doc",
150 provider_host_
->document_url().spec().c_str());
151 EXPECT_FALSE(InitializeHandlerCheck(
152 "https://host/scope/doc", "GET", true, RESOURCE_TYPE_SUB_FRAME
));
153 EXPECT_STREQ("https://host/scope/doc",
154 provider_host_
->document_url().spec().c_str());
156 provider_host_
->SetDocumentUrl(GURL(""));
157 EXPECT_FALSE(InitializeHandlerCheck(
158 "http://host/scope/doc", "GET", true, RESOURCE_TYPE_IMAGE
));
159 EXPECT_STREQ("", provider_host_
->document_url().spec().c_str());
160 EXPECT_FALSE(InitializeHandlerCheck(
161 "https://host/scope/doc", "GET", true, RESOURCE_TYPE_IMAGE
));
162 EXPECT_STREQ("", provider_host_
->document_url().spec().c_str());
165 } // namespace content