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

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

Powered by Google App Engine
This is Rietveld 408576698