1a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
2a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// found in the LICENSE file.
4a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
5a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/basictypes.h"
6a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/callback.h"
7a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
8cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/message_loop/message_loop.h"
9a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/run_loop.h"
10cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "content/browser/fileapi/chrome_blob_storage_context.h"
11cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "content/browser/fileapi/mock_url_request_delegate.h"
12a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "content/browser/service_worker/embedded_worker_registry.h"
13a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "content/browser/service_worker/embedded_worker_test_helper.h"
14a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "content/browser/service_worker/service_worker_context_core.h"
15a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "content/browser/service_worker/service_worker_provider_host.h"
16a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "content/browser/service_worker/service_worker_registration.h"
17a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "content/browser/service_worker/service_worker_test_utils.h"
18a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "content/browser/service_worker/service_worker_url_request_job.h"
19a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "content/browser/service_worker/service_worker_version.h"
2003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)#include "content/common/resource_request_body.h"
21cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "content/common/service_worker/service_worker_messages.h"
22cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "content/public/browser/blob_handle.h"
23cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "content/public/test/test_browser_context.h"
24a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "content/public/test/test_browser_thread_bundle.h"
25a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "net/base/io_buffer.h"
26a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "net/http/http_request_headers.h"
27a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "net/http/http_response_headers.h"
28a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "net/url_request/url_request.h"
29a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "net/url_request/url_request_context.h"
30a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "net/url_request/url_request_job_factory_impl.h"
311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "storage/browser/blob/blob_storage_context.h"
321320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "storage/browser/blob/blob_url_request_job.h"
331320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "storage/browser/blob/blob_url_request_job_factory.h"
341320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "storage/common/blob/blob_data.h"
35a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
36a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
37a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)namespace content {
38a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
39cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)class ServiceWorkerURLRequestJobTest;
40cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
41a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)namespace {
42a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
43a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)const int kProcessID = 1;
44a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)const int kProviderID = 100;
45cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)const char kTestData[] = "Here is sample text for the blob.";
46a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
47cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)class MockHttpProtocolHandler
48cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    : public net::URLRequestJobFactory::ProtocolHandler {
49a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) public:
50cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  MockHttpProtocolHandler(
51cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      base::WeakPtr<ServiceWorkerProviderHost> provider_host,
5203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      base::WeakPtr<storage::BlobStorageContext> blob_storage_context)
53cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      : provider_host_(provider_host),
54cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        blob_storage_context_(blob_storage_context) {}
55cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual ~MockHttpProtocolHandler() {}
56a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
57a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual net::URLRequestJob* MaybeCreateJob(
58a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      net::URLRequest* request,
59a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      net::NetworkDelegate* network_delegate) const OVERRIDE {
6003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    ServiceWorkerURLRequestJob* job =
6103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)        new ServiceWorkerURLRequestJob(request,
6203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)                                       network_delegate,
6303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)                                       provider_host_,
6403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)                                       blob_storage_context_,
6503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)                                       scoped_refptr<ResourceRequestBody>());
66a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    job->ForwardToServiceWorker();
67a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return job;
68a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
69a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
70a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) private:
71a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  base::WeakPtr<ServiceWorkerProviderHost> provider_host_;
7203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  base::WeakPtr<storage::BlobStorageContext> blob_storage_context_;
73a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)};
74a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
75cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Returns a BlobProtocolHandler that uses |blob_storage_context|. Caller owns
76cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// the memory.
7703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)storage::BlobProtocolHandler* CreateMockBlobProtocolHandler(
7803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    storage::BlobStorageContext* blob_storage_context) {
79cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // The FileSystemContext and MessageLoopProxy are not actually used but a
80cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // MessageLoopProxy is needed to avoid a DCHECK in BlobURLRequestJob ctor.
8103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  return new storage::BlobProtocolHandler(
82cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      blob_storage_context, NULL, base::MessageLoopProxy::current().get());
83cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
84cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
85a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}  // namespace
86a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
87a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class ServiceWorkerURLRequestJobTest : public testing::Test {
88a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) protected:
89a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ServiceWorkerURLRequestJobTest()
90cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP),
9103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)        blob_data_(new storage::BlobData("blob-id:myblob")) {}
92a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual ~ServiceWorkerURLRequestJobTest() {}
93a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
94a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual void SetUp() OVERRIDE {
95cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    browser_context_.reset(new TestBrowserContext);
96cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    SetUpWithHelper(new EmbeddedWorkerTestHelper(kProcessID));
97cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
98cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
99cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  void SetUpWithHelper(EmbeddedWorkerTestHelper* helper) {
100cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    helper_.reset(helper);
101a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
102a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    registration_ = new ServiceWorkerRegistration(
1035f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        GURL("http://example.com/"),
1045c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu        1L,
1055c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu        helper_->context()->AsWeakPtr());
106a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    version_ = new ServiceWorkerVersion(
1071320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        registration_.get(),
1081320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        GURL("http://example.com/service_worker.js"),
1091320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        1L,
1101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        helper_->context()->AsWeakPtr());
111a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
112a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    scoped_ptr<ServiceWorkerProviderHost> provider_host(
1135c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu        new ServiceWorkerProviderHost(
1145c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu            kProcessID, kProviderID, helper_->context()->AsWeakPtr(), NULL));
1151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    provider_host->AssociateRegistration(registration_.get());
1166e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    registration_->SetActiveVersion(version_.get());
117a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
118cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    ChromeBlobStorageContext* chrome_blob_storage_context =
119cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        ChromeBlobStorageContext::GetFor(browser_context_.get());
120cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    // Wait for chrome_blob_storage_context to finish initializing.
121cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    base::RunLoop().RunUntilIdle();
12203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    storage::BlobStorageContext* blob_storage_context =
123cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        chrome_blob_storage_context->context();
124cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
125cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    url_request_job_factory_.reset(new net::URLRequestJobFactoryImpl);
126cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    url_request_job_factory_->SetProtocolHandler(
127cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        "http",
128cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        new MockHttpProtocolHandler(provider_host->AsWeakPtr(),
129cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                    blob_storage_context->AsWeakPtr()));
130cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    url_request_job_factory_->SetProtocolHandler(
131cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        "blob", CreateMockBlobProtocolHandler(blob_storage_context));
132cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    url_request_context_.set_job_factory(url_request_job_factory_.get());
133a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
1345c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    helper_->context()->AddProviderHost(provider_host.Pass());
135a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
136a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
137a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual void TearDown() OVERRIDE {
138c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    version_ = NULL;
139c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    registration_ = NULL;
140a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    helper_.reset();
141a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
142a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
143cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  void TestRequest(int expected_status_code,
144cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                   const std::string& expected_status_text,
1455f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                   const std::string& expected_response) {
146a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    request_ = url_request_context_.CreateRequest(
147a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        GURL("http://example.com/foo.html"),
148a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        net::DEFAULT_PRIORITY,
149a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        &url_request_delegate_,
150a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        NULL);
151a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
152a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    request_->set_method("GET");
153a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    request_->Start();
154a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    base::RunLoop().RunUntilIdle();
155a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    EXPECT_TRUE(request_->status().is_success());
156cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    EXPECT_EQ(expected_status_code,
157cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)              request_->response_headers()->response_code());
158cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    EXPECT_EQ(expected_status_text,
159cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)              request_->response_headers()->GetStatusText());
160cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    EXPECT_EQ(expected_response, url_request_delegate_.response_data());
161a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
162a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
163a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  TestBrowserThreadBundle thread_bundle_;
164a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
165cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  scoped_ptr<TestBrowserContext> browser_context_;
166a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  scoped_ptr<EmbeddedWorkerTestHelper> helper_;
167a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  scoped_refptr<ServiceWorkerRegistration> registration_;
168a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  scoped_refptr<ServiceWorkerVersion> version_;
169a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
170cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  scoped_ptr<net::URLRequestJobFactoryImpl> url_request_job_factory_;
171a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  net::URLRequestContext url_request_context_;
172a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  MockURLRequestDelegate url_request_delegate_;
173a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  scoped_ptr<net::URLRequest> request_;
174a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
17503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  scoped_refptr<storage::BlobData> blob_data_;
176cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
1775f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) private:
178a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(ServiceWorkerURLRequestJobTest);
179a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)};
180a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
181a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)TEST_F(ServiceWorkerURLRequestJobTest, Simple) {
182116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  version_->SetStatus(ServiceWorkerVersion::ACTIVATED);
183cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  TestRequest(200, "OK", std::string());
184a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
185a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
186cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Responds to fetch events with a blob.
187cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)class BlobResponder : public EmbeddedWorkerTestHelper {
188cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) public:
189cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  BlobResponder(int mock_render_process_id, const std::string& blob_uuid)
190cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      : EmbeddedWorkerTestHelper(mock_render_process_id),
191cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        blob_uuid_(blob_uuid) {}
192cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual ~BlobResponder() {}
193cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
194cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) protected:
195cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual void OnFetchEvent(int embedded_worker_id,
196cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                            int request_id,
197cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                            const ServiceWorkerFetchRequest& request) OVERRIDE {
198cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    SimulateSend(new ServiceWorkerHostMsg_FetchEventFinished(
199cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        embedded_worker_id,
200cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        request_id,
201cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        SERVICE_WORKER_FETCH_EVENT_RESULT_RESPONSE,
2021320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        ServiceWorkerResponse(
2031320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci            GURL(""), 200, "OK", ServiceWorkerHeaderMap(), blob_uuid_)));
204cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
205cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
206cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  std::string blob_uuid_;
2075f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
2085f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) private:
209cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(BlobResponder);
210cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)};
211cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
212cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)TEST_F(ServiceWorkerURLRequestJobTest, BlobResponse) {
213cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  ChromeBlobStorageContext* blob_storage_context =
214cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      ChromeBlobStorageContext::GetFor(browser_context_.get());
215cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  std::string expected_response;
216cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  for (int i = 0; i < 1024; ++i) {
217cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    blob_data_->AppendData(kTestData);
218cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    expected_response += kTestData;
219cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
22003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  scoped_ptr<storage::BlobDataHandle> blob_handle =
2211320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      blob_storage_context->context()->AddFinishedBlob(blob_data_.get());
222cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  SetUpWithHelper(new BlobResponder(kProcessID, blob_handle->uuid()));
223cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
224116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  version_->SetStatus(ServiceWorkerVersion::ACTIVATED);
225cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  TestRequest(200, "OK", expected_response);
226cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
227cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
228cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)TEST_F(ServiceWorkerURLRequestJobTest, NonExistentBlobUUIDResponse) {
229cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  SetUpWithHelper(new BlobResponder(kProcessID, "blob-id:nothing-is-here"));
230116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  version_->SetStatus(ServiceWorkerVersion::ACTIVATED);
231cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  TestRequest(500, "Service Worker Response Error", std::string());
232cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
233cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
234a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// TODO(kinuko): Add more tests with different response data and also for
235a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// FallbackToNetwork case.
236a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
237a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}  // namespace content
238