| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/compiler_specific.h" | |
| 10 #include "base/message_loop.h" | |
| 11 #include "net/url_request/url_request_status.h" | |
| 12 | |
| 13 TestURLFetcher::TestURLFetcher(int id, | |
| 14 const GURL& url, | |
| 15 URLFetcher::RequestType request_type, | |
| 16 URLFetcher::Delegate* d) | |
| 17 : URLFetcher(url, request_type, d), | |
| 18 id_(id), | |
| 19 original_url_(url), | |
| 20 did_receive_last_chunk_(false) { | |
| 21 } | |
| 22 | |
| 23 TestURLFetcher::~TestURLFetcher() { | |
| 24 } | |
| 25 | |
| 26 void TestURLFetcher::AppendChunkToUpload(const std::string& data, | |
| 27 bool is_last_chunk) { | |
| 28 DCHECK(!did_receive_last_chunk_); | |
| 29 did_receive_last_chunk_ = is_last_chunk; | |
| 30 chunks_.push_back(data); | |
| 31 } | |
| 32 | |
| 33 void TestURLFetcher::set_status(const net::URLRequestStatus& status) { | |
| 34 fake_status_ = status; | |
| 35 } | |
| 36 | |
| 37 void TestURLFetcher::SetResponseString(const std::string& response) { | |
| 38 SetResponseDestinationForTesting(STRING); | |
| 39 fake_response_string_ = response; | |
| 40 } | |
| 41 | |
| 42 void TestURLFetcher::SetResponseFilePath(const FilePath& path) { | |
| 43 SetResponseDestinationForTesting(TEMP_FILE); | |
| 44 fake_response_file_path_ = path; | |
| 45 } | |
| 46 | |
| 47 bool TestURLFetcher::GetResponseAsString( | |
| 48 std::string* out_response_string) const { | |
| 49 if (GetResponseDestinationForTesting() != STRING) | |
| 50 return false; | |
| 51 | |
| 52 *out_response_string = fake_response_string_; | |
| 53 return true; | |
| 54 } | |
| 55 | |
| 56 bool TestURLFetcher::GetResponseAsFilePath( | |
| 57 bool take_ownership, FilePath* out_response_path) const { | |
| 58 if (GetResponseDestinationForTesting() != TEMP_FILE) | |
| 59 return false; | |
| 60 | |
| 61 *out_response_path = fake_response_file_path_; | |
| 62 return true; | |
| 63 } | |
| 64 | |
| 65 TestURLFetcherFactory::TestURLFetcherFactory() {} | |
| 66 | |
| 67 TestURLFetcherFactory::~TestURLFetcherFactory() {} | |
| 68 | |
| 69 URLFetcher* TestURLFetcherFactory::CreateURLFetcher( | |
| 70 int id, | |
| 71 const GURL& url, | |
| 72 URLFetcher::RequestType request_type, | |
| 73 URLFetcher::Delegate* d) { | |
| 74 TestURLFetcher* fetcher = new TestURLFetcher(id, url, request_type, d); | |
| 75 fetchers_[id] = fetcher; | |
| 76 return fetcher; | |
| 77 } | |
| 78 | |
| 79 TestURLFetcher* TestURLFetcherFactory::GetFetcherByID(int id) const { | |
| 80 Fetchers::const_iterator i = fetchers_.find(id); | |
| 81 return i == fetchers_.end() ? NULL : i->second; | |
| 82 } | |
| 83 | |
| 84 void TestURLFetcherFactory::RemoveFetcherFromMap(int id) { | |
| 85 Fetchers::iterator i = fetchers_.find(id); | |
| 86 DCHECK(i != fetchers_.end()); | |
| 87 fetchers_.erase(i); | |
| 88 } | |
| 89 | |
| 90 const GURL& TestURLFetcher::url() const { | |
| 91 return fake_url_; | |
| 92 } | |
| 93 | |
| 94 const net::URLRequestStatus& TestURLFetcher::status() const { | |
| 95 return fake_status_; | |
| 96 } | |
| 97 | |
| 98 int TestURLFetcher::response_code() const { | |
| 99 return fake_response_code_; | |
| 100 } | |
| 101 | |
| 102 // This class is used by the FakeURLFetcherFactory below. | |
| 103 class FakeURLFetcher : public URLFetcher { | |
| 104 public: | |
| 105 // Normal URL fetcher constructor but also takes in a pre-baked response. | |
| 106 FakeURLFetcher(const GURL& url, RequestType request_type, Delegate* d, | |
| 107 const std::string& response_data, bool success) | |
| 108 : URLFetcher(url, request_type, d), | |
| 109 url_(url), | |
| 110 response_data_(response_data), | |
| 111 success_(success), | |
| 112 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { | |
| 113 } | |
| 114 | |
| 115 // Start the request. This will call the given delegate asynchronously | |
| 116 // with the pre-baked response as parameter. | |
| 117 virtual void Start() { | |
| 118 MessageLoop::current()->PostTask( | |
| 119 FROM_HERE, | |
| 120 method_factory_.NewRunnableMethod(&FakeURLFetcher::RunDelegate)); | |
| 121 } | |
| 122 | |
| 123 private: | |
| 124 virtual ~FakeURLFetcher() { | |
| 125 } | |
| 126 | |
| 127 // This is the method which actually calls the delegate that is passed in the | |
| 128 // constructor. | |
| 129 void RunDelegate() { | |
| 130 net::URLRequestStatus status; | |
| 131 status.set_status(success_ ? net::URLRequestStatus::SUCCESS : | |
| 132 net::URLRequestStatus::FAILED); | |
| 133 delegate()->OnURLFetchComplete(this, url_, status, success_ ? 200 : 500, | |
| 134 net::ResponseCookies(), response_data_); | |
| 135 } | |
| 136 | |
| 137 // Pre-baked response data and flag which indicates whether the request should | |
| 138 // be successful or not. | |
| 139 GURL url_; | |
| 140 std::string response_data_; | |
| 141 bool success_; | |
| 142 | |
| 143 // Method factory used to run the delegate. | |
| 144 ScopedRunnableMethodFactory<FakeURLFetcher> method_factory_; | |
| 145 | |
| 146 DISALLOW_COPY_AND_ASSIGN(FakeURLFetcher); | |
| 147 }; | |
| 148 | |
| 149 FakeURLFetcherFactory::FakeURLFetcherFactory() {} | |
| 150 | |
| 151 FakeURLFetcherFactory::FakeURLFetcherFactory( | |
| 152 URLFetcher::Factory* default_factory) : default_factory_(default_factory) {} | |
| 153 | |
| 154 FakeURLFetcherFactory::~FakeURLFetcherFactory() {} | |
| 155 | |
| 156 URLFetcher* FakeURLFetcherFactory::CreateURLFetcher( | |
| 157 int id, | |
| 158 const GURL& url, | |
| 159 URLFetcher::RequestType request_type, | |
| 160 URLFetcher::Delegate* d) { | |
| 161 FakeResponseMap::const_iterator it = fake_responses_.find(url); | |
| 162 if (it == fake_responses_.end()) { | |
| 163 if (default_factory_ == NULL) { | |
| 164 // If we don't have a baked response for that URL we return NULL. | |
| 165 DLOG(ERROR) << "No baked response for URL: " << url.spec(); | |
| 166 return NULL; | |
| 167 } else { | |
| 168 return default_factory_->CreateURLFetcher(id, url, request_type, d); | |
| 169 } | |
| 170 } | |
| 171 return new FakeURLFetcher(url, request_type, d, | |
| 172 it->second.first, it->second.second); | |
| 173 } | |
| 174 | |
| 175 void FakeURLFetcherFactory::SetFakeResponse(const std::string& url, | |
| 176 const std::string& response_data, | |
| 177 bool success) { | |
| 178 // Overwrite existing URL if it already exists. | |
| 179 fake_responses_[GURL(url)] = std::make_pair(response_data, success); | |
| 180 } | |
| 181 | |
| 182 void FakeURLFetcherFactory::ClearFakeReponses() { | |
| 183 fake_responses_.clear(); | |
| 184 } | |
| 185 | |
| 186 URLFetcherFactory::URLFetcherFactory() {} | |
| 187 | |
| 188 URLFetcherFactory::~URLFetcherFactory() {} | |
| 189 | |
| 190 URLFetcher* URLFetcherFactory::CreateURLFetcher( | |
| 191 int id, | |
| 192 const GURL& url, | |
| 193 URLFetcher::RequestType request_type, | |
| 194 URLFetcher::Delegate* d) { | |
| 195 return new URLFetcher(url, request_type, d); | |
| 196 } | |
| OLD | NEW |