| 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. | |
| 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, | |
| 288 const std::string& response_data, bool success) | |
| 289 : TestURLFetcher(0, url, d), | |
| 290 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | |
| 291 set_status(URLRequestStatus( | |
| 292 success ? URLRequestStatus::SUCCESS : URLRequestStatus::FAILED, | |
| 293 0)); | |
| 294 set_response_code(success ? 200 : 500); | |
| 295 SetResponseString(response_data); | |
| 296 } | |
| 297 | |
| 298 // Start the request. This will call the given delegate asynchronously | |
| 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 | |
| 306 virtual const GURL& GetURL() const OVERRIDE { | |
| 307 return TestURLFetcher::GetOriginalURL(); | |
| 308 } | |
| 309 | |
| 310 private: | |
| 311 virtual ~FakeURLFetcher() { | |
| 312 } | |
| 313 | |
| 314 // This is the method which actually calls the delegate that is passed in the | |
| 315 // constructor. | |
| 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() | 282 FakeURLFetcherFactory::FakeURLFetcherFactory() |
| 326 : ScopedURLFetcherFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)), | 283 : ScopedURLFetcherFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
| 284 creator_(base::Bind(&DefaultFakeURLFetcherCreator)), |
| 327 default_factory_(NULL) { | 285 default_factory_(NULL) { |
| 328 } | 286 } |
| 329 | 287 |
| 330 FakeURLFetcherFactory::FakeURLFetcherFactory( | 288 FakeURLFetcherFactory::FakeURLFetcherFactory( |
| 331 URLFetcherFactory* default_factory) | 289 URLFetcherFactory* default_factory) |
| 332 : ScopedURLFetcherFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)), | 290 : ScopedURLFetcherFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
| 291 creator_(base::Bind(&DefaultFakeURLFetcherCreator)), |
| 333 default_factory_(default_factory) { | 292 default_factory_(default_factory) { |
| 334 } | 293 } |
| 335 | 294 |
| 295 FakeURLFetcherFactory::FakeURLFetcherFactory( |
| 296 const FakeURLFetcherCreator& creator, |
| 297 URLFetcherFactory* default_factory) |
| 298 : ScopedURLFetcherFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
| 299 creator_(creator), |
| 300 default_factory_(default_factory) { |
| 301 } |
| 302 |
| 303 FakeURLFetcher* FakeURLFetcherFactory::DefaultFakeURLFetcherCreator( |
| 304 const GURL& url, |
| 305 URLFetcherDelegate* delegate, |
| 306 const std::string& response, |
| 307 bool success) { |
| 308 return new FakeURLFetcher(url, delegate, response, success); |
| 309 } |
| 310 |
| 336 FakeURLFetcherFactory::~FakeURLFetcherFactory() {} | 311 FakeURLFetcherFactory::~FakeURLFetcherFactory() {} |
| 337 | 312 |
| 338 URLFetcher* FakeURLFetcherFactory::CreateURLFetcher( | 313 URLFetcher* FakeURLFetcherFactory::CreateURLFetcher( |
| 339 int id, | 314 int id, |
| 340 const GURL& url, | 315 const GURL& url, |
| 341 URLFetcher::RequestType request_type, | 316 URLFetcher::RequestType request_type, |
| 342 URLFetcherDelegate* d) { | 317 URLFetcherDelegate* d) { |
| 343 FakeResponseMap::const_iterator it = fake_responses_.find(url); | 318 FakeResponseMap::const_iterator it = fake_responses_.find(url); |
| 344 if (it == fake_responses_.end()) { | 319 if (it == fake_responses_.end()) { |
| 345 if (default_factory_ == NULL) { | 320 if (default_factory_ == NULL) { |
| 346 // If we don't have a baked response for that URL we return NULL. | 321 // If we don't have a baked response for that URL we return NULL. |
| 347 DLOG(ERROR) << "No baked response for URL: " << url.spec(); | 322 DLOG(ERROR) << "No baked response for URL: " << url.spec(); |
| 348 return NULL; | 323 return NULL; |
| 349 } else { | 324 } else { |
| 350 return default_factory_->CreateURLFetcher(id, url, request_type, d); | 325 return default_factory_->CreateURLFetcher(id, url, request_type, d); |
| 351 } | 326 } |
| 352 } | 327 } |
| 353 return new FakeURLFetcher(url, d, it->second.first, it->second.second); | 328 return creator_.Run(url, d, it->second.first, it->second.second); |
| 354 } | 329 } |
| 355 | 330 |
| 356 void FakeURLFetcherFactory::SetFakeResponse(const std::string& url, | 331 void FakeURLFetcherFactory::SetFakeResponse(const std::string& url, |
| 357 const std::string& response_data, | 332 const std::string& response_data, |
| 358 bool success) { | 333 bool success) { |
| 359 // Overwrite existing URL if it already exists. | 334 // Overwrite existing URL if it already exists. |
| 360 fake_responses_[GURL(url)] = std::make_pair(response_data, success); | 335 fake_responses_[GURL(url)] = std::make_pair(response_data, success); |
| 361 } | 336 } |
| 362 | 337 |
| 363 void FakeURLFetcherFactory::ClearFakeResponses() { | 338 void FakeURLFetcherFactory::ClearFakeResponses() { |
| 364 fake_responses_.clear(); | 339 fake_responses_.clear(); |
| 365 } | 340 } |
| 366 | 341 |
| 342 |
| 343 |
| 344 FakeURLFetcher::FakeURLFetcher(const GURL& url, |
| 345 URLFetcherDelegate* d, |
| 346 const std::string& response_data, bool success) |
| 347 : TestURLFetcher(0, url, d), |
| 348 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
| 349 set_status(URLRequestStatus( |
| 350 success ? URLRequestStatus::SUCCESS : URLRequestStatus::FAILED, |
| 351 0)); |
| 352 set_response_code(success ? 200 : 500); |
| 353 SetResponseString(response_data); |
| 354 } |
| 355 |
| 356 void FakeURLFetcher::Start() { |
| 357 MessageLoop::current()->PostTask( |
| 358 FROM_HERE, |
| 359 base::Bind(&FakeURLFetcher::RunDelegate, weak_factory_.GetWeakPtr())); |
| 360 } |
| 361 |
| 362 FakeURLFetcher::~FakeURLFetcher() {} |
| 363 |
| 364 void FakeURLFetcher::RunDelegate() { |
| 365 delegate()->OnURLFetchComplete(this); |
| 366 } |
| 367 |
| 368 |
| 369 const GURL& FakeURLFetcher::GetURL() const { |
| 370 return TestURLFetcher::GetOriginalURL(); |
| 371 } |
| 372 |
| 367 URLFetcherImplFactory::URLFetcherImplFactory() {} | 373 URLFetcherImplFactory::URLFetcherImplFactory() {} |
| 368 | 374 |
| 369 URLFetcherImplFactory::~URLFetcherImplFactory() {} | 375 URLFetcherImplFactory::~URLFetcherImplFactory() {} |
| 370 | 376 |
| 371 URLFetcher* URLFetcherImplFactory::CreateURLFetcher( | 377 URLFetcher* URLFetcherImplFactory::CreateURLFetcher( |
| 372 int id, | 378 int id, |
| 373 const GURL& url, | 379 const GURL& url, |
| 374 URLFetcher::RequestType request_type, | 380 URLFetcher::RequestType request_type, |
| 375 URLFetcherDelegate* d) { | 381 URLFetcherDelegate* d) { |
| 376 return new URLFetcherImpl(url, request_type, d); | 382 return new URLFetcherImpl(url, request_type, d); |
| 377 } | 383 } |
| 378 | 384 |
| 379 } // namespace net | 385 } // namespace net |
| OLD | NEW |