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