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(
54 kMockRenderProcessId
, MSG_ROUTING_NONE
, kMockProviderId
,
55 SERVICE_WORKER_PROVIDER_FOR_WINDOW
, context()->AsWeakPtr(), nullptr));
56 host
->SetDocumentUrl(GURL("http://host/scope/"));
57 provider_host_
= host
->AsWeakPtr();
58 context()->AddProviderHost(host
.Pass());
60 context()->storage()->LazyInitialize(base::Bind(&EmptyCallback
));
61 base::RunLoop().RunUntilIdle();
63 version_
->SetStatus(ServiceWorkerVersion::ACTIVATED
);
64 registration_
->SetActiveVersion(version_
);
65 context()->storage()->StoreRegistration(
68 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback
));
69 provider_host_
->AssociateRegistration(registration_
.get(),
70 false /* notify_controllerchange */);
71 base::RunLoop().RunUntilIdle();
74 void TearDown() override
{
76 registration_
= nullptr;
80 ServiceWorkerContextCore
* context() const { return helper_
->context(); }
81 ServiceWorkerContextWrapper
* context_wrapper() const {
82 return helper_
->context_wrapper();
85 bool InitializeHandlerCheck(const std::string
& url
,
86 const std::string
& method
,
87 bool skip_service_worker
,
88 ResourceType resource_type
) {
89 const GURL
kDocUrl(url
);
90 scoped_ptr
<net::URLRequest
> request
= url_request_context_
.CreateRequest(
91 kDocUrl
, net::DEFAULT_PRIORITY
, &url_request_delegate_
);
92 request
->set_method(method
);
93 ServiceWorkerRequestHandler::InitializeHandler(
96 &blob_storage_context_
,
100 FETCH_REQUEST_MODE_NO_CORS
,
101 FETCH_CREDENTIALS_MODE_OMIT
,
103 REQUEST_CONTEXT_TYPE_HYPERLINK
,
104 REQUEST_CONTEXT_FRAME_TYPE_TOP_LEVEL
,
106 return ServiceWorkerRequestHandler::GetHandler(request
.get()) != nullptr;
110 TestBrowserThreadBundle browser_thread_bundle_
;
111 scoped_ptr
<EmbeddedWorkerTestHelper
> helper_
;
112 scoped_refptr
<ServiceWorkerRegistration
> registration_
;
113 scoped_refptr
<ServiceWorkerVersion
> version_
;
114 base::WeakPtr
<ServiceWorkerProviderHost
> provider_host_
;
115 net::URLRequestContext url_request_context_
;
116 MockURLRequestDelegate url_request_delegate_
;
117 storage::BlobStorageContext blob_storage_context_
;
120 TEST_F(ServiceWorkerRequestHandlerTest
, InitializeHandler
) {
121 EXPECT_TRUE(InitializeHandlerCheck(
122 "http://host/scope/doc", "GET", false, RESOURCE_TYPE_MAIN_FRAME
));
123 EXPECT_TRUE(InitializeHandlerCheck(
124 "https://host/scope/doc", "GET", false, RESOURCE_TYPE_MAIN_FRAME
));
125 EXPECT_FALSE(InitializeHandlerCheck(
126 "ftp://host/scope/doc", "GET", false, RESOURCE_TYPE_MAIN_FRAME
));
128 EXPECT_TRUE(InitializeHandlerCheck(
129 "http://host/scope/doc", "OPTIONS", false, RESOURCE_TYPE_MAIN_FRAME
));
130 EXPECT_TRUE(InitializeHandlerCheck(
131 "https://host/scope/doc", "OPTIONS", false, RESOURCE_TYPE_MAIN_FRAME
));
133 provider_host_
->SetDocumentUrl(GURL(""));
134 EXPECT_FALSE(InitializeHandlerCheck(
135 "http://host/scope/doc", "GET", true, RESOURCE_TYPE_MAIN_FRAME
));
136 EXPECT_STREQ("http://host/scope/doc",
137 provider_host_
->document_url().spec().c_str());
138 EXPECT_FALSE(InitializeHandlerCheck(
139 "https://host/scope/doc", "GET", true, RESOURCE_TYPE_MAIN_FRAME
));
140 EXPECT_STREQ("https://host/scope/doc",
141 provider_host_
->document_url().spec().c_str());
143 provider_host_
->SetDocumentUrl(GURL(""));
144 EXPECT_FALSE(InitializeHandlerCheck(
145 "http://host/scope/doc", "GET", true, RESOURCE_TYPE_SUB_FRAME
));
146 EXPECT_STREQ("http://host/scope/doc",
147 provider_host_
->document_url().spec().c_str());
148 EXPECT_FALSE(InitializeHandlerCheck(
149 "https://host/scope/doc", "GET", true, RESOURCE_TYPE_SUB_FRAME
));
150 EXPECT_STREQ("https://host/scope/doc",
151 provider_host_
->document_url().spec().c_str());
153 provider_host_
->SetDocumentUrl(GURL(""));
154 EXPECT_FALSE(InitializeHandlerCheck(
155 "http://host/scope/doc", "GET", true, RESOURCE_TYPE_IMAGE
));
156 EXPECT_STREQ("", provider_host_
->document_url().spec().c_str());
157 EXPECT_FALSE(InitializeHandlerCheck(
158 "https://host/scope/doc", "GET", true, RESOURCE_TYPE_IMAGE
));
159 EXPECT_STREQ("", provider_host_
->document_url().spec().c_str());
162 } // namespace content