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

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

Issue 12211076: Refactored FakeURLFetcher to make it more flexible (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years, 10 months 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
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>
11 #include <utility> 11 #include <utility>
12 12
13 #include "base/basictypes.h"
14 #include "base/callback.h"
13 #include "base/compiler_specific.h" 15 #include "base/compiler_specific.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/memory/weak_ptr.h"
14 #include "base/threading/non_thread_safe.h" 18 #include "base/threading/non_thread_safe.h"
15 #include "googleurl/src/gurl.h" 19 #include "googleurl/src/gurl.h"
16 #include "net/http/http_request_headers.h" 20 #include "net/http/http_request_headers.h"
17 #include "net/url_request/url_fetcher_factory.h" 21 #include "net/url_request/url_fetcher_factory.h"
18 #include "net/url_request/url_request_status.h" 22 #include "net/url_request/url_request_status.h"
19 23
20 namespace net { 24 namespace net {
21 25
22 // Changes URLFetcher's Factory for the lifetime of the object. 26 // Changes URLFetcher's Factory for the lifetime of the object.
23 // Note that this scoper cannot be nested (to make it even harder to misuse). 27 // Note that this scoper cannot be nested (to make it even harder to misuse).
(...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after
257 261
258 // The FakeURLFetcher and FakeURLFetcherFactory classes are similar to the 262 // The FakeURLFetcher and FakeURLFetcherFactory classes are similar to the
259 // ones above but don't require you to know when exactly the URLFetcher objects 263 // ones above but don't require you to know when exactly the URLFetcher objects
260 // will be created. 264 // will be created.
261 // 265 //
262 // These classes let you set pre-baked HTTP responses for particular URLs. 266 // These classes let you set pre-baked HTTP responses for particular URLs.
263 // E.g., if the user requests http://a.com/ then respond with an HTTP/500. 267 // E.g., if the user requests http://a.com/ then respond with an HTTP/500.
264 // 268 //
265 // We assume that the thread that is calling Start() on the URLFetcher object 269 // We assume that the thread that is calling Start() on the URLFetcher object
266 // has a message loop running. 270 // has a message loop running.
271
272 // 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 // URLFetcher.
275 //
276 // Example usage:
277 // FakeURLFetcher fake_fetcher("http://a.com", some_delegate,
278 // "<html><body>hello world</body></html>",
279 // true);
280 //
281 // // Will schedule a call to some_delegate->OnURLFetchComplete(&fake_fetcher).
282 // fake_fetcher.Start();
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
290 // Start the request. This will call the given delegate asynchronously
291 // with the pre-baked response as parameter.
292 virtual void Start() OVERRIDE;
293
294 virtual const GURL& GetURL() const OVERRIDE;
295
296 virtual ~FakeURLFetcher();
297
298 private:
299 // This is the method which actually calls the delegate that is passed in the
300 // constructor.
301 void RunDelegate();
302
303 base::WeakPtrFactory<FakeURLFetcher> weak_factory_;
304
305 DISALLOW_COPY_AND_ASSIGN(FakeURLFetcher);
306 };
307
308
309 // FakeURLFetcherFactory is a factory for FakeURLFetcher objects. When
310 // instantiated, it sets itself up as the default URLFetcherFactory. Fake
311 // responses for given URLs can be set using SetFakeResponse.
267 // 312 //
268 // This class is not thread-safe. You should not call SetFakeResponse or 313 // This class is not thread-safe. You should not call SetFakeResponse or
269 // ClearFakeResponse at the same time you call CreateURLFetcher. However, it is 314 // ClearFakeResponse at the same time you call CreateURLFetcher. However, it is
270 // OK to start URLFetcher objects while setting or clearning fake responses 315 // OK to start URLFetcher objects while setting or clearning fake responses
271 // since already created URLFetcher objects will not be affected by any changes 316 // since already created URLFetcher objects will not be affected by any changes
272 // made to the fake responses (once a URLFetcher object is created you cannot 317 // made to the fake responses (once a URLFetcher object is created you cannot
273 // change its fake response). 318 // change its fake response).
274 // 319 //
275 // Example usage: 320 // Example usage:
276 // FakeURLFetcherFactory factory; 321 // FakeURLFetcherFactory factory;
277 // 322 //
278 // // You know that class SomeService will request url http://a.com/ and you 323 // // You know that class SomeService will request url http://a.com/ and you
279 // // want to test the service class by returning an error. 324 // // want to test the service class by returning an error.
280 // factory.SetFakeResponse("http://a.com/", "", false); 325 // factory.SetFakeResponse("http://a.com/", "", false);
281 // // But if the service requests http://b.com/asdf you want to respond with 326 // // But if the service requests http://b.com/asdf you want to respond with
282 // // a simple html page and an HTTP/200 code. 327 // // a simple html page and an HTTP/200 code.
283 // factory.SetFakeResponse("http://b.com/asdf", 328 // factory.SetFakeResponse("http://b.com/asdf",
284 // "<html><body>hello world</body></html>", 329 // "<html><body>hello world</body></html>",
285 // true); 330 // true);
286 // 331 //
287 // SomeService service; 332 // SomeService service;
288 // service.Run(); // Will eventually request these two URLs. 333 // service.Run(); // Will eventually request these two URLs.
289
290 class FakeURLFetcherFactory : public URLFetcherFactory, 334 class FakeURLFetcherFactory : public URLFetcherFactory,
291 public ScopedURLFetcherFactory { 335 public ScopedURLFetcherFactory {
292 public: 336 public:
293 FakeURLFetcherFactory(); 337 // Parameters to FakeURLFetcherCreator: url, delegate, response_data, success
294 // FakeURLFetcherFactory that will delegate creating URLFetcher for unknown 338 // |url| URL for instantiated FakeURLFetcher
295 // url to the given factory. 339 // |delegate| Delegate for FakeURLFetcher
340 // |response_data| response data for FakeURLFetcher
341 // |success| bool indicating response code. true = 200 and false = 500.
342 // These argument should by default be used in instantiating FakeURLFetcher
343 // as follows: new FakeURLFetcher(url, delegate, response_data, success)
344 typedef base::Callback<scoped_ptr<FakeURLFetcher>(
345 const GURL&,
346 URLFetcherDelegate*,
347 const std::string&,
348 bool)> FakeURLFetcherCreator;
349
350 // |default_factory|, which can be NULL, is a URLFetcherFactory that
351 // 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
353 // created in this case.
296 explicit FakeURLFetcherFactory(URLFetcherFactory* default_factory); 354 explicit FakeURLFetcherFactory(URLFetcherFactory* default_factory);
355
356 // |default_factory|, which can be NULL, is a URLFetcherFactory that
357 // 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
359 // created in this case.
360 // |creator| is a callback that returns will be called to create a
361 // FakeURLFetcher if a response is found to a given URL. It can be
362 // set to MakeFakeURLFetcher.
363 FakeURLFetcherFactory(URLFetcherFactory* default_factory,
364 const FakeURLFetcherCreator& creator);
365
297 virtual ~FakeURLFetcherFactory(); 366 virtual ~FakeURLFetcherFactory();
298 367
299 // If no fake response is set for the given URL this method will delegate the 368 // If no fake response is set for the given URL this method will delegate the
300 // call to |default_factory_| if it is not NULL, or return NULL if it is 369 // call to |default_factory_| if it is not NULL, or return NULL if it is
301 // NULL. 370 // NULL.
302 // Otherwise, it will return a URLFetcher object which will respond with the 371 // Otherwise, it will return a URLFetcher object which will respond with the
303 // pre-baked response that the client has set by calling SetFakeResponse(). 372 // pre-baked response that the client has set by calling SetFakeResponse().
304 virtual URLFetcher* CreateURLFetcher( 373 virtual URLFetcher* CreateURLFetcher(
305 int id, 374 int id,
306 const GURL& url, 375 const GURL& url,
307 URLFetcher::RequestType request_type, 376 URLFetcher::RequestType request_type,
308 URLFetcherDelegate* d) OVERRIDE; 377 URLFetcherDelegate* d) OVERRIDE;
309 378
310 // Sets the fake response for a given URL. If success is true we will serve 379 // Sets the fake response for a given URL. If success is true we will serve
311 // an HTTP/200 and an HTTP/500 otherwise. The |response_data| may be empty. 380 // an HTTP/200 and an HTTP/500 otherwise. The |response_data| may be empty.
312 void SetFakeResponse(const std::string& url, 381 void SetFakeResponse(const std::string& url,
313 const std::string& response_data, 382 const std::string& response_data,
314 bool success); 383 bool success);
315 384
316 // Clear all the fake responses that were previously set via 385 // Clear all the fake responses that were previously set via
317 // SetFakeResponse(). 386 // SetFakeResponse().
318 void ClearFakeResponses(); 387 void ClearFakeResponses();
319 388
320 private: 389 private:
390 const FakeURLFetcherCreator creator_;
321 typedef std::map<GURL, std::pair<std::string, bool> > FakeResponseMap; 391 typedef std::map<GURL, std::pair<std::string, bool> > FakeResponseMap;
322 FakeResponseMap fake_responses_; 392 FakeResponseMap fake_responses_;
323 URLFetcherFactory* default_factory_; 393 URLFetcherFactory* const default_factory_;
324 394
395 static scoped_ptr<FakeURLFetcher> DefaultFakeURLFetcherCreator(
396 const GURL& url,
397 URLFetcherDelegate* delegate,
398 const std::string& response,
399 bool success);
325 DISALLOW_COPY_AND_ASSIGN(FakeURLFetcherFactory); 400 DISALLOW_COPY_AND_ASSIGN(FakeURLFetcherFactory);
326 }; 401 };
327 402
328 // This is an implementation of URLFetcherFactory that will create a 403 // This is an implementation of URLFetcherFactory that will create a
329 // URLFetcherImpl. It can be use in conjunction with a FakeURLFetcherFactory in 404 // URLFetcherImpl. It can be use in conjunction with a FakeURLFetcherFactory in
330 // integration tests to control the behavior of some requests but execute 405 // integration tests to control the behavior of some requests but execute
331 // all the other ones. 406 // all the other ones.
332 class URLFetcherImplFactory : public URLFetcherFactory { 407 class URLFetcherImplFactory : public URLFetcherFactory {
333 public: 408 public:
334 URLFetcherImplFactory(); 409 URLFetcherImplFactory();
335 virtual ~URLFetcherImplFactory(); 410 virtual ~URLFetcherImplFactory();
336 411
337 // This method will create a real URLFetcher. 412 // This method will create a real URLFetcher.
338 virtual URLFetcher* CreateURLFetcher( 413 virtual URLFetcher* CreateURLFetcher(
339 int id, 414 int id,
340 const GURL& url, 415 const GURL& url,
341 URLFetcher::RequestType request_type, 416 URLFetcher::RequestType request_type,
342 URLFetcherDelegate* d) OVERRIDE; 417 URLFetcherDelegate* d) OVERRIDE;
343 418
344 }; 419 };
345 420
346 } // namespace net 421 } // namespace net
347 422
348 #endif // NET_URL_REQUEST_TEST_URL_FETCHER_FACTORY_H_ 423 #endif // NET_URL_REQUEST_TEST_URL_FETCHER_FACTORY_H_
OLDNEW
« no previous file with comments | « chrome/browser/safe_browsing/download_protection_service_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