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

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: Style issues & changing to scoped_ptr callback interface 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();
akalin 2013/02/14 14:52:06 newline before private:
Noam Samuel (WRONG ACCOUNT) 2013/02/14 18:08:11 Done.
297 private:
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
294 // FakeURLFetcherFactory that will delegate creating URLFetcher for unknown 337 // |url| URL for instantiated FakeURLFetcher
295 // url to the given factory. 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<scoped_ptr<FakeURLFetcher>(
344 const GURL&,
345 URLFetcherDelegate*,
346 const std::string&,
347 bool)> FakeURLFetcherCreator;
348
349 // |default_factory|, which can be NULL, is a URLFetcherFactory that
350 // will be used to construct a URLFetcher in case the URL being created
351 // has no pre-baked response. If it is NULL, a URLFetcherImpl will be
352 // created in this case.
296 explicit FakeURLFetcherFactory(URLFetcherFactory* default_factory); 353 explicit FakeURLFetcherFactory(URLFetcherFactory* default_factory);
354
355 // |default_factory|, which can be NULL, is a URLFetcherFactory that
356 // will be used to construct a URLFetcher in case the URL being created
357 // has no pre-baked response. If it is NULL, a URLFetcherImpl will be
358 // created in this case.
359 // |creator| is a callback that returns will be called to create a
360 // FakeURLFetcher if a response is found to a given URL. It can be
361 // set to MakeFakeURLFetcher.
362 FakeURLFetcherFactory(URLFetcherFactory* default_factory,
363 const FakeURLFetcherCreator& creator);
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 scoped_ptr<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