11320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// Copyright 2014 The Chromium Authors. All rights reserved.
21320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// Use of this source code is governed by a BSD-style license that can be
31320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// found in the LICENSE file.
41320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
51320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "content/browser/service_worker/service_worker_request_handler.h"
61320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
71320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/run_loop.h"
81320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "content/browser/fileapi/mock_url_request_delegate.h"
91320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "content/browser/service_worker/embedded_worker_test_helper.h"
101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "content/browser/service_worker/service_worker_context_core.h"
111320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "content/browser/service_worker/service_worker_provider_host.h"
121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "content/browser/service_worker/service_worker_registration.h"
131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "content/browser/service_worker/service_worker_utils.h"
141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "content/common/resource_request_body.h"
151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "content/public/test/test_browser_thread_bundle.h"
161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "net/url_request/url_request_context.h"
171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "storage/browser/blob/blob_storage_context.h"
181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "testing/gtest/include/gtest/gtest.h"
191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
201320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccinamespace content {
211320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
221320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccinamespace {
231320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciint kMockRenderProcessId = 1224;
251320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciint kMockProviderId = 1;
261320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccivoid EmptyCallback() {
281320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
291320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
301320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
321320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciclass ServiceWorkerRequestHandlerTest : public testing::Test {
331320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci public:
341320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  ServiceWorkerRequestHandlerTest()
351320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {}
361320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
371320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  virtual void SetUp() OVERRIDE {
381320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    helper_.reset(new EmbeddedWorkerTestHelper(kMockRenderProcessId));
391320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
401320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    // A new unstored registration/version.
411320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    registration_ = new ServiceWorkerRegistration(
421320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        GURL("http://host/scope/"), 1L, context()->AsWeakPtr());
431320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    version_ = new ServiceWorkerVersion(registration_.get(),
441320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                        GURL("http://host/script.js"),
451320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                        1L,
461320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                        context()->AsWeakPtr());
471320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
481320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    // An empty host.
491320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    scoped_ptr<ServiceWorkerProviderHost> host(new ServiceWorkerProviderHost(
501320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        kMockRenderProcessId, kMockProviderId, context()->AsWeakPtr(), NULL));
511320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    provider_host_ = host->AsWeakPtr();
521320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    context()->AddProviderHost(host.Pass());
531320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
541320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    context()->storage()->LazyInitialize(base::Bind(&EmptyCallback));
551320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    base::RunLoop().RunUntilIdle();
561320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
571320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    version_->SetStatus(ServiceWorkerVersion::ACTIVATED);
581320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    registration_->SetActiveVersion(version_.get());
591320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    context()->storage()->StoreRegistration(
601320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        registration_.get(),
611320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        version_.get(),
621320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        base::Bind(&ServiceWorkerUtils::NoOpStatusCallback));
631320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    provider_host_->AssociateRegistration(registration_.get());
641320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    base::RunLoop().RunUntilIdle();
651320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  }
661320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
671320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  virtual void TearDown() OVERRIDE {
681320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    version_ = NULL;
691320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    registration_ = NULL;
701320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    helper_.reset();
711320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  }
721320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
731320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  ServiceWorkerContextCore* context() const { return helper_->context(); }
741320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  ServiceWorkerContextWrapper* context_wrapper() const {
751320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    return helper_->context_wrapper();
761320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  }
771320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
781320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  bool InitializeHandlerCheck(const std::string& url,
791320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                              const std::string& method,
801320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                              bool skip_service_worker,
811320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                              ResourceType resource_type) {
821320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    const GURL kDocUrl(url);
831320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    scoped_ptr<net::URLRequest> request = url_request_context_.CreateRequest(
841320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        kDocUrl, net::DEFAULT_PRIORITY, &url_request_delegate_, NULL);
851320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    request->set_method(method);
861320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    ServiceWorkerRequestHandler::InitializeHandler(request.get(),
871320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                                   context_wrapper(),
881320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                                   &blob_storage_context_,
891320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                                   kMockRenderProcessId,
901320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                                   kMockProviderId,
911320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                                   skip_service_worker,
921320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                                   resource_type,
931320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                                   NULL);
941320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    return ServiceWorkerRequestHandler::GetHandler(request.get()) != NULL;
951320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  }
961320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
971320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci protected:
981320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  TestBrowserThreadBundle browser_thread_bundle_;
991320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  scoped_ptr<EmbeddedWorkerTestHelper> helper_;
1001320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  scoped_refptr<ServiceWorkerRegistration> registration_;
1011320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  scoped_refptr<ServiceWorkerVersion> version_;
1021320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  base::WeakPtr<ServiceWorkerProviderHost> provider_host_;
1031320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  net::URLRequestContext url_request_context_;
1041320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  MockURLRequestDelegate url_request_delegate_;
1051320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  storage::BlobStorageContext blob_storage_context_;
1061320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci};
1071320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1081320f92c476a1ad9d19dba2a48c72b75566198e9Primiano TucciTEST_F(ServiceWorkerRequestHandlerTest, InitializeHandler) {
1091320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  EXPECT_TRUE(InitializeHandlerCheck(
1101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      "http://host/scope/doc", "GET", false, RESOURCE_TYPE_MAIN_FRAME));
1111320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  EXPECT_TRUE(InitializeHandlerCheck(
1121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      "https://host/scope/doc", "GET", false, RESOURCE_TYPE_MAIN_FRAME));
1131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  EXPECT_FALSE(InitializeHandlerCheck(
1141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      "ftp://host/scope/doc", "GET", false, RESOURCE_TYPE_MAIN_FRAME));
1151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  EXPECT_FALSE(InitializeHandlerCheck(
1171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      "http://host/scope/doc", "OPTIONS", false, RESOURCE_TYPE_MAIN_FRAME));
1181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  EXPECT_FALSE(InitializeHandlerCheck(
1191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      "https://host/scope/doc", "OPTIONS", false, RESOURCE_TYPE_MAIN_FRAME));
1201320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1211320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  provider_host_->SetDocumentUrl(GURL(""));
1221320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  EXPECT_FALSE(InitializeHandlerCheck(
1231320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      "http://host/scope/doc", "GET", true, RESOURCE_TYPE_MAIN_FRAME));
1241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  EXPECT_STREQ("http://host/scope/doc",
1251320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci               provider_host_->document_url().spec().c_str());
1261320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  EXPECT_FALSE(InitializeHandlerCheck(
1271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      "https://host/scope/doc", "GET", true, RESOURCE_TYPE_MAIN_FRAME));
1281320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  EXPECT_STREQ("https://host/scope/doc",
1291320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci               provider_host_->document_url().spec().c_str());
1301320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  provider_host_->SetDocumentUrl(GURL(""));
1321320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  EXPECT_FALSE(InitializeHandlerCheck(
1331320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      "http://host/scope/doc", "GET", true, RESOURCE_TYPE_SUB_FRAME));
1341320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  EXPECT_STREQ("http://host/scope/doc",
1351320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci               provider_host_->document_url().spec().c_str());
1361320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  EXPECT_FALSE(InitializeHandlerCheck(
1371320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      "https://host/scope/doc", "GET", true, RESOURCE_TYPE_SUB_FRAME));
1381320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  EXPECT_STREQ("https://host/scope/doc",
1391320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci               provider_host_->document_url().spec().c_str());
1401320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1411320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  provider_host_->SetDocumentUrl(GURL(""));
1421320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  EXPECT_FALSE(InitializeHandlerCheck(
1431320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      "http://host/scope/doc", "GET", true, RESOURCE_TYPE_IMAGE));
1441320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  EXPECT_STREQ("", provider_host_->document_url().spec().c_str());
1451320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  EXPECT_FALSE(InitializeHandlerCheck(
1461320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      "https://host/scope/doc", "GET", true, RESOURCE_TYPE_IMAGE));
1471320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  EXPECT_STREQ("", provider_host_->document_url().spec().c_str());
1481320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
1491320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1501320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}  // namespace content
151