OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "net/spdy/bidirectional_spdy_stream.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/run_loop.h" |
| 11 #include "base/stl_util.h" |
| 12 #include "base/strings/string_piece.h" |
| 13 #include "net/http/http_request_info.h" |
| 14 #include "net/http/http_response_headers.h" |
| 15 #include "net/http/http_response_info.h" |
| 16 #include "net/socket/socket_test_util.h" |
| 17 #include "net/spdy/spdy_read_queue.h" |
| 18 #include "net/spdy/spdy_session.h" |
| 19 #include "net/spdy/spdy_test_util_common.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 |
| 22 namespace net { |
| 23 |
| 24 namespace { |
| 25 |
| 26 const char kBodyData[] = "Body data"; |
| 27 const size_t kBodyDataSize = arraysize(kBodyData); |
| 28 const base::StringPiece kBodyDataStringPiece(kBodyData, kBodyDataSize); |
| 29 // Size of the buffer to be allocated for each read. |
| 30 const size_t kReadBufferSize = 4096; |
| 31 |
| 32 class TestBidirectionalStreamDelegate : public BidirectionalStream::Delegate { |
| 33 public: |
| 34 TestBidirectionalStreamDelegate(base::WeakPtr<SpdySession> session, |
| 35 IOBuffer* buf, |
| 36 int buf_len) |
| 37 : bytes_read_(0), |
| 38 stream_(new BidirectionalSpdyStream(session)), |
| 39 loop_(new base::RunLoop), |
| 40 buf_(buf), |
| 41 buf_len_(buf_len) {} |
| 42 |
| 43 ~TestBidirectionalStreamDelegate() override {} |
| 44 |
| 45 void OnFailed(int rv) override { loop_->Quit(); } |
| 46 |
| 47 void OnRequestHeadersSent() override {} |
| 48 |
| 49 void OnHeaders(const SpdyHeaderBlock& response_headers) override { |
| 50 response_headers_ = response_headers; |
| 51 int rv = stream_->ReadData(buf_.get(), buf_len_); |
| 52 if (rv != ERR_IO_PENDING) |
| 53 bytes_read_ += rv; |
| 54 } |
| 55 |
| 56 void OnReadCompleted(int bytes_read) override { |
| 57 DCHECK_GT(bytes_read, OK); |
| 58 bytes_read_ += bytes_read; |
| 59 } |
| 60 |
| 61 void OnDataSent() override {} |
| 62 |
| 63 void OnTrailers(const SpdyHeaderBlock& trailers) override { |
| 64 trailers_ = trailers; |
| 65 } |
| 66 |
| 67 void OnClose(int status) override { loop_->Quit(); } |
| 68 |
| 69 void Start(HttpRequestInfo* request, |
| 70 RequestPriority priority, |
| 71 const BoundNetLog& net_log) { |
| 72 stream_->Start(request, priority, net_log, this); |
| 73 loop_->Run(); |
| 74 } |
| 75 |
| 76 void SendData(IOBuffer* data, int length, bool end_of_stream) { |
| 77 stream_->SendData(data, length, end_of_stream); |
| 78 } |
| 79 |
| 80 SpdyHeaderBlock response_headers_; |
| 81 SpdyHeaderBlock trailers_; |
| 82 int bytes_read_; |
| 83 |
| 84 protected: |
| 85 scoped_ptr<BidirectionalStream> stream_; |
| 86 scoped_ptr<base::RunLoop> loop_; |
| 87 |
| 88 private: |
| 89 scoped_refptr<IOBuffer> buf_; |
| 90 int buf_len_; |
| 91 }; |
| 92 |
| 93 // A delegate that sends data after request headers are sent. |
| 94 class SendDataDelegate : public TestBidirectionalStreamDelegate { |
| 95 public: |
| 96 SendDataDelegate(base::WeakPtr<SpdySession> session, |
| 97 IOBuffer* buf, |
| 98 int buf_len, |
| 99 base::StringPiece data) |
| 100 : TestBidirectionalStreamDelegate(session, buf, buf_len), data_(data) {} |
| 101 |
| 102 ~SendDataDelegate() override {} |
| 103 |
| 104 void OnRequestHeadersSent() override { |
| 105 TestBidirectionalStreamDelegate::OnRequestHeadersSent(); |
| 106 if (data_.data()) { |
| 107 scoped_refptr<StringIOBuffer> buf(new StringIOBuffer(data_.as_string())); |
| 108 SendData(buf.get(), buf->size(), NO_MORE_DATA_TO_SEND); |
| 109 } |
| 110 } |
| 111 |
| 112 private: |
| 113 base::StringPiece data_; |
| 114 }; |
| 115 |
| 116 } // namespace |
| 117 |
| 118 class BidirectionalSpdyStreamTest : public testing::Test { |
| 119 public: |
| 120 BidirectionalSpdyStreamTest() |
| 121 : spdy_util_(kProtoHTTP2), session_deps_(kProtoHTTP2) {} |
| 122 |
| 123 protected: |
| 124 void TearDown() override { |
| 125 EXPECT_TRUE(sequenced_data_->AllReadDataConsumed()); |
| 126 EXPECT_TRUE(sequenced_data_->AllWriteDataConsumed()); |
| 127 } |
| 128 |
| 129 // Initializes the session using SequencedSocketData. |
| 130 void InitSession(MockRead* reads, |
| 131 size_t reads_count, |
| 132 MockWrite* writes, |
| 133 size_t writes_count, |
| 134 const SpdySessionKey& key) { |
| 135 sequenced_data_.reset( |
| 136 new SequencedSocketData(reads, reads_count, writes, writes_count)); |
| 137 session_deps_.socket_factory->AddSocketDataProvider(sequenced_data_.get()); |
| 138 http_session_ = SpdySessionDependencies::SpdyCreateSession(&session_deps_); |
| 139 session_ = CreateInsecureSpdySession(http_session_, key, BoundNetLog()); |
| 140 } |
| 141 |
| 142 SpdyTestUtil spdy_util_; |
| 143 SpdySessionDependencies session_deps_; |
| 144 scoped_ptr<SequencedSocketData> sequenced_data_; |
| 145 scoped_refptr<HttpNetworkSession> http_session_; |
| 146 base::WeakPtr<SpdySession> session_; |
| 147 }; |
| 148 |
| 149 TEST_F(BidirectionalSpdyStreamTest, SendGetRequest) { |
| 150 scoped_ptr<SpdyFrame> req( |
| 151 spdy_util_.ConstructSpdyGet(NULL, 0, false, 1, LOWEST, true)); |
| 152 MockWrite writes[] = { |
| 153 CreateMockWrite(*req.get(), 0), |
| 154 }; |
| 155 |
| 156 const char* const kExtraResponseHeaders[] = {"header-name", "header-value"}; |
| 157 |
| 158 scoped_ptr<SpdyFrame> resp( |
| 159 spdy_util_.ConstructSpdyGetSynReply(kExtraResponseHeaders, 1, 1)); |
| 160 |
| 161 scoped_ptr<SpdyFrame> body(spdy_util_.ConstructSpdyBodyFrame(1, false)); |
| 162 |
| 163 const char* const kTrailers[] = {"foo", "bar"}; |
| 164 scoped_ptr<SpdyFrame> trailers( |
| 165 spdy_util_.ConstructSpdyHeaderFrame(1, kTrailers, 1, true)); |
| 166 |
| 167 MockRead reads[] = { |
| 168 CreateMockRead(*resp, 1), CreateMockRead(*body, 2), |
| 169 CreateMockRead(*trailers, 3), MockRead(SYNCHRONOUS, 0, 4), |
| 170 }; |
| 171 |
| 172 HostPortPair host_port_pair("www.example.org", 80); |
| 173 SpdySessionKey key(host_port_pair, ProxyServer::Direct(), |
| 174 PRIVACY_MODE_DISABLED); |
| 175 InitSession(reads, arraysize(reads), writes, arraysize(writes), key); |
| 176 |
| 177 HttpRequestInfo request; |
| 178 request.method = "GET"; |
| 179 request.url = GURL("http://www.example.org/"); |
| 180 |
| 181 scoped_refptr<IOBuffer> read_buffer(new IOBuffer(kReadBufferSize)); |
| 182 scoped_ptr<TestBidirectionalStreamDelegate> delegate( |
| 183 new TestBidirectionalStreamDelegate(session_, read_buffer.get(), |
| 184 kReadBufferSize)); |
| 185 BoundNetLog net_log; |
| 186 delegate->Start(&request, DEFAULT_PRIORITY, net_log); |
| 187 base::StringPiece status = delegate->response_headers_[":status"]; |
| 188 EXPECT_EQ("200", status); |
| 189 base::StringPiece extra_header = delegate->response_headers_["header-name"]; |
| 190 EXPECT_EQ("header-value", extra_header); |
| 191 EXPECT_EQ(kUploadData, |
| 192 std::string(read_buffer->data(), delegate->bytes_read_)); |
| 193 base::StringPiece trailer = delegate->trailers_["foo"]; |
| 194 EXPECT_EQ("bar", trailer); |
| 195 } |
| 196 |
| 197 TEST_F(BidirectionalSpdyStreamTest, SendPostRequest) { |
| 198 BufferedSpdyFramer framer(spdy_util_.spdy_version(), false); |
| 199 |
| 200 scoped_ptr<SpdyFrame> req(spdy_util_.ConstructChunkedSpdyPost(NULL, 0)); |
| 201 scoped_ptr<SpdyFrame> body( |
| 202 framer.CreateDataFrame(1, kBodyData, kBodyDataSize, DATA_FLAG_FIN)); |
| 203 MockWrite writes[] = { |
| 204 CreateMockWrite(*req, 0), // request |
| 205 CreateMockWrite(*body, 1), // POST upload frame |
| 206 }; |
| 207 const char* const kExtraResponseHeaders[] = {"header-name", "header-value"}; |
| 208 scoped_ptr<SpdyFrame> resp( |
| 209 spdy_util_.ConstructSpdyPostSynReply(kExtraResponseHeaders, 1)); |
| 210 |
| 211 scoped_ptr<SpdyFrame> resp_data( |
| 212 framer.CreateDataFrame(1, kBodyData, kBodyDataSize, DATA_FLAG_NONE)); |
| 213 |
| 214 const char* const kExtraHeaders[] = {"foo", "bar"}; |
| 215 scoped_ptr<SpdyFrame> trailers( |
| 216 spdy_util_.ConstructSpdyHeaderFrame(1, kExtraHeaders, 1, true)); |
| 217 |
| 218 MockRead reads[] = { |
| 219 CreateMockRead(*resp, 2), CreateMockRead(*resp_data, 3), |
| 220 CreateMockRead(*trailers, 4), MockRead(SYNCHRONOUS, 0, 5) // EOF |
| 221 }; |
| 222 |
| 223 HostPortPair host_port_pair("www.example.org", 80); |
| 224 SpdySessionKey key(host_port_pair, ProxyServer::Direct(), |
| 225 PRIVACY_MODE_DISABLED); |
| 226 InitSession(reads, arraysize(reads), writes, arraysize(writes), key); |
| 227 |
| 228 HttpRequestInfo request; |
| 229 request.method = "POST"; |
| 230 request.url = GURL("http://www.example.org/"); |
| 231 |
| 232 scoped_refptr<IOBuffer> read_buffer(new IOBuffer(kReadBufferSize)); |
| 233 scoped_ptr<TestBidirectionalStreamDelegate> delegate(new SendDataDelegate( |
| 234 session_, read_buffer.get(), kReadBufferSize, kBodyDataStringPiece)); |
| 235 BoundNetLog net_log; |
| 236 delegate->Start(&request, DEFAULT_PRIORITY, net_log); |
| 237 |
| 238 base::StringPiece header = delegate->response_headers_[":status"]; |
| 239 EXPECT_EQ("200", header); |
| 240 base::StringPiece extra_header = delegate->response_headers_["header-name"]; |
| 241 EXPECT_EQ("header-value", extra_header); |
| 242 EXPECT_EQ(std::string(kBodyData, kBodyDataSize), |
| 243 std::string(read_buffer->data(), delegate->bytes_read_)); |
| 244 base::StringPiece trailer = delegate->trailers_["foo"]; |
| 245 EXPECT_EQ("bar", trailer); |
| 246 } |
| 247 |
| 248 } // namespace net |
OLD | NEW |