Index: content/browser/service_worker/service_worker_request_handler_unittest.cc |
diff --git a/content/browser/service_worker/service_worker_controllee_request_handler_unittest.cc b/content/browser/service_worker/service_worker_request_handler_unittest.cc |
similarity index 32% |
copy from content/browser/service_worker/service_worker_controllee_request_handler_unittest.cc |
copy to content/browser/service_worker/service_worker_request_handler_unittest.cc |
index b9d2b202562626d8ef439bcd7bce0c642dd010dc..a94c71d5c20ba18124ff8d0d873e804962a67b83 100644 |
--- a/content/browser/service_worker/service_worker_controllee_request_handler_unittest.cc |
+++ b/content/browser/service_worker/service_worker_request_handler_unittest.cc |
@@ -2,59 +2,66 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
-#include "base/files/scoped_temp_dir.h" |
-#include "base/logging.h" |
+#include "content/browser/service_worker/service_worker_request_handler.h" |
+ |
#include "base/run_loop.h" |
-#include "content/browser/browser_thread_impl.h" |
#include "content/browser/fileapi/mock_url_request_delegate.h" |
#include "content/browser/service_worker/embedded_worker_test_helper.h" |
#include "content/browser/service_worker/service_worker_context_core.h" |
-#include "content/browser/service_worker/service_worker_controllee_request_handler.h" |
#include "content/browser/service_worker/service_worker_provider_host.h" |
#include "content/browser/service_worker/service_worker_registration.h" |
-#include "content/browser/service_worker/service_worker_registration.h" |
-#include "content/browser/service_worker/service_worker_url_request_job.h" |
#include "content/browser/service_worker/service_worker_utils.h" |
#include "content/common/resource_request_body.h" |
#include "content/public/test/test_browser_thread_bundle.h" |
#include "net/url_request/url_request_context.h" |
#include "testing/gtest/include/gtest/gtest.h" |
+#include "webkit/browser/blob/blob_storage_context.h" |
namespace content { |
namespace { |
int kMockRenderProcessId = 1224; |
+int kMockProviderId = 1; |
-void EmptyCallback() {} |
+void EmptyCallback() { |
+} |
} |
-class ServiceWorkerControlleeRequestHandlerTest : public testing::Test { |
+class ServiceWorkerRequestHandlerTest : public testing::Test { |
public: |
- ServiceWorkerControlleeRequestHandlerTest() |
+ ServiceWorkerRequestHandlerTest() |
: browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} |
virtual void SetUp() OVERRIDE { |
helper_.reset(new EmbeddedWorkerTestHelper(kMockRenderProcessId)); |
// A new unstored registration/version. |
- scope_ = GURL("http://host/scope/"); |
- script_url_ = GURL("http://host/script.js"); |
registration_ = new ServiceWorkerRegistration( |
- scope_, 1L, context()->AsWeakPtr()); |
- version_ = new ServiceWorkerVersion( |
- registration_.get(), script_url_, 1L, context()->AsWeakPtr()); |
+ GURL("http://host/scope/"), 1L, context()->AsWeakPtr()); |
+ version_ = new ServiceWorkerVersion(registration_.get(), |
+ GURL("http://host/script.js"), |
+ 1L, |
+ context()->AsWeakPtr()); |
// An empty host. |
scoped_ptr<ServiceWorkerProviderHost> host(new ServiceWorkerProviderHost( |
- kMockRenderProcessId, 1 /* provider_id */, |
- context()->AsWeakPtr(), NULL)); |
+ kMockRenderProcessId, kMockProviderId, context()->AsWeakPtr(), NULL)); |
provider_host_ = host->AsWeakPtr(); |
context()->AddProviderHost(host.Pass()); |
context()->storage()->LazyInitialize(base::Bind(&EmptyCallback)); |
base::RunLoop().RunUntilIdle(); |
+ |
+ version_->SetStatus(ServiceWorkerVersion::ACTIVATED); |
+ registration_->SetActiveVersion(version_.get()); |
+ context()->storage()->StoreRegistration( |
+ registration_.get(), |
+ version_.get(), |
+ base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
+ provider_host_->AssociateRegistration(registration_.get()); |
+ base::RunLoop().RunUntilIdle(); |
} |
virtual void TearDown() OVERRIDE { |
@@ -64,6 +71,28 @@ class ServiceWorkerControlleeRequestHandlerTest : public testing::Test { |
} |
ServiceWorkerContextCore* context() const { return helper_->context(); } |
+ ServiceWorkerContextWrapper* context_wrapper() const { |
+ return helper_->context_wrapper(); |
+ } |
+ |
+ bool InitializeHandlerCheck(const std::string& url, |
+ const std::string& method, |
+ bool skip_service_worker, |
+ ResourceType resource_type) { |
+ const GURL kDocUrl(url); |
+ scoped_ptr<net::URLRequest> request = url_request_context_.CreateRequest( |
+ kDocUrl, net::DEFAULT_PRIORITY, &url_request_delegate_, NULL); |
+ request->set_method(method); |
+ ServiceWorkerRequestHandler::InitializeHandler(request.get(), |
+ context_wrapper(), |
+ &blob_storage_context_, |
+ kMockRenderProcessId, |
+ kMockProviderId, |
+ skip_service_worker, |
+ resource_type, |
+ NULL); |
+ return ServiceWorkerRequestHandler::GetHandler(request.get()) != NULL; |
+ } |
protected: |
TestBrowserThreadBundle browser_thread_bundle_; |
@@ -73,54 +102,49 @@ class ServiceWorkerControlleeRequestHandlerTest : public testing::Test { |
base::WeakPtr<ServiceWorkerProviderHost> provider_host_; |
net::URLRequestContext url_request_context_; |
MockURLRequestDelegate url_request_delegate_; |
- GURL scope_; |
- GURL script_url_; |
+ storage::BlobStorageContext blob_storage_context_; |
}; |
-TEST_F(ServiceWorkerControlleeRequestHandlerTest, ActivateWaitingVersion) { |
- // Store a registration that is installed but not activated yet. |
- version_->SetStatus(ServiceWorkerVersion::INSTALLED); |
- registration_->SetWaitingVersion(version_.get()); |
- context()->storage()->StoreRegistration( |
- registration_.get(), |
- version_.get(), |
- base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
- base::RunLoop().RunUntilIdle(); |
- |
- // Conduct a main resource load. |
- const GURL kDocUrl("http://host/scope/doc"); |
- scoped_ptr<net::URLRequest> request = url_request_context_.CreateRequest( |
- kDocUrl, |
- net::DEFAULT_PRIORITY, |
- &url_request_delegate_, |
- NULL); |
- scoped_ptr<ServiceWorkerControlleeRequestHandler> handler( |
- new ServiceWorkerControlleeRequestHandler( |
- context()->AsWeakPtr(), |
- provider_host_, |
- base::WeakPtr<storage::BlobStorageContext>(), |
- RESOURCE_TYPE_MAIN_FRAME, |
- scoped_refptr<ResourceRequestBody>())); |
- scoped_refptr<net::URLRequestJob> job = |
- handler->MaybeCreateJob(request.get(), NULL); |
- ServiceWorkerURLRequestJob* sw_job = |
- static_cast<ServiceWorkerURLRequestJob*>(job.get()); |
- |
- EXPECT_FALSE(sw_job->ShouldFallbackToNetwork()); |
- EXPECT_FALSE(sw_job->ShouldForwardToServiceWorker()); |
- EXPECT_FALSE(version_->HasControllee()); |
- |
- base::RunLoop().RunUntilIdle(); |
- |
- EXPECT_EQ(ServiceWorkerVersion::ACTIVATED, |
- version_->status()); |
- EXPECT_FALSE(sw_job->ShouldFallbackToNetwork()); |
- EXPECT_TRUE(sw_job->ShouldForwardToServiceWorker()); |
- EXPECT_TRUE(version_->HasControllee()); |
- |
- // Navigations should trigger an update too. |
- handler.reset(NULL); |
- EXPECT_TRUE(version_->update_timer_.IsRunning()); |
+TEST_F(ServiceWorkerRequestHandlerTest, InitializeHandler) { |
+ EXPECT_TRUE(InitializeHandlerCheck( |
+ "http://host/scope/doc", "GET", false, RESOURCE_TYPE_MAIN_FRAME)); |
+ EXPECT_TRUE(InitializeHandlerCheck( |
+ "https://host/scope/doc", "GET", false, RESOURCE_TYPE_MAIN_FRAME)); |
+ EXPECT_FALSE(InitializeHandlerCheck( |
+ "ftp://host/scope/doc", "GET", false, RESOURCE_TYPE_MAIN_FRAME)); |
+ |
+ EXPECT_FALSE(InitializeHandlerCheck( |
+ "http://host/scope/doc", "OPTIONS", false, RESOURCE_TYPE_MAIN_FRAME)); |
+ EXPECT_FALSE(InitializeHandlerCheck( |
+ "https://host/scope/doc", "OPTIONS", false, RESOURCE_TYPE_MAIN_FRAME)); |
+ |
+ provider_host_->SetDocumentUrl(GURL("")); |
+ EXPECT_FALSE(InitializeHandlerCheck( |
+ "http://host/scope/doc", "GET", true, RESOURCE_TYPE_MAIN_FRAME)); |
+ EXPECT_STREQ("http://host/scope/doc", |
+ provider_host_->document_url().spec().c_str()); |
+ EXPECT_FALSE(InitializeHandlerCheck( |
+ "https://host/scope/doc", "GET", true, RESOURCE_TYPE_MAIN_FRAME)); |
+ EXPECT_STREQ("https://host/scope/doc", |
+ provider_host_->document_url().spec().c_str()); |
+ |
+ provider_host_->SetDocumentUrl(GURL("")); |
+ EXPECT_FALSE(InitializeHandlerCheck( |
+ "http://host/scope/doc", "GET", true, RESOURCE_TYPE_SUB_FRAME)); |
+ EXPECT_STREQ("http://host/scope/doc", |
+ provider_host_->document_url().spec().c_str()); |
+ EXPECT_FALSE(InitializeHandlerCheck( |
+ "https://host/scope/doc", "GET", true, RESOURCE_TYPE_SUB_FRAME)); |
+ EXPECT_STREQ("https://host/scope/doc", |
+ provider_host_->document_url().spec().c_str()); |
+ |
+ provider_host_->SetDocumentUrl(GURL("")); |
+ EXPECT_FALSE(InitializeHandlerCheck( |
+ "http://host/scope/doc", "GET", true, RESOURCE_TYPE_IMAGE)); |
+ EXPECT_STREQ("", provider_host_->document_url().spec().c_str()); |
+ EXPECT_FALSE(InitializeHandlerCheck( |
+ "https://host/scope/doc", "GET", true, RESOURCE_TYPE_IMAGE)); |
+ EXPECT_STREQ("", provider_host_->document_url().spec().c_str()); |
} |
} // namespace content |