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

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

Issue 2019613003: ServiceWorker: Bypass SW when the script doesn't have fetch handler (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add a test for subresources and fix the nits Created 4 years, 6 months 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_controllee_request_handl er.h" 5 #include "content/browser/service_worker/service_worker_controllee_request_handl er.h"
6 6
7 #include <utility> 7 #include <utility>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/callback_helpers.h" 10 #include "base/callback_helpers.h"
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
48 int64_t service_worker_version_id, 48 int64_t service_worker_version_id,
49 const GURL& scope, 49 const GURL& scope,
50 const GURL& script_url, 50 const GURL& script_url,
51 bool pause_after_download) override { 51 bool pause_after_download) override {
52 SimulateWorkerStopped(embedded_worker_id); 52 SimulateWorkerStopped(embedded_worker_id);
53 } 53 }
54 }; 54 };
55 55
56 class ServiceWorkerControlleeRequestHandlerTest : public testing::Test { 56 class ServiceWorkerControlleeRequestHandlerTest : public testing::Test {
57 public: 57 public:
58 struct ServiceWorkerRequestTestResources {
falken 2016/05/31 01:19:51 The style guide is to use structs only for "passiv
shimazu 2016/05/31 02:18:10 Done.
59 std::unique_ptr<net::URLRequest> request;
60 std::unique_ptr<ServiceWorkerControlleeRequestHandler> handler;
61 std::unique_ptr<net::URLRequestJob> job;
62
63 ServiceWorkerURLRequestJob* GetJob() {
64 return static_cast<ServiceWorkerURLRequestJob*>(job.get());
65 }
66
67 static std::unique_ptr<ServiceWorkerRequestTestResources> Create(
68 ServiceWorkerControlleeRequestHandlerTest* test,
69 const GURL& url,
70 ResourceType type) {
71 return std::unique_ptr<ServiceWorkerRequestTestResources>(
72 new ServiceWorkerRequestTestResources(test, url, type));
73 }
falken 2016/05/31 01:19:51 Alternatively, you don't need Create and use the c
shimazu 2016/05/31 02:18:10 Done.
74
75 private:
76 ServiceWorkerRequestTestResources(
77 ServiceWorkerControlleeRequestHandlerTest* test,
78 const GURL& url,
79 ResourceType type)
80 : request(test->url_request_context_.CreateRequest(
81 url,
82 net::DEFAULT_PRIORITY,
83 &test->url_request_delegate_)),
84 handler(new ServiceWorkerControlleeRequestHandler(
85 test->context()->AsWeakPtr(),
86 test->provider_host_,
87 base::WeakPtr<storage::BlobStorageContext>(),
88 FETCH_REQUEST_MODE_NO_CORS,
89 FETCH_CREDENTIALS_MODE_OMIT,
90 FetchRedirectMode::FOLLOW_MODE,
91 type,
92 REQUEST_CONTEXT_TYPE_HYPERLINK,
93 REQUEST_CONTEXT_FRAME_TYPE_TOP_LEVEL,
94 scoped_refptr<ResourceRequestBody>())),
95 job(handler->MaybeCreateJob(request.get(),
falken 2016/05/31 01:19:51 The coding style says to not do heavy work in cons
shimazu 2016/05/31 02:18:10 Done.
96 nullptr,
97 &test->mock_resource_context_)) {}
98 };
99
58 ServiceWorkerControlleeRequestHandlerTest() 100 ServiceWorkerControlleeRequestHandlerTest()
59 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} 101 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {}
60 102
61 void SetUp() override { 103 void SetUp() override {
62 SetUpWithHelper(new EmbeddedWorkerTestHelper(base::FilePath())); 104 SetUpWithHelper(new EmbeddedWorkerTestHelper(base::FilePath()));
63 } 105 }
64 106
65 void SetUpWithHelper(EmbeddedWorkerTestHelper* helper) { 107 void SetUpWithHelper(EmbeddedWorkerTestHelper* helper) {
66 helper_.reset(helper); 108 helper_.reset(helper);
67 109
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
124 } 166 }
125 }; 167 };
126 168
127 TEST_F(ServiceWorkerControlleeRequestHandlerTest, DisallowServiceWorker) { 169 TEST_F(ServiceWorkerControlleeRequestHandlerTest, DisallowServiceWorker) {
128 ServiceWorkerTestContentBrowserClient test_browser_client; 170 ServiceWorkerTestContentBrowserClient test_browser_client;
129 ContentBrowserClient* old_browser_client = 171 ContentBrowserClient* old_browser_client =
130 SetBrowserClientForTesting(&test_browser_client); 172 SetBrowserClientForTesting(&test_browser_client);
131 173
132 // Store an activated worker. 174 // Store an activated worker.
133 version_->SetStatus(ServiceWorkerVersion::ACTIVATED); 175 version_->SetStatus(ServiceWorkerVersion::ACTIVATED);
176 version_->set_has_fetch_handler(true);
134 registration_->SetActiveVersion(version_); 177 registration_->SetActiveVersion(version_);
135 context()->storage()->StoreRegistration( 178 context()->storage()->StoreRegistration(
136 registration_.get(), 179 registration_.get(),
137 version_.get(), 180 version_.get(),
138 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); 181 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback));
139 base::RunLoop().RunUntilIdle(); 182 base::RunLoop().RunUntilIdle();
140 183
141 // Conduct a main resource load. 184 // Conduct a main resource load.
142 const GURL kDocUrl("http://host/scope/doc"); 185 auto test_resources = ServiceWorkerRequestTestResources::Create(
143 std::unique_ptr<net::URLRequest> request = url_request_context_.CreateRequest( 186 this, GURL("http://host/scope/doc"), RESOURCE_TYPE_MAIN_FRAME);
144 kDocUrl, net::DEFAULT_PRIORITY, &url_request_delegate_); 187 ServiceWorkerURLRequestJob* sw_job = test_resources->GetJob();
145 std::unique_ptr<ServiceWorkerControlleeRequestHandler> handler(
146 new ServiceWorkerControlleeRequestHandler(
147 context()->AsWeakPtr(), provider_host_,
148 base::WeakPtr<storage::BlobStorageContext>(),
149 FETCH_REQUEST_MODE_NO_CORS, FETCH_CREDENTIALS_MODE_OMIT,
150 FetchRedirectMode::FOLLOW_MODE, RESOURCE_TYPE_MAIN_FRAME,
151 REQUEST_CONTEXT_TYPE_HYPERLINK, REQUEST_CONTEXT_FRAME_TYPE_TOP_LEVEL,
152 scoped_refptr<ResourceRequestBody>()));
153 std::unique_ptr<net::URLRequestJob> job(
154 handler->MaybeCreateJob(request.get(), nullptr, &mock_resource_context_));
155 ServiceWorkerURLRequestJob* sw_job =
156 static_cast<ServiceWorkerURLRequestJob*>(job.get());
157 188
158 EXPECT_FALSE(sw_job->ShouldFallbackToNetwork()); 189 EXPECT_FALSE(sw_job->ShouldFallbackToNetwork());
159 EXPECT_FALSE(sw_job->ShouldForwardToServiceWorker()); 190 EXPECT_FALSE(sw_job->ShouldForwardToServiceWorker());
160 EXPECT_FALSE(version_->HasControllee()); 191 EXPECT_FALSE(version_->HasControllee());
161 base::RunLoop().RunUntilIdle(); 192 base::RunLoop().RunUntilIdle();
162 193
163 // Verify we did not use the worker. 194 // Verify we did not use the worker.
164 EXPECT_TRUE(sw_job->ShouldFallbackToNetwork()); 195 EXPECT_TRUE(sw_job->ShouldFallbackToNetwork());
165 EXPECT_FALSE(sw_job->ShouldForwardToServiceWorker()); 196 EXPECT_FALSE(sw_job->ShouldForwardToServiceWorker());
166 EXPECT_FALSE(version_->HasControllee()); 197 EXPECT_FALSE(version_->HasControllee());
167 198
168 SetBrowserClientForTesting(old_browser_client); 199 SetBrowserClientForTesting(old_browser_client);
169 } 200 }
170 201
171 TEST_F(ServiceWorkerControlleeRequestHandlerTest, ActivateWaitingVersion) { 202 TEST_F(ServiceWorkerControlleeRequestHandlerTest, ActivateWaitingVersion) {
172 // Store a registration that is installed but not activated yet. 203 // Store a registration that is installed but not activated yet.
173 version_->SetStatus(ServiceWorkerVersion::INSTALLED); 204 version_->SetStatus(ServiceWorkerVersion::INSTALLED);
205 version_->set_has_fetch_handler(true);
174 registration_->SetWaitingVersion(version_); 206 registration_->SetWaitingVersion(version_);
175 context()->storage()->StoreRegistration( 207 context()->storage()->StoreRegistration(
176 registration_.get(), 208 registration_.get(),
177 version_.get(), 209 version_.get(),
178 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); 210 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback));
179 base::RunLoop().RunUntilIdle(); 211 base::RunLoop().RunUntilIdle();
180 212
181 // Conduct a main resource load. 213 // Conduct a main resource load.
182 const GURL kDocUrl("http://host/scope/doc"); 214 auto test_resources = ServiceWorkerRequestTestResources::Create(
183 std::unique_ptr<net::URLRequest> request = url_request_context_.CreateRequest( 215 this, GURL("http://host/scope/doc"), RESOURCE_TYPE_MAIN_FRAME);
184 kDocUrl, net::DEFAULT_PRIORITY, &url_request_delegate_); 216 ServiceWorkerURLRequestJob* sw_job = test_resources->GetJob();
185 std::unique_ptr<ServiceWorkerControlleeRequestHandler> handler(
186 new ServiceWorkerControlleeRequestHandler(
187 context()->AsWeakPtr(), provider_host_,
188 base::WeakPtr<storage::BlobStorageContext>(),
189 FETCH_REQUEST_MODE_NO_CORS, FETCH_CREDENTIALS_MODE_OMIT,
190 FetchRedirectMode::FOLLOW_MODE, RESOURCE_TYPE_MAIN_FRAME,
191 REQUEST_CONTEXT_TYPE_HYPERLINK, REQUEST_CONTEXT_FRAME_TYPE_TOP_LEVEL,
192 scoped_refptr<ResourceRequestBody>()));
193 std::unique_ptr<net::URLRequestJob> job(
194 handler->MaybeCreateJob(request.get(), nullptr, &mock_resource_context_));
195 ServiceWorkerURLRequestJob* sw_job =
196 static_cast<ServiceWorkerURLRequestJob*>(job.get());
197 217
198 EXPECT_FALSE(sw_job->ShouldFallbackToNetwork()); 218 EXPECT_FALSE(sw_job->ShouldFallbackToNetwork());
199 EXPECT_FALSE(sw_job->ShouldForwardToServiceWorker()); 219 EXPECT_FALSE(sw_job->ShouldForwardToServiceWorker());
200 EXPECT_FALSE(version_->HasControllee()); 220 EXPECT_FALSE(version_->HasControllee());
201 221
202 base::RunLoop().RunUntilIdle(); 222 base::RunLoop().RunUntilIdle();
203 223
204 EXPECT_EQ(ServiceWorkerVersion::ACTIVATED, 224 EXPECT_EQ(ServiceWorkerVersion::ACTIVATED,
205 version_->status()); 225 version_->status());
206 EXPECT_FALSE(sw_job->ShouldFallbackToNetwork()); 226 EXPECT_FALSE(sw_job->ShouldFallbackToNetwork());
207 EXPECT_TRUE(sw_job->ShouldForwardToServiceWorker()); 227 EXPECT_TRUE(sw_job->ShouldForwardToServiceWorker());
208 EXPECT_TRUE(version_->HasControllee()); 228 EXPECT_TRUE(version_->HasControllee());
209 229
210 // Navigations should trigger an update too. 230 // Navigations should trigger an update too.
211 handler.reset(NULL); 231 test_resources->handler.reset(nullptr);
212 EXPECT_TRUE(version_->update_timer_.IsRunning()); 232 EXPECT_TRUE(version_->update_timer_.IsRunning());
213 } 233 }
214 234
215 // Test that an installing registration is associated with a provider host. 235 // Test that an installing registration is associated with a provider host.
216 TEST_F(ServiceWorkerControlleeRequestHandlerTest, InstallingRegistration) { 236 TEST_F(ServiceWorkerControlleeRequestHandlerTest, InstallingRegistration) {
217 // Create an installing registration. 237 // Create an installing registration.
218 version_->SetStatus(ServiceWorkerVersion::INSTALLING); 238 version_->SetStatus(ServiceWorkerVersion::INSTALLING);
239 version_->set_has_fetch_handler(true);
219 registration_->SetInstallingVersion(version_); 240 registration_->SetInstallingVersion(version_);
220 context()->storage()->NotifyInstallingRegistration(registration_.get()); 241 context()->storage()->NotifyInstallingRegistration(registration_.get());
221 242
222 // Conduct a main resource load. 243 // Conduct a main resource load.
223 const GURL kDocUrl("http://host/scope/doc"); 244 auto test_resources = ServiceWorkerRequestTestResources::Create(
224 std::unique_ptr<net::URLRequest> request = url_request_context_.CreateRequest( 245 this, GURL("http://host/scope/doc"), RESOURCE_TYPE_MAIN_FRAME);
225 kDocUrl, net::DEFAULT_PRIORITY, &url_request_delegate_); 246 ServiceWorkerURLRequestJob* job = test_resources->GetJob();
226 std::unique_ptr<ServiceWorkerControlleeRequestHandler> handler( 247
227 new ServiceWorkerControlleeRequestHandler(
228 context()->AsWeakPtr(), provider_host_,
229 base::WeakPtr<storage::BlobStorageContext>(),
230 FETCH_REQUEST_MODE_NO_CORS, FETCH_CREDENTIALS_MODE_OMIT,
231 FetchRedirectMode::FOLLOW_MODE, RESOURCE_TYPE_MAIN_FRAME,
232 REQUEST_CONTEXT_TYPE_HYPERLINK, REQUEST_CONTEXT_FRAME_TYPE_TOP_LEVEL,
233 scoped_refptr<ResourceRequestBody>()));
234 std::unique_ptr<net::URLRequestJob> job(
235 handler->MaybeCreateJob(request.get(), nullptr, &mock_resource_context_));
236 base::RunLoop().RunUntilIdle(); 248 base::RunLoop().RunUntilIdle();
237 249
238 // The handler should have fallen back to network and destroyed the job. The 250 // The handler should have fallen back to network and destroyed the job. The
239 // registration should be associated with the provider host, although it is 251 // registration should be associated with the provider host, although it is
240 // not controlled since there is no active version. 252 // not controlled since there is no active version.
241 EXPECT_FALSE(job); 253 EXPECT_FALSE(job);
242 EXPECT_EQ(registration_.get(), provider_host_->associated_registration()); 254 EXPECT_EQ(registration_.get(), provider_host_->associated_registration());
243 EXPECT_EQ(version_.get(), provider_host_->installing_version()); 255 EXPECT_EQ(version_.get(), provider_host_->installing_version());
244 EXPECT_FALSE(version_->HasControllee()); 256 EXPECT_FALSE(version_->HasControllee());
245 EXPECT_FALSE(provider_host_->controlling_version()); 257 EXPECT_FALSE(provider_host_->controlling_version());
246 } 258 }
247 259
248 // Test to not regress crbug/414118. 260 // Test to not regress crbug/414118.
249 TEST_F(ServiceWorkerControlleeRequestHandlerTest, DeletedProviderHost) { 261 TEST_F(ServiceWorkerControlleeRequestHandlerTest, DeletedProviderHost) {
250 // Store a registration so the call to FindRegistrationForDocument will read 262 // Store a registration so the call to FindRegistrationForDocument will read
251 // from the database. 263 // from the database.
252 version_->SetStatus(ServiceWorkerVersion::ACTIVATED); 264 version_->SetStatus(ServiceWorkerVersion::ACTIVATED);
265 version_->set_has_fetch_handler(true);
253 registration_->SetActiveVersion(version_); 266 registration_->SetActiveVersion(version_);
254 context()->storage()->StoreRegistration( 267 context()->storage()->StoreRegistration(
255 registration_.get(), 268 registration_.get(),
256 version_.get(), 269 version_.get(),
257 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); 270 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback));
258 base::RunLoop().RunUntilIdle(); 271 base::RunLoop().RunUntilIdle();
259 version_ = NULL; 272 version_ = NULL;
260 registration_ = NULL; 273 registration_ = NULL;
261 274
262 // Conduct a main resource load. 275 // Conduct a main resource load.
263 const GURL kDocUrl("http://host/scope/doc"); 276 auto test_resources = ServiceWorkerRequestTestResources::Create(
264 std::unique_ptr<net::URLRequest> request = url_request_context_.CreateRequest( 277 this, GURL("http://host/scope/doc"), RESOURCE_TYPE_MAIN_FRAME);
265 kDocUrl, net::DEFAULT_PRIORITY, &url_request_delegate_); 278 ServiceWorkerURLRequestJob* sw_job = test_resources->GetJob();
266 std::unique_ptr<ServiceWorkerControlleeRequestHandler> handler(
267 new ServiceWorkerControlleeRequestHandler(
268 context()->AsWeakPtr(), provider_host_,
269 base::WeakPtr<storage::BlobStorageContext>(),
270 FETCH_REQUEST_MODE_NO_CORS, FETCH_CREDENTIALS_MODE_OMIT,
271 FetchRedirectMode::FOLLOW_MODE, RESOURCE_TYPE_MAIN_FRAME,
272 REQUEST_CONTEXT_TYPE_HYPERLINK, REQUEST_CONTEXT_FRAME_TYPE_TOP_LEVEL,
273 scoped_refptr<ResourceRequestBody>()));
274 std::unique_ptr<net::URLRequestJob> job(
275 handler->MaybeCreateJob(request.get(), nullptr, &mock_resource_context_));
276 ServiceWorkerURLRequestJob* sw_job =
277 static_cast<ServiceWorkerURLRequestJob*>(job.get());
278 279
279 EXPECT_FALSE(sw_job->ShouldFallbackToNetwork()); 280 EXPECT_FALSE(sw_job->ShouldFallbackToNetwork());
280 EXPECT_FALSE(sw_job->ShouldForwardToServiceWorker()); 281 EXPECT_FALSE(sw_job->ShouldForwardToServiceWorker());
281 282
282 // Shouldn't crash if the ProviderHost is deleted prior to completion of 283 // Shouldn't crash if the ProviderHost is deleted prior to completion of
283 // the database lookup. 284 // the database lookup.
284 context()->RemoveProviderHost(helper_->mock_render_process_id(), 285 context()->RemoveProviderHost(helper_->mock_render_process_id(),
285 kMockProviderId); 286 kMockProviderId);
286 EXPECT_FALSE(provider_host_.get()); 287 EXPECT_FALSE(provider_host_.get());
287 base::RunLoop().RunUntilIdle(); 288 base::RunLoop().RunUntilIdle();
288 EXPECT_TRUE(sw_job->ShouldFallbackToNetwork()); 289 EXPECT_TRUE(sw_job->ShouldFallbackToNetwork());
289 EXPECT_FALSE(sw_job->ShouldForwardToServiceWorker()); 290 EXPECT_FALSE(sw_job->ShouldForwardToServiceWorker());
290 } 291 }
291 292
293 TEST_F(ServiceWorkerControlleeRequestHandlerTest, FallbackWithNoFetchHandler) {
294 version_->SetStatus(ServiceWorkerVersion::ACTIVATED);
295 version_->set_has_fetch_handler(false);
296 registration_->SetActiveVersion(version_);
297 context()->storage()->StoreRegistration(
298 registration_.get(), version_.get(),
299 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback));
300 base::RunLoop().RunUntilIdle();
301
302 auto main_test_resources = ServiceWorkerRequestTestResources::Create(
303 this, GURL("http://host/scope/doc"), RESOURCE_TYPE_MAIN_FRAME);
304 ServiceWorkerURLRequestJob* main_job = main_test_resources->GetJob();
305
306 EXPECT_FALSE(main_job->ShouldFallbackToNetwork());
307 EXPECT_FALSE(main_job->ShouldForwardToServiceWorker());
308 EXPECT_FALSE(version_->HasControllee());
309
310 base::RunLoop().RunUntilIdle();
311
312 EXPECT_TRUE(main_job->ShouldFallbackToNetwork());
313 EXPECT_FALSE(main_job->ShouldForwardToServiceWorker());
314 EXPECT_TRUE(version_->HasControllee());
315 EXPECT_EQ(version_, provider_host_->controlling_version());
316
317 auto sub_test_resources = ServiceWorkerRequestTestResources::Create(
318 this, GURL("http://host/scope/doc/subresource"), RESOURCE_TYPE_IMAGE);
319 ServiceWorkerURLRequestJob* sub_job = sub_test_resources->GetJob();
320
321 // This job shouldn't be created because this worker doesn't have fetch
322 // handler.
323 EXPECT_EQ(nullptr, sub_job);
324 }
325
292 } // namespace content 326 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698