[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 | |
sclittle | 1edeeb2 | 2015-09-02 20:46:10 | [diff] [blame] | 7 | #include <stdint.h> |
| 8 | |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 11 | #include "base/memory/scoped_ptr.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 12 | #include "base/thread_task_runner_handle.h" |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 13 | #include "net/base/chunked_upload_data_stream.h" |
| 14 | #include "net/base/elements_upload_data_stream.h" |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 15 | #include "net/base/net_errors.h" |
tbansal | fdf5665b | 2015-09-21 22:46:40 | [diff] [blame] | 16 | #include "net/base/socket_performance_watcher.h" |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 17 | #include "net/base/test_completion_callback.h" |
[email protected] | b2d26cfd | 2012-12-11 10:36:06 | [diff] [blame] | 18 | #include "net/base/upload_bytes_element_reader.h" |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 19 | #include "net/http/http_response_headers.h" |
[email protected] | 5db45220 | 2014-08-19 05:22:15 | [diff] [blame] | 20 | #include "net/http/transport_security_state.h" |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 21 | #include "net/quic/congestion_control/send_algorithm_interface.h" |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 22 | #include "net/quic/crypto/crypto_protocol.h" |
[email protected] | 4df6984 | 2013-02-27 06:32:16 | [diff] [blame] | 23 | #include "net/quic/crypto/quic_decrypter.h" |
| 24 | #include "net/quic/crypto/quic_encrypter.h" |
[email protected] | 17bf15c | 2014-03-14 10:08:04 | [diff] [blame] | 25 | #include "net/quic/crypto/quic_server_info.h" |
ckrasic | 4f9d88d | 2015-07-22 22:23:16 | [diff] [blame] | 26 | #include "net/quic/quic_chromium_client_session.h" |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 27 | #include "net/quic/quic_connection.h" |
| 28 | #include "net/quic/quic_connection_helper.h" |
[email protected] | cbd731e | 2013-10-24 00:20:39 | [diff] [blame] | 29 | #include "net/quic/quic_default_packet_writer.h" |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 30 | #include "net/quic/quic_http_utils.h" |
rtenneti | 1cd3b16 | 2015-09-29 02:58:28 | [diff] [blame] | 31 | #include "net/quic/quic_packet_reader.h" |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 32 | #include "net/quic/quic_reliable_client_stream.h" |
[email protected] | 9f0dcd4e | 2014-01-16 15:58:14 | [diff] [blame] | 33 | #include "net/quic/quic_write_blocked_list.h" |
[email protected] | 3e7dca6 | 2013-09-10 16:14:23 | [diff] [blame] | 34 | #include "net/quic/spdy_utils.h" |
rch | 1fe2eeb | 2015-10-26 14:45:57 | [diff] [blame] | 35 | #include "net/quic/test_tools/crypto_test_utils.h" |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 36 | #include "net/quic/test_tools/mock_clock.h" |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 37 | #include "net/quic/test_tools/mock_crypto_client_stream_factory.h" |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 38 | #include "net/quic/test_tools/mock_random.h" |
[email protected] | b1f287d | 2012-12-22 17:25:39 | [diff] [blame] | 39 | #include "net/quic/test_tools/quic_connection_peer.h" |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 40 | #include "net/quic/test_tools/quic_test_packet_maker.h" |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 41 | #include "net/quic/test_tools/quic_test_utils.h" |
| 42 | #include "net/quic/test_tools/test_task_runner.h" |
| 43 | #include "net/socket/socket_test_util.h" |
[email protected] | 6cca996b | 2013-01-25 07:43:36 | [diff] [blame] | 44 | #include "net/spdy/spdy_frame_builder.h" |
| 45 | #include "net/spdy/spdy_framer.h" |
| 46 | #include "net/spdy/spdy_http_utils.h" |
| 47 | #include "net/spdy/spdy_protocol.h" |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 48 | #include "testing/gmock/include/gmock/gmock.h" |
| 49 | #include "testing/gtest/include/gtest/gtest.h" |
| 50 | |
| 51 | using testing::_; |
[email protected] | 06ff515 | 2013-08-29 01:03:05 | [diff] [blame] | 52 | using testing::AnyNumber; |
| 53 | using testing::Return; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 54 | |
| 55 | namespace net { |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 56 | namespace test { |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 57 | namespace { |
| 58 | |
[email protected] | 16ba774 | 2014-08-22 00:57:25 | [diff] [blame] | 59 | const char kUploadData[] = "Really nifty data!"; |
bnc | b07c0553 | 2015-05-14 19:07:20 | [diff] [blame] | 60 | const char kDefaultServerHostName[] = "www.google.com"; |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 61 | const uint16_t kDefaultServerPort = 80; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 62 | |
| 63 | class TestQuicConnection : public QuicConnection { |
| 64 | public: |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 65 | TestQuicConnection(const QuicVersionVector& versions, |
[email protected] | 3aa9ca7 | 2014-02-27 19:39:43 | [diff] [blame] | 66 | QuicConnectionId connection_id, |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 67 | IPEndPoint address, |
[email protected] | cbd731e | 2013-10-24 00:20:39 | [diff] [blame] | 68 | QuicConnectionHelper* helper, |
jdorfman | 90d185f3 | 2016-01-15 13:22:47 | [diff] [blame^] | 69 | QuicPacketWriter* writer) |
[email protected] | 66cd2d6 | 2014-08-01 18:42:39 | [diff] [blame] | 70 | : QuicConnection(connection_id, |
| 71 | address, |
| 72 | helper, |
jdorfman | 90d185f3 | 2016-01-15 13:22:47 | [diff] [blame^] | 73 | writer, |
rtenneti | 6f48aa9 | 2015-03-16 02:18:48 | [diff] [blame] | 74 | true /* owns_writer */, |
| 75 | Perspective::IS_CLIENT, |
rtenneti | 6f48aa9 | 2015-03-16 02:18:48 | [diff] [blame] | 76 | versions) {} |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 77 | |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 78 | void SetSendAlgorithm(SendAlgorithmInterface* send_algorithm) { |
| 79 | QuicConnectionPeer::SetSendAlgorithm(this, send_algorithm); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 80 | } |
| 81 | }; |
| 82 | |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 83 | // Subclass of QuicHttpStream that closes itself when the first piece of data |
| 84 | // is received. |
| 85 | class AutoClosingStream : public QuicHttpStream { |
| 86 | public: |
ckrasic | 4f9d88d | 2015-07-22 22:23:16 | [diff] [blame] | 87 | explicit AutoClosingStream( |
| 88 | const base::WeakPtr<QuicChromiumClientSession>& session) |
| 89 | : QuicHttpStream(session) {} |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 90 | |
sclittle | c4dc1a3 | 2015-09-24 00:15:45 | [diff] [blame] | 91 | void OnHeadersAvailable(const SpdyHeaderBlock& headers, |
| 92 | size_t frame_len) override { |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 93 | Close(false); |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 94 | } |
rch | b27683c | 2015-07-29 23:53:50 | [diff] [blame] | 95 | |
| 96 | void OnDataAvailable() override { Close(false); } |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 97 | }; |
| 98 | |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 99 | } // namespace |
| 100 | |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 101 | class QuicHttpStreamPeer { |
| 102 | public: |
| 103 | static QuicReliableClientStream* GetQuicReliableClientStream( |
| 104 | QuicHttpStream* stream) { |
| 105 | return stream->stream_; |
| 106 | } |
zhongyi | ca364fbb | 2015-12-12 03:39:12 | [diff] [blame] | 107 | |
| 108 | static bool WasHandshakeConfirmed(QuicHttpStream* stream) { |
| 109 | return stream->was_handshake_confirmed_; |
| 110 | } |
| 111 | |
| 112 | static void SetHandshakeConfirmed(QuicHttpStream* stream, bool confirmed) { |
| 113 | stream->was_handshake_confirmed_ = confirmed; |
| 114 | } |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 115 | }; |
| 116 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 117 | class QuicHttpStreamTest : public ::testing::TestWithParam<QuicVersion> { |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 118 | protected: |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 119 | static const bool kFin = true; |
| 120 | static const bool kIncludeVersion = true; |
| 121 | static const bool kIncludeCongestionFeedback = true; |
| 122 | |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 123 | // Holds a packet to be written to the wire, and the IO mode that should |
| 124 | // be used by the mock socket when performing the write. |
| 125 | struct PacketToWrite { |
| 126 | PacketToWrite(IoMode mode, QuicEncryptedPacket* packet) |
rjshade | d5ced07 | 2015-12-18 19:26:02 | [diff] [blame] | 127 | : mode(mode), packet(packet) {} |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 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), |
rch | 1fe2eeb | 2015-10-26 14:45:57 | [diff] [blame] | 135 | crypto_config_(CryptoTestUtils::ProofVerifierForTesting()), |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 136 | read_buffer_(new IOBufferWithSize(4096)), |
[email protected] | 3aa9ca7 | 2014-02-27 19:39:43 | [diff] [blame] | 137 | connection_id_(2), |
[email protected] | 66ae596 | 2014-05-22 11:13:05 | [diff] [blame] | 138 | stream_id_(kClientDataStreamId1), |
bnc | b07c0553 | 2015-05-14 19:07:20 | [diff] [blame] | 139 | maker_(GetParam(), connection_id_, &clock_, kDefaultServerHostName), |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 140 | random_generator_(0) { |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 141 | IPAddressNumber ip; |
| 142 | CHECK(ParseIPLiteralToNumber("192.0.2.33", &ip)); |
| 143 | peer_addr_ = IPEndPoint(ip, 443); |
| 144 | self_addr_ = IPEndPoint(ip, 8435); |
rtenneti | 4b06ae7 | 2014-08-26 03:43:43 | [diff] [blame] | 145 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(20)); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | ~QuicHttpStreamTest() { |
rtenneti | 85dcfac2 | 2015-03-27 20:22:19 | [diff] [blame] | 149 | session_->CloseSessionOnError(ERR_ABORTED, QUIC_INTERNAL_ERROR); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 150 | for (size_t i = 0; i < writes_.size(); i++) { |
| 151 | delete writes_[i].packet; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | // Adds a packet to the list of expected writes. |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 156 | void AddWrite(scoped_ptr<QuicEncryptedPacket> packet) { |
| 157 | writes_.push_back(PacketToWrite(SYNCHRONOUS, packet.release())); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | // Returns the packet to be written at position |pos|. |
rjshade | d5ced07 | 2015-12-18 19:26:02 | [diff] [blame] | 161 | QuicEncryptedPacket* GetWrite(size_t pos) { return writes_[pos].packet; } |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 162 | |
| 163 | bool AtEof() { |
rch | 37de576c | 2015-05-17 20:28:17 | [diff] [blame] | 164 | return socket_data_->AllReadDataConsumed() && |
| 165 | socket_data_->AllWriteDataConsumed(); |
[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++) { |
rjshade | d5ced07 | 2015-12-18 19:26:02 | [diff] [blame] | 176 | mock_writes_[i] = MockWrite(writes_[i].mode, writes_[i].packet->data(), |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 177 | writes_[i].packet->length()); |
| 178 | }; |
| 179 | |
rtenneti | be63573 | 2014-10-02 22:51:42 | [diff] [blame] | 180 | socket_data_.reset(new StaticSocketDataProvider( |
| 181 | nullptr, 0, mock_writes_.get(), writes_.size())); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 182 | |
rjshade | d5ced07 | 2015-12-18 19:26:02 | [diff] [blame] | 183 | MockUDPClientSocket* socket = |
| 184 | new MockUDPClientSocket(socket_data_.get(), net_log_.net_log()); |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 185 | socket->Connect(peer_addr_); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 186 | runner_ = new TestTaskRunner(&clock_); |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 187 | send_algorithm_ = new MockSendAlgorithm(); |
rch | 7dd1570 | 2015-07-01 18:57:57 | [diff] [blame] | 188 | EXPECT_CALL(*send_algorithm_, InRecovery()).WillRepeatedly(Return(false)); |
| 189 | EXPECT_CALL(*send_algorithm_, InSlowStart()).WillRepeatedly(Return(false)); |
rjshade | d5ced07 | 2015-12-18 19:26:02 | [diff] [blame] | 190 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 191 | .WillRepeatedly(Return(true)); |
rtenneti | 44f4a2e | 2015-08-07 14:00:07 | [diff] [blame] | 192 | EXPECT_CALL(*send_algorithm_, RetransmissionDelay()) |
| 193 | .WillRepeatedly(Return(QuicTime::Delta::Zero())); |
| 194 | EXPECT_CALL(*send_algorithm_, GetCongestionWindow()) |
| 195 | .WillRepeatedly(Return(kMaxPacketSize)); |
| 196 | EXPECT_CALL(*send_algorithm_, PacingRate()) |
| 197 | .WillRepeatedly(Return(QuicBandwidth::Zero())); |
| 198 | EXPECT_CALL(*send_algorithm_, TimeUntilSend(_, _, _)) |
| 199 | .WillRepeatedly(Return(QuicTime::Delta::Zero())); |
| 200 | EXPECT_CALL(*send_algorithm_, BandwidthEstimate()) |
| 201 | .WillRepeatedly(Return(QuicBandwidth::Zero())); |
rch | 1e543ec | 2015-03-29 07:04:40 | [diff] [blame] | 202 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)).Times(AnyNumber()); |
rjshade | d5ced07 | 2015-12-18 19:26:02 | [diff] [blame] | 203 | helper_.reset( |
| 204 | new QuicConnectionHelper(runner_.get(), &clock_, &random_generator_)); |
jdorfman | 90d185f3 | 2016-01-15 13:22:47 | [diff] [blame^] | 205 | connection_ = new TestQuicConnection( |
| 206 | SupportedVersions(GetParam()), connection_id_, peer_addr_, |
| 207 | helper_.get(), new QuicDefaultPacketWriter(socket)); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 208 | connection_->set_visitor(&visitor_); |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 209 | connection_->SetSendAlgorithm(send_algorithm_); |
ckrasic | 4f9d88d | 2015-07-22 22:23:16 | [diff] [blame] | 210 | session_.reset(new QuicChromiumClientSession( |
rtenneti | d39bd76 | 2015-06-12 01:05:52 | [diff] [blame] | 211 | connection_, scoped_ptr<DatagramClientSocket>(socket), |
rtenneti | 1cd3b16 | 2015-09-29 02:58:28 | [diff] [blame] | 212 | /*stream_factory=*/nullptr, &crypto_client_stream_factory_, &clock_, |
rtenneti | 041b299 | 2015-02-23 23:03:28 | [diff] [blame] | 213 | &transport_security_state_, make_scoped_ptr((QuicServerInfo*)nullptr), |
bnc | b07c0553 | 2015-05-14 19:07:20 | [diff] [blame] | 214 | QuicServerId(kDefaultServerHostName, kDefaultServerPort, |
rch | 1fe2eeb | 2015-10-26 14:45:57 | [diff] [blame] | 215 | PRIVACY_MODE_DISABLED), |
rtenneti | 1cd3b16 | 2015-09-29 02:58:28 | [diff] [blame] | 216 | kQuicYieldAfterPacketsRead, |
| 217 | QuicTime::Delta::FromMilliseconds(kQuicYieldAfterDurationMilliseconds), |
rtenneti | a75df62 | 2015-06-21 23:59:50 | [diff] [blame] | 218 | /*cert_verify_flags=*/0, DefaultQuicConfig(), &crypto_config_, |
| 219 | "CONNECTION_UNKNOWN", base::TimeTicks::Now(), |
tbansal | fdf5665b | 2015-09-21 22:46:40 | [diff] [blame] | 220 | base::ThreadTaskRunnerHandle::Get().get(), |
| 221 | /*socket_performance_watcher=*/nullptr, nullptr)); |
rtenneti | d39bd76 | 2015-06-12 01:05:52 | [diff] [blame] | 222 | session_->Initialize(); |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 223 | session_->GetCryptoStream()->CryptoConnect(); |
[email protected] | 8ba8121 | 2013-05-03 13:11:48 | [diff] [blame] | 224 | EXPECT_TRUE(session_->IsCryptoHandshakeConfirmed()); |
rjshade | d5ced07 | 2015-12-18 19:26:02 | [diff] [blame] | 225 | stream_.reset(use_closing_stream_ |
| 226 | ? new AutoClosingStream(session_->GetWeakPtr()) |
| 227 | : new QuicHttpStream(session_->GetWeakPtr())); |
[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 SetRequest(const std::string& method, |
| 231 | const std::string& path, |
| 232 | RequestPriority priority) { |
| 233 | request_headers_ = maker_.GetRequestHeaders(method, "http", path); |
[email protected] | 6cca996b | 2013-01-25 07:43:36 | [diff] [blame] | 234 | } |
| 235 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 236 | void SetResponse(const std::string& status, const std::string& body) { |
| 237 | response_headers_ = maker_.GetResponseHeaders(status); |
[email protected] | 92bf17c | 2014-03-03 21:14:03 | [diff] [blame] | 238 | response_data_ = body; |
[email protected] | 6cca996b | 2013-01-25 07:43:36 | [diff] [blame] | 239 | } |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 240 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 241 | scoped_ptr<QuicEncryptedPacket> ConstructDataPacket( |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 242 | QuicPacketNumber packet_number, |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 243 | bool should_include_version, |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 244 | bool fin, |
| 245 | QuicStreamOffset offset, |
| 246 | base::StringPiece data) { |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 247 | return maker_.MakeDataPacket(packet_number, stream_id_, |
rtenneti | f4bdb54 | 2015-01-21 14:33:05 | [diff] [blame] | 248 | should_include_version, fin, offset, data); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 249 | } |
| 250 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 251 | scoped_ptr<QuicEncryptedPacket> ConstructRequestHeadersPacket( |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 252 | QuicPacketNumber packet_number, |
rtenneti | f4bdb54 | 2015-01-21 14:33:05 | [diff] [blame] | 253 | bool fin, |
sclittle | c4dc1a3 | 2015-09-24 00:15:45 | [diff] [blame] | 254 | RequestPriority request_priority, |
| 255 | size_t* spdy_headers_frame_length) { |
ianswett | 0888cff | 2015-11-24 17:42:16 | [diff] [blame] | 256 | SpdyPriority priority = |
rtenneti | f4bdb54 | 2015-01-21 14:33:05 | [diff] [blame] | 257 | ConvertRequestPriorityToQuicPriority(request_priority); |
sclittle | c4dc1a3 | 2015-09-24 00:15:45 | [diff] [blame] | 258 | return maker_.MakeRequestHeadersPacket( |
| 259 | packet_number, stream_id_, kIncludeVersion, fin, priority, |
| 260 | request_headers_, spdy_headers_frame_length); |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | scoped_ptr<QuicEncryptedPacket> ConstructResponseHeadersPacket( |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 264 | QuicPacketNumber packet_number, |
sclittle | c4dc1a3 | 2015-09-24 00:15:45 | [diff] [blame] | 265 | bool fin, |
| 266 | size_t* spdy_headers_frame_length) { |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 267 | return maker_.MakeResponseHeadersPacket( |
sclittle | c4dc1a3 | 2015-09-24 00:15:45 | [diff] [blame] | 268 | packet_number, stream_id_, !kIncludeVersion, fin, response_headers_, |
| 269 | spdy_headers_frame_length); |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | scoped_ptr<QuicEncryptedPacket> ConstructRstStreamPacket( |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 273 | QuicPacketNumber packet_number) { |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 274 | return maker_.MakeRstPacket( |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 275 | packet_number, true, stream_id_, |
rtenneti | 4a5df26 | 2014-11-07 00:43:58 | [diff] [blame] | 276 | AdjustErrorForVersion(QUIC_RST_ACKNOWLEDGEMENT, GetParam())); |
[email protected] | 06ff515 | 2013-08-29 01:03:05 | [diff] [blame] | 277 | } |
| 278 | |
rtenneti | 6bd660b | 2015-07-18 00:19:53 | [diff] [blame] | 279 | scoped_ptr<QuicEncryptedPacket> ConstructRstStreamCancelledPacket( |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 280 | QuicPacketNumber packet_number) { |
| 281 | return maker_.MakeRstPacket(packet_number, !kIncludeVersion, stream_id_, |
rtenneti | 6bd660b | 2015-07-18 00:19:53 | [diff] [blame] | 282 | QUIC_STREAM_CANCELLED); |
| 283 | } |
| 284 | |
[email protected] | c5e1aca | 2014-01-30 04:03:04 | [diff] [blame] | 285 | scoped_ptr<QuicEncryptedPacket> ConstructAckAndRstStreamPacket( |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 286 | QuicPacketNumber packet_number) { |
| 287 | return maker_.MakeAckAndRstPacket(packet_number, !kIncludeVersion, |
| 288 | stream_id_, QUIC_STREAM_CANCELLED, 2, 1, |
| 289 | !kIncludeCongestionFeedback); |
[email protected] | c5e1aca | 2014-01-30 04:03:04 | [diff] [blame] | 290 | } |
| 291 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 292 | scoped_ptr<QuicEncryptedPacket> ConstructAckPacket( |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 293 | QuicPacketNumber packet_number, |
| 294 | QuicPacketNumber largest_received, |
| 295 | QuicPacketNumber least_unacked) { |
| 296 | return maker_.MakeAckPacket(packet_number, largest_received, least_unacked, |
| 297 | !kIncludeCongestionFeedback); |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 298 | } |
| 299 | |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 300 | BoundNetLog net_log_; |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 301 | bool use_closing_stream_; |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 302 | MockSendAlgorithm* send_algorithm_; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 303 | scoped_refptr<TestTaskRunner> runner_; |
[email protected] | 4356f0f | 2013-04-07 00:58:17 | [diff] [blame] | 304 | scoped_ptr<MockWrite[]> mock_writes_; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 305 | MockClock clock_; |
| 306 | TestQuicConnection* connection_; |
[email protected] | 2cfc6bb8 | 2013-10-27 03:40:44 | [diff] [blame] | 307 | scoped_ptr<QuicConnectionHelper> helper_; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 308 | testing::StrictMock<MockConnectionVisitor> visitor_; |
| 309 | scoped_ptr<QuicHttpStream> stream_; |
[email protected] | 5db45220 | 2014-08-19 05:22:15 | [diff] [blame] | 310 | TransportSecurityState transport_security_state_; |
ckrasic | 4f9d88d | 2015-07-22 22:23:16 | [diff] [blame] | 311 | scoped_ptr<QuicChromiumClientSession> session_; |
[email protected] | ef95114d | 2013-04-17 17:57:01 | [diff] [blame] | 312 | QuicCryptoClientConfig crypto_config_; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 313 | TestCompletionCallback callback_; |
| 314 | HttpRequestInfo request_; |
| 315 | HttpRequestHeaders headers_; |
| 316 | HttpResponseInfo response_; |
| 317 | scoped_refptr<IOBufferWithSize> read_buffer_; |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 318 | SpdyHeaderBlock request_headers_; |
| 319 | SpdyHeaderBlock response_headers_; |
[email protected] | 6cca996b | 2013-01-25 07:43:36 | [diff] [blame] | 320 | std::string request_data_; |
| 321 | std::string response_data_; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 322 | |
| 323 | private: |
[email protected] | 3aa9ca7 | 2014-02-27 19:39:43 | [diff] [blame] | 324 | const QuicConnectionId connection_id_; |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 325 | const QuicStreamId stream_id_; |
| 326 | QuicTestPacketMaker maker_; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 327 | IPEndPoint self_addr_; |
| 328 | IPEndPoint peer_addr_; |
[email protected] | 457d695 | 2013-12-13 09:24:58 | [diff] [blame] | 329 | MockRandom random_generator_; |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 330 | MockCryptoClientStreamFactory crypto_client_stream_factory_; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 331 | scoped_ptr<StaticSocketDataProvider> socket_data_; |
| 332 | std::vector<PacketToWrite> writes_; |
| 333 | }; |
| 334 | |
rjshade | d5ced07 | 2015-12-18 19:26:02 | [diff] [blame] | 335 | INSTANTIATE_TEST_CASE_P(Version, |
| 336 | QuicHttpStreamTest, |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 337 | ::testing::ValuesIn(QuicSupportedVersions())); |
| 338 | |
| 339 | TEST_P(QuicHttpStreamTest, RenewStreamForAuth) { |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 340 | Initialize(); |
rtenneti | be63573 | 2014-10-02 22:51:42 | [diff] [blame] | 341 | EXPECT_EQ(nullptr, stream_->RenewStreamForAuth()); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 342 | } |
| 343 | |
mmenke | bd84c39 | 2015-09-02 14:12:34 | [diff] [blame] | 344 | TEST_P(QuicHttpStreamTest, CanReuseConnection) { |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 345 | Initialize(); |
mmenke | bd84c39 | 2015-09-02 14:12:34 | [diff] [blame] | 346 | EXPECT_FALSE(stream_->CanReuseConnection()); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 347 | } |
| 348 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 349 | TEST_P(QuicHttpStreamTest, GetRequest) { |
| 350 | SetRequest("GET", "/", DEFAULT_PRIORITY); |
sclittle | c4dc1a3 | 2015-09-24 00:15:45 | [diff] [blame] | 351 | size_t spdy_request_header_frame_length; |
| 352 | AddWrite(ConstructRequestHeadersPacket(1, kFin, DEFAULT_PRIORITY, |
| 353 | &spdy_request_header_frame_length)); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 354 | Initialize(); |
| 355 | |
| 356 | request_.method = "GET"; |
| 357 | request_.url = GURL("http://www.google.com/"); |
| 358 | |
rjshade | d5ced07 | 2015-12-18 19:26:02 | [diff] [blame] | 359 | EXPECT_EQ(OK, stream_->InitializeStream(&request_, DEFAULT_PRIORITY, net_log_, |
| 360 | callback_.callback())); |
| 361 | EXPECT_EQ(OK, |
| 362 | stream_->SendRequest(headers_, &response_, callback_.callback())); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 363 | |
| 364 | // Ack the request. |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 365 | ProcessPacket(ConstructAckPacket(1, 0, 0)); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 366 | |
rjshade | d5ced07 | 2015-12-18 19:26:02 | [diff] [blame] | 367 | EXPECT_EQ(ERR_IO_PENDING, stream_->ReadResponseHeaders(callback_.callback())); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 368 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 369 | SetResponse("404 Not Found", std::string()); |
sclittle | c4dc1a3 | 2015-09-24 00:15:45 | [diff] [blame] | 370 | size_t spdy_response_header_frame_length; |
| 371 | ProcessPacket(ConstructResponseHeadersPacket( |
| 372 | 2, kFin, &spdy_response_header_frame_length)); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 373 | |
| 374 | // Now that the headers have been processed, the callback will return. |
| 375 | EXPECT_EQ(OK, callback_.WaitForResult()); |
[email protected] | cadac62 | 2013-06-11 16:46:36 | [diff] [blame] | 376 | ASSERT_TRUE(response_.headers.get()); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 377 | EXPECT_EQ(404, response_.headers->response_code()); |
| 378 | EXPECT_TRUE(response_.headers->HasHeaderValue("Content-Type", "text/plain")); |
[email protected] | 6ed6743 | 2014-06-24 01:53:53 | [diff] [blame] | 379 | EXPECT_FALSE(response_.response_time.is_null()); |
| 380 | EXPECT_FALSE(response_.request_time.is_null()); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 381 | |
| 382 | // There is no body, so this should return immediately. |
rjshade | d5ced07 | 2015-12-18 19:26:02 | [diff] [blame] | 383 | EXPECT_EQ(0, |
| 384 | stream_->ReadResponseBody(read_buffer_.get(), read_buffer_->size(), |
| 385 | callback_.callback())); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 386 | EXPECT_TRUE(stream_->IsResponseBodyComplete()); |
| 387 | EXPECT_TRUE(AtEof()); |
sclittle | 1edeeb2 | 2015-09-02 20:46:10 | [diff] [blame] | 388 | |
| 389 | // QuicHttpStream::GetTotalSent/ReceivedBytes currently only includes the |
sclittle | c4dc1a3 | 2015-09-24 00:15:45 | [diff] [blame] | 390 | // headers and payload. |
| 391 | EXPECT_EQ(static_cast<int64_t>(spdy_request_header_frame_length), |
| 392 | stream_->GetTotalSentBytes()); |
| 393 | EXPECT_EQ(static_cast<int64_t>(spdy_response_header_frame_length), |
| 394 | stream_->GetTotalReceivedBytes()); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 395 | } |
| 396 | |
[email protected] | 3e7dca6 | 2013-09-10 16:14:23 | [diff] [blame] | 397 | // Regression test for http://crbug.com/288128 |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 398 | TEST_P(QuicHttpStreamTest, GetRequestLargeResponse) { |
| 399 | SetRequest("GET", "/", DEFAULT_PRIORITY); |
sclittle | c4dc1a3 | 2015-09-24 00:15:45 | [diff] [blame] | 400 | size_t spdy_request_headers_frame_length; |
| 401 | AddWrite(ConstructRequestHeadersPacket(1, kFin, DEFAULT_PRIORITY, |
| 402 | &spdy_request_headers_frame_length)); |
[email protected] | 3e7dca6 | 2013-09-10 16:14:23 | [diff] [blame] | 403 | Initialize(); |
| 404 | |
| 405 | request_.method = "GET"; |
| 406 | request_.url = GURL("http://www.google.com/"); |
| 407 | |
rjshade | d5ced07 | 2015-12-18 19:26:02 | [diff] [blame] | 408 | EXPECT_EQ(OK, stream_->InitializeStream(&request_, DEFAULT_PRIORITY, net_log_, |
| 409 | callback_.callback())); |
| 410 | EXPECT_EQ(OK, |
| 411 | stream_->SendRequest(headers_, &response_, callback_.callback())); |
[email protected] | 3e7dca6 | 2013-09-10 16:14:23 | [diff] [blame] | 412 | |
| 413 | // Ack the request. |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 414 | ProcessPacket(ConstructAckPacket(1, 0, 0)); |
[email protected] | 3e7dca6 | 2013-09-10 16:14:23 | [diff] [blame] | 415 | |
rjshade | d5ced07 | 2015-12-18 19:26:02 | [diff] [blame] | 416 | EXPECT_EQ(ERR_IO_PENDING, stream_->ReadResponseHeaders(callback_.callback())); |
[email protected] | 3e7dca6 | 2013-09-10 16:14:23 | [diff] [blame] | 417 | |
| 418 | SpdyHeaderBlock headers; |
| 419 | headers[":status"] = "200 OK"; |
| 420 | headers[":version"] = "HTTP/1.1"; |
| 421 | headers["content-type"] = "text/plain"; |
rch | 7dd1570 | 2015-07-01 18:57:57 | [diff] [blame] | 422 | headers["big6"] = std::string(1000, 'x'); // Lots of x's. |
[email protected] | 3e7dca6 | 2013-09-10 16:14:23 | [diff] [blame] | 423 | |
rch | 7dd1570 | 2015-07-01 18:57:57 | [diff] [blame] | 424 | response_headers_ = headers; |
sclittle | c4dc1a3 | 2015-09-24 00:15:45 | [diff] [blame] | 425 | size_t spdy_response_headers_frame_length; |
| 426 | ProcessPacket(ConstructResponseHeadersPacket( |
| 427 | 2, kFin, &spdy_response_headers_frame_length)); |
[email protected] | 3e7dca6 | 2013-09-10 16:14:23 | [diff] [blame] | 428 | |
| 429 | // Now that the headers have been processed, the callback will return. |
| 430 | EXPECT_EQ(OK, callback_.WaitForResult()); |
| 431 | ASSERT_TRUE(response_.headers.get()); |
| 432 | EXPECT_EQ(200, response_.headers->response_code()); |
| 433 | EXPECT_TRUE(response_.headers->HasHeaderValue("Content-Type", "text/plain")); |
| 434 | |
| 435 | // There is no body, so this should return immediately. |
rjshade | d5ced07 | 2015-12-18 19:26:02 | [diff] [blame] | 436 | EXPECT_EQ(0, |
| 437 | stream_->ReadResponseBody(read_buffer_.get(), read_buffer_->size(), |
| 438 | callback_.callback())); |
[email protected] | 3e7dca6 | 2013-09-10 16:14:23 | [diff] [blame] | 439 | EXPECT_TRUE(stream_->IsResponseBodyComplete()); |
| 440 | EXPECT_TRUE(AtEof()); |
sclittle | 1edeeb2 | 2015-09-02 20:46:10 | [diff] [blame] | 441 | |
| 442 | // QuicHttpStream::GetTotalSent/ReceivedBytes currently only includes the |
sclittle | c4dc1a3 | 2015-09-24 00:15:45 | [diff] [blame] | 443 | // headers and payload. |
| 444 | EXPECT_EQ(static_cast<int64_t>(spdy_request_headers_frame_length), |
| 445 | stream_->GetTotalSentBytes()); |
| 446 | EXPECT_EQ(static_cast<int64_t>(spdy_response_headers_frame_length), |
| 447 | stream_->GetTotalReceivedBytes()); |
[email protected] | 3e7dca6 | 2013-09-10 16:14:23 | [diff] [blame] | 448 | } |
| 449 | |
rch | f9f103cbc | 2014-08-30 05:28:04 | [diff] [blame] | 450 | // Regression test for http://crbug.com/409101 |
| 451 | TEST_P(QuicHttpStreamTest, SessionClosedBeforeSendRequest) { |
| 452 | SetRequest("GET", "/", DEFAULT_PRIORITY); |
| 453 | Initialize(); |
| 454 | |
| 455 | request_.method = "GET"; |
| 456 | request_.url = GURL("http://www.google.com/"); |
| 457 | |
rjshade | d5ced07 | 2015-12-18 19:26:02 | [diff] [blame] | 458 | EXPECT_EQ(OK, stream_->InitializeStream(&request_, DEFAULT_PRIORITY, net_log_, |
| 459 | callback_.callback())); |
rch | f9f103cbc | 2014-08-30 05:28:04 | [diff] [blame] | 460 | |
| 461 | session_->connection()->CloseConnection(QUIC_NO_ERROR, true); |
| 462 | |
| 463 | EXPECT_EQ(ERR_CONNECTION_CLOSED, |
rjshade | d5ced07 | 2015-12-18 19:26:02 | [diff] [blame] | 464 | stream_->SendRequest(headers_, &response_, callback_.callback())); |
sclittle | 1edeeb2 | 2015-09-02 20:46:10 | [diff] [blame] | 465 | |
| 466 | EXPECT_EQ(0, stream_->GetTotalSentBytes()); |
| 467 | EXPECT_EQ(0, stream_->GetTotalReceivedBytes()); |
rch | f9f103cbc | 2014-08-30 05:28:04 | [diff] [blame] | 468 | } |
| 469 | |
zhongyi | ca364fbb | 2015-12-12 03:39:12 | [diff] [blame] | 470 | TEST_P(QuicHttpStreamTest, LogGranularQuicConnectionError) { |
| 471 | SetRequest("GET", "/", DEFAULT_PRIORITY); |
| 472 | size_t spdy_request_headers_frame_length; |
| 473 | AddWrite(ConstructRequestHeadersPacket(1, kFin, DEFAULT_PRIORITY, |
| 474 | &spdy_request_headers_frame_length)); |
| 475 | AddWrite(ConstructAckAndRstStreamPacket(2)); |
| 476 | use_closing_stream_ = true; |
| 477 | Initialize(); |
| 478 | |
| 479 | request_.method = "GET"; |
| 480 | request_.url = GURL("http://www.google.com/"); |
| 481 | |
| 482 | EXPECT_EQ(OK, stream_->InitializeStream(&request_, DEFAULT_PRIORITY, net_log_, |
| 483 | callback_.callback())); |
| 484 | EXPECT_EQ(OK, |
| 485 | stream_->SendRequest(headers_, &response_, callback_.callback())); |
| 486 | |
| 487 | // Ack the request. |
| 488 | ProcessPacket(ConstructAckPacket(1, 0, 0)); |
| 489 | EXPECT_EQ(ERR_IO_PENDING, stream_->ReadResponseHeaders(callback_.callback())); |
| 490 | |
| 491 | EXPECT_TRUE(QuicHttpStreamPeer::WasHandshakeConfirmed(stream_.get())); |
| 492 | stream_->OnClose(QUIC_PEER_GOING_AWAY); |
| 493 | |
| 494 | NetErrorDetails details; |
| 495 | EXPECT_EQ(QUIC_NO_ERROR, details.quic_connection_error); |
| 496 | stream_->PopulateNetErrorDetails(&details); |
| 497 | EXPECT_EQ(QUIC_PEER_GOING_AWAY, details.quic_connection_error); |
| 498 | } |
| 499 | |
| 500 | TEST_P(QuicHttpStreamTest, DoNotLogGranularQuicErrorIfHandshakeNotConfirmed) { |
| 501 | SetRequest("GET", "/", DEFAULT_PRIORITY); |
| 502 | size_t spdy_request_headers_frame_length; |
| 503 | AddWrite(ConstructRequestHeadersPacket(1, kFin, DEFAULT_PRIORITY, |
| 504 | &spdy_request_headers_frame_length)); |
| 505 | AddWrite(ConstructAckAndRstStreamPacket(2)); |
| 506 | use_closing_stream_ = true; |
| 507 | Initialize(); |
| 508 | |
| 509 | request_.method = "GET"; |
| 510 | request_.url = GURL("http://www.google.com/"); |
| 511 | |
| 512 | EXPECT_EQ(OK, stream_->InitializeStream(&request_, DEFAULT_PRIORITY, net_log_, |
| 513 | callback_.callback())); |
| 514 | EXPECT_EQ(OK, |
| 515 | stream_->SendRequest(headers_, &response_, callback_.callback())); |
| 516 | |
| 517 | // Ack the request. |
| 518 | ProcessPacket(ConstructAckPacket(1, 0, 0)); |
| 519 | EXPECT_EQ(ERR_IO_PENDING, stream_->ReadResponseHeaders(callback_.callback())); |
| 520 | |
| 521 | // The test setup defaults handshake to be confirmed. Manually set |
| 522 | // it to be not confirmed. |
| 523 | // Granular errors shouldn't be reported if handshake not confirmed. |
| 524 | QuicHttpStreamPeer::SetHandshakeConfirmed(stream_.get(), false); |
| 525 | |
| 526 | EXPECT_FALSE(QuicHttpStreamPeer::WasHandshakeConfirmed(stream_.get())); |
| 527 | stream_->OnClose(QUIC_PEER_GOING_AWAY); |
| 528 | |
| 529 | NetErrorDetails details; |
| 530 | EXPECT_EQ(QUIC_NO_ERROR, details.quic_connection_error); |
| 531 | stream_->PopulateNetErrorDetails(&details); |
| 532 | EXPECT_EQ(QUIC_NO_ERROR, details.quic_connection_error); |
| 533 | } |
| 534 | |
rch | 11a114a | 2014-09-04 23:41:59 | [diff] [blame] | 535 | // Regression test for http://crbug.com/409871 |
| 536 | TEST_P(QuicHttpStreamTest, SessionClosedBeforeReadResponseHeaders) { |
| 537 | SetRequest("GET", "/", DEFAULT_PRIORITY); |
sclittle | c4dc1a3 | 2015-09-24 00:15:45 | [diff] [blame] | 538 | size_t spdy_request_headers_frame_length; |
| 539 | AddWrite(ConstructRequestHeadersPacket(1, kFin, DEFAULT_PRIORITY, |
| 540 | &spdy_request_headers_frame_length)); |
rch | 11a114a | 2014-09-04 23:41:59 | [diff] [blame] | 541 | Initialize(); |
| 542 | |
| 543 | request_.method = "GET"; |
| 544 | request_.url = GURL("http://www.google.com/"); |
| 545 | |
rjshade | d5ced07 | 2015-12-18 19:26:02 | [diff] [blame] | 546 | EXPECT_EQ(OK, stream_->InitializeStream(&request_, DEFAULT_PRIORITY, net_log_, |
| 547 | callback_.callback())); |
rch | 11a114a | 2014-09-04 23:41:59 | [diff] [blame] | 548 | |
rjshade | d5ced07 | 2015-12-18 19:26:02 | [diff] [blame] | 549 | EXPECT_EQ(OK, |
| 550 | stream_->SendRequest(headers_, &response_, callback_.callback())); |
rch | 11a114a | 2014-09-04 23:41:59 | [diff] [blame] | 551 | |
| 552 | session_->connection()->CloseConnection(QUIC_NO_ERROR, true); |
| 553 | |
| 554 | EXPECT_NE(OK, stream_->ReadResponseHeaders(callback_.callback())); |
sclittle | 1edeeb2 | 2015-09-02 20:46:10 | [diff] [blame] | 555 | |
| 556 | // QuicHttpStream::GetTotalSent/ReceivedBytes currently only includes the |
sclittle | c4dc1a3 | 2015-09-24 00:15:45 | [diff] [blame] | 557 | // headers and payload. |
| 558 | EXPECT_EQ(static_cast<int64_t>(spdy_request_headers_frame_length), |
| 559 | stream_->GetTotalSentBytes()); |
sclittle | 1edeeb2 | 2015-09-02 20:46:10 | [diff] [blame] | 560 | EXPECT_EQ(0, stream_->GetTotalReceivedBytes()); |
rch | 11a114a | 2014-09-04 23:41:59 | [diff] [blame] | 561 | } |
| 562 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 563 | TEST_P(QuicHttpStreamTest, SendPostRequest) { |
| 564 | SetRequest("POST", "/", DEFAULT_PRIORITY); |
sclittle | c4dc1a3 | 2015-09-24 00:15:45 | [diff] [blame] | 565 | size_t spdy_request_headers_frame_length; |
| 566 | AddWrite(ConstructRequestHeadersPacket(1, !kFin, DEFAULT_PRIORITY, |
| 567 | &spdy_request_headers_frame_length)); |
[email protected] | 92bf17c | 2014-03-03 21:14:03 | [diff] [blame] | 568 | AddWrite(ConstructDataPacket(2, kIncludeVersion, kFin, 0, kUploadData)); |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 569 | AddWrite(ConstructAckPacket(3, 3, 1)); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 570 | |
| 571 | Initialize(); |
| 572 | |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 573 | std::vector<scoped_ptr<UploadElementReader>> element_readers; |
| 574 | element_readers.push_back(make_scoped_ptr( |
| 575 | new UploadBytesElementReader(kUploadData, strlen(kUploadData)))); |
| 576 | ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 577 | request_.method = "POST"; |
| 578 | request_.url = GURL("http://www.google.com/"); |
| 579 | request_.upload_data_stream = &upload_data_stream; |
[email protected] | 4db27d8 | 2012-12-20 11:50:24 | [diff] [blame] | 580 | ASSERT_EQ(OK, request_.upload_data_stream->Init(CompletionCallback())); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 581 | |
rjshade | d5ced07 | 2015-12-18 19:26:02 | [diff] [blame] | 582 | EXPECT_EQ(OK, stream_->InitializeStream(&request_, DEFAULT_PRIORITY, net_log_, |
| 583 | callback_.callback())); |
| 584 | EXPECT_EQ(OK, |
| 585 | stream_->SendRequest(headers_, &response_, callback_.callback())); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 586 | |
| 587 | // Ack both packets in the request. |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 588 | ProcessPacket(ConstructAckPacket(1, 0, 0)); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 589 | |
| 590 | // Send the response headers (but not the body). |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 591 | SetResponse("200 OK", std::string()); |
sclittle | c4dc1a3 | 2015-09-24 00:15:45 | [diff] [blame] | 592 | size_t spdy_response_headers_frame_length; |
| 593 | ProcessPacket(ConstructResponseHeadersPacket( |
| 594 | 2, !kFin, &spdy_response_headers_frame_length)); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 595 | |
rch | b27683c | 2015-07-29 23:53:50 | [diff] [blame] | 596 | // The headers have arrived, but they are delivered asynchronously. |
| 597 | EXPECT_EQ(ERR_IO_PENDING, stream_->ReadResponseHeaders(callback_.callback())); |
| 598 | EXPECT_EQ(OK, callback_.WaitForResult()); |
[email protected] | cadac62 | 2013-06-11 16:46:36 | [diff] [blame] | 599 | ASSERT_TRUE(response_.headers.get()); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 600 | EXPECT_EQ(200, response_.headers->response_code()); |
| 601 | EXPECT_TRUE(response_.headers->HasHeaderValue("Content-Type", "text/plain")); |
| 602 | |
| 603 | // Send the response body. |
| 604 | const char kResponseBody[] = "Hello world!"; |
[email protected] | 92bf17c | 2014-03-03 21:14:03 | [diff] [blame] | 605 | ProcessPacket(ConstructDataPacket(3, false, kFin, 0, kResponseBody)); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 606 | // Since the body has already arrived, this should return immediately. |
| 607 | EXPECT_EQ(static_cast<int>(strlen(kResponseBody)), |
| 608 | stream_->ReadResponseBody(read_buffer_.get(), read_buffer_->size(), |
| 609 | callback_.callback())); |
| 610 | |
| 611 | EXPECT_TRUE(stream_->IsResponseBodyComplete()); |
| 612 | EXPECT_TRUE(AtEof()); |
sclittle | 1edeeb2 | 2015-09-02 20:46:10 | [diff] [blame] | 613 | |
| 614 | // QuicHttpStream::GetTotalSent/ReceivedBytes currently only includes the |
sclittle | c4dc1a3 | 2015-09-24 00:15:45 | [diff] [blame] | 615 | // headers and payload. |
| 616 | EXPECT_EQ(static_cast<int64_t>(spdy_request_headers_frame_length + |
| 617 | strlen(kUploadData)), |
sclittle | 1edeeb2 | 2015-09-02 20:46:10 | [diff] [blame] | 618 | stream_->GetTotalSentBytes()); |
sclittle | c4dc1a3 | 2015-09-24 00:15:45 | [diff] [blame] | 619 | EXPECT_EQ(static_cast<int64_t>(spdy_response_headers_frame_length + |
| 620 | strlen(kResponseBody)), |
sclittle | 1edeeb2 | 2015-09-02 20:46:10 | [diff] [blame] | 621 | stream_->GetTotalReceivedBytes()); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 622 | } |
| 623 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 624 | TEST_P(QuicHttpStreamTest, SendChunkedPostRequest) { |
| 625 | SetRequest("POST", "/", DEFAULT_PRIORITY); |
[email protected] | c9e49a0 | 2013-02-26 05:56:47 | [diff] [blame] | 626 | size_t chunk_size = strlen(kUploadData); |
sclittle | c4dc1a3 | 2015-09-24 00:15:45 | [diff] [blame] | 627 | size_t spdy_request_headers_frame_length; |
| 628 | AddWrite(ConstructRequestHeadersPacket(1, !kFin, DEFAULT_PRIORITY, |
| 629 | &spdy_request_headers_frame_length)); |
[email protected] | 92bf17c | 2014-03-03 21:14:03 | [diff] [blame] | 630 | AddWrite(ConstructDataPacket(2, kIncludeVersion, !kFin, 0, kUploadData)); |
rjshade | d5ced07 | 2015-12-18 19:26:02 | [diff] [blame] | 631 | AddWrite( |
| 632 | ConstructDataPacket(3, kIncludeVersion, kFin, chunk_size, kUploadData)); |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 633 | AddWrite(ConstructAckPacket(4, 3, 1)); |
[email protected] | c9e49a0 | 2013-02-26 05:56:47 | [diff] [blame] | 634 | Initialize(); |
| 635 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 636 | ChunkedUploadDataStream upload_data_stream(0); |
| 637 | upload_data_stream.AppendData(kUploadData, chunk_size, false); |
[email protected] | c9e49a0 | 2013-02-26 05:56:47 | [diff] [blame] | 638 | |
| 639 | request_.method = "POST"; |
| 640 | request_.url = GURL("http://www.google.com/"); |
| 641 | request_.upload_data_stream = &upload_data_stream; |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 642 | ASSERT_EQ(OK, request_.upload_data_stream->Init( |
rjshade | d5ced07 | 2015-12-18 19:26:02 | [diff] [blame] | 643 | TestCompletionCallback().callback())); |
[email protected] | c9e49a0 | 2013-02-26 05:56:47 | [diff] [blame] | 644 | |
rjshade | d5ced07 | 2015-12-18 19:26:02 | [diff] [blame] | 645 | ASSERT_EQ(OK, stream_->InitializeStream(&request_, DEFAULT_PRIORITY, net_log_, |
| 646 | callback_.callback())); |
| 647 | ASSERT_EQ(ERR_IO_PENDING, |
| 648 | stream_->SendRequest(headers_, &response_, callback_.callback())); |
[email protected] | c9e49a0 | 2013-02-26 05:56:47 | [diff] [blame] | 649 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 650 | upload_data_stream.AppendData(kUploadData, chunk_size, true); |
rch | b27683c | 2015-07-29 23:53:50 | [diff] [blame] | 651 | EXPECT_EQ(OK, callback_.WaitForResult()); |
[email protected] | c9e49a0 | 2013-02-26 05:56:47 | [diff] [blame] | 652 | |
| 653 | // Ack both packets in the request. |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 654 | ProcessPacket(ConstructAckPacket(1, 0, 0)); |
[email protected] | c9e49a0 | 2013-02-26 05:56:47 | [diff] [blame] | 655 | |
| 656 | // Send the response headers (but not the body). |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 657 | SetResponse("200 OK", std::string()); |
sclittle | c4dc1a3 | 2015-09-24 00:15:45 | [diff] [blame] | 658 | size_t spdy_response_headers_frame_length; |
| 659 | ProcessPacket(ConstructResponseHeadersPacket( |
| 660 | 2, !kFin, &spdy_response_headers_frame_length)); |
[email protected] | c9e49a0 | 2013-02-26 05:56:47 | [diff] [blame] | 661 | |
rch | b27683c | 2015-07-29 23:53:50 | [diff] [blame] | 662 | // The headers have arrived, but they are delivered asynchronously |
| 663 | EXPECT_EQ(ERR_IO_PENDING, stream_->ReadResponseHeaders(callback_.callback())); |
| 664 | EXPECT_EQ(OK, callback_.WaitForResult()); |
[email protected] | cadac62 | 2013-06-11 16:46:36 | [diff] [blame] | 665 | ASSERT_TRUE(response_.headers.get()); |
[email protected] | c9e49a0 | 2013-02-26 05:56:47 | [diff] [blame] | 666 | EXPECT_EQ(200, response_.headers->response_code()); |
| 667 | EXPECT_TRUE(response_.headers->HasHeaderValue("Content-Type", "text/plain")); |
| 668 | |
| 669 | // Send the response body. |
| 670 | const char kResponseBody[] = "Hello world!"; |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 671 | ProcessPacket(ConstructDataPacket(3, false, kFin, response_data_.length(), |
| 672 | kResponseBody)); |
[email protected] | c9e49a0 | 2013-02-26 05:56:47 | [diff] [blame] | 673 | |
| 674 | // Since the body has already arrived, this should return immediately. |
| 675 | ASSERT_EQ(static_cast<int>(strlen(kResponseBody)), |
| 676 | stream_->ReadResponseBody(read_buffer_.get(), read_buffer_->size(), |
| 677 | callback_.callback())); |
| 678 | |
| 679 | EXPECT_TRUE(stream_->IsResponseBodyComplete()); |
| 680 | EXPECT_TRUE(AtEof()); |
sclittle | 1edeeb2 | 2015-09-02 20:46:10 | [diff] [blame] | 681 | |
| 682 | // QuicHttpStream::GetTotalSent/ReceivedBytes currently only includes the |
sclittle | c4dc1a3 | 2015-09-24 00:15:45 | [diff] [blame] | 683 | // headers and payload. |
| 684 | EXPECT_EQ(static_cast<int64_t>(spdy_request_headers_frame_length + |
| 685 | strlen(kUploadData) * 2), |
sclittle | 1edeeb2 | 2015-09-02 20:46:10 | [diff] [blame] | 686 | stream_->GetTotalSentBytes()); |
sclittle | c4dc1a3 | 2015-09-24 00:15:45 | [diff] [blame] | 687 | EXPECT_EQ(static_cast<int64_t>(spdy_response_headers_frame_length + |
| 688 | strlen(kResponseBody)), |
sclittle | 1edeeb2 | 2015-09-02 20:46:10 | [diff] [blame] | 689 | stream_->GetTotalReceivedBytes()); |
[email protected] | c9e49a0 | 2013-02-26 05:56:47 | [diff] [blame] | 690 | } |
| 691 | |
[email protected] | 16ba774 | 2014-08-22 00:57:25 | [diff] [blame] | 692 | TEST_P(QuicHttpStreamTest, SendChunkedPostRequestWithFinalEmptyDataPacket) { |
| 693 | SetRequest("POST", "/", DEFAULT_PRIORITY); |
| 694 | size_t chunk_size = strlen(kUploadData); |
sclittle | c4dc1a3 | 2015-09-24 00:15:45 | [diff] [blame] | 695 | size_t spdy_request_headers_frame_length; |
| 696 | AddWrite(ConstructRequestHeadersPacket(1, !kFin, DEFAULT_PRIORITY, |
| 697 | &spdy_request_headers_frame_length)); |
[email protected] | 16ba774 | 2014-08-22 00:57:25 | [diff] [blame] | 698 | AddWrite(ConstructDataPacket(2, kIncludeVersion, !kFin, 0, kUploadData)); |
| 699 | AddWrite(ConstructDataPacket(3, kIncludeVersion, kFin, chunk_size, "")); |
| 700 | AddWrite(ConstructAckPacket(4, 3, 1)); |
| 701 | Initialize(); |
| 702 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 703 | ChunkedUploadDataStream upload_data_stream(0); |
| 704 | upload_data_stream.AppendData(kUploadData, chunk_size, false); |
[email protected] | 16ba774 | 2014-08-22 00:57:25 | [diff] [blame] | 705 | |
| 706 | request_.method = "POST"; |
| 707 | request_.url = GURL("http://www.google.com/"); |
| 708 | request_.upload_data_stream = &upload_data_stream; |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 709 | ASSERT_EQ(OK, request_.upload_data_stream->Init( |
rjshade | d5ced07 | 2015-12-18 19:26:02 | [diff] [blame] | 710 | TestCompletionCallback().callback())); |
[email protected] | 16ba774 | 2014-08-22 00:57:25 | [diff] [blame] | 711 | |
rjshade | d5ced07 | 2015-12-18 19:26:02 | [diff] [blame] | 712 | ASSERT_EQ(OK, stream_->InitializeStream(&request_, DEFAULT_PRIORITY, net_log_, |
| 713 | callback_.callback())); |
| 714 | ASSERT_EQ(ERR_IO_PENDING, |
| 715 | stream_->SendRequest(headers_, &response_, callback_.callback())); |
[email protected] | 16ba774 | 2014-08-22 00:57:25 | [diff] [blame] | 716 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 717 | upload_data_stream.AppendData(nullptr, 0, true); |
rch | b27683c | 2015-07-29 23:53:50 | [diff] [blame] | 718 | EXPECT_EQ(OK, callback_.WaitForResult()); |
[email protected] | 16ba774 | 2014-08-22 00:57:25 | [diff] [blame] | 719 | |
| 720 | ProcessPacket(ConstructAckPacket(1, 0, 0)); |
| 721 | |
| 722 | // Send the response headers (but not the body). |
| 723 | SetResponse("200 OK", std::string()); |
sclittle | c4dc1a3 | 2015-09-24 00:15:45 | [diff] [blame] | 724 | size_t spdy_response_headers_frame_length; |
| 725 | ProcessPacket(ConstructResponseHeadersPacket( |
| 726 | 2, !kFin, &spdy_response_headers_frame_length)); |
[email protected] | 16ba774 | 2014-08-22 00:57:25 | [diff] [blame] | 727 | |
rch | b27683c | 2015-07-29 23:53:50 | [diff] [blame] | 728 | // The headers have arrived, but they are delivered asynchronously |
| 729 | EXPECT_EQ(ERR_IO_PENDING, stream_->ReadResponseHeaders(callback_.callback())); |
| 730 | EXPECT_EQ(OK, callback_.WaitForResult()); |
[email protected] | 16ba774 | 2014-08-22 00:57:25 | [diff] [blame] | 731 | ASSERT_TRUE(response_.headers.get()); |
| 732 | EXPECT_EQ(200, response_.headers->response_code()); |
| 733 | EXPECT_TRUE(response_.headers->HasHeaderValue("Content-Type", "text/plain")); |
| 734 | |
| 735 | // Send the response body. |
| 736 | const char kResponseBody[] = "Hello world!"; |
| 737 | ProcessPacket(ConstructDataPacket(3, false, kFin, response_data_.length(), |
| 738 | kResponseBody)); |
| 739 | |
rch | b27683c | 2015-07-29 23:53:50 | [diff] [blame] | 740 | // The body has arrived, but it is delivered asynchronously |
[email protected] | 16ba774 | 2014-08-22 00:57:25 | [diff] [blame] | 741 | ASSERT_EQ(static_cast<int>(strlen(kResponseBody)), |
| 742 | stream_->ReadResponseBody(read_buffer_.get(), read_buffer_->size(), |
| 743 | callback_.callback())); |
[email protected] | 16ba774 | 2014-08-22 00:57:25 | [diff] [blame] | 744 | EXPECT_TRUE(stream_->IsResponseBodyComplete()); |
| 745 | EXPECT_TRUE(AtEof()); |
sclittle | 1edeeb2 | 2015-09-02 20:46:10 | [diff] [blame] | 746 | |
| 747 | // QuicHttpStream::GetTotalSent/ReceivedBytes currently only includes the |
sclittle | c4dc1a3 | 2015-09-24 00:15:45 | [diff] [blame] | 748 | // headers and payload. |
| 749 | EXPECT_EQ(static_cast<int64_t>(spdy_request_headers_frame_length + |
| 750 | strlen(kUploadData)), |
sclittle | 1edeeb2 | 2015-09-02 20:46:10 | [diff] [blame] | 751 | stream_->GetTotalSentBytes()); |
sclittle | c4dc1a3 | 2015-09-24 00:15:45 | [diff] [blame] | 752 | EXPECT_EQ(static_cast<int64_t>(spdy_response_headers_frame_length + |
| 753 | strlen(kResponseBody)), |
sclittle | 1edeeb2 | 2015-09-02 20:46:10 | [diff] [blame] | 754 | stream_->GetTotalReceivedBytes()); |
[email protected] | 16ba774 | 2014-08-22 00:57:25 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | TEST_P(QuicHttpStreamTest, SendChunkedPostRequestWithOneEmptyDataPacket) { |
| 758 | SetRequest("POST", "/", DEFAULT_PRIORITY); |
sclittle | c4dc1a3 | 2015-09-24 00:15:45 | [diff] [blame] | 759 | size_t spdy_request_headers_frame_length; |
| 760 | AddWrite(ConstructRequestHeadersPacket(1, !kFin, DEFAULT_PRIORITY, |
| 761 | &spdy_request_headers_frame_length)); |
[email protected] | 16ba774 | 2014-08-22 00:57:25 | [diff] [blame] | 762 | AddWrite(ConstructDataPacket(2, kIncludeVersion, kFin, 0, "")); |
| 763 | AddWrite(ConstructAckPacket(3, 3, 1)); |
| 764 | Initialize(); |
| 765 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 766 | ChunkedUploadDataStream upload_data_stream(0); |
[email protected] | 16ba774 | 2014-08-22 00:57:25 | [diff] [blame] | 767 | |
| 768 | request_.method = "POST"; |
| 769 | request_.url = GURL("http://www.google.com/"); |
| 770 | request_.upload_data_stream = &upload_data_stream; |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 771 | ASSERT_EQ(OK, request_.upload_data_stream->Init( |
rjshade | d5ced07 | 2015-12-18 19:26:02 | [diff] [blame] | 772 | TestCompletionCallback().callback())); |
[email protected] | 16ba774 | 2014-08-22 00:57:25 | [diff] [blame] | 773 | |
rjshade | d5ced07 | 2015-12-18 19:26:02 | [diff] [blame] | 774 | ASSERT_EQ(OK, stream_->InitializeStream(&request_, DEFAULT_PRIORITY, net_log_, |
| 775 | callback_.callback())); |
| 776 | ASSERT_EQ(ERR_IO_PENDING, |
| 777 | stream_->SendRequest(headers_, &response_, callback_.callback())); |
[email protected] | 16ba774 | 2014-08-22 00:57:25 | [diff] [blame] | 778 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 779 | upload_data_stream.AppendData(nullptr, 0, true); |
rch | b27683c | 2015-07-29 23:53:50 | [diff] [blame] | 780 | EXPECT_EQ(OK, callback_.WaitForResult()); |
[email protected] | 16ba774 | 2014-08-22 00:57:25 | [diff] [blame] | 781 | |
| 782 | ProcessPacket(ConstructAckPacket(1, 0, 0)); |
| 783 | |
| 784 | // Send the response headers (but not the body). |
| 785 | SetResponse("200 OK", std::string()); |
sclittle | c4dc1a3 | 2015-09-24 00:15:45 | [diff] [blame] | 786 | size_t spdy_response_headers_frame_length; |
| 787 | ProcessPacket(ConstructResponseHeadersPacket( |
| 788 | 2, !kFin, &spdy_response_headers_frame_length)); |
[email protected] | 16ba774 | 2014-08-22 00:57:25 | [diff] [blame] | 789 | |
rch | b27683c | 2015-07-29 23:53:50 | [diff] [blame] | 790 | // The headers have arrived, but they are delivered asynchronously |
| 791 | EXPECT_EQ(ERR_IO_PENDING, stream_->ReadResponseHeaders(callback_.callback())); |
| 792 | EXPECT_EQ(OK, callback_.WaitForResult()); |
[email protected] | 16ba774 | 2014-08-22 00:57:25 | [diff] [blame] | 793 | ASSERT_TRUE(response_.headers.get()); |
| 794 | EXPECT_EQ(200, response_.headers->response_code()); |
| 795 | EXPECT_TRUE(response_.headers->HasHeaderValue("Content-Type", "text/plain")); |
| 796 | |
| 797 | // Send the response body. |
| 798 | const char kResponseBody[] = "Hello world!"; |
| 799 | ProcessPacket(ConstructDataPacket(3, false, kFin, response_data_.length(), |
| 800 | kResponseBody)); |
| 801 | |
rch | b27683c | 2015-07-29 23:53:50 | [diff] [blame] | 802 | // The body has arrived, but it is delivered asynchronously |
[email protected] | 16ba774 | 2014-08-22 00:57:25 | [diff] [blame] | 803 | ASSERT_EQ(static_cast<int>(strlen(kResponseBody)), |
| 804 | stream_->ReadResponseBody(read_buffer_.get(), read_buffer_->size(), |
| 805 | callback_.callback())); |
| 806 | |
| 807 | EXPECT_TRUE(stream_->IsResponseBodyComplete()); |
| 808 | EXPECT_TRUE(AtEof()); |
sclittle | 1edeeb2 | 2015-09-02 20:46:10 | [diff] [blame] | 809 | |
| 810 | // QuicHttpStream::GetTotalSent/ReceivedBytes currently only includes the |
sclittle | c4dc1a3 | 2015-09-24 00:15:45 | [diff] [blame] | 811 | // headers and payload. |
| 812 | EXPECT_EQ(static_cast<int64_t>(spdy_request_headers_frame_length), |
| 813 | stream_->GetTotalSentBytes()); |
| 814 | EXPECT_EQ(static_cast<int64_t>(spdy_response_headers_frame_length + |
| 815 | strlen(kResponseBody)), |
sclittle | 1edeeb2 | 2015-09-02 20:46:10 | [diff] [blame] | 816 | stream_->GetTotalReceivedBytes()); |
[email protected] | 16ba774 | 2014-08-22 00:57:25 | [diff] [blame] | 817 | } |
| 818 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 819 | TEST_P(QuicHttpStreamTest, DestroyedEarly) { |
| 820 | SetRequest("GET", "/", DEFAULT_PRIORITY); |
sclittle | c4dc1a3 | 2015-09-24 00:15:45 | [diff] [blame] | 821 | size_t spdy_request_headers_frame_length; |
| 822 | AddWrite(ConstructRequestHeadersPacket(1, kFin, DEFAULT_PRIORITY, |
| 823 | &spdy_request_headers_frame_length)); |
rtenneti | 4efd55dd | 2015-09-18 01:12:04 | [diff] [blame] | 824 | AddWrite(ConstructAckAndRstStreamPacket(2)); |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 825 | use_closing_stream_ = true; |
| 826 | Initialize(); |
| 827 | |
| 828 | request_.method = "GET"; |
| 829 | request_.url = GURL("http://www.google.com/"); |
| 830 | |
rjshade | d5ced07 | 2015-12-18 19:26:02 | [diff] [blame] | 831 | EXPECT_EQ(OK, stream_->InitializeStream(&request_, DEFAULT_PRIORITY, net_log_, |
| 832 | callback_.callback())); |
| 833 | EXPECT_EQ(OK, |
| 834 | stream_->SendRequest(headers_, &response_, callback_.callback())); |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 835 | |
| 836 | // Ack the request. |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 837 | ProcessPacket(ConstructAckPacket(1, 0, 0)); |
rjshade | d5ced07 | 2015-12-18 19:26:02 | [diff] [blame] | 838 | EXPECT_EQ(ERR_IO_PENDING, stream_->ReadResponseHeaders(callback_.callback())); |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 839 | |
| 840 | // Send the response with a body. |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 841 | SetResponse("404 OK", "hello world!"); |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 842 | // In the course of processing this packet, the QuicHttpStream close itself. |
sclittle | c4dc1a3 | 2015-09-24 00:15:45 | [diff] [blame] | 843 | ProcessPacket(ConstructResponseHeadersPacket(2, kFin, nullptr)); |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 844 | |
rch | b27683c | 2015-07-29 23:53:50 | [diff] [blame] | 845 | base::MessageLoop::current()->RunUntilIdle(); |
| 846 | |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 847 | EXPECT_TRUE(AtEof()); |
sclittle | 1edeeb2 | 2015-09-02 20:46:10 | [diff] [blame] | 848 | |
| 849 | // QuicHttpStream::GetTotalSent/ReceivedBytes currently only includes the |
sclittle | c4dc1a3 | 2015-09-24 00:15:45 | [diff] [blame] | 850 | // headers and payload. |
| 851 | EXPECT_EQ(static_cast<int64_t>(spdy_request_headers_frame_length), |
| 852 | stream_->GetTotalSentBytes()); |
| 853 | // Zero since the stream is closed before processing the headers. |
sclittle | 1edeeb2 | 2015-09-02 20:46:10 | [diff] [blame] | 854 | EXPECT_EQ(0, stream_->GetTotalReceivedBytes()); |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 855 | } |
| 856 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 857 | TEST_P(QuicHttpStreamTest, Priority) { |
| 858 | SetRequest("GET", "/", MEDIUM); |
sclittle | c4dc1a3 | 2015-09-24 00:15:45 | [diff] [blame] | 859 | size_t spdy_request_headers_frame_length; |
| 860 | AddWrite(ConstructRequestHeadersPacket(1, kFin, MEDIUM, |
| 861 | &spdy_request_headers_frame_length)); |
rtenneti | 4efd55dd | 2015-09-18 01:12:04 | [diff] [blame] | 862 | AddWrite(ConstructAckAndRstStreamPacket(2)); |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 863 | use_closing_stream_ = true; |
| 864 | Initialize(); |
| 865 | |
| 866 | request_.method = "GET"; |
| 867 | request_.url = GURL("http://www.google.com/"); |
| 868 | |
rjshade | d5ced07 | 2015-12-18 19:26:02 | [diff] [blame] | 869 | EXPECT_EQ(OK, stream_->InitializeStream(&request_, MEDIUM, net_log_, |
| 870 | callback_.callback())); |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 871 | |
| 872 | // Check that priority is highest. |
| 873 | QuicReliableClientStream* reliable_stream = |
| 874 | QuicHttpStreamPeer::GetQuicReliableClientStream(stream_.get()); |
| 875 | DCHECK(reliable_stream); |
zhongyi | b867702 | 2015-12-01 05:51:30 | [diff] [blame] | 876 | DCHECK_EQ(kV3HighestPriority, reliable_stream->Priority()); |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 877 | |
rjshade | d5ced07 | 2015-12-18 19:26:02 | [diff] [blame] | 878 | EXPECT_EQ(OK, |
| 879 | stream_->SendRequest(headers_, &response_, callback_.callback())); |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 880 | |
| 881 | // Check that priority has now dropped back to MEDIUM. |
ianswett | 0888cff | 2015-11-24 17:42:16 | [diff] [blame] | 882 | DCHECK_EQ(MEDIUM, |
| 883 | ConvertQuicPriorityToRequestPriority(reliable_stream->Priority())); |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 884 | |
| 885 | // Ack the request. |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 886 | ProcessPacket(ConstructAckPacket(1, 0, 0)); |
rjshade | d5ced07 | 2015-12-18 19:26:02 | [diff] [blame] | 887 | EXPECT_EQ(ERR_IO_PENDING, stream_->ReadResponseHeaders(callback_.callback())); |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 888 | |
| 889 | // Send the response with a body. |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 890 | SetResponse("404 OK", "hello world!"); |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 891 | // In the course of processing this packet, the QuicHttpStream close itself. |
sclittle | c4dc1a3 | 2015-09-24 00:15:45 | [diff] [blame] | 892 | ProcessPacket(ConstructResponseHeadersPacket(2, kFin, nullptr)); |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 893 | |
rch | b27683c | 2015-07-29 23:53:50 | [diff] [blame] | 894 | base::MessageLoop::current()->RunUntilIdle(); |
| 895 | |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 896 | EXPECT_TRUE(AtEof()); |
sclittle | 1edeeb2 | 2015-09-02 20:46:10 | [diff] [blame] | 897 | |
| 898 | // QuicHttpStream::GetTotalSent/ReceivedBytes currently only includes the |
sclittle | c4dc1a3 | 2015-09-24 00:15:45 | [diff] [blame] | 899 | // headers and payload. |
| 900 | EXPECT_EQ(static_cast<int64_t>(spdy_request_headers_frame_length), |
| 901 | stream_->GetTotalSentBytes()); |
| 902 | // Zero since the stream is closed before processing the headers. |
sclittle | 1edeeb2 | 2015-09-02 20:46:10 | [diff] [blame] | 903 | EXPECT_EQ(0, stream_->GetTotalReceivedBytes()); |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 904 | } |
| 905 | |
[email protected] | e1cca9a | 2013-09-20 17:14:44 | [diff] [blame] | 906 | // Regression test for http://crbug.com/294870 |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 907 | TEST_P(QuicHttpStreamTest, CheckPriorityWithNoDelegate) { |
| 908 | SetRequest("GET", "/", MEDIUM); |
[email protected] | e1cca9a | 2013-09-20 17:14:44 | [diff] [blame] | 909 | use_closing_stream_ = true; |
[email protected] | 459a740 | 2014-02-10 12:58:52 | [diff] [blame] | 910 | |
[email protected] | 08da9adb | 2014-04-24 08:33:31 | [diff] [blame] | 911 | AddWrite(ConstructRstStreamPacket(1)); |
[email protected] | 459a740 | 2014-02-10 12:58:52 | [diff] [blame] | 912 | |
[email protected] | e1cca9a | 2013-09-20 17:14:44 | [diff] [blame] | 913 | Initialize(); |
| 914 | |
| 915 | request_.method = "GET"; |
| 916 | request_.url = GURL("http://www.google.com/"); |
| 917 | |
rjshade | d5ced07 | 2015-12-18 19:26:02 | [diff] [blame] | 918 | EXPECT_EQ(OK, stream_->InitializeStream(&request_, MEDIUM, net_log_, |
| 919 | callback_.callback())); |
[email protected] | e1cca9a | 2013-09-20 17:14:44 | [diff] [blame] | 920 | |
| 921 | // Check that priority is highest. |
| 922 | QuicReliableClientStream* reliable_stream = |
| 923 | QuicHttpStreamPeer::GetQuicReliableClientStream(stream_.get()); |
| 924 | DCHECK(reliable_stream); |
| 925 | QuicReliableClientStream::Delegate* delegate = reliable_stream->GetDelegate(); |
| 926 | DCHECK(delegate); |
zhongyi | b867702 | 2015-12-01 05:51:30 | [diff] [blame] | 927 | DCHECK_EQ(kV3HighestPriority, reliable_stream->Priority()); |
[email protected] | e1cca9a | 2013-09-20 17:14:44 | [diff] [blame] | 928 | |
ianswett | 0888cff | 2015-11-24 17:42:16 | [diff] [blame] | 929 | // Set Delegate to nullptr and make sure Priority returns highest |
[email protected] | e1cca9a | 2013-09-20 17:14:44 | [diff] [blame] | 930 | // priority. |
rtenneti | be63573 | 2014-10-02 22:51:42 | [diff] [blame] | 931 | reliable_stream->SetDelegate(nullptr); |
zhongyi | b867702 | 2015-12-01 05:51:30 | [diff] [blame] | 932 | DCHECK_EQ(kV3HighestPriority, reliable_stream->Priority()); |
[email protected] | e1cca9a | 2013-09-20 17:14:44 | [diff] [blame] | 933 | reliable_stream->SetDelegate(delegate); |
sclittle | 1edeeb2 | 2015-09-02 20:46:10 | [diff] [blame] | 934 | |
sclittle | 1edeeb2 | 2015-09-02 20:46:10 | [diff] [blame] | 935 | EXPECT_EQ(0, stream_->GetTotalSentBytes()); |
| 936 | EXPECT_EQ(0, stream_->GetTotalReceivedBytes()); |
[email protected] | e1cca9a | 2013-09-20 17:14:44 | [diff] [blame] | 937 | } |
| 938 | |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 939 | } // namespace test |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 940 | } // namespace net |