blob: f9a8aa6d4fde6a3fd23e6f58a64b6fa8b32a5777 [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 <cmath>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/memory/ptr_util.h"
#include "base/run_loop.h"
#include "base/strings/string_piece.h"
#include "base/test/test_file_util.h"
#include "base/threading/thread_task_runner_handle.h"
#include "net/base/auth.h"
#include "net/base/chunked_upload_data_stream.h"
#include "net/base/elements_upload_data_stream.h"
#include "net/base/proxy_delegate.h"
#include "net/base/request_priority.h"
#include "net/base/test_proxy_delegate.h"
#include "net/base/upload_bytes_element_reader.h"
#include "net/base/upload_file_element_reader.h"
#include "net/http/http_auth_scheme.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/log/net_log_event_type.h"
#include "net/log/net_log_with_source.h"
#include "net/log/test_net_log.h"
#include "net/log/test_net_log_entry.h"
#include "net/log/test_net_log_util.h"
#include "net/proxy/proxy_server.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/test/cert_test_util.h"
#include "net/test/gtest_util.h"
#include "net/test/test_data_directory.h"
#include "net/url_request/url_request_test_util.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/platform_test.h"
using net::test::IsError;
using net::test::IsOk;
//-----------------------------------------------------------------------------
namespace net {
namespace {
using testing::Each;
using testing::Eq;
const int32_t kBufferSize = SpdyHttpStream::kRequestBodyBufferSize;
} // namespace
class SpdyNetworkTransactionTest : public ::testing::Test {
protected:
SpdyNetworkTransactionTest()
: default_url_(kDefaultUrl),
host_port_pair_(HostPortPair::FromURL(default_url_)) {}
~SpdyNetworkTransactionTest() override {
// UploadDataStream may post a deletion tasks back to the message loop on
// destruction.
upload_data_stream_.reset();
base::RunLoop().RunUntilIdle();
}
void SetUp() override {
get_request_initialized_ = false;
post_request_initialized_ = false;
chunked_post_request_initialized_ = false;
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
}
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 NetLogWithSource& log,
std::unique_ptr<SpdySessionDependencies> session_deps)
: request_(request),
priority_(priority),
session_deps_(session_deps.get() == NULL
? base::MakeUnique<SpdySessionDependencies>()
: std::move(session_deps)),
session_(
SpdySessionDependencies::SpdyCreateSession(session_deps_.get())),
log_(log) {}
~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 RunPreTestSetup() {
// We're now ready to use SSL-npn SPDY.
trans_.reset(new HttpNetworkTransaction(priority_, session_.get()));
}
// Start the transaction, read some data, finish.
void RunDefaultTest() {
if (!StartDefaultTest())
return;
FinishDefaultTest();
}
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(ERR_ABORTED);
return;
}
// Verify responses.
const HttpResponseInfo* response = trans_->GetResponseInfo();
ASSERT_TRUE(response);
ASSERT_TRUE(response->headers);
EXPECT_EQ(HttpResponseInfo::CONNECTION_INFO_HTTP2,
response->connection_info);
EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine());
EXPECT_TRUE(response->was_fetched_via_spdy);
EXPECT_TRUE(response->was_alpn_negotiated);
EXPECT_EQ("127.0.0.1", response->socket_address.host());
EXPECT_EQ(443, 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);
}
void FinishDefaultTestWithoutVerification() {
output_.rv = callback_.WaitForResult();
if (output_.rv != OK)
session_->spdy_session_pool()->CloseCurrentSessions(ERR_ABORTED);
}
void WaitForCallbackToComplete() { output_.rv = callback_.WaitForResult(); }
// 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 (const SocketDataProvider* provider : data_vector_) {
EXPECT_TRUE(provider->AllReadDataConsumed());
EXPECT_TRUE(provider->AllWriteDataConsumed());
}
}
// 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 (const SocketDataProvider* provider : data_vector_) {
EXPECT_FALSE(provider->AllReadDataConsumed());
EXPECT_FALSE(provider->AllWriteDataConsumed());
}
}
void RunToCompletion(SocketDataProvider* data) {
RunPreTestSetup();
AddData(data);
RunDefaultTest();
VerifyDataConsumed();
}
void RunToCompletionWithSSLData(
SocketDataProvider* data,
std::unique_ptr<SSLSocketDataProvider> ssl_provider) {
RunPreTestSetup();
AddDataWithSSLSocketDataProvider(data, std::move(ssl_provider));
RunDefaultTest();
VerifyDataConsumed();
}
void AddData(SocketDataProvider* data) {
std::unique_ptr<SSLSocketDataProvider> ssl_provider(
new SSLSocketDataProvider(ASYNC, OK));
ssl_provider->cert =
ImportCertFromFile(GetTestCertsDirectory(), "spdy_pooling.pem");
AddDataWithSSLSocketDataProvider(data, std::move(ssl_provider));
}
void AddDataWithSSLSocketDataProvider(
SocketDataProvider* data,
std::unique_ptr<SSLSocketDataProvider> ssl_provider) {
data_vector_.push_back(data);
if (ssl_provider->next_proto == kProtoUnknown)
ssl_provider->next_proto = kProtoHTTP2;
session_deps_->socket_factory->AddSSLSocketDataProvider(
ssl_provider.get());
ssl_vector_.push_back(std::move(ssl_provider));
session_deps_->socket_factory->AddSocketDataProvider(data);
}
HttpNetworkTransaction* trans() { return trans_.get(); }
void ResetTrans() { trans_.reset(); }
const TransactionHelperResult& output() { return output_; }
const HttpRequestInfo& request() const { return request_; }
HttpNetworkSession* session() const { return session_.get(); }
SpdySessionDependencies* session_deps() { return session_deps_.get(); }
private:
typedef std::vector<SocketDataProvider*> DataVector;
typedef std::vector<std::unique_ptr<SSLSocketDataProvider>> SSLVector;
typedef std::vector<std::unique_ptr<SocketDataProvider>> AlternateVector;
HttpRequestInfo request_;
RequestPriority priority_;
std::unique_ptr<SpdySessionDependencies> session_deps_;
std::unique_ptr<HttpNetworkSession> session_;
TransactionHelperResult output_;
SSLVector ssl_vector_;
TestCompletionCallback callback_;
std::unique_ptr<HttpNetworkTransaction> trans_;
DataVector data_vector_;
const NetLogWithSource log_;
};
void ConnectStatusHelperWithExpectedStatus(const MockRead& status,
int expected_status);
void ConnectStatusHelper(const MockRead& status);
const HttpRequestInfo& CreateGetPushRequest() {
get_push_request_.method = "GET";
get_push_request_.url = GURL(GetDefaultUrlWithPath("/foo.dat"));
get_push_request_.load_flags = 0;
return get_push_request_;
}
const HttpRequestInfo& CreateGetRequest() {
if (!get_request_initialized_) {
get_request_.method = "GET";
get_request_.url = default_url_;
get_request_.load_flags = 0;
get_request_initialized_ = true;
}
return get_request_;
}
const HttpRequestInfo& CreateGetRequestWithUserAgent() {
if (!get_request_initialized_) {
get_request_.method = "GET";
get_request_.url = default_url_;
get_request_.load_flags = 0;
get_request_.extra_headers.SetHeader("User-Agent", "Chrome");
get_request_initialized_ = true;
}
return get_request_;
}
const HttpRequestInfo& CreatePostRequest() {
if (!post_request_initialized_) {
std::vector<std::unique_ptr<UploadElementReader>> element_readers;
element_readers.push_back(base::MakeUnique<UploadBytesElementReader>(
kUploadData, kUploadDataSize));
upload_data_stream_.reset(
new ElementsUploadDataStream(std::move(element_readers), 0));
post_request_.method = "POST";
post_request_.url = default_url_;
post_request_.upload_data_stream = upload_data_stream_.get();
post_request_initialized_ = true;
}
return post_request_;
}
const HttpRequestInfo& CreateFilePostRequest() {
if (!post_request_initialized_) {
base::FilePath file_path;
CHECK(base::CreateTemporaryFileInDir(temp_dir_.GetPath(), &file_path));
CHECK_EQ(static_cast<int>(kUploadDataSize),
base::WriteFile(file_path, kUploadData, kUploadDataSize));
std::vector<std::unique_ptr<UploadElementReader>> element_readers;
element_readers.push_back(base::MakeUnique<UploadFileElementReader>(
base::ThreadTaskRunnerHandle::Get().get(), file_path, 0,
kUploadDataSize, base::Time()));
upload_data_stream_.reset(
new ElementsUploadDataStream(std::move(element_readers), 0));
post_request_.method = "POST";
post_request_.url = default_url_;
post_request_.upload_data_stream = upload_data_stream_.get();
post_request_initialized_ = true;
}
return post_request_;
}
const HttpRequestInfo& CreateUnreadableFilePostRequest() {
if (post_request_initialized_)
return post_request_;
base::FilePath file_path;
CHECK(base::CreateTemporaryFileInDir(temp_dir_.GetPath(), &file_path));
CHECK_EQ(static_cast<int>(kUploadDataSize),
base::WriteFile(file_path, kUploadData, kUploadDataSize));
CHECK(base::MakeFileUnreadable(file_path));
std::vector<std::unique_ptr<UploadElementReader>> element_readers;
element_readers.push_back(base::MakeUnique<UploadFileElementReader>(
base::ThreadTaskRunnerHandle::Get().get(), file_path, 0,
kUploadDataSize, base::Time()));
upload_data_stream_.reset(
new ElementsUploadDataStream(std::move(element_readers), 0));
post_request_.method = "POST";
post_request_.url = default_url_;
post_request_.upload_data_stream = upload_data_stream_.get();
post_request_initialized_ = true;
return post_request_;
}
const HttpRequestInfo& CreateComplexPostRequest() {
if (!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_.GetPath(), &file_path));
CHECK_EQ(static_cast<int>(kUploadDataSize),
base::WriteFile(file_path, kUploadData, kUploadDataSize));
std::vector<std::unique_ptr<UploadElementReader>> element_readers;
element_readers.push_back(base::MakeUnique<UploadBytesElementReader>(
kUploadData, kFileRangeOffset));
element_readers.push_back(base::MakeUnique<UploadFileElementReader>(
base::ThreadTaskRunnerHandle::Get().get(), file_path,
kFileRangeOffset, kFileRangeLength, base::Time()));
element_readers.push_back(base::MakeUnique<UploadBytesElementReader>(
kUploadData + kFileRangeOffset + kFileRangeLength,
kUploadDataSize - (kFileRangeOffset + kFileRangeLength)));
upload_data_stream_.reset(
new ElementsUploadDataStream(std::move(element_readers), 0));
post_request_.method = "POST";
post_request_.url = default_url_;
post_request_.upload_data_stream = upload_data_stream_.get();
post_request_initialized_ = true;
}
return post_request_;
}
const HttpRequestInfo& CreateChunkedPostRequest() {
if (!chunked_post_request_initialized_) {
upload_chunked_data_stream_.reset(new ChunkedUploadDataStream(0));
chunked_post_request_.method = "POST";
chunked_post_request_.url = default_url_;
chunked_post_request_.upload_data_stream =
upload_chunked_data_stream_.get();
chunked_post_request_initialized_ = true;
}
return 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,
std::string* result) {
const int kSize = 3000;
int bytes_read = 0;
scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(kSize));
TestCompletionCallback callback;
while (true) {
int rv = trans->Read(buf.get(), kSize, callback.callback());
if (rv == ERR_IO_PENDING) {
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;
SpdySessionKey key(HostPortPair::FromURL(url), ProxyServer::Direct(),
PRIVACY_MODE_DISABLED);
NetLogWithSource log;
HttpNetworkSession* session = helper.session();
base::WeakPtr<SpdySession> spdy_session =
session->spdy_session_pool()->FindAvailableSession(key, url, log);
ASSERT_TRUE(spdy_session);
EXPECT_EQ(0u, spdy_session->num_active_streams());
EXPECT_EQ(0u, spdy_session->num_unclaimed_pushed_streams());
}
void RunServerPushTest(SequencedSocketData* data,
HttpResponseInfo* response,
HttpResponseInfo* push_response,
const std::string& expected) {
NormalSpdyTransactionHelper helper(CreateGetRequest(), DEFAULT_PRIORITY,
NetLogWithSource(), 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(),
NetLogWithSource());
EXPECT_THAT(rv, IsError(ERR_IO_PENDING));
rv = callback.WaitForResult();
// Request the pushed path.
HttpNetworkTransaction trans2(DEFAULT_PRIORITY, helper.session());
rv = trans2.Start(&CreateGetPushRequest(), callback.callback(),
NetLogWithSource());
EXPECT_THAT(rv, IsError(ERR_IO_PENDING));
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, &result2);
// Read the response body.
std::string result;
ReadResult(trans, &result);
// Verify that we consumed all test data.
EXPECT_TRUE(data->AllReadDataConsumed());
EXPECT_TRUE(data->AllWriteDataConsumed());
LoadTimingInfo load_timing_info;
EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info));
EXPECT_TRUE(load_timing_info.push_start.is_null());
EXPECT_TRUE(load_timing_info.push_end.is_null());
LoadTimingInfo load_timing_info2;
EXPECT_TRUE(trans2.GetLoadTimingInfo(&load_timing_info2));
EXPECT_FALSE(load_timing_info2.push_start.is_null());
EXPECT_FALSE(load_timing_info2.push_end.is_null());
// 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 response HEADERS.
// 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(HttpNetworkSession* session,
GURL url,
int result) {
HttpNetworkTransaction trans(DEFAULT_PRIORITY, session);
TestCompletionCallback callback;
HttpRequestInfo request;
request.method = "GET";
request.url = url;
request.load_flags = 0;
int rv = trans.Start(&request, callback.callback(), NetLogWithSource());
EXPECT_THAT(rv, IsError(ERR_IO_PENDING));
callback.WaitForResult();
}
ChunkedUploadDataStream* upload_chunked_data_stream() const {
return upload_chunked_data_stream_.get();
}
std::string GetDefaultUrlWithPath(const char* path) {
return std::string(kDefaultUrl) + path;
}
const GURL default_url_;
const HostPortPair host_port_pair_;
SpdyTestUtil spdy_util_;
private:
std::unique_ptr<ChunkedUploadDataStream> upload_chunked_data_stream_;
std::unique_ptr<UploadDataStream> upload_data_stream_;
bool get_request_initialized_;
bool post_request_initialized_;
bool chunked_post_request_initialized_;
HttpRequestInfo get_request_;
HttpRequestInfo post_request_;
HttpRequestInfo chunked_post_request_;
HttpRequestInfo get_push_request_;
base::ScopedTempDir temp_dir_;
};
// Verify HttpNetworkTransaction constructor.
TEST_F(SpdyNetworkTransactionTest, Constructor) {
auto session_deps = base::MakeUnique<SpdySessionDependencies>();
std::unique_ptr<HttpNetworkSession> session(
SpdySessionDependencies::SpdyCreateSession(session_deps.get()));
auto trans =
base::MakeUnique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get());
}
TEST_F(SpdyNetworkTransactionTest, Get) {
// Construct the request.
SpdySerializedFrame req(
spdy_util_.ConstructSpdyGet(nullptr, 0, 1, LOWEST, true));
MockWrite writes[] = {CreateMockWrite(req, 0)};
SpdySerializedFrame resp(spdy_util_.ConstructSpdyGetReply(NULL, 0, 1));
SpdySerializedFrame body(spdy_util_.ConstructSpdyDataFrame(1, true));
MockRead reads[] = {
CreateMockRead(resp, 1), CreateMockRead(body, 2),
MockRead(ASYNC, 0, 3) // EOF
};
SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes));
NormalSpdyTransactionHelper helper(CreateGetRequest(), DEFAULT_PRIORITY,
NetLogWithSource(), NULL);
helper.RunToCompletion(&data);
TransactionHelperResult out = helper.output();
EXPECT_THAT(out.rv, IsOk());
EXPECT_EQ("HTTP/1.1 200", out.status_line);
EXPECT_EQ("hello!", out.response_data);
}
TEST_F(SpdyNetworkTransactionTest, GetAtEachPriority) {
for (RequestPriority p = MINIMUM_PRIORITY; p <= MAXIMUM_PRIORITY;
p = RequestPriority(p + 1)) {
SpdyTestUtil spdy_test_util;
// Construct the request.
SpdySerializedFrame req(
spdy_test_util.ConstructSpdyGet(nullptr, 0, 1, p, true));
MockWrite writes[] = {CreateMockWrite(req, 0)};
SpdyPriority spdy_prio = 0;
EXPECT_TRUE(GetSpdyPriority(req, &spdy_prio));
// this repeats the RequestPriority-->SpdyPriority mapping from
// SpdyFramer::ConvertRequestPriorityToSpdyPriority to make
// sure it's being done right.
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;
case THROTTLED:
EXPECT_EQ(5, spdy_prio);
break;
default:
FAIL();
}
SpdySerializedFrame resp(
spdy_test_util.ConstructSpdyGetReply(nullptr, 0, 1));
SpdySerializedFrame body(spdy_test_util.ConstructSpdyDataFrame(1, true));
MockRead reads[] = {
CreateMockRead(resp, 1), CreateMockRead(body, 2),
MockRead(ASYNC, 0, 3) // EOF
};
SequencedSocketData data(reads, arraysize(reads), writes,
arraysize(writes));
HttpRequestInfo http_req = CreateGetRequest();
NormalSpdyTransactionHelper helper(http_req, p, NetLogWithSource(), NULL);
helper.RunToCompletion(&data);
TransactionHelperResult out = helper.output();
EXPECT_THAT(out.rv, IsOk());
EXPECT_EQ("HTTP/1.1 200", 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_F(SpdyNetworkTransactionTest, ThreeGets) {
SpdySerializedFrame req(
spdy_util_.ConstructSpdyGet(nullptr, 0, 1, LOWEST, true));
SpdySerializedFrame resp(spdy_util_.ConstructSpdyGetReply(NULL, 0, 1));
SpdySerializedFrame body(spdy_util_.ConstructSpdyDataFrame(1, false));
SpdySerializedFrame fbody(spdy_util_.ConstructSpdyDataFrame(1, true));
SpdySerializedFrame req2(
spdy_util_.ConstructSpdyGet(nullptr, 0, 3, LOWEST, true));
SpdySerializedFrame resp2(spdy_util_.ConstructSpdyGetReply(NULL, 0, 3));
SpdySerializedFrame body2(spdy_util_.ConstructSpdyDataFrame(3, false));
SpdySerializedFrame fbody2(spdy_util_.ConstructSpdyDataFrame(3, true));
SpdySerializedFrame req3(
spdy_util_.ConstructSpdyGet(nullptr, 0, 5, LOWEST, true));
SpdySerializedFrame resp3(spdy_util_.ConstructSpdyGetReply(NULL, 0, 5));
SpdySerializedFrame body3(spdy_util_.ConstructSpdyDataFrame(5, false));
SpdySerializedFrame fbody3(spdy_util_.ConstructSpdyDataFrame(5, true));
MockWrite writes[] = {
CreateMockWrite(req, 0), CreateMockWrite(req2, 3),
CreateMockWrite(req3, 6),
};
MockRead reads[] = {
CreateMockRead(resp, 1), CreateMockRead(body, 2),
CreateMockRead(resp2, 4), CreateMockRead(body2, 5),
CreateMockRead(resp3, 7), CreateMockRead(body3, 8),
CreateMockRead(fbody, 9), CreateMockRead(fbody2, 10),
CreateMockRead(fbody3, 11),
MockRead(ASYNC, 0, 12), // EOF
};
SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes));
SequencedSocketData data_placeholder1(NULL, 0, NULL, 0);
SequencedSocketData data_placeholder2(NULL, 0, NULL, 0);
NetLogWithSource log;
TransactionHelperResult out;
NormalSpdyTransactionHelper helper(CreateGetRequest(), DEFAULT_PRIORITY,
NetLogWithSource(), NULL);
helper.RunPreTestSetup();
helper.AddData(&data);
// We require placeholder data because three get requests are sent out at
// the same time which results in three sockets being connected. The first
// on will negotiate SPDY and will be used for all requests.
helper.AddData(&data_placeholder1);
helper.AddData(&data_placeholder2);
HttpNetworkTransaction trans1(DEFAULT_PRIORITY, helper.session());
HttpNetworkTransaction trans2(DEFAULT_PRIORITY, helper.session());
HttpNetworkTransaction trans3(DEFAULT_PRIORITY, helper.session());
TestCompletionCallback callback1;
TestCompletionCallback callback2;
TestCompletionCallback callback3;
HttpRequestInfo httpreq1 = CreateGetRequest();
HttpRequestInfo httpreq2 = CreateGetRequest();
HttpRequestInfo httpreq3 = CreateGetRequest();
out.rv = trans1.Start(&httpreq1, callback1.callback(), log);
ASSERT_THAT(out.rv, IsError(ERR_IO_PENDING));
out.rv = trans2.Start(&httpreq2, callback2.callback(), log);
ASSERT_THAT(out.rv, IsError(ERR_IO_PENDING));
out.rv = trans3.Start(&httpreq3, callback3.callback(), log);
ASSERT_THAT(out.rv, IsError(ERR_IO_PENDING));
out.rv = callback1.WaitForResult();
ASSERT_THAT(out.rv, IsOk());
out.rv = callback3.WaitForResult();
ASSERT_THAT(out.rv, IsOk());
const HttpResponseInfo* response1 = trans1.GetResponseInfo();
EXPECT_TRUE(response1->headers);
EXPECT_TRUE(response1->was_fetched_via_spdy);
out.status_line = response1->headers->GetStatusLine();
out.response_info = *response1;
trans2.GetResponseInfo();
out.rv = ReadTransaction(&trans1, &out.response_data);
helper.VerifyDataConsumed();
EXPECT_THAT(out.rv, IsOk());
EXPECT_THAT(out.rv, IsOk());
EXPECT_EQ("HTTP/1.1 200", out.status_line);
EXPECT_EQ("hello!hello!", out.response_data);
}
TEST_F(SpdyNetworkTransactionTest, TwoGetsLateBinding) {
SpdySerializedFrame req(
spdy_util_.ConstructSpdyGet(nullptr, 0, 1, LOWEST, true));
SpdySerializedFrame resp(spdy_util_.ConstructSpdyGetReply(NULL, 0, 1));
SpdySerializedFrame body(spdy_util_.ConstructSpdyDataFrame(1, false));
SpdySerializedFrame fbody(spdy_util_.ConstructSpdyDataFrame(1, true));
SpdySerializedFrame req2(
spdy_util_.ConstructSpdyGet(nullptr, 0, 3, LOWEST, true));
SpdySerializedFrame resp2(spdy_util_.ConstructSpdyGetReply(NULL, 0, 3));
SpdySerializedFrame body2(spdy_util_.ConstructSpdyDataFrame(3, false));
SpdySerializedFrame fbody2(spdy_util_.ConstructSpdyDataFrame(3, true));
MockWrite writes[] = {
CreateMockWrite(req, 0), CreateMockWrite(req2, 3),
};
MockRead reads[] = {
CreateMockRead(resp, 1), CreateMockRead(body, 2),
CreateMockRead(resp2, 4), CreateMockRead(body2, 5),
CreateMockRead(fbody, 6), CreateMockRead(fbody2, 7),
MockRead(ASYNC, 0, 8), // EOF
};
SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes));
MockConnect never_finishing_connect(SYNCHRONOUS, ERR_IO_PENDING);
SequencedSocketData data_placeholder(NULL, 0, NULL, 0);
data_placeholder.set_connect_data(never_finishing_connect);
NetLogWithSource log;
TransactionHelperResult out;
NormalSpdyTransactionHelper helper(CreateGetRequest(), DEFAULT_PRIORITY,
NetLogWithSource(), NULL);
helper.RunPreTestSetup();
helper.AddData(&data);
// We require placeholder data because two requests are sent out at
// the same time which results in two sockets being connected. The first
// on will negotiate SPDY and will be used for all requests.
helper.AddData(&data_placeholder);
HttpNetworkTransaction trans1(DEFAULT_PRIORITY, helper.session());
HttpNetworkTransaction trans2(DEFAULT_PRIORITY, helper.session());
TestCompletionCallback callback1;
TestCompletionCallback callback2;
HttpRequestInfo httpreq1 = CreateGetRequest();
HttpRequestInfo httpreq2 = CreateGetRequest();
out.rv = trans1.Start(&httpreq1, callback1.callback(), log);
ASSERT_THAT(out.rv, IsError(ERR_IO_PENDING));
out.rv = trans2.Start(&httpreq2, callback2.callback(), log);
ASSERT_THAT(out.rv, IsError(ERR_IO_PENDING));
out.rv = callback1.WaitForResult();
ASSERT_THAT(out.rv, IsOk());
out.rv = callback2.WaitForResult();
ASSERT_THAT(out.rv, IsOk());
const HttpResponseInfo* response1 = trans1.GetResponseInfo();
EXPECT_TRUE(response1->headers);
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_THAT(out.rv, IsOk());
EXPECT_EQ("HTTP/1.1 200", out.status_line);
EXPECT_EQ("hello!hello!", out.response_data);
const HttpResponseInfo* response2 = trans2.GetResponseInfo();
EXPECT_TRUE(response2->headers);
EXPECT_TRUE(response2->was_fetched_via_spdy);
out.status_line = response2->headers->GetStatusLine();
out.response_info = *response2;
out.rv = ReadTransaction(&trans2, &out.response_data);
EXPECT_THAT(out.rv, IsOk());
EXPECT_EQ("HTTP/1.1 200", out.status_line);
EXPECT_EQ("hello!hello!", out.response_data);
helper.VerifyDataConsumed();
}
TEST_F(SpdyNetworkTransactionTest, TwoGetsLateBindingFromPreconnect) {
SpdySerializedFrame req(
spdy_util_.ConstructSpdyGet(nullptr, 0, 1, LOWEST, true));
SpdySerializedFrame resp(spdy_util_.ConstructSpdyGetReply(NULL, 0, 1));
SpdySerializedFrame body(spdy_util_.ConstructSpdyDataFrame(1, false));
SpdySerializedFrame fbody(spdy_util_.ConstructSpdyDataFrame(1, true));
SpdySerializedFrame req2(
spdy_util_.ConstructSpdyGet(nullptr, 0, 3, LOWEST, true));
SpdySerializedFrame resp2(spdy_util_.ConstructSpdyGetReply(NULL, 0, 3));
SpdySerializedFrame body2(spdy_util_.ConstructSpdyDataFrame(3, false));
SpdySerializedFrame fbody2(spdy_util_.ConstructSpdyDataFrame(3, true));
MockWrite writes[] = {
CreateMockWrite(req, 0), CreateMockWrite(req2, 3),
};
MockRead reads[] = {
CreateMockRead(resp, 1), CreateMockRead(body, 2),
CreateMockRead(resp2, 4), CreateMockRead(body2, 5),
CreateMockRead(fbody, 6), CreateMockRead(fbody2, 7),
MockRead(ASYNC, 0, 8), // EOF
};
SequencedSocketData preconnect_data(reads, arraysize(reads), writes,
arraysize(writes));
MockConnect never_finishing_connect(ASYNC, ERR_IO_PENDING);
SequencedSocketData data_placeholder(NULL, 0, NULL, 0);
data_placeholder.set_connect_data(never_finishing_connect);
NetLogWithSource log;
TransactionHelperResult out;
NormalSpdyTransactionHelper helper(CreateGetRequest(), DEFAULT_PRIORITY,
NetLogWithSource(), 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);
HttpNetworkTransaction trans1(DEFAULT_PRIORITY, helper.session());
HttpNetworkTransaction trans2(DEFAULT_PRIORITY, helper.session());
TestCompletionCallback callback1;
TestCompletionCallback callback2;
HttpRequestInfo httpreq = CreateGetRequest();
// Preconnect the first.
HttpStreamFactory* http_stream_factory =
helper.session()->http_stream_factory();
http_stream_factory->PreconnectStreams(1, httpreq);
out.rv = trans1.Start(&httpreq, callback1.callback(), log);
ASSERT_THAT(out.rv, IsError(ERR_IO_PENDING));
out.rv = trans2.Start(&httpreq, callback2.callback(), log);
ASSERT_THAT(out.rv, IsError(ERR_IO_PENDING));
out.rv = callback1.WaitForResult();
ASSERT_THAT(out.rv, IsOk());
out.rv = callback2.WaitForResult();
ASSERT_THAT(out.rv, IsOk());
const HttpResponseInfo* response1 = trans1.GetResponseInfo();
EXPECT_TRUE(response1->headers);
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_THAT(out.rv, IsOk());
EXPECT_EQ("HTTP/1.1 200", out.status_line);
EXPECT_EQ("hello!hello!", out.response_data);
const HttpResponseInfo* response2 = trans2.GetResponseInfo();
EXPECT_TRUE(response2->headers);
EXPECT_TRUE(response2->was_fetched_via_spdy);
out.status_line = response2->headers->GetStatusLine();
out.response_info = *response2;
out.rv = ReadTransaction(&trans2, &out.response_data);
EXPECT_THAT(out.rv, IsOk());
EXPECT_EQ("HTTP/1.1 200", 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_F(SpdyNetworkTransactionTest, ThreeGetsWithMaxConcurrent) {
// Construct the request.
// Each request fully completes before the next starts.
SpdySerializedFrame req(
spdy_util_.ConstructSpdyGet(nullptr, 0, 1, LOWEST, true));
SpdySerializedFrame resp(spdy_util_.ConstructSpdyGetReply(NULL, 0, 1));
SpdySerializedFrame body(spdy_util_.ConstructSpdyDataFrame(1, false));
SpdySerializedFrame fbody(spdy_util_.ConstructSpdyDataFrame(1, true));
spdy_util_.UpdateWithStreamDestruction(1);
SpdySerializedFrame req2(
spdy_util_.ConstructSpdyGet(nullptr, 0, 3, LOWEST, true));
SpdySerializedFrame resp2(spdy_util_.ConstructSpdyGetReply(NULL, 0, 3));
SpdySerializedFrame body2(spdy_util_.ConstructSpdyDataFrame(3, false));
SpdySerializedFrame fbody2(spdy_util_.ConstructSpdyDataFrame(3, true));
spdy_util_.UpdateWithStreamDestruction(3);
SpdySerializedFrame req3(
spdy_util_.ConstructSpdyGet(nullptr, 0, 5, LOWEST, true));
SpdySerializedFrame resp3(spdy_util_.ConstructSpdyGetReply(NULL, 0, 5));
SpdySerializedFrame body3(spdy_util_.ConstructSpdyDataFrame(5, false));
SpdySerializedFrame fbody3(spdy_util_.ConstructSpdyDataFrame(5, true));
SettingsMap settings;
const uint32_t max_concurrent_streams = 1;
settings[SETTINGS_MAX_CONCURRENT_STREAMS] =
SettingsFlagsAndValue(SETTINGS_FLAG_NONE, max_concurrent_streams);
SpdySerializedFrame settings_frame(
spdy_util_.ConstructSpdySettings(settings));
SpdySerializedFrame settings_ack(spdy_util_.ConstructSpdySettingsAck());
MockWrite writes[] = {
CreateMockWrite(req, 0), CreateMockWrite(settings_ack, 5),
CreateMockWrite(req2, 6), CreateMockWrite(req3, 10),
};
MockRead reads[] = {
CreateMockRead(settings_frame, 1),
CreateMockRead(resp, 2),
CreateMockRead(body, 3),
CreateMockRead(fbody, 4),
CreateMockRead(resp2, 7),
CreateMockRead(body2, 8),
CreateMockRead(fbody2, 9),
CreateMockRead(resp3, 11),
CreateMockRead(body3, 12),
CreateMockRead(fbody3, 13),
MockRead(ASYNC, 0, 14), // EOF
};
SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes));
NetLogWithSource log;
TransactionHelperResult out;
{
NormalSpdyTransactionHelper helper(CreateGetRequest(), DEFAULT_PRIORITY,
NetLogWithSource(), NULL);
helper.RunPreTestSetup();
helper.AddData(&data);
HttpNetworkTransaction trans1(DEFAULT_PRIORITY, helper.session());
HttpNetworkTransaction trans2(DEFAULT_PRIORITY, helper.session());
HttpNetworkTransaction trans3(DEFAULT_PRIORITY, helper.session());
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_THAT(out.rv, IsOk());
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_THAT(out.rv, IsOk());
out.rv = callback3.WaitForResult();
ASSERT_THAT(out.rv, IsOk());
const HttpResponseInfo* response1 = trans1.GetResponseInfo();
ASSERT_TRUE(response1);
EXPECT_TRUE(response1->headers);
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_THAT(out.rv, IsOk());
EXPECT_EQ("HTTP/1.1 200", 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, &out.response_data);
EXPECT_THAT(out.rv, IsOk());
EXPECT_EQ("HTTP/1.1 200", 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, &out.response_data);
EXPECT_THAT(out.rv, IsOk());
EXPECT_EQ("HTTP/1.1 200", out.status_line);
EXPECT_EQ("hello!hello!", out.response_data);
helper.VerifyDataConsumed();
}
EXPECT_THAT(out.rv, IsOk());
}
// 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_F(SpdyNetworkTransactionTest, FourGetsWithMaxConcurrentPriority) {
// Construct the request.
SpdySerializedFrame req(
spdy_util_.ConstructSpdyGet(nullptr, 0, 1, LOWEST, true));
SpdySerializedFrame resp(spdy_util_.ConstructSpdyGetReply(NULL, 0, 1));
SpdySerializedFrame body(spdy_util_.ConstructSpdyDataFrame(1, false));
SpdySerializedFrame fbody(spdy_util_.ConstructSpdyDataFrame(1, true));
spdy_util_.UpdateWithStreamDestruction(1);
SpdySerializedFrame req2(
spdy_util_.ConstructSpdyGet(nullptr, 0, 3, LOWEST, true));
SpdySerializedFrame resp2(spdy_util_.ConstructSpdyGetReply(NULL, 0, 3));
SpdySerializedFrame body2(spdy_util_.ConstructSpdyDataFrame(3, false));
SpdySerializedFrame fbody2(spdy_util_.ConstructSpdyDataFrame(3, true));
spdy_util_.UpdateWithStreamDestruction(3);
SpdySerializedFrame req4(
spdy_util_.ConstructSpdyGet(nullptr, 0, 5, HIGHEST, true));
SpdySerializedFrame resp4(spdy_util_.ConstructSpdyGetReply(NULL, 0, 5));
SpdySerializedFrame fbody4(spdy_util_.ConstructSpdyDataFrame(5, true));
spdy_util_.UpdateWithStreamDestruction(5);
SpdySerializedFrame req3(
spdy_util_.ConstructSpdyGet(nullptr, 0, 7, LOWEST, true));
SpdySerializedFrame resp3(spdy_util_.ConstructSpdyGetReply(NULL, 0, 7));
SpdySerializedFrame body3(spdy_util_.ConstructSpdyDataFrame(7, false));
SpdySerializedFrame fbody3(spdy_util_.ConstructSpdyDataFrame(7, true));
SettingsMap settings;
const uint32_t max_concurrent_streams = 1;
settings[SETTINGS_MAX_CONCURRENT_STREAMS] =
SettingsFlagsAndValue(SETTINGS_FLAG_NONE, max_concurrent_streams);
SpdySerializedFrame settings_frame(
spdy_util_.ConstructSpdySettings(settings));
SpdySerializedFrame settings_ack(spdy_util_.ConstructSpdySettingsAck());
MockWrite writes[] = {
CreateMockWrite(req, 0), CreateMockWrite(settings_ack, 5),
// By making these synchronous, it guarantees that they are not *started*
// before their sequence number, which in turn verifies that only a single
// request is in-flight at a time.
CreateMockWrite(req2, 6, SYNCHRONOUS),
CreateMockWrite(req4, 10, SYNCHRONOUS),
CreateMockWrite(req3, 13, SYNCHRONOUS),
};
MockRead reads[] = {
CreateMockRead(settings_frame, 1),
CreateMockRead(resp, 2),
CreateMockRead(body, 3),
CreateMockRead(fbody, 4),
CreateMockRead(resp2, 7),
CreateMockRead(body2, 8),
CreateMockRead(fbody2, 9),
CreateMockRead(resp4, 11),
CreateMockRead(fbody4, 12),
CreateMockRead(resp3, 14),
CreateMockRead(body3, 15),
CreateMockRead(fbody3, 16),
MockRead(ASYNC, 0, 17), // EOF
};
SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes));
NetLogWithSource log;
TransactionHelperResult out;
NormalSpdyTransactionHelper helper(CreateGetRequest(), DEFAULT_PRIORITY,
NetLogWithSource(), NULL);
helper.RunPreTestSetup();
helper.AddData(&data);
HttpNetworkTransaction trans1(DEFAULT_PRIORITY, helper.session());
HttpNetworkTransaction trans2(DEFAULT_PRIORITY, helper.session());
HttpNetworkTransaction trans3(DEFAULT_PRIORITY, helper.session());
HttpNetworkTransaction trans4(HIGHEST, helper.session());
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_THAT(out.rv, IsError(ERR_IO_PENDING));
// Run transaction 1 through quickly to force a read of our SETTINGS frame.
out.rv = callback1.WaitForResult();
ASSERT_THAT(out.rv, IsOk());
out.rv = trans2.Start(&httpreq2, callback2.callback(), log);
ASSERT_THAT(out.rv, IsError(ERR_IO_PENDING));
out.rv = trans3.Start(&httpreq3, callback3.callback(), log);
ASSERT_THAT(out.rv, IsError(ERR_IO_PENDING));
out.rv = trans4.Start(&httpreq4, callback4.callback(), log);
ASSERT_THAT(out.rv, IsError(ERR_IO_PENDING));
out.rv = callback2.WaitForResult();
ASSERT_THAT(out.rv, IsOk());
out.rv = callback3.WaitForResult();
ASSERT_THAT(out.rv, IsOk());
const HttpResponseInfo* response1 = trans1.GetResponseInfo();
EXPECT_TRUE(response1->headers);
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_THAT(out.rv, IsOk());
EXPECT_EQ("HTTP/1.1 200", 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, &out.response_data);
EXPECT_THAT(out.rv, IsOk());
EXPECT_EQ("HTTP/1.1 200", 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, &out.response_data);
EXPECT_THAT(out.rv, IsOk());
EXPECT_EQ("HTTP/1.1 200", out.status_line);
EXPECT_EQ("hello!hello!", out.response_data);
out.rv = callback4.WaitForResult();
EXPECT_THAT(out.rv, IsOk());
const HttpResponseInfo* response4 = trans4.GetResponseInfo();
out.status_line = response4->headers->GetStatusLine();
out.response_info = *response4;
out.rv = ReadTransaction(&trans4, &out.response_data);
EXPECT_THAT(out.rv, IsOk());
EXPECT_EQ("HTTP/1.1 200", out.status_line);
EXPECT_EQ("hello!", out.response_data);
helper.VerifyDataConsumed();
EXPECT_THAT(out.rv, IsOk());
}
// Similar to ThreeGetsMaxConcurrrent above, however, this test
// deletes a session in the middle of the transaction to ensure
// that we properly remove pendingcreatestream objects from
// the spdy_session
TEST_F(SpdyNetworkTransactionTest, ThreeGetsWithMaxConcurrentDelete) {
// Construct the request.
SpdySerializedFrame req(
spdy_util_.ConstructSpdyGet(nullptr, 0, 1, LOWEST, true));
SpdySerializedFrame resp(spdy_util_.ConstructSpdyGetReply(NULL, 0, 1));
SpdySerializedFrame body(spdy_util_.ConstructSpdyDataFrame(1, false));
SpdySerializedFrame fbody(spdy_util_.ConstructSpdyDataFrame(1, true));
spdy_util_.UpdateWithStreamDestruction(1);
SpdySerializedFrame req2(
spdy_util_.ConstructSpdyGet(nullptr, 0, 3, LOWEST, true));
SpdySerializedFrame resp2(spdy_util_.ConstructSpdyGetReply(NULL, 0, 3));
SpdySerializedFrame body2(spdy_util_.ConstructSpdyDataFrame(3, false));
SpdySerializedFrame fbody2(spdy_util_.ConstructSpdyDataFrame(3, true));
SettingsMap settings;
const uint32_t max_concurrent_streams = 1;
settings[SETTINGS_MAX_CONCURRENT_STREAMS] =
SettingsFlagsAndValue(SETTINGS_FLAG_NONE, max_concurrent_streams);
SpdySerializedFrame settings_frame(
spdy_util_.ConstructSpdySettings(settings));
SpdySerializedFrame settings_ack(spdy_util_.ConstructSpdySettingsAck());
MockWrite writes[] = {
CreateMockWrite(req, 0), CreateMockWrite(settings_ack, 5),
CreateMockWrite(req2, 6),
};
MockRead reads[] = {
CreateMockRead(settings_frame, 1), CreateMockRead(resp, 2),
CreateMockRead(body, 3), CreateMockRead(fbody, 4),
CreateMockRead(resp2, 7), CreateMockRead(body2, 8),
CreateMockRead(fbody2, 9), MockRead(ASYNC, 0, 10), // EOF
};
SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes));
NetLogWithSource log;
TransactionHelperResult out;
NormalSpdyTransactionHelper helper(CreateGetRequest(), DEFAULT_PRIORITY,
NetLogWithSource(), NULL);
helper.RunPreTestSetup();
helper.AddData(&data);
std::unique_ptr<HttpNetworkTransaction> trans1(
new HttpNetworkTransaction(DEFAULT_PRIORITY, helper.session()));
std::unique_ptr<HttpNetworkTransaction> trans2(
new HttpNetworkTransaction(DEFAULT_PRIORITY, helper.session()));
std::unique_ptr<HttpNetworkTransaction> trans3(
new HttpNetworkTransaction(DEFAULT_PRIORITY, helper.session()));
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_THAT(out.rv, IsOk());
out.rv = trans2->Start(&httpreq2, callback2.callback(), log);
ASSERT_EQ(out.rv, ERR_IO_PENDING);
out.rv = trans3->Start(&httpreq3, callback3.callback(), log);
trans3.reset();
ASSERT_EQ(out.rv, ERR_IO_PENDING);
out.rv = callback2.WaitForResult();
ASSERT_THAT(out.rv, IsOk());
const HttpResponseInfo* response1 = trans1->GetResponseInfo();
ASSERT_TRUE(response1);
EXPECT_TRUE(response1->headers);
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_THAT(out.rv, IsOk());
EXPECT_EQ("HTTP/1.1 200", out.status_line);
EXPECT_EQ("hello!hello!", out.response_data);
const HttpResponseInfo* response2 = trans2->GetResponseInfo();
ASSERT_TRUE(response2);
out.status_line = response2->headers->GetStatusLine();
out.response_info = *response2;
out.rv = ReadTransaction(trans2.get(), &out.response_data);
EXPECT_THAT(out.rv, IsOk());
EXPECT_EQ("HTTP/1.1 200", out.status_line);
EXPECT_EQ("hello!hello!", out.response_data);
helper.VerifyDataConsumed();
EXPECT_THAT(out.rv, IsOk());
}
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))) {
}
~KillerCallback() override {}
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_F(SpdyNetworkTransactionTest, ThreeGetsWithMaxConcurrentSocketClose) {
// Construct the request.
SpdySerializedFrame req(
spdy_util_.ConstructSpdyGet(nullptr, 0, 1, LOWEST, true));
SpdySerializedFrame resp(spdy_util_.ConstructSpdyGetReply(NULL, 0, 1));
SpdySerializedFrame body(spdy_util_.ConstructSpdyDataFrame(1, false));
SpdySerializedFrame fin_body(spdy_util_.ConstructSpdyDataFrame(1, true));
spdy_util_.UpdateWithStreamDestruction(1);
SpdySerializedFrame req2(
spdy_util_.ConstructSpdyGet(nullptr, 0, 3, LOWEST, true));
SpdySerializedFrame resp2(spdy_util_.ConstructSpdyGetReply(NULL, 0, 3));
SettingsMap settings;
const uint32_t max_concurrent_streams = 1;
settings[SETTINGS_MAX_CONCURRENT_STREAMS] =
SettingsFlagsAndValue(SETTINGS_FLAG_NONE, max_concurrent_streams);
SpdySerializedFrame settings_frame(
spdy_util_.ConstructSpdySettings(settings));
SpdySerializedFrame settings_ack(spdy_util_.ConstructSpdySettingsAck());
MockWrite writes[] = {
CreateMockWrite(req, 0), CreateMockWrite(settings_ack, 5),
CreateMockWrite(req2, 6),
};
MockRead reads[] = {
CreateMockRead(settings_frame, 1),
CreateMockRead(resp, 2),
CreateMockRead(body, 3),
CreateMockRead(fin_body, 4),
CreateMockRead(resp2, 7),
MockRead(ASYNC, ERR_CONNECTION_RESET, 8), // Abort!
};
SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes));
SequencedSocketData data_placeholder(NULL, 0, NULL, 0);
NetLogWithSource log;
TransactionHelperResult out;
NormalSpdyTransactionHelper helper(CreateGetRequest(), DEFAULT_PRIORITY,
NetLogWithSource(), 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());
HttpNetworkTransaction trans2(DEFAULT_PRIORITY, helper.session());
HttpNetworkTransaction* trans3(
new HttpNetworkTransaction(DEFAULT_PRIORITY, helper.session()));
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_THAT(out.rv, IsOk());
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_THAT(out.rv, IsError(ERR_ABORTED));
const HttpResponseInfo* response1 = trans1.GetResponseInfo();
ASSERT_TRUE(response1);
EXPECT_TRUE(response1->headers);
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_THAT(out.rv, IsOk());
const HttpResponseInfo* response2 = trans2.GetResponseInfo();
ASSERT_TRUE(response2);
out.status_line = response2->headers->GetStatusLine();
out.response_info = *response2;
out.rv = ReadTransaction(&trans2, &out.response_data);
EXPECT_THAT(out.rv, IsError(ERR_CONNECTION_RESET));
helper.VerifyDataConsumed();
}
// Test that a simple PUT request works.
TEST_F(SpdyNetworkTransactionTest, Put) {
// Setup the request
HttpRequestInfo request;
request.method = "PUT";
request.url = default_url_;
SpdyHeaderBlock put_headers(
spdy_util_.ConstructPutHeaderBlock(kDefaultUrl, 0));
SpdySerializedFrame req(
spdy_util_.ConstructSpdyHeaders(1, std::move(put_headers), LOWEST, true));
MockWrite writes[] = {
CreateMockWrite(req, 0),
};
SpdySerializedFrame resp(spdy_util_.ConstructSpdyGetReply(NULL, 0, 1));
SpdySerializedFrame body(spdy_util_.ConstructSpdyDataFrame(1, true));
MockRead reads[] = {
CreateMockRead(resp, 1), CreateMockRead(body, 2),
MockRead(ASYNC, 0, 3) // EOF
};
SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes));
NormalSpdyTransactionHelper helper(request, DEFAULT_PRIORITY,
NetLogWithSource(), NULL);
helper.RunToCompletion(&data);
TransactionHelperResult out = helper.output();
EXPECT_THAT(out.rv, IsOk());
EXPECT_EQ("HTTP/1.1 200", out.status_line);
}
// Test that a simple HEAD request works.
TEST_F(SpdyNetworkTransactionTest, Head) {
// Setup the request
HttpRequestInfo request;
request.method = "HEAD";
request.url = default_url_;
SpdyHeaderBlock head_headers(
spdy_util_.ConstructHeadHeaderBlock(kDefaultUrl, 0));
SpdySerializedFrame req(spdy_util_.ConstructSpdyHeaders(
1, std::move(head_headers), LOWEST, true));
MockWrite writes[] = {
CreateMockWrite(req, 0),
};
SpdySerializedFrame resp(spdy_util_.ConstructSpdyGetReply(NULL, 0, 1));
SpdySerializedFrame body(spdy_util_.ConstructSpdyDataFrame(1, true));
MockRead reads[] = {
CreateMockRead(resp, 1), CreateMockRead(body, 2),
MockRead(ASYNC, 0, 3) // EOF
};
SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes));
NormalSpdyTransactionHelper helper(request, DEFAULT_PRIORITY,
NetLogWithSource(), NULL);
helper.RunToCompletion(&data);
TransactionHelperResult out = helper.output();
EXPECT_THAT(out.rv, IsOk());
EXPECT_EQ("HTTP/1.1 200", out.status_line);
}
// Test that a simple POST works.
TEST_F(SpdyNetworkTransactionTest, Post) {
SpdySerializedFrame req(spdy_util_.ConstructSpdyPost(
kDefaultUrl, 1, kUploadDataSize, LOWEST, NULL, 0));
SpdySerializedFrame body(spdy_util_.ConstructSpdyDataFrame(1, true));
MockWrite writes[] = {
CreateMockWrite(req, 0), CreateMockWrite(body, 1), // POST upload frame
};
SpdySerializedFrame resp(spdy_util_.ConstructSpdyPostReply(NULL, 0));
MockRead reads[] = {
CreateMockRead(resp, 2), CreateMockRead(body, 3),
MockRead(ASYNC, 0, 4) // EOF
};
SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes));
NormalSpdyTransactionHelper helper(CreatePostRequest(), DEFAULT_PRIORITY,
NetLogWithSource(), NULL);
helper.RunToCompletion(&data);
TransactionHelperResult out = helper.output();
EXPECT_THAT(out.rv, IsOk());
EXPECT_EQ("HTTP/1.1 200", out.status_line);
EXPECT_EQ("hello!", out.response_data);
}
// Test that a POST with a file works.
TEST_F(SpdyNetworkTransactionTest, FilePost) {
SpdySerializedFrame req(spdy_util_.ConstructSpdyPost(
kDefaultUrl, 1, kUploadDataSize, LOWEST, NULL, 0));
SpdySerializedFrame body(spdy_util_.ConstructSpdyDataFrame(1, true));
MockWrite writes[] = {
CreateMockWrite(req, 0), CreateMockWrite(body, 1), // POST upload frame
};
SpdySerializedFrame resp(spdy_util_.ConstructSpdyPostReply(NULL, 0));
MockRead reads[] = {
CreateMockRead(resp, 2), CreateMockRead(body, 3),
MockRead(ASYNC, 0, 4) // EOF
};
SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes));
NormalSpdyTransactionHelper helper(CreateFilePostRequest(), DEFAULT_PRIORITY,
NetLogWithSource(), NULL);
helper.RunToCompletion(&data);
TransactionHelperResult out = helper.output();
EXPECT_THAT(out.rv, IsOk());
EXPECT_EQ("HTTP/1.1 200", out.status_line);
EXPECT_EQ("hello!", out.response_data);
}
// Test that a POST with a unreadable file fails.
TEST_F(SpdyNetworkTransactionTest, UnreadableFilePost) {
MockWrite writes[] = {
MockWrite(ASYNC, 0, 0) // EOF
};
MockRead reads[] = {
MockRead(ASYNC, 0, 1) // EOF
};
SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes));
NormalSpdyTransactionHelper helper(CreateUnreadableFilePostRequest(),
DEFAULT_PRIORITY, NetLogWithSource(),
NULL);
helper.RunPreTestSetup();
helper.AddData(&data);
helper.RunDefaultTest();
base::RunLoop().RunUntilIdle();
helper.VerifyDataNotConsumed();
EXPECT_THAT(helper.output().rv, IsError(ERR_ACCESS_DENIED));
}
// Test that a complex POST works.
TEST_F(SpdyNetworkTransactionTest, ComplexPost) {
SpdySerializedFrame req(spdy_util_.ConstructSpdyPost(
kDefaultUrl, 1, kUploadDataSize, LOWEST, NULL, 0));
SpdySerializedFrame body(spdy_util_.ConstructSpdyDataFrame(1, true));
MockWrite writes[] = {
CreateMockWrite(req, 0), CreateMockWrite(body, 1), // POST upload frame
};
SpdySerializedFrame resp(spdy_util_.ConstructSpdyPostReply(NULL, 0));
MockRead reads[] = {
CreateMockRead(resp, 2), CreateMockRead(body, 3),
MockRead(ASYNC, 0, 4) // EOF
};
SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes));
NormalSpdyTransactionHelper helper(
CreateComplexPostRequest(), DEFAULT_PRIORITY, NetLogWithSource(), NULL);
helper.RunToCompletion(&data);
TransactionHelperResult out = helper.output();
EXPECT_THAT(out.rv, IsOk());
EXPECT_EQ("HTTP/1.1 200", out.status_line);
EXPECT_EQ("hello!", out.response_data);
}
// Test that a chunked POST works.
TEST_F(SpdyNetworkTransactionTest, ChunkedPost) {
SpdySerializedFrame req(spdy_util_.ConstructChunkedSpdyPost(NULL, 0));
SpdySerializedFrame body(spdy_util_.ConstructSpdyDataFrame(1, true));
MockWrite writes[] = {
CreateMockWrite(req, 0), CreateMockWrite(body, 1),
};
SpdySerializedFrame resp(spdy_util_.ConstructSpdyPostReply(NULL, 0));
MockRead reads[] = {
CreateMockRead(resp, 2), CreateMockRead(body, 3),
MockRead(ASYNC, 0, 4) // EOF
};
SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes));
NormalSpdyTransactionHelper helper(
CreateChunkedPostRequest(), DEFAULT_PRIORITY, NetLogWithSource(), NULL);
// These chunks get merged into a single frame when being sent.
const int kFirstChunkSize = kUploadDataSize/2;
upload_chunked_data_stream()->AppendData(kUploadData, kFirstChunkSize, false);
upload_chunked_data_stream()->AppendData(
kUploadData + kFirstChunkSize, kUploadDataSize - kFirstChunkSize, true);
helper.RunToCompletion(&data);
TransactionHelperResult out = helper.output();
EXPECT_THAT(out.rv, IsOk());
EXPECT_EQ("HTTP/1.1 200", out.status_line);
EXPECT_EQ(kUploadData, out.response_data);
}
// Test that a chunked POST works with chunks appended after transaction starts.
TEST_F(SpdyNetworkTransactionTest, DelayedChunkedPost) {
SpdySerializedFrame req(spdy_util_.ConstructChunkedSpdyPost(NULL, 0));
SpdySerializedFrame chunk1(spdy_util_.ConstructSpdyDataFrame(1, false));
SpdySerializedFrame chunk2(spdy_util_.ConstructSpdyDataFrame(1, false));
SpdySerializedFrame chunk3(spdy_util_.ConstructSpdyDataFrame(1, true));
MockWrite writes[] = {
CreateMockWrite(req, 0), CreateMockWrite(chunk1, 1),
CreateMockWrite(chunk2, 2), CreateMockWrite(chunk3, 3),
};
SpdySerializedFrame resp(spdy_util_.ConstructSpdyPostReply(NULL, 0));
MockRead reads[] = {
CreateMockRead(resp, 4), CreateMockRead(chunk1, 5),
CreateMockRead(chunk2, 6), CreateMockRead(chunk3, 7),
MockRead(ASYNC, 0, 8) // EOF
};
SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes));
NormalSpdyTransactionHelper helper(
CreateChunkedPostRequest(), DEFAULT_PRIORITY, NetLogWithSource(), NULL);
upload_chunked_data_stream()->AppendData(kUploadData, kUploadDataSize, false);
helper.RunPreTestSetup();
helper.AddData(&data);
ASSERT_TRUE(helper.StartDefaultTest());
base::RunLoop().RunUntilIdle();
upload_chunked_data_stream()->AppendData(kUploadData, kUploadDataSize, false);
base::RunLoop().RunUntilIdle();
upload_chunked_data_stream()->AppendData(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_THAT(out.rv, IsOk());
EXPECT_EQ("HTTP/1.1 200", out.status_line);
EXPECT_EQ(expected_response, out.response_data);
}
// Test that a POST without any post data works.
TEST_F(SpdyNetworkTransactionTest, NullPost) {
// Setup the request
HttpRequestInfo request;
request.method = "POST";
request.url = default_url_;
// 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.
SpdyHeaderBlock req_block(
spdy_util_.ConstructPostHeaderBlock(kDefaultUrl, 0));
SpdySerializedFrame req(
spdy_util_.ConstructSpdyHeaders(1, std::move(req_block), LOWEST, true));
MockWrite writes[] = {
CreateMockWrite(req, 0),
};
SpdySerializedFrame resp(spdy_util_.ConstructSpdyPostReply(NULL, 0));
SpdySerializedFrame body(spdy_util_.ConstructSpdyDataFrame(1, true));
MockRead reads[] = {
CreateMockRead(resp, 1), CreateMockRead(body, 2),
MockRead(ASYNC, 0, 3) // EOF
};
SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes));
NormalSpdyTransactionHelper helper(request, DEFAULT_PRIORITY,
NetLogWithSource(), NULL);
helper.RunToCompletion(&data);
TransactionHelperResult out = helper.output();
EXPECT_THAT(out.rv, IsOk());
EXPECT_EQ("HTTP/1.1 200", out.status_line);
EXPECT_EQ("hello!", out.response_data);
}
// Test that a simple POST works.
TEST_F(SpdyNetworkTransactionTest, EmptyPost) {
// Create an empty UploadDataStream.
std::vector<std::unique_ptr<UploadElementReader>> element_readers;
ElementsUploadDataStream stream(std::move(element_readers), 0);
// Setup the request
HttpRequestInfo request;
request.method = "POST";
request.url = default_url_;
request.upload_data_stream = &stream;
const uint64_t kContentLength = 0;
SpdyHeaderBlock req_block(
spdy_util_.ConstructPostHeaderBlock(kDefaultUrl, kContentLength));
SpdySerializedFrame req(
spdy_util_.ConstructSpdyHeaders(1, std::move(req_block), LOWEST, true));
MockWrite writes[] = {
CreateMockWrite(req, 0),
};
SpdySerializedFrame resp(spdy_util_.ConstructSpdyPostReply(NULL, 0));
SpdySerializedFrame body(spdy_util_.ConstructSpdyDataFrame(1, true));
MockRead reads[] = {
CreateMockRead(resp, 1), CreateMockRead(body, 2),
MockRead(ASYNC, 0, 3) // EOF
};
SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes));
NormalSpdyTransactionHelper helper(request, DEFAULT_PRIORITY,
NetLogWithSource(), NULL);
helper.RunToCompletion(&data);
TransactionHelperResult out = helper.output();
EXPECT_THAT(out.rv, IsOk());
EXPECT_EQ("HTTP/1.1 200", out.status_line);
EXPECT_EQ("hello!", out.response_data);
}
// While we're doing a post, the server sends the reply before upload completes.
TEST_F(SpdyNetworkTransactionTest, ResponseBeforePostCompletes) {
SpdySerializedFrame req(spdy_util_.ConstructChunkedSpdyPost(NULL, 0));
SpdySerializedFrame body(spdy_util_.ConstructSpdyDataFrame(1, true));
MockWrite writes[] = {
CreateMockWrite(req, 0), CreateMockWrite(body, 3),
};
SpdySerializedFrame resp(spdy_util_.ConstructSpdyPostReply(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.
SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes));
NormalSpdyTransactionHelper helper(
CreateChunkedPostRequest(), DEFAULT_PRIORITY, NetLogWithSource(), NULL);
helper.RunPreTestSetup();
helper.AddData(&data);
ASSERT_TRUE(helper.StartDefaultTest());
base::RunLoop().RunUntilIdle();
// Process the request headers, response headers, and response body.
// The request body is still in flight.
const HttpResponseInfo* response = helper.trans()->GetResponseInfo();
EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine());
// Finish sending the request body.
upload_chunked_data_stream()->AppendData(kUploadData, kUploadDataSize, true);
helper.WaitForCallbackToComplete();
EXPECT_THAT(helper.output().rv, IsOk());
std::string response_body;
EXPECT_THAT(ReadTransaction(helper.trans(), &response_body), IsOk());
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_F(SpdyNetworkTransactionTest, SocketWriteReturnsZero) {
SpdySerializedFrame req(
spdy_util_.ConstructSpdyGet(nullptr, 0, 1, LOWEST, true));
SpdySerializedFrame rst(
spdy_util_.ConstructSpdyRstStream(1, RST_STREAM_CANCEL));
MockWrite writes[] = {
CreateMockWrite(req, 0, SYNCHRONOUS), MockWrite(SYNCHRONOUS, 0, 0, 2),
CreateMockWrite(rst, 3, SYNCHRONOUS),
};
SpdySerializedFrame resp(spdy_util_.ConstructSpdyGetReply(NULL, 0, 1));
MockRead reads[] = {
CreateMockRead(resp, 1, ASYNC), MockRead(ASYNC, 0, 0, 4) // EOF
};
SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes));
NormalSpdyTransactionHelper helper(CreateGetRequest(), DEFAULT_PRIORITY,
NetLogWithSource(), NULL);
helper.RunPreTestSetup();
helper.AddData(&data);
helper.StartDefaultTest();
EXPECT_THAT(helper.output().rv, IsError(ERR_IO_PENDING));
helper.WaitForCallbackToComplete();
EXPECT_THAT(helper.output().rv, IsOk());
helper.ResetTrans();
base::RunLoop().RunUntilIdle();
helper.VerifyDataConsumed();
}
// Test that the transaction doesn't crash when we don't have a reply.
TEST_F(SpdyNetworkTransactionTest, ResponseWithoutHeaders) {
SpdySerializedFrame body(spdy_util_.ConstructSpdyDataFrame(1, true));
MockRead reads[] = {
CreateMockRead(body, 1), MockRead(ASYNC, 0, 3) // EOF
};
SpdySerializedFrame req(
spdy_util_.ConstructSpdyGet(nullptr, 0, 1, LOWEST, true));
SpdySerializedFrame rst(
spdy_util_.ConstructSpdyRstStream(1, RST_STREAM_PROTOCOL_ERROR));
MockWrite writes[] = {
CreateMockWrite(req, 0), CreateMockWrite(rst, 2),
};
SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes));
NormalSpdyTransactionHelper helper(CreateGetRequest(), DEFAULT_PRIORITY,
NetLogWithSource(), NULL);
helper.RunToCompletion(&data);
TransactionHelperResult out = helper.output();
EXPECT_THAT(out.rv, IsError(ERR_SPDY_PROTOCOL_ERROR));
}
// Test that the transaction doesn't crash when we get two replies on the same
// stream ID. See http://crbug.com/45639.
TEST_F(SpdyNetworkTransactionTest, ResponseWithTwoSynReplies) {
SpdySerializedFrame req(
spdy_util_.ConstructSpdyGet(nullptr, 0, 1, LOWEST, true));
SpdySerializedFrame rst(
spdy_util_.ConstructSpdyRstStream(1, RST_STREAM_PROTOCOL_ERROR));
MockWrite writes[] = {
CreateMockWrite(req, 0), CreateMockWrite(rst, 4),
};
SpdySerializedFrame resp0(spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1));
SpdySerializedFrame resp1(spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1));
SpdySerializedFrame body(spdy_util_.ConstructSpdyDataFrame(1, true));
MockRead reads[] = {
CreateMockRead(resp0, 1), CreateMockRead(resp1, 2),
CreateMockRead(body, 3), MockRead(ASYNC, 0, 5) // EOF
};
SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes));
NormalSpdyTransactionHelper helper(CreateGetRequest(), DEFAULT_PRIORITY,
NetLogWithSource(), NULL);
helper.RunPreTestSetup();
helper.AddData(&data);
HttpNetworkTransaction* trans = helper.trans();
TestCompletionCallback callback;
int rv =
trans->Start(&helper.request(), callback.callback(), NetLogWithSource());
EXPECT_THAT(rv, IsError(ERR_IO_PENDING));
rv = callback.WaitForResult();
EXPECT_THAT(rv, IsOk());
const HttpResponseInfo* response = trans->GetResponseInfo();
ASSERT_TRUE(response);
EXPECT_TRUE(response->headers);
EXPECT_TRUE(response->was_fetched_via_spdy);
std::string response_data;
rv = ReadTransaction(trans, &response_data);
EXPECT_THAT(rv, IsError(ERR_SPDY_PROTOCOL_ERROR));
helper.VerifyDataConsumed();
}
TEST_F(SpdyNetworkTransactionTest, ResetReplyWithTransferEncoding) {
// Construct the request.
SpdySerializedFrame req(
spdy_util_.ConstructSpdyGet(nullptr, 0, 1, LOWEST, true));
SpdySerializedFrame rst(
spdy_util_.ConstructSpdyRstStream(1, RST_STREAM_PROTOCOL_ERROR));
MockWrite writes[] = {
CreateMockWrite(req, 0), CreateMockWrite(rst, 2),
};
const char* const headers[] = {
"transfer-encoding", "chunked"
};
SpdySerializedFrame resp(spdy_util_.ConstructSpdyGetReply(headers, 1, 1));
SpdySerializedFrame body(spdy_util_.ConstructSpdyDataFrame(1, true));
MockRead reads[] = {
CreateMockRead(resp, 1), CreateMockRead(body, 3),
MockRead(ASYNC, 0, 4) // EOF
};
SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes));
NormalSpdyTransactionHelper helper(CreateGetRequest(), DEFAULT_PRIORITY,
NetLogWithSource(), NULL);
helper.RunToCompletion(&data);
TransactionHelperResult out = helper.output();
EXPECT_THAT(out.rv, IsError(ERR_SPDY_PROTOCOL_ERROR));
helper.session()->spdy_session_pool()->CloseAllSessions();
helper.VerifyDataConsumed();
}
TEST_F(SpdyNetworkTransactionTest, ResetPushWithTransferEncoding) {
// Construct the request.
SpdySerializedFrame req(
spdy_util_.ConstructSpdyGet(nullptr, 0, 1, LOWEST, true));
SpdySerializedFrame rst(
spdy_util_.ConstructSpdyRstStream(2, RST_STREAM_PROTOCOL_ERROR));
MockWrite writes[] = {
CreateMockWrite(req, 0), CreateMockWrite(rst, 4),
};
SpdySerializedFrame resp(spdy_util_.ConstructSpdyGetReply(NULL, 0, 1));
const char* const headers[] = {
"transfer-encoding", "chunked"
};
SpdySerializedFrame push(
spdy_util_.ConstructSpdyPush(headers, arraysize(headers) / 2, 2, 1,
GetDefaultUrlWithPath("/1").c_str()));
SpdySerializedFrame body(spdy_util_.ConstructSpdyDataFrame(1, true));
MockRead reads[] = {
CreateMockRead(resp, 1), CreateMockRead(push, 2), CreateMockRead(body, 3),
MockRead(ASYNC, 0, 5) // EOF
};
SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes));
NormalSpdyTransactionHelper helper(CreateGetRequest(), DEFAULT_PRIORITY,
NetLogWithSource(), NULL);
helper.RunToCompletion(&data);
TransactionHelperResult out = helper.output();
EXPECT_THAT(out.rv, IsOk());
EXPECT_EQ("HTTP/1.1 200", out.status_line);
EXPECT_EQ("hello!", out.response_data);
helper.session()->spdy_session_pool()->CloseAllSessions();
helper.VerifyDataConsumed();
}
TEST_F(SpdyNetworkTransactionTest, CancelledTransaction) {
// Construct the request.
SpdySerializedFrame req(
spdy_util_.ConstructSpdyGet(nullptr, 0, 1, LOWEST, true));
MockWrite writes[] = {
CreateMockWrite(req),
};
SpdySerializedFrame resp(spdy_util_.ConstructSpdyGetReply(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,
NetLogWithSource(), NULL);
helper.RunPreTestSetup();
helper.AddData(&data);
HttpNetworkTransaction* trans = helper.trans();
TestCompletionCallback callback;
int rv = trans->Start(&CreateGetRequest(), callback.callback(),
NetLogWithSource());
EXPECT_THAT(rv, IsError(ERR_IO_PENDING));
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_F(SpdyNetworkTransactionTest, CancelledTransactionSendRst) {
SpdySerializedFrame req(
spdy_util_.ConstructSpdyGet(nullptr, 0, 1, LOWEST, true));
SpdySerializedFrame rst(
spdy_util_.ConstructSpdyRstStream(1, RST_STREAM_CANCEL));
MockWrite writes[] = {
CreateMockWrite(req, 0, SYNCHRONOUS),
CreateMockWrite(rst, 2, SYNCHRONOUS),
};
SpdySerializedFrame resp(spdy_util_.ConstructSpdyGetReply(NULL, 0, 1));
MockRead reads[] = {
CreateMockRead(resp, 1, ASYNC), MockRead(ASYNC, 0, 0, 3) // EOF
};
SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes));
NormalSpdyTransactionHelper helper(CreateGetRequest(), DEFAULT_PRIORITY,
NetLogWithSource(), NULL);
helper.RunPreTestSetup();
helper.AddData(&data);
HttpNetworkTransaction* trans = helper.trans();
TestCompletionCallback callback;
int rv = trans->Start(&CreateGetRequest(), callback.callback(),
NetLogWithSource());
EXPECT_THAT(callback.GetResult(rv), IsOk());
helper.ResetTrans();
base::RunLoop().RunUntilIdle();
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_F(SpdyNetworkTransactionTest, StartTransactionOnReadCallback) {
SpdySerializedFrame req(
spdy_util_.ConstructSpdyGet(nullptr, 0, 1, LOWEST, true));
MockWrite writes[] = {CreateMockWrite(req)};
MockWrite writes2[] = {CreateMockWrite(req, 0)};
// 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_t kGetBodyFrame2[] = {
0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00,
0x07, 'h', 'e', 'l', 'l', 'o', '!',
};
SpdySerializedFrame resp(spdy_util_.ConstructSpdyGetReply(NULL, 0, 1));
MockRead reads[] = {
CreateMockRead(resp, 1),
MockRead(ASYNC, ERR_IO_PENDING, 2), // Force a pause
MockRead(ASYNC, reinterpret_cast<const char*>(kGetBodyFrame2),
arraysize(kGetBodyFrame2), 3),
MockRead(ASYNC, ERR_IO_PENDING, 4), // Force a pause
MockRead(ASYNC, 0, 0, 5), // EOF
};
MockRead reads2[] = {
CreateMockRead(resp, 1), MockRead(ASYNC, 0, 0, 2), // EOF
};
SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes));
SequencedSocketData data2(reads2, arraysize(reads2), writes2,
arraysize(writes2));
NormalSpdyTransactionHelper helper(CreateGetRequest(), DEFAULT_PRIORITY,
NetLogWithSource(), 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(), NetLogWithSource());
EXPECT_THAT(rv, IsError(ERR_IO_PENDING));
rv = callback.WaitForResult();
const int kSize = 3000;
scoped_refptr<IOBuffer> buf(new IOBuffer(kSize));
rv = trans->Read(
buf.get(), kSize,
base::Bind(&SpdyNetworkTransactionTest::StartTransactionCallback,
helper.session(), default_url_));
ASSERT_THAT(rv, IsError(ERR_IO_PENDING));
// This forces an err_IO_pending, which sets the callback.
data.Resume();
data.RunUntilPaused();
// This finishes the read.
data.Resume();
base::RunLoop().RunUntilIdle();
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_F(SpdyNetworkTransactionTest, DeleteSessionOnReadCallback) {
SpdySerializedFrame req(
spdy_util_.ConstructSpdyGet(nullptr, 0, 1, LOWEST, true));
MockWrite writes[] = {CreateMockWrite(req, 0)};
SpdySerializedFrame resp(spdy_util_.ConstructSpdyGetReply(NULL, 0, 1));
SpdySerializedFrame body(spdy_util_.ConstructSpdyDataFrame(1, true));
MockRead reads[] = {
CreateMockRead(resp, 1),
MockRead(ASYNC, ERR_IO_PENDING, 2), // Force a pause
CreateMockRead(body, 3), MockRead(ASYNC, 0, 0, 4), // EOF
};
SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes));
NormalSpdyTransactionHelper helper(CreateGetRequest(), DEFAULT_PRIORITY,
NetLogWithSource(), 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(), NetLogWithSource());
EXPECT_THAT(rv, IsError(ERR_IO_PENDING));
rv = callback.WaitForResult();
// Setup a user callback which will delete the session, and clear out the
// memory holding the stream object. Note that the callback deletes trans.
const int kSize = 3000;
scoped_refptr<IOBuffer> buf(new IOBuffer(kSize));
rv = trans->Read(
buf.get(),
kSize,
base::Bind(&SpdyNetworkTransactionTest::DeleteSessionCallback,
base::Unretained(&helper)));
ASSERT_THAT(rv, IsError(ERR_IO_PENDING));
data.Resume();
// Finish running rest of tasks.
base::RunLoop().RunUntilIdle();
helper.VerifyDataConsumed();
}
TEST_F(SpdyNetworkTransactionTest, TestRawHeaderSizeSuccessfullRequest) {
SpdyHeaderBlock headers(spdy_util_.ConstructGetHeaderBlock(kDefaultUrl));
headers["user-agent"] = "";
headers["accept-encoding"] = "gzip, deflate";
SpdySerializedFrame req(
spdy_util_.ConstructSpdyHeaders(1, std::move(headers), LOWEST, true));
MockWrite writes[] = {
CreateMockWrite(req, 0),
};
SpdySerializedFrame resp(spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1));
SpdySerializedFrame response_body_frame(
spdy_util_.ConstructSpdyDataFrame(1, "should not include", 18, true));
MockRead response_headers(CreateMockRead(resp, 1));
MockRead reads[] = {
response_headers, CreateMockRead(response_body_frame, 2),
MockRead(ASYNC, 0, 0, 3) // EOF
};
SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes));
TestDelegate delegate;
SpdyURLRequestContext spdy_url_request_context;
TestNetworkDelegate network_delegate;
spdy_url_request_context.set_network_delegate(&network_delegate);
SSLSocketDataProvider ssl_data(ASYNC, OK);
ssl_data.next_proto = kProtoHTTP2;
std::unique_ptr<URLRequest> request(spdy_url_request_context.CreateRequest(
GURL(kDefaultUrl), DEFAULT_PRIORITY, &delegate));
spdy_url_request_context.socket_factory().AddSSLSocketDataProvider(&ssl_data);
spdy_url_request_context.socket_factory().AddSocketDataProvider(&data);
request->Start();
base::RunLoop().Run();
EXPECT_LT(0, request->GetTotalSentBytes());
EXPECT_LT(0, request->GetTotalReceivedBytes());
EXPECT_EQ(network_delegate.total_network_bytes_sent(),
request->GetTotalSentBytes());
EXPECT_EQ(network_delegate.total_network_bytes_received(),
request->GetTotalReceivedBytes());
EXPECT_EQ(response_headers.data_len, request->raw_header_size());
EXPECT_TRUE(data.AllReadDataConsumed());
EXPECT_TRUE(data.AllWriteDataConsumed());
}
TEST_F(SpdyNetworkTransactionTest,
TestRawHeaderSizeSuccessfullPushHeadersFirst) {
SpdyHeaderBlock headers(spdy_util_.ConstructGetHeaderBlock(kDefaultUrl));
headers["user-agent"] = "";
headers["accept-encoding"] = "gzip, deflate";
SpdySerializedFrame req(
spdy_util_.ConstructSpdyHeaders(1, std::move(headers), LOWEST, true));
MockWrite writes[] = {
CreateMockWrite(req, 0),
};
SpdySerializedFrame resp(spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1));
SpdySerializedFrame response_body_frame(
spdy_util_.ConstructSpdyDataFrame(1, "should not include", 18, true));
SpdyHeaderBlock push_headers;
spdy_util_.AddUrlToHeaderBlock(std::string(kDefaultUrl) + "b.dat",
&push_headers);
SpdySerializedFrame push_init_frame(
spdy_util_.ConstructInitialSpdyPushFrame(std::move(push_headers), 2, 1));
SpdySerializedFrame push_headers_frame(
spdy_util_.ConstructSpdyPushHeaders(2, nullptr, 0));
SpdySerializedFrame push_body_frame(spdy_util_.ConstructSpdyDataFrame(
2, "should not include either", 25, false));
MockRead push_init_read(CreateMockRead(push_init_frame, 1));
MockRead response_headers(CreateMockRead(resp, 4));
// raw_header_size() will contain the size of the push promise frame
// initialization.
int expected_response_headers_size =
response_headers.data_len + push_init_read.data_len;
MockRead reads[] = {
push_init_read,
CreateMockRead(push_headers_frame, 2),
CreateMockRead(push_body_frame, 3),
response_headers,
CreateMockRead(response_body_frame, 5),
MockRead(ASYNC, 0, 6) // EOF
};
SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes));
TestDelegate delegate;
SpdyURLRequestContext spdy_url_request_context;
TestNetworkDelegate network_delegate;
spdy_url_request_context.set_network_delegate(&network_delegate);
SSLSocketDataProvider ssl_data(ASYNC, OK);
ssl_data.next_proto = kProtoHTTP2;
std::unique_ptr<URLRequest> request(spdy_url_request_context.CreateRequest(
GURL(kDefaultUrl), DEFAULT_PRIORITY, &delegate));
spdy_url_request_context.socket_factory().AddSSLSocketDataProvider(&ssl_data);
spdy_url_request_context.socket_factory().AddSocketDataProvider(&data);
request->Start();
base::RunLoop().Run();
EXPECT_LT(0, request->GetTotalSentBytes());
EXPECT_LT(0, request->GetTotalReceivedBytes());
EXPECT_EQ(network_delegate.total_network_bytes_sent(),
request->GetTotalSentBytes());
EXPECT_EQ(network_delegate.total_network_bytes_received(),
request->GetTotalReceivedBytes());
EXPECT_EQ(expected_response_headers_size, request->raw_header_size());
EXPECT_TRUE(data.AllReadDataConsumed());
EXPECT_TRUE(data.AllWriteDataConsumed());
}
// Send a spdy request to www.example.org that gets redirected to www.foo.com.
TEST_F(SpdyNetworkTransactionTest, DISABLED_RedirectGetRequest) {
SpdyHeaderBlock headers(spdy_util_.ConstructGetHeaderBlock(kDefaultUrl));
headers["user-agent"] = "";
headers["accept-encoding"] = "gzip, deflate";
// Setup writes/reads to www.example.org
SpdySerializedFrame req(
spdy_util_.ConstructSpdyHeaders(1, std::move(headers), LOWEST, true));
SpdySerializedFrame resp(spdy_util_.ConstructSpdyGetReplyRedirect(1));
MockWrite writes[] = {
CreateMockWrite(req, 1),
};
MockRead reads[] = {
CreateMockRead(resp, 2), MockRead(ASYNC, 0, 0, 3) // EOF
};
SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes));
// Setup writes/reads to www.foo.com
SpdyHeaderBlock headers2(
spdy_util_.ConstructGetHeaderBlock("http://www.foo.com/index.php"));
headers2["user-agent"] = "";
headers2["accept-encoding"] = "gzip, deflate";
SpdySerializedFrame req2(
spdy_util_.ConstructSpdyHeaders(1, std::move(headers2), LOWEST, true));
MockWrite writes2[] = {
CreateMockWrite(req2, 1),
};
SpdySerializedFrame resp2(spdy_util_.ConstructSpdyGetReply(NULL, 0, 1));
SpdySerializedFrame body2(spdy_util_.ConstructSpdyDataFrame(1, true));
MockRead reads2[] = {
CreateMockRead(resp2, 2), CreateMockRead(body2, 3),
MockRead(ASYNC, 0, 0, 4) // EOF
};
SequencedSocketData data2(reads2, arraysize(reads2), writes2,
arraysize(writes2));
// TODO(erikchen): Make test support SPDYSSL, SPDYNPN
TestDelegate d;
{
SpdyURLRequestContext spdy_url_request_context;
std::unique_ptr<URLRequest> r(spdy_url_request_context.CreateRequest(
default_url_, DEFAULT_PRIORITY, &d));
spdy_url_request_context.socket_factory().
AddSocketDataProvider(&data);
spdy_url_request_context.socket_factory().
AddSocketDataProvider(&data2);
d.set_quit_on_redirect(true);
r->Start();
base::RunLoop().Run();
EXPECT_EQ(1, d.received_redirect_count());
r->FollowDeferredRedirect();
base::RunLoop().Run();
EXPECT_EQ(1, d.response_started_count());
EXPECT_FALSE(d.received_data_before_response());
EXPECT_EQ(OK, d.request_status());
std::string contents("hello!");
EXPECT_EQ(contents, d.data_received());
}
EXPECT_TRUE(data.AllReadDataConsumed());
EXPECT_TRUE(data.AllWriteDataConsumed());
EXPECT_TRUE(data2.AllReadDataConsumed());
EXPECT_TRUE(data2.AllWriteDataConsumed());
}
// Send a spdy request to www.example.org. Get a pushed stream that redirects to
// www.foo.com.
TEST_F(SpdyNetworkTransactionTest, DISABLED_RedirectServerPush) {
SpdyHeaderBlock headers(spdy_util_.ConstructGetHeaderBlock(kDefaultUrl));
headers["user-agent"] = "";
headers["accept-encoding"] = "gzip, deflate";
// Setup writes/reads to www.example.org
SpdySerializedFrame req(
spdy_util_.ConstructSpdyHeaders(1, std::move(headers), LOWEST, true));
SpdySerializedFrame resp(spdy_util_.ConstructSpdyGetReply(NULL, 0, 1));
SpdySerializedFrame rep(spdy_util_.ConstructSpdyPush(
NULL, 0, 2, 1, GetDefaultUrlWithPath("/foo.dat").c_str(),
"301 Moved Permanently", "http://www.foo.com/index.php"));
SpdySerializedFrame body(spdy_util_.ConstructSpdyDataFrame(1, true));
SpdySerializedFrame rst(
spdy_util_.ConstructSpdyRstStream(2, RST_STREAM_CANCEL));
MockWrite writes[] = {
CreateMockWrite(req, 1), CreateMockWrite(rst, 6),
};
MockRead reads[] = {
CreateMockRead(resp, 2), CreateMockRead(rep, 3), CreateMockRead(body, 4),
MockRead(ASYNC, ERR_IO_PENDING, 5), // Force a pause
MockRead(ASYNC, 0, 0, 7) // EOF
};
// Setup writes/reads to www.foo.com
SpdyHeaderBlock headers2(
spdy_util_.ConstructGetHeaderBlock("http://www.foo.com/index.php"));
headers2["user-agent"] = "";
headers2["accept-encoding"] = "gzip, deflate";
SpdySerializedFrame req2(
spdy_util_.ConstructSpdyHeaders(1, std::move(headers2), LOWEST, true));
SpdySerializedFrame resp2(spdy_util_.ConstructSpdyGetReply(NULL, 0, 1));
SpdySerializedFrame body2(spdy_util_.ConstructSpdyDataFrame(1, true));
MockWrite writes2[] = {
CreateMockWrite(req2, 1),
};
MockRead reads2[] = {
CreateMockRead(resp2, 2), CreateMockRead(body2, 3),
MockRead(ASYNC, 0, 0, 5) // EOF
};
SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes));
SequencedSocketData data2(reads2, arraysize(reads2), writes2,
arraysize(writes2));
TestDelegate d;
TestDelegate d2;
SpdyURLRequestContext spdy_url_request_context;
{
std::unique_ptr<URLRequest> r(spdy_url_request_context.CreateRequest(
default_url_, DEFAULT_PRIORITY, &d));
spdy_url_request_context.socket_factory().
AddSocketDataProvider(&data);
r->Start();
base::RunLoop().Run();
EXPECT_EQ(0, d.received_redirect_count());
std::string contents("hello!");
EXPECT_EQ(contents, d.data_received());
std::unique_ptr<URLRequest> r2(spdy_url_request_context.CreateRequest(
GURL(GetDefaultUrlWithPath("/foo.dat")), DEFAULT_PRIORITY, &d2));
spdy_url_request_context.socket_factory().
AddSocketDataProvider(&data2);
d2.set_quit_on_redirect(true);
r2->Start();
base::RunLoop().Run();
EXPECT_EQ(1, d2.received_redirect_count());
r2->FollowDeferredRedirect();
base::RunLoop().Run();
EXPECT_EQ(1, d2.response_started_count());
EXPECT_FALSE(d2.received_data_before_response());
EXPECT_EQ(OK, d2.request_status());
std::string contents2("hello!");
EXPECT_EQ(contents2, d2.data_received());
}
EXPECT_TRUE(data.AllReadDataConsumed());
EXPECT_TRUE(data.AllWriteDataConsumed());
EXPECT_TRUE(data2.AllReadDataConsumed());
EXPECT_TRUE(data2.AllWriteDataConsumed());
}
TEST_F(SpdyNetworkTransactionTest, ServerPushSingleDataFrame) {
SpdySerializedFrame stream1_syn(
spdy_util_.ConstructSpdyGet(nullptr, 0, 1, LOWEST, true));
SpdySerializedFrame stream1_body(spdy_util_.ConstructSpdyDataFrame(1, true));
MockWrite writes[] = {
CreateMockWrite(stream1_syn, 0),
};
SpdySerializedFrame stream1_reply(
spdy_util_.ConstructSpdyGetReply(NULL, 0, 1));
SpdySerializedFrame stream2_syn(spdy_util_.ConstructSpdyPush(
NULL, 0, 2, 1, GetDefaultUrlWithPath("/foo.dat").c_str()));
const char kPushedData[] = "pushed";
SpdySerializedFrame stream2_body(spdy_util_.ConstructSpdyDataFrame(
2, kPushedData, strlen(kPushedData), true));
MockRead reads[] = {
CreateMockRead(stream1_reply, 1),
CreateMockRead(stream2_syn, 2),
CreateMockRead(stream1_body, 3, SYNCHRONOUS),
CreateMockRead(stream2_body, 4),
MockRead(SYNCHRONOUS, ERR_IO_PENDING, 5), // Force a pause
};
HttpResponseInfo response;
HttpResponseInfo response2;
std::string expected_push_result("pushed");
SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes));
RunServerPushTest(&data,
&response,
&response2,
expected_push_result);
// Verify the response headers.
EXPECT_TRUE(response.headers);
EXPECT_EQ("HTTP/1.1 200", response.headers->GetStatusLine());
// Verify the pushed stream.
EXPECT_TRUE(response2.headers);
EXPECT_EQ("HTTP/1.1 200", response2.headers->GetStatusLine());
}
TEST_F(SpdyNetworkTransactionTest, ServerPushBeforeHeaders) {
SpdySerializedFrame stream1_syn(
spdy_util_.ConstructSpdyGet(nullptr, 0, 1, LOWEST, true));
MockWrite writes[] = {
CreateMockWrite(stream1_syn, 0),
};
SpdySerializedFrame stream2_syn(spdy_util_.ConstructSpdyPush(
nullptr, 0, 2, 1, GetDefaultUrlWithPath("/foo.dat").c_str()));
SpdySerializedFrame stream1_reply(
spdy_util_.ConstructSpdyGetReply(NULL, 0, 1));
SpdySerializedFrame stream1_body(spdy_util_.ConstructSpdyDataFrame(1, true));
const char kPushedData[] = "pushed";
SpdySerializedFrame stream2_body(spdy_util_.ConstructSpdyDataFrame(
2, kPushedData, strlen(kPushedData), true));
MockRead reads[] = {
CreateMockRead(stream2_syn, 1),