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

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

Issue 548773002: [ServiceWorker] Don't pass OPTIONS request to the ServiceWorker (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add test Created 6 years, 3 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 "base/files/scoped_temp_dir.h"
6 #include "base/logging.h"
7 #include "base/run_loop.h" 5 #include "base/run_loop.h"
8 #include "content/browser/browser_thread_impl.h"
9 #include "content/browser/fileapi/mock_url_request_delegate.h" 6 #include "content/browser/fileapi/mock_url_request_delegate.h"
10 #include "content/browser/service_worker/embedded_worker_test_helper.h" 7 #include "content/browser/service_worker/embedded_worker_test_helper.h"
11 #include "content/browser/service_worker/service_worker_context_core.h" 8 #include "content/browser/service_worker/service_worker_context_core.h"
12 #include "content/browser/service_worker/service_worker_controllee_request_handl er.h"
13 #include "content/browser/service_worker/service_worker_provider_host.h" 9 #include "content/browser/service_worker/service_worker_provider_host.h"
14 #include "content/browser/service_worker/service_worker_registration.h" 10 #include "content/browser/service_worker/service_worker_registration.h"
15 #include "content/browser/service_worker/service_worker_registration.h" 11 #include "content/browser/service_worker/service_worker_request_handler.h"
yhirano 2014/09/08 07:58:01 Can you move this include to the top?
horo 2014/09/08 08:13:24 Done.
16 #include "content/browser/service_worker/service_worker_url_request_job.h"
17 #include "content/browser/service_worker/service_worker_utils.h" 12 #include "content/browser/service_worker/service_worker_utils.h"
18 #include "content/common/resource_request_body.h" 13 #include "content/common/resource_request_body.h"
19 #include "content/public/test/test_browser_thread_bundle.h" 14 #include "content/public/test/test_browser_thread_bundle.h"
20 #include "net/url_request/url_request_context.h" 15 #include "net/url_request/url_request_context.h"
21 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "webkit/browser/blob/blob_storage_context.h"
22 18
23 namespace content { 19 namespace content {
24 20
25 namespace { 21 namespace {
26 22
27 int kMockRenderProcessId = 1224; 23 int kMockRenderProcessId = 1224;
24 int kMockProviderId = 1;
28 25
29 void EmptyCallback() {} 26 void EmptyCallback() {
27 }
30 28
31 } 29 }
32 30
33 class ServiceWorkerControlleeRequestHandlerTest : public testing::Test { 31 class ServiceWorkerRequestHandlerTest : public testing::Test {
34 public: 32 public:
35 ServiceWorkerControlleeRequestHandlerTest() 33 ServiceWorkerRequestHandlerTest()
36 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} 34 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {}
37 35
38 virtual void SetUp() OVERRIDE { 36 virtual void SetUp() OVERRIDE {
39 helper_.reset(new EmbeddedWorkerTestHelper(kMockRenderProcessId)); 37 helper_.reset(new EmbeddedWorkerTestHelper(kMockRenderProcessId));
40 38
41 // A new unstored registration/version. 39 // A new unstored registration/version.
42 scope_ = GURL("http://host/scope/");
43 script_url_ = GURL("http://host/script.js");
44 registration_ = new ServiceWorkerRegistration( 40 registration_ = new ServiceWorkerRegistration(
45 scope_, 1L, context()->AsWeakPtr()); 41 GURL("http://host/scope/"), 1L, context()->AsWeakPtr());
46 version_ = new ServiceWorkerVersion( 42 version_ = new ServiceWorkerVersion(registration_.get(),
47 registration_.get(), script_url_, 1L, context()->AsWeakPtr()); 43 GURL("http://host/script.js"),
44 1L,
45 context()->AsWeakPtr());
48 46
49 // An empty host. 47 // An empty host.
50 scoped_ptr<ServiceWorkerProviderHost> host(new ServiceWorkerProviderHost( 48 scoped_ptr<ServiceWorkerProviderHost> host(new ServiceWorkerProviderHost(
51 kMockRenderProcessId, 1 /* provider_id */, 49 kMockRenderProcessId, kMockProviderId, context()->AsWeakPtr(), NULL));
52 context()->AsWeakPtr(), NULL));
53 provider_host_ = host->AsWeakPtr(); 50 provider_host_ = host->AsWeakPtr();
54 context()->AddProviderHost(host.Pass()); 51 context()->AddProviderHost(host.Pass());
55 52
56 context()->storage()->LazyInitialize(base::Bind(&EmptyCallback)); 53 context()->storage()->LazyInitialize(base::Bind(&EmptyCallback));
57 base::RunLoop().RunUntilIdle(); 54 base::RunLoop().RunUntilIdle();
55
56 version_->SetStatus(ServiceWorkerVersion::ACTIVATED);
57 registration_->SetActiveVersion(version_.get());
58 context()->storage()->StoreRegistration(
59 registration_.get(),
60 version_.get(),
61 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback));
62 provider_host_->AssociateRegistration(registration_.get());
63 base::RunLoop().RunUntilIdle();
58 } 64 }
59 65
60 virtual void TearDown() OVERRIDE { 66 virtual void TearDown() OVERRIDE {
61 version_ = NULL; 67 version_ = NULL;
62 registration_ = NULL; 68 registration_ = NULL;
63 helper_.reset(); 69 helper_.reset();
64 } 70 }
65 71
66 ServiceWorkerContextCore* context() const { return helper_->context(); } 72 ServiceWorkerContextCore* context() const { return helper_->context(); }
73 ServiceWorkerContextWrapper* context_wrapper() const {
74 return helper_->context_wrapper();
75 }
76
77 bool InitializeHandlerCheck(const std::string& url,
78 const std::string& method,
79 bool skip_service_worker,
80 ResourceType resource_type) {
81 const GURL kDocUrl(url);
82 scoped_ptr<net::URLRequest> request = url_request_context_.CreateRequest(
83 kDocUrl, net::DEFAULT_PRIORITY, &url_request_delegate_, NULL);
84 request->set_method(method);
85 ServiceWorkerRequestHandler::InitializeHandler(request.get(),
86 context_wrapper(),
87 &blob_storage_context_,
88 kMockRenderProcessId,
89 kMockProviderId,
90 skip_service_worker,
91 resource_type,
92 NULL);
93 return ServiceWorkerRequestHandler::GetHandler(request.get()) != NULL;
94 }
67 95
68 protected: 96 protected:
69 TestBrowserThreadBundle browser_thread_bundle_; 97 TestBrowserThreadBundle browser_thread_bundle_;
70 scoped_ptr<EmbeddedWorkerTestHelper> helper_; 98 scoped_ptr<EmbeddedWorkerTestHelper> helper_;
71 scoped_refptr<ServiceWorkerRegistration> registration_; 99 scoped_refptr<ServiceWorkerRegistration> registration_;
72 scoped_refptr<ServiceWorkerVersion> version_; 100 scoped_refptr<ServiceWorkerVersion> version_;
73 base::WeakPtr<ServiceWorkerProviderHost> provider_host_; 101 base::WeakPtr<ServiceWorkerProviderHost> provider_host_;
74 net::URLRequestContext url_request_context_; 102 net::URLRequestContext url_request_context_;
75 MockURLRequestDelegate url_request_delegate_; 103 MockURLRequestDelegate url_request_delegate_;
76 GURL scope_; 104 storage::BlobStorageContext blob_storage_context_;
77 GURL script_url_;
78 }; 105 };
79 106
80 TEST_F(ServiceWorkerControlleeRequestHandlerTest, ActivateWaitingVersion) { 107 TEST_F(ServiceWorkerRequestHandlerTest, InitializeHandler) {
81 // Store a registration that is installed but not activated yet. 108 EXPECT_TRUE(InitializeHandlerCheck(
82 version_->SetStatus(ServiceWorkerVersion::INSTALLED); 109 "http://host/scope/doc", "GET", false, RESOURCE_TYPE_MAIN_FRAME));
83 registration_->SetWaitingVersion(version_.get()); 110 EXPECT_TRUE(InitializeHandlerCheck(
84 context()->storage()->StoreRegistration( 111 "https://host/scope/doc", "GET", false, RESOURCE_TYPE_MAIN_FRAME));
85 registration_.get(), 112 EXPECT_FALSE(InitializeHandlerCheck(
86 version_.get(), 113 "ftp://host/scope/doc", "GET", false, RESOURCE_TYPE_MAIN_FRAME));
87 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback));
88 base::RunLoop().RunUntilIdle();
89 114
90 // Conduct a main resource load. 115 EXPECT_FALSE(InitializeHandlerCheck(
91 const GURL kDocUrl("http://host/scope/doc"); 116 "http://host/scope/doc", "OPTIONS", false, RESOURCE_TYPE_MAIN_FRAME));
92 scoped_ptr<net::URLRequest> request = url_request_context_.CreateRequest( 117 EXPECT_FALSE(InitializeHandlerCheck(
93 kDocUrl, 118 "https://host/scope/doc", "OPTIONS", false, RESOURCE_TYPE_MAIN_FRAME));
94 net::DEFAULT_PRIORITY,
95 &url_request_delegate_,
96 NULL);
97 scoped_ptr<ServiceWorkerControlleeRequestHandler> handler(
98 new ServiceWorkerControlleeRequestHandler(
99 context()->AsWeakPtr(),
100 provider_host_,
101 base::WeakPtr<storage::BlobStorageContext>(),
102 RESOURCE_TYPE_MAIN_FRAME,
103 scoped_refptr<ResourceRequestBody>()));
104 scoped_refptr<net::URLRequestJob> job =
105 handler->MaybeCreateJob(request.get(), NULL);
106 ServiceWorkerURLRequestJob* sw_job =
107 static_cast<ServiceWorkerURLRequestJob*>(job.get());
108 119
109 EXPECT_FALSE(sw_job->ShouldFallbackToNetwork()); 120 provider_host_->SetDocumentUrl(GURL(""));
110 EXPECT_FALSE(sw_job->ShouldForwardToServiceWorker()); 121 EXPECT_FALSE(InitializeHandlerCheck(
111 EXPECT_FALSE(version_->HasControllee()); 122 "http://host/scope/doc", "GET", true, RESOURCE_TYPE_MAIN_FRAME));
123 EXPECT_STREQ("http://host/scope/doc",
124 provider_host_->document_url().spec().c_str());
125 EXPECT_FALSE(InitializeHandlerCheck(
126 "https://host/scope/doc", "GET", true, RESOURCE_TYPE_MAIN_FRAME));
127 EXPECT_STREQ("https://host/scope/doc",
128 provider_host_->document_url().spec().c_str());
112 129
113 base::RunLoop().RunUntilIdle(); 130 provider_host_->SetDocumentUrl(GURL(""));
131 EXPECT_FALSE(InitializeHandlerCheck(
132 "http://host/scope/doc", "GET", true, RESOURCE_TYPE_SUB_FRAME));
133 EXPECT_STREQ("http://host/scope/doc",
134 provider_host_->document_url().spec().c_str());
135 EXPECT_FALSE(InitializeHandlerCheck(
136 "https://host/scope/doc", "GET", true, RESOURCE_TYPE_SUB_FRAME));
137 EXPECT_STREQ("https://host/scope/doc",
138 provider_host_->document_url().spec().c_str());
114 139
115 EXPECT_EQ(ServiceWorkerVersion::ACTIVATED, 140 provider_host_->SetDocumentUrl(GURL(""));
116 version_->status()); 141 EXPECT_FALSE(InitializeHandlerCheck(
117 EXPECT_FALSE(sw_job->ShouldFallbackToNetwork()); 142 "http://host/scope/doc", "GET", true, RESOURCE_TYPE_IMAGE));
118 EXPECT_TRUE(sw_job->ShouldForwardToServiceWorker()); 143 EXPECT_STREQ("", provider_host_->document_url().spec().c_str());
119 EXPECT_TRUE(version_->HasControllee()); 144 EXPECT_FALSE(InitializeHandlerCheck(
120 145 "https://host/scope/doc", "GET", true, RESOURCE_TYPE_IMAGE));
121 // Navigations should trigger an update too. 146 EXPECT_STREQ("", provider_host_->document_url().spec().c_str());
122 handler.reset(NULL);
123 EXPECT_TRUE(version_->update_timer_.IsRunning());
124 } 147 }
125 148
126 } // namespace content 149 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698