service_worker_url_request_job_unittest.cc revision 03b57e008b61dfcb1fbad3aea950ae0e001748b0
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"
31a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
32cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "webkit/browser/blob/blob_storage_context.h"
33cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "webkit/browser/blob/blob_url_request_job.h"
34cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "webkit/browser/blob/blob_url_request_job_factory.h"
35cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "webkit/common/blob/blob_data.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/"),
104a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        GURL("http://example.com/service_worker.js"),
1055c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu        1L,
1065c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu        helper_->context()->AsWeakPtr());
107a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    version_ = new ServiceWorkerVersion(
1085c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu        registration_, 1L, helper_->context()->AsWeakPtr());
109a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
110a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    scoped_ptr<ServiceWorkerProviderHost> provider_host(
1115c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu        new ServiceWorkerProviderHost(
1125c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu            kProcessID, kProviderID, helper_->context()->AsWeakPtr(), NULL));
1136e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    provider_host->AssociateRegistration(registration_);
1146e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    registration_->SetActiveVersion(version_.get());
115a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
116cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    ChromeBlobStorageContext* chrome_blob_storage_context =
117cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        ChromeBlobStorageContext::GetFor(browser_context_.get());
118cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    // Wait for chrome_blob_storage_context to finish initializing.
119cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    base::RunLoop().RunUntilIdle();
12003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    storage::BlobStorageContext* blob_storage_context =
121cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        chrome_blob_storage_context->context();
122cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
123cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    url_request_job_factory_.reset(new net::URLRequestJobFactoryImpl);
124cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    url_request_job_factory_->SetProtocolHandler(
125cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        "http",
126cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        new MockHttpProtocolHandler(provider_host->AsWeakPtr(),
127cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                    blob_storage_context->AsWeakPtr()));
128cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    url_request_job_factory_->SetProtocolHandler(
129cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        "blob", CreateMockBlobProtocolHandler(blob_storage_context));
130cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    url_request_context_.set_job_factory(url_request_job_factory_.get());
131a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
1325c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    helper_->context()->AddProviderHost(provider_host.Pass());
133a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
134a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
135a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual void TearDown() OVERRIDE {
136c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    version_ = NULL;
137c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    registration_ = NULL;
138a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    helper_.reset();
139a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
140a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
141cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  void TestRequest(int expected_status_code,
142cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                   const std::string& expected_status_text,
1435f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                   const std::string& expected_response) {
144a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    request_ = url_request_context_.CreateRequest(
145a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        GURL("http://example.com/foo.html"),
146a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        net::DEFAULT_PRIORITY,
147a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        &url_request_delegate_,
148a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        NULL);
149a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
150a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    request_->set_method("GET");
151a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    request_->Start();
152a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    base::RunLoop().RunUntilIdle();
153a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    EXPECT_TRUE(request_->status().is_success());
154cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    EXPECT_EQ(expected_status_code,
155cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)              request_->response_headers()->response_code());
156cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    EXPECT_EQ(expected_status_text,
157cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)              request_->response_headers()->GetStatusText());
158cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    EXPECT_EQ(expected_response, url_request_delegate_.response_data());
159a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
160a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
161a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  TestBrowserThreadBundle thread_bundle_;
162a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
163cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  scoped_ptr<TestBrowserContext> browser_context_;
164a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  scoped_ptr<EmbeddedWorkerTestHelper> helper_;
165a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  scoped_refptr<ServiceWorkerRegistration> registration_;
166a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  scoped_refptr<ServiceWorkerVersion> version_;
167a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
168cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  scoped_ptr<net::URLRequestJobFactoryImpl> url_request_job_factory_;
169a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  net::URLRequestContext url_request_context_;
170a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  MockURLRequestDelegate url_request_delegate_;
171a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  scoped_ptr<net::URLRequest> request_;
172a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
17303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  scoped_refptr<storage::BlobData> blob_data_;
174cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
1755f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) private:
176a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(ServiceWorkerURLRequestJobTest);
177a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)};
178a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
179a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)TEST_F(ServiceWorkerURLRequestJobTest, Simple) {
180116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  version_->SetStatus(ServiceWorkerVersion::ACTIVATED);
181cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  TestRequest(200, "OK", std::string());
182a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
183a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
184cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Responds to fetch events with a blob.
185cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)class BlobResponder : public EmbeddedWorkerTestHelper {
186cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) public:
187cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  BlobResponder(int mock_render_process_id, const std::string& blob_uuid)
188cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      : EmbeddedWorkerTestHelper(mock_render_process_id),
189cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        blob_uuid_(blob_uuid) {}
190cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual ~BlobResponder() {}
191cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
192cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) protected:
193cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual void OnFetchEvent(int embedded_worker_id,
194cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                            int request_id,
195cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                            const ServiceWorkerFetchRequest& request) OVERRIDE {
196cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    SimulateSend(new ServiceWorkerHostMsg_FetchEventFinished(
197cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        embedded_worker_id,
198cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        request_id,
199cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        SERVICE_WORKER_FETCH_EVENT_RESULT_RESPONSE,
2005f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        ServiceWorkerResponse(GURL(""),
2015f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                              200,
202cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                              "OK",
203cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                              std::map<std::string, std::string>(),
204cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                              blob_uuid_)));
205cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
206cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
207cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  std::string blob_uuid_;
2085f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
2095f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) private:
210cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(BlobResponder);
211cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)};
212cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
213cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)TEST_F(ServiceWorkerURLRequestJobTest, BlobResponse) {
214cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  ChromeBlobStorageContext* blob_storage_context =
215cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      ChromeBlobStorageContext::GetFor(browser_context_.get());
216cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  std::string expected_response;
217cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  for (int i = 0; i < 1024; ++i) {
218cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    blob_data_->AppendData(kTestData);
219cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    expected_response += kTestData;
220cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
22103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  scoped_ptr<storage::BlobDataHandle> blob_handle =
222cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      blob_storage_context->context()->AddFinishedBlob(blob_data_);
223cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  SetUpWithHelper(new BlobResponder(kProcessID, blob_handle->uuid()));
224cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
225116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  version_->SetStatus(ServiceWorkerVersion::ACTIVATED);
226cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  TestRequest(200, "OK", expected_response);
227cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
228cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
229cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)TEST_F(ServiceWorkerURLRequestJobTest, NonExistentBlobUUIDResponse) {
230cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  SetUpWithHelper(new BlobResponder(kProcessID, "blob-id:nothing-is-here"));
231116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  version_->SetStatus(ServiceWorkerVersion::ACTIVATED);
232cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  TestRequest(500, "Service Worker Response Error", std::string());
233cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
234cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
235a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// TODO(kinuko): Add more tests with different response data and also for
236a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// FallbackToNetwork case.
237a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
238a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}  // namespace content
239