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

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

Powered by Google App Engine
This is Rietveld 408576698