diff options
author | jam@chromium.org <jam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-10-26 23:39:59 +0000 |
---|---|---|
committer | jam@chromium.org <jam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-10-26 23:39:59 +0000 |
commit | beba03b3790fd1645b301818034d357a0e99032d (patch) | |
tree | cd973eb9325b2fc21a777f003037aeb96d6b8681 /content/test | |
parent | d8e942a682fccb8f96300efa6120f25abc55a52e (diff) | |
download | chromium_src-beba03b3790fd1645b301818034d357a0e99032d.zip chromium_src-beba03b3790fd1645b301818034d357a0e99032d.tar.gz chromium_src-beba03b3790fd1645b301818034d357a0e99032d.tar.bz2 |
Make test URLFetcher implementations not derive from the URLFetcher implementation, since we want to hide that from chrome completely.
SetBackoffDelayForTesting moves from content::UrlFetcher to TestURLFetcher, now that the test objects derive from it.
BUG=98716
Review URL: http://codereview.chromium.org/8395038
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@107468 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'content/test')
-rw-r--r-- | content/test/test_url_fetcher_factory.cc | 223 | ||||
-rw-r--r-- | content/test/test_url_fetcher_factory.h | 134 |
2 files changed, 230 insertions, 127 deletions
diff --git a/content/test/test_url_fetcher_factory.cc b/content/test/test_url_fetcher_factory.cc index 8ec1b92..62cf94e 100644 --- a/content/test/test_url_fetcher_factory.cc +++ b/content/test/test_url_fetcher_factory.cc @@ -8,11 +8,14 @@ #include "base/compiler_specific.h" #include "base/message_loop.h" +#include "content/common/net/url_fetcher.h" #include "content/public/common/url_fetcher_delegate.h" +#include "net/base/host_port_pair.h" #include "net/http/http_response_headers.h" #include "net/url_request/url_request_status.h" -ScopedURLFetcherFactory::ScopedURLFetcherFactory(URLFetcher::Factory* factory) { +ScopedURLFetcherFactory::ScopedURLFetcherFactory( + content::URLFetcherFactory* factory) { DCHECK(!URLFetcher::factory()); URLFetcher::set_factory(factory); } @@ -26,15 +29,28 @@ TestURLFetcher::TestURLFetcher(int id, const GURL& url, URLFetcher::RequestType request_type, content::URLFetcherDelegate* d) - : URLFetcher(url, request_type, d), - id_(id), + : id_(id), original_url_(url), - did_receive_last_chunk_(false) { + delegate_(d), + did_receive_last_chunk_(false), + fake_load_flags_(0), + fake_response_code_(-1), + fake_response_destination_(STRING), + fake_was_fetched_via_proxy_(false), + fake_max_retries_(0) { } TestURLFetcher::~TestURLFetcher() { } +void TestURLFetcher::SetUploadData(const std::string& upload_content_type, + const std::string& upload_content) { + upload_data_ = upload_content; +} + +void TestURLFetcher::SetChunkedUpload(const std::string& upload_content_type) { +} + void TestURLFetcher::AppendChunkToUpload(const std::string& data, bool is_last_chunk) { DCHECK(!did_receive_last_chunk_); @@ -42,36 +58,104 @@ void TestURLFetcher::AppendChunkToUpload(const std::string& data, chunks_.push_back(data); } +void TestURLFetcher::SetLoadFlags(int load_flags) { + fake_load_flags_= load_flags; +} + +int TestURLFetcher::GetLoadFlags() const { + return fake_load_flags_; +} + +void TestURLFetcher::SetReferrer(const std::string& referrer) { +} + +void TestURLFetcher::SetExtraRequestHeaders( + const std::string& extra_request_headers) { + fake_extra_request_headers_.Clear(); + fake_extra_request_headers_.AddHeadersFromString(extra_request_headers); +} + +void TestURLFetcher::GetExtraRequestHeaders(net::HttpRequestHeaders* headers) { + *headers = fake_extra_request_headers_; +} + +void TestURLFetcher::SetRequestContext( + net::URLRequestContextGetter* request_context_getter) { +} + +void TestURLFetcher::SetAutomaticallyRetryOn5xx(bool retry) { +} + +void TestURLFetcher::SetMaxRetries(int max_retries) { + fake_max_retries_ = max_retries; +} + +int TestURLFetcher::GetMaxRetries() const { + return fake_max_retries_; +} + +base::TimeDelta TestURLFetcher::GetBackoffDelay() const { + return fake_backoff_delay_; +} + +void TestURLFetcher::SaveResponseToTemporaryFile( + scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy) { +} + +net::HttpResponseHeaders* TestURLFetcher::GetResponseHeaders() const { + return fake_response_headers_; +} + +net::HostPortPair TestURLFetcher::GetSocketAddress() const { + NOTIMPLEMENTED(); + return net::HostPortPair(); +} + +bool TestURLFetcher::WasFetchedViaProxy() const { + return fake_was_fetched_via_proxy_; +} + +void TestURLFetcher::Start() { + // Overriden to do nothing. It is assumed the caller will notify the delegate. +} + +void TestURLFetcher::StartWithRequestContextGetter( + net::URLRequestContextGetter* request_context_getter) { + NOTIMPLEMENTED(); +} + const GURL& TestURLFetcher::GetOriginalUrl() const { return original_url_; } -void TestURLFetcher::set_status(const net::URLRequestStatus& status) { - fake_status_ = status; +const GURL& TestURLFetcher::GetUrl() const { + return fake_url_; } -void TestURLFetcher::set_was_fetched_via_proxy(bool flag) { - URLFetcher::set_was_fetched_via_proxy(flag); +const net::URLRequestStatus& TestURLFetcher::GetStatus() const { + return fake_status_; } -void TestURLFetcher::set_response_headers( - scoped_refptr<net::HttpResponseHeaders> headers) { - URLFetcher::set_response_headers(headers); +int TestURLFetcher::GetResponseCode() const { + return fake_response_code_; } -void TestURLFetcher::SetResponseString(const std::string& response) { - SetResponseDestinationForTesting(STRING); - fake_response_string_ = response; +const net::ResponseCookies& TestURLFetcher::GetCookies() const { + return fake_cookies_; } -void TestURLFetcher::SetResponseFilePath(const FilePath& path) { - SetResponseDestinationForTesting(TEMP_FILE); - fake_response_file_path_ = path; +bool TestURLFetcher::FileErrorOccurred( + base::PlatformFileError* out_error_code) const { + NOTIMPLEMENTED(); + return false; +} + +void TestURLFetcher::ReceivedContentWasMalformed() { } bool TestURLFetcher::GetResponseAsString( std::string* out_response_string) const { - if (GetResponseDestinationForTesting() != STRING) + if (fake_response_destination_ != STRING) return false; *out_response_string = fake_response_string_; @@ -80,23 +164,50 @@ bool TestURLFetcher::GetResponseAsString( bool TestURLFetcher::GetResponseAsFilePath( bool take_ownership, FilePath* out_response_path) const { - if (GetResponseDestinationForTesting() != TEMP_FILE) + if (fake_response_destination_ != TEMP_FILE) return false; *out_response_path = fake_response_file_path_; return true; } +void TestURLFetcher::set_status(const net::URLRequestStatus& status) { + fake_status_ = status; +} + +void TestURLFetcher::set_was_fetched_via_proxy(bool flag) { + fake_was_fetched_via_proxy_ = flag; +} + +void TestURLFetcher::set_response_headers( + scoped_refptr<net::HttpResponseHeaders> headers) { + fake_response_headers_ = headers; +} + +void TestURLFetcher::set_backoff_delay(base::TimeDelta backoff_delay) { + fake_backoff_delay_ = backoff_delay; +} + +void TestURLFetcher::SetResponseString(const std::string& response) { + fake_response_destination_ = STRING; + fake_response_string_ = response; +} + +void TestURLFetcher::SetResponseFilePath(const FilePath& path) { + fake_response_destination_ = TEMP_FILE; + fake_response_file_path_ = path; +} + TestURLFetcherFactory::TestURLFetcherFactory() : ScopedURLFetcherFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { } TestURLFetcherFactory::~TestURLFetcherFactory() {} -URLFetcher* TestURLFetcherFactory::CreateURLFetcher( +content::URLFetcher* TestURLFetcherFactory::CreateURLFetcher( int id, const GURL& url, - URLFetcher::RequestType request_type, + content::URLFetcher::RequestType request_type, content::URLFetcherDelegate* d) { TestURLFetcher* fetcher = new TestURLFetcher(id, url, request_type, d); fetchers_[id] = fetcher; @@ -114,36 +225,20 @@ void TestURLFetcherFactory::RemoveFetcherFromMap(int id) { fetchers_.erase(i); } -const GURL& TestURLFetcher::GetUrl() const { - return fake_url_; -} - -const net::URLRequestStatus& TestURLFetcher::GetStatus() const { - return fake_status_; -} - -int TestURLFetcher::GetResponseCode() const { - return fake_response_code_; -} - -const net::ResponseCookies& TestURLFetcher::GetCookies() const { - return fake_cookies_; -} - // This class is used by the FakeURLFetcherFactory below. -class FakeURLFetcher : public URLFetcher { +class FakeURLFetcher : public TestURLFetcher { public: // Normal URL fetcher constructor but also takes in a pre-baked response. FakeURLFetcher(const GURL& url, RequestType request_type, content::URLFetcherDelegate* d, const std::string& response_data, bool success) - : URLFetcher(url, request_type, d), - url_(url), - response_data_(response_data), - success_(success), - status_(success ? net::URLRequestStatus::SUCCESS : - net::URLRequestStatus::FAILED, 0), + : TestURLFetcher(0, url, request_type, d), ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { + set_status(net::URLRequestStatus( + success ? net::URLRequestStatus::SUCCESS :net::URLRequestStatus::FAILED, + 0)); + set_response_code(success ? 200 : 500); + SetResponseString(response_data); } // Start the request. This will call the given delegate asynchronously @@ -154,28 +249,8 @@ class FakeURLFetcher : public URLFetcher { method_factory_.NewRunnableMethod(&FakeURLFetcher::RunDelegate)); } - // These methods are overriden so we can use the version of - // OnURLFetchComplete that only has a single URLFetcher argument. - virtual const net::ResponseCookies& GetCookies() const OVERRIDE { - return cookies_; - } - - virtual bool GetResponseAsString( - std::string* out_response_string) const OVERRIDE { - *out_response_string = response_data_; - return true; - } - - virtual int GetResponseCode() const OVERRIDE { - return success_ ? 200 : 500; - } - - virtual const net::URLRequestStatus& GetStatus() const OVERRIDE { - return status_; - } - virtual const GURL& GetUrl() const OVERRIDE { - return url_; + return TestURLFetcher::GetOriginalUrl(); } private: @@ -188,14 +263,6 @@ class FakeURLFetcher : public URLFetcher { delegate()->OnURLFetchComplete(this); } - // Pre-baked response data and flag which indicates whether the request should - // be successful or not. - GURL url_; - std::string response_data_; - bool success_; - net::URLRequestStatus status_; - net::ResponseCookies cookies_; - // Method factory used to run the delegate. ScopedRunnableMethodFactory<FakeURLFetcher> method_factory_; @@ -207,17 +274,17 @@ FakeURLFetcherFactory::FakeURLFetcherFactory() } FakeURLFetcherFactory::FakeURLFetcherFactory( - URLFetcher::Factory* default_factory) + content::URLFetcherFactory* default_factory) : ScopedURLFetcherFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)), default_factory_(default_factory) { } FakeURLFetcherFactory::~FakeURLFetcherFactory() {} -URLFetcher* FakeURLFetcherFactory::CreateURLFetcher( +content::URLFetcher* FakeURLFetcherFactory::CreateURLFetcher( int id, const GURL& url, - URLFetcher::RequestType request_type, + content::URLFetcher::RequestType request_type, content::URLFetcherDelegate* d) { FakeResponseMap::const_iterator it = fake_responses_.find(url); if (it == fake_responses_.end()) { @@ -248,10 +315,10 @@ URLFetcherFactory::URLFetcherFactory() {} URLFetcherFactory::~URLFetcherFactory() {} -URLFetcher* URLFetcherFactory::CreateURLFetcher( +content::URLFetcher* URLFetcherFactory::CreateURLFetcher( int id, const GURL& url, - URLFetcher::RequestType request_type, + content::URLFetcher::RequestType request_type, content::URLFetcherDelegate* d) { return new URLFetcher(url, request_type, d); } diff --git a/content/test/test_url_fetcher_factory.h b/content/test/test_url_fetcher_factory.h index 27e03ef..7d3cf0a 100644 --- a/content/test/test_url_fetcher_factory.h +++ b/content/test/test_url_fetcher_factory.h @@ -12,15 +12,16 @@ #include <utility> #include "base/threading/non_thread_safe.h" -#include "content/common/net/url_fetcher.h" +#include "content/public/common/url_fetcher_factory.h" #include "googleurl/src/gurl.h" +#include "net/http/http_request_headers.h" #include "net/url_request/url_request_status.h" // Changes URLFetcher's Factory for the lifetime of the object. // Note that this scoper cannot be nested (to make it even harder to misuse). class ScopedURLFetcherFactory : public base::NonThreadSafe { public: - explicit ScopedURLFetcherFactory(URLFetcher::Factory* factory); + explicit ScopedURLFetcherFactory(content::URLFetcherFactory* factory); virtual ~ScopedURLFetcherFactory(); private: @@ -52,7 +53,7 @@ class ScopedURLFetcherFactory : public base::NonThreadSafe { // might want to use the FakeUrlFetcher and FakeUrlFetcherFactory classes // below. -class TestURLFetcher : public URLFetcher { +class TestURLFetcher : public content::URLFetcher { public: TestURLFetcher(int id, const GURL& url, @@ -60,50 +61,75 @@ class TestURLFetcher : public URLFetcher { content::URLFetcherDelegate* d); virtual ~TestURLFetcher(); - // Overriden to do nothing. It is assumed the caller will notify the delegate. - virtual void Start() {} - + // content::URLFetcher implementation + virtual void SetUploadData(const std::string& upload_content_type, + const std::string& upload_content) OVERRIDE; + virtual void SetChunkedUpload( + const std::string& upload_content_type) OVERRIDE; // Overriden to cache the chunks uploaded. Caller can read back the uploaded - // chunks with the upload_data() accessor. - virtual void AppendChunkToUpload(const std::string& data, bool is_last_chunk) - OVERRIDE; - - // Unique ID in our factory. - int id() const { return id_; } + // chunks with the upload_chunks() accessor. + virtual void AppendChunkToUpload(const std::string& data, + bool is_last_chunk) OVERRIDE; + virtual void SetLoadFlags(int load_flags) OVERRIDE; + virtual int GetLoadFlags() const OVERRIDE; + virtual void SetReferrer(const std::string& referrer) OVERRIDE; + virtual void SetExtraRequestHeaders( + const std::string& extra_request_headers) OVERRIDE; + virtual void GetExtraRequestHeaders( + net::HttpRequestHeaders* headers) OVERRIDE; + virtual void SetRequestContext( + net::URLRequestContextGetter* request_context_getter) OVERRIDE; + virtual void SetAutomaticallyRetryOn5xx(bool retry) OVERRIDE; + virtual void SetMaxRetries(int max_retries) OVERRIDE; + virtual int GetMaxRetries() const OVERRIDE; + virtual base::TimeDelta GetBackoffDelay() const OVERRIDE; + virtual void SaveResponseToTemporaryFile( + scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy) OVERRIDE; + virtual net::HttpResponseHeaders* GetResponseHeaders() const OVERRIDE; + virtual net::HostPortPair GetSocketAddress() const OVERRIDE; + virtual bool WasFetchedViaProxy() const OVERRIDE; + virtual void Start() OVERRIDE; + virtual void StartWithRequestContextGetter( + net::URLRequestContextGetter* request_context_getter) OVERRIDE; // URL we were created with. Because of how we're using URLFetcher GetUrl() // always returns an empty URL. Chances are you'll want to use // GetOriginalUrl() in your tests. virtual const GURL& GetOriginalUrl() const OVERRIDE; + virtual const GURL& GetUrl() const OVERRIDE; + virtual const net::URLRequestStatus& GetStatus() const OVERRIDE; + virtual int GetResponseCode() const OVERRIDE; + virtual const net::ResponseCookies& GetCookies() const OVERRIDE; + virtual bool FileErrorOccurred( + base::PlatformFileError* out_error_code) const OVERRIDE; + virtual void ReceivedContentWasMalformed() OVERRIDE; + // Override response access functions to return fake data. + virtual bool GetResponseAsString( + std::string* out_response_string) const OVERRIDE; + virtual bool GetResponseAsFilePath( + bool take_ownership, FilePath* out_response_path) const OVERRIDE; + + // Unique ID in our factory. + int id() const { return id_; } // Returns the data uploaded on this URLFetcher. - const std::string& upload_data() const { return URLFetcher::upload_data(); } + const std::string& upload_data() const { return upload_data_; } // Returns the chunks of data uploaded on this URLFetcher. const std::list<std::string>& upload_chunks() const { return chunks_; } // Returns the delegate installed on the URLFetcher. - content::URLFetcherDelegate* delegate() const { - return URLFetcher::delegate(); - } + content::URLFetcherDelegate* delegate() const { return delegate_; } void set_url(const GURL& url) { fake_url_ = url; } - virtual const GURL& GetUrl() const OVERRIDE; - void set_status(const net::URLRequestStatus& status); - virtual const net::URLRequestStatus& GetStatus() const OVERRIDE; - void set_response_code(int response_code) { fake_response_code_ = response_code; } - virtual int GetResponseCode() const OVERRIDE; - void set_cookies(const net::ResponseCookies& c) { fake_cookies_ = c; } - virtual const net::ResponseCookies& GetCookies() const OVERRIDE; - void set_was_fetched_via_proxy(bool flag); - void set_response_headers(scoped_refptr<net::HttpResponseHeaders> headers); + void set_backoff_delay(base::TimeDelta backoff_delay); // Set string data. void SetResponseString(const std::string& response); @@ -111,16 +137,16 @@ class TestURLFetcher : public URLFetcher { // Set File data. void SetResponseFilePath(const FilePath& path); - // Override response access functions to return fake data. - virtual bool GetResponseAsString(std::string* out_response_string) const - OVERRIDE; - virtual bool GetResponseAsFilePath(bool take_ownership, - FilePath* out_response_path) const - OVERRIDE; - private: + enum ResponseDestinationType { + STRING, // Default: In a std::string + TEMP_FILE // Write to a temp file + }; + const int id_; const GURL original_url_; + content::URLFetcherDelegate* delegate_; + std::string upload_data_; std::list<std::string> chunks_; bool did_receive_last_chunk_; @@ -128,28 +154,36 @@ class TestURLFetcher : public URLFetcher { // Setting the real values is not possible, because the real class // has no setters. The data is a private member of a class defined // in a .cc file, so we can't get at it with friendship. + int fake_load_flags_; GURL fake_url_; net::URLRequestStatus fake_status_; int fake_response_code_; net::ResponseCookies fake_cookies_; + ResponseDestinationType fake_response_destination_; std::string fake_response_string_; FilePath fake_response_file_path_; + bool fake_was_fetched_via_proxy_; + scoped_refptr<net::HttpResponseHeaders> fake_response_headers_; + net::HttpRequestHeaders fake_extra_request_headers_; + int fake_max_retries_; + base::TimeDelta fake_backoff_delay_; DISALLOW_COPY_AND_ASSIGN(TestURLFetcher); }; // Simple URLFetcher::Factory method that creates TestURLFetchers. All fetchers // are registered in a map by the id passed to the create method. -class TestURLFetcherFactory : public URLFetcher::Factory, +class TestURLFetcherFactory : public content::URLFetcherFactory, public ScopedURLFetcherFactory { public: TestURLFetcherFactory(); virtual ~TestURLFetcherFactory(); - virtual URLFetcher* CreateURLFetcher(int id, - const GURL& url, - URLFetcher::RequestType request_type, - content::URLFetcherDelegate* d) OVERRIDE; + virtual content::URLFetcher* CreateURLFetcher( + int id, + const GURL& url, + content::URLFetcher::RequestType request_type, + content::URLFetcherDelegate* d) OVERRIDE; TestURLFetcher* GetFetcherByID(int id) const; void RemoveFetcherFromMap(int id); @@ -193,13 +227,13 @@ class TestURLFetcherFactory : public URLFetcher::Factory, // SomeService service; // service.Run(); // Will eventually request these two URLs. -class FakeURLFetcherFactory : public URLFetcher::Factory, +class FakeURLFetcherFactory : public content::URLFetcherFactory, public ScopedURLFetcherFactory { public: FakeURLFetcherFactory(); // FakeURLFetcherFactory that will delegate creating URLFetcher for unknown // url to the given factory. - explicit FakeURLFetcherFactory(URLFetcher::Factory* default_factory); + explicit FakeURLFetcherFactory(content::URLFetcherFactory* default_factory); virtual ~FakeURLFetcherFactory(); // If no fake response is set for the given URL this method will delegate the @@ -207,10 +241,11 @@ class FakeURLFetcherFactory : public URLFetcher::Factory, // NULL. // Otherwise, it will return a URLFetcher object which will respond with the // pre-baked response that the client has set by calling SetFakeResponse(). - virtual URLFetcher* CreateURLFetcher(int id, - const GURL& url, - URLFetcher::RequestType request_type, - content::URLFetcherDelegate* d) OVERRIDE; + virtual content::URLFetcher* CreateURLFetcher( + int id, + const GURL& url, + content::URLFetcher::RequestType request_type, + content::URLFetcherDelegate* d) OVERRIDE; // Sets the fake response for a given URL. If success is true we will serve // an HTTP/200 and an HTTP/500 otherwise. The |response_data| may be empty. @@ -225,7 +260,7 @@ class FakeURLFetcherFactory : public URLFetcher::Factory, private: typedef std::map<GURL, std::pair<std::string, bool> > FakeResponseMap; FakeResponseMap fake_responses_; - URLFetcher::Factory* default_factory_; + content::URLFetcherFactory* default_factory_; DISALLOW_COPY_AND_ASSIGN(FakeURLFetcherFactory); }; @@ -234,16 +269,17 @@ class FakeURLFetcherFactory : public URLFetcher::Factory, // URLFetcher. It can be use in conjunction with a FakeURLFetcherFactory in // integration tests to control the behavior of some requests but execute // all the other ones. -class URLFetcherFactory : public URLFetcher::Factory { +class URLFetcherFactory : public content::URLFetcherFactory { public: URLFetcherFactory(); virtual ~URLFetcherFactory(); // This method will create a real URLFetcher. - virtual URLFetcher* CreateURLFetcher(int id, - const GURL& url, - URLFetcher::RequestType request_type, - content::URLFetcherDelegate* d) OVERRIDE; + virtual content::URLFetcher* CreateURLFetcher( + int id, + const GURL& url, + content::URLFetcher::RequestType request_type, + content::URLFetcherDelegate* d) OVERRIDE; }; |