Chromium Code Reviews| Index: net/url_request/test_url_fetcher_factory.h |
| diff --git a/net/url_request/test_url_fetcher_factory.h b/net/url_request/test_url_fetcher_factory.h |
| index 9fa42d896b8b5df7b5b816240672218011ca6780..914c6b68a16c74f2afe7f3a590d5ace38376f021 100644 |
| --- a/net/url_request/test_url_fetcher_factory.h |
| +++ b/net/url_request/test_url_fetcher_factory.h |
| @@ -10,7 +10,9 @@ |
| #include <string> |
| #include <utility> |
| +#include "base/callback.h" |
| #include "base/compiler_specific.h" |
| +#include "base/memory/weak_ptr.h" |
| #include "base/threading/non_thread_safe.h" |
| #include "googleurl/src/gurl.h" |
| #include "net/http/http_request_headers.h" |
| @@ -264,6 +266,47 @@ class TestURLFetcherFactory : public URLFetcherFactory, |
| // |
| // We assume that the thread that is calling Start() on the URLFetcher object |
| // has a message loop running. |
| + |
| +// FakeURLFetcher can be used to create a URLFetcher that will emit a fake |
| +// response when started. This class can be used in place of an actual |
| +// URLFetcher. |
| +// |
| +// ExampleUsage: |
|
akalin
2013/02/13 20:44:01
ExampleUsage -> Example usage
Noam Samuel (WRONG ACCOUNT)
2013/02/13 21:37:46
Done.
|
| +// FakeURLFetcher fake_fetcher("http://a.com", some_delegate, |
| +// "<html><body>hello world</body></html>", |
| +// true); |
| +// |
| +// // Will schedule a call some_delegate->OnURLFetchComplete(&fake_fetcher) |
| +// fake_fetcher.Start(); |
| +class FakeURLFetcher : public TestURLFetcher { |
| + public: |
| + // Normal URL fetcher constructor but also takes in a pre-baked response. |
| + FakeURLFetcher(const GURL& url, |
| + URLFetcherDelegate* d, |
| + const std::string& response_data, bool success); |
| + |
| + // Start the request. This will call the given delegate asynchronously |
| + // with the pre-baked response as parameter. |
| + virtual void Start() OVERRIDE; |
| + |
| + virtual const GURL& GetURL() const OVERRIDE; |
| + |
| + private: |
| + virtual ~FakeURLFetcher(); |
| + |
| + // This is the method which actually calls the delegate that is passed in the |
| + // constructor. |
| + void RunDelegate(); |
| + |
| + base::WeakPtrFactory<FakeURLFetcher> weak_factory_; |
| + |
| + 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.
|
| +}; |
| + |
| + |
| +// FakeURLFetcherFactory is a factory for FakeURLFetcher objects. When |
| +// instantiated, it sets itself up as the default URLFetcherFactory. Fake |
| +// responses for given URLs can be set using SetFakeResponse. |
| // |
| // This class is not thread-safe. You should not call SetFakeResponse or |
| // ClearFakeResponse at the same time you call CreateURLFetcher. However, it is |
| @@ -286,14 +329,38 @@ class TestURLFetcherFactory : public URLFetcherFactory, |
| // |
| // SomeService service; |
| // service.Run(); // Will eventually request these two URLs. |
| - |
| class FakeURLFetcherFactory : public URLFetcherFactory, |
| public ScopedURLFetcherFactory { |
| public: |
| + // Parameters to FakeURLFetcherCreator: url, delegate, response_data, success |
| + // |url| URL for instantiated FakeURLFetcher |
| + // |delegate| Delegate for FakeURLFetcher |
| + // |response_data| response data for FakeURLFetcher |
| + // |success| bool indicating response code. true = 200 and false = 500. |
| + // These argument should by default be used in instantiating FakeURLFetcher |
| + // as follows: new FakeURLFetcher(url, delegate, response_data, success) |
| + typedef base::Callback<FakeURLFetcher*( |
| + const GURL&, |
| + URLFetcherDelegate*, |
| + const std::string&, |
| + bool)> FakeURLFetcherCreator; |
| + |
| FakeURLFetcherFactory(); |
| // FakeURLFetcherFactory that will delegate creating URLFetcher for unknown |
| // url to the given factory. |
| explicit FakeURLFetcherFactory(URLFetcherFactory* default_factory); |
| + |
|
akalin
2013/02/13 20:44:01
remove extra newline
Noam Samuel (WRONG ACCOUNT)
2013/02/13 21:37:46
Done.
|
| + |
| + // |creator| is a callback that returns will be called to create a |
| + // FakeURLFetcher if a response is found to a given URL. It can be |
| + // set to MakeFakeURLFetcher |
|
akalin
2013/02/13 20:44:01
append period
Noam Samuel (WRONG ACCOUNT)
2013/02/13 21:37:46
Done.
|
| + // |default_factory|, which can be NULL, is a URLFetcherFactory that |
| + // will be used to construct a URLFetcher in case the URL being created |
| + // has no pre-baked response. If it is NULL, a URLFetcherImpl will be |
| + // created in this case. |
| + FakeURLFetcherFactory(const FakeURLFetcherCreator& creator, |
| + URLFetcherFactory* default_factory); |
| + |
| virtual ~FakeURLFetcherFactory(); |
| // If no fake response is set for the given URL this method will delegate the |
| @@ -318,10 +385,17 @@ class FakeURLFetcherFactory : public URLFetcherFactory, |
| void ClearFakeResponses(); |
| private: |
| + const FakeURLFetcherCreator creator_; |
| typedef std::map<GURL, std::pair<std::string, bool> > FakeResponseMap; |
| FakeResponseMap fake_responses_; |
| - URLFetcherFactory* default_factory_; |
| + URLFetcherFactory* const default_factory_; |
| + |
|
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.
|
| + static FakeURLFetcher* DefaultFakeURLFetcherCreator( |
| + const GURL& url, |
| + URLFetcherDelegate* delegate, |
| + const std::string& response, |
| + bool success); |
| DISALLOW_COPY_AND_ASSIGN(FakeURLFetcherFactory); |
| }; |