[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 | |
| 9 | #include "net/base/net_errors.h" |
| 10 | #include "net/base/test_completion_callback.h" |
[email protected] | b2d26cfd | 2012-12-11 10:36:06 | [diff] [blame] | 11 | #include "net/base/upload_bytes_element_reader.h" |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 12 | #include "net/base/upload_data_stream.h" |
| 13 | #include "net/http/http_response_headers.h" |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 14 | #include "net/quic/congestion_control/receive_algorithm_interface.h" |
| 15 | #include "net/quic/congestion_control/send_algorithm_interface.h" |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 16 | #include "net/quic/crypto/crypto_protocol.h" |
[email protected] | 4df6984 | 2013-02-27 06:32:16 | [diff] [blame] | 17 | #include "net/quic/crypto/quic_decrypter.h" |
| 18 | #include "net/quic/crypto/quic_encrypter.h" |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 19 | #include "net/quic/quic_client_session.h" |
| 20 | #include "net/quic/quic_connection.h" |
| 21 | #include "net/quic/quic_connection_helper.h" |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame^] | 22 | #include "net/quic/quic_http_utils.h" |
| 23 | #include "net/quic/quic_reliable_client_stream.h" |
[email protected] | 3e7dca6 | 2013-09-10 16:14:23 | [diff] [blame] | 24 | #include "net/quic/spdy_utils.h" |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 25 | #include "net/quic/test_tools/mock_clock.h" |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 26 | #include "net/quic/test_tools/mock_crypto_client_stream_factory.h" |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 27 | #include "net/quic/test_tools/mock_random.h" |
[email protected] | b1f287d | 2012-12-22 17:25:39 | [diff] [blame] | 28 | #include "net/quic/test_tools/quic_connection_peer.h" |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 29 | #include "net/quic/test_tools/quic_test_utils.h" |
| 30 | #include "net/quic/test_tools/test_task_runner.h" |
| 31 | #include "net/socket/socket_test_util.h" |
[email protected] | 6cca996b | 2013-01-25 07:43:36 | [diff] [blame] | 32 | #include "net/spdy/spdy_frame_builder.h" |
| 33 | #include "net/spdy/spdy_framer.h" |
| 34 | #include "net/spdy/spdy_http_utils.h" |
| 35 | #include "net/spdy/spdy_protocol.h" |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame^] | 36 | #include "net/spdy/write_blocked_list.h" |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 37 | #include "testing/gmock/include/gmock/gmock.h" |
| 38 | #include "testing/gtest/include/gtest/gtest.h" |
| 39 | |
| 40 | using testing::_; |
[email protected] | 06ff515 | 2013-08-29 01:03:05 | [diff] [blame] | 41 | using testing::AnyNumber; |
| 42 | using testing::Return; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 43 | |
| 44 | namespace net { |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 45 | namespace test { |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 46 | namespace { |
| 47 | |
| 48 | const char kUploadData[] = "hello world!"; |
| 49 | |
| 50 | class TestQuicConnection : public QuicConnection { |
| 51 | public: |
| 52 | TestQuicConnection(QuicGuid guid, |
| 53 | IPEndPoint address, |
| 54 | QuicConnectionHelper* helper) |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 55 | : QuicConnection(guid, address, helper, false, QuicVersionMax()) { |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 56 | } |
| 57 | |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 58 | void SetSendAlgorithm(SendAlgorithmInterface* send_algorithm) { |
| 59 | QuicConnectionPeer::SetSendAlgorithm(this, send_algorithm); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 60 | } |
[email protected] | 26f3f8e | 2012-12-13 21:07:19 | [diff] [blame] | 61 | |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 62 | void SetReceiveAlgorithm(ReceiveAlgorithmInterface* receive_algorithm) { |
| 63 | QuicConnectionPeer::SetReceiveAlgorithm(this, receive_algorithm); |
[email protected] | 26f3f8e | 2012-12-13 21:07:19 | [diff] [blame] | 64 | } |
| 65 | }; |
| 66 | |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 67 | class TestReceiveAlgorithm : public ReceiveAlgorithmInterface { |
[email protected] | 26f3f8e | 2012-12-13 21:07:19 | [diff] [blame] | 68 | public: |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 69 | explicit TestReceiveAlgorithm(QuicCongestionFeedbackFrame* feedback) |
| 70 | : feedback_(feedback) { |
[email protected] | 26f3f8e | 2012-12-13 21:07:19 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | bool GenerateCongestionFeedback( |
| 74 | QuicCongestionFeedbackFrame* congestion_feedback) { |
| 75 | if (feedback_ == NULL) { |
| 76 | return false; |
| 77 | } |
| 78 | *congestion_feedback = *feedback_; |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | MOCK_METHOD4(RecordIncomingPacket, |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 83 | void(QuicByteCount, QuicPacketSequenceNumber, QuicTime, bool)); |
[email protected] | 26f3f8e | 2012-12-13 21:07:19 | [diff] [blame] | 84 | |
| 85 | private: |
| 86 | MockClock clock_; |
| 87 | QuicCongestionFeedbackFrame* feedback_; |
| 88 | |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 89 | DISALLOW_COPY_AND_ASSIGN(TestReceiveAlgorithm); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 90 | }; |
| 91 | |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 92 | // Subclass of QuicHttpStream that closes itself when the first piece of data |
| 93 | // is received. |
| 94 | class AutoClosingStream : public QuicHttpStream { |
| 95 | public: |
[email protected] | 0b2294d3 | 2013-08-02 00:46:36 | [diff] [blame] | 96 | explicit AutoClosingStream(const base::WeakPtr<QuicClientSession>& session) |
| 97 | : QuicHttpStream(session) { |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 98 | } |
| 99 | |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 100 | virtual int OnDataReceived(const char* data, int length) OVERRIDE { |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 101 | Close(false); |
| 102 | return OK; |
| 103 | } |
| 104 | }; |
| 105 | |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 106 | } // namespace |
| 107 | |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame^] | 108 | class QuicHttpStreamPeer { |
| 109 | public: |
| 110 | static QuicReliableClientStream* GetQuicReliableClientStream( |
| 111 | QuicHttpStream* stream) { |
| 112 | return stream->stream_; |
| 113 | } |
| 114 | }; |
| 115 | |
[email protected] | 6cca996b | 2013-01-25 07:43:36 | [diff] [blame] | 116 | class QuicHttpStreamTest : public ::testing::TestWithParam<bool> { |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 117 | protected: |
| 118 | const static bool kFin = true; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 119 | // Holds a packet to be written to the wire, and the IO mode that should |
| 120 | // be used by the mock socket when performing the write. |
| 121 | struct PacketToWrite { |
| 122 | PacketToWrite(IoMode mode, QuicEncryptedPacket* packet) |
| 123 | : mode(mode), |
| 124 | packet(packet) { |
| 125 | } |
| 126 | IoMode mode; |
| 127 | QuicEncryptedPacket* packet; |
| 128 | }; |
| 129 | |
| 130 | QuicHttpStreamTest() |
| 131 | : net_log_(BoundNetLog()), |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 132 | use_closing_stream_(false), |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 133 | read_buffer_(new IOBufferWithSize(4096)), |
| 134 | guid_(2), |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 135 | framer_(QuicVersionMax(), QuicTime::Zero(), false), |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 136 | creator_(guid_, &framer_, &random_, false) { |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 137 | IPAddressNumber ip; |
| 138 | CHECK(ParseIPLiteralToNumber("192.0.2.33", &ip)); |
| 139 | peer_addr_ = IPEndPoint(ip, 443); |
| 140 | self_addr_ = IPEndPoint(ip, 8435); |
[email protected] | d6202d01 | 2013-09-03 21:38:32 | [diff] [blame] | 141 | // TODO(rch): remove this. |
| 142 | QuicConnection::g_acks_do_not_instigate_acks = true; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | ~QuicHttpStreamTest() { |
| 146 | for (size_t i = 0; i < writes_.size(); i++) { |
| 147 | delete writes_[i].packet; |
| 148 | } |
[email protected] | d6202d01 | 2013-09-03 21:38:32 | [diff] [blame] | 149 | // TODO(rch): remove this. |
| 150 | QuicConnection::g_acks_do_not_instigate_acks = false; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | // Adds a packet to the list of expected writes. |
| 154 | void AddWrite(IoMode mode, QuicEncryptedPacket* packet) { |
| 155 | writes_.push_back(PacketToWrite(mode, packet)); |
| 156 | } |
| 157 | |
| 158 | // Returns the packet to be written at position |pos|. |
| 159 | QuicEncryptedPacket* GetWrite(size_t pos) { |
| 160 | return writes_[pos].packet; |
| 161 | } |
| 162 | |
| 163 | bool AtEof() { |
| 164 | return socket_data_->at_read_eof() && socket_data_->at_write_eof(); |
| 165 | } |
| 166 | |
| 167 | void ProcessPacket(const QuicEncryptedPacket& packet) { |
| 168 | connection_->ProcessUdpPacket(self_addr_, peer_addr_, packet); |
| 169 | } |
| 170 | |
| 171 | // Configures the test fixture to use the list of expected writes. |
| 172 | void Initialize() { |
| 173 | mock_writes_.reset(new MockWrite[writes_.size()]); |
| 174 | for (size_t i = 0; i < writes_.size(); i++) { |
| 175 | mock_writes_[i] = MockWrite(writes_[i].mode, |
| 176 | writes_[i].packet->data(), |
| 177 | writes_[i].packet->length()); |
| 178 | }; |
| 179 | |
| 180 | socket_data_.reset(new StaticSocketDataProvider(NULL, 0, mock_writes_.get(), |
| 181 | writes_.size())); |
| 182 | |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 183 | MockUDPClientSocket* socket = new MockUDPClientSocket(socket_data_.get(), |
| 184 | net_log_.net_log()); |
| 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(); |
| 188 | receive_algorithm_ = new TestReceiveAlgorithm(NULL); |
[email protected] | 06ff515 | 2013-08-29 01:03:05 | [diff] [blame] | 189 | EXPECT_CALL(*receive_algorithm_, RecordIncomingPacket(_, _, _, _)). |
| 190 | Times(AnyNumber()); |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame^] | 191 | EXPECT_CALL(*send_algorithm_, SentPacket(_, _, _, _, _)).Times(AnyNumber()); |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 192 | EXPECT_CALL(*send_algorithm_, RetransmissionDelay()).WillRepeatedly( |
[email protected] | 06ff515 | 2013-08-29 01:03:05 | [diff] [blame] | 193 | Return(QuicTime::Delta::Zero())); |
[email protected] | 575cce6 | 2013-08-03 02:06:43 | [diff] [blame] | 194 | EXPECT_CALL(*send_algorithm_, TimeUntilSend(_, _, _, _)). |
[email protected] | 06ff515 | 2013-08-29 01:03:05 | [diff] [blame] | 195 | WillRepeatedly(Return(QuicTime::Delta::Zero())); |
[email protected] | ea825e0 | 2013-08-21 18:12:45 | [diff] [blame] | 196 | EXPECT_CALL(*send_algorithm_, SmoothedRtt()).WillRepeatedly( |
[email protected] | 06ff515 | 2013-08-29 01:03:05 | [diff] [blame] | 197 | Return(QuicTime::Delta::Zero())); |
[email protected] | ea825e0 | 2013-08-21 18:12:45 | [diff] [blame] | 198 | EXPECT_CALL(*send_algorithm_, BandwidthEstimate()).WillRepeatedly( |
[email protected] | 06ff515 | 2013-08-29 01:03:05 | [diff] [blame] | 199 | Return(QuicBandwidth::Zero())); |
[email protected] | 9558c5d3 | 2012-12-22 00:08:14 | [diff] [blame] | 200 | helper_ = new QuicConnectionHelper(runner_.get(), &clock_, |
| 201 | &random_generator_, socket); |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 202 | connection_ = new TestQuicConnection(guid_, peer_addr_, helper_); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 203 | connection_->set_visitor(&visitor_); |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 204 | connection_->SetSendAlgorithm(send_algorithm_); |
| 205 | connection_->SetReceiveAlgorithm(receive_algorithm_); |
[email protected] | ef95114d | 2013-04-17 17:57:01 | [diff] [blame] | 206 | crypto_config_.SetDefaults(); |
[email protected] | 18ccfdb | 2013-08-15 00:13:44 | [diff] [blame] | 207 | session_.reset( |
| 208 | new QuicClientSession(connection_, |
| 209 | scoped_ptr<DatagramClientSocket>(socket), NULL, |
| 210 | &crypto_client_stream_factory_, |
| 211 | "www.google.com", DefaultQuicConfig(), |
| 212 | &crypto_config_, NULL)); |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 213 | session_->GetCryptoStream()->CryptoConnect(); |
[email protected] | 8ba8121 | 2013-05-03 13:11:48 | [diff] [blame] | 214 | EXPECT_TRUE(session_->IsCryptoHandshakeConfirmed()); |
[email protected] | 6cca996b | 2013-01-25 07:43:36 | [diff] [blame] | 215 | stream_.reset(use_closing_stream_ ? |
[email protected] | 0b2294d3 | 2013-08-02 00:46:36 | [diff] [blame] | 216 | new AutoClosingStream(session_->GetWeakPtr()) : |
| 217 | new QuicHttpStream(session_->GetWeakPtr())); |
[email protected] | 6cca996b | 2013-01-25 07:43:36 | [diff] [blame] | 218 | } |
| 219 | |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame^] | 220 | void SetRequestString(const std::string& method, |
| 221 | const std::string& path, |
| 222 | RequestPriority priority) { |
[email protected] | 9d9e793 | 2013-02-25 18:31:05 | [diff] [blame] | 223 | SpdyHeaderBlock headers; |
| 224 | headers[":method"] = method; |
| 225 | headers[":host"] = "www.google.com"; |
| 226 | headers[":path"] = path; |
| 227 | headers[":scheme"] = "http"; |
| 228 | headers[":version"] = "HTTP/1.1"; |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame^] | 229 | request_data_ = SerializeHeaderBlock(headers, true, priority); |
[email protected] | 6cca996b | 2013-01-25 07:43:36 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | void SetResponseString(const std::string& status, const std::string& body) { |
[email protected] | 9d9e793 | 2013-02-25 18:31:05 | [diff] [blame] | 233 | SpdyHeaderBlock headers; |
| 234 | headers[":status"] = status; |
| 235 | headers[":version"] = "HTTP/1.1"; |
| 236 | headers["content-type"] = "text/plain"; |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame^] | 237 | response_data_ = SerializeHeaderBlock(headers, false, DEFAULT_PRIORITY) + |
| 238 | body; |
[email protected] | 6cca996b | 2013-01-25 07:43:36 | [diff] [blame] | 239 | } |
| 240 | |
[email protected] | 8edeb8d | 2013-08-28 06:11:43 | [diff] [blame] | 241 | std::string SerializeHeaderBlock(const SpdyHeaderBlock& headers, |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame^] | 242 | bool write_priority, |
| 243 | RequestPriority priority) { |
[email protected] | 821555c | 2013-05-16 20:20:17 | [diff] [blame] | 244 | QuicSpdyCompressor compressor; |
[email protected] | 8edeb8d | 2013-08-28 06:11:43 | [diff] [blame] | 245 | if (framer_.version() >= QUIC_VERSION_9 && write_priority) { |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame^] | 246 | return compressor.CompressHeadersWithPriority( |
| 247 | ConvertRequestPriorityToQuicPriority(priority), headers); |
[email protected] | 8edeb8d | 2013-08-28 06:11:43 | [diff] [blame] | 248 | } |
[email protected] | e4e4746 | 2013-08-22 19:37:46 | [diff] [blame] | 249 | return compressor.CompressHeaders(headers); |
[email protected] | 6cca996b | 2013-01-25 07:43:36 | [diff] [blame] | 250 | } |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 251 | |
[email protected] | 06ff515 | 2013-08-29 01:03:05 | [diff] [blame] | 252 | // Returns a newly created packet to send kData on stream 3. |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 253 | QuicEncryptedPacket* ConstructDataPacket( |
| 254 | QuicPacketSequenceNumber sequence_number, |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 255 | bool should_include_version, |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 256 | bool fin, |
| 257 | QuicStreamOffset offset, |
| 258 | base::StringPiece data) { |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 259 | InitializeHeader(sequence_number, should_include_version); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 260 | QuicStreamFrame frame(3, fin, offset, data); |
| 261 | return ConstructPacket(header_, QuicFrame(&frame)); |
| 262 | } |
| 263 | |
[email protected] | 06ff515 | 2013-08-29 01:03:05 | [diff] [blame] | 264 | // Returns a newly created packet to RST_STREAM stream 3. |
| 265 | QuicEncryptedPacket* ConstructRstStreamPacket( |
| 266 | QuicPacketSequenceNumber sequence_number) { |
| 267 | InitializeHeader(sequence_number, false); |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame^] | 268 | QuicRstStreamFrame frame(3, QUIC_ERROR_PROCESSING_STREAM); |
[email protected] | 06ff515 | 2013-08-29 01:03:05 | [diff] [blame] | 269 | return ConstructPacket(header_, QuicFrame(&frame)); |
| 270 | } |
| 271 | |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 272 | // Returns a newly created packet to send ack data. |
| 273 | QuicEncryptedPacket* ConstructAckPacket( |
| 274 | QuicPacketSequenceNumber sequence_number, |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 275 | QuicPacketSequenceNumber largest_received, |
| 276 | QuicPacketSequenceNumber least_unacked) { |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 277 | InitializeHeader(sequence_number, false); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 278 | |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 279 | QuicAckFrame ack(largest_received, QuicTime::Zero(), least_unacked); |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 280 | ack.sent_info.entropy_hash = 0; |
| 281 | ack.received_info.entropy_hash = 0; |
| 282 | |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 283 | return ConstructPacket(header_, QuicFrame(&ack)); |
| 284 | } |
| 285 | |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 286 | // Returns a newly created packet to send ack data. |
| 287 | QuicEncryptedPacket* ConstructRstPacket( |
| 288 | QuicPacketSequenceNumber sequence_number, |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 289 | QuicStreamId stream_id) { |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 290 | InitializeHeader(sequence_number, false); |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 291 | |
[email protected] | 74bda14 | 2013-03-31 02:49:11 | [diff] [blame] | 292 | QuicRstStreamFrame rst(stream_id, QUIC_STREAM_NO_ERROR); |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 293 | return ConstructPacket(header_, QuicFrame(&rst)); |
| 294 | } |
| 295 | |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 296 | BoundNetLog net_log_; |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 297 | bool use_closing_stream_; |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 298 | MockSendAlgorithm* send_algorithm_; |
| 299 | TestReceiveAlgorithm* receive_algorithm_; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 300 | scoped_refptr<TestTaskRunner> runner_; |
[email protected] | 4356f0f | 2013-04-07 00:58:17 | [diff] [blame] | 301 | scoped_ptr<MockWrite[]> mock_writes_; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 302 | MockClock clock_; |
[email protected] | 9558c5d3 | 2012-12-22 00:08:14 | [diff] [blame] | 303 | MockRandom random_generator_; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 304 | TestQuicConnection* connection_; |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 305 | QuicConnectionHelper* helper_; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 306 | testing::StrictMock<MockConnectionVisitor> visitor_; |
| 307 | scoped_ptr<QuicHttpStream> stream_; |
| 308 | scoped_ptr<QuicClientSession> session_; |
[email protected] | ef95114d | 2013-04-17 17:57:01 | [diff] [blame] | 309 | QuicCryptoClientConfig crypto_config_; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 310 | TestCompletionCallback callback_; |
| 311 | HttpRequestInfo request_; |
| 312 | HttpRequestHeaders headers_; |
| 313 | HttpResponseInfo response_; |
| 314 | scoped_refptr<IOBufferWithSize> read_buffer_; |
[email protected] | 6cca996b | 2013-01-25 07:43:36 | [diff] [blame] | 315 | std::string request_data_; |
| 316 | std::string response_data_; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 317 | |
| 318 | private: |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 319 | void InitializeHeader(QuicPacketSequenceNumber sequence_number, |
| 320 | bool should_include_version) { |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 321 | header_.public_header.guid = guid_; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 322 | header_.public_header.reset_flag = false; |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 323 | header_.public_header.version_flag = should_include_version; |
[email protected] | ea825e0 | 2013-08-21 18:12:45 | [diff] [blame] | 324 | header_.public_header.sequence_number_length = PACKET_1BYTE_SEQUENCE_NUMBER; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 325 | header_.packet_sequence_number = sequence_number; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 326 | header_.fec_group = 0; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 327 | header_.entropy_flag = false; |
| 328 | header_.fec_flag = false; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | QuicEncryptedPacket* ConstructPacket(const QuicPacketHeader& header, |
| 332 | const QuicFrame& frame) { |
| 333 | QuicFrames frames; |
| 334 | frames.push_back(frame); |
[email protected] | 610a7e94 | 2012-12-18 00:21:39 | [diff] [blame] | 335 | scoped_ptr<QuicPacket> packet( |
[email protected] | 3e60db8 | 2013-08-05 19:43:06 | [diff] [blame] | 336 | framer_.BuildUnsizedDataPacket(header_, frames).packet); |
[email protected] | 8ba8121 | 2013-05-03 13:11:48 | [diff] [blame] | 337 | return framer_.EncryptPacket( |
| 338 | ENCRYPTION_NONE, header.packet_sequence_number, *packet); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | const QuicGuid guid_; |
| 342 | QuicFramer framer_; |
| 343 | IPEndPoint self_addr_; |
| 344 | IPEndPoint peer_addr_; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 345 | MockRandom random_; |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 346 | MockCryptoClientStreamFactory crypto_client_stream_factory_; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 347 | QuicPacketCreator creator_; |
| 348 | QuicPacketHeader header_; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 349 | scoped_ptr<StaticSocketDataProvider> socket_data_; |
| 350 | std::vector<PacketToWrite> writes_; |
| 351 | }; |
| 352 | |
[email protected] | 9d9e793 | 2013-02-25 18:31:05 | [diff] [blame] | 353 | TEST_F(QuicHttpStreamTest, RenewStreamForAuth) { |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 354 | Initialize(); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 355 | EXPECT_EQ(NULL, stream_->RenewStreamForAuth()); |
| 356 | } |
| 357 | |
[email protected] | 9d9e793 | 2013-02-25 18:31:05 | [diff] [blame] | 358 | TEST_F(QuicHttpStreamTest, CanFindEndOfResponse) { |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 359 | Initialize(); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 360 | EXPECT_TRUE(stream_->CanFindEndOfResponse()); |
| 361 | } |
| 362 | |
[email protected] | 9d9e793 | 2013-02-25 18:31:05 | [diff] [blame] | 363 | TEST_F(QuicHttpStreamTest, IsConnectionReusable) { |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 364 | Initialize(); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 365 | EXPECT_FALSE(stream_->IsConnectionReusable()); |
| 366 | } |
| 367 | |
[email protected] | 9d9e793 | 2013-02-25 18:31:05 | [diff] [blame] | 368 | TEST_F(QuicHttpStreamTest, GetRequest) { |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame^] | 369 | SetRequestString("GET", "/", DEFAULT_PRIORITY); |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 370 | AddWrite(SYNCHRONOUS, ConstructDataPacket(1, true, kFin, 0, |
[email protected] | 6cca996b | 2013-01-25 07:43:36 | [diff] [blame] | 371 | request_data_)); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 372 | Initialize(); |
| 373 | |
| 374 | request_.method = "GET"; |
| 375 | request_.url = GURL("http://www.google.com/"); |
| 376 | |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 377 | EXPECT_EQ(OK, stream_->InitializeStream(&request_, DEFAULT_PRIORITY, |
| 378 | net_log_, callback_.callback())); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 379 | EXPECT_EQ(OK, stream_->SendRequest(headers_, &response_, |
| 380 | callback_.callback())); |
| 381 | EXPECT_EQ(&response_, stream_->GetResponseInfo()); |
| 382 | |
| 383 | // Ack the request. |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 384 | scoped_ptr<QuicEncryptedPacket> ack(ConstructAckPacket(1, 0, 0)); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 385 | ProcessPacket(*ack); |
| 386 | |
| 387 | EXPECT_EQ(ERR_IO_PENDING, |
| 388 | stream_->ReadResponseHeaders(callback_.callback())); |
| 389 | |
| 390 | // Send the response without a body. |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 391 | SetResponseString("404 Not Found", std::string()); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 392 | scoped_ptr<QuicEncryptedPacket> resp( |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 393 | ConstructDataPacket(2, false, kFin, 0, response_data_)); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 394 | ProcessPacket(*resp); |
| 395 | |
| 396 | // Now that the headers have been processed, the callback will return. |
| 397 | EXPECT_EQ(OK, callback_.WaitForResult()); |
[email protected] | cadac62 | 2013-06-11 16:46:36 | [diff] [blame] | 398 | ASSERT_TRUE(response_.headers.get()); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 399 | EXPECT_EQ(404, response_.headers->response_code()); |
| 400 | EXPECT_TRUE(response_.headers->HasHeaderValue("Content-Type", "text/plain")); |
| 401 | |
| 402 | // There is no body, so this should return immediately. |
| 403 | EXPECT_EQ(0, stream_->ReadResponseBody(read_buffer_.get(), |
| 404 | read_buffer_->size(), |
| 405 | callback_.callback())); |
| 406 | EXPECT_TRUE(stream_->IsResponseBodyComplete()); |
| 407 | EXPECT_TRUE(AtEof()); |
| 408 | } |
| 409 | |
[email protected] | 3e7dca6 | 2013-09-10 16:14:23 | [diff] [blame] | 410 | // Regression test for http://crbug.com/288128 |
| 411 | TEST_F(QuicHttpStreamTest, GetRequestLargeResponse) { |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame^] | 412 | SetRequestString("GET", "/", DEFAULT_PRIORITY); |
[email protected] | 3e7dca6 | 2013-09-10 16:14:23 | [diff] [blame] | 413 | AddWrite(SYNCHRONOUS, ConstructDataPacket(1, true, kFin, 0, |
| 414 | request_data_)); |
| 415 | Initialize(); |
| 416 | |
| 417 | request_.method = "GET"; |
| 418 | request_.url = GURL("http://www.google.com/"); |
| 419 | |
| 420 | EXPECT_EQ(OK, stream_->InitializeStream(&request_, DEFAULT_PRIORITY, |
| 421 | net_log_, callback_.callback())); |
| 422 | EXPECT_EQ(OK, stream_->SendRequest(headers_, &response_, |
| 423 | callback_.callback())); |
| 424 | EXPECT_EQ(&response_, stream_->GetResponseInfo()); |
| 425 | |
| 426 | // Ack the request. |
| 427 | scoped_ptr<QuicEncryptedPacket> ack(ConstructAckPacket(1, 0, 0)); |
| 428 | ProcessPacket(*ack); |
| 429 | |
| 430 | EXPECT_EQ(ERR_IO_PENDING, |
| 431 | stream_->ReadResponseHeaders(callback_.callback())); |
| 432 | |
| 433 | SpdyHeaderBlock headers; |
| 434 | headers[":status"] = "200 OK"; |
| 435 | headers[":version"] = "HTTP/1.1"; |
| 436 | headers["content-type"] = "text/plain"; |
| 437 | headers["big6"] = std::string(10000, 'x'); // Lots of x's. |
| 438 | |
| 439 | std::string response = SpdyUtils::SerializeUncompressedHeaders(headers); |
| 440 | EXPECT_LT(4096u, response.length()); |
| 441 | stream_->OnDataReceived(response.data(), response.length()); |
| 442 | stream_->OnClose(QUIC_NO_ERROR); |
| 443 | |
| 444 | // Now that the headers have been processed, the callback will return. |
| 445 | EXPECT_EQ(OK, callback_.WaitForResult()); |
| 446 | ASSERT_TRUE(response_.headers.get()); |
| 447 | EXPECT_EQ(200, response_.headers->response_code()); |
| 448 | EXPECT_TRUE(response_.headers->HasHeaderValue("Content-Type", "text/plain")); |
| 449 | |
| 450 | // There is no body, so this should return immediately. |
| 451 | EXPECT_EQ(0, stream_->ReadResponseBody(read_buffer_.get(), |
| 452 | read_buffer_->size(), |
| 453 | callback_.callback())); |
| 454 | EXPECT_TRUE(stream_->IsResponseBodyComplete()); |
| 455 | EXPECT_TRUE(AtEof()); |
| 456 | } |
| 457 | |
[email protected] | 9d9e793 | 2013-02-25 18:31:05 | [diff] [blame] | 458 | TEST_F(QuicHttpStreamTest, GetRequestFullResponseInSinglePacket) { |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame^] | 459 | SetRequestString("GET", "/", DEFAULT_PRIORITY); |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 460 | AddWrite(SYNCHRONOUS, ConstructDataPacket(1, true, kFin, 0, request_data_)); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 461 | Initialize(); |
| 462 | |
| 463 | request_.method = "GET"; |
| 464 | request_.url = GURL("http://www.google.com/"); |
| 465 | |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 466 | EXPECT_EQ(OK, stream_->InitializeStream(&request_, DEFAULT_PRIORITY, |
| 467 | net_log_, callback_.callback())); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 468 | EXPECT_EQ(OK, stream_->SendRequest(headers_, &response_, |
| 469 | callback_.callback())); |
| 470 | EXPECT_EQ(&response_, stream_->GetResponseInfo()); |
| 471 | |
| 472 | // Ack the request. |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 473 | scoped_ptr<QuicEncryptedPacket> ack(ConstructAckPacket(1, 0, 0)); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 474 | ProcessPacket(*ack); |
| 475 | |
| 476 | EXPECT_EQ(ERR_IO_PENDING, |
| 477 | stream_->ReadResponseHeaders(callback_.callback())); |
| 478 | |
| 479 | // Send the response with a body. |
[email protected] | 6cca996b | 2013-01-25 07:43:36 | [diff] [blame] | 480 | SetResponseString("200 OK", "hello world!"); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 481 | scoped_ptr<QuicEncryptedPacket> resp( |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 482 | ConstructDataPacket(2, false, kFin, 0, response_data_)); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 483 | ProcessPacket(*resp); |
| 484 | |
| 485 | // Now that the headers have been processed, the callback will return. |
| 486 | EXPECT_EQ(OK, callback_.WaitForResult()); |
[email protected] | cadac62 | 2013-06-11 16:46:36 | [diff] [blame] | 487 | ASSERT_TRUE(response_.headers.get()); |
[email protected] | 6cca996b | 2013-01-25 07:43:36 | [diff] [blame] | 488 | EXPECT_EQ(200, response_.headers->response_code()); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 489 | EXPECT_TRUE(response_.headers->HasHeaderValue("Content-Type", "text/plain")); |
| 490 | |
| 491 | // There is no body, so this should return immediately. |
| 492 | // Since the body has already arrived, this should return immediately. |
| 493 | EXPECT_EQ(12, stream_->ReadResponseBody(read_buffer_.get(), |
| 494 | read_buffer_->size(), |
| 495 | callback_.callback())); |
| 496 | EXPECT_TRUE(stream_->IsResponseBodyComplete()); |
| 497 | EXPECT_TRUE(AtEof()); |
| 498 | } |
| 499 | |
[email protected] | 9d9e793 | 2013-02-25 18:31:05 | [diff] [blame] | 500 | TEST_F(QuicHttpStreamTest, SendPostRequest) { |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame^] | 501 | SetRequestString("POST", "/", DEFAULT_PRIORITY); |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 502 | AddWrite(SYNCHRONOUS, ConstructDataPacket(1, true, !kFin, 0, request_data_)); |
| 503 | AddWrite(SYNCHRONOUS, ConstructDataPacket(2, true, kFin, |
| 504 | request_data_.length(), |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 505 | kUploadData)); |
[email protected] | ec64011 | 2013-08-09 03:56:18 | [diff] [blame] | 506 | AddWrite(SYNCHRONOUS, ConstructAckPacket(3, 3, 1)); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 507 | |
| 508 | Initialize(); |
| 509 | |
[email protected] | b2d26cfd | 2012-12-11 10:36:06 | [diff] [blame] | 510 | ScopedVector<UploadElementReader> element_readers; |
| 511 | element_readers.push_back( |
| 512 | new UploadBytesElementReader(kUploadData, strlen(kUploadData))); |
| 513 | UploadDataStream upload_data_stream(&element_readers, 0); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 514 | request_.method = "POST"; |
| 515 | request_.url = GURL("http://www.google.com/"); |
| 516 | request_.upload_data_stream = &upload_data_stream; |
[email protected] | 4db27d8 | 2012-12-20 11:50:24 | [diff] [blame] | 517 | ASSERT_EQ(OK, request_.upload_data_stream->Init(CompletionCallback())); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 518 | |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 519 | EXPECT_EQ(OK, stream_->InitializeStream(&request_, DEFAULT_PRIORITY, |
| 520 | net_log_, callback_.callback())); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 521 | EXPECT_EQ(OK, stream_->SendRequest(headers_, &response_, |
| 522 | callback_.callback())); |
| 523 | EXPECT_EQ(&response_, stream_->GetResponseInfo()); |
| 524 | |
| 525 | // Ack both packets in the request. |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 526 | scoped_ptr<QuicEncryptedPacket> ack(ConstructAckPacket(1, 0, 0)); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 527 | ProcessPacket(*ack); |
| 528 | |
| 529 | // Send the response headers (but not the body). |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 530 | SetResponseString("200 OK", std::string()); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 531 | scoped_ptr<QuicEncryptedPacket> resp( |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 532 | ConstructDataPacket(2, false, !kFin, 0, response_data_)); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 533 | ProcessPacket(*resp); |
| 534 | |
| 535 | // Since the headers have already arrived, this should return immediately. |
| 536 | EXPECT_EQ(OK, stream_->ReadResponseHeaders(callback_.callback())); |
[email protected] | cadac62 | 2013-06-11 16:46:36 | [diff] [blame] | 537 | ASSERT_TRUE(response_.headers.get()); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 538 | EXPECT_EQ(200, response_.headers->response_code()); |
| 539 | EXPECT_TRUE(response_.headers->HasHeaderValue("Content-Type", "text/plain")); |
| 540 | |
| 541 | // Send the response body. |
| 542 | const char kResponseBody[] = "Hello world!"; |
| 543 | scoped_ptr<QuicEncryptedPacket> resp_body( |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 544 | ConstructDataPacket(3, false, kFin, response_data_.length(), |
| 545 | kResponseBody)); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 546 | ProcessPacket(*resp_body); |
| 547 | |
| 548 | // Since the body has already arrived, this should return immediately. |
| 549 | EXPECT_EQ(static_cast<int>(strlen(kResponseBody)), |
| 550 | stream_->ReadResponseBody(read_buffer_.get(), read_buffer_->size(), |
| 551 | callback_.callback())); |
| 552 | |
| 553 | EXPECT_TRUE(stream_->IsResponseBodyComplete()); |
| 554 | EXPECT_TRUE(AtEof()); |
| 555 | } |
| 556 | |
[email protected] | c9e49a0 | 2013-02-26 05:56:47 | [diff] [blame] | 557 | TEST_F(QuicHttpStreamTest, SendChunkedPostRequest) { |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame^] | 558 | SetRequestString("POST", "/", DEFAULT_PRIORITY); |
[email protected] | c9e49a0 | 2013-02-26 05:56:47 | [diff] [blame] | 559 | size_t chunk_size = strlen(kUploadData); |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 560 | AddWrite(SYNCHRONOUS, ConstructDataPacket(1, true, !kFin, 0, request_data_)); |
| 561 | AddWrite(SYNCHRONOUS, ConstructDataPacket(2, true, !kFin, |
| 562 | request_data_.length(), |
[email protected] | c9e49a0 | 2013-02-26 05:56:47 | [diff] [blame] | 563 | kUploadData)); |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 564 | AddWrite(SYNCHRONOUS, ConstructDataPacket(3, true, kFin, |
[email protected] | c9e49a0 | 2013-02-26 05:56:47 | [diff] [blame] | 565 | request_data_.length() + chunk_size, |
| 566 | kUploadData)); |
[email protected] | ec64011 | 2013-08-09 03:56:18 | [diff] [blame] | 567 | AddWrite(SYNCHRONOUS, ConstructAckPacket(4, 3, 1)); |
[email protected] | c9e49a0 | 2013-02-26 05:56:47 | [diff] [blame] | 568 | |
| 569 | Initialize(); |
| 570 | |
| 571 | UploadDataStream upload_data_stream(UploadDataStream::CHUNKED, 0); |
| 572 | upload_data_stream.AppendChunk(kUploadData, chunk_size, false); |
| 573 | |
| 574 | request_.method = "POST"; |
| 575 | request_.url = GURL("http://www.google.com/"); |
| 576 | request_.upload_data_stream = &upload_data_stream; |
| 577 | ASSERT_EQ(OK, request_.upload_data_stream->Init(CompletionCallback())); |
| 578 | |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 579 | ASSERT_EQ(OK, stream_->InitializeStream(&request_, DEFAULT_PRIORITY, |
| 580 | net_log_, callback_.callback())); |
[email protected] | c9e49a0 | 2013-02-26 05:56:47 | [diff] [blame] | 581 | ASSERT_EQ(ERR_IO_PENDING, stream_->SendRequest(headers_, &response_, |
| 582 | callback_.callback())); |
| 583 | EXPECT_EQ(&response_, stream_->GetResponseInfo()); |
| 584 | |
| 585 | upload_data_stream.AppendChunk(kUploadData, chunk_size, true); |
| 586 | |
| 587 | // Ack both packets in the request. |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 588 | scoped_ptr<QuicEncryptedPacket> ack(ConstructAckPacket(1, 0, 0)); |
[email protected] | c9e49a0 | 2013-02-26 05:56:47 | [diff] [blame] | 589 | ProcessPacket(*ack); |
| 590 | |
| 591 | // Send the response headers (but not the body). |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 592 | SetResponseString("200 OK", std::string()); |
[email protected] | c9e49a0 | 2013-02-26 05:56:47 | [diff] [blame] | 593 | scoped_ptr<QuicEncryptedPacket> resp( |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 594 | ConstructDataPacket(2, false, !kFin, 0, response_data_)); |
[email protected] | c9e49a0 | 2013-02-26 05:56:47 | [diff] [blame] | 595 | ProcessPacket(*resp); |
| 596 | |
| 597 | // Since the headers have already arrived, this should return immediately. |
| 598 | ASSERT_EQ(OK, stream_->ReadResponseHeaders(callback_.callback())); |
[email protected] | cadac62 | 2013-06-11 16:46:36 | [diff] [blame] | 599 | ASSERT_TRUE(response_.headers.get()); |
[email protected] | c9e49a0 | 2013-02-26 05:56:47 | [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!"; |
| 605 | scoped_ptr<QuicEncryptedPacket> resp_body( |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 606 | ConstructDataPacket(3, false, kFin, response_data_.length(), |
| 607 | kResponseBody)); |
[email protected] | c9e49a0 | 2013-02-26 05:56:47 | [diff] [blame] | 608 | ProcessPacket(*resp_body); |
| 609 | |
| 610 | // Since the body has already arrived, this should return immediately. |
| 611 | ASSERT_EQ(static_cast<int>(strlen(kResponseBody)), |
| 612 | stream_->ReadResponseBody(read_buffer_.get(), read_buffer_->size(), |
| 613 | callback_.callback())); |
| 614 | |
| 615 | EXPECT_TRUE(stream_->IsResponseBodyComplete()); |
| 616 | EXPECT_TRUE(AtEof()); |
| 617 | } |
| 618 | |
[email protected] | 9d9e793 | 2013-02-25 18:31:05 | [diff] [blame] | 619 | TEST_F(QuicHttpStreamTest, DestroyedEarly) { |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame^] | 620 | SetRequestString("GET", "/", DEFAULT_PRIORITY); |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 621 | AddWrite(SYNCHRONOUS, ConstructDataPacket(1, true, kFin, 0, request_data_)); |
[email protected] | 06ff515 | 2013-08-29 01:03:05 | [diff] [blame] | 622 | AddWrite(SYNCHRONOUS, ConstructRstStreamPacket(2)); |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 623 | use_closing_stream_ = true; |
| 624 | Initialize(); |
| 625 | |
| 626 | request_.method = "GET"; |
| 627 | request_.url = GURL("http://www.google.com/"); |
| 628 | |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 629 | EXPECT_EQ(OK, stream_->InitializeStream(&request_, DEFAULT_PRIORITY, |
| 630 | net_log_, callback_.callback())); |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 631 | EXPECT_EQ(OK, stream_->SendRequest(headers_, &response_, |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame^] | 632 | callback_.callback())); |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 633 | EXPECT_EQ(&response_, stream_->GetResponseInfo()); |
| 634 | |
| 635 | // Ack the request. |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 636 | scoped_ptr<QuicEncryptedPacket> ack(ConstructAckPacket(1, 0, 0)); |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 637 | ProcessPacket(*ack); |
| 638 | EXPECT_EQ(ERR_IO_PENDING, |
| 639 | stream_->ReadResponseHeaders(callback_.callback())); |
| 640 | |
| 641 | // Send the response with a body. |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 642 | SetResponseString("404 OK", "hello world!"); |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 643 | scoped_ptr<QuicEncryptedPacket> resp( |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 644 | ConstructDataPacket(2, false, kFin, 0, response_data_)); |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 645 | |
| 646 | // In the course of processing this packet, the QuicHttpStream close itself. |
| 647 | ProcessPacket(*resp); |
| 648 | |
| 649 | EXPECT_TRUE(AtEof()); |
| 650 | } |
| 651 | |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame^] | 652 | TEST_F(QuicHttpStreamTest, Priority) { |
| 653 | SetRequestString("GET", "/", MEDIUM); |
| 654 | AddWrite(SYNCHRONOUS, ConstructDataPacket(1, true, kFin, 0, request_data_)); |
| 655 | AddWrite(SYNCHRONOUS, ConstructRstStreamPacket(2)); |
| 656 | use_closing_stream_ = true; |
| 657 | Initialize(); |
| 658 | |
| 659 | request_.method = "GET"; |
| 660 | request_.url = GURL("http://www.google.com/"); |
| 661 | |
| 662 | EXPECT_EQ(OK, stream_->InitializeStream(&request_, MEDIUM, |
| 663 | net_log_, callback_.callback())); |
| 664 | |
| 665 | // Check that priority is highest. |
| 666 | QuicReliableClientStream* reliable_stream = |
| 667 | QuicHttpStreamPeer::GetQuicReliableClientStream(stream_.get()); |
| 668 | DCHECK(reliable_stream); |
| 669 | DCHECK_EQ(static_cast<QuicPriority>(kHighestPriority), |
| 670 | reliable_stream->EffectivePriority()); |
| 671 | |
| 672 | EXPECT_EQ(OK, stream_->SendRequest(headers_, &response_, |
| 673 | callback_.callback())); |
| 674 | EXPECT_EQ(&response_, stream_->GetResponseInfo()); |
| 675 | |
| 676 | // Check that priority has now dropped back to MEDIUM. |
| 677 | DCHECK_EQ(MEDIUM, ConvertQuicPriorityToRequestPriority( |
| 678 | reliable_stream->EffectivePriority())); |
| 679 | |
| 680 | // Ack the request. |
| 681 | scoped_ptr<QuicEncryptedPacket> ack(ConstructAckPacket(1, 0, 0)); |
| 682 | ProcessPacket(*ack); |
| 683 | EXPECT_EQ(ERR_IO_PENDING, |
| 684 | stream_->ReadResponseHeaders(callback_.callback())); |
| 685 | |
| 686 | // Send the response with a body. |
| 687 | SetResponseString("404 OK", "hello world!"); |
| 688 | scoped_ptr<QuicEncryptedPacket> resp( |
| 689 | ConstructDataPacket(2, false, kFin, 0, response_data_)); |
| 690 | |
| 691 | // In the course of processing this packet, the QuicHttpStream close itself. |
| 692 | ProcessPacket(*resp); |
| 693 | |
| 694 | EXPECT_TRUE(AtEof()); |
| 695 | } |
| 696 | |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 697 | } // namespace test |
| 698 | |
| 699 | } // namespace net |