| 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 #ifndef NET_URL_REQUEST_TEST_URL_FETCHER_FACTORY_H_ | 5 #ifndef NET_URL_REQUEST_TEST_URL_FETCHER_FACTORY_H_ |
| 6 #define NET_URL_REQUEST_TEST_URL_FETCHER_FACTORY_H_ | 6 #define NET_URL_REQUEST_TEST_URL_FETCHER_FACTORY_H_ |
| 7 | 7 |
| 8 #include <list> | 8 #include <list> |
| 9 #include <map> | 9 #include <map> |
| 10 #include <string> | 10 #include <string> |
| 11 #include <utility> | 11 #include <utility> |
| 12 | 12 |
| 13 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
| 14 #include "base/callback.h" | 14 #include "base/callback.h" |
| 15 #include "base/compiler_specific.h" | 15 #include "base/compiler_specific.h" |
| 16 #include "base/files/file_path.h" | 16 #include "base/files/file_path.h" |
| 17 #include "base/memory/scoped_ptr.h" | 17 #include "base/memory/scoped_ptr.h" |
| 18 #include "base/memory/weak_ptr.h" | 18 #include "base/memory/weak_ptr.h" |
| 19 #include "base/threading/non_thread_safe.h" | 19 #include "base/threading/non_thread_safe.h" |
| 20 #include "net/http/http_request_headers.h" | 20 #include "net/http/http_request_headers.h" |
| 21 #include "net/http/http_status_code.h" |
| 21 #include "net/url_request/url_fetcher_factory.h" | 22 #include "net/url_request/url_fetcher_factory.h" |
| 22 #include "net/url_request/url_request_status.h" | 23 #include "net/url_request/url_request_status.h" |
| 23 #include "url/gurl.h" | 24 #include "url/gurl.h" |
| 24 | 25 |
| 25 namespace net { | 26 namespace net { |
| 26 | 27 |
| 27 // Changes URLFetcher's Factory for the lifetime of the object. | 28 // Changes URLFetcher's Factory for the lifetime of the object. |
| 28 // Note that this scoper cannot be nested (to make it even harder to misuse). | 29 // Note that this scoper cannot be nested (to make it even harder to misuse). |
| 29 class ScopedURLFetcherFactory : public base::NonThreadSafe { | 30 class ScopedURLFetcherFactory : public base::NonThreadSafe { |
| 30 public: | 31 public: |
| (...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 // We assume that the thread that is calling Start() on the URLFetcher object | 270 // We assume that the thread that is calling Start() on the URLFetcher object |
| 270 // has a message loop running. | 271 // has a message loop running. |
| 271 | 272 |
| 272 // FakeURLFetcher can be used to create a URLFetcher that will emit a fake | 273 // FakeURLFetcher can be used to create a URLFetcher that will emit a fake |
| 273 // response when started. This class can be used in place of an actual | 274 // response when started. This class can be used in place of an actual |
| 274 // URLFetcher. | 275 // URLFetcher. |
| 275 // | 276 // |
| 276 // Example usage: | 277 // Example usage: |
| 277 // FakeURLFetcher fake_fetcher("http://a.com", some_delegate, | 278 // FakeURLFetcher fake_fetcher("http://a.com", some_delegate, |
| 278 // "<html><body>hello world</body></html>", | 279 // "<html><body>hello world</body></html>", |
| 279 // true); | 280 // HTTP_OK); |
| 280 // | 281 // |
| 281 // // Will schedule a call to some_delegate->OnURLFetchComplete(&fake_fetcher). | 282 // // Will schedule a call to some_delegate->OnURLFetchComplete(&fake_fetcher). |
| 282 // fake_fetcher.Start(); | 283 // fake_fetcher.Start(); |
| 283 class FakeURLFetcher : public TestURLFetcher { | 284 class FakeURLFetcher : public TestURLFetcher { |
| 284 public: | 285 public: |
| 285 // Normal URL fetcher constructor but also takes in a pre-baked response. | 286 // Normal URL fetcher constructor but also takes in a pre-baked response. |
| 286 FakeURLFetcher(const GURL& url, | 287 FakeURLFetcher(const GURL& url, |
| 287 URLFetcherDelegate* d, | 288 URLFetcherDelegate* d, |
| 288 const std::string& response_data, bool success); | 289 const std::string& response_data, |
| 290 HttpStatusCode response_code); |
| 289 | 291 |
| 290 // Start the request. This will call the given delegate asynchronously | 292 // Start the request. This will call the given delegate asynchronously |
| 291 // with the pre-baked response as parameter. | 293 // with the pre-baked response as parameter. |
| 292 virtual void Start() OVERRIDE; | 294 virtual void Start() OVERRIDE; |
| 293 | 295 |
| 294 virtual const GURL& GetURL() const OVERRIDE; | 296 virtual const GURL& GetURL() const OVERRIDE; |
| 295 | 297 |
| 296 virtual ~FakeURLFetcher(); | 298 virtual ~FakeURLFetcher(); |
| 297 | 299 |
| 298 private: | 300 private: |
| 299 // This is the method which actually calls the delegate that is passed in the | 301 // This is the method which actually calls the delegate that is passed in the |
| 300 // constructor. | 302 // constructor. |
| 301 void RunDelegate(); | 303 void RunDelegate(); |
| 302 | 304 |
| 303 base::WeakPtrFactory<FakeURLFetcher> weak_factory_; | 305 base::WeakPtrFactory<FakeURLFetcher> weak_factory_; |
| 304 | 306 |
| 305 DISALLOW_COPY_AND_ASSIGN(FakeURLFetcher); | 307 DISALLOW_COPY_AND_ASSIGN(FakeURLFetcher); |
| 306 }; | 308 }; |
| 307 | 309 |
| 308 | 310 |
| 309 // FakeURLFetcherFactory is a factory for FakeURLFetcher objects. When | 311 // FakeURLFetcherFactory is a factory for FakeURLFetcher objects. When |
| 310 // instantiated, it sets itself up as the default URLFetcherFactory. Fake | 312 // instantiated, it sets itself up as the default URLFetcherFactory. Fake |
| 311 // responses for given URLs can be set using SetFakeResponse. | 313 // responses for given URLs can be set using SetFakeResponse. |
| 312 // | 314 // |
| 313 // This class is not thread-safe. You should not call SetFakeResponse or | 315 // This class is not thread-safe. You should not call SetFakeResponse or |
| 314 // ClearFakeResponse at the same time you call CreateURLFetcher. However, it is | 316 // ClearFakeResponse at the same time you call CreateURLFetcher. However, it is |
| 315 // OK to start URLFetcher objects while setting or clearning fake responses | 317 // OK to start URLFetcher objects while setting or clearing fake responses |
| 316 // since already created URLFetcher objects will not be affected by any changes | 318 // since already created URLFetcher objects will not be affected by any changes |
| 317 // made to the fake responses (once a URLFetcher object is created you cannot | 319 // made to the fake responses (once a URLFetcher object is created you cannot |
| 318 // change its fake response). | 320 // change its fake response). |
| 319 // | 321 // |
| 320 // Example usage: | 322 // Example usage: |
| 321 // FakeURLFetcherFactory factory; | 323 // FakeURLFetcherFactory factory; |
| 322 // | 324 // |
| 323 // // You know that class SomeService will request url http://a.com/ and you | 325 // // You know that class SomeService will request http://a.com/success and you |
| 324 // // want to test the service class by returning an error. | 326 // // want to respond with a simple html page and an HTTP/200 code. |
| 325 // factory.SetFakeResponse("http://a.com/", "", false); | 327 // factory.SetFakeResponse("http://a.com/success", |
| 326 // // But if the service requests http://b.com/asdf you want to respond with | |
| 327 // // a simple html page and an HTTP/200 code. | |
| 328 // factory.SetFakeResponse("http://b.com/asdf", | |
| 329 // "<html><body>hello world</body></html>", | 328 // "<html><body>hello world</body></html>", |
| 330 // true); | 329 // HTTP_OK); |
| 330 // // You know that class SomeService will request url http://a.com/failure and |
| 331 // // you want to test the service class by returning a server error. |
| 332 // factory.SetFakeResponse("http://a.com/failure", |
| 333 // "", |
| 334 // HTTP_INTERNAL_SERVER_ERROR); |
| 335 // // You know that class SomeService will request url http://a.com/error and |
| 336 // // you want to test the service class by returning a specific error code, |
| 337 // // say, a HTTP/401 error. |
| 338 // factory.SetFakeResponse("http://a.com/error", |
| 339 // "some_response", |
| 340 // HTTP_UNAUTHORIZED); |
| 331 // | 341 // |
| 332 // SomeService service; | 342 // SomeService service; |
| 333 // service.Run(); // Will eventually request these two URLs. | 343 // service.Run(); // Will eventually request these three URLs. |
| 334 class FakeURLFetcherFactory : public URLFetcherFactory, | 344 class FakeURLFetcherFactory : public URLFetcherFactory, |
| 335 public ScopedURLFetcherFactory { | 345 public ScopedURLFetcherFactory { |
| 336 public: | 346 public: |
| 337 // Parameters to FakeURLFetcherCreator: url, delegate, response_data, success | 347 // Parameters to FakeURLFetcherCreator: url, delegate, response_data, |
| 348 // response_code |
| 338 // |url| URL for instantiated FakeURLFetcher | 349 // |url| URL for instantiated FakeURLFetcher |
| 339 // |delegate| Delegate for FakeURLFetcher | 350 // |delegate| Delegate for FakeURLFetcher |
| 340 // |response_data| response data for FakeURLFetcher | 351 // |response_data| response data for FakeURLFetcher |
| 341 // |success| bool indicating response code. true = 200 and false = 500. | 352 // |response_code| response code for FakeURLFetcher |
| 342 // These argument should by default be used in instantiating FakeURLFetcher | 353 // These arguments should by default be used in instantiating FakeURLFetcher |
| 343 // as follows: new FakeURLFetcher(url, delegate, response_data, success) | 354 // as follows: new FakeURLFetcher(url, delegate, response_data, response_code) |
| 344 typedef base::Callback<scoped_ptr<FakeURLFetcher>( | 355 typedef base::Callback<scoped_ptr<FakeURLFetcher>( |
| 345 const GURL&, | 356 const GURL&, |
| 346 URLFetcherDelegate*, | 357 URLFetcherDelegate*, |
| 347 const std::string&, | 358 const std::string&, |
| 348 bool)> FakeURLFetcherCreator; | 359 HttpStatusCode)> FakeURLFetcherCreator; |
| 349 | 360 |
| 350 // |default_factory|, which can be NULL, is a URLFetcherFactory that | 361 // |default_factory|, which can be NULL, is a URLFetcherFactory that |
| 351 // will be used to construct a URLFetcher in case the URL being created | 362 // will be used to construct a URLFetcher in case the URL being created |
| 352 // has no pre-baked response. If it is NULL, a URLFetcherImpl will be | 363 // has no pre-baked response. If it is NULL, a URLFetcherImpl will be |
| 353 // created in this case. | 364 // created in this case. |
| 354 explicit FakeURLFetcherFactory(URLFetcherFactory* default_factory); | 365 explicit FakeURLFetcherFactory(URLFetcherFactory* default_factory); |
| 355 | 366 |
| 356 // |default_factory|, which can be NULL, is a URLFetcherFactory that | 367 // |default_factory|, which can be NULL, is a URLFetcherFactory that |
| 357 // will be used to construct a URLFetcher in case the URL being created | 368 // will be used to construct a URLFetcher in case the URL being created |
| 358 // has no pre-baked response. If it is NULL, a URLFetcherImpl will be | 369 // has no pre-baked response. If it is NULL, a URLFetcherImpl will be |
| (...skipping 10 matching lines...) Expand all Loading... |
| 369 // call to |default_factory_| if it is not NULL, or return NULL if it is | 380 // call to |default_factory_| if it is not NULL, or return NULL if it is |
| 370 // NULL. | 381 // NULL. |
| 371 // Otherwise, it will return a URLFetcher object which will respond with the | 382 // Otherwise, it will return a URLFetcher object which will respond with the |
| 372 // pre-baked response that the client has set by calling SetFakeResponse(). | 383 // pre-baked response that the client has set by calling SetFakeResponse(). |
| 373 virtual URLFetcher* CreateURLFetcher( | 384 virtual URLFetcher* CreateURLFetcher( |
| 374 int id, | 385 int id, |
| 375 const GURL& url, | 386 const GURL& url, |
| 376 URLFetcher::RequestType request_type, | 387 URLFetcher::RequestType request_type, |
| 377 URLFetcherDelegate* d) OVERRIDE; | 388 URLFetcherDelegate* d) OVERRIDE; |
| 378 | 389 |
| 379 // Sets the fake response for a given URL. If success is true we will serve | 390 // Sets the fake response for a given URL. The |response_data| may be empty. |
| 380 // an HTTP/200 and an HTTP/500 otherwise. The |response_data| may be empty. | 391 // The |response_code| may be any HttpStatusCode. For instance, HTTP_OK will |
| 392 // return an HTTP/200 and HTTP_INTERNAL_SERVER_ERROR will return an HTTP/500. |
| 393 // Note: The URLRequestStatus of FakeURLFetchers created by the factory will |
| 394 // be FAILED for HttpStatusCodes HTTP/5xx, and SUCCESS for all other codes. |
| 381 void SetFakeResponse(const GURL& url, | 395 void SetFakeResponse(const GURL& url, |
| 382 const std::string& response_data, | 396 const std::string& response_data, |
| 383 bool success); | 397 HttpStatusCode response_code); |
| 384 | 398 |
| 385 // Clear all the fake responses that were previously set via | 399 // Clear all the fake responses that were previously set via |
| 386 // SetFakeResponse(). | 400 // SetFakeResponse(). |
| 387 void ClearFakeResponses(); | 401 void ClearFakeResponses(); |
| 388 | 402 |
| 389 private: | 403 private: |
| 390 const FakeURLFetcherCreator creator_; | 404 const FakeURLFetcherCreator creator_; |
| 391 typedef std::map<GURL, std::pair<std::string, bool> > FakeResponseMap; | 405 typedef std::map<GURL, |
| 406 std::pair<std::string, HttpStatusCode> > FakeResponseMap; |
| 392 FakeResponseMap fake_responses_; | 407 FakeResponseMap fake_responses_; |
| 393 URLFetcherFactory* const default_factory_; | 408 URLFetcherFactory* const default_factory_; |
| 394 | 409 |
| 395 static scoped_ptr<FakeURLFetcher> DefaultFakeURLFetcherCreator( | 410 static scoped_ptr<FakeURLFetcher> DefaultFakeURLFetcherCreator( |
| 396 const GURL& url, | 411 const GURL& url, |
| 397 URLFetcherDelegate* delegate, | 412 URLFetcherDelegate* delegate, |
| 398 const std::string& response, | 413 const std::string& response_data, |
| 399 bool success); | 414 HttpStatusCode response_code); |
| 400 DISALLOW_COPY_AND_ASSIGN(FakeURLFetcherFactory); | 415 DISALLOW_COPY_AND_ASSIGN(FakeURLFetcherFactory); |
| 401 }; | 416 }; |
| 402 | 417 |
| 403 // This is an implementation of URLFetcherFactory that will create a | 418 // This is an implementation of URLFetcherFactory that will create a |
| 404 // URLFetcherImpl. It can be use in conjunction with a FakeURLFetcherFactory in | 419 // URLFetcherImpl. It can be use in conjunction with a FakeURLFetcherFactory in |
| 405 // integration tests to control the behavior of some requests but execute | 420 // integration tests to control the behavior of some requests but execute |
| 406 // all the other ones. | 421 // all the other ones. |
| 407 class URLFetcherImplFactory : public URLFetcherFactory { | 422 class URLFetcherImplFactory : public URLFetcherFactory { |
| 408 public: | 423 public: |
| 409 URLFetcherImplFactory(); | 424 URLFetcherImplFactory(); |
| 410 virtual ~URLFetcherImplFactory(); | 425 virtual ~URLFetcherImplFactory(); |
| 411 | 426 |
| 412 // This method will create a real URLFetcher. | 427 // This method will create a real URLFetcher. |
| 413 virtual URLFetcher* CreateURLFetcher( | 428 virtual URLFetcher* CreateURLFetcher( |
| 414 int id, | 429 int id, |
| 415 const GURL& url, | 430 const GURL& url, |
| 416 URLFetcher::RequestType request_type, | 431 URLFetcher::RequestType request_type, |
| 417 URLFetcherDelegate* d) OVERRIDE; | 432 URLFetcherDelegate* d) OVERRIDE; |
| 418 | 433 |
| 419 }; | 434 }; |
| 420 | 435 |
| 421 } // namespace net | 436 } // namespace net |
| 422 | 437 |
| 423 #endif // NET_URL_REQUEST_TEST_URL_FETCHER_FACTORY_H_ | 438 #endif // NET_URL_REQUEST_TEST_URL_FETCHER_FACTORY_H_ |
| OLD | NEW |