test_url_fetcher_factory.cc revision 4a5e2dc747d50c653511c68ccb2cfbfb740bd5a7
1// Copyright (c) 2010 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 "chrome/common/net/test_url_fetcher_factory.h"
6#include <string>
7
8#include "base/message_loop.h"
9#include "net/url_request/url_request_status.h"
10
11TestURLFetcher::TestURLFetcher(int id,
12                               const GURL& url,
13                               URLFetcher::RequestType request_type,
14                               URLFetcher::Delegate* d)
15    : URLFetcher(url, request_type, d),
16      id_(id),
17      original_url_(url) {
18}
19
20URLFetcher* TestURLFetcherFactory::CreateURLFetcher(
21    int id,
22    const GURL& url,
23    URLFetcher::RequestType request_type,
24    URLFetcher::Delegate* d) {
25  TestURLFetcher* fetcher = new TestURLFetcher(id, url, request_type, d);
26  fetchers_[id] = fetcher;
27  return fetcher;
28}
29
30TestURLFetcher* TestURLFetcherFactory::GetFetcherByID(int id) const {
31  Fetchers::const_iterator i = fetchers_.find(id);
32  return i == fetchers_.end() ? NULL : i->second;
33}
34
35void TestURLFetcherFactory::RemoveFetcherFromMap(int id) {
36  Fetchers::iterator i = fetchers_.find(id);
37  DCHECK(i != fetchers_.end());
38  fetchers_.erase(i);
39}
40
41// This class is used by the FakeURLFetcherFactory below.
42class FakeURLFetcher : public URLFetcher {
43 public:
44  // Normal URL fetcher constructor but also takes in a pre-baked response.
45  FakeURLFetcher(const GURL& url, RequestType request_type, Delegate* d,
46                 const std::string& response_data, bool success)
47    : URLFetcher(url, request_type, d),
48      url_(url),
49      response_data_(response_data),
50      success_(success),
51      ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) {
52  }
53
54  // Start the request.  This will call the given delegate asynchronously
55  // with the pre-baked response as parameter.
56  virtual void Start() {
57    MessageLoop::current()->PostTask(
58        FROM_HERE,
59        method_factory_.NewRunnableMethod(&FakeURLFetcher::RunDelegate));
60  }
61
62 private:
63  virtual ~FakeURLFetcher() {
64  }
65
66  // This is the method which actually calls the delegate that is passed in the
67  // constructor.
68  void RunDelegate() {
69    URLRequestStatus status;
70    status.set_status(success_ ? URLRequestStatus::SUCCESS :
71                                 URLRequestStatus::FAILED);
72    delegate()->OnURLFetchComplete(this, url_, status, success_ ? 200 : 500,
73                                   ResponseCookies(), response_data_);
74  }
75
76  // Pre-baked response data and flag which indicates whether the request should
77  // be successful or not.
78  GURL url_;
79  std::string response_data_;
80  bool success_;
81
82  // Method factory used to run the delegate.
83  ScopedRunnableMethodFactory<FakeURLFetcher> method_factory_;
84
85  DISALLOW_COPY_AND_ASSIGN(FakeURLFetcher);
86};
87
88URLFetcher* FakeURLFetcherFactory::CreateURLFetcher(
89    int id,
90    const GURL& url,
91    URLFetcher::RequestType request_type,
92    URLFetcher::Delegate* d) {
93  FakeResponseMap::const_iterator it = fake_responses_.find(url);
94  if (it == fake_responses_.end()) {
95    // If we don't have a baked response for that URL we return NULL.
96    DLOG(ERROR) << "No baked response for URL: " << url.spec();
97    return NULL;
98  }
99  return new FakeURLFetcher(url, request_type, d,
100                            it->second.first, it->second.second);
101}
102
103void FakeURLFetcherFactory::SetFakeResponse(const std::string& url,
104                                            const std::string& response_data,
105                                            bool success) {
106  // Overwrite existing URL if it already exists.
107  fake_responses_[GURL(url)] = std::make_pair(response_data, success);
108}
109
110void FakeURLFetcherFactory::ClearFakeReponses() {
111  fake_responses_.clear();
112}
113