15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/url_request/test_url_fetcher_factory.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/bind.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/compiler_specific.h"
111320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/files/file_util.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/weak_ptr.h"
139ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include "base/message_loop/message_loop.h"
14cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/threading/thread_restrictions.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/host_port_pair.h"
165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "net/base/io_buffer.h"
17f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "net/base/net_errors.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/http/http_response_headers.h"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/url_request/url_fetcher_delegate.h"
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/url_request/url_fetcher_impl.h"
214e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "net/url_request/url_fetcher_response_writer.h"
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/url_request/url_request_status.h"
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace net {
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ScopedURLFetcherFactory::ScopedURLFetcherFactory(
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    URLFetcherFactory* factory) {
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(!URLFetcherImpl::factory());
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  URLFetcherImpl::set_factory(factory);
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ScopedURLFetcherFactory::~ScopedURLFetcherFactory() {
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(URLFetcherImpl::factory());
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  URLFetcherImpl::set_factory(NULL);
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TestURLFetcher::TestURLFetcher(int id,
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               const GURL& url,
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               URLFetcherDelegate* d)
402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    : owner_(NULL),
412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      id_(id),
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      original_url_(url),
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      delegate_(d),
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      delegate_for_tests_(NULL),
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      did_receive_last_chunk_(false),
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      fake_load_flags_(0),
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      fake_response_code_(-1),
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      fake_response_destination_(STRING),
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      fake_was_fetched_via_proxy_(false),
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      fake_max_retries_(0) {
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TestURLFetcher::~TestURLFetcher() {
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (delegate_for_tests_)
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    delegate_for_tests_->OnRequestEnd(id_);
562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (owner_)
572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    owner_->RemoveFetcherFromMap(id_);
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TestURLFetcher::SetUploadData(const std::string& upload_content_type,
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                   const std::string& upload_content) {
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  upload_data_ = upload_content;
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void TestURLFetcher::SetUploadFilePath(
662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::string& upload_content_type,
672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::FilePath& file_path,
68c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    uint64 range_offset,
69c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    uint64 range_length,
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    scoped_refptr<base::TaskRunner> file_task_runner) {
712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  upload_file_path_ = file_path;
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TestURLFetcher::SetChunkedUpload(const std::string& upload_content_type) {
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TestURLFetcher::AppendChunkToUpload(const std::string& data,
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                         bool is_last_chunk) {
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(!did_receive_last_chunk_);
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  did_receive_last_chunk_ = is_last_chunk;
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  chunks_.push_back(data);
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (delegate_for_tests_)
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    delegate_for_tests_->OnChunkUpload(id_);
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TestURLFetcher::SetLoadFlags(int load_flags) {
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  fake_load_flags_= load_flags;
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int TestURLFetcher::GetLoadFlags() const {
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return fake_load_flags_;
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TestURLFetcher::SetReferrer(const std::string& referrer) {
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void TestURLFetcher::SetReferrerPolicy(
985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    URLRequest::ReferrerPolicy referrer_policy) {
995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TestURLFetcher::SetExtraRequestHeaders(
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& extra_request_headers) {
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  fake_extra_request_headers_.Clear();
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  fake_extra_request_headers_.AddHeadersFromString(extra_request_headers);
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TestURLFetcher::AddExtraRequestHeader(const std::string& header_line) {
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  fake_extra_request_headers_.AddHeaderFromString(header_line);
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TestURLFetcher::SetRequestContext(
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    URLRequestContextGetter* request_context_getter) {
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TestURLFetcher::SetFirstPartyForCookies(
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const GURL& first_party_for_cookies) {
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TestURLFetcher::SetURLRequestUserData(
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const void* key,
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const CreateDataCallback& create_data_callback) {
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TestURLFetcher::SetStopOnRedirect(bool stop_on_redirect) {
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TestURLFetcher::SetAutomaticallyRetryOn5xx(bool retry) {
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void TestURLFetcher::SetMaxRetriesOn5xx(int max_retries) {
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  fake_max_retries_ = max_retries;
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)int TestURLFetcher::GetMaxRetriesOn5xx() const {
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return fake_max_retries_;
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)base::TimeDelta TestURLFetcher::GetBackoffDelay() const {
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return fake_backoff_delay_;
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void TestURLFetcher::SetAutomaticallyRetryOnNetworkChanges(int max_retries) {
1432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TestURLFetcher::SaveResponseToFileAtPath(
1462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::FilePath& file_path,
147f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    scoped_refptr<base::SequencedTaskRunner> file_task_runner) {
148cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  SetResponseFilePath(file_path);
149cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Asynchronous IO is not supported, so file_task_runner is ignored.
150cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  base::ThreadRestrictions::ScopedAllowIO allow_io;
151cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  const size_t written_bytes = base::WriteFile(
152cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      file_path, fake_response_string_.c_str(), fake_response_string_.size());
153cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  DCHECK_EQ(written_bytes, fake_response_string_.size());
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TestURLFetcher::SaveResponseToTemporaryFile(
157f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    scoped_refptr<base::SequencedTaskRunner> file_task_runner) {
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1604e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)void TestURLFetcher::SaveResponseWithWriter(
1614e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    scoped_ptr<URLFetcherResponseWriter> response_writer) {
162cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // In class URLFetcherCore this method is called by all three:
163cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // GetResponseAsString() / SaveResponseToFileAtPath() /
164cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // SaveResponseToTemporaryFile(). But here (in TestURLFetcher), this method
165cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // is never used by any of these three methods. So, file writing is expected
166cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // to be done in SaveResponseToFileAtPath(), and this method supports only
167cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // URLFetcherStringWriter (for testing of this method only).
1685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (fake_response_destination_ == STRING) {
1695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    response_writer_ = response_writer.Pass();
1705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    int response = response_writer_->Initialize(CompletionCallback());
1715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // The TestURLFetcher doesn't handle asynchronous writes.
1725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    DCHECK_EQ(OK, response);
1735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    scoped_refptr<IOBuffer> buffer(new StringIOBuffer(fake_response_string_));
1755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    response = response_writer_->Write(buffer.get(),
1765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                       fake_response_string_.size(),
1775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                       CompletionCallback());
1785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    DCHECK_EQ(static_cast<int>(fake_response_string_.size()), response);
1795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    response = response_writer_->Finish(CompletionCallback());
1805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    DCHECK_EQ(OK, response);
181cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  } else if (fake_response_destination_ == TEMP_FILE) {
182cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    // SaveResponseToFileAtPath() should be called instead of this method to
183cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    // save file. Asynchronous file writing using URLFetcherFileWriter is not
184cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    // supported.
1855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    NOTIMPLEMENTED();
186cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  } else {
187cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    NOTREACHED();
1885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
1894e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
1904e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)HttpResponseHeaders* TestURLFetcher::GetResponseHeaders() const {
192868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return fake_response_headers_.get();
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)HostPortPair TestURLFetcher::GetSocketAddress() const {
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NOTIMPLEMENTED();
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return HostPortPair();
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool TestURLFetcher::WasFetchedViaProxy() const {
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return fake_was_fetched_via_proxy_;
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TestURLFetcher::Start() {
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Overriden to do nothing. It is assumed the caller will notify the delegate.
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (delegate_for_tests_)
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    delegate_for_tests_->OnRequestStart(id_);
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const GURL& TestURLFetcher::GetOriginalURL() const {
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return original_url_;
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const GURL& TestURLFetcher::GetURL() const {
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return fake_url_;
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const URLRequestStatus& TestURLFetcher::GetStatus() const {
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return fake_status_;
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int TestURLFetcher::GetResponseCode() const {
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return fake_response_code_;
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const ResponseCookies& TestURLFetcher::GetCookies() const {
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return fake_cookies_;
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TestURLFetcher::ReceivedContentWasMalformed() {
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool TestURLFetcher::GetResponseAsString(
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::string* out_response_string) const {
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (fake_response_destination_ != STRING)
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  *out_response_string = fake_response_string_;
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return true;
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool TestURLFetcher::GetResponseAsFilePath(
2432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    bool take_ownership, base::FilePath* out_response_path) const {
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (fake_response_destination_ != TEMP_FILE)
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  *out_response_path = fake_response_file_path_;
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return true;
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void TestURLFetcher::GetExtraRequestHeaders(
2525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    HttpRequestHeaders* headers) const {
2535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  *headers = fake_extra_request_headers_;
2545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TestURLFetcher::set_status(const URLRequestStatus& status) {
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  fake_status_ = status;
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TestURLFetcher::set_was_fetched_via_proxy(bool flag) {
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  fake_was_fetched_via_proxy_ = flag;
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TestURLFetcher::set_response_headers(
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    scoped_refptr<HttpResponseHeaders> headers) {
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  fake_response_headers_ = headers;
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TestURLFetcher::set_backoff_delay(base::TimeDelta backoff_delay) {
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  fake_backoff_delay_ = backoff_delay;
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TestURLFetcher::SetDelegateForTests(DelegateForTests* delegate_for_tests) {
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  delegate_for_tests_ = delegate_for_tests;
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TestURLFetcher::SetResponseString(const std::string& response) {
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  fake_response_destination_ = STRING;
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  fake_response_string_ = response;
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void TestURLFetcher::SetResponseFilePath(const base::FilePath& path) {
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  fake_response_destination_ = TEMP_FILE;
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  fake_response_file_path_ = path;
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TestURLFetcherFactory::TestURLFetcherFactory()
288c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    : ScopedURLFetcherFactory(this),
2892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      delegate_for_tests_(NULL),
2902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      remove_fetcher_on_delete_(false) {
2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TestURLFetcherFactory::~TestURLFetcherFactory() {}
2945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)URLFetcher* TestURLFetcherFactory::CreateURLFetcher(
2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int id,
2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const GURL& url,
2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    URLFetcher::RequestType request_type,
2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    URLFetcherDelegate* d) {
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TestURLFetcher* fetcher = new TestURLFetcher(id, url, d);
3012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (remove_fetcher_on_delete_)
3022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    fetcher->set_owner(this);
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  fetcher->SetDelegateForTests(delegate_for_tests_);
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  fetchers_[id] = fetcher;
3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return fetcher;
3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TestURLFetcher* TestURLFetcherFactory::GetFetcherByID(int id) const {
3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Fetchers::const_iterator i = fetchers_.find(id);
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return i == fetchers_.end() ? NULL : i->second;
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TestURLFetcherFactory::RemoveFetcherFromMap(int id) {
3145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Fetchers::iterator i = fetchers_.find(id);
3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(i != fetchers_.end());
3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  fetchers_.erase(i);
3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TestURLFetcherFactory::SetDelegateForTests(
3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TestURLFetcherDelegateForTests* delegate_for_tests) {
3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  delegate_for_tests_ = delegate_for_tests;
3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)FakeURLFetcher::FakeURLFetcher(const GURL& url,
3252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                               URLFetcherDelegate* d,
3262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                               const std::string& response_data,
327f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                               HttpStatusCode response_code,
328f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                               URLRequestStatus::Status status)
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : TestURLFetcher(0, url, d),
330c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      weak_factory_(this) {
331f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  Error error = OK;
332f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  switch(status) {
333f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    case URLRequestStatus::SUCCESS:
334f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      // |error| is initialized to OK.
335f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      break;
336f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    case URLRequestStatus::IO_PENDING:
337f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      error = ERR_IO_PENDING;
338f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      break;
339f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    case URLRequestStatus::CANCELED:
340f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      error = ERR_ABORTED;
341f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      break;
342f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    case URLRequestStatus::FAILED:
343f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      error = ERR_FAILED;
344f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      break;
345f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
346f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  set_status(URLRequestStatus(status, error));
3470f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  set_response_code(response_code);
3482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  SetResponseString(response_data);
3492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
3505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)FakeURLFetcher::~FakeURLFetcher() {}
3525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void FakeURLFetcher::Start() {
35490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  base::MessageLoop::current()->PostTask(
3552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      FROM_HERE,
3562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      base::Bind(&FakeURLFetcher::RunDelegate, weak_factory_.GetWeakPtr()));
3572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
3585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void FakeURLFetcher::RunDelegate() {
3602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  delegate()->OnURLFetchComplete(this);
3612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
3625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const GURL& FakeURLFetcher::GetURL() const {
3642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return TestURLFetcher::GetOriginalURL();
3652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
3665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)FakeURLFetcherFactory::FakeURLFetcherFactory(
3682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    URLFetcherFactory* default_factory)
369c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    : ScopedURLFetcherFactory(this),
3702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      creator_(base::Bind(&DefaultFakeURLFetcherCreator)),
3712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      default_factory_(default_factory) {
3725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)FakeURLFetcherFactory::FakeURLFetcherFactory(
3752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    URLFetcherFactory* default_factory,
3762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const FakeURLFetcherCreator& creator)
377c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    : ScopedURLFetcherFactory(this),
3782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      creator_(creator),
3795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      default_factory_(default_factory) {
3805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)scoped_ptr<FakeURLFetcher> FakeURLFetcherFactory::DefaultFakeURLFetcherCreator(
3832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const GURL& url,
3842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      URLFetcherDelegate* delegate,
3850f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      const std::string& response_data,
386f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      HttpStatusCode response_code,
387f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      URLRequestStatus::Status status) {
388f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return scoped_ptr<FakeURLFetcher>(
389f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      new FakeURLFetcher(url, delegate, response_data, response_code, status));
3902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
3912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)FakeURLFetcherFactory::~FakeURLFetcherFactory() {}
3935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)URLFetcher* FakeURLFetcherFactory::CreateURLFetcher(
3955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int id,
3965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const GURL& url,
3975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    URLFetcher::RequestType request_type,
3985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    URLFetcherDelegate* d) {
3995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FakeResponseMap::const_iterator it = fake_responses_.find(url);
4005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (it == fake_responses_.end()) {
4015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (default_factory_ == NULL) {
4025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // If we don't have a baked response for that URL we return NULL.
4035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      DLOG(ERROR) << "No baked response for URL: " << url.spec();
4045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return NULL;
4055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    } else {
4065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return default_factory_->CreateURLFetcher(id, url, request_type, d);
4075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
4085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<FakeURLFetcher> fake_fetcher =
411f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      creator_.Run(url, d, it->second.response_data,
412f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                   it->second.response_code, it->second.status);
4132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // TODO: Make URLFetcherFactory::CreateURLFetcher return a scoped_ptr
4142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return fake_fetcher.release();
4155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4174e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)void FakeURLFetcherFactory::SetFakeResponse(
418d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    const GURL& url,
419d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    const std::string& response_data,
420f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    HttpStatusCode response_code,
421f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    URLRequestStatus::Status status) {
422d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // Overwrite existing URL if it already exists.
423f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  FakeURLResponse response;
424f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  response.response_data = response_data;
425f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  response.response_code = response_code;
426f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  response.status = status;
427f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  fake_responses_[url] = response;
428d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)}
429d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
4305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void FakeURLFetcherFactory::ClearFakeResponses() {
4315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  fake_responses_.clear();
4325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)URLFetcherImplFactory::URLFetcherImplFactory() {}
4355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)URLFetcherImplFactory::~URLFetcherImplFactory() {}
4375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)URLFetcher* URLFetcherImplFactory::CreateURLFetcher(
4395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int id,
4405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const GURL& url,
4415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    URLFetcher::RequestType request_type,
4425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    URLFetcherDelegate* d) {
4435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return new URLFetcherImpl(url, request_type, d);
4445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace net
447