summaryrefslogtreecommitdiffstats
path: root/net/http/http_transaction_unittest.cc
diff options
context:
space:
mode:
authorerg@google.com <erg@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2011-01-26 20:23:44 +0000
committererg@google.com <erg@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2011-01-26 20:23:44 +0000
commitd2db029ab5a3ee3d103d71447467f7f0074869dc (patch)
tree38d2cc560189c0d79493460051764e5a3a5bbd4c /net/http/http_transaction_unittest.cc
parent48f1e5cb62b299a83d7ae5f897eca0a587abf3f0 (diff)
downloadchromium_src-d2db029ab5a3ee3d103d71447467f7f0074869dc.zip
chromium_src-d2db029ab5a3ee3d103d71447467f7f0074869dc.tar.gz
chromium_src-d2db029ab5a3ee3d103d71447467f7f0074869dc.tar.bz2
Clean up net unit testing code.
(Reapply r72562 with willchan's nits + locally tested shlib fixes + removing duplicate code that was partially responsible for the failure.) - Move code included from blah_unittest.h (where blah_unittest.cc has actual unittests) into their own files, often completely out-of-lining the definitions. - Remove anonymous namespaces from headers. - Reorder method declarations. - Make other unit test link against net_test_support instead of reaching in and including .h files in their targets directly. BUG=68682 TEST=compiles First Review URL: http://codereview.chromium.org/6264013 Review URL: http://codereview.chromium.org/6248021 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@72682 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/http/http_transaction_unittest.cc')
-rw-r--r--net/http/http_transaction_unittest.cc232
1 files changed, 218 insertions, 14 deletions
diff --git a/net/http/http_transaction_unittest.cc b/net/http/http_transaction_unittest.cc
index 518c1f8..fabfb01 100644
--- a/net/http/http_transaction_unittest.cc
+++ b/net/http/http_transaction_unittest.cc
@@ -1,10 +1,11 @@
-// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/http/http_transaction_unittest.h"
-#include "base/hash_tables.h"
+#include <algorithm>
+
#include "base/message_loop.h"
#include "base/string_util.h"
#include "net/base/net_errors.h"
@@ -16,6 +17,11 @@
#include "net/http/http_transaction.h"
#include "testing/gtest/include/gtest/gtest.h"
+namespace {
+typedef base::hash_map<std::string, const MockTransaction*> MockTransactionMap;
+static MockTransactionMap mock_transactions;
+} // namespace
+
//-----------------------------------------------------------------------------
// mock transaction data
@@ -104,18 +110,6 @@ static const MockTransaction* const kBuiltinMockTransactions[] = {
&kRangeGET_Transaction
};
-typedef base::hash_map<std::string, const MockTransaction*>
-MockTransactionMap;
-static MockTransactionMap mock_transactions;
-
-void AddMockTransaction(const MockTransaction* trans) {
- mock_transactions[GURL(trans->url).spec()] = trans;
-}
-
-void RemoveMockTransaction(const MockTransaction* trans) {
- mock_transactions.erase(GURL(trans->url).spec());
-}
-
const MockTransaction* FindMockTransaction(const GURL& url) {
// look for overrides:
MockTransactionMap::const_iterator it = mock_transactions.find(url.spec());
@@ -130,12 +124,222 @@ const MockTransaction* FindMockTransaction(const GURL& url) {
return NULL;
}
+void AddMockTransaction(const MockTransaction* trans) {
+ mock_transactions[GURL(trans->url).spec()] = trans;
+}
+
+void RemoveMockTransaction(const MockTransaction* trans) {
+ mock_transactions.erase(GURL(trans->url).spec());
+}
+
+MockHttpRequest::MockHttpRequest(const MockTransaction& t) {
+ url = GURL(t.url);
+ method = t.method;
+ extra_headers.AddHeadersFromString(t.request_headers);
+ load_flags = t.load_flags;
+}
//-----------------------------------------------------------------------------
// static
int TestTransactionConsumer::quit_counter_ = 0;
+TestTransactionConsumer::TestTransactionConsumer(
+ net::HttpTransactionFactory* factory)
+ : state_(IDLE),
+ trans_(NULL),
+ error_(net::OK) {
+ // Disregard the error code.
+ factory->CreateTransaction(&trans_);
+ ++quit_counter_;
+}
+
+TestTransactionConsumer::~TestTransactionConsumer() {
+}
+
+void TestTransactionConsumer::Start(const net::HttpRequestInfo* request,
+ const net::BoundNetLog& net_log) {
+ state_ = STARTING;
+ int result = trans_->Start(request, this, net_log);
+ if (result != net::ERR_IO_PENDING)
+ DidStart(result);
+}
+
+void TestTransactionConsumer::DidStart(int result) {
+ if (result != net::OK) {
+ DidFinish(result);
+ } else {
+ Read();
+ }
+}
+
+void TestTransactionConsumer::DidRead(int result) {
+ if (result <= 0) {
+ DidFinish(result);
+ } else {
+ content_.append(read_buf_->data(), result);
+ Read();
+ }
+}
+
+void TestTransactionConsumer::DidFinish(int result) {
+ state_ = DONE;
+ error_ = result;
+ if (--quit_counter_ == 0)
+ MessageLoop::current()->Quit();
+}
+
+void TestTransactionConsumer::Read() {
+ state_ = READING;
+ read_buf_ = new net::IOBuffer(1024);
+ int result = trans_->Read(read_buf_, 1024, this);
+ if (result != net::ERR_IO_PENDING)
+ DidRead(result);
+}
+
+void TestTransactionConsumer::RunWithParams(const Tuple1<int>& params) {
+ int result = params.a;
+ switch (state_) {
+ case STARTING:
+ DidStart(result);
+ break;
+ case READING:
+ DidRead(result);
+ break;
+ default:
+ NOTREACHED();
+ }
+}
+
+
+MockNetworkTransaction::MockNetworkTransaction() :
+ ALLOW_THIS_IN_INITIALIZER_LIST(task_factory_(this)), data_cursor_(0) {
+}
+
+MockNetworkTransaction::~MockNetworkTransaction() {}
+
+int MockNetworkTransaction::Start(const net::HttpRequestInfo* request,
+ net::CompletionCallback* callback,
+ const net::BoundNetLog& net_log) {
+ const MockTransaction* t = FindMockTransaction(request->url);
+ if (!t)
+ return net::ERR_FAILED;
+
+ std::string resp_status = t->status;
+ std::string resp_headers = t->response_headers;
+ std::string resp_data = t->data;
+ if (t->handler)
+ (t->handler)(request, &resp_status, &resp_headers, &resp_data);
+
+ std::string header_data = base::StringPrintf(
+ "%s\n%s\n", resp_status.c_str(), resp_headers.c_str());
+ std::replace(header_data.begin(), header_data.end(), '\n', '\0');
+
+ response_.request_time = base::Time::Now();
+ if (!t->request_time.is_null())
+ response_.request_time = t->request_time;
+
+ response_.was_cached = false;
+
+ response_.response_time = base::Time::Now();
+ if (!t->response_time.is_null())
+ response_.response_time = t->response_time;
+
+ response_.headers = new net::HttpResponseHeaders(header_data);
+ response_.ssl_info.cert_status = t->cert_status;
+ data_ = resp_data;
+ test_mode_ = t->test_mode;
+
+ if (test_mode_ & TEST_MODE_SYNC_NET_START)
+ return net::OK;
+
+ CallbackLater(callback, net::OK);
+ return net::ERR_IO_PENDING;
+}
+
+int MockNetworkTransaction::RestartIgnoringLastError(
+ net::CompletionCallback* callback) {
+ return net::ERR_FAILED;
+}
+
+int MockNetworkTransaction::RestartWithCertificate(
+ net::X509Certificate* client_cert,
+ net::CompletionCallback* callback) {
+ return net::ERR_FAILED;
+}
+
+int MockNetworkTransaction::RestartWithAuth(const string16& username,
+ const string16& password,
+ net::CompletionCallback* callback) {
+ return net::ERR_FAILED;
+}
+
+bool MockNetworkTransaction::IsReadyToRestartForAuth() {
+ return false;
+}
+
+int MockNetworkTransaction::Read(net::IOBuffer* buf, int buf_len,
+ net::CompletionCallback* callback) {
+ int data_len = static_cast<int>(data_.size());
+ int num = std::min(buf_len, data_len - data_cursor_);
+ if (num) {
+ memcpy(buf->data(), data_.data() + data_cursor_, num);
+ data_cursor_ += num;
+ }
+ if (test_mode_ & TEST_MODE_SYNC_NET_READ)
+ return num;
+
+ CallbackLater(callback, num);
+ return net::ERR_IO_PENDING;
+}
+
+void MockNetworkTransaction::StopCaching() {}
+
+const net::HttpResponseInfo* MockNetworkTransaction::GetResponseInfo() const {
+ return &response_;
+}
+
+net::LoadState MockNetworkTransaction::GetLoadState() const {
+ if (data_cursor_)
+ return net::LOAD_STATE_READING_RESPONSE;
+ return net::LOAD_STATE_IDLE;
+}
+
+uint64 MockNetworkTransaction::GetUploadProgress() const {
+ return 0;
+}
+
+void MockNetworkTransaction::CallbackLater(net::CompletionCallback* callback,
+ int result) {
+ MessageLoop::current()->PostTask(FROM_HERE, task_factory_.NewRunnableMethod(
+ &MockNetworkTransaction::RunCallback, callback, result));
+}
+
+void MockNetworkTransaction::RunCallback(net::CompletionCallback* callback,
+ int result) {
+ callback->Run(result);
+}
+
+MockNetworkLayer::MockNetworkLayer() : transaction_count_(0) {}
+
+MockNetworkLayer::~MockNetworkLayer() {}
+
+int MockNetworkLayer::CreateTransaction(
+ scoped_ptr<net::HttpTransaction>* trans) {
+ transaction_count_++;
+ trans->reset(new MockNetworkTransaction());
+ return net::OK;
+}
+
+net::HttpCache* MockNetworkLayer::GetCache() {
+ return NULL;
+}
+
+net::HttpNetworkSession* MockNetworkLayer::GetSession() {
+ return NULL;
+}
+
+void MockNetworkLayer::Suspend(bool suspend) {}
//-----------------------------------------------------------------------------
// helpers