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
3// found in the LICENSE file.
4
5#include "content/browser/service_worker/service_worker_request_handler.h"
6
7#include "base/run_loop.h"
8#include "content/browser/fileapi/mock_url_request_delegate.h"
9#include "content/browser/service_worker/embedded_worker_test_helper.h"
10#include "content/browser/service_worker/service_worker_context_core.h"
11#include "content/browser/service_worker/service_worker_provider_host.h"
12#include "content/browser/service_worker/service_worker_registration.h"
13#include "content/browser/service_worker/service_worker_utils.h"
14#include "content/common/resource_request_body.h"
15#include "content/public/test/test_browser_thread_bundle.h"
16#include "net/url_request/url_request_context.h"
17#include "storage/browser/blob/blob_storage_context.h"
18#include "testing/gtest/include/gtest/gtest.h"
19
20namespace content {
21
22namespace {
23
24int kMockRenderProcessId = 1224;
25int kMockProviderId = 1;
26
27void EmptyCallback() {
28}
29
30}
31
32class ServiceWorkerRequestHandlerTest : public testing::Test {
33 public:
34  ServiceWorkerRequestHandlerTest()
35      : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {}
36
37  virtual void SetUp() OVERRIDE {
38    helper_.reset(new EmbeddedWorkerTestHelper(kMockRenderProcessId));
39
40    // A new unstored registration/version.
41    registration_ = new ServiceWorkerRegistration(
42        GURL("http://host/scope/"), 1L, context()->AsWeakPtr());
43    version_ = new ServiceWorkerVersion(registration_.get(),
44                                        GURL("http://host/script.js"),
45                                        1L,
46                                        context()->AsWeakPtr());
47
48    // An empty host.
49    scoped_ptr<ServiceWorkerProviderHost> host(new ServiceWorkerProviderHost(
50        kMockRenderProcessId, kMockProviderId, context()->AsWeakPtr(), NULL));
51    provider_host_ = host->AsWeakPtr();
52    context()->AddProviderHost(host.Pass());
53
54    context()->storage()->LazyInitialize(base::Bind(&EmptyCallback));
55    base::RunLoop().RunUntilIdle();
56
57    version_->SetStatus(ServiceWorkerVersion::ACTIVATED);
58    registration_->SetActiveVersion(version_.get());
59    context()->storage()->StoreRegistration(
60        registration_.get(),
61        version_.get(),
62        base::Bind(&ServiceWorkerUtils::NoOpStatusCallback));
63    provider_host_->AssociateRegistration(registration_.get());
64    base::RunLoop().RunUntilIdle();
65  }
66
67  virtual void TearDown() OVERRIDE {
68    version_ = NULL;
69    registration_ = NULL;
70    helper_.reset();
71  }
72
73  ServiceWorkerContextCore* context() const { return helper_->context(); }
74  ServiceWorkerContextWrapper* context_wrapper() const {
75    return helper_->context_wrapper();
76  }
77
78  bool InitializeHandlerCheck(const std::string& url,
79                              const std::string& method,
80                              bool skip_service_worker,
81                              ResourceType resource_type) {
82    const GURL kDocUrl(url);
83    scoped_ptr<net::URLRequest> request = url_request_context_.CreateRequest(
84        kDocUrl, net::DEFAULT_PRIORITY, &url_request_delegate_, NULL);
85    request->set_method(method);
86    ServiceWorkerRequestHandler::InitializeHandler(request.get(),
87                                                   context_wrapper(),
88                                                   &blob_storage_context_,
89                                                   kMockRenderProcessId,
90                                                   kMockProviderId,
91                                                   skip_service_worker,
92                                                   resource_type,
93                                                   NULL);
94    return ServiceWorkerRequestHandler::GetHandler(request.get()) != NULL;
95  }
96
97 protected:
98  TestBrowserThreadBundle browser_thread_bundle_;
99  scoped_ptr<EmbeddedWorkerTestHelper> helper_;
100  scoped_refptr<ServiceWorkerRegistration> registration_;
101  scoped_refptr<ServiceWorkerVersion> version_;
102  base::WeakPtr<ServiceWorkerProviderHost> provider_host_;
103  net::URLRequestContext url_request_context_;
104  MockURLRequestDelegate url_request_delegate_;
105  storage::BlobStorageContext blob_storage_context_;
106};
107
108TEST_F(ServiceWorkerRequestHandlerTest, InitializeHandler) {
109  EXPECT_TRUE(InitializeHandlerCheck(
110      "http://host/scope/doc", "GET", false, RESOURCE_TYPE_MAIN_FRAME));
111  EXPECT_TRUE(InitializeHandlerCheck(
112      "https://host/scope/doc", "GET", false, RESOURCE_TYPE_MAIN_FRAME));
113  EXPECT_FALSE(InitializeHandlerCheck(
114      "ftp://host/scope/doc", "GET", false, RESOURCE_TYPE_MAIN_FRAME));
115
116  EXPECT_FALSE(InitializeHandlerCheck(
117      "http://host/scope/doc", "OPTIONS", false, RESOURCE_TYPE_MAIN_FRAME));
118  EXPECT_FALSE(InitializeHandlerCheck(
119      "https://host/scope/doc", "OPTIONS", false, RESOURCE_TYPE_MAIN_FRAME));
120
121  provider_host_->SetDocumentUrl(GURL(""));
122  EXPECT_FALSE(InitializeHandlerCheck(
123      "http://host/scope/doc", "GET", true, RESOURCE_TYPE_MAIN_FRAME));
124  EXPECT_STREQ("http://host/scope/doc",
125               provider_host_->document_url().spec().c_str());
126  EXPECT_FALSE(InitializeHandlerCheck(
127      "https://host/scope/doc", "GET", true, RESOURCE_TYPE_MAIN_FRAME));
128  EXPECT_STREQ("https://host/scope/doc",
129               provider_host_->document_url().spec().c_str());
130
131  provider_host_->SetDocumentUrl(GURL(""));
132  EXPECT_FALSE(InitializeHandlerCheck(
133      "http://host/scope/doc", "GET", true, RESOURCE_TYPE_SUB_FRAME));
134  EXPECT_STREQ("http://host/scope/doc",
135               provider_host_->document_url().spec().c_str());
136  EXPECT_FALSE(InitializeHandlerCheck(
137      "https://host/scope/doc", "GET", true, RESOURCE_TYPE_SUB_FRAME));
138  EXPECT_STREQ("https://host/scope/doc",
139               provider_host_->document_url().spec().c_str());
140
141  provider_host_->SetDocumentUrl(GURL(""));
142  EXPECT_FALSE(InitializeHandlerCheck(
143      "http://host/scope/doc", "GET", true, RESOURCE_TYPE_IMAGE));
144  EXPECT_STREQ("", provider_host_->document_url().spec().c_str());
145  EXPECT_FALSE(InitializeHandlerCheck(
146      "https://host/scope/doc", "GET", true, RESOURCE_TYPE_IMAGE));
147  EXPECT_STREQ("", provider_host_->document_url().spec().c_str());
148}
149
150}  // namespace content
151