| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/url_request/test_url_fetcher_factory.h" | 5 #include "net/url_request/test_url_fetcher_factory.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 DCHECK(URLFetcherImpl::factory()); | 28 DCHECK(URLFetcherImpl::factory()); |
| 29 URLFetcherImpl::set_factory(NULL); | 29 URLFetcherImpl::set_factory(NULL); |
| 30 } | 30 } |
| 31 | 31 |
| 32 TestURLFetcher::TestURLFetcher(int id, | 32 TestURLFetcher::TestURLFetcher(int id, |
| 33 const GURL& url, | 33 const GURL& url, |
| 34 URLFetcherDelegate* d) | 34 URLFetcherDelegate* d) |
| 35 : id_(id), | 35 : id_(id), |
| 36 original_url_(url), | 36 original_url_(url), |
| 37 delegate_(d), | 37 delegate_(d), |
| 38 delegate_for_tests_(NULL), |
| 38 did_receive_last_chunk_(false), | 39 did_receive_last_chunk_(false), |
| 39 fake_load_flags_(0), | 40 fake_load_flags_(0), |
| 40 fake_response_code_(-1), | 41 fake_response_code_(-1), |
| 41 fake_response_destination_(STRING), | 42 fake_response_destination_(STRING), |
| 42 fake_was_fetched_via_proxy_(false), | 43 fake_was_fetched_via_proxy_(false), |
| 43 fake_max_retries_(0) { | 44 fake_max_retries_(0) { |
| 44 } | 45 } |
| 45 | 46 |
| 46 TestURLFetcher::~TestURLFetcher() { | 47 TestURLFetcher::~TestURLFetcher() { |
| 48 if (delegate_for_tests_) |
| 49 delegate_for_tests_->OnRequestEnd(id_); |
| 47 } | 50 } |
| 48 | 51 |
| 49 void TestURLFetcher::SetUploadData(const std::string& upload_content_type, | 52 void TestURLFetcher::SetUploadData(const std::string& upload_content_type, |
| 50 const std::string& upload_content) { | 53 const std::string& upload_content) { |
| 51 upload_data_ = upload_content; | 54 upload_data_ = upload_content; |
| 52 } | 55 } |
| 53 | 56 |
| 54 void TestURLFetcher::SetChunkedUpload(const std::string& upload_content_type) { | 57 void TestURLFetcher::SetChunkedUpload(const std::string& upload_content_type) { |
| 55 } | 58 } |
| 56 | 59 |
| 57 void TestURLFetcher::AppendChunkToUpload(const std::string& data, | 60 void TestURLFetcher::AppendChunkToUpload(const std::string& data, |
| 58 bool is_last_chunk) { | 61 bool is_last_chunk) { |
| 59 DCHECK(!did_receive_last_chunk_); | 62 DCHECK(!did_receive_last_chunk_); |
| 60 did_receive_last_chunk_ = is_last_chunk; | 63 did_receive_last_chunk_ = is_last_chunk; |
| 61 chunks_.push_back(data); | 64 chunks_.push_back(data); |
| 65 if (delegate_for_tests_) |
| 66 delegate_for_tests_->OnChunkUpload(id_); |
| 62 } | 67 } |
| 63 | 68 |
| 64 void TestURLFetcher::SetLoadFlags(int load_flags) { | 69 void TestURLFetcher::SetLoadFlags(int load_flags) { |
| 65 fake_load_flags_= load_flags; | 70 fake_load_flags_= load_flags; |
| 66 } | 71 } |
| 67 | 72 |
| 68 int TestURLFetcher::GetLoadFlags() const { | 73 int TestURLFetcher::GetLoadFlags() const { |
| 69 return fake_load_flags_; | 74 return fake_load_flags_; |
| 70 } | 75 } |
| 71 | 76 |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 NOTIMPLEMENTED(); | 140 NOTIMPLEMENTED(); |
| 136 return HostPortPair(); | 141 return HostPortPair(); |
| 137 } | 142 } |
| 138 | 143 |
| 139 bool TestURLFetcher::WasFetchedViaProxy() const { | 144 bool TestURLFetcher::WasFetchedViaProxy() const { |
| 140 return fake_was_fetched_via_proxy_; | 145 return fake_was_fetched_via_proxy_; |
| 141 } | 146 } |
| 142 | 147 |
| 143 void TestURLFetcher::Start() { | 148 void TestURLFetcher::Start() { |
| 144 // Overriden to do nothing. It is assumed the caller will notify the delegate. | 149 // Overriden to do nothing. It is assumed the caller will notify the delegate. |
| 150 if (delegate_for_tests_) |
| 151 delegate_for_tests_->OnRequestStart(id_); |
| 145 } | 152 } |
| 146 | 153 |
| 147 const GURL& TestURLFetcher::GetOriginalURL() const { | 154 const GURL& TestURLFetcher::GetOriginalURL() const { |
| 148 return original_url_; | 155 return original_url_; |
| 149 } | 156 } |
| 150 | 157 |
| 151 const GURL& TestURLFetcher::GetURL() const { | 158 const GURL& TestURLFetcher::GetURL() const { |
| 152 return fake_url_; | 159 return fake_url_; |
| 153 } | 160 } |
| 154 | 161 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 | 208 |
| 202 void TestURLFetcher::set_response_headers( | 209 void TestURLFetcher::set_response_headers( |
| 203 scoped_refptr<HttpResponseHeaders> headers) { | 210 scoped_refptr<HttpResponseHeaders> headers) { |
| 204 fake_response_headers_ = headers; | 211 fake_response_headers_ = headers; |
| 205 } | 212 } |
| 206 | 213 |
| 207 void TestURLFetcher::set_backoff_delay(base::TimeDelta backoff_delay) { | 214 void TestURLFetcher::set_backoff_delay(base::TimeDelta backoff_delay) { |
| 208 fake_backoff_delay_ = backoff_delay; | 215 fake_backoff_delay_ = backoff_delay; |
| 209 } | 216 } |
| 210 | 217 |
| 218 void TestURLFetcher::SetDelegateForTests(DelegateForTests* delegate_for_tests) { |
| 219 delegate_for_tests_ = delegate_for_tests; |
| 220 } |
| 221 |
| 211 void TestURLFetcher::SetResponseString(const std::string& response) { | 222 void TestURLFetcher::SetResponseString(const std::string& response) { |
| 212 fake_response_destination_ = STRING; | 223 fake_response_destination_ = STRING; |
| 213 fake_response_string_ = response; | 224 fake_response_string_ = response; |
| 214 } | 225 } |
| 215 | 226 |
| 216 void TestURLFetcher::SetResponseFilePath(const FilePath& path) { | 227 void TestURLFetcher::SetResponseFilePath(const FilePath& path) { |
| 217 fake_response_destination_ = TEMP_FILE; | 228 fake_response_destination_ = TEMP_FILE; |
| 218 fake_response_file_path_ = path; | 229 fake_response_file_path_ = path; |
| 219 } | 230 } |
| 220 | 231 |
| 221 TestURLFetcherFactory::TestURLFetcherFactory() | 232 TestURLFetcherFactory::TestURLFetcherFactory() |
| 222 : ScopedURLFetcherFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | 233 : ScopedURLFetcherFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
| 234 delegate_for_tests_(NULL) { |
| 223 } | 235 } |
| 224 | 236 |
| 225 TestURLFetcherFactory::~TestURLFetcherFactory() {} | 237 TestURLFetcherFactory::~TestURLFetcherFactory() {} |
| 226 | 238 |
| 227 URLFetcher* TestURLFetcherFactory::CreateURLFetcher( | 239 URLFetcher* TestURLFetcherFactory::CreateURLFetcher( |
| 228 int id, | 240 int id, |
| 229 const GURL& url, | 241 const GURL& url, |
| 230 URLFetcher::RequestType request_type, | 242 URLFetcher::RequestType request_type, |
| 231 URLFetcherDelegate* d) { | 243 URLFetcherDelegate* d) { |
| 232 TestURLFetcher* fetcher = new TestURLFetcher(id, url, d); | 244 TestURLFetcher* fetcher = new TestURLFetcher(id, url, d); |
| 245 fetcher->SetDelegateForTests(delegate_for_tests_); |
| 233 fetchers_[id] = fetcher; | 246 fetchers_[id] = fetcher; |
| 234 return fetcher; | 247 return fetcher; |
| 235 } | 248 } |
| 236 | 249 |
| 237 TestURLFetcher* TestURLFetcherFactory::GetFetcherByID(int id) const { | 250 TestURLFetcher* TestURLFetcherFactory::GetFetcherByID(int id) const { |
| 238 Fetchers::const_iterator i = fetchers_.find(id); | 251 Fetchers::const_iterator i = fetchers_.find(id); |
| 239 return i == fetchers_.end() ? NULL : i->second; | 252 return i == fetchers_.end() ? NULL : i->second; |
| 240 } | 253 } |
| 241 | 254 |
| 242 void TestURLFetcherFactory::RemoveFetcherFromMap(int id) { | 255 void TestURLFetcherFactory::RemoveFetcherFromMap(int id) { |
| 243 Fetchers::iterator i = fetchers_.find(id); | 256 Fetchers::iterator i = fetchers_.find(id); |
| 244 DCHECK(i != fetchers_.end()); | 257 DCHECK(i != fetchers_.end()); |
| 245 fetchers_.erase(i); | 258 fetchers_.erase(i); |
| 246 } | 259 } |
| 247 | 260 |
| 261 void TestURLFetcherFactory::SetDelegateForTests( |
| 262 TestURLFetcherDelegateForTests* delegate_for_tests) { |
| 263 delegate_for_tests_ = delegate_for_tests; |
| 264 } |
| 265 |
| 248 // This class is used by the FakeURLFetcherFactory below. | 266 // This class is used by the FakeURLFetcherFactory below. |
| 249 class FakeURLFetcher : public TestURLFetcher { | 267 class FakeURLFetcher : public TestURLFetcher { |
| 250 public: | 268 public: |
| 251 // Normal URL fetcher constructor but also takes in a pre-baked response. | 269 // Normal URL fetcher constructor but also takes in a pre-baked response. |
| 252 FakeURLFetcher(const GURL& url, | 270 FakeURLFetcher(const GURL& url, |
| 253 URLFetcherDelegate* d, | 271 URLFetcherDelegate* d, |
| 254 const std::string& response_data, bool success) | 272 const std::string& response_data, bool success) |
| 255 : TestURLFetcher(0, url, d), | 273 : TestURLFetcher(0, url, d), |
| 256 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | 274 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
| 257 set_status(URLRequestStatus( | 275 set_status(URLRequestStatus( |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 337 | 355 |
| 338 URLFetcher* URLFetcherImplFactory::CreateURLFetcher( | 356 URLFetcher* URLFetcherImplFactory::CreateURLFetcher( |
| 339 int id, | 357 int id, |
| 340 const GURL& url, | 358 const GURL& url, |
| 341 URLFetcher::RequestType request_type, | 359 URLFetcher::RequestType request_type, |
| 342 URLFetcherDelegate* d) { | 360 URLFetcherDelegate* d) { |
| 343 return new URLFetcherImpl(url, request_type, d); | 361 return new URLFetcherImpl(url, request_type, d); |
| 344 } | 362 } |
| 345 | 363 |
| 346 } // namespace net | 364 } // namespace net |
| OLD | NEW |