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> |
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
280 // HTTP_OK); | 280 // HTTP_OK); |
281 // | 281 // |
282 // // Will schedule a call to some_delegate->OnURLFetchComplete(&fake_fetcher). | 282 // // Will schedule a call to some_delegate->OnURLFetchComplete(&fake_fetcher). |
283 // fake_fetcher.Start(); | 283 // fake_fetcher.Start(); |
284 class FakeURLFetcher : public TestURLFetcher { | 284 class FakeURLFetcher : public TestURLFetcher { |
285 public: | 285 public: |
286 // 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. |
287 FakeURLFetcher(const GURL& url, | 287 FakeURLFetcher(const GURL& url, |
288 URLFetcherDelegate* d, | 288 URLFetcherDelegate* d, |
289 const std::string& response_data, | 289 const std::string& response_data, |
290 HttpStatusCode response_code); | 290 HttpStatusCode response_code, |
291 URLRequestStatus::Status status); | |
291 | 292 |
292 // Start the request. This will call the given delegate asynchronously | 293 // Start the request. This will call the given delegate asynchronously |
293 // with the pre-baked response as parameter. | 294 // with the pre-baked response as parameter. |
294 virtual void Start() OVERRIDE; | 295 virtual void Start() OVERRIDE; |
295 | 296 |
296 virtual const GURL& GetURL() const OVERRIDE; | 297 virtual const GURL& GetURL() const OVERRIDE; |
297 | 298 |
298 virtual ~FakeURLFetcher(); | 299 virtual ~FakeURLFetcher(); |
299 | 300 |
300 private: | 301 private: |
(...skipping 18 matching lines...) Expand all Loading... | |
319 // made to the fake responses (once a URLFetcher object is created you cannot | 320 // made to the fake responses (once a URLFetcher object is created you cannot |
320 // change its fake response). | 321 // change its fake response). |
321 // | 322 // |
322 // Example usage: | 323 // Example usage: |
323 // FakeURLFetcherFactory factory; | 324 // FakeURLFetcherFactory factory; |
324 // | 325 // |
325 // // You know that class SomeService will request http://a.com/success and you | 326 // // You know that class SomeService will request http://a.com/success and you |
326 // // want to respond with a simple html page and an HTTP/200 code. | 327 // // want to respond with a simple html page and an HTTP/200 code. |
327 // factory.SetFakeResponse("http://a.com/success", | 328 // factory.SetFakeResponse("http://a.com/success", |
328 // "<html><body>hello world</body></html>", | 329 // "<html><body>hello world</body></html>", |
329 // HTTP_OK); | 330 // HTTP_OK, |
331 // URLRequestStatus::SUCCESS); | |
330 // // You know that class SomeService will request url http://a.com/failure and | 332 // // 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. | 333 // // you want to test the service class by returning a server error. |
332 // factory.SetFakeResponse("http://a.com/failure", | 334 // factory.SetFakeResponse("http://a.com/failure", |
333 // "", | 335 // "", |
334 // HTTP_INTERNAL_SERVER_ERROR); | 336 // HTTP_INTERNAL_SERVER_ERROR, |
337 // URLRequestStatus::FAILED); | |
mmenke
2013/11/06 15:40:12
This really should be URLRequestStatus::SUCCESS.
Raghu Simha
2013/11/06 16:58:59
I've fixed the example section to include both the
| |
335 // // You know that class SomeService will request url http://a.com/error and | 338 // // 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, | 339 // // you want to test the service class by returning a specific error code, |
337 // // say, a HTTP/401 error. | 340 // // say, a HTTP/401 error. |
338 // factory.SetFakeResponse("http://a.com/error", | 341 // factory.SetFakeResponse("http://a.com/error", |
339 // "some_response", | 342 // "some_response", |
340 // HTTP_UNAUTHORIZED); | 343 // HTTP_UNAUTHORIZED, |
344 // URLRequestStatus::SUCCESS); | |
341 // | 345 // |
342 // SomeService service; | 346 // SomeService service; |
343 // service.Run(); // Will eventually request these three URLs. | 347 // service.Run(); // Will eventually request these three URLs. |
344 class FakeURLFetcherFactory : public URLFetcherFactory, | 348 class FakeURLFetcherFactory : public URLFetcherFactory, |
345 public ScopedURLFetcherFactory { | 349 public ScopedURLFetcherFactory { |
346 public: | 350 public: |
347 // Parameters to FakeURLFetcherCreator: url, delegate, response_data, | 351 // Parameters to FakeURLFetcherCreator: url, delegate, response_data, |
348 // response_code | 352 // response_code |
349 // |url| URL for instantiated FakeURLFetcher | 353 // |url| URL for instantiated FakeURLFetcher |
350 // |delegate| Delegate for FakeURLFetcher | 354 // |delegate| Delegate for FakeURLFetcher |
351 // |response_data| response data for FakeURLFetcher | 355 // |response_data| response data for FakeURLFetcher |
352 // |response_code| response code for FakeURLFetcher | 356 // |response_code| response code for FakeURLFetcher |
357 // |status| URL fetch status for FakeURLFetcher | |
353 // These arguments should by default be used in instantiating FakeURLFetcher | 358 // These arguments should by default be used in instantiating FakeURLFetcher |
354 // as follows: new FakeURLFetcher(url, delegate, response_data, response_code) | 359 // like so: |
360 // new FakeURLFetcher(url, delegate, response_data, response_code, status) | |
355 typedef base::Callback<scoped_ptr<FakeURLFetcher>( | 361 typedef base::Callback<scoped_ptr<FakeURLFetcher>( |
356 const GURL&, | 362 const GURL&, |
357 URLFetcherDelegate*, | 363 URLFetcherDelegate*, |
358 const std::string&, | 364 const std::string&, |
359 HttpStatusCode)> FakeURLFetcherCreator; | 365 HttpStatusCode, |
366 URLRequestStatus::Status)> FakeURLFetcherCreator; | |
360 | 367 |
361 // |default_factory|, which can be NULL, is a URLFetcherFactory that | 368 // |default_factory|, which can be NULL, is a URLFetcherFactory that |
362 // will be used to construct a URLFetcher in case the URL being created | 369 // will be used to construct a URLFetcher in case the URL being created |
363 // has no pre-baked response. If it is NULL, a URLFetcherImpl will be | 370 // has no pre-baked response. If it is NULL, a URLFetcherImpl will be |
364 // created in this case. | 371 // created in this case. |
365 explicit FakeURLFetcherFactory(URLFetcherFactory* default_factory); | 372 explicit FakeURLFetcherFactory(URLFetcherFactory* default_factory); |
366 | 373 |
367 // |default_factory|, which can be NULL, is a URLFetcherFactory that | 374 // |default_factory|, which can be NULL, is a URLFetcherFactory that |
368 // will be used to construct a URLFetcher in case the URL being created | 375 // will be used to construct a URLFetcher in case the URL being created |
369 // has no pre-baked response. If it is NULL, a URLFetcherImpl will be | 376 // has no pre-baked response. If it is NULL, a URLFetcherImpl will be |
(...skipping 13 matching lines...) Expand all Loading... | |
383 // pre-baked response that the client has set by calling SetFakeResponse(). | 390 // pre-baked response that the client has set by calling SetFakeResponse(). |
384 virtual URLFetcher* CreateURLFetcher( | 391 virtual URLFetcher* CreateURLFetcher( |
385 int id, | 392 int id, |
386 const GURL& url, | 393 const GURL& url, |
387 URLFetcher::RequestType request_type, | 394 URLFetcher::RequestType request_type, |
388 URLFetcherDelegate* d) OVERRIDE; | 395 URLFetcherDelegate* d) OVERRIDE; |
389 | 396 |
390 // Sets the fake response for a given URL. The |response_data| may be empty. | 397 // Sets the fake response for a given URL. The |response_data| may be empty. |
391 // The |response_code| may be any HttpStatusCode. For instance, HTTP_OK will | 398 // 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. | 399 // 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 | 400 // The |status| argument may be any URLRequestStatus::Status value. Typically, |
394 // be FAILED for HttpStatusCodes HTTP/5xx, and SUCCESS for all other codes. | 401 // requests that return a valid HttpStatusCode have the SUCCESS status, while |
402 // requests that indicate a failure to connect to the server have the FAILED | |
403 // status. | |
395 void SetFakeResponse(const GURL& url, | 404 void SetFakeResponse(const GURL& url, |
396 const std::string& response_data, | 405 const std::string& response_data, |
397 HttpStatusCode response_code); | 406 HttpStatusCode response_code, |
407 URLRequestStatus::Status status); | |
398 | 408 |
399 // Clear all the fake responses that were previously set via | 409 // Clear all the fake responses that were previously set via |
400 // SetFakeResponse(). | 410 // SetFakeResponse(). |
401 void ClearFakeResponses(); | 411 void ClearFakeResponses(); |
402 | 412 |
403 private: | 413 private: |
414 struct FakeURLResponse { | |
415 std::string response_data; | |
416 HttpStatusCode response_code; | |
417 URLRequestStatus::Status status; | |
418 }; | |
419 typedef std::map<GURL, FakeURLResponse> FakeResponseMap; | |
420 | |
404 const FakeURLFetcherCreator creator_; | 421 const FakeURLFetcherCreator creator_; |
405 typedef std::map<GURL, | |
406 std::pair<std::string, HttpStatusCode> > FakeResponseMap; | |
407 FakeResponseMap fake_responses_; | 422 FakeResponseMap fake_responses_; |
408 URLFetcherFactory* const default_factory_; | 423 URLFetcherFactory* const default_factory_; |
409 | 424 |
410 static scoped_ptr<FakeURLFetcher> DefaultFakeURLFetcherCreator( | 425 static scoped_ptr<FakeURLFetcher> DefaultFakeURLFetcherCreator( |
411 const GURL& url, | 426 const GURL& url, |
412 URLFetcherDelegate* delegate, | 427 URLFetcherDelegate* delegate, |
413 const std::string& response_data, | 428 const std::string& response_data, |
414 HttpStatusCode response_code); | 429 HttpStatusCode response_code, |
430 URLRequestStatus::Status status); | |
415 DISALLOW_COPY_AND_ASSIGN(FakeURLFetcherFactory); | 431 DISALLOW_COPY_AND_ASSIGN(FakeURLFetcherFactory); |
416 }; | 432 }; |
417 | 433 |
418 // This is an implementation of URLFetcherFactory that will create a | 434 // This is an implementation of URLFetcherFactory that will create a |
419 // URLFetcherImpl. It can be use in conjunction with a FakeURLFetcherFactory in | 435 // URLFetcherImpl. It can be use in conjunction with a FakeURLFetcherFactory in |
420 // integration tests to control the behavior of some requests but execute | 436 // integration tests to control the behavior of some requests but execute |
421 // all the other ones. | 437 // all the other ones. |
422 class URLFetcherImplFactory : public URLFetcherFactory { | 438 class URLFetcherImplFactory : public URLFetcherFactory { |
423 public: | 439 public: |
424 URLFetcherImplFactory(); | 440 URLFetcherImplFactory(); |
425 virtual ~URLFetcherImplFactory(); | 441 virtual ~URLFetcherImplFactory(); |
426 | 442 |
427 // This method will create a real URLFetcher. | 443 // This method will create a real URLFetcher. |
428 virtual URLFetcher* CreateURLFetcher( | 444 virtual URLFetcher* CreateURLFetcher( |
429 int id, | 445 int id, |
430 const GURL& url, | 446 const GURL& url, |
431 URLFetcher::RequestType request_type, | 447 URLFetcher::RequestType request_type, |
432 URLFetcherDelegate* d) OVERRIDE; | 448 URLFetcherDelegate* d) OVERRIDE; |
433 | 449 |
434 }; | 450 }; |
435 | 451 |
436 } // namespace net | 452 } // namespace net |
437 | 453 |
438 #endif // NET_URL_REQUEST_TEST_URL_FETCHER_FACTORY_H_ | 454 #endif // NET_URL_REQUEST_TEST_URL_FETCHER_FACTORY_H_ |
OLD | NEW |