| Index: net/spdy/bidirectional_spdy_stream_unittest.cc
|
| diff --git a/net/spdy/bidirectional_spdy_stream_unittest.cc b/net/spdy/bidirectional_spdy_stream_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..79198450877fe989893b45d5bd7d58192c7d5e09
|
| --- /dev/null
|
| +++ b/net/spdy/bidirectional_spdy_stream_unittest.cc
|
| @@ -0,0 +1,248 @@
|
| +// Copyright 2015 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "net/spdy/bidirectional_spdy_stream.h"
|
| +
|
| +#include <string>
|
| +
|
| +#include "base/memory/scoped_ptr.h"
|
| +#include "base/run_loop.h"
|
| +#include "base/stl_util.h"
|
| +#include "base/strings/string_piece.h"
|
| +#include "net/http/http_request_info.h"
|
| +#include "net/http/http_response_headers.h"
|
| +#include "net/http/http_response_info.h"
|
| +#include "net/socket/socket_test_util.h"
|
| +#include "net/spdy/spdy_read_queue.h"
|
| +#include "net/spdy/spdy_session.h"
|
| +#include "net/spdy/spdy_test_util_common.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +namespace net {
|
| +
|
| +namespace {
|
| +
|
| +const char kBodyData[] = "Body data";
|
| +const size_t kBodyDataSize = arraysize(kBodyData);
|
| +const base::StringPiece kBodyDataStringPiece(kBodyData, kBodyDataSize);
|
| +// Size of the buffer to be allocated for each read.
|
| +const size_t kReadBufferSize = 4096;
|
| +
|
| +class TestBidirectionalStreamDelegate : public BidirectionalStream::Delegate {
|
| + public:
|
| + TestBidirectionalStreamDelegate(base::WeakPtr<SpdySession> session,
|
| + IOBuffer* buf,
|
| + int buf_len)
|
| + : bytes_read_(0),
|
| + stream_(new BidirectionalSpdyStream(session)),
|
| + loop_(new base::RunLoop),
|
| + buf_(buf),
|
| + buf_len_(buf_len) {}
|
| +
|
| + ~TestBidirectionalStreamDelegate() override {}
|
| +
|
| + void OnFailed(int rv) override { loop_->Quit(); }
|
| +
|
| + void OnRequestHeadersSent() override {}
|
| +
|
| + void OnHeaders(const SpdyHeaderBlock& response_headers) override {
|
| + response_headers_ = response_headers;
|
| + int rv = stream_->ReadData(buf_.get(), buf_len_);
|
| + if (rv != ERR_IO_PENDING)
|
| + bytes_read_ += rv;
|
| + }
|
| +
|
| + void OnReadCompleted(int bytes_read) override {
|
| + DCHECK_GT(bytes_read, OK);
|
| + bytes_read_ += bytes_read;
|
| + }
|
| +
|
| + void OnDataSent() override {}
|
| +
|
| + void OnTrailers(const SpdyHeaderBlock& trailers) override {
|
| + trailers_ = trailers;
|
| + }
|
| +
|
| + void OnClose(int status) override { loop_->Quit(); }
|
| +
|
| + void Start(HttpRequestInfo* request,
|
| + RequestPriority priority,
|
| + const BoundNetLog& net_log) {
|
| + stream_->Start(request, priority, net_log, this);
|
| + loop_->Run();
|
| + }
|
| +
|
| + void SendData(IOBuffer* data, int length, bool end_of_stream) {
|
| + stream_->SendData(data, length, end_of_stream);
|
| + }
|
| +
|
| + SpdyHeaderBlock response_headers_;
|
| + SpdyHeaderBlock trailers_;
|
| + int bytes_read_;
|
| +
|
| + protected:
|
| + scoped_ptr<BidirectionalStream> stream_;
|
| + scoped_ptr<base::RunLoop> loop_;
|
| +
|
| + private:
|
| + scoped_refptr<IOBuffer> buf_;
|
| + int buf_len_;
|
| +};
|
| +
|
| +// A delegate that sends data after request headers are sent.
|
| +class SendDataDelegate : public TestBidirectionalStreamDelegate {
|
| + public:
|
| + SendDataDelegate(base::WeakPtr<SpdySession> session,
|
| + IOBuffer* buf,
|
| + int buf_len,
|
| + base::StringPiece data)
|
| + : TestBidirectionalStreamDelegate(session, buf, buf_len), data_(data) {}
|
| +
|
| + ~SendDataDelegate() override {}
|
| +
|
| + void OnRequestHeadersSent() override {
|
| + TestBidirectionalStreamDelegate::OnRequestHeadersSent();
|
| + if (data_.data()) {
|
| + scoped_refptr<StringIOBuffer> buf(new StringIOBuffer(data_.as_string()));
|
| + SendData(buf.get(), buf->size(), NO_MORE_DATA_TO_SEND);
|
| + }
|
| + }
|
| +
|
| + private:
|
| + base::StringPiece data_;
|
| +};
|
| +
|
| +} // namespace
|
| +
|
| +class BidirectionalSpdyStreamTest : public testing::Test {
|
| + public:
|
| + BidirectionalSpdyStreamTest()
|
| + : spdy_util_(kProtoHTTP2), session_deps_(kProtoHTTP2) {}
|
| +
|
| + protected:
|
| + void TearDown() override {
|
| + EXPECT_TRUE(sequenced_data_->AllReadDataConsumed());
|
| + EXPECT_TRUE(sequenced_data_->AllWriteDataConsumed());
|
| + }
|
| +
|
| + // Initializes the session using SequencedSocketData.
|
| + void InitSession(MockRead* reads,
|
| + size_t reads_count,
|
| + MockWrite* writes,
|
| + size_t writes_count,
|
| + const SpdySessionKey& key) {
|
| + sequenced_data_.reset(
|
| + new SequencedSocketData(reads, reads_count, writes, writes_count));
|
| + session_deps_.socket_factory->AddSocketDataProvider(sequenced_data_.get());
|
| + http_session_ = SpdySessionDependencies::SpdyCreateSession(&session_deps_);
|
| + session_ = CreateInsecureSpdySession(http_session_, key, BoundNetLog());
|
| + }
|
| +
|
| + SpdyTestUtil spdy_util_;
|
| + SpdySessionDependencies session_deps_;
|
| + scoped_ptr<SequencedSocketData> sequenced_data_;
|
| + scoped_refptr<HttpNetworkSession> http_session_;
|
| + base::WeakPtr<SpdySession> session_;
|
| +};
|
| +
|
| +TEST_F(BidirectionalSpdyStreamTest, SendGetRequest) {
|
| + scoped_ptr<SpdyFrame> req(
|
| + spdy_util_.ConstructSpdyGet(NULL, 0, false, 1, LOWEST, true));
|
| + MockWrite writes[] = {
|
| + CreateMockWrite(*req.get(), 0),
|
| + };
|
| +
|
| + const char* const kExtraResponseHeaders[] = {"header-name", "header-value"};
|
| +
|
| + scoped_ptr<SpdyFrame> resp(
|
| + spdy_util_.ConstructSpdyGetSynReply(kExtraResponseHeaders, 1, 1));
|
| +
|
| + scoped_ptr<SpdyFrame> body(spdy_util_.ConstructSpdyBodyFrame(1, false));
|
| +
|
| + const char* const kTrailers[] = {"foo", "bar"};
|
| + scoped_ptr<SpdyFrame> trailers(
|
| + spdy_util_.ConstructSpdyHeaderFrame(1, kTrailers, 1, true));
|
| +
|
| + MockRead reads[] = {
|
| + CreateMockRead(*resp, 1), CreateMockRead(*body, 2),
|
| + CreateMockRead(*trailers, 3), MockRead(SYNCHRONOUS, 0, 4),
|
| + };
|
| +
|
| + HostPortPair host_port_pair("www.example.org", 80);
|
| + SpdySessionKey key(host_port_pair, ProxyServer::Direct(),
|
| + PRIVACY_MODE_DISABLED);
|
| + InitSession(reads, arraysize(reads), writes, arraysize(writes), key);
|
| +
|
| + HttpRequestInfo request;
|
| + request.method = "GET";
|
| + request.url = GURL("http://www.example.org/");
|
| +
|
| + scoped_refptr<IOBuffer> read_buffer(new IOBuffer(kReadBufferSize));
|
| + scoped_ptr<TestBidirectionalStreamDelegate> delegate(
|
| + new TestBidirectionalStreamDelegate(session_, read_buffer.get(),
|
| + kReadBufferSize));
|
| + BoundNetLog net_log;
|
| + delegate->Start(&request, DEFAULT_PRIORITY, net_log);
|
| + base::StringPiece status = delegate->response_headers_[":status"];
|
| + EXPECT_EQ("200", status);
|
| + base::StringPiece extra_header = delegate->response_headers_["header-name"];
|
| + EXPECT_EQ("header-value", extra_header);
|
| + EXPECT_EQ(kUploadData,
|
| + std::string(read_buffer->data(), delegate->bytes_read_));
|
| + base::StringPiece trailer = delegate->trailers_["foo"];
|
| + EXPECT_EQ("bar", trailer);
|
| +}
|
| +
|
| +TEST_F(BidirectionalSpdyStreamTest, SendPostRequest) {
|
| + BufferedSpdyFramer framer(spdy_util_.spdy_version(), false);
|
| +
|
| + scoped_ptr<SpdyFrame> req(spdy_util_.ConstructChunkedSpdyPost(NULL, 0));
|
| + scoped_ptr<SpdyFrame> body(
|
| + framer.CreateDataFrame(1, kBodyData, kBodyDataSize, DATA_FLAG_FIN));
|
| + MockWrite writes[] = {
|
| + CreateMockWrite(*req, 0), // request
|
| + CreateMockWrite(*body, 1), // POST upload frame
|
| + };
|
| + const char* const kExtraResponseHeaders[] = {"header-name", "header-value"};
|
| + scoped_ptr<SpdyFrame> resp(
|
| + spdy_util_.ConstructSpdyPostSynReply(kExtraResponseHeaders, 1));
|
| +
|
| + scoped_ptr<SpdyFrame> resp_data(
|
| + framer.CreateDataFrame(1, kBodyData, kBodyDataSize, DATA_FLAG_NONE));
|
| +
|
| + const char* const kExtraHeaders[] = {"foo", "bar"};
|
| + scoped_ptr<SpdyFrame> trailers(
|
| + spdy_util_.ConstructSpdyHeaderFrame(1, kExtraHeaders, 1, true));
|
| +
|
| + MockRead reads[] = {
|
| + CreateMockRead(*resp, 2), CreateMockRead(*resp_data, 3),
|
| + CreateMockRead(*trailers, 4), MockRead(SYNCHRONOUS, 0, 5) // EOF
|
| + };
|
| +
|
| + HostPortPair host_port_pair("www.example.org", 80);
|
| + SpdySessionKey key(host_port_pair, ProxyServer::Direct(),
|
| + PRIVACY_MODE_DISABLED);
|
| + InitSession(reads, arraysize(reads), writes, arraysize(writes), key);
|
| +
|
| + HttpRequestInfo request;
|
| + request.method = "POST";
|
| + request.url = GURL("http://www.example.org/");
|
| +
|
| + scoped_refptr<IOBuffer> read_buffer(new IOBuffer(kReadBufferSize));
|
| + scoped_ptr<TestBidirectionalStreamDelegate> delegate(new SendDataDelegate(
|
| + session_, read_buffer.get(), kReadBufferSize, kBodyDataStringPiece));
|
| + BoundNetLog net_log;
|
| + delegate->Start(&request, DEFAULT_PRIORITY, net_log);
|
| +
|
| + base::StringPiece header = delegate->response_headers_[":status"];
|
| + EXPECT_EQ("200", header);
|
| + base::StringPiece extra_header = delegate->response_headers_["header-name"];
|
| + EXPECT_EQ("header-value", extra_header);
|
| + EXPECT_EQ(std::string(kBodyData, kBodyDataSize),
|
| + std::string(read_buffer->data(), delegate->bytes_read_));
|
| + base::StringPiece trailer = delegate->trailers_["foo"];
|
| + EXPECT_EQ("bar", trailer);
|
| +}
|
| +
|
| +} // namespace net
|
|
|