summaryrefslogtreecommitdiffstats
path: root/net
diff options
context:
space:
mode:
authorprimiano@chromium.org <primiano@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-07-12 11:00:33 +0000
committerprimiano@chromium.org <primiano@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-07-12 11:00:33 +0000
commitb26305b415e6f241a906e4f3bfc781187198b5d0 (patch)
treee996ff28c29625f800d3051de52f10608ccf4fa8 /net
parentf6efc0abc8ce4d7292d56058f37837b1696d050b (diff)
downloadchromium_src-b26305b415e6f241a906e4f3bfc781187198b5d0.zip
chromium_src-b26305b415e6f241a906e4f3bfc781187198b5d0.tar.gz
chromium_src-b26305b415e6f241a906e4f3bfc781187198b5d0.tar.bz2
Introduced TestURLFetcher::DelegateForTests interface to allow event-driven end-to-end tests of classes using URLFetcher.
BUG=116954 TEST=none. Review URL: https://chromiumcodereview.appspot.com/10702141 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@146315 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net')
-rw-r--r--net/url_request/test_url_fetcher_factory.cc20
-rw-r--r--net/url_request/test_url_fetcher_factory.h25
2 files changed, 44 insertions, 1 deletions
diff --git a/net/url_request/test_url_fetcher_factory.cc b/net/url_request/test_url_fetcher_factory.cc
index 03169f8..b191e12 100644
--- a/net/url_request/test_url_fetcher_factory.cc
+++ b/net/url_request/test_url_fetcher_factory.cc
@@ -35,6 +35,7 @@ TestURLFetcher::TestURLFetcher(int id,
: id_(id),
original_url_(url),
delegate_(d),
+ delegate_for_tests_(NULL),
did_receive_last_chunk_(false),
fake_load_flags_(0),
fake_response_code_(-1),
@@ -44,6 +45,8 @@ TestURLFetcher::TestURLFetcher(int id,
}
TestURLFetcher::~TestURLFetcher() {
+ if (delegate_for_tests_)
+ delegate_for_tests_->OnRequestEnd(id_);
}
void TestURLFetcher::SetUploadData(const std::string& upload_content_type,
@@ -59,6 +62,8 @@ void TestURLFetcher::AppendChunkToUpload(const std::string& data,
DCHECK(!did_receive_last_chunk_);
did_receive_last_chunk_ = is_last_chunk;
chunks_.push_back(data);
+ if (delegate_for_tests_)
+ delegate_for_tests_->OnChunkUpload(id_);
}
void TestURLFetcher::SetLoadFlags(int load_flags) {
@@ -142,6 +147,8 @@ bool TestURLFetcher::WasFetchedViaProxy() const {
void TestURLFetcher::Start() {
// Overriden to do nothing. It is assumed the caller will notify the delegate.
+ if (delegate_for_tests_)
+ delegate_for_tests_->OnRequestStart(id_);
}
const GURL& TestURLFetcher::GetOriginalURL() const {
@@ -208,6 +215,10 @@ void TestURLFetcher::set_backoff_delay(base::TimeDelta backoff_delay) {
fake_backoff_delay_ = backoff_delay;
}
+void TestURLFetcher::SetDelegateForTests(DelegateForTests* delegate_for_tests) {
+ delegate_for_tests_ = delegate_for_tests;
+}
+
void TestURLFetcher::SetResponseString(const std::string& response) {
fake_response_destination_ = STRING;
fake_response_string_ = response;
@@ -219,7 +230,8 @@ void TestURLFetcher::SetResponseFilePath(const FilePath& path) {
}
TestURLFetcherFactory::TestURLFetcherFactory()
- : ScopedURLFetcherFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
+ : ScopedURLFetcherFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
+ delegate_for_tests_(NULL) {
}
TestURLFetcherFactory::~TestURLFetcherFactory() {}
@@ -230,6 +242,7 @@ URLFetcher* TestURLFetcherFactory::CreateURLFetcher(
URLFetcher::RequestType request_type,
URLFetcherDelegate* d) {
TestURLFetcher* fetcher = new TestURLFetcher(id, url, d);
+ fetcher->SetDelegateForTests(delegate_for_tests_);
fetchers_[id] = fetcher;
return fetcher;
}
@@ -245,6 +258,11 @@ void TestURLFetcherFactory::RemoveFetcherFromMap(int id) {
fetchers_.erase(i);
}
+void TestURLFetcherFactory::SetDelegateForTests(
+ TestURLFetcherDelegateForTests* delegate_for_tests) {
+ delegate_for_tests_ = delegate_for_tests;
+}
+
// This class is used by the FakeURLFetcherFactory below.
class FakeURLFetcher : public TestURLFetcher {
public:
diff --git a/net/url_request/test_url_fetcher_factory.h b/net/url_request/test_url_fetcher_factory.h
index 601ce04..f067e97 100644
--- a/net/url_request/test_url_fetcher_factory.h
+++ b/net/url_request/test_url_fetcher_factory.h
@@ -58,6 +58,22 @@ class ScopedURLFetcherFactory : public base::NonThreadSafe {
class TestURLFetcher : public URLFetcher {
public:
+ // Interface for tests to intercept production code classes using URLFetcher.
+ // Allows even-driven mock server classes to analyze the correctness of
+ // requests / uploads events and forge responses back at the right moment.
+ class DelegateForTests {
+ public:
+ // Callback issued correspondingly to the call to the |Start()| method.
+ virtual void OnRequestStart(int fetcher_id) = 0;
+
+ // Callback issued correspondingly to the call to |AppendChunkToUpload|.
+ // Uploaded chunks can be retrieved with the |upload_chunks()| getter.
+ virtual void OnChunkUpload(int fetcher_id) = 0;
+
+ // Callback issued correspondingly to the destructor.
+ virtual void OnRequestEnd(int fetcher_id) = 0;
+ };
+
TestURLFetcher(int id,
const GURL& url,
URLFetcherDelegate* d);
@@ -128,6 +144,9 @@ class TestURLFetcher : public URLFetcher {
// Returns the chunks of data uploaded on this URLFetcher.
const std::list<std::string>& upload_chunks() const { return chunks_; }
+ // Checks whether the last call to |AppendChunkToUpload(...)| was final.
+ bool did_receive_last_chunk() const { return did_receive_last_chunk_; }
+
// Returns the delegate installed on the URLFetcher.
URLFetcherDelegate* delegate() const { return delegate_; }
@@ -140,6 +159,7 @@ class TestURLFetcher : public URLFetcher {
void set_was_fetched_via_proxy(bool flag);
void set_response_headers(scoped_refptr<HttpResponseHeaders> headers);
void set_backoff_delay(base::TimeDelta backoff_delay);
+ void SetDelegateForTests(DelegateForTests* delegate_for_tests);
// Set string data.
void SetResponseString(const std::string& response);
@@ -156,6 +176,7 @@ class TestURLFetcher : public URLFetcher {
const int id_;
const GURL original_url_;
URLFetcherDelegate* delegate_;
+ DelegateForTests* delegate_for_tests_;
std::string upload_data_;
std::list<std::string> chunks_;
bool did_receive_last_chunk_;
@@ -181,6 +202,8 @@ class TestURLFetcher : public URLFetcher {
DISALLOW_COPY_AND_ASSIGN(TestURLFetcher);
};
+typedef TestURLFetcher::DelegateForTests TestURLFetcherDelegateForTests;
+
// Simple URLFetcherFactory method that creates TestURLFetchers. All fetchers
// are registered in a map by the id passed to the create method.
class TestURLFetcherFactory : public URLFetcherFactory,
@@ -196,11 +219,13 @@ class TestURLFetcherFactory : public URLFetcherFactory,
URLFetcherDelegate* d) OVERRIDE;
TestURLFetcher* GetFetcherByID(int id) const;
void RemoveFetcherFromMap(int id);
+ void SetDelegateForTests(TestURLFetcherDelegateForTests* delegate_for_tests);
private:
// Maps from id passed to create to the returned URLFetcher.
typedef std::map<int, TestURLFetcher*> Fetchers;
Fetchers fetchers_;
+ TestURLFetcherDelegateForTests* delegate_for_tests_;
DISALLOW_COPY_AND_ASSIGN(TestURLFetcherFactory);
};