blob: f11cb656a9c372d94858c70ed960ad29a97e504a [file] [log] [blame]
// Copyright (c) 2012 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 <string>
#include <vector>
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/memory/scoped_vector.h"
#include "base/run_loop.h"
#include "base/stl_util.h"
#include "base/strings/string_piece.h"
#include "base/test/test_file_util.h"
#include "net/base/auth.h"
#include "net/base/net_log_unittest.h"
#include "net/base/request_priority.h"
#include "net/base/upload_bytes_element_reader.h"
#include "net/base/upload_data_stream.h"
#include "net/base/upload_file_element_reader.h"
#include "net/http/http_network_session_peer.h"
#include "net/http/http_network_transaction.h"
#include "net/http/http_server_properties.h"
#include "net/http/http_transaction_test_util.h"
#include "net/socket/client_socket_pool_base.h"
#include "net/socket/next_proto.h"
#include "net/spdy/buffered_spdy_framer.h"
#include "net/spdy/spdy_http_stream.h"
#include "net/spdy/spdy_http_utils.h"
#include "net/spdy/spdy_session.h"
#include "net/spdy/spdy_session_pool.h"
#include "net/spdy/spdy_test_util_common.h"
#include "net/spdy/spdy_test_utils.h"
#include "net/ssl/ssl_connection_status_flags.h"
#include "net/url_request/url_request_test_util.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/platform_test.h"
//-----------------------------------------------------------------------------
namespace net {
namespace {
using testing::Each;
using testing::Eq;
const char kRequestUrl[] = "http://www.google.com/";
enum SpdyNetworkTransactionTestSSLType {
SPDYNPN,
SPDYNOSSL,
SPDYSSL,
};
struct SpdyNetworkTransactionTestParams {
SpdyNetworkTransactionTestParams()
: protocol(kProtoSPDY3),
ssl_type(SPDYNPN) {}
SpdyNetworkTransactionTestParams(
NextProto protocol,
SpdyNetworkTransactionTestSSLType ssl_type)
: protocol(protocol),
ssl_type(ssl_type) {}
NextProto protocol;
SpdyNetworkTransactionTestSSLType ssl_type;
};
void UpdateSpdySessionDependencies(
SpdyNetworkTransactionTestParams test_params,
SpdySessionDependencies* session_deps) {
switch (test_params.ssl_type) {
case SPDYNPN:
session_deps->http_server_properties.SetAlternateProtocol(
HostPortPair("www.google.com", 80), 443,
AlternateProtocolFromNextProto(test_params.protocol));
session_deps->use_alternate_protocols = true;
session_deps->next_protos = SpdyNextProtos();
break;
case SPDYNOSSL:
session_deps->force_spdy_over_ssl = false;
session_deps->force_spdy_always = true;
break;
case SPDYSSL:
session_deps->force_spdy_over_ssl = true;
session_deps->force_spdy_always = true;
break;
default:
NOTREACHED();
}
}
SpdySessionDependencies* CreateSpdySessionDependencies(
SpdyNetworkTransactionTestParams test_params) {
SpdySessionDependencies* session_deps =
new SpdySessionDependencies(test_params.protocol);
UpdateSpdySessionDependencies(test_params, session_deps);
return session_deps;
}
SpdySessionDependencies* CreateSpdySessionDependencies(
SpdyNetworkTransactionTestParams test_params,
ProxyService* proxy_service) {
SpdySessionDependencies* session_deps =
new SpdySessionDependencies(test_params.protocol, proxy_service);
UpdateSpdySessionDependencies(test_params, session_deps);
return session_deps;
}
} // namespace
class SpdyNetworkTransactionTest
: public ::testing::TestWithParam<SpdyNetworkTransactionTestParams> {
protected:
SpdyNetworkTransactionTest() : spdy_util_(GetParam().protocol) {
LOG(INFO) << __FUNCTION__;
}
virtual ~SpdyNetworkTransactionTest() {
LOG(INFO) << __FUNCTION__;
// UploadDataStream posts deletion tasks back to the message loop on
// destruction.
upload_data_stream_.reset();
base::RunLoop().RunUntilIdle();
LOG(INFO) << __FUNCTION__;
}
virtual void SetUp() {
LOG(INFO) << __FUNCTION__;
google_get_request_initialized_ = false;
google_post_request_initialized_ = false;
google_chunked_post_request_initialized_ = false;
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
LOG(INFO) << __FUNCTION__;
}
struct TransactionHelperResult {
int rv;
std::string status_line;
std::string response_data;
HttpResponseInfo response_info;
};
// A helper class that handles all the initial npn/ssl setup.
class NormalSpdyTransactionHelper {
public:
NormalSpdyTransactionHelper(const HttpRequestInfo& request,
RequestPriority priority,
const BoundNetLog& log,
SpdyNetworkTransactionTestParams test_params,
SpdySessionDependencies* session_deps)
: request_(request),
priority_(priority),
session_deps_(session_deps == NULL ?
CreateSpdySessionDependencies(test_params) :
session_deps),
session_(SpdySessionDependencies::SpdyCreateSession(
session_deps_.get())),
log_(log),
test_params_(test_params),
deterministic_(false),
spdy_enabled_(true) {
switch (test_params_.ssl_type) {
case SPDYNOSSL:
case SPDYSSL:
port_ = 80;
break;
case SPDYNPN:
port_ = 443;
break;
default:
NOTREACHED();
}
}
~NormalSpdyTransactionHelper() {
// Any test which doesn't close the socket by sending it an EOF will
// have a valid session left open, which leaks the entire session pool.
// This is just fine - in fact, some of our tests intentionally do this
// so that we can check consistency of the SpdySessionPool as the test
// finishes. If we had put an EOF on the socket, the SpdySession would
// have closed and we wouldn't be able to check the consistency.
// Forcefully close existing sessions here.
session()->spdy_session_pool()->CloseAllSessions();
}
void SetDeterministic() {
session_ = SpdySessionDependencies::SpdyCreateSessionDeterministic(
session_deps_.get());
deterministic_ = true;
}
void SetSpdyDisabled() {
spdy_enabled_ = false;
port_ = 80;
}
void RunPreTestSetup() {
LOG(INFO) << __FUNCTION__;
if (!session_deps_.get())
session_deps_.reset(CreateSpdySessionDependencies(test_params_));
if (!session_.get()) {
session_ = SpdySessionDependencies::SpdyCreateSession(
session_deps_.get());
}
// We're now ready to use SSL-npn SPDY.
trans_.reset(new HttpNetworkTransaction(priority_, session_.get()));
LOG(INFO) << __FUNCTION__;
}
// Start the transaction, read some data, finish.
void RunDefaultTest() {
LOG(INFO) << __FUNCTION__;
if (!StartDefaultTest())
return;
FinishDefaultTest();
LOG(INFO) << __FUNCTION__;
}
bool StartDefaultTest() {
output_.rv = trans_->Start(&request_, callback_.callback(), log_);
// We expect an IO Pending or some sort of error.
EXPECT_LT(output_.rv, 0);
return output_.rv == ERR_IO_PENDING;
}
void FinishDefaultTest() {
output_.rv = callback_.WaitForResult();
if (output_.rv != OK) {
session_->spdy_session_pool()->CloseCurrentSessions(net::ERR_ABORTED);
return;
}
// Verify responses.
const HttpResponseInfo* response = trans_->GetResponseInfo();
ASSERT_TRUE(response != NULL);
ASSERT_TRUE(response->headers.get() != NULL);
EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine());
EXPECT_EQ(spdy_enabled_, response->was_fetched_via_spdy);
if (HttpStreamFactory::spdy_enabled()) {
EXPECT_EQ(
HttpResponseInfo::ConnectionInfoFromNextProto(
test_params_.protocol),
response->connection_info);
} else {
EXPECT_EQ(HttpResponseInfo::CONNECTION_INFO_HTTP1,
response->connection_info);
}
if (test_params_.ssl_type == SPDYNPN && spdy_enabled_) {
EXPECT_TRUE(response->was_npn_negotiated);
} else {
EXPECT_TRUE(!response->was_npn_negotiated);
}
// If SPDY is not enabled, a HTTP request should not be diverted
// over a SSL session.
if (!spdy_enabled_) {
EXPECT_EQ(request_.url.SchemeIs("https"),
response->was_npn_negotiated);
}
EXPECT_EQ("127.0.0.1", response->socket_address.host());
EXPECT_EQ(port_, response->socket_address.port());
output_.status_line = response->headers->GetStatusLine();
output_.response_info = *response; // Make a copy so we can verify.
output_.rv = ReadTransaction(trans_.get(), &output_.response_data);
}
// Most tests will want to call this function. In particular, the MockReads
// should end with an empty read, and that read needs to be processed to
// ensure proper deletion of the spdy_session_pool.
void VerifyDataConsumed() {
for (DataVector::iterator it = data_vector_.begin();
it != data_vector_.end(); ++it) {
EXPECT_TRUE((*it)->at_read_eof()) << "Read count: "
<< (*it)->read_count()
<< " Read index: "
<< (*it)->read_index();
EXPECT_TRUE((*it)->at_write_eof()) << "Write count: "
<< (*it)->write_count()
<< " Write index: "
<< (*it)->write_index();
}
}
// Occasionally a test will expect to error out before certain reads are
// processed. In that case we want to explicitly ensure that the reads were
// not processed.
void VerifyDataNotConsumed() {
for (DataVector::iterator it = data_vector_.begin();
it != data_vector_.end(); ++it) {
EXPECT_TRUE(!(*it)->at_read_eof()) << "Read count: "
<< (*it)->read_count()
<< " Read index: "
<< (*it)->read_index();
EXPECT_TRUE(!(*it)->at_write_eof()) << "Write count: "
<< (*it)->write_count()
<< " Write index: "
<< (*it)->write_index();
}
}
void RunToCompletion(StaticSocketDataProvider* data) {
RunPreTestSetup();
AddData(data);
RunDefaultTest();
VerifyDataConsumed();
}
void RunToCompletionWithSSLData(
StaticSocketDataProvider* data,
scoped_ptr<SSLSocketDataProvider> ssl_provider) {
RunPreTestSetup();
AddDataWithSSLSocketDataProvider(data, ssl_provider.Pass());
RunDefaultTest();
VerifyDataConsumed();
}
void AddData(StaticSocketDataProvider* data) {
scoped_ptr<SSLSocketDataProvider> ssl_provider(
new SSLSocketDataProvider(ASYNC, OK));
AddDataWithSSLSocketDataProvider(data, ssl_provider.Pass());
}
void AddDataWithSSLSocketDataProvider(
StaticSocketDataProvider* data,
scoped_ptr<SSLSocketDataProvider> ssl_provider) {
DCHECK(!deterministic_);
data_vector_.push_back(data);
if (test_params_.ssl_type == SPDYNPN)
ssl_provider->SetNextProto(test_params_.protocol);
if (test_params_.ssl_type == SPDYNPN ||
test_params_.ssl_type == SPDYSSL) {
session_deps_->socket_factory->AddSSLSocketDataProvider(
ssl_provider.get());
}
ssl_vector_.push_back(ssl_provider.release());
session_deps_->socket_factory->AddSocketDataProvider(data);
if (test_params_.ssl_type == SPDYNPN) {
MockConnect never_finishing_connect(SYNCHRONOUS, ERR_IO_PENDING);
StaticSocketDataProvider* hanging_non_alternate_protocol_socket =
new StaticSocketDataProvider(NULL, 0, NULL, 0);
hanging_non_alternate_protocol_socket->set_connect_data(
never_finishing_connect);
session_deps_->socket_factory->AddSocketDataProvider(
hanging_non_alternate_protocol_socket);
alternate_vector_.push_back(hanging_non_alternate_protocol_socket);
}
}
void AddDeterministicData(DeterministicSocketData* data) {
DCHECK(deterministic_);
data_vector_.push_back(data);
SSLSocketDataProvider* ssl_provider =
new SSLSocketDataProvider(ASYNC, OK);
if (test_params_.ssl_type == SPDYNPN)
ssl_provider->SetNextProto(test_params_.protocol);
ssl_vector_.push_back(ssl_provider);
if (test_params_.ssl_type == SPDYNPN ||
test_params_.ssl_type == SPDYSSL) {
session_deps_->deterministic_socket_factory->
AddSSLSocketDataProvider(ssl_provider);
}
session_deps_->deterministic_socket_factory->AddSocketDataProvider(data);
if (test_params_.ssl_type == SPDYNPN) {
MockConnect never_finishing_connect(SYNCHRONOUS, ERR_IO_PENDING);
DeterministicSocketData* hanging_non_alternate_protocol_socket =
new DeterministicSocketData(NULL, 0, NULL, 0);
hanging_non_alternate_protocol_socket->set_connect_data(
never_finishing_connect);
session_deps_->deterministic_socket_factory->AddSocketDataProvider(
hanging_non_alternate_protocol_socket);
alternate_deterministic_vector_.push_back(
hanging_non_alternate_protocol_socket);
}
}
void SetSession(const scoped_refptr<HttpNetworkSession>& session) {
session_ = session;
}
HttpNetworkTransaction* trans() { return trans_.get(); }
void ResetTrans() { trans_.reset(); }
TransactionHelperResult& output() { return output_; }
const HttpRequestInfo& request() const { return request_; }
const scoped_refptr<HttpNetworkSession>& session() const {
return session_;
}
scoped_ptr<SpdySessionDependencies>& session_deps() {
return session_deps_;
}
int port() const { return port_; }
SpdyNetworkTransactionTestParams test_params() const {
return test_params_;
}
private:
typedef std::vector<StaticSocketDataProvider*> DataVector;
typedef ScopedVector<SSLSocketDataProvider> SSLVector;
typedef ScopedVector<StaticSocketDataProvider> AlternateVector;
typedef ScopedVector<DeterministicSocketData> AlternateDeterministicVector;
HttpRequestInfo request_;
RequestPriority priority_;
scoped_ptr<SpdySessionDependencies> session_deps_;
scoped_refptr<HttpNetworkSession> session_;
TransactionHelperResult output_;
scoped_ptr<StaticSocketDataProvider> first_transaction_;
SSLVector ssl_vector_;
TestCompletionCallback callback_;
scoped_ptr<HttpNetworkTransaction> trans_;
scoped_ptr<HttpNetworkTransaction> trans_http_;
DataVector data_vector_;
AlternateVector alternate_vector_;
AlternateDeterministicVector alternate_deterministic_vector_;
const BoundNetLog& log_;
SpdyNetworkTransactionTestParams test_params_;
int port_;
bool deterministic_;
bool spdy_enabled_;
};
void ConnectStatusHelperWithExpectedStatus(const MockRead& status,
int expected_status);
void ConnectStatusHelper(const MockRead& status);
const HttpRequestInfo& CreateGetPushRequest() {
google_get_push_request_.method = "GET";
google_get_push_request_.url = GURL("http://www.google.com/foo.dat");
google_get_push_request_.load_flags = 0;
return google_get_push_request_;
}
const HttpRequestInfo& CreateGetRequest() {
if (!google_get_request_initialized_) {
google_get_request_.method = "GET";
google_get_request_.url = GURL(kDefaultURL);
google_get_request_.load_flags = 0;
google_get_request_initialized_ = true;
}
return google_get_request_;
}
const HttpRequestInfo& CreateGetRequestWithUserAgent() {
if (!google_get_request_initialized_) {
google_get_request_.method = "GET";
google_get_request_.url = GURL(kDefaultURL);
google_get_request_.load_flags = 0;
google_get_request_.extra_headers.SetHeader("User-Agent", "Chrome");
google_get_request_initialized_ = true;
}
return google_get_request_;
}
const HttpRequestInfo& CreatePostRequest() {
if (!google_post_request_initialized_) {
ScopedVector<UploadElementReader> element_readers;
element_readers.push_back(
new UploadBytesElementReader(kUploadData, kUploadDataSize));
upload_data_stream_.reset(
new UploadDataStream(element_readers.Pass(), 0));
google_post_request_.method = "POST";
google_post_request_.url = GURL(kDefaultURL);
google_post_request_.upload_data_stream = upload_data_stream_.get();
google_post_request_initialized_ = true;
}
return google_post_request_;
}
const HttpRequestInfo& CreateFilePostRequest() {
if (!google_post_request_initialized_) {
base::FilePath file_path;
CHECK(base::CreateTemporaryFileInDir(temp_dir_.path(), &file_path));
CHECK_EQ(static_cast<int>(kUploadDataSize),
base::WriteFile(file_path, kUploadData, kUploadDataSize));
ScopedVector<UploadElementReader> element_readers;
element_readers.push_back(
new UploadFileElementReader(base::MessageLoopProxy::current().get(),
file_path,
0,
kUploadDataSize,
base::Time()));
upload_data_stream_.reset(
new UploadDataStream(element_readers.Pass(), 0));
google_post_request_.method = "POST";
google_post_request_.url = GURL(kDefaultURL);
google_post_request_.upload_data_stream = upload_data_stream_.get();
google_post_request_initialized_ = true;
}
return google_post_request_;
}
const HttpRequestInfo& CreateUnreadableFilePostRequest() {
if (google_post_request_initialized_)
return google_post_request_;
base::FilePath file_path;
CHECK(base::CreateTemporaryFileInDir(temp_dir_.path(), &file_path));
CHECK_EQ(static_cast<int>(kUploadDataSize),
base::WriteFile(file_path, kUploadData, kUploadDataSize));
CHECK(file_util::MakeFileUnreadable(file_path));
ScopedVector<UploadElementReader> element_readers;
element_readers.push_back(
new UploadFileElementReader(base::MessageLoopProxy::current().get(),
file_path,
0,
kUploadDataSize,
base::Time()));
upload_data_stream_.reset(
new UploadDataStream(element_readers.Pass(), 0));
google_post_request_.method = "POST";
google_post_request_.url = GURL(kDefaultURL);
google_post_request_.upload_data_stream = upload_data_stream_.get();
google_post_request_initialized_ = true;
return google_post_request_;
}
const HttpRequestInfo& CreateComplexPostRequest() {
if (!google_post_request_initialized_) {
const int kFileRangeOffset = 1;
const int kFileRangeLength = 3;
CHECK_LT(kFileRangeOffset + kFileRangeLength, kUploadDataSize);
base::FilePath file_path;
CHECK(base::CreateTemporaryFileInDir(temp_dir_.path(), &file_path));
CHECK_EQ(static_cast<int>(kUploadDataSize),
base::WriteFile(file_path, kUploadData, kUploadDataSize));
ScopedVector<UploadElementReader> element_readers;
element_readers.push_back(
new UploadBytesElementReader(kUploadData, kFileRangeOffset));
element_readers.push_back(
new UploadFileElementReader(base::MessageLoopProxy::current().get(),
file_path,
kFileRangeOffset,
kFileRangeLength,
base::Time()));
element_readers.push_back(new UploadBytesElementReader(
kUploadData + kFileRangeOffset + kFileRangeLength,
kUploadDataSize - (kFileRangeOffset + kFileRangeLength)));
upload_data_stream_.reset(
new UploadDataStream(element_readers.Pass(), 0));
google_post_request_.method = "POST";
google_post_request_.url = GURL(kDefaultURL);
google_post_request_.upload_data_stream = upload_data_stream_.get();
google_post_request_initialized_ = true;
}
return google_post_request_;
}
const HttpRequestInfo& CreateChunkedPostRequest() {
if (!google_chunked_post_request_initialized_) {
upload_data_stream_.reset(
new UploadDataStream(UploadDataStream::CHUNKED, 0));
google_chunked_post_request_.method = "POST";
google_chunked_post_request_.url = GURL(kDefaultURL);
google_chunked_post_request_.upload_data_stream =
upload_data_stream_.get();
google_chunked_post_request_initialized_ = true;
}
return google_chunked_post_request_;
}
// Read the result of a particular transaction, knowing that we've got
// multiple transactions in the read pipeline; so as we read, we may have
// to skip over data destined for other transactions while we consume
// the data for |trans|.
int ReadResult(HttpNetworkTransaction* trans,
StaticSocketDataProvider* data,
std::string* result) {
const int kSize = 3000;
int bytes_read = 0;
scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(kSize));
TestCompletionCallback callback;
while (true) {
int rv = trans->Read(buf.get(), kSize, callback.callback());
if (rv == ERR_IO_PENDING) {
// Multiple transactions may be in the data set. Keep pulling off
// reads until we complete our callback.
while (!callback.have_result()) {
data->CompleteRead();
base::RunLoop().RunUntilIdle();
}
rv = callback.WaitForResult();
} else if (rv <= 0) {
break;
}
result->append(buf->data(), rv);
bytes_read += rv;
}
return bytes_read;
}
void VerifyStreamsClosed(const NormalSpdyTransactionHelper& helper) {
// This lengthy block is reaching into the pool to dig out the active
// session. Once we have the session, we verify that the streams are
// all closed and not leaked at this point.
const GURL& url = helper.request().url;
int port = helper.test_params().ssl_type == SPDYNPN ? 443 : 80;
HostPortPair host_port_pair(url.host(), port);
SpdySessionKey key(host_port_pair, ProxyServer::Direct(),
PRIVACY_MODE_DISABLED);
BoundNetLog log;
const scoped_refptr<HttpNetworkSession>& session = helper.session();
base::WeakPtr<SpdySession> spdy_session =
session->spdy_session_pool()->FindAvailableSession(key, log);
ASSERT_TRUE(spdy_session != NULL);
EXPECT_EQ(0u, spdy_session->num_active_streams());
EXPECT_EQ(0u, spdy_session->num_unclaimed_pushed_streams());
}
void RunServerPushTest(OrderedSocketData* data,
HttpResponseInfo* response,
HttpResponseInfo* push_response,
const std::string& expected) {
NormalSpdyTransactionHelper helper(CreateGetRequest(), DEFAULT_PRIORITY,
BoundNetLog(), GetParam(), NULL);
helper.RunPreTestSetup();
helper.AddData(data);
HttpNetworkTransaction* trans = helper.trans();
// Start the transaction with basic parameters.
TestCompletionCallback callback;
int rv = trans->Start(
&CreateGetRequest(), callback.callback(), BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
rv = callback.WaitForResult();
// Request the pushed path.
scoped_ptr<HttpNetworkTransaction> trans2(
new HttpNetworkTransaction(DEFAULT_PRIORITY, helper.session().get()));
rv = trans2->Start(
&CreateGetPushRequest(), callback.callback(), BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
base::RunLoop().RunUntilIdle();
// The data for the pushed path may be coming in more than 1 frame. Compile
// the results into a single string.
// Read the server push body.
std::string result2;
ReadResult(trans2.get(), data, &result2);
// Read the response body.
std::string result;
ReadResult(trans, data, &result);
// Verify that we consumed all test data.
EXPECT_TRUE(data->at_read_eof());
EXPECT_TRUE(data->at_write_eof());
// Verify that the received push data is same as the expected push data.
EXPECT_EQ(result2.compare(expected), 0) << "Received data: "
<< result2
<< "||||| Expected data: "
<< expected;
// Verify the SYN_REPLY.
// Copy the response info, because trans goes away.
*response = *trans->GetResponseInfo();
*push_response = *trans2->GetResponseInfo();
VerifyStreamsClosed(helper);
}
static void DeleteSessionCallback(NormalSpdyTransactionHelper* helper,
int result) {
helper->ResetTrans();
}
static void StartTransactionCallback(
const scoped_refptr<HttpNetworkSession>& session,
int result) {
scoped_ptr<HttpNetworkTransaction> trans(
new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get()));
TestCompletionCallback callback;
HttpRequestInfo request;
request.method = "GET";
request.url = GURL("http://www.google.com/");
request.load_flags = 0;
int rv = trans->Start(&request, callback.callback(), BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
callback.WaitForResult();
}
SpdyTestUtil spdy_util_;
private:
scoped_ptr<UploadDataStream> upload_data_stream_;
bool google_get_request_initialized_;
bool google_post_request_initialized_;
bool google_chunked_post_request_initialized_;
HttpRequestInfo google_get_request_;
HttpRequestInfo google_post_request_;
HttpRequestInfo google_chunked_post_request_;
HttpRequestInfo google_get_push_request_;
base::ScopedTempDir temp_dir_;
};
//-----------------------------------------------------------------------------
// All tests are run with three different connection types: SPDY after NPN
// negotiation, SPDY without SSL, and SPDY with SSL.
//
// TODO(akalin): Use ::testing::Combine() when we are able to use
// <tr1/tuple>.
INSTANTIATE_TEST_CASE_P(
Spdy,
SpdyNetworkTransactionTest,
::testing::Values(
SpdyNetworkTransactionTestParams(kProtoDeprecatedSPDY2, SPDYNOSSL),
SpdyNetworkTransactionTestParams(kProtoDeprecatedSPDY2, SPDYSSL),
SpdyNetworkTransactionTestParams(kProtoDeprecatedSPDY2, SPDYNPN),
SpdyNetworkTransactionTestParams(kProtoSPDY3, SPDYNOSSL),
SpdyNetworkTransactionTestParams(kProtoSPDY3, SPDYSSL),
SpdyNetworkTransactionTestParams(kProtoSPDY3, SPDYNPN),
SpdyNetworkTransactionTestParams(kProtoSPDY31, SPDYNOSSL),
SpdyNetworkTransactionTestParams(kProtoSPDY31, SPDYSSL),
SpdyNetworkTransactionTestParams(kProtoSPDY31, SPDYNPN),
SpdyNetworkTransactionTestParams(kProtoSPDY4, SPDYNOSSL),
SpdyNetworkTransactionTestParams(kProtoSPDY4, SPDYSSL),
SpdyNetworkTransactionTestParams(kProtoSPDY4, SPDYNPN)));
// Verify HttpNetworkTransaction constructor.
TEST_P(SpdyNetworkTransactionTest, Constructor) {
LOG(INFO) << __FUNCTION__;
scoped_ptr<SpdySessionDependencies> session_deps(
CreateSpdySessionDependencies(GetParam()));
LOG(INFO) << __FUNCTION__;
scoped_refptr<HttpNetworkSession> session(
SpdySessionDependencies::SpdyCreateSession(session_deps.get()));
LOG(INFO) << __FUNCTION__;
scoped_ptr<HttpTransaction> trans(
new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get()));
LOG(INFO) << __FUNCTION__;
}
TEST_P(SpdyNetworkTransactionTest, Get) {
LOG(INFO) << __FUNCTION__;
// Construct the request.
scoped_ptr<SpdyFrame> req(
spdy_util_.ConstructSpdyGet(NULL, 0, false, 1, LOWEST, true));
MockWrite writes[] = { CreateMockWrite(*req) };
scoped_ptr<SpdyFrame> resp(spdy_util_.ConstructSpdyGetSynReply(NULL, 0, 1));
scoped_ptr<SpdyFrame> body(spdy_util_.ConstructSpdyBodyFrame(1, true));
MockRead reads[] = {
CreateMockRead(*resp),
CreateMockRead(*body),
MockRead(ASYNC, 0, 0) // EOF
};
DelayedSocketData data(1, reads, arraysize(reads),
writes, arraysize(writes));
NormalSpdyTransactionHelper helper(CreateGetRequest(), DEFAULT_PRIORITY,
BoundNetLog(), GetParam(), NULL);
helper.RunToCompletion(&data);
TransactionHelperResult out = helper.output();
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.1 200 OK", out.status_line);
EXPECT_EQ("hello!", out.response_data);
}
TEST_P(SpdyNetworkTransactionTest, GetAtEachPriority) {
for (RequestPriority p = MINIMUM_PRIORITY; p <= MAXIMUM_PRIORITY;
p = RequestPriority(p + 1)) {
// Construct the request.
scoped_ptr<SpdyFrame> req(
spdy_util_.ConstructSpdyGet(NULL, 0, false, 1, p, true));
MockWrite writes[] = { CreateMockWrite(*req) };
SpdyPriority spdy_prio = 0;
EXPECT_TRUE(GetSpdyPriority(spdy_util_.spdy_version(), *req, &spdy_prio));
// this repeats the RequestPriority-->SpdyPriority mapping from
// SpdyFramer::ConvertRequestPriorityToSpdyPriority to make
// sure it's being done right.
if (spdy_util_.spdy_version() < SPDY3) {
switch(p) {
case HIGHEST:
EXPECT_EQ(0, spdy_prio);
break;
case MEDIUM:
EXPECT_EQ(1, spdy_prio);
break;
case LOW:
case LOWEST:
EXPECT_EQ(2, spdy_prio);
break;
case IDLE:
EXPECT_EQ(3, spdy_prio);
break;
default:
FAIL();
}
} else {
switch(p) {
case HIGHEST:
EXPECT_EQ(0, spdy_prio);
break;
case MEDIUM:
EXPECT_EQ(1, spdy_prio);
break;
case LOW:
EXPECT_EQ(2, spdy_prio);
break;
case LOWEST:
EXPECT_EQ(3, spdy_prio);
break;
case IDLE:
EXPECT_EQ(4, spdy_prio);
break;
default:
FAIL();
}
}
scoped_ptr<SpdyFrame> resp(spdy_util_.ConstructSpdyGetSynReply(NULL, 0, 1));
scoped_ptr<SpdyFrame> body(spdy_util_.ConstructSpdyBodyFrame(1, true));
MockRead reads[] = {
CreateMockRead(*resp),
CreateMockRead(*body),
MockRead(ASYNC, 0, 0) // EOF
};
DelayedSocketData data(1, reads, arraysize(reads),
writes, arraysize(writes));
HttpRequestInfo http_req = CreateGetRequest();
NormalSpdyTransactionHelper helper(http_req, p, BoundNetLog(),
GetParam(), NULL);
helper.RunToCompletion(&data);
TransactionHelperResult out = helper.output();
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.1 200 OK", out.status_line);
EXPECT_EQ("hello!", out.response_data);
}
}
// Start three gets simultaniously; making sure that multiplexed
// streams work properly.
// This can't use the TransactionHelper method, since it only
// handles a single transaction, and finishes them as soon
// as it launches them.
// TODO(gavinp): create a working generalized TransactionHelper that
// can allow multiple streams in flight.
TEST_P(SpdyNetworkTransactionTest, ThreeGets) {
scoped_ptr<SpdyFrame> req(
spdy_util_.ConstructSpdyGet(NULL, 0, false, 1, LOWEST, true));
scoped_ptr<SpdyFrame> resp(spdy_util_.ConstructSpdyGetSynReply(NULL, 0, 1));
scoped_ptr<SpdyFrame> body(spdy_util_.ConstructSpdyBodyFrame(1, false));
scoped_ptr<SpdyFrame> fbody(spdy_util_.ConstructSpdyBodyFrame(1, true));
scoped_ptr<SpdyFrame> req2(
spdy_util_.ConstructSpdyGet(NULL, 0, false, 3, LOWEST, true));
scoped_ptr<SpdyFrame> resp2(spdy_util_.ConstructSpdyGetSynReply(NULL, 0, 3));
scoped_ptr<SpdyFrame> body2(spdy_util_.ConstructSpdyBodyFrame(3, false));
scoped_ptr<SpdyFrame> fbody2(spdy_util_.ConstructSpdyBodyFrame(3, true));
scoped_ptr<SpdyFrame> req3(
spdy_util_.ConstructSpdyGet(NULL, 0, false, 5, LOWEST, true));
scoped_ptr<SpdyFrame> resp3(spdy_util_.ConstructSpdyGetSynReply(NULL, 0, 5));
scoped_ptr<SpdyFrame> body3(spdy_util_.ConstructSpdyBodyFrame(5, false));
scoped_ptr<SpdyFrame> fbody3(spdy_util_.ConstructSpdyBodyFrame(5, true));
MockWrite writes[] = {
CreateMockWrite(*req),
CreateMockWrite(*req2),
CreateMockWrite(*req3),
};
MockRead reads[] = {
CreateMockRead(*resp, 1),
CreateMockRead(*body),
CreateMockRead(*resp2, 4),
CreateMockRead(*body2),
CreateMockRead(*resp3, 7),
CreateMockRead(*body3),
CreateMockRead(*fbody),
CreateMockRead(*fbody2),
CreateMockRead(*fbody3),
MockRead(ASYNC, 0, 0), // EOF
};
OrderedSocketData data(reads, arraysize(reads),
writes, arraysize(writes));
OrderedSocketData data_placeholder(NULL, 0, NULL, 0);
BoundNetLog log;
TransactionHelperResult out;
NormalSpdyTransactionHelper helper(CreateGetRequest(), DEFAULT_PRIORITY,
BoundNetLog(), GetParam(), NULL);
helper.RunPreTestSetup();
helper.AddData(&data);
// We require placeholder data because three get requests are sent out, so
// there needs to be three sets of SSL connection data.
helper.AddData(&data_placeholder);
helper.AddData(&data_placeholder);
scoped_ptr<HttpNetworkTransaction> trans1(
new HttpNetworkTransaction(DEFAULT_PRIORITY, helper.session().get()));
scoped_ptr<HttpNetworkTransaction> trans2(
new HttpNetworkTransaction(DEFAULT_PRIORITY, helper.session().get()));
scoped_ptr<HttpNetworkTransaction> trans3(
new HttpNetworkTransaction(DEFAULT_PRIORITY, helper.session().get()));
TestCompletionCallback callback1;
TestCompletionCallback callback2;
TestCompletionCallback callback3;
HttpRequestInfo httpreq1 = CreateGetRequest();
HttpRequestInfo httpreq2 = CreateGetRequest();
HttpRequestInfo httpreq3 = CreateGetRequest();
out.rv = trans1->Start(&httpreq1, callback1.callback(), log);
ASSERT_EQ(ERR_IO_PENDING, out.rv);
out.rv = trans2->Start(&httpreq2, callback2.callback(), log);
ASSERT_EQ(ERR_IO_PENDING, out.rv);
out.rv = trans3->Start(&httpreq3, callback3.callback(), log);
ASSERT_EQ(ERR_IO_PENDING, out.rv);
out.rv = callback1.WaitForResult();
ASSERT_EQ(OK, out.rv);
out.rv = callback3.WaitForResult();
ASSERT_EQ(OK, out.rv);
const HttpResponseInfo* response1 = trans1->GetResponseInfo();
EXPECT_TRUE(response1->headers.get() != NULL);
EXPECT_TRUE(response1->was_fetched_via_spdy);
out.status_line = response1->headers->GetStatusLine();
out.response_info = *response1;
trans2->GetResponseInfo();
out.rv = ReadTransaction(trans1.get(), &out.response_data);
helper.VerifyDataConsumed();
EXPECT_EQ(OK, out.rv);
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.1 200 OK", out.status_line);
EXPECT_EQ("hello!hello!", out.response_data);
}
TEST_P(SpdyNetworkTransactionTest, TwoGetsLateBinding) {
scoped_ptr<SpdyFrame> req(
spdy_util_.ConstructSpdyGet(NULL, 0, false, 1, LOWEST, true));
scoped_ptr<SpdyFrame> resp(spdy_util_.ConstructSpdyGetSynReply(NULL, 0, 1));
scoped_ptr<SpdyFrame> body(spdy_util_.ConstructSpdyBodyFrame(1, false));
scoped_ptr<SpdyFrame> fbody(spdy_util_.ConstructSpdyBodyFrame(1, true));
scoped_ptr<SpdyFrame> req2(
spdy_util_.ConstructSpdyGet(NULL, 0, false, 3, LOWEST, true));
scoped_ptr<SpdyFrame> resp2(spdy_util_.ConstructSpdyGetSynReply(NULL, 0, 3));
scoped_ptr<SpdyFrame> body2(spdy_util_.ConstructSpdyBodyFrame(3, false));
scoped_ptr<SpdyFrame> fbody2(spdy_util_.ConstructSpdyBodyFrame(3, true));
MockWrite writes[] = {
CreateMockWrite(*req),
CreateMockWrite(*req2),
};
MockRead reads[] = {
CreateMockRead(*resp, 1),
CreateMockRead(*body),
CreateMockRead(*resp2, 4),
CreateMockRead(*body2),
CreateMockRead(*fbody),
CreateMockRead(*fbody2),
MockRead(ASYNC, 0, 0), // EOF
};
OrderedSocketData data(reads, arraysize(reads),
writes, arraysize(writes));
MockConnect never_finishing_connect(SYNCHRONOUS, ERR_IO_PENDING);
OrderedSocketData data_placeholder(NULL, 0, NULL, 0);
data_placeholder.set_connect_data(never_finishing_connect);
BoundNetLog log;
TransactionHelperResult out;
NormalSpdyTransactionHelper helper(CreateGetRequest(), DEFAULT_PRIORITY,
BoundNetLog(), GetParam(), NULL);
helper.RunPreTestSetup();
helper.AddData(&data);
// We require placeholder data because two get requests are sent out, so
// there needs to be two sets of SSL connection data.
helper.AddData(&data_placeholder);
scoped_ptr<HttpNetworkTransaction> trans1(
new HttpNetworkTransaction(DEFAULT_PRIORITY, helper.session().get()));
scoped_ptr<HttpNetworkTransaction> trans2(
new HttpNetworkTransaction(DEFAULT_PRIORITY, helper.session().get()));
TestCompletionCallback callback1;
TestCompletionCallback callback2;
HttpRequestInfo httpreq1 = CreateGetRequest();
HttpRequestInfo httpreq2 = CreateGetRequest();
out.rv = trans1->Start(&httpreq1, callback1.callback(), log);
ASSERT_EQ(ERR_IO_PENDING, out.rv);
out.rv = trans2->Start(&httpreq2, callback2.callback(), log);
ASSERT_EQ(ERR_IO_PENDING, out.rv);
out.rv = callback1.WaitForResult();
ASSERT_EQ(OK, out.rv);
out.rv = callback2.WaitForResult();
ASSERT_EQ(OK, out.rv);
const HttpResponseInfo* response1 = trans1->GetResponseInfo();
EXPECT_TRUE(response1->headers.get() != NULL);
EXPECT_TRUE(response1->was_fetched_via_spdy);
out.status_line = response1->headers->GetStatusLine();
out.response_info = *response1;
out.rv = ReadTransaction(trans1.get(), &out.response_data);
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.1 200 OK", out.status_line);
EXPECT_EQ("hello!hello!", out.response_data);
const HttpResponseInfo* response2 = trans2->GetResponseInfo();
EXPECT_TRUE(response2->headers.get() != NULL);
EXPECT_TRUE(response2->was_fetched_via_spdy);
out.status_line = response2->headers->GetStatusLine();
out.response_info = *response2;
out.rv = ReadTransaction(trans2.get(), &out.response_data);
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.1 200 OK", out.status_line);
EXPECT_EQ("hello!hello!", out.response_data);
helper.VerifyDataConsumed();
}
TEST_P(SpdyNetworkTransactionTest, TwoGetsLateBindingFromPreconnect) {
scoped_ptr<SpdyFrame> req(
spdy_util_.ConstructSpdyGet(NULL, 0, false, 1, LOWEST, true));
scoped_ptr<SpdyFrame> resp(spdy_util_.ConstructSpdyGetSynReply(NULL, 0, 1));
scoped_ptr<SpdyFrame> body(spdy_util_.ConstructSpdyBodyFrame(1, false));
scoped_ptr<SpdyFrame> fbody(spdy_util_.ConstructSpdyBodyFrame(1, true));
scoped_ptr<SpdyFrame> req2(
spdy_util_.ConstructSpdyGet(NULL, 0, false, 3, LOWEST, true));
scoped_ptr<SpdyFrame> resp2(spdy_util_.ConstructSpdyGetSynReply(NULL, 0, 3));
scoped_ptr<SpdyFrame> body2(spdy_util_.ConstructSpdyBodyFrame(3, false));
scoped_ptr<SpdyFrame> fbody2(spdy_util_.ConstructSpdyBodyFrame(3, true));
MockWrite writes[] = {
CreateMockWrite(*req),
CreateMockWrite(*req2),
};
MockRead reads[] = {
CreateMockRead(*resp, 1),
CreateMockRead(*body),
CreateMockRead(*resp2, 4),
CreateMockRead(*body2),
CreateMockRead(*fbody),
CreateMockRead(*fbody2),
MockRead(ASYNC, 0, 0), // EOF
};
OrderedSocketData preconnect_data(reads, arraysize(reads),
writes, arraysize(writes));
MockConnect never_finishing_connect(ASYNC, ERR_IO_PENDING);
OrderedSocketData data_placeholder(NULL, 0, NULL, 0);
data_placeholder.set_connect_data(never_finishing_connect);
BoundNetLog log;
TransactionHelperResult out;
NormalSpdyTransactionHelper helper(CreateGetRequest(), DEFAULT_PRIORITY,
BoundNetLog(), GetParam(), NULL);
helper.RunPreTestSetup();
helper.AddData(&preconnect_data);
// We require placeholder data because 3 connections are attempted (first is
// the preconnect, 2nd and 3rd are the never finished connections.
helper.AddData(&data_placeholder);
helper.AddData(&data_placeholder);
scoped_ptr<HttpNetworkTransaction> trans1(
new HttpNetworkTransaction(DEFAULT_PRIORITY, helper.session().get()));
scoped_ptr<HttpNetworkTransaction> trans2(
new HttpNetworkTransaction(DEFAULT_PRIORITY, helper.session().get()));
TestCompletionCallback callback1;
TestCompletionCallback callback2;
HttpRequestInfo httpreq = CreateGetRequest();
// Preconnect the first.
SSLConfig preconnect_ssl_config;
helper.session()->ssl_config_service()->GetSSLConfig(&preconnect_ssl_config);
HttpStreamFactory* http_stream_factory =
helper.session()->http_stream_factory();
helper.session()->GetNextProtos(&preconnect_ssl_config.next_protos);
http_stream_factory->PreconnectStreams(
1, httpreq, DEFAULT_PRIORITY,
preconnect_ssl_config, preconnect_ssl_config);
out.rv = trans1->Start(&httpreq, callback1.callback(), log);
ASSERT_EQ(ERR_IO_PENDING, out.rv);
out.rv = trans2->Start(&httpreq, callback2.callback(), log);
ASSERT_EQ(ERR_IO_PENDING, out.rv);
out.rv = callback1.WaitForResult();
ASSERT_EQ(OK, out.rv);
out.rv = callback2.WaitForResult();
ASSERT_EQ(OK, out.rv);
const HttpResponseInfo* response1 = trans1->GetResponseInfo();
EXPECT_TRUE(response1->headers.get() != NULL);
EXPECT_TRUE(response1->was_fetched_via_spdy);
out.status_line = response1->headers->GetStatusLine();
out.response_info = *response1;
out.rv = ReadTransaction(trans1.get(), &out.response_data);
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.1 200 OK", out.status_line);
EXPECT_EQ("hello!hello!", out.response_data);
const HttpResponseInfo* response2 = trans2->GetResponseInfo();
EXPECT_TRUE(response2->headers.get() != NULL);
EXPECT_TRUE(response2->was_fetched_via_spdy);
out.status_line = response2->headers->GetStatusLine();
out.response_info = *response2;
out.rv = ReadTransaction(trans2.get(), &out.response_data);
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.1 200 OK", out.status_line);
EXPECT_EQ("hello!hello!", out.response_data);
helper.VerifyDataConsumed();
}
// Similar to ThreeGets above, however this test adds a SETTINGS
// frame. The SETTINGS frame is read during the IO loop waiting on
// the first transaction completion, and sets a maximum concurrent
// stream limit of 1. This means that our IO loop exists after the
// second transaction completes, so we can assert on read_index().
TEST_P(SpdyNetworkTransactionTest, ThreeGetsWithMaxConcurrent) {
// Construct the request.
scoped_ptr<SpdyFrame> req(
spdy_util_.ConstructSpdyGet(NULL, 0, false, 1, LOWEST, true));
scoped_ptr<SpdyFrame> resp(spdy_util_.ConstructSpdyGetSynReply(NULL, 0, 1));
scoped_ptr<SpdyFrame> body(spdy_util_.ConstructSpdyBodyFrame(1, false));
scoped_ptr<SpdyFrame> fbody(spdy_util_.ConstructSpdyBodyFrame(1, true));
scoped_ptr<SpdyFrame> req2(
spdy_util_.ConstructSpdyGet(NULL, 0, false, 3, LOWEST, true));
scoped_ptr<SpdyFrame> resp2(spdy_util_.ConstructSpdyGetSynReply(NULL, 0, 3));
scoped_ptr<SpdyFrame> body2(spdy_util_.ConstructSpdyBodyFrame(3, false));
scoped_ptr<SpdyFrame> fbody2(spdy_util_.ConstructSpdyBodyFrame(3, true));
scoped_ptr<SpdyFrame> req3(
spdy_util_.ConstructSpdyGet(NULL, 0, false, 5, LOWEST, true));
scoped_ptr<SpdyFrame> resp3(spdy_util_.ConstructSpdyGetSynReply(NULL, 0, 5));
scoped_ptr<SpdyFrame> body3(spdy_util_.ConstructSpdyBodyFrame(5, false));
scoped_ptr<SpdyFrame> fbody3(spdy_util_.ConstructSpdyBodyFrame(5, true));
SettingsMap settings;
const uint32 max_concurrent_streams = 1;
settings[SETTINGS_MAX_CONCURRENT_STREAMS] =
SettingsFlagsAndValue(SETTINGS_FLAG_NONE, max_concurrent_streams);
scoped_ptr<SpdyFrame> settings_frame(
spdy_util_.ConstructSpdySettings(settings));
scoped_ptr<SpdyFrame> settings_ack(spdy_util_.ConstructSpdySettingsAck());
MockWrite writes[] = {
CreateMockWrite(*req),
CreateMockWrite(*settings_ack, 2),
CreateMockWrite(*req2),
CreateMockWrite(*req3),
};
MockRead reads[] = {
CreateMockRead(*settings_frame, 1),
CreateMockRead(*resp),
CreateMockRead(*body),
CreateMockRead(*fbody),
CreateMockRead(*resp2, 8),
CreateMockRead(*body2),
CreateMockRead(*fbody2),
CreateMockRead(*resp3, 13),
CreateMockRead(*body3),
CreateMockRead(*fbody3),
MockRead(ASYNC, 0, 0), // EOF
};
OrderedSocketData data(reads, arraysize(reads),
writes, arraysize(writes));
OrderedSocketData data_placeholder(NULL, 0, NULL, 0);
BoundNetLog log;
TransactionHelperResult out;
{
NormalSpdyTransactionHelper helper(CreateGetRequest(), DEFAULT_PRIORITY,
BoundNetLog(), GetParam(), NULL);
helper.RunPreTestSetup();
helper.AddData(&data);
// We require placeholder data because three get requests are sent out, so
// there needs to be three sets of SSL connection data.
helper.AddData(&data_placeholder);
helper.AddData(&data_placeholder);
scoped_ptr<HttpNetworkTransaction> trans1(
new HttpNetworkTransaction(DEFAULT_PRIORITY, helper.session().get()));
scoped_ptr<HttpNetworkTransaction> trans2(
new HttpNetworkTransaction(DEFAULT_PRIORITY, helper.session().get()));
scoped_ptr<HttpNetworkTransaction> trans3(
new HttpNetworkTransaction(DEFAULT_PRIORITY, helper.session().get()));
TestCompletionCallback callback1;
TestCompletionCallback callback2;
TestCompletionCallback callback3;
HttpRequestInfo httpreq1 = CreateGetRequest();
HttpRequestInfo httpreq2 = CreateGetRequest();
HttpRequestInfo httpreq3 = CreateGetRequest();
out.rv = trans1->Start(&httpreq1, callback1.callback(), log);
ASSERT_EQ(out.rv, ERR_IO_PENDING);
// Run transaction 1 through quickly to force a read of our SETTINGS
// frame.
out.rv = callback1.WaitForResult();
ASSERT_EQ(OK, out.rv);
out.rv = trans2->Start(&httpreq2, callback2.callback(), log);
ASSERT_EQ(out.rv, ERR_IO_PENDING);
out.rv = trans3->Start(&httpreq3, callback3.callback(), log);
ASSERT_EQ(out.rv, ERR_IO_PENDING);
out.rv = callback2.WaitForResult();
ASSERT_EQ(OK, out.rv);
EXPECT_EQ(7U, data.read_index()); // i.e. the third trans was queued
out.rv = callback3.WaitForResult();
ASSERT_EQ(OK, out.rv);
const HttpResponseInfo* response1 = trans1->GetResponseInfo();
ASSERT_TRUE(response1 != NULL);
EXPECT_TRUE(response1->headers.get() != NULL);
EXPECT_TRUE(response1->was_fetched_via_spdy);
out.status_line = response1->headers->GetStatusLine();
out.response_info = *response1;
out.rv = ReadTransaction(trans1.get(), &out.response_data);
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.1 200 OK", out.status_line);
EXPECT_EQ("hello!hello!", out.response_data);
const HttpResponseInfo* response2 = trans2->GetResponseInfo();
out.status_line = response2->headers->GetStatusLine();
out.response_info = *response2;
out.rv = ReadTransaction(trans2.get(), &out.response_data);
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.1 200 OK", out.status_line);
EXPECT_EQ("hello!hello!", out.response_data);
const HttpResponseInfo* response3 = trans3->GetResponseInfo();
out.status_line = response3->headers->GetStatusLine();
out.response_info = *response3;
out.rv = ReadTransaction(trans3.get(), &out.response_data);
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.1 200 OK", out.status_line);
EXPECT_EQ("hello!hello!", out.response_data);
helper.VerifyDataConsumed();
}
EXPECT_EQ(OK, out.rv);
}
// Similar to ThreeGetsWithMaxConcurrent above, however this test adds
// a fourth transaction. The third and fourth transactions have
// different data ("hello!" vs "hello!hello!") and because of the
// user specified priority, we expect to see them inverted in
// the response from the server.
TEST_P(SpdyNetworkTransactionTest, FourGetsWithMaxConcurrentPriority) {
// Construct the request.
scoped_ptr<SpdyFrame> req(
spdy_util_.ConstructSpdyGet(NULL, 0, false, 1, LOWEST, true));
scoped_ptr<SpdyFrame> resp(spdy_util_.ConstructSpdyGetSynReply(NULL, 0, 1));
scoped_ptr<SpdyFrame> body(spdy_util_.ConstructSpdyBodyFrame(1, false));
scoped_ptr<SpdyFrame> fbody(spdy_util_.ConstructSpdyBodyFrame(1, true));
scoped_ptr<SpdyFrame> req2(
spdy_util_.ConstructSpdyGet(NULL, 0, false, 3, LOWEST, true));
scoped_ptr<SpdyFrame> resp2(spdy_util_.ConstructSpdyGetSynReply(NULL, 0, 3));
scoped_ptr<SpdyFrame> body2(spdy_util_.ConstructSpdyBodyFrame(3, false));
scoped_ptr<SpdyFrame> fbody2(spdy_util_.ConstructSpdyBodyFrame(3, true));
scoped_ptr<SpdyFrame> req4(
spdy_util_.ConstructSpdyGet(NULL, 0, false, 5, HIGHEST, true));
scoped_ptr<SpdyFrame> resp4(spdy_util_.ConstructSpdyGetSynReply(NULL, 0, 5));
scoped_ptr<SpdyFrame> fbody4(spdy_util_.ConstructSpdyBodyFrame(5, true));
scoped_ptr<SpdyFrame> req3(
spdy_util_.ConstructSpdyGet(NULL, 0, false, 7, LOWEST, true));
scoped_ptr<SpdyFrame> resp3(spdy_util_.ConstructSpdyGetSynReply(NULL, 0, 7));
scoped_ptr<SpdyFrame> body3(spdy_util_.ConstructSpdyBodyFrame(7, false));
scoped_ptr<SpdyFrame> fbody3(spdy_util_.ConstructSpdyBodyFrame(7, true));
SettingsMap settings;
const uint32 max_concurrent_streams = 1;
settings[SETTINGS_MAX_CONCURRENT_STREAMS] =
SettingsFlagsAndValue(SETTINGS_FLAG_NONE, max_concurrent_streams);
scoped_ptr<SpdyFrame> settings_frame(
spdy_util_.ConstructSpdySettings(settings));
scoped_ptr<SpdyFrame> settings_ack(spdy_util_.ConstructSpdySettingsAck());
MockWrite writes[] = { CreateMockWrite(*req),
CreateMockWrite(*settings_ack, 2),
CreateMockWrite(*req2),
CreateMockWrite(*req4),
CreateMockWrite(*req3),
};
MockRead reads[] = {
CreateMockRead(*settings_frame, 1),
CreateMockRead(*resp),
CreateMockRead(*body),
CreateMockRead(*fbody),
CreateMockRead(*resp2, 8),
CreateMockRead(*body2),
CreateMockRead(*fbody2),
CreateMockRead(*resp4, 14),
CreateMockRead(*fbody4),
CreateMockRead(*resp3, 17),
CreateMockRead(*body3),
CreateMockRead(*fbody3),
MockRead(ASYNC, 0, 0), // EOF
};
OrderedSocketData data(reads, arraysize(reads),
writes, arraysize(writes));
OrderedSocketData data_placeholder(NULL, 0, NULL, 0);
BoundNetLog log;
TransactionHelperResult out;
NormalSpdyTransactionHelper helper(CreateGetRequest(), DEFAULT_PRIORITY,
BoundNetLog(), GetParam(), NULL);
helper.RunPreTestSetup();
helper.AddData(&data);
// We require placeholder data because four get requests are sent out, so
// there needs to be four sets of SSL connection data.
helper.AddData(&data_placeholder);
helper.AddData(&data_placeholder);
helper.AddData(&data_placeholder);
scoped_ptr<HttpNetworkTransaction> trans1(
new HttpNetworkTransaction(DEFAULT_PRIORITY, helper.session().get()));
scoped_ptr<HttpNetworkTransaction> trans2(
new HttpNetworkTransaction(DEFAULT_PRIORITY, helper.session().get()));
scoped_ptr<HttpNetworkTransaction> trans3(
new HttpNetworkTransaction(DEFAULT_PRIORITY, helper.session().get()));
scoped_ptr<HttpNetworkTransaction> trans4(
new HttpNetworkTransaction(HIGHEST, helper.session().get()));
TestCompletionCallback callback1;
TestCompletionCallback callback2;
TestCompletionCallback callback3;
TestCompletionCallback callback4;
HttpRequestInfo httpreq1 = CreateGetRequest();
HttpRequestInfo httpreq2 = CreateGetRequest();
HttpRequestInfo httpreq3 = CreateGetRequest();
HttpRequestInfo httpreq4 = CreateGetRequest();
out.rv = trans1->Start(&httpreq1, callback1.callback(), log);
ASSERT_EQ(ERR_IO_PENDING, out.rv);
// Run transaction 1 through quickly to force a read of our SETTINGS frame.
out.rv = callback1.WaitForResult();
ASSERT_EQ(OK, out.rv);
out.rv = trans2->Start(&httpreq2, callback2.callback(), log);
ASSERT_EQ(ERR_IO_PENDING, out.rv);
out.rv = trans3->Start(&httpreq3, callback3.callback(), log);
ASSERT_EQ(ERR_IO_PENDING, out.rv);
out.rv = trans4->Start(&httpreq4, callback4.callback(), log);
ASSERT_EQ(ERR_IO_PENDING, out.rv);
out.rv = callback2.WaitForResult();
ASSERT_EQ(OK, out.rv);
EXPECT_EQ(data.read_index(), 7U); // i.e. the third & fourth trans queued
out.rv = callback3.WaitForResult();
ASSERT_EQ(OK, out.rv);
const HttpResponseInfo* response1 = trans1->GetResponseInfo();
EXPECT_TRUE(response1->headers.get() != NULL);
EXPECT_TRUE(response1->was_fetched_via_spdy);
out.status_line = response1->headers->GetStatusLine();
out.response_info = *response1;
out.rv = ReadTransaction(trans1.get(), &out.response_data);
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.1 200 OK", out.status_line);
EXPECT_EQ("hello!hello!", out.response_data);
const HttpResponseInfo* response2 = trans2->GetResponseInfo();
out.status_line = response2->headers->GetStatusLine();
out.response_info = *response2;
out.rv = ReadTransaction(trans2.get(), &out.response_data);
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.1 200 OK", out.status_line);
EXPECT_EQ("hello!hello!", out.response_data);
// notice: response3 gets two hellos, response4 gets one
// hello, so we know dequeuing priority was respected.
const HttpResponseInfo* response3 = trans3->GetResponseInfo();
out.status_line = response3->headers->GetStatusLine();
out.response_info = *response3;
out.rv = ReadTransaction(trans3.get(), &out.response_data);
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.1 200 OK", out.status_line);
EXPECT_EQ("hello!hello!", out.response_data);
out.rv = callback4.WaitForResult();
EXPECT_EQ(OK, out.rv);
const HttpResponseInfo* response4 = trans4->GetResponseInfo();
out.status_line = response4->headers->GetStatusLine();
out.response_info = *response4;
out.rv = ReadTransaction(trans4.get(), &out.response_data);
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.1 200 OK", out.status_line);
EXPECT_EQ("hello!", out.response_data);
helper.VerifyDataConsumed();
EXPECT_EQ(OK, out.rv);
}
// Similar to ThreeGetsMaxConcurrrent above, however, this test
// deletes a session in the middle of the transaction to insure
// that we properly remove pendingcreatestream objects from
// the spdy_session
TEST_P(SpdyNetworkTransactionTest, ThreeGetsWithMaxConcurrentDelete) {
// Construct the request.
scoped_ptr<SpdyFrame> req(
spdy_util_.ConstructSpdyGet(NULL, 0, false, 1, LOWEST, true));
scoped_ptr<SpdyFrame> resp(spdy_util_.ConstructSpdyGetSynReply(NULL, 0, 1));
scoped_ptr<SpdyFrame> body(spdy_util_.ConstructSpdyBodyFrame(1, false));
scoped_ptr<SpdyFrame> fbody(spdy_util_.ConstructSpdyBodyFrame(1, true));
scoped_ptr<SpdyFrame> req2(
spdy_util_.ConstructSpdyGet(NULL, 0, false, 3, LOWEST, true));
scoped_ptr<SpdyFrame> resp2(spdy_util_.ConstructSpdyGetSynReply(NULL, 0, 3));
scoped_ptr<SpdyFrame> body2(spdy_util_.ConstructSpdyBodyFrame(3, false));
scoped_ptr<SpdyFrame> fbody2(spdy_util_.ConstructSpdyBodyFrame(3, true));
SettingsMap settings;
const uint32 max_concurrent_streams = 1;
settings[SETTINGS_MAX_CONCURRENT_STREAMS] =
SettingsFlagsAndValue(SETTINGS_FLAG_NONE, max_concurrent_streams);
scoped_ptr<SpdyFrame> settings_frame(
spdy_util_.ConstructSpdySettings(settings));
scoped_ptr<SpdyFrame> settings_ack(spdy_util_.ConstructSpdySettingsAck());
MockWrite writes[] = {
CreateMockWrite(*req),
CreateMockWrite(*settings_ack, 2),
CreateMockWrite(*req2),
};
MockRead reads[] = {
CreateMockRead(*settings_frame, 1),
CreateMockRead(*resp),
CreateMockRead(*body),
CreateMockRead(*fbody),
CreateMockRead(*resp2, 8),
CreateMockRead(*body2),
CreateMockRead(*fbody2),
MockRead(ASYNC, 0, 0), // EOF
};
OrderedSocketData data(reads, arraysize(reads),
writes, arraysize(writes));
OrderedSocketData data_placeholder(NULL, 0, NULL, 0);
BoundNetLog log;
TransactionHelperResult out;
NormalSpdyTransactionHelper helper(CreateGetRequest(), DEFAULT_PRIORITY,
BoundNetLog(), GetParam(), NULL);
helper.RunPreTestSetup();
helper.AddData(&data);
// We require placeholder data because three get requests are sent out, so
// there needs to be three sets of SSL connection data.
helper.AddData(&data_placeholder);
helper.AddData(&data_placeholder);
scoped_ptr<HttpNetworkTransaction> trans1(
new HttpNetworkTransaction(DEFAULT_PRIORITY, helper.session().get()));
scoped_ptr<HttpNetworkTransaction> trans2(
new HttpNetworkTransaction(DEFAULT_PRIORITY, helper.session().get()));
scoped_ptr<HttpNetworkTransaction> trans3(
new HttpNetworkTransaction(DEFAULT_PRIORITY, helper.session().get()));
TestCompletionCallback callback1;
TestCompletionCallback callback2;
TestCompletionCallback callback3;
HttpRequestInfo httpreq1 = CreateGetRequest();
HttpRequestInfo httpreq2 = CreateGetRequest();
HttpRequestInfo httpreq3 = CreateGetRequest();
out.rv = trans1->Start(&httpreq1, callback1.callback(), log);
ASSERT_EQ(out.rv, ERR_IO_PENDING);
// Run transaction 1 through quickly to force a read of our SETTINGS frame.
out.rv = callback1.WaitForResult();
ASSERT_EQ(OK, out.rv);
out.rv = trans2->Start(&httpreq2, callback2.callback(), log);
ASSERT_EQ(out.rv, ERR_IO_PENDING);
out.rv = trans3->Start(&httpreq3, callback3.callback(), log);
delete trans3.release();
ASSERT_EQ(out.rv, ERR_IO_PENDING);
out.rv = callback2.WaitForResult();
ASSERT_EQ(OK, out.rv);
EXPECT_EQ(8U, data.read_index());
const HttpResponseInfo* response1 = trans1->GetResponseInfo();
ASSERT_TRUE(response1 != NULL);
EXPECT_TRUE(response1->headers.get() != NULL);
EXPECT_TRUE(response1->was_fetched_via_spdy);
out.status_line = response1->headers->GetStatusLine();
out.response_info = *response1;
out.rv = ReadTransaction(trans1.get(), &out.response_data);
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.1 200 OK", out.status_line);
EXPECT_EQ("hello!hello!", out.response_data);
const HttpResponseInfo* response2 = trans2->GetResponseInfo();
ASSERT_TRUE(response2 != NULL);
out.status_line = response2->headers->GetStatusLine();
out.response_info = *response2;
out.rv = ReadTransaction(trans2.get(), &out.response_data);
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.1 200 OK", out.status_line);
EXPECT_EQ("hello!hello!", out.response_data);
helper.VerifyDataConsumed();
EXPECT_EQ(OK, out.rv);
}
namespace {
// The KillerCallback will delete the transaction on error as part of the
// callback.
class KillerCallback : public TestCompletionCallbackBase {
public:
explicit KillerCallback(HttpNetworkTransaction* transaction)
: transaction_(transaction),
callback_(base::Bind(&KillerCallback::OnComplete,
base::Unretained(this))) {
}
virtual ~KillerCallback() {}
const CompletionCallback& callback() const { return callback_; }
private:
void OnComplete(int result) {
if (result < 0)
delete transaction_;
SetResult(result);
}
HttpNetworkTransaction* transaction_;
CompletionCallback callback_;
};
} // namespace
// Similar to ThreeGetsMaxConcurrrentDelete above, however, this test
// closes the socket while we have a pending transaction waiting for
// a pending stream creation. http://crbug.com/52901
TEST_P(SpdyNetworkTransactionTest, ThreeGetsWithMaxConcurrentSocketClose) {
// Construct the request.
scoped_ptr<SpdyFrame> req(
spdy_util_.ConstructSpdyGet(NULL, 0, false, 1, LOWEST, true));
scoped_ptr<SpdyFrame> resp(spdy_util_.ConstructSpdyGetSynReply(NULL, 0, 1));
scoped_ptr<SpdyFrame> body(spdy_util_.ConstructSpdyBodyFrame(1, false));
scoped_ptr<SpdyFrame> fin_body(spdy_util_.ConstructSpdyBodyFrame(1, true));
scoped_ptr<SpdyFrame> req2(
spdy_util_.ConstructSpdyGet(NULL, 0, false, 3, LOWEST, true));
scoped_ptr<SpdyFrame> resp2(spdy_util_.ConstructSpdyGetSynReply(NULL, 0, 3));
SettingsMap settings;
const uint32 max_concurrent_streams = 1;
settings[SETTINGS_MAX_CONCURRENT_STREAMS] =
SettingsFlagsAndValue(SETTINGS_FLAG_NONE, max_concurrent_streams);
scoped_ptr<SpdyFrame> settings_frame(
spdy_util_.ConstructSpdySettings(settings));
scoped_ptr<SpdyFrame> settings_ack(spdy_util_.ConstructSpdySettingsAck());
MockWrite writes[] = {
CreateMockWrite(*req),
CreateMockWrite(*settings_ack, 2),
CreateMockWrite(*req2),
};
MockRead reads[] = {
CreateMockRead(*settings_frame, 1),
CreateMockRead(*resp),
CreateMockRead(*body),
CreateMockRead(*fin_body),
CreateMockRead(*resp2, 8),
MockRead(ASYNC, ERR_CONNECTION_RESET, 0), // Abort!
};
OrderedSocketData data(reads, arraysize(reads),
writes, arraysize(writes));
OrderedSocketData data_placeholder(NULL, 0, NULL, 0);
BoundNetLog log;
TransactionHelperResult out;
NormalSpdyTransactionHelper helper(CreateGetRequest(), DEFAULT_PRIORITY,
BoundNetLog(), GetParam(), NULL);
helper.RunPreTestSetup();
helper.AddData(&data);
// We require placeholder data because three get requests are sent out, so
// there needs to be three sets of SSL connection data.
helper.AddData(&data_placeholder);
helper.AddData(&data_placeholder);
HttpNetworkTransaction trans1(DEFAULT_PRIORITY, helper.session().get());
HttpNetworkTransaction trans2(DEFAULT_PRIORITY, helper.session().get());
HttpNetworkTransaction* trans3(
new HttpNetworkTransaction(DEFAULT_PRIORITY, helper.session().get()));
TestCompletionCallback callback1;
TestCompletionCallback callback2;
KillerCallback callback3(trans3);
HttpRequestInfo httpreq1 = CreateGetRequest();
HttpRequestInfo httpreq2 = CreateGetRequest();
HttpRequestInfo httpreq3 = CreateGetRequest();
out.rv = trans1.Start(&httpreq1, callback1.callback(), log);
ASSERT_EQ(out.rv, ERR_IO_PENDING);
// Run transaction 1 through quickly to force a read of our SETTINGS frame.
out.rv = callback1.WaitForResult();
ASSERT_EQ(OK, out.rv);
out.rv = trans2.Start(&httpreq2, callback2.callback(), log);
ASSERT_EQ(out.rv, ERR_IO_PENDING);
out.rv = trans3->Start(&httpreq3, callback3.callback(), log);
ASSERT_EQ(out.rv, ERR_IO_PENDING);
out.rv = callback3.WaitForResult();
ASSERT_EQ(ERR_ABORTED, out.rv);
EXPECT_EQ(6U, data.read_index());
const HttpResponseInfo* response1 = trans1.GetResponseInfo();
ASSERT_TRUE(response1 != NULL);
EXPECT_TRUE(response1->headers.get() != NULL);
EXPECT_TRUE(response1->was_fetched_via_spdy);
out.status_line = response1->headers->GetStatusLine();
out.response_info = *response1;
out.rv = ReadTransaction(&trans1, &out.response_data);
EXPECT_EQ(OK, out.rv);
const HttpResponseInfo* response2 = trans2.GetResponseInfo();
ASSERT_TRUE(response2 != NULL);
out.status_line = response2->headers->GetStatusLine();
out.response_info = *response2;
out.rv = ReadTransaction(&trans2, &out.response_data);
EXPECT_EQ(ERR_CONNECTION_RESET, out.rv);
helper.VerifyDataConsumed();
}
// Test that a simple PUT request works.
TEST_P(SpdyNetworkTransactionTest, Put) {
// Setup the request
HttpRequestInfo request;
request.method = "PUT";
request.url = GURL("http://www.google.com/");
const SpdyHeaderInfo kSynStartHeader = {
SYN_STREAM, // Kind = Syn
1, // Stream ID
0, // Associated stream ID
ConvertRequestPriorityToSpdyPriority(
LOWEST, spdy_util_.spdy_version()),
kSpdyCredentialSlotUnused,
CONTROL_FLAG_FIN, // Control Flags
false, // Compressed
RST_STREAM_INVALID, // Status
NULL, // Data
0, // Length
DATA_FLAG_NONE // Data Flags
};
scoped_ptr<SpdyHeaderBlock> put_headers(
spdy_util_.ConstructPutHeaderBlock("http://www.google.com", 0));
scoped_ptr<SpdyFrame> req(spdy_util_.ConstructSpdyFrame(
kSynStartHeader, put_headers.Pass()));
MockWrite writes[] = {
CreateMockWrite(*req),
};
scoped_ptr<SpdyFrame> body(spdy_util_.ConstructSpdyBodyFrame(1, true));
const SpdyHeaderInfo kSynReplyHeader = {
SYN_REPLY, // Kind = SynReply
1, // Stream ID
0, // Associated stream ID
ConvertRequestPriorityToSpdyPriority(
LOWEST, spdy_util_.spdy_version()),
kSpdyCredentialSlotUnused,
CONTROL_FLAG_NONE, // Control Flags
false, // Compressed
RST_STREAM_INVALID, // Status
NULL, // Data
0, // Length
DATA_FLAG_NONE // Data Flags
};
scoped_ptr<SpdyHeaderBlock> reply_headers(new SpdyHeaderBlock());
(*reply_headers)[spdy_util_.GetStatusKey()] = "200";
(*reply_headers)[spdy_util_.GetVersionKey()] = "HTTP/1.1";
(*reply_headers)["content-length"] = "1234";
scoped_ptr<SpdyFrame> resp(spdy_util_.ConstructSpdyFrame(
kSynReplyHeader, reply_headers.Pass()));
MockRead reads[] = {
CreateMockRead(*resp),
CreateMockRead(*body),
MockRead(ASYNC, 0, 0) // EOF
};
DelayedSocketData data(1, reads, arraysize(reads),
writes, arraysize(writes));
NormalSpdyTransactionHelper helper(request, DEFAULT_PRIORITY,
BoundNetLog(), GetParam(), NULL);
helper.RunToCompletion(&data);
TransactionHelperResult out = helper.output();
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.1 200 OK", out.status_line);
}
// Test that a simple HEAD request works.
TEST_P(SpdyNetworkTransactionTest, Head) {
// Setup the request
HttpRequestInfo request;
request.method = "HEAD";
request.url = GURL("http://www.google.com/");
const SpdyHeaderInfo kSynStartHeader = {
SYN_STREAM, // Kind = Syn
1, // Stream ID
0, // Associated stream ID
ConvertRequestPriorityToSpdyPriority(
LOWEST, spdy_util_.spdy_version()),
kSpdyCredentialSlotUnused,
CONTROL_FLAG_FIN, // Control Flags
false, // Compressed
RST_STREAM_INVALID, // Status
NULL, // Data
0, // Length
DATA_FLAG_NONE // Data Flags
};
scoped_ptr<SpdyHeaderBlock> head_headers(
spdy_util_.ConstructHeadHeaderBlock("http://www.google.com", 0));
scoped_ptr<SpdyFrame> req(spdy_util_.ConstructSpdyFrame(
kSynStartHeader, head_headers.Pass()));
MockWrite writes[] = {
CreateMockWrite(*req),
};
scoped_ptr<SpdyFrame> body(spdy_util_.ConstructSpdyBodyFrame(1, true));
const SpdyHeaderInfo kSynReplyHeader = {
SYN_REPLY, // Kind = SynReply
1, // Stream ID
0, // Associated stream ID
ConvertRequestPriorityToSpdyPriority(
LOWEST, spdy_util_.spdy_version()),
kSpdyCredentialSlotUnused,
CONTROL_FLAG_NONE, // Control Flags
false, // Compressed
RST_STREAM_INVALID, // Status
NULL, // Data
0, // Length
DATA_FLAG_NONE // Data Flags
};
scoped_ptr<SpdyHeaderBlock> reply_headers(new SpdyHeaderBlock());
(*reply_headers)[spdy_util_.GetStatusKey()] = "200";
(*reply_headers)[spdy_util_.GetVersionKey()] = "HTTP/1.1";
(*reply_headers)["content-length"] = "1234";
scoped_ptr<SpdyFrame> resp(spdy_util_.ConstructSpdyFrame(
kSynReplyHeader,
reply_headers.Pass()));
MockRead reads[] = {
CreateMockRead(*resp),
CreateMockRead(*body),
MockRead(ASYNC, 0, 0) // EOF
};
DelayedSocketData data(1, reads, arraysize(reads),
writes, arraysize(writes));
NormalSpdyTransactionHelper helper(request, DEFAULT_PRIORITY,
BoundNetLog(), GetParam(), NULL);
helper.RunToCompletion(&data);
TransactionHelperResult out = helper.output();
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.1 200 OK", out.status_line);
}
// Test that a simple POST works.
TEST_P(SpdyNetworkTransactionTest, Post) {
scoped_ptr<SpdyFrame> req(
spdy_util_.ConstructSpdyPost(
kRequestUrl, 1, kUploadDataSize, LOWEST, NULL, 0));
scoped_ptr<SpdyFrame> body(spdy_util_.ConstructSpdyBodyFrame(1, true));
MockWrite writes[] = {
CreateMockWrite(*req),
CreateMockWrite(*body), // POST upload frame
};
scoped_ptr<SpdyFrame> resp(spdy_util_.ConstructSpdyPostSynReply(NULL, 0));
MockRead reads[] = {
CreateMockRead(*resp),
CreateMockRead(*body),
MockRead(ASYNC, 0, 0) // EOF
};
DelayedSocketData data(2, reads, arraysize(reads),
writes, arraysize(writes));
NormalSpdyTransactionHelper helper(CreatePostRequest(), DEFAULT_PRIORITY,
BoundNetLog(), GetParam(), NULL);
helper.RunToCompletion(&data);
TransactionHelperResult out = helper.output();
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.1 200 OK", out.status_line);
EXPECT_EQ("hello!", out.response_data);
}
// Test that a POST with a file works.
TEST_P(SpdyNetworkTransactionTest, FilePost) {
scoped_ptr<SpdyFrame> req(
spdy_util_.ConstructSpdyPost(
kRequestUrl, 1, kUploadDataSize, LOWEST, NULL, 0));
scoped_ptr<SpdyFrame> body(spdy_util_.ConstructSpdyBodyFrame(1, true));
MockWrite writes[] = {
CreateMockWrite(*req),
CreateMockWrite(*body), // POST upload frame
};
scoped_ptr<SpdyFrame> resp(spdy_util_.ConstructSpdyPostSynReply(NULL, 0));
MockRead reads[] = {
CreateMockRead(*resp),
CreateMockRead(*body),
MockRead(ASYNC, 0, 0) // EOF
};
DelayedSocketData data(2, reads, arraysize(reads),
writes, arraysize(writes));
NormalSpdyTransactionHelper helper(CreateFilePostRequest(), DEFAULT_PRIORITY,
BoundNetLog(), GetParam(), NULL);
helper.RunToCompletion(&data);
TransactionHelperResult out = helper.output();
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.1 200 OK", out.status_line);
EXPECT_EQ("hello!", out.response_data);
}
// Test that a POST with a unreadable file fails.
TEST_P(SpdyNetworkTransactionTest, UnreadableFilePost) {
MockWrite writes[] = {
MockWrite(ASYNC, 0, 0) // EOF
};
MockRead reads[] = {
MockRead(ASYNC, 0, 0) // EOF
};
DelayedSocketData data(1, reads, arraysize(reads), writes, arraysize(writes));
NormalSpdyTransactionHelper helper(CreateUnreadableFilePostRequest(),
DEFAULT_PRIORITY,
BoundNetLog(), GetParam(), NULL);
helper.RunPreTestSetup();
helper.AddData(&data);
helper.RunDefaultTest();
base::RunLoop().RunUntilIdle();
helper.VerifyDataNotConsumed();
EXPECT_EQ(ERR_ACCESS_DENIED, helper.output().rv);
}
// Test that a complex POST works.
TEST_P(SpdyNetworkTransactionTest, ComplexPost) {
scoped_ptr<SpdyFrame> req(
spdy_util_.ConstructSpdyPost(
kRequestUrl, 1, kUploadDataSize, LOWEST, NULL, 0));
scoped_ptr<SpdyFrame> body(spdy_util_.ConstructSpdyBodyFrame(1, true));
MockWrite writes[] = {
CreateMockWrite(*req),
CreateMockWrite(*body), // POST upload frame
};
scoped_ptr<SpdyFrame> resp(spdy_util_.ConstructSpdyPostSynReply(NULL, 0));
MockRead reads[] = {
CreateMockRead(*resp),
CreateMockRead(*body),
MockRead(ASYNC, 0, 0) // EOF
};
DelayedSocketData data(2, reads, arraysize(reads),
writes, arraysize(writes));
NormalSpdyTransactionHelper helper(CreateComplexPostRequest(),
DEFAULT_PRIORITY,
BoundNetLog(), GetParam(), NULL);
helper.RunToCompletion(&data);
TransactionHelperResult out = helper.output();
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.1 200 OK", out.status_line);
EXPECT_EQ("hello!", out.response_data);
}
// Test that a chunked POST works.
TEST_P(SpdyNetworkTransactionTest, ChunkedPost) {
scoped_ptr<SpdyFrame> req(spdy_util_.ConstructChunkedSpdyPost(NULL, 0));
scoped_ptr<SpdyFrame> body(spdy_util_.ConstructSpdyBodyFrame(1, true));
MockWrite writes[] = {
CreateMockWrite(*req),
CreateMockWrite(*body),
};
scoped_ptr<SpdyFrame> resp(spdy_util_.ConstructSpdyPostSynReply(NULL, 0));
MockRead reads[] = {
CreateMockRead(*resp),
CreateMockRead(*body),
MockRead(ASYNC, 0, 0) // EOF
};
DelayedSocketData data(2, reads, arraysize(reads),
writes, arraysize(writes));
NormalSpdyTransactionHelper helper(CreateChunkedPostRequest(),
DEFAULT_PRIORITY,
BoundNetLog(), GetParam(), NULL);
// These chunks get merged into a single frame when being sent.
const int kFirstChunkSize = kUploadDataSize/2;
helper.request().upload_data_stream->AppendChunk(
kUploadData, kFirstChunkSize, false);
helper.request().upload_data_stream->AppendChunk(
kUploadData + kFirstChunkSize, kUploadDataSize - kFirstChunkSize, true);
helper.RunToCompletion(&data);
TransactionHelperResult out = helper.output();
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.1 200 OK", out.status_line);
EXPECT_EQ(kUploadData, out.response_data);
}
// Test that a chunked POST works with chunks appended after transaction starts.
TEST_P(SpdyNetworkTransactionTest, DelayedChunkedPost) {
scoped_ptr<SpdyFrame> req(spdy_util_.ConstructChunkedSpdyPost(NULL, 0));
scoped_ptr<SpdyFrame> chunk1(spdy_util_.ConstructSpdyBodyFrame(1, false));
scoped_ptr<SpdyFrame> chunk2(spdy_util_.ConstructSpdyBodyFrame(1, false));
scoped_ptr<SpdyFrame> chunk3(spdy_util_.ConstructSpdyBodyFrame(1, true));
MockWrite writes[] = {
CreateMockWrite(*req),
CreateMockWrite(*chunk1),
CreateMockWrite(*chunk2),
CreateMockWrite(*chunk3),
};
scoped_ptr<SpdyFrame> resp(spdy_util_.ConstructSpdyPostSynReply(NULL, 0));
MockRead reads[] = {
CreateMockRead(*resp),
CreateMockRead(*chunk1),
CreateMockRead(*chunk2),
CreateMockRead(*chunk3),
MockRead(ASYNC, 0, 0) // EOF
};
DelayedSocketData data(4, reads, arraysize(reads),
writes, arraysize(writes));
NormalSpdyTransactionHelper helper(CreateChunkedPostRequest(),
DEFAULT_PRIORITY,
BoundNetLog(), GetParam(), NULL);
helper.request().upload_data_stream->AppendChunk(
kUploadData, kUploadDataSize, false);
helper.RunPreTestSetup();
helper.AddData(&data);
ASSERT_TRUE(helper.StartDefaultTest());
base::RunLoop().RunUntilIdle();
helper.request().upload_data_stream->AppendChunk(
kUploadData, kUploadDataSize, false);
base::RunLoop().RunUntilIdle();
helper.request().upload_data_stream->AppendChunk(
kUploadData, kUploadDataSize, true);
helper.FinishDefaultTest();
helper.VerifyDataConsumed();
std::string expected_response;
expected_response += kUploadData;
expected_response += kUploadData;
expected_response += kUploadData;
TransactionHelperResult out = helper.output();
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.1 200 OK", out.status_line);
EXPECT_EQ(expected_response, out.response_data);
}
// Test that a POST without any post data works.
TEST_P(SpdyNetworkTransactionTest, NullPost) {
BufferedSpdyFramer framer(spdy_util_.spdy_version(), false);
// Setup the request
HttpRequestInfo request;
request.method = "POST";
request.url = GURL(kRequestUrl);
// Create an empty UploadData.
request.upload_data_stream = NULL;
// When request.upload_data_stream is NULL for post, content-length is
// expected to be 0.
SpdySynStreamIR syn_ir(1);
syn_ir.set_name_value_block(
*spdy_util_.ConstructPostHeaderBlock(kRequestUrl, 0));
syn_ir.set_fin(true); // No body.
syn_ir.set_priority(ConvertRequestPriorityToSpdyPriority(
LOWEST, spdy_util_.spdy_version()));
scoped_ptr<SpdyFrame> req(framer.SerializeFrame(syn_ir));
MockWrite writes[] = {
CreateMockWrite(*req),
};
scoped_ptr<SpdyFrame> resp(spdy_util_.ConstructSpdyPostSynReply(NULL, 0));
scoped_ptr<SpdyFrame> body(spdy_util_.ConstructSpdyBodyFrame(1, true));
MockRead reads[] = {
CreateMockRead(*resp),
CreateMockRead(*body),
MockRead(ASYNC, 0, 0) // EOF
};
DelayedSocketData data(1, reads, arraysize(reads),
writes, arraysize(writes));
NormalSpdyTransactionHelper helper(request, DEFAULT_PRIORITY,
BoundNetLog(), GetParam(), NULL);
helper.RunToCompletion(&data);
TransactionHelperResult out = helper.output();
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.1 200 OK", out.status_line);
EXPECT_EQ("hello!", out.response_data);
}
// Test that a simple POST works.
TEST_P(SpdyNetworkTransactionTest, EmptyPost) {
BufferedSpdyFramer framer(spdy_util_.spdy_version(), false);
// Create an empty UploadDataStream.
ScopedVector<UploadElementReader> element_readers;
UploadDataStream stream(element_readers.Pass(), 0);
// Setup the request
HttpRequestInfo request;
request.method = "POST";
request.url = GURL(kRequestUrl);
request.upload_data_stream = &stream;
const uint64 kContentLength = 0;
SpdySynStreamIR syn_ir(1);
syn_ir.set_name_value_block(
*spdy_util_.ConstructPostHeaderBlock(kRequestUrl, kContentLength));
syn_ir.set_fin(true); // No body.
syn_ir.set_priority(ConvertRequestPriorityToSpdyPriority(
LOWEST, spdy_util_.spdy_version()));
scoped_ptr<SpdyFrame> req(framer.SerializeFrame(syn_ir));
MockWrite writes[] = {
CreateMockWrite(*req),
};
scoped_ptr<SpdyFrame> resp(spdy_util_.ConstructSpdyPostSynReply(NULL, 0));
scoped_ptr<SpdyFrame> body(spdy_util_.ConstructSpdyBodyFrame(1, true));
MockRead reads[] = {
CreateMockRead(*resp),
CreateMockRead(*body),
MockRead(ASYNC, 0, 0) // EOF
};
DelayedSocketData data(1, reads, arraysize(reads), writes, arraysize(writes));
NormalSpdyTransactionHelper helper(request, DEFAULT_PRIORITY,
BoundNetLog(), GetParam(), NULL);
helper.RunToCompletion(&data);
TransactionHelperResult out = helper.output();
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.1 200 OK", out.status_line);
EXPECT_EQ("hello!", out.response_data);
}
// While we're doing a post, the server sends the reply before upload completes.
TEST_P(SpdyNetworkTransactionTest, ResponseBeforePostCompletes) {
scoped_ptr<SpdyFrame> req(spdy_util_.ConstructChunkedSpdyPost(NULL, 0));
scoped_ptr<SpdyFrame> body(spdy_util_.ConstructSpdyBodyFrame(1, true));
MockWrite writes[] = {
CreateMockWrite(*req, 0),
CreateMockWrite(*body, 3),
};
scoped_ptr<SpdyFrame> resp(spdy_util_.ConstructSpdyPostSynReply(NULL, 0));
MockRead reads[] = {
CreateMockRead(*resp, 1),
CreateMockRead(*body, 2),
MockRead(ASYNC, 0, 4) // EOF
};
// Write the request headers, and read the complete response
// while still waiting for chunked request data.
DeterministicSocketData data(reads, arraysize(reads),
writes, arraysize(writes));
NormalSpdyTransactionHelper helper(CreateChunkedPostRequest(),
DEFAULT_PRIORITY,
BoundNetLog(), GetParam(), NULL);
helper.SetDeterministic();
helper.RunPreTestSetup();
helper.AddDeterministicData(&data);
ASSERT_TRUE(helper.StartDefaultTest());
// Process the request headers, SYN_REPLY, and response body.
// The request body is still in flight.
data.RunFor(3);
const HttpResponseInfo* response = helper.trans()->GetResponseInfo();
EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine());
// Finish sending the request body.
helper.request().upload_data_stream->AppendChunk(
kUploadData, kUploadDataSize, true);
data.RunFor(2);
std::string response_body;
EXPECT_EQ(OK, ReadTransaction(helper.trans(), &response_body));
EXPECT_EQ(kUploadData, response_body);
helper.VerifyDataConsumed();
}
// The client upon cancellation tries to send a RST_STREAM frame. The mock
// socket causes the TCP write to return zero. This test checks that the client
// tries to queue up the RST_STREAM frame again.
TEST_P(SpdyNetworkTransactionTest, SocketWriteReturnsZero) {
scoped_ptr<SpdyFrame> req(
spdy_util_.ConstructSpdyGet(NULL, 0, false, 1, LOWEST, true));
scoped_ptr<SpdyFrame> rst(
spdy_util_.ConstructSpdyRstStream(1, RST_STREAM_CANCEL));
MockWrite writes[] = {
CreateMockWrite(*req.get(), 0, SYNCHRONOUS),
MockWrite(SYNCHRONOUS, 0, 0, 2),
CreateMockWrite(*rst.get(), 3, SYNCHRONOUS),
};
scoped_ptr<SpdyFrame> resp(spdy_util_.ConstructSpdyGetSynReply(NULL, 0, 1));
MockRead reads[] = {
CreateMockRead(*resp.get(), 1, ASYNC),
MockRead(ASYNC, 0, 0, 4) // EOF
};
DeterministicSocketData data(reads, arraysize(reads),
writes, arraysize(writes));
NormalSpdyTransactionHelper helper(CreateGetRequest(), DEFAULT_PRIORITY,
BoundNetLog(), GetParam(), NULL);
helper.SetDeterministic();
helper.RunPreTestSetup();
helper.AddDeterministicData(&data);
HttpNetworkTransaction* trans = helper.trans();
TestCompletionCallback callback;
int rv = trans->Start(
&CreateGetRequest(), callback.callback(), BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
data.SetStop(2);
data.Run();
helper.ResetTrans();
data.SetStop(20);
data.Run();
helper.VerifyDataConsumed();
}
// Test that the transaction doesn't crash when we don't have a reply.
TEST_P(SpdyNetworkTransactionTest, ResponseWithoutSynReply) {
scoped_ptr<SpdyFrame> body(spdy_util_.ConstructSpdyBodyFrame(1, true));
MockRead reads[] = {
CreateMockRead(*body),
MockRead(ASYNC, 0, 0) // EOF
};
DelayedSocketData data(1, reads, arraysize(reads), NULL, 0);
NormalSpdyTransactionHelper helper(CreateGetRequest(), DEFAULT_PRIORITY,
BoundNetLog(), GetParam(), NULL);
helper.RunToCompletion(&data);
TransactionHelperResult out = helper.output();
EXPECT_EQ(ERR_SPDY_PROTOCOL_ERROR, out.rv);
}
// Test that the transaction doesn't crash when we get two replies on the same
// stream ID. See http://crbug.com/45639.
TEST_P(SpdyNetworkTransactionTest, ResponseWithTwoSynReplies) {
scoped_ptr<SpdyFrame> req(
spdy_util_.ConstructSpdyGet(NULL, 0, false, 1, LOWEST, true));
scoped_ptr<SpdyFrame> rst(
spdy_util_.ConstructSpdyRstStream(1, RST_STREAM_PROTOCOL_ERROR));
MockWrite writes[] = {
CreateMockWrite(*req),
CreateMockWrite(*rst),
};
scoped_ptr<SpdyFrame> resp(spdy_util_.ConstructSpdyGetSynReply(NULL, 0, 1));
scoped_ptr<SpdyFrame> body(spdy_util_.ConstructSpdyBodyFrame(1, true));
MockRead reads[] = {
CreateMockRead(*resp),
CreateMockRead(*resp),
CreateMockRead(*body),
MockRead(ASYNC, 0, 0) // EOF
};
DelayedSocketData data(1, reads, arraysize(reads),
writes, arraysize(writes));
NormalSpdyTransactionHelper helper(CreateGetRequest(), DEFAULT_PRIORITY,
BoundNetLog(), GetParam(), NULL);
helper.RunPreTestSetup();
helper.AddData(&data);
HttpNetworkTransaction* trans = helper.trans();
TestCompletionCallback callback;
int rv = trans->Start(&helper.request(), callback.callback(), BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
rv = callback.WaitForResult();
EXPECT_EQ(OK, rv);
const HttpResponseInfo* response = trans->GetResponseInfo();
ASSERT_TRUE(response != NULL);
EXPECT_TRUE(response->headers.get() != NULL);
EXPECT_TRUE(response->was_fetched_via_spdy);
std::string response_data;
rv = ReadTransaction(trans, &response_data);
EXPECT_EQ(ERR_SPDY_PROTOCOL_ERROR, rv);
helper.VerifyDataConsumed();
}
TEST_P(SpdyNetworkTransactionTest, ResetReplyWithTransferEncoding) {
// Construct the request.
scoped_ptr<SpdyFrame> req(
spdy_util_.ConstructSpdyGet(NULL, 0, false, 1, LOWEST, true));
scoped_ptr<SpdyFrame> rst(
spdy_util_.ConstructSpdyRstStream(1, RST_STREAM_PROTOCOL_ERROR));
MockWrite writes[] = {
CreateMockWrite(*req),
CreateMockWrite(*rst),
};
const char* const headers[] = {
"transfer-encoding", "chunked"
};
scoped_ptr<SpdyFrame> resp(
spdy_util_.ConstructSpdyGetSynReply(headers, 1, 1));
scoped_ptr<SpdyFrame> body(
spdy_util_.ConstructSpdyBodyFrame(1, true));
MockRead reads[] = {
CreateMockRead(*resp),
CreateMockRead(*body),
MockRead(ASYNC, 0, 0) // EOF
};
DelayedSocketData data(1, reads, arraysize(reads),
writes, arraysize(writes));
NormalSpdyTransactionHelper helper(CreateGetRequest(), DEFAULT_PRIORITY,
BoundNetLog(), GetParam(), NULL);
helper.RunToCompletion(&data);
TransactionHelperResult out = helper.output();
EXPECT_EQ(ERR_SPDY_PROTOCOL_ERROR, out.rv);
helper.session()->spdy_session_pool()->CloseAllSessions();
helper.VerifyDataConsumed();
}
TEST_P(SpdyNetworkTransactionTest, ResetPushWithTransferEncoding) {
// Construct the request.
scoped_ptr<SpdyFrame> req(
spdy_util_.ConstructSpdyGet(NULL, 0, false, 1, LOWEST, true));
scoped_ptr<SpdyFrame> rst(
spdy_util_.ConstructSpdyRstStream(2, RST_STREAM_PROTOCOL_ERROR));
MockWrite writes[] = {
CreateMockWrite(*req),
CreateMockWrite(*rst),
};
scoped_ptr<SpdyFrame> resp(spdy_util_.ConstructSpdyGetSynReply(NULL, 0, 1));
const char* const headers[] = {
"transfer-encoding", "chunked"
};
scoped_ptr<SpdyFrame> push(
spdy_util_.ConstructSpdyPush(headers, arraysize(headers) / 2,
2, 1, "http://www.google.com/1"));
scoped_ptr<SpdyFrame> body(spdy_util_.ConstructSpdyBodyFrame(1, true));
MockRead reads[] = {
CreateMockRead(*resp),
CreateMockRead(*push),
CreateMockRead(*body),
MockRead(ASYNC, 0, 0) // EOF
};
DelayedSocketData data(1, reads, arraysize(reads),
writes, arraysize(writes));
NormalSpdyTransactionHelper helper(CreateGetRequest(), DEFAULT_PRIORITY,
BoundNetLog(), GetParam(), NULL);
helper.RunToCompletion(&data);
TransactionHelperResult out = helper.output();
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.1 200 OK", out.status_line);
EXPECT_EQ("hello!", out.response_data);
helper.session()->spdy_session_pool()->CloseAllSessions();
helper.VerifyDataConsumed();
}
TEST_P(SpdyNetworkTransactionTest, CancelledTransaction) {
// Construct the request.
scoped_ptr<SpdyFrame> req(
spdy_util_.ConstructSpdyGet(NULL, 0, false, 1, LOWEST, true));
MockWrite writes[] = {
CreateMockWrite(*req),
};
scoped_ptr<SpdyFrame> resp(spdy_util_.ConstructSpdyGetSynReply(NULL, 0, 1));
MockRead reads[] = {
CreateMockRead(*resp),
// This following read isn't used by the test, except during the
// RunUntilIdle() call at the end since the SpdySession survives the
// HttpNetworkTransaction and still tries to continue Read()'ing. Any
// MockRead will do here.
MockRead(ASYNC, 0, 0) // EOF
};
StaticSocketDataProvider data(reads, arraysize(reads),
writes, arraysize(writes));
NormalSpdyTransactionHelper helper(CreateGetRequest(), DEFAULT_PRIORITY,
BoundNetLog(), GetParam(), NULL);
helper.RunPreTestSetup();
helper.AddData(&data);
HttpNetworkTransaction* trans = helper.trans();
TestCompletionCallback callback;
int rv = trans->Start(
&CreateGetRequest(), callback.callback(), BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
helper.ResetTrans(); // Cancel the transaction.
// Flush the MessageLoop while the SpdySessionDependencies (in particular, the
// MockClientSocketFactory) are still alive.
base::RunLoop().RunUntilIdle();
helper.VerifyDataNotConsumed();
}
// Verify that the client sends a Rst Frame upon cancelling the stream.
TEST_P(SpdyNetworkTransactionTest, CancelledTransactionSendRst) {
scoped_ptr<SpdyFrame> req(
spdy_util_.ConstructSpdyGet(NULL, 0, false, 1, LOWEST, true));
scoped_ptr<SpdyFrame> rst(
spdy_util_.ConstructSpdyRstStream(1, RST_STREAM_CANCEL));
MockWrite writes[] = {
CreateMockWrite(*req, 0, SYNCHRONOUS),
CreateMockWrite(*rst, 2, SYNCHRONOUS),
};
scoped_ptr<SpdyFrame> resp(spdy_util_.ConstructSpdyGetSynReply(NULL, 0, 1));
MockRead reads[] = {
CreateMockRead(*resp, 1, ASYNC),
MockRead(ASYNC, 0, 0, 3) // EOF
};
DeterministicSocketData data(reads, arraysize(reads),
writes, arraysize(writes));
NormalSpdyTransactionHelper helper(CreateGetRequest(), DEFAULT_PRIORITY,
BoundNetLog(),
GetParam(), NULL);
helper.SetDeterministic();
helper.RunPreTestSetup();
helper.AddDeterministicData(&data);
HttpNetworkTransaction* trans = helper.trans();
TestCompletionCallback callback;
int rv = trans->Start(
&CreateGetRequest(), callback.callback(), BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
data.SetStop(2);
data.Run();
helper.ResetTrans();
data.SetStop(20);
data.Run();
helper.VerifyDataConsumed();
}
// Verify that the client can correctly deal with the user callback attempting
// to start another transaction on a session that is closing down. See
// http://crbug.com/47455
TEST_P(SpdyNetworkTransactionTest, StartTransactionOnReadCallback) {
scoped_ptr<SpdyFrame> req(
spdy_util_.ConstructSpdyGet(NULL, 0, false, 1, LOWEST, true));
MockWrite writes[] = { CreateMockWrite(*req) };
MockWrite writes2[] = { CreateMockWrite(*req) };
// The indicated length of this frame is longer than its actual length. When
// the session receives an empty frame after this one, it shuts down the
// session, and calls the read callback with the incomplete data.
const uint8 kGetBodyFrame2[] = {
0x00, 0x00, 0x00, 0x01,
0x01, 0x00, 0x00, 0x07,
'h', 'e', 'l', 'l', 'o', '!',
};
scoped_ptr<SpdyFrame> resp(spdy_util_.ConstructSpdyGetSynReply(NULL, 0, 1));
MockRead reads[] = {
CreateMockRead(*resp, 2),
MockRead(ASYNC, ERR_IO_PENDING, 3), // Force a pause
MockRead(ASYNC, reinterpret_cast<const char*>(kGetBodyFrame2),
arraysize(kGetBodyFrame2), 4),
MockRead(ASYNC, ERR_IO_PENDING, 5), // Force a pause
MockRead(ASYNC, 0, 0, 6), // EOF
};
MockRead reads2[] = {
CreateMockRead(*resp, 2),
MockRead(ASYNC, 0, 0, 3), // EOF
};
OrderedSocketData data(reads, arraysize(reads),
writes, arraysize(writes));
DelayedSocketData data2(1, reads2, arraysize(reads2),
writes2, arraysize(writes2));
NormalSpdyTransactionHelper helper(CreateGetRequest(), DEFAULT_PRIORITY,
BoundNetLog(), GetParam(), NULL);
helper.RunPreTestSetup();
helper.AddData(&data);
helper.AddData(&data2);
HttpNetworkTransaction* trans = helper.trans();
// Start the transaction with basic parameters.
TestCompletionCallback callback;
int rv = trans->Start(&helper.request(), callback.callback(), BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
rv = callback.WaitForResult();
const int kSize = 3000;
scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(kSize));
rv = trans->Read(
buf.get(),
kSize,
base::Bind(&SpdyNetworkTransactionTest::StartTransactionCallback,
helper.session()));
// This forces an err_IO_pending, which sets the callback.
data.CompleteRead();
// This finishes the read.
data.CompleteRead();
helper.VerifyDataConsumed();
}
// Verify that the client can correctly deal with the user callback deleting the
// transaction. Failures will usually be valgrind errors. See
// http://crbug.com/46925
TEST_P(SpdyNetworkTransactionTest, DeleteSessionOnReadCallback) {
scoped_ptr<SpdyFrame> req(
spdy_util_.ConstructSpdyGet(NULL, 0, false, 1, LOWEST, true));
MockWrite writes[] = { CreateMockWrite(*req) };
scoped_ptr<SpdyFrame> resp(spdy_util_.ConstructSpdyGetSynReply(NULL, 0, 1));
scoped_ptr<SpdyFrame> body(spdy_util_.ConstructSpdyBodyFrame(1, true));
MockRead reads[] = {
CreateMockRead(*resp.get(), 2),
MockRead(ASYNC, ERR_IO_PENDING, 3), // Force a pause
CreateMockRead(*body.get(), 4),
MockRead(ASYNC, 0, 0, 5), // EOF
};
OrderedSocketData data(reads, arraysize(reads),
writes, arraysize(writes));
NormalSpdyTransactionHelper helper(CreateGetRequest(), DEFAULT_PRIORITY,
BoundNetLog(), GetParam(), NULL);
helper.RunPreTestSetup();
helper.AddData(&data);
HttpNetworkTransaction* trans = helper.trans();
// Start the transaction with basic parameters.
TestCompletionCallback callback;
int rv = trans->Start(&helper.request(), callback.callback(), BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
rv = callback.WaitForResult();