Chromium Code Reviews| 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 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 272 Fetchers::iterator i = fetchers_.find(id); | 272 Fetchers::iterator i = fetchers_.find(id); |
| 273 DCHECK(i != fetchers_.end()); | 273 DCHECK(i != fetchers_.end()); |
| 274 fetchers_.erase(i); | 274 fetchers_.erase(i); |
| 275 } | 275 } |
| 276 | 276 |
| 277 void TestURLFetcherFactory::SetDelegateForTests( | 277 void TestURLFetcherFactory::SetDelegateForTests( |
| 278 TestURLFetcherDelegateForTests* delegate_for_tests) { | 278 TestURLFetcherDelegateForTests* delegate_for_tests) { |
| 279 delegate_for_tests_ = delegate_for_tests; | 279 delegate_for_tests_ = delegate_for_tests; |
| 280 } | 280 } |
| 281 | 281 |
| 282 // This class is used by the FakeURLFetcherFactory below. | 282 FakeURLFetcher::FakeURLFetcher(const GURL& url, |
| 283 class FakeURLFetcher : public TestURLFetcher { | |
| 284 public: | |
| 285 // Normal URL fetcher constructor but also takes in a pre-baked response. | |
| 286 FakeURLFetcher(const GURL& url, | |
| 287 URLFetcherDelegate* d, | 283 URLFetcherDelegate* d, |
|
akalin
2013/02/13 22:17:49
indentation
Noam Samuel (WRONG ACCOUNT)
2013/02/13 23:35:11
Done.
| |
| 288 const std::string& response_data, bool success) | 284 const std::string& response_data, bool success) |
| 289 : TestURLFetcher(0, url, d), | 285 : TestURLFetcher(0, url, d), |
| 290 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | 286 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
| 291 set_status(URLRequestStatus( | 287 set_status(URLRequestStatus( |
| 292 success ? URLRequestStatus::SUCCESS : URLRequestStatus::FAILED, | 288 success ? URLRequestStatus::SUCCESS : URLRequestStatus::FAILED, |
| 293 0)); | 289 0)); |
| 294 set_response_code(success ? 200 : 500); | 290 set_response_code(success ? 200 : 500); |
| 295 SetResponseString(response_data); | 291 SetResponseString(response_data); |
| 296 } | 292 } |
| 297 | 293 |
| 298 // Start the request. This will call the given delegate asynchronously | 294 FakeURLFetcher::~FakeURLFetcher() {} |
| 299 // with the pre-baked response as parameter. | |
| 300 virtual void Start() OVERRIDE { | |
| 301 MessageLoop::current()->PostTask( | |
| 302 FROM_HERE, | |
| 303 base::Bind(&FakeURLFetcher::RunDelegate, weak_factory_.GetWeakPtr())); | |
| 304 } | |
| 305 | 295 |
| 306 virtual const GURL& GetURL() const OVERRIDE { | 296 void FakeURLFetcher::Start() { |
| 307 return TestURLFetcher::GetOriginalURL(); | 297 MessageLoop::current()->PostTask( |
| 308 } | 298 FROM_HERE, |
| 299 base::Bind(&FakeURLFetcher::RunDelegate, weak_factory_.GetWeakPtr())); | |
| 300 } | |
| 309 | 301 |
| 310 private: | 302 void FakeURLFetcher::RunDelegate() { |
| 311 virtual ~FakeURLFetcher() { | 303 delegate()->OnURLFetchComplete(this); |
| 312 } | 304 } |
| 313 | 305 |
| 314 // This is the method which actually calls the delegate that is passed in the | 306 const GURL& FakeURLFetcher::GetURL() const { |
| 315 // constructor. | 307 return TestURLFetcher::GetOriginalURL(); |
| 316 void RunDelegate() { | |
| 317 delegate()->OnURLFetchComplete(this); | |
| 318 } | |
| 319 | |
| 320 base::WeakPtrFactory<FakeURLFetcher> weak_factory_; | |
| 321 | |
| 322 DISALLOW_COPY_AND_ASSIGN(FakeURLFetcher); | |
| 323 }; | |
| 324 | |
| 325 FakeURLFetcherFactory::FakeURLFetcherFactory() | |
| 326 : ScopedURLFetcherFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)), | |
| 327 default_factory_(NULL) { | |
| 328 } | 308 } |
| 329 | 309 |
| 330 FakeURLFetcherFactory::FakeURLFetcherFactory( | 310 FakeURLFetcherFactory::FakeURLFetcherFactory( |
| 331 URLFetcherFactory* default_factory) | 311 URLFetcherFactory* default_factory) |
| 332 : ScopedURLFetcherFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)), | 312 : ScopedURLFetcherFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
| 313 creator_(base::Bind(&DefaultFakeURLFetcherCreator)), | |
| 333 default_factory_(default_factory) { | 314 default_factory_(default_factory) { |
| 334 } | 315 } |
| 335 | 316 |
| 317 FakeURLFetcherFactory::FakeURLFetcherFactory( | |
| 318 const FakeURLFetcherCreator& creator, | |
| 319 URLFetcherFactory* default_factory) | |
| 320 : ScopedURLFetcherFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)), | |
| 321 creator_(creator), | |
| 322 default_factory_(default_factory) { | |
| 323 } | |
| 324 | |
| 325 FakeURLFetcher* FakeURLFetcherFactory::DefaultFakeURLFetcherCreator( | |
| 326 const GURL& url, | |
| 327 URLFetcherDelegate* delegate, | |
| 328 const std::string& response, | |
| 329 bool success) { | |
| 330 return new FakeURLFetcher(url, delegate, response, success); | |
| 331 } | |
| 332 | |
| 336 FakeURLFetcherFactory::~FakeURLFetcherFactory() {} | 333 FakeURLFetcherFactory::~FakeURLFetcherFactory() {} |
| 337 | 334 |
| 338 URLFetcher* FakeURLFetcherFactory::CreateURLFetcher( | 335 URLFetcher* FakeURLFetcherFactory::CreateURLFetcher( |
| 339 int id, | 336 int id, |
| 340 const GURL& url, | 337 const GURL& url, |
| 341 URLFetcher::RequestType request_type, | 338 URLFetcher::RequestType request_type, |
| 342 URLFetcherDelegate* d) { | 339 URLFetcherDelegate* d) { |
| 343 FakeResponseMap::const_iterator it = fake_responses_.find(url); | 340 FakeResponseMap::const_iterator it = fake_responses_.find(url); |
| 344 if (it == fake_responses_.end()) { | 341 if (it == fake_responses_.end()) { |
| 345 if (default_factory_ == NULL) { | 342 if (default_factory_ == NULL) { |
| 346 // If we don't have a baked response for that URL we return NULL. | 343 // If we don't have a baked response for that URL we return NULL. |
| 347 DLOG(ERROR) << "No baked response for URL: " << url.spec(); | 344 DLOG(ERROR) << "No baked response for URL: " << url.spec(); |
| 348 return NULL; | 345 return NULL; |
| 349 } else { | 346 } else { |
| 350 return default_factory_->CreateURLFetcher(id, url, request_type, d); | 347 return default_factory_->CreateURLFetcher(id, url, request_type, d); |
| 351 } | 348 } |
| 352 } | 349 } |
| 353 return new FakeURLFetcher(url, d, it->second.first, it->second.second); | 350 return creator_.Run(url, d, it->second.first, it->second.second); |
| 354 } | 351 } |
| 355 | 352 |
| 356 void FakeURLFetcherFactory::SetFakeResponse(const std::string& url, | 353 void FakeURLFetcherFactory::SetFakeResponse(const std::string& url, |
| 357 const std::string& response_data, | 354 const std::string& response_data, |
| 358 bool success) { | 355 bool success) { |
| 359 // Overwrite existing URL if it already exists. | 356 // Overwrite existing URL if it already exists. |
| 360 fake_responses_[GURL(url)] = std::make_pair(response_data, success); | 357 fake_responses_[GURL(url)] = std::make_pair(response_data, success); |
| 361 } | 358 } |
| 362 | 359 |
| 363 void FakeURLFetcherFactory::ClearFakeResponses() { | 360 void FakeURLFetcherFactory::ClearFakeResponses() { |
| 364 fake_responses_.clear(); | 361 fake_responses_.clear(); |
| 365 } | 362 } |
| 366 | 363 |
| 367 URLFetcherImplFactory::URLFetcherImplFactory() {} | 364 URLFetcherImplFactory::URLFetcherImplFactory() {} |
| 368 | 365 |
| 369 URLFetcherImplFactory::~URLFetcherImplFactory() {} | 366 URLFetcherImplFactory::~URLFetcherImplFactory() {} |
| 370 | 367 |
| 371 URLFetcher* URLFetcherImplFactory::CreateURLFetcher( | 368 URLFetcher* URLFetcherImplFactory::CreateURLFetcher( |
| 372 int id, | 369 int id, |
| 373 const GURL& url, | 370 const GURL& url, |
| 374 URLFetcher::RequestType request_type, | 371 URLFetcher::RequestType request_type, |
| 375 URLFetcherDelegate* d) { | 372 URLFetcherDelegate* d) { |
| 376 return new URLFetcherImpl(url, request_type, d); | 373 return new URLFetcherImpl(url, request_type, d); |
| 377 } | 374 } |
| 378 | 375 |
| 379 } // namespace net | 376 } // namespace net |
| OLD | NEW |