[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 1 | // Copyright (c) 2012 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/quic/quic_http_stream.h" |
| 6 | |
| 7 | #include <vector> |
| 8 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 9 | #include "net/base/chunked_upload_data_stream.h" |
| 10 | #include "net/base/elements_upload_data_stream.h" |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 11 | #include "net/base/net_errors.h" |
| 12 | #include "net/base/test_completion_callback.h" |
[email protected] | b2d26cfd | 2012-12-11 10:36:06 | [diff] [blame] | 13 | #include "net/base/upload_bytes_element_reader.h" |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 14 | #include "net/http/http_response_headers.h" |
[email protected] | 5db45220 | 2014-08-19 05:22:15 | [diff] [blame] | 15 | #include "net/http/transport_security_state.h" |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 16 | #include "net/quic/congestion_control/send_algorithm_interface.h" |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 17 | #include "net/quic/crypto/crypto_protocol.h" |
[email protected] | 4df6984 | 2013-02-27 06:32:16 | [diff] [blame] | 18 | #include "net/quic/crypto/quic_decrypter.h" |
| 19 | #include "net/quic/crypto/quic_encrypter.h" |
[email protected] | 17bf15c | 2014-03-14 10:08:04 | [diff] [blame] | 20 | #include "net/quic/crypto/quic_server_info.h" |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 21 | #include "net/quic/quic_client_session.h" |
| 22 | #include "net/quic/quic_connection.h" |
| 23 | #include "net/quic/quic_connection_helper.h" |
[email protected] | cbd731e | 2013-10-24 00:20:39 | [diff] [blame] | 24 | #include "net/quic/quic_default_packet_writer.h" |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 25 | #include "net/quic/quic_http_utils.h" |
| 26 | #include "net/quic/quic_reliable_client_stream.h" |
[email protected] | 9f0dcd4e | 2014-01-16 15:58:14 | [diff] [blame] | 27 | #include "net/quic/quic_write_blocked_list.h" |
[email protected] | 3e7dca6 | 2013-09-10 16:14:23 | [diff] [blame] | 28 | #include "net/quic/spdy_utils.h" |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 29 | #include "net/quic/test_tools/mock_clock.h" |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 30 | #include "net/quic/test_tools/mock_crypto_client_stream_factory.h" |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 31 | #include "net/quic/test_tools/mock_random.h" |
[email protected] | b1f287d | 2012-12-22 17:25:39 | [diff] [blame] | 32 | #include "net/quic/test_tools/quic_connection_peer.h" |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 33 | #include "net/quic/test_tools/quic_test_packet_maker.h" |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 34 | #include "net/quic/test_tools/quic_test_utils.h" |
| 35 | #include "net/quic/test_tools/test_task_runner.h" |
| 36 | #include "net/socket/socket_test_util.h" |
[email protected] | 6cca996b | 2013-01-25 07:43:36 | [diff] [blame] | 37 | #include "net/spdy/spdy_frame_builder.h" |
| 38 | #include "net/spdy/spdy_framer.h" |
| 39 | #include "net/spdy/spdy_http_utils.h" |
| 40 | #include "net/spdy/spdy_protocol.h" |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 41 | #include "testing/gmock/include/gmock/gmock.h" |
| 42 | #include "testing/gtest/include/gtest/gtest.h" |
| 43 | |
| 44 | using testing::_; |
[email protected] | 06ff515 | 2013-08-29 01:03:05 | [diff] [blame] | 45 | using testing::AnyNumber; |
| 46 | using testing::Return; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 47 | |
| 48 | namespace net { |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 49 | namespace test { |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 50 | namespace { |
| 51 | |
[email protected] | 16ba774 | 2014-08-22 00:57:25 | [diff] [blame] | 52 | const char kUploadData[] = "Really nifty data!"; |
bnc | b07c0553 | 2015-05-14 19:07:20 | [diff] [blame^] | 53 | const char kDefaultServerHostName[] = "www.google.com"; |
| 54 | const uint16 kDefaultServerPort = 80; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 55 | |
| 56 | class TestQuicConnection : public QuicConnection { |
| 57 | public: |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 58 | TestQuicConnection(const QuicVersionVector& versions, |
[email protected] | 3aa9ca7 | 2014-02-27 19:39:43 | [diff] [blame] | 59 | QuicConnectionId connection_id, |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 60 | IPEndPoint address, |
[email protected] | cbd731e | 2013-10-24 00:20:39 | [diff] [blame] | 61 | QuicConnectionHelper* helper, |
[email protected] | 6d51582 | 2014-08-22 01:58:06 | [diff] [blame] | 62 | const QuicConnection::PacketWriterFactory& writer_factory) |
[email protected] | 66cd2d6 | 2014-08-01 18:42:39 | [diff] [blame] | 63 | : QuicConnection(connection_id, |
| 64 | address, |
| 65 | helper, |
[email protected] | 6d51582 | 2014-08-22 01:58:06 | [diff] [blame] | 66 | writer_factory, |
rtenneti | 6f48aa9 | 2015-03-16 02:18:48 | [diff] [blame] | 67 | true /* owns_writer */, |
| 68 | Perspective::IS_CLIENT, |
| 69 | false /* is_secure */, |
| 70 | versions) {} |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 71 | |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 72 | void SetSendAlgorithm(SendAlgorithmInterface* send_algorithm) { |
| 73 | QuicConnectionPeer::SetSendAlgorithm(this, send_algorithm); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 74 | } |
| 75 | }; |
| 76 | |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 77 | // Subclass of QuicHttpStream that closes itself when the first piece of data |
| 78 | // is received. |
| 79 | class AutoClosingStream : public QuicHttpStream { |
| 80 | public: |
[email protected] | 0b2294d3 | 2013-08-02 00:46:36 | [diff] [blame] | 81 | explicit AutoClosingStream(const base::WeakPtr<QuicClientSession>& session) |
| 82 | : QuicHttpStream(session) { |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 83 | } |
| 84 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 85 | int OnDataReceived(const char* data, int length) override { |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 86 | Close(false); |
| 87 | return OK; |
| 88 | } |
| 89 | }; |
| 90 | |
[email protected] | 6d51582 | 2014-08-22 01:58:06 | [diff] [blame] | 91 | class TestPacketWriterFactory : public QuicConnection::PacketWriterFactory { |
| 92 | public: |
| 93 | explicit TestPacketWriterFactory(DatagramClientSocket* socket) |
| 94 | : socket_(socket) {} |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 95 | ~TestPacketWriterFactory() override {} |
[email protected] | 6d51582 | 2014-08-22 01:58:06 | [diff] [blame] | 96 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 97 | QuicPacketWriter* Create(QuicConnection* connection) const override { |
[email protected] | 6d51582 | 2014-08-22 01:58:06 | [diff] [blame] | 98 | return new QuicDefaultPacketWriter(socket_); |
| 99 | } |
| 100 | |
| 101 | private: |
| 102 | DatagramClientSocket* socket_; |
| 103 | }; |
| 104 | |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 105 | } // namespace |
| 106 | |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 107 | class QuicHttpStreamPeer { |
| 108 | public: |
| 109 | static QuicReliableClientStream* GetQuicReliableClientStream( |
| 110 | QuicHttpStream* stream) { |
| 111 | return stream->stream_; |
| 112 | } |
| 113 | }; |
| 114 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 115 | class QuicHttpStreamTest : public ::testing::TestWithParam<QuicVersion> { |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 116 | protected: |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 117 | static const bool kFin = true; |
| 118 | static const bool kIncludeVersion = true; |
| 119 | static const bool kIncludeCongestionFeedback = true; |
| 120 | |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 121 | // Holds a packet to be written to the wire, and the IO mode that should |
| 122 | // be used by the mock socket when performing the write. |
| 123 | struct PacketToWrite { |
| 124 | PacketToWrite(IoMode mode, QuicEncryptedPacket* packet) |
| 125 | : mode(mode), |
| 126 | packet(packet) { |
| 127 | } |
| 128 | IoMode mode; |
| 129 | QuicEncryptedPacket* packet; |
| 130 | }; |
| 131 | |
| 132 | QuicHttpStreamTest() |
| 133 | : net_log_(BoundNetLog()), |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 134 | use_closing_stream_(false), |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 135 | read_buffer_(new IOBufferWithSize(4096)), |
[email protected] | 3aa9ca7 | 2014-02-27 19:39:43 | [diff] [blame] | 136 | connection_id_(2), |
[email protected] | 66ae596 | 2014-05-22 11:13:05 | [diff] [blame] | 137 | stream_id_(kClientDataStreamId1), |
bnc | b07c0553 | 2015-05-14 19:07:20 | [diff] [blame^] | 138 | maker_(GetParam(), connection_id_, &clock_, kDefaultServerHostName), |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 139 | random_generator_(0) { |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 140 | IPAddressNumber ip; |
| 141 | CHECK(ParseIPLiteralToNumber("192.0.2.33", &ip)); |
| 142 | peer_addr_ = IPEndPoint(ip, 443); |
| 143 | self_addr_ = IPEndPoint(ip, 8435); |
rtenneti | 4b06ae7 | 2014-08-26 03:43:43 | [diff] [blame] | 144 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(20)); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | ~QuicHttpStreamTest() { |
rtenneti | 85dcfac2 | 2015-03-27 20:22:19 | [diff] [blame] | 148 | session_->CloseSessionOnError(ERR_ABORTED, QUIC_INTERNAL_ERROR); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 149 | for (size_t i = 0; i < writes_.size(); i++) { |
| 150 | delete writes_[i].packet; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | // Adds a packet to the list of expected writes. |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 155 | void AddWrite(scoped_ptr<QuicEncryptedPacket> packet) { |
| 156 | writes_.push_back(PacketToWrite(SYNCHRONOUS, packet.release())); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | // Returns the packet to be written at position |pos|. |
| 160 | QuicEncryptedPacket* GetWrite(size_t pos) { |
| 161 | return writes_[pos].packet; |
| 162 | } |
| 163 | |
| 164 | bool AtEof() { |
rch | b6dc7357 | 2015-05-08 04:01:07 | [diff] [blame] | 165 | return socket_data_->at_read_eof() && socket_data_->at_write_eof(); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 166 | } |
| 167 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 168 | void ProcessPacket(scoped_ptr<QuicEncryptedPacket> packet) { |
| 169 | connection_->ProcessUdpPacket(self_addr_, peer_addr_, *packet); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | // Configures the test fixture to use the list of expected writes. |
| 173 | void Initialize() { |
| 174 | mock_writes_.reset(new MockWrite[writes_.size()]); |
| 175 | for (size_t i = 0; i < writes_.size(); i++) { |
| 176 | mock_writes_[i] = MockWrite(writes_[i].mode, |
| 177 | writes_[i].packet->data(), |
| 178 | writes_[i].packet->length()); |
| 179 | }; |
| 180 | |
rtenneti | be63573 | 2014-10-02 22:51:42 | [diff] [blame] | 181 | socket_data_.reset(new StaticSocketDataProvider( |
| 182 | nullptr, 0, mock_writes_.get(), writes_.size())); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 183 | |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 184 | MockUDPClientSocket* socket = new MockUDPClientSocket(socket_data_.get(), |
| 185 | net_log_.net_log()); |
| 186 | socket->Connect(peer_addr_); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 187 | runner_ = new TestTaskRunner(&clock_); |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 188 | send_algorithm_ = new MockSendAlgorithm(); |
[email protected] | efecff9 | 2013-09-24 07:49:23 | [diff] [blame] | 189 | EXPECT_CALL(*send_algorithm_, |
[email protected] | ef0da58 | 2014-05-09 07:16:30 | [diff] [blame] | 190 | OnPacketSent(_, _, _, _, _)).WillRepeatedly(Return(true)); |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 191 | EXPECT_CALL(*send_algorithm_, RetransmissionDelay()).WillRepeatedly( |
[email protected] | 06ff515 | 2013-08-29 01:03:05 | [diff] [blame] | 192 | Return(QuicTime::Delta::Zero())); |
[email protected] | 08da9adb | 2014-04-24 08:33:31 | [diff] [blame] | 193 | EXPECT_CALL(*send_algorithm_, GetCongestionWindow()).WillRepeatedly( |
| 194 | Return(kMaxPacketSize)); |
[email protected] | 77b5d50b | 2014-05-07 22:48:48 | [diff] [blame] | 195 | EXPECT_CALL(*send_algorithm_, TimeUntilSend(_, _, _)). |
[email protected] | 06ff515 | 2013-08-29 01:03:05 | [diff] [blame] | 196 | WillRepeatedly(Return(QuicTime::Delta::Zero())); |
[email protected] | ea825e0 | 2013-08-21 18:12:45 | [diff] [blame] | 197 | EXPECT_CALL(*send_algorithm_, BandwidthEstimate()).WillRepeatedly( |
[email protected] | 06ff515 | 2013-08-29 01:03:05 | [diff] [blame] | 198 | Return(QuicBandwidth::Zero())); |
rch | 1e543ec | 2015-03-29 07:04:40 | [diff] [blame] | 199 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)).Times(AnyNumber()); |
[email protected] | 2cfc6bb8 | 2013-10-27 03:40:44 | [diff] [blame] | 200 | helper_.reset(new QuicConnectionHelper(runner_.get(), &clock_, |
| 201 | &random_generator_)); |
[email protected] | 6d51582 | 2014-08-22 01:58:06 | [diff] [blame] | 202 | TestPacketWriterFactory writer_factory(socket); |
[email protected] | 3aa9ca7 | 2014-02-27 19:39:43 | [diff] [blame] | 203 | connection_ = new TestQuicConnection(SupportedVersions(GetParam()), |
| 204 | connection_id_, peer_addr_, |
[email protected] | 6d51582 | 2014-08-22 01:58:06 | [diff] [blame] | 205 | helper_.get(), writer_factory); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 206 | connection_->set_visitor(&visitor_); |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 207 | connection_->SetSendAlgorithm(send_algorithm_); |
rtenneti | 041b299 | 2015-02-23 23:03:28 | [diff] [blame] | 208 | session_.reset(new QuicClientSession( |
| 209 | connection_, scoped_ptr<DatagramClientSocket>(socket), nullptr, |
| 210 | &transport_security_state_, make_scoped_ptr((QuicServerInfo*)nullptr), |
rtenneti | f4f0885 | 2015-02-27 17:50:04 | [diff] [blame] | 211 | DefaultQuicConfig(), "CONNECTION_UNKNOWN", base::TimeTicks::Now(), |
rtenneti | 041b299 | 2015-02-23 23:03:28 | [diff] [blame] | 212 | base::MessageLoop::current()->message_loop_proxy().get(), nullptr)); |
bnc | b07c0553 | 2015-05-14 19:07:20 | [diff] [blame^] | 213 | session_->InitializeSession( |
| 214 | QuicServerId(kDefaultServerHostName, kDefaultServerPort, |
| 215 | /*is_secure=*/false, PRIVACY_MODE_DISABLED), |
| 216 | &crypto_config_, &crypto_client_stream_factory_); |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 217 | session_->GetCryptoStream()->CryptoConnect(); |
[email protected] | 8ba8121 | 2013-05-03 13:11:48 | [diff] [blame] | 218 | EXPECT_TRUE(session_->IsCryptoHandshakeConfirmed()); |
[email protected] | 6cca996b | 2013-01-25 07:43:36 | [diff] [blame] | 219 | stream_.reset(use_closing_stream_ ? |
[email protected] | 0b2294d3 | 2013-08-02 00:46:36 | [diff] [blame] | 220 | new AutoClosingStream(session_->GetWeakPtr()) : |
| 221 | new QuicHttpStream(session_->GetWeakPtr())); |
[email protected] | 6cca996b | 2013-01-25 07:43:36 | [diff] [blame] | 222 | } |
| 223 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 224 | void SetRequest(const std::string& method, |
| 225 | const std::string& path, |
| 226 | RequestPriority priority) { |
| 227 | request_headers_ = maker_.GetRequestHeaders(method, "http", path); |
[email protected] | 6cca996b | 2013-01-25 07:43:36 | [diff] [blame] | 228 | } |
| 229 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 230 | void SetResponse(const std::string& status, const std::string& body) { |
| 231 | response_headers_ = maker_.GetResponseHeaders(status); |
[email protected] | 92bf17c | 2014-03-03 21:14:03 | [diff] [blame] | 232 | response_data_ = body; |
[email protected] | 6cca996b | 2013-01-25 07:43:36 | [diff] [blame] | 233 | } |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 234 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 235 | scoped_ptr<QuicEncryptedPacket> ConstructDataPacket( |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 236 | QuicPacketSequenceNumber sequence_number, |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 237 | bool should_include_version, |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 238 | bool fin, |
| 239 | QuicStreamOffset offset, |
| 240 | base::StringPiece data) { |
rtenneti | f4bdb54 | 2015-01-21 14:33:05 | [diff] [blame] | 241 | return maker_.MakeDataPacket(sequence_number, stream_id_, |
| 242 | should_include_version, fin, offset, data); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 243 | } |
| 244 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 245 | scoped_ptr<QuicEncryptedPacket> ConstructRequestHeadersPacket( |
| 246 | QuicPacketSequenceNumber sequence_number, |
rtenneti | f4bdb54 | 2015-01-21 14:33:05 | [diff] [blame] | 247 | bool fin, |
| 248 | RequestPriority request_priority) { |
| 249 | QuicPriority priority = |
| 250 | ConvertRequestPriorityToQuicPriority(request_priority); |
| 251 | return maker_.MakeRequestHeadersPacket(sequence_number, stream_id_, |
| 252 | kIncludeVersion, fin, priority, |
| 253 | request_headers_); |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | scoped_ptr<QuicEncryptedPacket> ConstructResponseHeadersPacket( |
| 257 | QuicPacketSequenceNumber sequence_number, |
| 258 | bool fin) { |
| 259 | return maker_.MakeResponseHeadersPacket( |
| 260 | sequence_number, stream_id_, !kIncludeVersion, fin, response_headers_); |
| 261 | } |
| 262 | |
| 263 | scoped_ptr<QuicEncryptedPacket> ConstructRstStreamPacket( |
[email protected] | 06ff515 | 2013-08-29 01:03:05 | [diff] [blame] | 264 | QuicPacketSequenceNumber sequence_number) { |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 265 | return maker_.MakeRstPacket( |
[email protected] | 51cc134 | 2014-04-18 23:44:37 | [diff] [blame] | 266 | sequence_number, true, stream_id_, |
rtenneti | 4a5df26 | 2014-11-07 00:43:58 | [diff] [blame] | 267 | AdjustErrorForVersion(QUIC_RST_ACKNOWLEDGEMENT, GetParam())); |
[email protected] | 06ff515 | 2013-08-29 01:03:05 | [diff] [blame] | 268 | } |
| 269 | |
[email protected] | c5e1aca | 2014-01-30 04:03:04 | [diff] [blame] | 270 | scoped_ptr<QuicEncryptedPacket> ConstructAckAndRstStreamPacket( |
| 271 | QuicPacketSequenceNumber sequence_number) { |
| 272 | return maker_.MakeAckAndRstPacket( |
| 273 | sequence_number, !kIncludeVersion, stream_id_, QUIC_STREAM_CANCELLED, |
[email protected] | 08da9adb | 2014-04-24 08:33:31 | [diff] [blame] | 274 | 2, 1, !kIncludeCongestionFeedback); |
[email protected] | c5e1aca | 2014-01-30 04:03:04 | [diff] [blame] | 275 | } |
| 276 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 277 | scoped_ptr<QuicEncryptedPacket> ConstructAckPacket( |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 278 | QuicPacketSequenceNumber sequence_number, |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 279 | QuicPacketSequenceNumber largest_received, |
| 280 | QuicPacketSequenceNumber least_unacked) { |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 281 | return maker_.MakeAckPacket(sequence_number, largest_received, |
| 282 | least_unacked, !kIncludeCongestionFeedback); |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 283 | } |
| 284 | |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 285 | BoundNetLog net_log_; |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 286 | bool use_closing_stream_; |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 287 | MockSendAlgorithm* send_algorithm_; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 288 | scoped_refptr<TestTaskRunner> runner_; |
[email protected] | 4356f0f | 2013-04-07 00:58:17 | [diff] [blame] | 289 | scoped_ptr<MockWrite[]> mock_writes_; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 290 | MockClock clock_; |
| 291 | TestQuicConnection* connection_; |
[email protected] | 2cfc6bb8 | 2013-10-27 03:40:44 | [diff] [blame] | 292 | scoped_ptr<QuicConnectionHelper> helper_; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 293 | testing::StrictMock<MockConnectionVisitor> visitor_; |
| 294 | scoped_ptr<QuicHttpStream> stream_; |
[email protected] | 5db45220 | 2014-08-19 05:22:15 | [diff] [blame] | 295 | TransportSecurityState transport_security_state_; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 296 | scoped_ptr<QuicClientSession> session_; |
[email protected] | ef95114d | 2013-04-17 17:57:01 | [diff] [blame] | 297 | QuicCryptoClientConfig crypto_config_; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 298 | TestCompletionCallback callback_; |
| 299 | HttpRequestInfo request_; |
| 300 | HttpRequestHeaders headers_; |
| 301 | HttpResponseInfo response_; |
| 302 | scoped_refptr<IOBufferWithSize> read_buffer_; |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 303 | SpdyHeaderBlock request_headers_; |
| 304 | SpdyHeaderBlock response_headers_; |
[email protected] | 6cca996b | 2013-01-25 07:43:36 | [diff] [blame] | 305 | std::string request_data_; |
| 306 | std::string response_data_; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 307 | |
| 308 | private: |
[email protected] | 3aa9ca7 | 2014-02-27 19:39:43 | [diff] [blame] | 309 | const QuicConnectionId connection_id_; |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 310 | const QuicStreamId stream_id_; |
| 311 | QuicTestPacketMaker maker_; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 312 | IPEndPoint self_addr_; |
| 313 | IPEndPoint peer_addr_; |
[email protected] | 457d695 | 2013-12-13 09:24:58 | [diff] [blame] | 314 | MockRandom random_generator_; |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 315 | MockCryptoClientStreamFactory crypto_client_stream_factory_; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 316 | scoped_ptr<StaticSocketDataProvider> socket_data_; |
| 317 | std::vector<PacketToWrite> writes_; |
| 318 | }; |
| 319 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 320 | INSTANTIATE_TEST_CASE_P(Version, QuicHttpStreamTest, |
| 321 | ::testing::ValuesIn(QuicSupportedVersions())); |
| 322 | |
| 323 | TEST_P(QuicHttpStreamTest, RenewStreamForAuth) { |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 324 | Initialize(); |
rtenneti | be63573 | 2014-10-02 22:51:42 | [diff] [blame] | 325 | EXPECT_EQ(nullptr, stream_->RenewStreamForAuth()); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 326 | } |
| 327 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 328 | TEST_P(QuicHttpStreamTest, CanFindEndOfResponse) { |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 329 | Initialize(); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 330 | EXPECT_TRUE(stream_->CanFindEndOfResponse()); |
| 331 | } |
| 332 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 333 | TEST_P(QuicHttpStreamTest, IsConnectionReusable) { |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 334 | Initialize(); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 335 | EXPECT_FALSE(stream_->IsConnectionReusable()); |
| 336 | } |
| 337 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 338 | TEST_P(QuicHttpStreamTest, GetRequest) { |
| 339 | SetRequest("GET", "/", DEFAULT_PRIORITY); |
rtenneti | f4bdb54 | 2015-01-21 14:33:05 | [diff] [blame] | 340 | AddWrite(ConstructRequestHeadersPacket(1, kFin, DEFAULT_PRIORITY)); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 341 | Initialize(); |
| 342 | |
| 343 | request_.method = "GET"; |
| 344 | request_.url = GURL("http://www.google.com/"); |
| 345 | |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 346 | EXPECT_EQ(OK, stream_->InitializeStream(&request_, DEFAULT_PRIORITY, |
| 347 | net_log_, callback_.callback())); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 348 | EXPECT_EQ(OK, stream_->SendRequest(headers_, &response_, |
| 349 | callback_.callback())); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 350 | |
| 351 | // Ack the request. |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 352 | ProcessPacket(ConstructAckPacket(1, 0, 0)); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 353 | |
| 354 | EXPECT_EQ(ERR_IO_PENDING, |
| 355 | stream_->ReadResponseHeaders(callback_.callback())); |
| 356 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 357 | SetResponse("404 Not Found", std::string()); |
[email protected] | 92bf17c | 2014-03-03 21:14:03 | [diff] [blame] | 358 | ProcessPacket(ConstructResponseHeadersPacket(2, kFin)); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 359 | |
| 360 | // Now that the headers have been processed, the callback will return. |
| 361 | EXPECT_EQ(OK, callback_.WaitForResult()); |
[email protected] | cadac62 | 2013-06-11 16:46:36 | [diff] [blame] | 362 | ASSERT_TRUE(response_.headers.get()); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 363 | EXPECT_EQ(404, response_.headers->response_code()); |
| 364 | EXPECT_TRUE(response_.headers->HasHeaderValue("Content-Type", "text/plain")); |
[email protected] | 6ed6743 | 2014-06-24 01:53:53 | [diff] [blame] | 365 | EXPECT_FALSE(response_.response_time.is_null()); |
| 366 | EXPECT_FALSE(response_.request_time.is_null()); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 367 | |
| 368 | // There is no body, so this should return immediately. |
| 369 | EXPECT_EQ(0, stream_->ReadResponseBody(read_buffer_.get(), |
| 370 | read_buffer_->size(), |
| 371 | callback_.callback())); |
| 372 | EXPECT_TRUE(stream_->IsResponseBodyComplete()); |
| 373 | EXPECT_TRUE(AtEof()); |
| 374 | } |
| 375 | |
[email protected] | 3e7dca6 | 2013-09-10 16:14:23 | [diff] [blame] | 376 | // Regression test for http://crbug.com/288128 |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 377 | TEST_P(QuicHttpStreamTest, GetRequestLargeResponse) { |
| 378 | SetRequest("GET", "/", DEFAULT_PRIORITY); |
rtenneti | f4bdb54 | 2015-01-21 14:33:05 | [diff] [blame] | 379 | AddWrite(ConstructRequestHeadersPacket(1, kFin, DEFAULT_PRIORITY)); |
[email protected] | 3e7dca6 | 2013-09-10 16:14:23 | [diff] [blame] | 380 | Initialize(); |
| 381 | |
| 382 | request_.method = "GET"; |
| 383 | request_.url = GURL("http://www.google.com/"); |
| 384 | |
| 385 | EXPECT_EQ(OK, stream_->InitializeStream(&request_, DEFAULT_PRIORITY, |
| 386 | net_log_, callback_.callback())); |
| 387 | EXPECT_EQ(OK, stream_->SendRequest(headers_, &response_, |
| 388 | callback_.callback())); |
[email protected] | 3e7dca6 | 2013-09-10 16:14:23 | [diff] [blame] | 389 | |
| 390 | // Ack the request. |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 391 | ProcessPacket(ConstructAckPacket(1, 0, 0)); |
[email protected] | 3e7dca6 | 2013-09-10 16:14:23 | [diff] [blame] | 392 | |
| 393 | EXPECT_EQ(ERR_IO_PENDING, |
| 394 | stream_->ReadResponseHeaders(callback_.callback())); |
| 395 | |
| 396 | SpdyHeaderBlock headers; |
| 397 | headers[":status"] = "200 OK"; |
| 398 | headers[":version"] = "HTTP/1.1"; |
| 399 | headers["content-type"] = "text/plain"; |
| 400 | headers["big6"] = std::string(10000, 'x'); // Lots of x's. |
| 401 | |
rch | c081544 | 2015-04-18 13:29:46 | [diff] [blame] | 402 | std::string response = |
| 403 | SpdyUtils::SerializeUncompressedHeaders(headers, GetParam()); |
[email protected] | 3e7dca6 | 2013-09-10 16:14:23 | [diff] [blame] | 404 | EXPECT_LT(4096u, response.length()); |
| 405 | stream_->OnDataReceived(response.data(), response.length()); |
| 406 | stream_->OnClose(QUIC_NO_ERROR); |
| 407 | |
| 408 | // Now that the headers have been processed, the callback will return. |
| 409 | EXPECT_EQ(OK, callback_.WaitForResult()); |
| 410 | ASSERT_TRUE(response_.headers.get()); |
| 411 | EXPECT_EQ(200, response_.headers->response_code()); |
| 412 | EXPECT_TRUE(response_.headers->HasHeaderValue("Content-Type", "text/plain")); |
| 413 | |
| 414 | // There is no body, so this should return immediately. |
| 415 | EXPECT_EQ(0, stream_->ReadResponseBody(read_buffer_.get(), |
| 416 | read_buffer_->size(), |
| 417 | callback_.callback())); |
| 418 | EXPECT_TRUE(stream_->IsResponseBodyComplete()); |
| 419 | EXPECT_TRUE(AtEof()); |
| 420 | } |
| 421 | |
rch | f9f103cbc | 2014-08-30 05:28:04 | [diff] [blame] | 422 | // Regression test for http://crbug.com/409101 |
| 423 | TEST_P(QuicHttpStreamTest, SessionClosedBeforeSendRequest) { |
| 424 | SetRequest("GET", "/", DEFAULT_PRIORITY); |
| 425 | Initialize(); |
| 426 | |
| 427 | request_.method = "GET"; |
| 428 | request_.url = GURL("http://www.google.com/"); |
| 429 | |
| 430 | EXPECT_EQ(OK, stream_->InitializeStream(&request_, DEFAULT_PRIORITY, |
| 431 | net_log_, callback_.callback())); |
| 432 | |
| 433 | session_->connection()->CloseConnection(QUIC_NO_ERROR, true); |
| 434 | |
| 435 | EXPECT_EQ(ERR_CONNECTION_CLOSED, |
| 436 | stream_->SendRequest(headers_, &response_, |
| 437 | callback_.callback())); |
| 438 | } |
| 439 | |
rch | 11a114a | 2014-09-04 23:41:59 | [diff] [blame] | 440 | // Regression test for http://crbug.com/409871 |
| 441 | TEST_P(QuicHttpStreamTest, SessionClosedBeforeReadResponseHeaders) { |
| 442 | SetRequest("GET", "/", DEFAULT_PRIORITY); |
rtenneti | f4bdb54 | 2015-01-21 14:33:05 | [diff] [blame] | 443 | AddWrite(ConstructRequestHeadersPacket(1, kFin, DEFAULT_PRIORITY)); |
rch | 11a114a | 2014-09-04 23:41:59 | [diff] [blame] | 444 | Initialize(); |
| 445 | |
| 446 | request_.method = "GET"; |
| 447 | request_.url = GURL("http://www.google.com/"); |
| 448 | |
| 449 | EXPECT_EQ(OK, stream_->InitializeStream(&request_, DEFAULT_PRIORITY, |
| 450 | net_log_, callback_.callback())); |
| 451 | |
| 452 | EXPECT_EQ(OK, stream_->SendRequest(headers_, &response_, |
| 453 | callback_.callback())); |
| 454 | |
| 455 | session_->connection()->CloseConnection(QUIC_NO_ERROR, true); |
| 456 | |
| 457 | EXPECT_NE(OK, stream_->ReadResponseHeaders(callback_.callback())); |
| 458 | } |
| 459 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 460 | TEST_P(QuicHttpStreamTest, SendPostRequest) { |
| 461 | SetRequest("POST", "/", DEFAULT_PRIORITY); |
rtenneti | f4bdb54 | 2015-01-21 14:33:05 | [diff] [blame] | 462 | AddWrite(ConstructRequestHeadersPacket(1, !kFin, DEFAULT_PRIORITY)); |
[email protected] | 92bf17c | 2014-03-03 21:14:03 | [diff] [blame] | 463 | AddWrite(ConstructDataPacket(2, kIncludeVersion, kFin, 0, kUploadData)); |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 464 | AddWrite(ConstructAckPacket(3, 3, 1)); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 465 | |
| 466 | Initialize(); |
| 467 | |
[email protected] | b2d26cfd | 2012-12-11 10:36:06 | [diff] [blame] | 468 | ScopedVector<UploadElementReader> element_readers; |
| 469 | element_readers.push_back( |
| 470 | new UploadBytesElementReader(kUploadData, strlen(kUploadData))); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 471 | ElementsUploadDataStream upload_data_stream(element_readers.Pass(), 0); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 472 | request_.method = "POST"; |
| 473 | request_.url = GURL("http://www.google.com/"); |
| 474 | request_.upload_data_stream = &upload_data_stream; |
[email protected] | 4db27d8 | 2012-12-20 11:50:24 | [diff] [blame] | 475 | ASSERT_EQ(OK, request_.upload_data_stream->Init(CompletionCallback())); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 476 | |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 477 | EXPECT_EQ(OK, stream_->InitializeStream(&request_, DEFAULT_PRIORITY, |
| 478 | net_log_, callback_.callback())); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 479 | EXPECT_EQ(OK, stream_->SendRequest(headers_, &response_, |
| 480 | callback_.callback())); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 481 | |
| 482 | // Ack both packets in the request. |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 483 | ProcessPacket(ConstructAckPacket(1, 0, 0)); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 484 | |
| 485 | // Send the response headers (but not the body). |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 486 | SetResponse("200 OK", std::string()); |
[email protected] | 92bf17c | 2014-03-03 21:14:03 | [diff] [blame] | 487 | ProcessPacket(ConstructResponseHeadersPacket(2, !kFin)); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 488 | |
| 489 | // Since the headers have already arrived, this should return immediately. |
| 490 | EXPECT_EQ(OK, stream_->ReadResponseHeaders(callback_.callback())); |
[email protected] | cadac62 | 2013-06-11 16:46:36 | [diff] [blame] | 491 | ASSERT_TRUE(response_.headers.get()); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 492 | EXPECT_EQ(200, response_.headers->response_code()); |
| 493 | EXPECT_TRUE(response_.headers->HasHeaderValue("Content-Type", "text/plain")); |
| 494 | |
| 495 | // Send the response body. |
| 496 | const char kResponseBody[] = "Hello world!"; |
[email protected] | 92bf17c | 2014-03-03 21:14:03 | [diff] [blame] | 497 | ProcessPacket(ConstructDataPacket(3, false, kFin, 0, kResponseBody)); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 498 | // Since the body has already arrived, this should return immediately. |
| 499 | EXPECT_EQ(static_cast<int>(strlen(kResponseBody)), |
| 500 | stream_->ReadResponseBody(read_buffer_.get(), read_buffer_->size(), |
| 501 | callback_.callback())); |
| 502 | |
| 503 | EXPECT_TRUE(stream_->IsResponseBodyComplete()); |
| 504 | EXPECT_TRUE(AtEof()); |
| 505 | } |
| 506 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 507 | TEST_P(QuicHttpStreamTest, SendChunkedPostRequest) { |
| 508 | SetRequest("POST", "/", DEFAULT_PRIORITY); |
[email protected] | c9e49a0 | 2013-02-26 05:56:47 | [diff] [blame] | 509 | size_t chunk_size = strlen(kUploadData); |
rtenneti | f4bdb54 | 2015-01-21 14:33:05 | [diff] [blame] | 510 | AddWrite(ConstructRequestHeadersPacket(1, !kFin, DEFAULT_PRIORITY)); |
[email protected] | 92bf17c | 2014-03-03 21:14:03 | [diff] [blame] | 511 | AddWrite(ConstructDataPacket(2, kIncludeVersion, !kFin, 0, kUploadData)); |
| 512 | AddWrite(ConstructDataPacket(3, kIncludeVersion, kFin, chunk_size, |
| 513 | kUploadData)); |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 514 | AddWrite(ConstructAckPacket(4, 3, 1)); |
[email protected] | c9e49a0 | 2013-02-26 05:56:47 | [diff] [blame] | 515 | Initialize(); |
| 516 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 517 | ChunkedUploadDataStream upload_data_stream(0); |
| 518 | upload_data_stream.AppendData(kUploadData, chunk_size, false); |
[email protected] | c9e49a0 | 2013-02-26 05:56:47 | [diff] [blame] | 519 | |
| 520 | request_.method = "POST"; |
| 521 | request_.url = GURL("http://www.google.com/"); |
| 522 | request_.upload_data_stream = &upload_data_stream; |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 523 | ASSERT_EQ(OK, request_.upload_data_stream->Init( |
| 524 | TestCompletionCallback().callback())); |
[email protected] | c9e49a0 | 2013-02-26 05:56:47 | [diff] [blame] | 525 | |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 526 | ASSERT_EQ(OK, stream_->InitializeStream(&request_, DEFAULT_PRIORITY, |
| 527 | net_log_, callback_.callback())); |
[email protected] | c9e49a0 | 2013-02-26 05:56:47 | [diff] [blame] | 528 | ASSERT_EQ(ERR_IO_PENDING, stream_->SendRequest(headers_, &response_, |
| 529 | callback_.callback())); |
[email protected] | c9e49a0 | 2013-02-26 05:56:47 | [diff] [blame] | 530 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 531 | upload_data_stream.AppendData(kUploadData, chunk_size, true); |
[email protected] | c9e49a0 | 2013-02-26 05:56:47 | [diff] [blame] | 532 | |
| 533 | // Ack both packets in the request. |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 534 | ProcessPacket(ConstructAckPacket(1, 0, 0)); |
[email protected] | c9e49a0 | 2013-02-26 05:56:47 | [diff] [blame] | 535 | |
| 536 | // Send the response headers (but not the body). |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 537 | SetResponse("200 OK", std::string()); |
[email protected] | 92bf17c | 2014-03-03 21:14:03 | [diff] [blame] | 538 | ProcessPacket(ConstructResponseHeadersPacket(2, !kFin)); |
[email protected] | c9e49a0 | 2013-02-26 05:56:47 | [diff] [blame] | 539 | |
| 540 | // Since the headers have already arrived, this should return immediately. |
| 541 | ASSERT_EQ(OK, stream_->ReadResponseHeaders(callback_.callback())); |
[email protected] | cadac62 | 2013-06-11 16:46:36 | [diff] [blame] | 542 | ASSERT_TRUE(response_.headers.get()); |
[email protected] | c9e49a0 | 2013-02-26 05:56:47 | [diff] [blame] | 543 | EXPECT_EQ(200, response_.headers->response_code()); |
| 544 | EXPECT_TRUE(response_.headers->HasHeaderValue("Content-Type", "text/plain")); |
| 545 | |
| 546 | // Send the response body. |
| 547 | const char kResponseBody[] = "Hello world!"; |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 548 | ProcessPacket(ConstructDataPacket(3, false, kFin, response_data_.length(), |
| 549 | kResponseBody)); |
[email protected] | c9e49a0 | 2013-02-26 05:56:47 | [diff] [blame] | 550 | |
| 551 | // Since the body has already arrived, this should return immediately. |
| 552 | ASSERT_EQ(static_cast<int>(strlen(kResponseBody)), |
| 553 | stream_->ReadResponseBody(read_buffer_.get(), read_buffer_->size(), |
| 554 | callback_.callback())); |
| 555 | |
| 556 | EXPECT_TRUE(stream_->IsResponseBodyComplete()); |
| 557 | EXPECT_TRUE(AtEof()); |
| 558 | } |
| 559 | |
[email protected] | 16ba774 | 2014-08-22 00:57:25 | [diff] [blame] | 560 | TEST_P(QuicHttpStreamTest, SendChunkedPostRequestWithFinalEmptyDataPacket) { |
| 561 | SetRequest("POST", "/", DEFAULT_PRIORITY); |
| 562 | size_t chunk_size = strlen(kUploadData); |
rtenneti | f4bdb54 | 2015-01-21 14:33:05 | [diff] [blame] | 563 | AddWrite(ConstructRequestHeadersPacket(1, !kFin, DEFAULT_PRIORITY)); |
[email protected] | 16ba774 | 2014-08-22 00:57:25 | [diff] [blame] | 564 | AddWrite(ConstructDataPacket(2, kIncludeVersion, !kFin, 0, kUploadData)); |
| 565 | AddWrite(ConstructDataPacket(3, kIncludeVersion, kFin, chunk_size, "")); |
| 566 | AddWrite(ConstructAckPacket(4, 3, 1)); |
| 567 | Initialize(); |
| 568 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 569 | ChunkedUploadDataStream upload_data_stream(0); |
| 570 | upload_data_stream.AppendData(kUploadData, chunk_size, false); |
[email protected] | 16ba774 | 2014-08-22 00:57:25 | [diff] [blame] | 571 | |
| 572 | request_.method = "POST"; |
| 573 | request_.url = GURL("http://www.google.com/"); |
| 574 | request_.upload_data_stream = &upload_data_stream; |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 575 | ASSERT_EQ(OK, request_.upload_data_stream->Init( |
| 576 | TestCompletionCallback().callback())); |
[email protected] | 16ba774 | 2014-08-22 00:57:25 | [diff] [blame] | 577 | |
| 578 | ASSERT_EQ(OK, stream_->InitializeStream(&request_, DEFAULT_PRIORITY, |
| 579 | net_log_, callback_.callback())); |
| 580 | ASSERT_EQ(ERR_IO_PENDING, stream_->SendRequest(headers_, &response_, |
| 581 | callback_.callback())); |
| 582 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 583 | upload_data_stream.AppendData(nullptr, 0, true); |
[email protected] | 16ba774 | 2014-08-22 00:57:25 | [diff] [blame] | 584 | |
| 585 | ProcessPacket(ConstructAckPacket(1, 0, 0)); |
| 586 | |
| 587 | // Send the response headers (but not the body). |
| 588 | SetResponse("200 OK", std::string()); |
| 589 | ProcessPacket(ConstructResponseHeadersPacket(2, !kFin)); |
| 590 | |
| 591 | // Since the headers have already arrived, this should return immediately. |
| 592 | ASSERT_EQ(OK, stream_->ReadResponseHeaders(callback_.callback())); |
| 593 | ASSERT_TRUE(response_.headers.get()); |
| 594 | EXPECT_EQ(200, response_.headers->response_code()); |
| 595 | EXPECT_TRUE(response_.headers->HasHeaderValue("Content-Type", "text/plain")); |
| 596 | |
| 597 | // Send the response body. |
| 598 | const char kResponseBody[] = "Hello world!"; |
| 599 | ProcessPacket(ConstructDataPacket(3, false, kFin, response_data_.length(), |
| 600 | kResponseBody)); |
| 601 | |
| 602 | // Since the body has already arrived, this should return immediately. |
| 603 | ASSERT_EQ(static_cast<int>(strlen(kResponseBody)), |
| 604 | stream_->ReadResponseBody(read_buffer_.get(), read_buffer_->size(), |
| 605 | callback_.callback())); |
| 606 | |
| 607 | EXPECT_TRUE(stream_->IsResponseBodyComplete()); |
| 608 | EXPECT_TRUE(AtEof()); |
| 609 | } |
| 610 | |
| 611 | TEST_P(QuicHttpStreamTest, SendChunkedPostRequestWithOneEmptyDataPacket) { |
| 612 | SetRequest("POST", "/", DEFAULT_PRIORITY); |
rtenneti | f4bdb54 | 2015-01-21 14:33:05 | [diff] [blame] | 613 | AddWrite(ConstructRequestHeadersPacket(1, !kFin, DEFAULT_PRIORITY)); |
[email protected] | 16ba774 | 2014-08-22 00:57:25 | [diff] [blame] | 614 | AddWrite(ConstructDataPacket(2, kIncludeVersion, kFin, 0, "")); |
| 615 | AddWrite(ConstructAckPacket(3, 3, 1)); |
| 616 | Initialize(); |
| 617 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 618 | ChunkedUploadDataStream upload_data_stream(0); |
[email protected] | 16ba774 | 2014-08-22 00:57:25 | [diff] [blame] | 619 | |
| 620 | request_.method = "POST"; |
| 621 | request_.url = GURL("http://www.google.com/"); |
| 622 | request_.upload_data_stream = &upload_data_stream; |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 623 | ASSERT_EQ(OK, request_.upload_data_stream->Init( |
| 624 | TestCompletionCallback().callback())); |
[email protected] | 16ba774 | 2014-08-22 00:57:25 | [diff] [blame] | 625 | |
| 626 | ASSERT_EQ(OK, stream_->InitializeStream(&request_, DEFAULT_PRIORITY, |
| 627 | net_log_, callback_.callback())); |
| 628 | ASSERT_EQ(ERR_IO_PENDING, stream_->SendRequest(headers_, &response_, |
| 629 | callback_.callback())); |
| 630 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 631 | upload_data_stream.AppendData(nullptr, 0, true); |
[email protected] | 16ba774 | 2014-08-22 00:57:25 | [diff] [blame] | 632 | |
| 633 | ProcessPacket(ConstructAckPacket(1, 0, 0)); |
| 634 | |
| 635 | // Send the response headers (but not the body). |
| 636 | SetResponse("200 OK", std::string()); |
| 637 | ProcessPacket(ConstructResponseHeadersPacket(2, !kFin)); |
| 638 | |
| 639 | // Since the headers have already arrived, this should return immediately. |
| 640 | ASSERT_EQ(OK, stream_->ReadResponseHeaders(callback_.callback())); |
| 641 | ASSERT_TRUE(response_.headers.get()); |
| 642 | EXPECT_EQ(200, response_.headers->response_code()); |
| 643 | EXPECT_TRUE(response_.headers->HasHeaderValue("Content-Type", "text/plain")); |
| 644 | |
| 645 | // Send the response body. |
| 646 | const char kResponseBody[] = "Hello world!"; |
| 647 | ProcessPacket(ConstructDataPacket(3, false, kFin, response_data_.length(), |
| 648 | kResponseBody)); |
| 649 | |
| 650 | // Since the body has already arrived, this should return immediately. |
| 651 | ASSERT_EQ(static_cast<int>(strlen(kResponseBody)), |
| 652 | stream_->ReadResponseBody(read_buffer_.get(), read_buffer_->size(), |
| 653 | callback_.callback())); |
| 654 | |
| 655 | EXPECT_TRUE(stream_->IsResponseBodyComplete()); |
| 656 | EXPECT_TRUE(AtEof()); |
| 657 | } |
| 658 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 659 | TEST_P(QuicHttpStreamTest, DestroyedEarly) { |
| 660 | SetRequest("GET", "/", DEFAULT_PRIORITY); |
rtenneti | f4bdb54 | 2015-01-21 14:33:05 | [diff] [blame] | 661 | AddWrite(ConstructRequestHeadersPacket(1, kFin, DEFAULT_PRIORITY)); |
[email protected] | c5e1aca | 2014-01-30 04:03:04 | [diff] [blame] | 662 | AddWrite(ConstructAckAndRstStreamPacket(2)); |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 663 | use_closing_stream_ = true; |
| 664 | Initialize(); |
| 665 | |
| 666 | request_.method = "GET"; |
| 667 | request_.url = GURL("http://www.google.com/"); |
| 668 | |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 669 | EXPECT_EQ(OK, stream_->InitializeStream(&request_, DEFAULT_PRIORITY, |
| 670 | net_log_, callback_.callback())); |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 671 | EXPECT_EQ(OK, stream_->SendRequest(headers_, &response_, |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 672 | callback_.callback())); |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 673 | |
| 674 | // Ack the request. |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 675 | ProcessPacket(ConstructAckPacket(1, 0, 0)); |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 676 | EXPECT_EQ(ERR_IO_PENDING, |
| 677 | stream_->ReadResponseHeaders(callback_.callback())); |
| 678 | |
| 679 | // Send the response with a body. |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 680 | SetResponse("404 OK", "hello world!"); |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 681 | // In the course of processing this packet, the QuicHttpStream close itself. |
[email protected] | 92bf17c | 2014-03-03 21:14:03 | [diff] [blame] | 682 | ProcessPacket(ConstructResponseHeadersPacket(2, kFin)); |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 683 | |
| 684 | EXPECT_TRUE(AtEof()); |
| 685 | } |
| 686 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 687 | TEST_P(QuicHttpStreamTest, Priority) { |
| 688 | SetRequest("GET", "/", MEDIUM); |
rtenneti | f4bdb54 | 2015-01-21 14:33:05 | [diff] [blame] | 689 | AddWrite(ConstructRequestHeadersPacket(1, kFin, MEDIUM)); |
[email protected] | c5e1aca | 2014-01-30 04:03:04 | [diff] [blame] | 690 | AddWrite(ConstructAckAndRstStreamPacket(2)); |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 691 | use_closing_stream_ = true; |
| 692 | Initialize(); |
| 693 | |
| 694 | request_.method = "GET"; |
| 695 | request_.url = GURL("http://www.google.com/"); |
| 696 | |
| 697 | EXPECT_EQ(OK, stream_->InitializeStream(&request_, MEDIUM, |
| 698 | net_log_, callback_.callback())); |
| 699 | |
| 700 | // Check that priority is highest. |
| 701 | QuicReliableClientStream* reliable_stream = |
| 702 | QuicHttpStreamPeer::GetQuicReliableClientStream(stream_.get()); |
| 703 | DCHECK(reliable_stream); |
[email protected] | 9f0dcd4e | 2014-01-16 15:58:14 | [diff] [blame] | 704 | DCHECK_EQ(QuicWriteBlockedList::kHighestPriority, |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 705 | reliable_stream->EffectivePriority()); |
| 706 | |
| 707 | EXPECT_EQ(OK, stream_->SendRequest(headers_, &response_, |
| 708 | callback_.callback())); |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 709 | |
| 710 | // Check that priority has now dropped back to MEDIUM. |
| 711 | DCHECK_EQ(MEDIUM, ConvertQuicPriorityToRequestPriority( |
| 712 | reliable_stream->EffectivePriority())); |
| 713 | |
| 714 | // Ack the request. |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 715 | ProcessPacket(ConstructAckPacket(1, 0, 0)); |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 716 | EXPECT_EQ(ERR_IO_PENDING, |
| 717 | stream_->ReadResponseHeaders(callback_.callback())); |
| 718 | |
| 719 | // Send the response with a body. |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 720 | SetResponse("404 OK", "hello world!"); |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 721 | // In the course of processing this packet, the QuicHttpStream close itself. |
[email protected] | 92bf17c | 2014-03-03 21:14:03 | [diff] [blame] | 722 | ProcessPacket(ConstructResponseHeadersPacket(2, kFin)); |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 723 | |
| 724 | EXPECT_TRUE(AtEof()); |
| 725 | } |
| 726 | |
[email protected] | e1cca9a | 2013-09-20 17:14:44 | [diff] [blame] | 727 | // Regression test for http://crbug.com/294870 |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 728 | TEST_P(QuicHttpStreamTest, CheckPriorityWithNoDelegate) { |
| 729 | SetRequest("GET", "/", MEDIUM); |
[email protected] | e1cca9a | 2013-09-20 17:14:44 | [diff] [blame] | 730 | use_closing_stream_ = true; |
[email protected] | 459a740 | 2014-02-10 12:58:52 | [diff] [blame] | 731 | |
[email protected] | 08da9adb | 2014-04-24 08:33:31 | [diff] [blame] | 732 | AddWrite(ConstructRstStreamPacket(1)); |
[email protected] | 459a740 | 2014-02-10 12:58:52 | [diff] [blame] | 733 | |
[email protected] | e1cca9a | 2013-09-20 17:14:44 | [diff] [blame] | 734 | Initialize(); |
| 735 | |
| 736 | request_.method = "GET"; |
| 737 | request_.url = GURL("http://www.google.com/"); |
| 738 | |
| 739 | EXPECT_EQ(OK, stream_->InitializeStream(&request_, MEDIUM, |
| 740 | net_log_, callback_.callback())); |
| 741 | |
| 742 | // Check that priority is highest. |
| 743 | QuicReliableClientStream* reliable_stream = |
| 744 | QuicHttpStreamPeer::GetQuicReliableClientStream(stream_.get()); |
| 745 | DCHECK(reliable_stream); |
| 746 | QuicReliableClientStream::Delegate* delegate = reliable_stream->GetDelegate(); |
| 747 | DCHECK(delegate); |
[email protected] | 9f0dcd4e | 2014-01-16 15:58:14 | [diff] [blame] | 748 | DCHECK_EQ(QuicWriteBlockedList::kHighestPriority, |
[email protected] | e1cca9a | 2013-09-20 17:14:44 | [diff] [blame] | 749 | reliable_stream->EffectivePriority()); |
| 750 | |
rtenneti | be63573 | 2014-10-02 22:51:42 | [diff] [blame] | 751 | // Set Delegate to nullptr and make sure EffectivePriority returns highest |
[email protected] | e1cca9a | 2013-09-20 17:14:44 | [diff] [blame] | 752 | // priority. |
rtenneti | be63573 | 2014-10-02 22:51:42 | [diff] [blame] | 753 | reliable_stream->SetDelegate(nullptr); |
[email protected] | 9f0dcd4e | 2014-01-16 15:58:14 | [diff] [blame] | 754 | DCHECK_EQ(QuicWriteBlockedList::kHighestPriority, |
[email protected] | e1cca9a | 2013-09-20 17:14:44 | [diff] [blame] | 755 | reliable_stream->EffectivePriority()); |
| 756 | reliable_stream->SetDelegate(delegate); |
| 757 | } |
| 758 | |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 759 | } // namespace test |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 760 | } // namespace net |