[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!"; |
[email protected] | e4c3ea6 | 2014-03-15 00:45:14 | [diff] [blame] | 53 | const char kServerHostname[] = "www.google.com"; |
| 54 | const uint16 kServerPort = 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), |
rtenneti | 4b06ae7 | 2014-08-26 03:43:43 | [diff] [blame] | 138 | maker_(GetParam(), connection_id_, &clock_), |
[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() { |
| 165 | return socket_data_->at_read_eof() && socket_data_->at_write_eof(); |
| 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)); |
rtenneti | b998b32 | 2014-08-26 00:36:33 | [diff] [blame] | 213 | session_->InitializeSession(QuicServerId(kServerHostname, kServerPort, |
rtenneti | 4a5df26 | 2014-11-07 00:43:58 | [diff] [blame] | 214 | /*is_secure=*/false, |
| 215 | PRIVACY_MODE_DISABLED), |
rtenneti | b998b32 | 2014-08-26 00:36:33 | [diff] [blame] | 216 | &crypto_config_, |
| 217 | &crypto_client_stream_factory_); |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 218 | session_->GetCryptoStream()->CryptoConnect(); |
[email protected] | 8ba8121 | 2013-05-03 13:11:48 | [diff] [blame] | 219 | EXPECT_TRUE(session_->IsCryptoHandshakeConfirmed()); |
[email protected] | 6cca996b | 2013-01-25 07:43:36 | [diff] [blame] | 220 | stream_.reset(use_closing_stream_ ? |
[email protected] | 0b2294d3 | 2013-08-02 00:46:36 | [diff] [blame] | 221 | new AutoClosingStream(session_->GetWeakPtr()) : |
| 222 | new QuicHttpStream(session_->GetWeakPtr())); |
[email protected] | 6cca996b | 2013-01-25 07:43:36 | [diff] [blame] | 223 | } |
| 224 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 225 | void SetRequest(const std::string& method, |
| 226 | const std::string& path, |
| 227 | RequestPriority priority) { |
| 228 | request_headers_ = maker_.GetRequestHeaders(method, "http", path); |
[email protected] | 6cca996b | 2013-01-25 07:43:36 | [diff] [blame] | 229 | } |
| 230 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 231 | void SetResponse(const std::string& status, const std::string& body) { |
| 232 | response_headers_ = maker_.GetResponseHeaders(status); |
[email protected] | 92bf17c | 2014-03-03 21:14:03 | [diff] [blame] | 233 | response_data_ = body; |
[email protected] | 6cca996b | 2013-01-25 07:43:36 | [diff] [blame] | 234 | } |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 235 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 236 | scoped_ptr<QuicEncryptedPacket> ConstructDataPacket( |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 237 | QuicPacketSequenceNumber sequence_number, |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 238 | bool should_include_version, |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 239 | bool fin, |
| 240 | QuicStreamOffset offset, |
| 241 | base::StringPiece data) { |
rtenneti | f4bdb54 | 2015-01-21 14:33:05 | [diff] [blame] | 242 | return maker_.MakeDataPacket(sequence_number, stream_id_, |
| 243 | should_include_version, fin, offset, data); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 244 | } |
| 245 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 246 | scoped_ptr<QuicEncryptedPacket> ConstructRequestHeadersPacket( |
| 247 | QuicPacketSequenceNumber sequence_number, |
rtenneti | f4bdb54 | 2015-01-21 14:33:05 | [diff] [blame] | 248 | bool fin, |
| 249 | RequestPriority request_priority) { |
| 250 | QuicPriority priority = |
| 251 | ConvertRequestPriorityToQuicPriority(request_priority); |
| 252 | return maker_.MakeRequestHeadersPacket(sequence_number, stream_id_, |
| 253 | kIncludeVersion, fin, priority, |
| 254 | request_headers_); |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | scoped_ptr<QuicEncryptedPacket> ConstructResponseHeadersPacket( |
| 258 | QuicPacketSequenceNumber sequence_number, |
| 259 | bool fin) { |
| 260 | return maker_.MakeResponseHeadersPacket( |
| 261 | sequence_number, stream_id_, !kIncludeVersion, fin, response_headers_); |
| 262 | } |
| 263 | |
| 264 | scoped_ptr<QuicEncryptedPacket> ConstructRstStreamPacket( |
[email protected] | 06ff515 | 2013-08-29 01:03:05 | [diff] [blame] | 265 | QuicPacketSequenceNumber sequence_number) { |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 266 | return maker_.MakeRstPacket( |
[email protected] | 51cc134 | 2014-04-18 23:44:37 | [diff] [blame] | 267 | sequence_number, true, stream_id_, |
rtenneti | 4a5df26 | 2014-11-07 00:43:58 | [diff] [blame] | 268 | AdjustErrorForVersion(QUIC_RST_ACKNOWLEDGEMENT, GetParam())); |
[email protected] | 06ff515 | 2013-08-29 01:03:05 | [diff] [blame] | 269 | } |
| 270 | |
[email protected] | c5e1aca | 2014-01-30 04:03:04 | [diff] [blame] | 271 | scoped_ptr<QuicEncryptedPacket> ConstructAckAndRstStreamPacket( |
| 272 | QuicPacketSequenceNumber sequence_number) { |
| 273 | return maker_.MakeAckAndRstPacket( |
| 274 | sequence_number, !kIncludeVersion, stream_id_, QUIC_STREAM_CANCELLED, |
[email protected] | 08da9adb | 2014-04-24 08:33:31 | [diff] [blame] | 275 | 2, 1, !kIncludeCongestionFeedback); |
[email protected] | c5e1aca | 2014-01-30 04:03:04 | [diff] [blame] | 276 | } |
| 277 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 278 | scoped_ptr<QuicEncryptedPacket> ConstructAckPacket( |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 279 | QuicPacketSequenceNumber sequence_number, |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 280 | QuicPacketSequenceNumber largest_received, |
| 281 | QuicPacketSequenceNumber least_unacked) { |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 282 | return maker_.MakeAckPacket(sequence_number, largest_received, |
| 283 | least_unacked, !kIncludeCongestionFeedback); |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 284 | } |
| 285 | |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 286 | BoundNetLog net_log_; |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 287 | bool use_closing_stream_; |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 288 | MockSendAlgorithm* send_algorithm_; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 289 | scoped_refptr<TestTaskRunner> runner_; |
[email protected] | 4356f0f | 2013-04-07 00:58:17 | [diff] [blame] | 290 | scoped_ptr<MockWrite[]> mock_writes_; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 291 | MockClock clock_; |
| 292 | TestQuicConnection* connection_; |
[email protected] | 2cfc6bb8 | 2013-10-27 03:40:44 | [diff] [blame] | 293 | scoped_ptr<QuicConnectionHelper> helper_; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 294 | testing::StrictMock<MockConnectionVisitor> visitor_; |
| 295 | scoped_ptr<QuicHttpStream> stream_; |
[email protected] | 5db45220 | 2014-08-19 05:22:15 | [diff] [blame] | 296 | TransportSecurityState transport_security_state_; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 297 | scoped_ptr<QuicClientSession> session_; |
[email protected] | ef95114d | 2013-04-17 17:57:01 | [diff] [blame] | 298 | QuicCryptoClientConfig crypto_config_; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 299 | TestCompletionCallback callback_; |
| 300 | HttpRequestInfo request_; |
| 301 | HttpRequestHeaders headers_; |
| 302 | HttpResponseInfo response_; |
| 303 | scoped_refptr<IOBufferWithSize> read_buffer_; |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 304 | SpdyHeaderBlock request_headers_; |
| 305 | SpdyHeaderBlock response_headers_; |
[email protected] | 6cca996b | 2013-01-25 07:43:36 | [diff] [blame] | 306 | std::string request_data_; |
| 307 | std::string response_data_; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 308 | |
| 309 | private: |
[email protected] | 3aa9ca7 | 2014-02-27 19:39:43 | [diff] [blame] | 310 | const QuicConnectionId connection_id_; |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 311 | const QuicStreamId stream_id_; |
| 312 | QuicTestPacketMaker maker_; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 313 | IPEndPoint self_addr_; |
| 314 | IPEndPoint peer_addr_; |
[email protected] | 457d695 | 2013-12-13 09:24:58 | [diff] [blame] | 315 | MockRandom random_generator_; |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 316 | MockCryptoClientStreamFactory crypto_client_stream_factory_; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 317 | scoped_ptr<StaticSocketDataProvider> socket_data_; |
| 318 | std::vector<PacketToWrite> writes_; |
| 319 | }; |
| 320 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 321 | INSTANTIATE_TEST_CASE_P(Version, QuicHttpStreamTest, |
| 322 | ::testing::ValuesIn(QuicSupportedVersions())); |
| 323 | |
| 324 | TEST_P(QuicHttpStreamTest, RenewStreamForAuth) { |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 325 | Initialize(); |
rtenneti | be63573 | 2014-10-02 22:51:42 | [diff] [blame] | 326 | EXPECT_EQ(nullptr, stream_->RenewStreamForAuth()); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 327 | } |
| 328 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 329 | TEST_P(QuicHttpStreamTest, CanFindEndOfResponse) { |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 330 | Initialize(); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 331 | EXPECT_TRUE(stream_->CanFindEndOfResponse()); |
| 332 | } |
| 333 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 334 | TEST_P(QuicHttpStreamTest, IsConnectionReusable) { |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 335 | Initialize(); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 336 | EXPECT_FALSE(stream_->IsConnectionReusable()); |
| 337 | } |
| 338 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 339 | TEST_P(QuicHttpStreamTest, GetRequest) { |
| 340 | SetRequest("GET", "/", DEFAULT_PRIORITY); |
rtenneti | f4bdb54 | 2015-01-21 14:33:05 | [diff] [blame] | 341 | AddWrite(ConstructRequestHeadersPacket(1, kFin, DEFAULT_PRIORITY)); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 342 | Initialize(); |
| 343 | |
| 344 | request_.method = "GET"; |
| 345 | request_.url = GURL("http://www.google.com/"); |
| 346 | |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 347 | EXPECT_EQ(OK, stream_->InitializeStream(&request_, DEFAULT_PRIORITY, |
| 348 | net_log_, callback_.callback())); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 349 | EXPECT_EQ(OK, stream_->SendRequest(headers_, &response_, |
| 350 | callback_.callback())); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 351 | |
| 352 | // Ack the request. |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 353 | ProcessPacket(ConstructAckPacket(1, 0, 0)); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 354 | |
| 355 | EXPECT_EQ(ERR_IO_PENDING, |
| 356 | stream_->ReadResponseHeaders(callback_.callback())); |
| 357 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 358 | SetResponse("404 Not Found", std::string()); |
[email protected] | 92bf17c | 2014-03-03 21:14:03 | [diff] [blame] | 359 | ProcessPacket(ConstructResponseHeadersPacket(2, kFin)); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 360 | |
| 361 | // Now that the headers have been processed, the callback will return. |
| 362 | EXPECT_EQ(OK, callback_.WaitForResult()); |
[email protected] | cadac62 | 2013-06-11 16:46:36 | [diff] [blame] | 363 | ASSERT_TRUE(response_.headers.get()); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 364 | EXPECT_EQ(404, response_.headers->response_code()); |
| 365 | EXPECT_TRUE(response_.headers->HasHeaderValue("Content-Type", "text/plain")); |
[email protected] | 6ed6743 | 2014-06-24 01:53:53 | [diff] [blame] | 366 | EXPECT_FALSE(response_.response_time.is_null()); |
| 367 | EXPECT_FALSE(response_.request_time.is_null()); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 368 | |
| 369 | // There is no body, so this should return immediately. |
| 370 | EXPECT_EQ(0, stream_->ReadResponseBody(read_buffer_.get(), |
| 371 | read_buffer_->size(), |
| 372 | callback_.callback())); |
| 373 | EXPECT_TRUE(stream_->IsResponseBodyComplete()); |
| 374 | EXPECT_TRUE(AtEof()); |
| 375 | } |
| 376 | |
[email protected] | 3e7dca6 | 2013-09-10 16:14:23 | [diff] [blame] | 377 | // Regression test for http://crbug.com/288128 |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 378 | TEST_P(QuicHttpStreamTest, GetRequestLargeResponse) { |
| 379 | SetRequest("GET", "/", DEFAULT_PRIORITY); |
rtenneti | f4bdb54 | 2015-01-21 14:33:05 | [diff] [blame] | 380 | AddWrite(ConstructRequestHeadersPacket(1, kFin, DEFAULT_PRIORITY)); |
[email protected] | 3e7dca6 | 2013-09-10 16:14:23 | [diff] [blame] | 381 | Initialize(); |
| 382 | |
| 383 | request_.method = "GET"; |
| 384 | request_.url = GURL("http://www.google.com/"); |
| 385 | |
| 386 | EXPECT_EQ(OK, stream_->InitializeStream(&request_, DEFAULT_PRIORITY, |
| 387 | net_log_, callback_.callback())); |
| 388 | EXPECT_EQ(OK, stream_->SendRequest(headers_, &response_, |
| 389 | callback_.callback())); |
[email protected] | 3e7dca6 | 2013-09-10 16:14:23 | [diff] [blame] | 390 | |
| 391 | // Ack the request. |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 392 | ProcessPacket(ConstructAckPacket(1, 0, 0)); |
[email protected] | 3e7dca6 | 2013-09-10 16:14:23 | [diff] [blame] | 393 | |
| 394 | EXPECT_EQ(ERR_IO_PENDING, |
| 395 | stream_->ReadResponseHeaders(callback_.callback())); |
| 396 | |
| 397 | SpdyHeaderBlock headers; |
| 398 | headers[":status"] = "200 OK"; |
| 399 | headers[":version"] = "HTTP/1.1"; |
| 400 | headers["content-type"] = "text/plain"; |
| 401 | headers["big6"] = std::string(10000, 'x'); // Lots of x's. |
| 402 | |
rch | c081544 | 2015-04-18 13:29:46 | [diff] [blame^] | 403 | std::string response = |
| 404 | SpdyUtils::SerializeUncompressedHeaders(headers, GetParam()); |
[email protected] | 3e7dca6 | 2013-09-10 16:14:23 | [diff] [blame] | 405 | EXPECT_LT(4096u, response.length()); |
| 406 | stream_->OnDataReceived(response.data(), response.length()); |
| 407 | stream_->OnClose(QUIC_NO_ERROR); |
| 408 | |
| 409 | // Now that the headers have been processed, the callback will return. |
| 410 | EXPECT_EQ(OK, callback_.WaitForResult()); |
| 411 | ASSERT_TRUE(response_.headers.get()); |
| 412 | EXPECT_EQ(200, response_.headers->response_code()); |
| 413 | EXPECT_TRUE(response_.headers->HasHeaderValue("Content-Type", "text/plain")); |
| 414 | |
| 415 | // There is no body, so this should return immediately. |
| 416 | EXPECT_EQ(0, stream_->ReadResponseBody(read_buffer_.get(), |
| 417 | read_buffer_->size(), |
| 418 | callback_.callback())); |
| 419 | EXPECT_TRUE(stream_->IsResponseBodyComplete()); |
| 420 | EXPECT_TRUE(AtEof()); |
| 421 | } |
| 422 | |
rch | f9f103cbc | 2014-08-30 05:28:04 | [diff] [blame] | 423 | // Regression test for http://crbug.com/409101 |
| 424 | TEST_P(QuicHttpStreamTest, SessionClosedBeforeSendRequest) { |
| 425 | SetRequest("GET", "/", DEFAULT_PRIORITY); |
| 426 | Initialize(); |
| 427 | |
| 428 | request_.method = "GET"; |
| 429 | request_.url = GURL("http://www.google.com/"); |
| 430 | |
| 431 | EXPECT_EQ(OK, stream_->InitializeStream(&request_, DEFAULT_PRIORITY, |
| 432 | net_log_, callback_.callback())); |
| 433 | |
| 434 | session_->connection()->CloseConnection(QUIC_NO_ERROR, true); |
| 435 | |
| 436 | EXPECT_EQ(ERR_CONNECTION_CLOSED, |
| 437 | stream_->SendRequest(headers_, &response_, |
| 438 | callback_.callback())); |
| 439 | } |
| 440 | |
rch | 11a114a | 2014-09-04 23:41:59 | [diff] [blame] | 441 | // Regression test for http://crbug.com/409871 |
| 442 | TEST_P(QuicHttpStreamTest, SessionClosedBeforeReadResponseHeaders) { |
| 443 | SetRequest("GET", "/", DEFAULT_PRIORITY); |
rtenneti | f4bdb54 | 2015-01-21 14:33:05 | [diff] [blame] | 444 | AddWrite(ConstructRequestHeadersPacket(1, kFin, DEFAULT_PRIORITY)); |
rch | 11a114a | 2014-09-04 23:41:59 | [diff] [blame] | 445 | Initialize(); |
| 446 | |
| 447 | request_.method = "GET"; |
| 448 | request_.url = GURL("http://www.google.com/"); |
| 449 | |
| 450 | EXPECT_EQ(OK, stream_->InitializeStream(&request_, DEFAULT_PRIORITY, |
| 451 | net_log_, callback_.callback())); |
| 452 | |
| 453 | EXPECT_EQ(OK, stream_->SendRequest(headers_, &response_, |
| 454 | callback_.callback())); |
| 455 | |
| 456 | session_->connection()->CloseConnection(QUIC_NO_ERROR, true); |
| 457 | |
| 458 | EXPECT_NE(OK, stream_->ReadResponseHeaders(callback_.callback())); |
| 459 | } |
| 460 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 461 | TEST_P(QuicHttpStreamTest, SendPostRequest) { |
| 462 | SetRequest("POST", "/", DEFAULT_PRIORITY); |
rtenneti | f4bdb54 | 2015-01-21 14:33:05 | [diff] [blame] | 463 | AddWrite(ConstructRequestHeadersPacket(1, !kFin, DEFAULT_PRIORITY)); |
[email protected] | 92bf17c | 2014-03-03 21:14:03 | [diff] [blame] | 464 | AddWrite(ConstructDataPacket(2, kIncludeVersion, kFin, 0, kUploadData)); |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 465 | AddWrite(ConstructAckPacket(3, 3, 1)); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 466 | |
| 467 | Initialize(); |
| 468 | |
[email protected] | b2d26cfd | 2012-12-11 10:36:06 | [diff] [blame] | 469 | ScopedVector<UploadElementReader> element_readers; |
| 470 | element_readers.push_back( |
| 471 | new UploadBytesElementReader(kUploadData, strlen(kUploadData))); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 472 | ElementsUploadDataStream upload_data_stream(element_readers.Pass(), 0); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 473 | request_.method = "POST"; |
| 474 | request_.url = GURL("http://www.google.com/"); |
| 475 | request_.upload_data_stream = &upload_data_stream; |
[email protected] | 4db27d8 | 2012-12-20 11:50:24 | [diff] [blame] | 476 | ASSERT_EQ(OK, request_.upload_data_stream->Init(CompletionCallback())); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 477 | |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 478 | EXPECT_EQ(OK, stream_->InitializeStream(&request_, DEFAULT_PRIORITY, |
| 479 | net_log_, callback_.callback())); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 480 | EXPECT_EQ(OK, stream_->SendRequest(headers_, &response_, |
| 481 | callback_.callback())); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 482 | |
| 483 | // Ack both packets in the request. |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 484 | ProcessPacket(ConstructAckPacket(1, 0, 0)); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 485 | |
| 486 | // Send the response headers (but not the body). |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 487 | SetResponse("200 OK", std::string()); |
[email protected] | 92bf17c | 2014-03-03 21:14:03 | [diff] [blame] | 488 | ProcessPacket(ConstructResponseHeadersPacket(2, !kFin)); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 489 | |
| 490 | // Since the headers have already arrived, this should return immediately. |
| 491 | EXPECT_EQ(OK, stream_->ReadResponseHeaders(callback_.callback())); |
[email protected] | cadac62 | 2013-06-11 16:46:36 | [diff] [blame] | 492 | ASSERT_TRUE(response_.headers.get()); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 493 | EXPECT_EQ(200, response_.headers->response_code()); |
| 494 | EXPECT_TRUE(response_.headers->HasHeaderValue("Content-Type", "text/plain")); |
| 495 | |
| 496 | // Send the response body. |
| 497 | const char kResponseBody[] = "Hello world!"; |
[email protected] | 92bf17c | 2014-03-03 21:14:03 | [diff] [blame] | 498 | ProcessPacket(ConstructDataPacket(3, false, kFin, 0, kResponseBody)); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 499 | // Since the body has already arrived, this should return immediately. |
| 500 | EXPECT_EQ(static_cast<int>(strlen(kResponseBody)), |
| 501 | stream_->ReadResponseBody(read_buffer_.get(), read_buffer_->size(), |
| 502 | callback_.callback())); |
| 503 | |
| 504 | EXPECT_TRUE(stream_->IsResponseBodyComplete()); |
| 505 | EXPECT_TRUE(AtEof()); |
| 506 | } |
| 507 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 508 | TEST_P(QuicHttpStreamTest, SendChunkedPostRequest) { |
| 509 | SetRequest("POST", "/", DEFAULT_PRIORITY); |
[email protected] | c9e49a0 | 2013-02-26 05:56:47 | [diff] [blame] | 510 | size_t chunk_size = strlen(kUploadData); |
rtenneti | f4bdb54 | 2015-01-21 14:33:05 | [diff] [blame] | 511 | AddWrite(ConstructRequestHeadersPacket(1, !kFin, DEFAULT_PRIORITY)); |
[email protected] | 92bf17c | 2014-03-03 21:14:03 | [diff] [blame] | 512 | AddWrite(ConstructDataPacket(2, kIncludeVersion, !kFin, 0, kUploadData)); |
| 513 | AddWrite(ConstructDataPacket(3, kIncludeVersion, kFin, chunk_size, |
| 514 | kUploadData)); |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 515 | AddWrite(ConstructAckPacket(4, 3, 1)); |
[email protected] | c9e49a0 | 2013-02-26 05:56:47 | [diff] [blame] | 516 | Initialize(); |
| 517 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 518 | ChunkedUploadDataStream upload_data_stream(0); |
| 519 | upload_data_stream.AppendData(kUploadData, chunk_size, false); |
[email protected] | c9e49a0 | 2013-02-26 05:56:47 | [diff] [blame] | 520 | |
| 521 | request_.method = "POST"; |
| 522 | request_.url = GURL("http://www.google.com/"); |
| 523 | request_.upload_data_stream = &upload_data_stream; |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 524 | ASSERT_EQ(OK, request_.upload_data_stream->Init( |
| 525 | TestCompletionCallback().callback())); |
[email protected] | c9e49a0 | 2013-02-26 05:56:47 | [diff] [blame] | 526 | |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 527 | ASSERT_EQ(OK, stream_->InitializeStream(&request_, DEFAULT_PRIORITY, |
| 528 | net_log_, callback_.callback())); |
[email protected] | c9e49a0 | 2013-02-26 05:56:47 | [diff] [blame] | 529 | ASSERT_EQ(ERR_IO_PENDING, stream_->SendRequest(headers_, &response_, |
| 530 | callback_.callback())); |
[email protected] | c9e49a0 | 2013-02-26 05:56:47 | [diff] [blame] | 531 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 532 | upload_data_stream.AppendData(kUploadData, chunk_size, true); |
[email protected] | c9e49a0 | 2013-02-26 05:56:47 | [diff] [blame] | 533 | |
| 534 | // Ack both packets in the request. |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 535 | ProcessPacket(ConstructAckPacket(1, 0, 0)); |
[email protected] | c9e49a0 | 2013-02-26 05:56:47 | [diff] [blame] | 536 | |
| 537 | // Send the response headers (but not the body). |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 538 | SetResponse("200 OK", std::string()); |
[email protected] | 92bf17c | 2014-03-03 21:14:03 | [diff] [blame] | 539 | ProcessPacket(ConstructResponseHeadersPacket(2, !kFin)); |
[email protected] | c9e49a0 | 2013-02-26 05:56:47 | [diff] [blame] | 540 | |
| 541 | // Since the headers have already arrived, this should return immediately. |
| 542 | ASSERT_EQ(OK, stream_->ReadResponseHeaders(callback_.callback())); |
[email protected] | cadac62 | 2013-06-11 16:46:36 | [diff] [blame] | 543 | ASSERT_TRUE(response_.headers.get()); |
[email protected] | c9e49a0 | 2013-02-26 05:56:47 | [diff] [blame] | 544 | EXPECT_EQ(200, response_.headers->response_code()); |
| 545 | EXPECT_TRUE(response_.headers->HasHeaderValue("Content-Type", "text/plain")); |
| 546 | |
| 547 | // Send the response body. |
| 548 | const char kResponseBody[] = "Hello world!"; |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 549 | ProcessPacket(ConstructDataPacket(3, false, kFin, response_data_.length(), |
| 550 | kResponseBody)); |
[email protected] | c9e49a0 | 2013-02-26 05:56:47 | [diff] [blame] | 551 | |
| 552 | // Since the body has already arrived, this should return immediately. |
| 553 | ASSERT_EQ(static_cast<int>(strlen(kResponseBody)), |
| 554 | stream_->ReadResponseBody(read_buffer_.get(), read_buffer_->size(), |
| 555 | callback_.callback())); |
| 556 | |
| 557 | EXPECT_TRUE(stream_->IsResponseBodyComplete()); |
| 558 | EXPECT_TRUE(AtEof()); |
| 559 | } |
| 560 | |
[email protected] | 16ba774 | 2014-08-22 00:57:25 | [diff] [blame] | 561 | TEST_P(QuicHttpStreamTest, SendChunkedPostRequestWithFinalEmptyDataPacket) { |
| 562 | SetRequest("POST", "/", DEFAULT_PRIORITY); |
| 563 | size_t chunk_size = strlen(kUploadData); |
rtenneti | f4bdb54 | 2015-01-21 14:33:05 | [diff] [blame] | 564 | AddWrite(ConstructRequestHeadersPacket(1, !kFin, DEFAULT_PRIORITY)); |
[email protected] | 16ba774 | 2014-08-22 00:57:25 | [diff] [blame] | 565 | AddWrite(ConstructDataPacket(2, kIncludeVersion, !kFin, 0, kUploadData)); |
| 566 | AddWrite(ConstructDataPacket(3, kIncludeVersion, kFin, chunk_size, "")); |
| 567 | AddWrite(ConstructAckPacket(4, 3, 1)); |
| 568 | Initialize(); |
| 569 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 570 | ChunkedUploadDataStream upload_data_stream(0); |
| 571 | upload_data_stream.AppendData(kUploadData, chunk_size, false); |
[email protected] | 16ba774 | 2014-08-22 00:57:25 | [diff] [blame] | 572 | |
| 573 | request_.method = "POST"; |
| 574 | request_.url = GURL("http://www.google.com/"); |
| 575 | request_.upload_data_stream = &upload_data_stream; |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 576 | ASSERT_EQ(OK, request_.upload_data_stream->Init( |
| 577 | TestCompletionCallback().callback())); |
[email protected] | 16ba774 | 2014-08-22 00:57:25 | [diff] [blame] | 578 | |
| 579 | ASSERT_EQ(OK, stream_->InitializeStream(&request_, DEFAULT_PRIORITY, |
| 580 | net_log_, callback_.callback())); |
| 581 | ASSERT_EQ(ERR_IO_PENDING, stream_->SendRequest(headers_, &response_, |
| 582 | callback_.callback())); |
| 583 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 584 | upload_data_stream.AppendData(nullptr, 0, true); |
[email protected] | 16ba774 | 2014-08-22 00:57:25 | [diff] [blame] | 585 | |
| 586 | ProcessPacket(ConstructAckPacket(1, 0, 0)); |
| 587 | |
| 588 | // Send the response headers (but not the body). |
| 589 | SetResponse("200 OK", std::string()); |
| 590 | ProcessPacket(ConstructResponseHeadersPacket(2, !kFin)); |
| 591 | |
| 592 | // Since the headers have already arrived, this should return immediately. |
| 593 | ASSERT_EQ(OK, stream_->ReadResponseHeaders(callback_.callback())); |
| 594 | ASSERT_TRUE(response_.headers.get()); |
| 595 | EXPECT_EQ(200, response_.headers->response_code()); |
| 596 | EXPECT_TRUE(response_.headers->HasHeaderValue("Content-Type", "text/plain")); |
| 597 | |
| 598 | // Send the response body. |
| 599 | const char kResponseBody[] = "Hello world!"; |
| 600 | ProcessPacket(ConstructDataPacket(3, false, kFin, response_data_.length(), |
| 601 | kResponseBody)); |
| 602 | |
| 603 | // Since the body has already arrived, this should return immediately. |
| 604 | ASSERT_EQ(static_cast<int>(strlen(kResponseBody)), |
| 605 | stream_->ReadResponseBody(read_buffer_.get(), read_buffer_->size(), |
| 606 | callback_.callback())); |
| 607 | |
| 608 | EXPECT_TRUE(stream_->IsResponseBodyComplete()); |
| 609 | EXPECT_TRUE(AtEof()); |
| 610 | } |
| 611 | |
| 612 | TEST_P(QuicHttpStreamTest, SendChunkedPostRequestWithOneEmptyDataPacket) { |
| 613 | SetRequest("POST", "/", DEFAULT_PRIORITY); |
rtenneti | f4bdb54 | 2015-01-21 14:33:05 | [diff] [blame] | 614 | AddWrite(ConstructRequestHeadersPacket(1, !kFin, DEFAULT_PRIORITY)); |
[email protected] | 16ba774 | 2014-08-22 00:57:25 | [diff] [blame] | 615 | AddWrite(ConstructDataPacket(2, kIncludeVersion, kFin, 0, "")); |
| 616 | AddWrite(ConstructAckPacket(3, 3, 1)); |
| 617 | Initialize(); |
| 618 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 619 | ChunkedUploadDataStream upload_data_stream(0); |
[email protected] | 16ba774 | 2014-08-22 00:57:25 | [diff] [blame] | 620 | |
| 621 | request_.method = "POST"; |
| 622 | request_.url = GURL("http://www.google.com/"); |
| 623 | request_.upload_data_stream = &upload_data_stream; |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 624 | ASSERT_EQ(OK, request_.upload_data_stream->Init( |
| 625 | TestCompletionCallback().callback())); |
[email protected] | 16ba774 | 2014-08-22 00:57:25 | [diff] [blame] | 626 | |
| 627 | ASSERT_EQ(OK, stream_->InitializeStream(&request_, DEFAULT_PRIORITY, |
| 628 | net_log_, callback_.callback())); |
| 629 | ASSERT_EQ(ERR_IO_PENDING, stream_->SendRequest(headers_, &response_, |
| 630 | callback_.callback())); |
| 631 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 632 | upload_data_stream.AppendData(nullptr, 0, true); |
[email protected] | 16ba774 | 2014-08-22 00:57:25 | [diff] [blame] | 633 | |
| 634 | ProcessPacket(ConstructAckPacket(1, 0, 0)); |
| 635 | |
| 636 | // Send the response headers (but not the body). |
| 637 | SetResponse("200 OK", std::string()); |
| 638 | ProcessPacket(ConstructResponseHeadersPacket(2, !kFin)); |
| 639 | |
| 640 | // Since the headers have already arrived, this should return immediately. |
| 641 | ASSERT_EQ(OK, stream_->ReadResponseHeaders(callback_.callback())); |
| 642 | ASSERT_TRUE(response_.headers.get()); |
| 643 | EXPECT_EQ(200, response_.headers->response_code()); |
| 644 | EXPECT_TRUE(response_.headers->HasHeaderValue("Content-Type", "text/plain")); |
| 645 | |
| 646 | // Send the response body. |
| 647 | const char kResponseBody[] = "Hello world!"; |
| 648 | ProcessPacket(ConstructDataPacket(3, false, kFin, response_data_.length(), |
| 649 | kResponseBody)); |
| 650 | |
| 651 | // Since the body has already arrived, this should return immediately. |
| 652 | ASSERT_EQ(static_cast<int>(strlen(kResponseBody)), |
| 653 | stream_->ReadResponseBody(read_buffer_.get(), read_buffer_->size(), |
| 654 | callback_.callback())); |
| 655 | |
| 656 | EXPECT_TRUE(stream_->IsResponseBodyComplete()); |
| 657 | EXPECT_TRUE(AtEof()); |
| 658 | } |
| 659 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 660 | TEST_P(QuicHttpStreamTest, DestroyedEarly) { |
| 661 | SetRequest("GET", "/", DEFAULT_PRIORITY); |
rtenneti | f4bdb54 | 2015-01-21 14:33:05 | [diff] [blame] | 662 | AddWrite(ConstructRequestHeadersPacket(1, kFin, DEFAULT_PRIORITY)); |
[email protected] | c5e1aca | 2014-01-30 04:03:04 | [diff] [blame] | 663 | AddWrite(ConstructAckAndRstStreamPacket(2)); |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 664 | use_closing_stream_ = true; |
| 665 | Initialize(); |
| 666 | |
| 667 | request_.method = "GET"; |
| 668 | request_.url = GURL("http://www.google.com/"); |
| 669 | |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 670 | EXPECT_EQ(OK, stream_->InitializeStream(&request_, DEFAULT_PRIORITY, |
| 671 | net_log_, callback_.callback())); |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 672 | EXPECT_EQ(OK, stream_->SendRequest(headers_, &response_, |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 673 | callback_.callback())); |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 674 | |
| 675 | // Ack the request. |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 676 | ProcessPacket(ConstructAckPacket(1, 0, 0)); |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 677 | EXPECT_EQ(ERR_IO_PENDING, |
| 678 | stream_->ReadResponseHeaders(callback_.callback())); |
| 679 | |
| 680 | // Send the response with a body. |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 681 | SetResponse("404 OK", "hello world!"); |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 682 | // In the course of processing this packet, the QuicHttpStream close itself. |
[email protected] | 92bf17c | 2014-03-03 21:14:03 | [diff] [blame] | 683 | ProcessPacket(ConstructResponseHeadersPacket(2, kFin)); |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 684 | |
| 685 | EXPECT_TRUE(AtEof()); |
| 686 | } |
| 687 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 688 | TEST_P(QuicHttpStreamTest, Priority) { |
| 689 | SetRequest("GET", "/", MEDIUM); |
rtenneti | f4bdb54 | 2015-01-21 14:33:05 | [diff] [blame] | 690 | AddWrite(ConstructRequestHeadersPacket(1, kFin, MEDIUM)); |
[email protected] | c5e1aca | 2014-01-30 04:03:04 | [diff] [blame] | 691 | AddWrite(ConstructAckAndRstStreamPacket(2)); |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 692 | use_closing_stream_ = true; |
| 693 | Initialize(); |
| 694 | |
| 695 | request_.method = "GET"; |
| 696 | request_.url = GURL("http://www.google.com/"); |
| 697 | |
| 698 | EXPECT_EQ(OK, stream_->InitializeStream(&request_, MEDIUM, |
| 699 | net_log_, callback_.callback())); |
| 700 | |
| 701 | // Check that priority is highest. |
| 702 | QuicReliableClientStream* reliable_stream = |
| 703 | QuicHttpStreamPeer::GetQuicReliableClientStream(stream_.get()); |
| 704 | DCHECK(reliable_stream); |
[email protected] | 9f0dcd4e | 2014-01-16 15:58:14 | [diff] [blame] | 705 | DCHECK_EQ(QuicWriteBlockedList::kHighestPriority, |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 706 | reliable_stream->EffectivePriority()); |
| 707 | |
| 708 | EXPECT_EQ(OK, stream_->SendRequest(headers_, &response_, |
| 709 | callback_.callback())); |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 710 | |
| 711 | // Check that priority has now dropped back to MEDIUM. |
| 712 | DCHECK_EQ(MEDIUM, ConvertQuicPriorityToRequestPriority( |
| 713 | reliable_stream->EffectivePriority())); |
| 714 | |
| 715 | // Ack the request. |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 716 | ProcessPacket(ConstructAckPacket(1, 0, 0)); |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 717 | EXPECT_EQ(ERR_IO_PENDING, |
| 718 | stream_->ReadResponseHeaders(callback_.callback())); |
| 719 | |
| 720 | // Send the response with a body. |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 721 | SetResponse("404 OK", "hello world!"); |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 722 | // In the course of processing this packet, the QuicHttpStream close itself. |
[email protected] | 92bf17c | 2014-03-03 21:14:03 | [diff] [blame] | 723 | ProcessPacket(ConstructResponseHeadersPacket(2, kFin)); |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 724 | |
| 725 | EXPECT_TRUE(AtEof()); |
| 726 | } |
| 727 | |
[email protected] | e1cca9a | 2013-09-20 17:14:44 | [diff] [blame] | 728 | // Regression test for http://crbug.com/294870 |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 729 | TEST_P(QuicHttpStreamTest, CheckPriorityWithNoDelegate) { |
| 730 | SetRequest("GET", "/", MEDIUM); |
[email protected] | e1cca9a | 2013-09-20 17:14:44 | [diff] [blame] | 731 | use_closing_stream_ = true; |
[email protected] | 459a740 | 2014-02-10 12:58:52 | [diff] [blame] | 732 | |
[email protected] | 08da9adb | 2014-04-24 08:33:31 | [diff] [blame] | 733 | AddWrite(ConstructRstStreamPacket(1)); |
[email protected] | 459a740 | 2014-02-10 12:58:52 | [diff] [blame] | 734 | |
[email protected] | e1cca9a | 2013-09-20 17:14:44 | [diff] [blame] | 735 | Initialize(); |
| 736 | |
| 737 | request_.method = "GET"; |
| 738 | request_.url = GURL("http://www.google.com/"); |
| 739 | |
| 740 | EXPECT_EQ(OK, stream_->InitializeStream(&request_, MEDIUM, |
| 741 | net_log_, callback_.callback())); |
| 742 | |
| 743 | // Check that priority is highest. |
| 744 | QuicReliableClientStream* reliable_stream = |
| 745 | QuicHttpStreamPeer::GetQuicReliableClientStream(stream_.get()); |
| 746 | DCHECK(reliable_stream); |
| 747 | QuicReliableClientStream::Delegate* delegate = reliable_stream->GetDelegate(); |
| 748 | DCHECK(delegate); |
[email protected] | 9f0dcd4e | 2014-01-16 15:58:14 | [diff] [blame] | 749 | DCHECK_EQ(QuicWriteBlockedList::kHighestPriority, |
[email protected] | e1cca9a | 2013-09-20 17:14:44 | [diff] [blame] | 750 | reliable_stream->EffectivePriority()); |
| 751 | |
rtenneti | be63573 | 2014-10-02 22:51:42 | [diff] [blame] | 752 | // Set Delegate to nullptr and make sure EffectivePriority returns highest |
[email protected] | e1cca9a | 2013-09-20 17:14:44 | [diff] [blame] | 753 | // priority. |
rtenneti | be63573 | 2014-10-02 22:51:42 | [diff] [blame] | 754 | reliable_stream->SetDelegate(nullptr); |
[email protected] | 9f0dcd4e | 2014-01-16 15:58:14 | [diff] [blame] | 755 | DCHECK_EQ(QuicWriteBlockedList::kHighestPriority, |
[email protected] | e1cca9a | 2013-09-20 17:14:44 | [diff] [blame] | 756 | reliable_stream->EffectivePriority()); |
| 757 | reliable_stream->SetDelegate(delegate); |
| 758 | } |
| 759 | |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 760 | } // namespace test |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 761 | } // namespace net |