Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(82)

Side by Side Diff: net/url_request/test_url_fetcher_factory.h

Issue 60923002: [sync] Allow FakeURLFetcher to return an arbitrary URLRequestStatus (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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
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,
330 // // You know that class SomeService will request url http://a.com/failure and 331 // URLRequestStatus::SUCCESS);
331 // // you want to test the service class by returning a server error. 332 // // You know that class SomeService will request url http://a.com/servererror
333 // // and you want to test the service class by returning a server error.
334 // factory.SetFakeResponse("http://a.com/servererror",
335 // "",
336 // HTTP_INTERNAL_SERVER_ERROR,
337 // URLRequestStatus::SUCCESS);
338 // // You know that class SomeService will request url http://a.com/autherror
339 // // and you want to test the service class by returning a specific error
340 // // code, say, a HTTP/401 error.
341 // factory.SetFakeResponse("http://a.com/autherror",
342 // "some_response",
343 // HTTP_UNAUTHORIZED,
344 // URLRequestStatus::SUCCESS);
345 //
346 // // You know that class SomeService will request url http://a.com/failure
347 // // and you want to test the service class by returning a failure in the
348 // // network layer.
332 // factory.SetFakeResponse("http://a.com/failure", 349 // factory.SetFakeResponse("http://a.com/failure",
333 // "", 350 // "",
334 // HTTP_INTERNAL_SERVER_ERROR); 351 // HTTP_INTERNAL_SERVER_ERROR,
335 // // You know that class SomeService will request url http://a.com/error and 352 // URLRequestStatus::FAILURE);
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);
341 // 353 //
342 // SomeService service; 354 // SomeService service;
343 // service.Run(); // Will eventually request these three URLs. 355 // service.Run(); // Will eventually request these three URLs.
344 class FakeURLFetcherFactory : public URLFetcherFactory, 356 class FakeURLFetcherFactory : public URLFetcherFactory,
345 public ScopedURLFetcherFactory { 357 public ScopedURLFetcherFactory {
346 public: 358 public:
347 // Parameters to FakeURLFetcherCreator: url, delegate, response_data, 359 // Parameters to FakeURLFetcherCreator: url, delegate, response_data,
348 // response_code 360 // response_code
349 // |url| URL for instantiated FakeURLFetcher 361 // |url| URL for instantiated FakeURLFetcher
350 // |delegate| Delegate for FakeURLFetcher 362 // |delegate| Delegate for FakeURLFetcher
351 // |response_data| response data for FakeURLFetcher 363 // |response_data| response data for FakeURLFetcher
352 // |response_code| response code for FakeURLFetcher 364 // |response_code| response code for FakeURLFetcher
365 // |status| URL fetch status for FakeURLFetcher
353 // These arguments should by default be used in instantiating FakeURLFetcher 366 // These arguments should by default be used in instantiating FakeURLFetcher
354 // as follows: new FakeURLFetcher(url, delegate, response_data, response_code) 367 // like so:
368 // new FakeURLFetcher(url, delegate, response_data, response_code, status)
355 typedef base::Callback<scoped_ptr<FakeURLFetcher>( 369 typedef base::Callback<scoped_ptr<FakeURLFetcher>(
356 const GURL&, 370 const GURL&,
357 URLFetcherDelegate*, 371 URLFetcherDelegate*,
358 const std::string&, 372 const std::string&,
359 HttpStatusCode)> FakeURLFetcherCreator; 373 HttpStatusCode,
374 URLRequestStatus::Status)> FakeURLFetcherCreator;
360 375
361 // |default_factory|, which can be NULL, is a URLFetcherFactory that 376 // |default_factory|, which can be NULL, is a URLFetcherFactory that
362 // will be used to construct a URLFetcher in case the URL being created 377 // 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 378 // has no pre-baked response. If it is NULL, a URLFetcherImpl will be
364 // created in this case. 379 // created in this case.
365 explicit FakeURLFetcherFactory(URLFetcherFactory* default_factory); 380 explicit FakeURLFetcherFactory(URLFetcherFactory* default_factory);
366 381
367 // |default_factory|, which can be NULL, is a URLFetcherFactory that 382 // |default_factory|, which can be NULL, is a URLFetcherFactory that
368 // will be used to construct a URLFetcher in case the URL being created 383 // 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 384 // has no pre-baked response. If it is NULL, a URLFetcherImpl will be
(...skipping 13 matching lines...) Expand all
383 // pre-baked response that the client has set by calling SetFakeResponse(). 398 // pre-baked response that the client has set by calling SetFakeResponse().
384 virtual URLFetcher* CreateURLFetcher( 399 virtual URLFetcher* CreateURLFetcher(
385 int id, 400 int id,
386 const GURL& url, 401 const GURL& url,
387 URLFetcher::RequestType request_type, 402 URLFetcher::RequestType request_type,
388 URLFetcherDelegate* d) OVERRIDE; 403 URLFetcherDelegate* d) OVERRIDE;
389 404
390 // Sets the fake response for a given URL. The |response_data| may be empty. 405 // 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 406 // 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. 407 // 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 408 // The |status| argument may be any URLRequestStatus::Status value. Typically,
394 // be FAILED for HttpStatusCodes HTTP/5xx, and SUCCESS for all other codes. 409 // requests that return a valid HttpStatusCode have the SUCCESS status, while
410 // requests that indicate a failure to connect to the server have the FAILED
411 // status.
395 void SetFakeResponse(const GURL& url, 412 void SetFakeResponse(const GURL& url,
396 const std::string& response_data, 413 const std::string& response_data,
397 HttpStatusCode response_code); 414 HttpStatusCode response_code,
415 URLRequestStatus::Status status);
398 416
399 // Clear all the fake responses that were previously set via 417 // Clear all the fake responses that were previously set via
400 // SetFakeResponse(). 418 // SetFakeResponse().
401 void ClearFakeResponses(); 419 void ClearFakeResponses();
402 420
403 private: 421 private:
422 struct FakeURLResponse {
423 std::string response_data;
424 HttpStatusCode response_code;
425 URLRequestStatus::Status status;
426 };
427 typedef std::map<GURL, FakeURLResponse> FakeResponseMap;
428
404 const FakeURLFetcherCreator creator_; 429 const FakeURLFetcherCreator creator_;
405 typedef std::map<GURL,
406 std::pair<std::string, HttpStatusCode> > FakeResponseMap;
407 FakeResponseMap fake_responses_; 430 FakeResponseMap fake_responses_;
408 URLFetcherFactory* const default_factory_; 431 URLFetcherFactory* const default_factory_;
409 432
410 static scoped_ptr<FakeURLFetcher> DefaultFakeURLFetcherCreator( 433 static scoped_ptr<FakeURLFetcher> DefaultFakeURLFetcherCreator(
411 const GURL& url, 434 const GURL& url,
412 URLFetcherDelegate* delegate, 435 URLFetcherDelegate* delegate,
413 const std::string& response_data, 436 const std::string& response_data,
414 HttpStatusCode response_code); 437 HttpStatusCode response_code,
438 URLRequestStatus::Status status);
415 DISALLOW_COPY_AND_ASSIGN(FakeURLFetcherFactory); 439 DISALLOW_COPY_AND_ASSIGN(FakeURLFetcherFactory);
416 }; 440 };
417 441
418 // This is an implementation of URLFetcherFactory that will create a 442 // This is an implementation of URLFetcherFactory that will create a
419 // URLFetcherImpl. It can be use in conjunction with a FakeURLFetcherFactory in 443 // URLFetcherImpl. It can be use in conjunction with a FakeURLFetcherFactory in
420 // integration tests to control the behavior of some requests but execute 444 // integration tests to control the behavior of some requests but execute
421 // all the other ones. 445 // all the other ones.
422 class URLFetcherImplFactory : public URLFetcherFactory { 446 class URLFetcherImplFactory : public URLFetcherFactory {
423 public: 447 public:
424 URLFetcherImplFactory(); 448 URLFetcherImplFactory();
425 virtual ~URLFetcherImplFactory(); 449 virtual ~URLFetcherImplFactory();
426 450
427 // This method will create a real URLFetcher. 451 // This method will create a real URLFetcher.
428 virtual URLFetcher* CreateURLFetcher( 452 virtual URLFetcher* CreateURLFetcher(
429 int id, 453 int id,
430 const GURL& url, 454 const GURL& url,
431 URLFetcher::RequestType request_type, 455 URLFetcher::RequestType request_type,
432 URLFetcherDelegate* d) OVERRIDE; 456 URLFetcherDelegate* d) OVERRIDE;
433 457
434 }; 458 };
435 459
436 } // namespace net 460 } // namespace net
437 461
438 #endif // NET_URL_REQUEST_TEST_URL_FETCHER_FACTORY_H_ 462 #endif // NET_URL_REQUEST_TEST_URL_FETCHER_FACTORY_H_
OLDNEW
« no previous file with comments | « components/precache/core/precache_fetcher_unittest.cc ('k') | net/url_request/test_url_fetcher_factory.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698