Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1377)

Side by Side Diff: content/browser/service_worker/service_worker_request_handler_unittest.cc

Issue 2562523003: Set the document URL of ServiceWorkerProviderHost when redirected even if skip_service_worker is set (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/service_worker/service_worker_request_handler.h" 5 #include "content/browser/service_worker/service_worker_request_handler.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/run_loop.h" 9 #include "base/run_loop.h"
10 #include "content/browser/fileapi/mock_url_request_delegate.h" 10 #include "content/browser/fileapi/mock_url_request_delegate.h"
11 #include "content/browser/service_worker/embedded_worker_test_helper.h" 11 #include "content/browser/service_worker/embedded_worker_test_helper.h"
12 #include "content/browser/service_worker/service_worker_context_core.h" 12 #include "content/browser/service_worker/service_worker_context_core.h"
13 #include "content/browser/service_worker/service_worker_context_wrapper.h"
13 #include "content/browser/service_worker/service_worker_provider_host.h" 14 #include "content/browser/service_worker/service_worker_provider_host.h"
14 #include "content/browser/service_worker/service_worker_registration.h"
15 #include "content/browser/service_worker/service_worker_test_utils.h" 15 #include "content/browser/service_worker/service_worker_test_utils.h"
16 #include "content/common/resource_request_body_impl.h" 16 #include "content/common/resource_request_body_impl.h"
17 #include "content/common/service_worker/service_worker_utils.h" 17 #include "content/common/service_worker/service_worker_utils.h"
18 #include "content/public/common/request_context_frame_type.h" 18 #include "content/public/common/request_context_frame_type.h"
19 #include "content/public/common/request_context_type.h" 19 #include "content/public/common/request_context_type.h"
20 #include "content/public/common/resource_type.h" 20 #include "content/public/common/resource_type.h"
21 #include "content/public/test/test_browser_thread_bundle.h" 21 #include "content/public/test/test_browser_thread_bundle.h"
22 #include "net/url_request/redirect_info.h"
22 #include "net/url_request/url_request_context.h" 23 #include "net/url_request/url_request_context.h"
24 #include "net/url_request/url_request_job.h"
23 #include "storage/browser/blob/blob_storage_context.h" 25 #include "storage/browser/blob/blob_storage_context.h"
24 #include "testing/gtest/include/gtest/gtest.h" 26 #include "testing/gtest/include/gtest/gtest.h"
25 27
26 namespace content { 28 namespace content {
27 29
28 namespace { 30 namespace {
29 31
30 int kMockProviderId = 1; 32 int kMockProviderId = 1;
31 33
32 void EmptyCallback() {
33 }
34
35 } 34 }
36 35
37 class ServiceWorkerRequestHandlerTest : public testing::Test { 36 class ServiceWorkerRequestHandlerTest : public testing::Test {
38 public: 37 public:
39 ServiceWorkerRequestHandlerTest() 38 ServiceWorkerRequestHandlerTest()
40 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} 39 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {}
41 40
42 void SetUp() override { 41 void SetUp() override {
43 helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath())); 42 helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath()));
44 43
45 // A new unstored registration/version.
46 registration_ = new ServiceWorkerRegistration(GURL("https://host/scope/"),
47 1L, context()->AsWeakPtr());
48 version_ = new ServiceWorkerVersion(registration_.get(),
49 GURL("https://host/script.js"), 1L,
50 context()->AsWeakPtr());
51
52 // An empty host. 44 // An empty host.
53 std::unique_ptr<ServiceWorkerProviderHost> host( 45 std::unique_ptr<ServiceWorkerProviderHost> host(
54 new ServiceWorkerProviderHost( 46 new ServiceWorkerProviderHost(
55 helper_->mock_render_process_id(), MSG_ROUTING_NONE, 47 helper_->mock_render_process_id(), MSG_ROUTING_NONE,
56 kMockProviderId, SERVICE_WORKER_PROVIDER_FOR_WINDOW, 48 kMockProviderId, SERVICE_WORKER_PROVIDER_FOR_WINDOW,
57 ServiceWorkerProviderHost::FrameSecurityLevel::SECURE, 49 ServiceWorkerProviderHost::FrameSecurityLevel::SECURE,
58 context()->AsWeakPtr(), nullptr)); 50 context()->AsWeakPtr(), nullptr));
59 host->SetDocumentUrl(GURL("https://host/scope/"));
60 provider_host_ = host->AsWeakPtr(); 51 provider_host_ = host->AsWeakPtr();
61 context()->AddProviderHost(std::move(host)); 52 context()->AddProviderHost(std::move(host));
62 53
63 context()->storage()->LazyInitialize(base::Bind(&EmptyCallback));
64 base::RunLoop().RunUntilIdle();
65
66 version_->set_fetch_handler_existence(
67 ServiceWorkerVersion::FetchHandlerExistence::EXISTS);
68 version_->SetStatus(ServiceWorkerVersion::ACTIVATED);
69 registration_->SetActiveVersion(version_);
70 context()->storage()->StoreRegistration(
71 registration_.get(),
72 version_.get(),
73 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback));
74 provider_host_->AssociateRegistration(registration_.get(),
75 false /* notify_controllerchange */);
76 base::RunLoop().RunUntilIdle();
77 } 54 }
78 55
79 void TearDown() override { 56 void TearDown() override {
80 version_ = nullptr;
81 registration_ = nullptr;
82 helper_.reset(); 57 helper_.reset();
83 } 58 }
84 59
85 ServiceWorkerContextCore* context() const { return helper_->context(); } 60 ServiceWorkerContextCore* context() const { return helper_->context(); }
86 ServiceWorkerContextWrapper* context_wrapper() const { 61 ServiceWorkerContextWrapper* context_wrapper() const {
87 return helper_->context_wrapper(); 62 return helper_->context_wrapper();
88 } 63 }
89 64
90 bool InitializeHandlerCheck(const std::string& url, 65 std::unique_ptr<net::URLRequest> CreateRequest(const std::string& url,
91 const std::string& method, 66 const std::string& method) {
92 bool skip_service_worker,
93 ResourceType resource_type) {
94 const GURL kDocUrl(url);
95 std::unique_ptr<net::URLRequest> request = 67 std::unique_ptr<net::URLRequest> request =
96 url_request_context_.CreateRequest(kDocUrl, net::DEFAULT_PRIORITY, 68 url_request_context_.CreateRequest(GURL(url), net::DEFAULT_PRIORITY,
97 &url_request_delegate_); 69 &url_request_delegate_);
98 request->set_method(method); 70 request->set_method(method);
71 return request;
72 }
73
74 void InitializeHandler(net::URLRequest* request,
75 bool skip_service_worker,
76 ResourceType resource_type) {
99 ServiceWorkerRequestHandler::InitializeHandler( 77 ServiceWorkerRequestHandler::InitializeHandler(
100 request.get(), context_wrapper(), &blob_storage_context_, 78 request, context_wrapper(), &blob_storage_context_,
101 helper_->mock_render_process_id(), kMockProviderId, skip_service_worker, 79 helper_->mock_render_process_id(), kMockProviderId, skip_service_worker,
102 FETCH_REQUEST_MODE_NO_CORS, FETCH_CREDENTIALS_MODE_OMIT, 80 FETCH_REQUEST_MODE_NO_CORS, FETCH_CREDENTIALS_MODE_OMIT,
103 FetchRedirectMode::FOLLOW_MODE, resource_type, 81 FetchRedirectMode::FOLLOW_MODE, resource_type,
104 REQUEST_CONTEXT_TYPE_HYPERLINK, REQUEST_CONTEXT_FRAME_TYPE_TOP_LEVEL, 82 REQUEST_CONTEXT_TYPE_HYPERLINK, REQUEST_CONTEXT_FRAME_TYPE_TOP_LEVEL,
105 nullptr); 83 nullptr);
106 return ServiceWorkerRequestHandler::GetHandler(request.get()) != nullptr; 84 }
85
86 static ServiceWorkerRequestHandler* GetHandler(net::URLRequest* request) {
87 return ServiceWorkerRequestHandler::GetHandler(request);
88 }
89
90 std::unique_ptr<net::URLRequestJob> MaybeCreateJob(net::URLRequest* request) {
91 return std::unique_ptr<net::URLRequestJob>(
92 ServiceWorkerRequestHandler::GetHandler(request)->MaybeCreateJob(
shimazu 2016/12/09 05:00:46 How about calling GetHandler which is defined abov
horo 2016/12/09 05:21:21 Done.
93 request, url_request_context_.network_delegate(),
94 context_wrapper()->resource_context()));
95 }
96
97 void InitializeHandlerSimpleTest(const std::string& url,
98 const std::string& method,
99 bool skip_service_worker,
100 ResourceType resource_type) {
101 std::unique_ptr<net::URLRequest> request = CreateRequest(url, method);
102 InitializeHandler(request.get(), skip_service_worker, resource_type);
103 ASSERT_TRUE(GetHandler(request.get()));
104 MaybeCreateJob(request.get());
105 EXPECT_EQ(url, provider_host_->document_url().spec());
107 } 106 }
108 107
109 protected: 108 protected:
110 TestBrowserThreadBundle browser_thread_bundle_; 109 TestBrowserThreadBundle browser_thread_bundle_;
111 std::unique_ptr<EmbeddedWorkerTestHelper> helper_; 110 std::unique_ptr<EmbeddedWorkerTestHelper> helper_;
112 scoped_refptr<ServiceWorkerRegistration> registration_;
113 scoped_refptr<ServiceWorkerVersion> version_;
114 base::WeakPtr<ServiceWorkerProviderHost> provider_host_; 111 base::WeakPtr<ServiceWorkerProviderHost> provider_host_;
115 net::URLRequestContext url_request_context_; 112 net::URLRequestContext url_request_context_;
116 MockURLRequestDelegate url_request_delegate_; 113 MockURLRequestDelegate url_request_delegate_;
117 storage::BlobStorageContext blob_storage_context_; 114 storage::BlobStorageContext blob_storage_context_;
118 }; 115 };
119 116
120 class ServiceWorkerRequestHandlerTestP 117 class ServiceWorkerRequestHandlerTestP
121 : public MojoServiceWorkerTestP<ServiceWorkerRequestHandlerTest> {}; 118 : public MojoServiceWorkerTestP<ServiceWorkerRequestHandlerTest> {};
122 119
123 TEST_P(ServiceWorkerRequestHandlerTestP, InitializeHandler) { 120 TEST_P(ServiceWorkerRequestHandlerTestP, InitializeHandler_FTP) {
121 std::unique_ptr<net::URLRequest> request =
122 CreateRequest("fpp://host/scope/doc", "GET");
shimazu 2016/12/09 05:00:46 s%fpp://%ftp://%
horo 2016/12/09 05:21:21 Done.
123 InitializeHandler(request.get(), false, RESOURCE_TYPE_MAIN_FRAME);
124 // Cannot initialize a handler for non-secure origins. 124 // Cannot initialize a handler for non-secure origins.
125 EXPECT_FALSE(InitializeHandlerCheck( 125 EXPECT_FALSE(GetHandler(request.get()));
126 "ftp://host/scope/doc", "GET", false, RESOURCE_TYPE_MAIN_FRAME)); 126 }
127 // HTTP is ok because it might redirect to HTTPS.
128 EXPECT_TRUE(InitializeHandlerCheck("http://host/scope/doc", "GET", false,
129 RESOURCE_TYPE_MAIN_FRAME));
130 EXPECT_TRUE(InitializeHandlerCheck("https://host/scope/doc", "GET", false,
131 RESOURCE_TYPE_MAIN_FRAME));
132 127
128 TEST_P(ServiceWorkerRequestHandlerTestP, InitializeHandler_HTTP_MAIN_FRAME) {
129 InitializeHandlerSimpleTest("http://host/scope/doc", "GET", false,
shimazu 2016/12/09 05:00:46 How about adding a comment like "HTTP should have
horo 2016/12/09 05:21:21 Done.
130 RESOURCE_TYPE_MAIN_FRAME);
131 }
132
133 TEST_P(ServiceWorkerRequestHandlerTestP, InitializeHandler_HTTPS_MAIN_FRAME) {
134 InitializeHandlerSimpleTest("https://host/scope/doc", "GET", false,
135 RESOURCE_TYPE_MAIN_FRAME);
136 }
137
138 TEST_P(ServiceWorkerRequestHandlerTestP, InitializeHandler_HTTP_SUB_FRAME) {
139 InitializeHandlerSimpleTest("http://host/scope/doc", "GET", false,
140 RESOURCE_TYPE_SUB_FRAME);
141 }
142
143 TEST_P(ServiceWorkerRequestHandlerTestP, InitializeHandler_HTTPS_SUB_FRAME) {
144 InitializeHandlerSimpleTest("https://host/scope/doc", "GET", false,
145 RESOURCE_TYPE_SUB_FRAME);
146 }
147
148 TEST_P(ServiceWorkerRequestHandlerTestP, InitializeHandler_HTTPS_OPTIONS) {
133 // OPTIONS is also supported. See crbug.com/434660. 149 // OPTIONS is also supported. See crbug.com/434660.
134 EXPECT_TRUE(InitializeHandlerCheck( 150 InitializeHandlerSimpleTest("https://host/scope/doc", "OPTIONS", false,
135 "https://host/scope/doc", "OPTIONS", false, RESOURCE_TYPE_MAIN_FRAME)); 151 RESOURCE_TYPE_MAIN_FRAME);
152 }
136 153
137 // Check provider host's URL after initializing a handler for main 154 TEST_P(ServiceWorkerRequestHandlerTestP, InitializeHandler_HTTPS_SKIP) {
138 // frame. 155 InitializeHandlerSimpleTest("https://host/scope/doc", "GET", true,
139 provider_host_->SetDocumentUrl(GURL("")); 156 RESOURCE_TYPE_MAIN_FRAME);
140 EXPECT_FALSE(InitializeHandlerCheck( 157 }
141 "http://host/scope/doc", "GET", true, RESOURCE_TYPE_MAIN_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_MAIN_FRAME));
146 EXPECT_STREQ("https://host/scope/doc",
147 provider_host_->document_url().spec().c_str());
148 158
149 // Check provider host's URL after initializing a handler for a subframe. 159 TEST_P(ServiceWorkerRequestHandlerTestP, InitializeHandler_IMAGE) {
150 provider_host_->SetDocumentUrl(GURL(""));
151 EXPECT_FALSE(InitializeHandlerCheck(
152 "http://host/scope/doc", "GET", true, RESOURCE_TYPE_SUB_FRAME));
153 EXPECT_STREQ("http://host/scope/doc",
154 provider_host_->document_url().spec().c_str());
155 EXPECT_FALSE(InitializeHandlerCheck(
156 "https://host/scope/doc", "GET", true, RESOURCE_TYPE_SUB_FRAME));
157 EXPECT_STREQ("https://host/scope/doc",
158 provider_host_->document_url().spec().c_str());
159
160 // Check provider host's URL after initializing a handler for an image. 160 // Check provider host's URL after initializing a handler for an image.
161 provider_host_->SetDocumentUrl(GURL("")); 161 provider_host_->SetDocumentUrl(GURL("https://host/scope/doc"));
162 EXPECT_FALSE(InitializeHandlerCheck( 162 std::unique_ptr<net::URLRequest> request =
163 "http://host/scope/doc", "GET", true, RESOURCE_TYPE_IMAGE)); 163 CreateRequest("https://host/scope/image", "GET");
164 EXPECT_STREQ("", provider_host_->document_url().spec().c_str()); 164 InitializeHandler(request.get(), true, RESOURCE_TYPE_IMAGE);
165 EXPECT_FALSE(InitializeHandlerCheck( 165 ASSERT_FALSE(GetHandler(request.get()));
166 "https://host/scope/doc", "GET", true, RESOURCE_TYPE_IMAGE)); 166 EXPECT_EQ(GURL("https://host/scope/doc"), provider_host_->document_url());
167 EXPECT_STREQ("", provider_host_->document_url().spec().c_str());
168 } 167 }
169 168
170 INSTANTIATE_TEST_CASE_P(ServiceWorkerRequestHandlerTest, 169 INSTANTIATE_TEST_CASE_P(ServiceWorkerRequestHandlerTest,
171 ServiceWorkerRequestHandlerTestP, 170 ServiceWorkerRequestHandlerTestP,
172 testing::Bool()); 171 testing::Bool());
173 172
174 } // namespace content 173 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698