Chromium Code Reviews| 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/http/bidirectional_stream.h" | |
| 6 | |
| 7 #include "base/macros.h" | |
| 8 #include "base/memory/scoped_ptr.h" | |
| 9 #include "base/run_loop.h" | |
| 10 #include "base/strings/string_piece.h" | |
| 11 #include "net/base/net_errors.h" | |
| 12 #include "net/base/test_data_directory.h" | |
| 13 #include "net/http/http_network_session.h" | |
| 14 #include "net/http/http_request_info.h" | |
| 15 #include "net/http/http_response_headers.h" | |
| 16 #include "net/log/net_log.h" | |
| 17 #include "net/socket/socket_test_util.h" | |
| 18 #include "net/spdy/spdy_session.h" | |
| 19 #include "net/spdy/spdy_test_util_common.h" | |
| 20 #include "net/spdy/spdy_test_util_common.h" | |
| 21 #include "net/test/cert_test_util.h" | |
| 22 #include "net/url_request/url_request_test_util.h" | |
| 23 #include "testing/gtest/include/gtest/gtest.h" | |
| 24 | |
| 25 namespace net { | |
| 26 | |
| 27 namespace { | |
| 28 | |
| 29 const char kBodyData[] = "Body data"; | |
| 30 const int kBodyDataSize = arraysize(kBodyData); | |
| 31 const base::StringPiece kBodyDataStringPiece(kBodyData, kBodyDataSize); | |
| 32 // Size of the buffer to be allocated for each read. | |
| 33 const size_t kReadBufferSize = 4096; | |
| 34 | |
| 35 // Delegate that automatically reads data from the stream, but does not send | |
| 36 // any data unless it is told to. | |
| 37 class TestBidirectionalStreamDelegate : public BidirectionalStream::Delegate { | |
| 38 public: | |
| 39 TestBidirectionalStreamDelegate(IOBuffer* read_buf, int read_buf_len) | |
| 40 : error_(OK), read_buf_(read_buf), read_buf_len_(read_buf_len) {} | |
| 41 | |
| 42 ~TestBidirectionalStreamDelegate() override {} | |
| 43 | |
| 44 void OnHeadersSent() override {} | |
| 45 | |
| 46 void OnHeadersReceived(const SpdyHeaderBlock& response_headers) override { | |
| 47 response_headers_ = response_headers; | |
| 48 StartOrContinueReading(); | |
| 49 } | |
| 50 | |
| 51 void OnDataRead(int bytes_read) override { | |
| 52 CHECK_GT(bytes_read, OK); | |
| 53 data_received_.append(read_buf_->data(), bytes_read); | |
| 54 StartOrContinueReading(); | |
| 55 } | |
| 56 | |
| 57 void OnDataSent() override {} | |
| 58 | |
| 59 void OnTrailersReceived(const SpdyHeaderBlock& trailers) override { | |
| 60 trailers_ = trailers; | |
| 61 loop_.Quit(); | |
| 62 } | |
| 63 | |
| 64 void OnFailed(int error) override { | |
| 65 CHECK_NE(OK, error); | |
|
mmenke
2015/12/11 16:42:53
CHECK_EQ(OK, error_);? Or can we get multiple err
xunjieli
2015/12/11 23:48:40
Done. I don't think we can get multiple OnFailed c
| |
| 66 error_ = error; | |
| 67 loop_.Quit(); | |
| 68 } | |
| 69 | |
| 70 void RunTest(const BidirectionalStream::RequestInfo& request_info, | |
| 71 RequestPriority priority, | |
| 72 HttpNetworkSession* session) { | |
| 73 stream_.reset( | |
| 74 new BidirectionalStream(request_info, priority, session, this)); | |
| 75 loop_.Run(); | |
| 76 } | |
| 77 | |
| 78 void SendData(IOBuffer* data, int length, bool end_of_stream) { | |
| 79 stream_->SendData(data, length, end_of_stream); | |
| 80 } | |
| 81 | |
| 82 // Const getters for internal states. | |
| 83 const std::string& data_received() const { return data_received_; } | |
| 84 int error() const { return error_; } | |
| 85 const SpdyHeaderBlock response_headers() const { return response_headers_; } | |
| 86 const SpdyHeaderBlock trailers() const { return trailers_; } | |
|
mmenke
2015/12/11 16:42:53
Return const references?
xunjieli
2015/12/11 23:48:40
Done.
| |
| 87 | |
| 88 private: | |
| 89 // Starts or continues reading data from |stream_| until no more bytes can be | |
| 90 // read synchronously. | |
| 91 void StartOrContinueReading() { | |
| 92 int rv = stream_->ReadData(read_buf_.get(), read_buf_len_); | |
| 93 while (rv > 0) { | |
| 94 data_received_.append(read_buf_->data(), rv); | |
| 95 rv = stream_->ReadData(read_buf_.get(), read_buf_len_); | |
| 96 } | |
| 97 if (rv == OK) | |
| 98 loop_.Quit(); | |
| 99 } | |
| 100 | |
| 101 SpdyHeaderBlock response_headers_; | |
| 102 SpdyHeaderBlock trailers_; | |
| 103 int error_; | |
| 104 scoped_refptr<IOBuffer> read_buf_; | |
| 105 int read_buf_len_; | |
| 106 std::string data_received_; | |
| 107 base::RunLoop loop_; | |
| 108 scoped_ptr<BidirectionalStream> stream_; | |
| 109 | |
| 110 DISALLOW_COPY_AND_ASSIGN(TestBidirectionalStreamDelegate); | |
| 111 }; | |
| 112 | |
| 113 // A delegate that sends data after request headers are sent. | |
| 114 class SendDataDelegate : public TestBidirectionalStreamDelegate { | |
| 115 public: | |
| 116 SendDataDelegate(IOBuffer* buf, int buf_len, base::StringPiece data) | |
| 117 : TestBidirectionalStreamDelegate(buf, buf_len), data_(data) {} | |
| 118 | |
| 119 ~SendDataDelegate() override {} | |
| 120 | |
| 121 void OnHeadersSent() override { | |
| 122 TestBidirectionalStreamDelegate::OnHeadersSent(); | |
| 123 if (data_.data()) { | |
| 124 scoped_refptr<StringIOBuffer> buf(new StringIOBuffer(data_.as_string())); | |
| 125 SendData(buf.get(), buf->size(), NO_MORE_DATA_TO_SEND); | |
| 126 } | |
| 127 } | |
| 128 | |
| 129 private: | |
| 130 base::StringPiece data_; | |
| 131 | |
| 132 DISALLOW_COPY_AND_ASSIGN(SendDataDelegate); | |
| 133 }; | |
| 134 | |
| 135 class BidirectionalStreamTest : public testing::Test { | |
| 136 public: | |
| 137 BidirectionalStreamTest() | |
| 138 : spdy_util_(kProtoHTTP2, false), session_deps_(kProtoHTTP2) {} | |
| 139 | |
| 140 protected: | |
| 141 // Initializes the session using SequencedSocketData. | |
| 142 void InitSession(MockRead* reads, | |
| 143 size_t reads_count, | |
| 144 MockWrite* writes, | |
| 145 size_t writes_count, | |
| 146 const SpdySessionKey& key) { | |
| 147 sequenced_data_.reset( | |
| 148 new SequencedSocketData(reads, reads_count, writes, writes_count)); | |
| 149 session_deps_.socket_factory->AddSocketDataProvider(sequenced_data_.get()); | |
| 150 http_session_ = SpdySessionDependencies::SpdyCreateSession(&session_deps_); | |
| 151 session_ = CreateSecureSpdySession(http_session_.get(), key, BoundNetLog()); | |
| 152 } | |
| 153 | |
| 154 SpdyTestUtil spdy_util_; | |
| 155 SpdySessionDependencies session_deps_; | |
| 156 scoped_ptr<SequencedSocketData> sequenced_data_; | |
| 157 scoped_ptr<HttpNetworkSession> http_session_; | |
| 158 base::WeakPtr<SpdySession> session_; | |
| 159 }; | |
| 160 | |
| 161 } // namespace | |
| 162 | |
| 163 TEST_F(BidirectionalStreamTest, CreateInsecureStream) { | |
| 164 BidirectionalStream::RequestInfo request; | |
| 165 request.method = "GET"; | |
| 166 request.url = GURL("http://www.example.org/"); | |
| 167 | |
| 168 TestBidirectionalStreamDelegate delegate(nullptr, 0); | |
| 169 HttpNetworkSession::Params params = | |
| 170 SpdySessionDependencies::CreateSessionParams(&session_deps_); | |
| 171 scoped_ptr<HttpNetworkSession> session(new HttpNetworkSession(params)); | |
| 172 delegate.RunTest(request, LOWEST, session.get()); | |
| 173 EXPECT_EQ(ERR_DISALLOWED_URL_SCHEME, delegate.error()); | |
| 174 } | |
| 175 | |
| 176 TEST_F(BidirectionalStreamTest, SendPostRequest) { | |
| 177 spdy_util_.set_default_url(GURL("https://www.example.org")); | |
| 178 BufferedSpdyFramer framer(spdy_util_.spdy_version(), false); | |
| 179 | |
| 180 scoped_ptr<SpdyFrame> req(spdy_util_.ConstructChunkedSpdyPost(nullptr, 0)); | |
| 181 scoped_ptr<SpdyFrame> body( | |
| 182 framer.CreateDataFrame(1, kBodyData, kBodyDataSize, DATA_FLAG_FIN)); | |
| 183 MockWrite writes[] = { | |
| 184 CreateMockWrite(*req, 0), // request | |
| 185 CreateMockWrite(*body, 1), // POST upload frame | |
| 186 }; | |
| 187 const char* const kExtraResponseHeaders[] = {"header-name", "header-value"}; | |
| 188 scoped_ptr<SpdyFrame> resp( | |
| 189 spdy_util_.ConstructSpdyPostSynReply(kExtraResponseHeaders, 1)); | |
| 190 | |
| 191 scoped_ptr<SpdyFrame> resp_data( | |
| 192 framer.CreateDataFrame(1, kBodyData, kBodyDataSize, DATA_FLAG_NONE)); | |
| 193 | |
| 194 const char* const kExtraHeaders[] = {"foo", "bar"}; | |
| 195 scoped_ptr<SpdyFrame> trailers( | |
| 196 spdy_util_.ConstructSpdyHeaderFrame(1, kExtraHeaders, 1, true)); | |
| 197 | |
| 198 MockRead reads[] = { | |
| 199 CreateMockRead(*resp, 2), CreateMockRead(*resp_data, 3), | |
| 200 CreateMockRead(*trailers, 4), MockRead(SYNCHRONOUS, 0, 5) // EOF | |
| 201 }; | |
| 202 | |
| 203 HostPortPair host_port_pair("www.example.org", 443); | |
| 204 SpdySessionKey key(host_port_pair, ProxyServer::Direct(), | |
| 205 PRIVACY_MODE_DISABLED); | |
| 206 | |
| 207 SSLSocketDataProvider ssl_data(ASYNC, OK); | |
| 208 ssl_data.SetNextProto(kProtoHTTP2); | |
| 209 ssl_data.cert = ImportCertFromFile(GetTestCertsDirectory(), "ok_cert.pem"); | |
| 210 ASSERT_TRUE(ssl_data.cert.get()); | |
| 211 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_data); | |
| 212 | |
| 213 InitSession(reads, arraysize(reads), writes, arraysize(writes), key); | |
| 214 | |
| 215 BidirectionalStream::RequestInfo request; | |
| 216 request.method = "POST"; | |
| 217 request.url = GURL("https://www.example.org/"); | |
| 218 | |
| 219 scoped_refptr<IOBuffer> read_buffer(new IOBuffer(kReadBufferSize)); | |
| 220 scoped_ptr<SendDataDelegate> delegate(new SendDataDelegate( | |
| 221 read_buffer.get(), kReadBufferSize, kBodyDataStringPiece)); | |
| 222 delegate->RunTest(request, DEFAULT_PRIORITY, http_session_.get()); | |
| 223 | |
| 224 const SpdyHeaderBlock response_headers = delegate->response_headers(); | |
| 225 EXPECT_EQ("200", response_headers.find(":status")->second); | |
| 226 EXPECT_EQ("header-value", response_headers.find("header-name")->second); | |
| 227 EXPECT_EQ(std::string(kBodyData, kBodyDataSize), delegate->data_received()); | |
| 228 EXPECT_EQ("bar", delegate->trailers().find("foo")->second); | |
| 229 } | |
| 230 | |
| 231 } // namespace net | |
| OLD | NEW |