blob: 5e46258f2cbce5d46a4b131e3722ffcf0fdd7451 [file] [log] [blame]
[email protected]f702d572012-12-04 15:56:201// 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]b2d26cfd2012-12-11 10:36:0611#include "net/base/upload_bytes_element_reader.h"
[email protected]f702d572012-12-04 15:56:2012#include "net/base/upload_data_stream.h"
13#include "net/http/http_response_headers.h"
[email protected]fee17f72013-02-03 07:47:4114#include "net/quic/congestion_control/receive_algorithm_interface.h"
15#include "net/quic/congestion_control/send_algorithm_interface.h"
[email protected]e8ff26842013-03-22 21:02:0516#include "net/quic/crypto/crypto_protocol.h"
[email protected]4df69842013-02-27 06:32:1617#include "net/quic/crypto/quic_decrypter.h"
18#include "net/quic/crypto/quic_encrypter.h"
[email protected]17bf15c2014-03-14 10:08:0419#include "net/quic/crypto/quic_server_info.h"
[email protected]f702d572012-12-04 15:56:2020#include "net/quic/quic_client_session.h"
21#include "net/quic/quic_connection.h"
22#include "net/quic/quic_connection_helper.h"
[email protected]cbd731e2013-10-24 00:20:3923#include "net/quic/quic_default_packet_writer.h"
[email protected]24e5bc52013-09-18 15:36:5824#include "net/quic/quic_http_utils.h"
25#include "net/quic/quic_reliable_client_stream.h"
[email protected]9f0dcd4e2014-01-16 15:58:1426#include "net/quic/quic_write_blocked_list.h"
[email protected]3e7dca62013-09-10 16:14:2327#include "net/quic/spdy_utils.h"
[email protected]f702d572012-12-04 15:56:2028#include "net/quic/test_tools/mock_clock.h"
[email protected]e8ff26842013-03-22 21:02:0529#include "net/quic/test_tools/mock_crypto_client_stream_factory.h"
[email protected]9db443912013-02-25 05:27:0330#include "net/quic/test_tools/mock_random.h"
[email protected]b1f287d2012-12-22 17:25:3931#include "net/quic/test_tools/quic_connection_peer.h"
[email protected]1e960032013-12-20 19:00:2032#include "net/quic/test_tools/quic_test_packet_maker.h"
[email protected]f702d572012-12-04 15:56:2033#include "net/quic/test_tools/quic_test_utils.h"
34#include "net/quic/test_tools/test_task_runner.h"
35#include "net/socket/socket_test_util.h"
[email protected]6cca996b2013-01-25 07:43:3636#include "net/spdy/spdy_frame_builder.h"
37#include "net/spdy/spdy_framer.h"
38#include "net/spdy/spdy_http_utils.h"
39#include "net/spdy/spdy_protocol.h"
[email protected]f702d572012-12-04 15:56:2040#include "testing/gmock/include/gmock/gmock.h"
41#include "testing/gtest/include/gtest/gtest.h"
42
43using testing::_;
[email protected]06ff5152013-08-29 01:03:0544using testing::AnyNumber;
45using testing::Return;
[email protected]f702d572012-12-04 15:56:2046
47namespace net {
[email protected]f702d572012-12-04 15:56:2048namespace test {
[email protected]f702d572012-12-04 15:56:2049namespace {
50
51const char kUploadData[] = "hello world!";
[email protected]e4c3ea62014-03-15 00:45:1452const char kServerHostname[] = "www.google.com";
53const uint16 kServerPort = 80;
[email protected]f702d572012-12-04 15:56:2054
55class TestQuicConnection : public QuicConnection {
56 public:
[email protected]1e960032013-12-20 19:00:2057 TestQuicConnection(const QuicVersionVector& versions,
[email protected]3aa9ca72014-02-27 19:39:4358 QuicConnectionId connection_id,
[email protected]f702d572012-12-04 15:56:2059 IPEndPoint address,
[email protected]cbd731e2013-10-24 00:20:3960 QuicConnectionHelper* helper,
61 QuicPacketWriter* writer)
[email protected]66cd2d62014-08-01 18:42:3962 : QuicConnection(connection_id,
63 address,
64 helper,
65 writer,
66 false /* owns_writer */,
67 false /* is_server */,
[email protected]ce7bb1412014-05-17 15:51:3368 versions) {
[email protected]f702d572012-12-04 15:56:2069 }
70
[email protected]fee17f72013-02-03 07:47:4171 void SetSendAlgorithm(SendAlgorithmInterface* send_algorithm) {
72 QuicConnectionPeer::SetSendAlgorithm(this, send_algorithm);
[email protected]f702d572012-12-04 15:56:2073 }
[email protected]26f3f8e2012-12-13 21:07:1974
[email protected]fee17f72013-02-03 07:47:4175 void SetReceiveAlgorithm(ReceiveAlgorithmInterface* receive_algorithm) {
76 QuicConnectionPeer::SetReceiveAlgorithm(this, receive_algorithm);
[email protected]26f3f8e2012-12-13 21:07:1977 }
78};
79
[email protected]fee17f72013-02-03 07:47:4180class TestReceiveAlgorithm : public ReceiveAlgorithmInterface {
[email protected]26f3f8e2012-12-13 21:07:1981 public:
[email protected]1e960032013-12-20 19:00:2082 virtual bool GenerateCongestionFeedback(
83 QuicCongestionFeedbackFrame* /*congestion_feedback*/) {
84 return false;
[email protected]26f3f8e2012-12-13 21:07:1985 }
86
[email protected]6ae6e342014-02-06 02:21:4287 MOCK_METHOD3(RecordIncomingPacket,
88 void(QuicByteCount, QuicPacketSequenceNumber, QuicTime));
[email protected]f702d572012-12-04 15:56:2089};
90
[email protected]63534512012-12-23 18:49:0091// Subclass of QuicHttpStream that closes itself when the first piece of data
92// is received.
93class AutoClosingStream : public QuicHttpStream {
94 public:
[email protected]0b2294d32013-08-02 00:46:3695 explicit AutoClosingStream(const base::WeakPtr<QuicClientSession>& session)
96 : QuicHttpStream(session) {
[email protected]63534512012-12-23 18:49:0097 }
98
[email protected]46fadfd2013-02-06 09:40:1699 virtual int OnDataReceived(const char* data, int length) OVERRIDE {
[email protected]63534512012-12-23 18:49:00100 Close(false);
101 return OK;
102 }
103};
104
[email protected]f702d572012-12-04 15:56:20105} // namespace
106
[email protected]24e5bc52013-09-18 15:36:58107class QuicHttpStreamPeer {
108 public:
109 static QuicReliableClientStream* GetQuicReliableClientStream(
110 QuicHttpStream* stream) {
111 return stream->stream_;
112 }
113};
114
[email protected]1e960032013-12-20 19:00:20115class QuicHttpStreamTest : public ::testing::TestWithParam<QuicVersion> {
[email protected]f702d572012-12-04 15:56:20116 protected:
[email protected]1e960032013-12-20 19:00:20117 static const bool kFin = true;
118 static const bool kIncludeVersion = true;
119 static const bool kIncludeCongestionFeedback = true;
120
[email protected]f702d572012-12-04 15:56:20121 // Holds a packet to be written to the wire, and the IO mode that should
122 // be used by the mock socket when performing the write.
123 struct PacketToWrite {
124 PacketToWrite(IoMode mode, QuicEncryptedPacket* packet)
125 : mode(mode),
126 packet(packet) {
127 }
128 IoMode mode;
129 QuicEncryptedPacket* packet;
130 };
131
132 QuicHttpStreamTest()
133 : net_log_(BoundNetLog()),
[email protected]63534512012-12-23 18:49:00134 use_closing_stream_(false),
[email protected]f702d572012-12-04 15:56:20135 read_buffer_(new IOBufferWithSize(4096)),
[email protected]3aa9ca72014-02-27 19:39:43136 connection_id_(2),
[email protected]66ae5962014-05-22 11:13:05137 stream_id_(kClientDataStreamId1),
[email protected]3aa9ca72014-02-27 19:39:43138 maker_(GetParam(), connection_id_),
[email protected]1e960032013-12-20 19:00:20139 random_generator_(0) {
[email protected]f702d572012-12-04 15:56:20140 IPAddressNumber ip;
141 CHECK(ParseIPLiteralToNumber("192.0.2.33", &ip));
142 peer_addr_ = IPEndPoint(ip, 443);
143 self_addr_ = IPEndPoint(ip, 8435);
[email protected]f702d572012-12-04 15:56:20144 }
145
146 ~QuicHttpStreamTest() {
[email protected]4d283b32013-10-17 12:57:27147 session_->CloseSessionOnError(ERR_ABORTED);
[email protected]f702d572012-12-04 15:56:20148 for (size_t i = 0; i < writes_.size(); i++) {
149 delete writes_[i].packet;
150 }
151 }
152
153 // Adds a packet to the list of expected writes.
[email protected]1e960032013-12-20 19:00:20154 void AddWrite(scoped_ptr<QuicEncryptedPacket> packet) {
155 writes_.push_back(PacketToWrite(SYNCHRONOUS, packet.release()));
[email protected]f702d572012-12-04 15:56:20156 }
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
[email protected]1e960032013-12-20 19:00:20167 void ProcessPacket(scoped_ptr<QuicEncryptedPacket> packet) {
168 connection_->ProcessUdpPacket(self_addr_, peer_addr_, *packet);
[email protected]f702d572012-12-04 15:56:20169 }
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]e13201d82012-12-12 05:00:32183 MockUDPClientSocket* socket = new MockUDPClientSocket(socket_data_.get(),
184 net_log_.net_log());
185 socket->Connect(peer_addr_);
[email protected]f702d572012-12-04 15:56:20186 runner_ = new TestTaskRunner(&clock_);
[email protected]fee17f72013-02-03 07:47:41187 send_algorithm_ = new MockSendAlgorithm();
[email protected]1e960032013-12-20 19:00:20188 receive_algorithm_ = new TestReceiveAlgorithm();
[email protected]6ae6e342014-02-06 02:21:42189 EXPECT_CALL(*receive_algorithm_, RecordIncomingPacket(_, _, _)).
[email protected]06ff5152013-08-29 01:03:05190 Times(AnyNumber());
[email protected]efecff92013-09-24 07:49:23191 EXPECT_CALL(*send_algorithm_,
[email protected]ef0da582014-05-09 07:16:30192 OnPacketSent(_, _, _, _, _)).WillRepeatedly(Return(true));
[email protected]48878092013-07-26 14:51:56193 EXPECT_CALL(*send_algorithm_, RetransmissionDelay()).WillRepeatedly(
[email protected]06ff5152013-08-29 01:03:05194 Return(QuicTime::Delta::Zero()));
[email protected]08da9adb2014-04-24 08:33:31195 EXPECT_CALL(*send_algorithm_, GetCongestionWindow()).WillRepeatedly(
196 Return(kMaxPacketSize));
[email protected]77b5d50b2014-05-07 22:48:48197 EXPECT_CALL(*send_algorithm_, TimeUntilSend(_, _, _)).
[email protected]06ff5152013-08-29 01:03:05198 WillRepeatedly(Return(QuicTime::Delta::Zero()));
[email protected]ea825e02013-08-21 18:12:45199 EXPECT_CALL(*send_algorithm_, BandwidthEstimate()).WillRepeatedly(
[email protected]06ff5152013-08-29 01:03:05200 Return(QuicBandwidth::Zero()));
[email protected]62f90632013-11-01 13:07:11201 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)).Times(AnyNumber());
[email protected]2cfc6bb82013-10-27 03:40:44202 helper_.reset(new QuicConnectionHelper(runner_.get(), &clock_,
203 &random_generator_));
[email protected]cbd731e2013-10-24 00:20:39204 writer_.reset(new QuicDefaultPacketWriter(socket));
[email protected]3aa9ca72014-02-27 19:39:43205 connection_ = new TestQuicConnection(SupportedVersions(GetParam()),
206 connection_id_, peer_addr_,
207 helper_.get(), writer_.get());
[email protected]f702d572012-12-04 15:56:20208 connection_->set_visitor(&visitor_);
[email protected]fee17f72013-02-03 07:47:41209 connection_->SetSendAlgorithm(send_algorithm_);
210 connection_->SetReceiveAlgorithm(receive_algorithm_);
[email protected]ef95114d2013-04-17 17:57:01211 crypto_config_.SetDefaults();
[email protected]18ccfdb2013-08-15 00:13:44212 session_.reset(
213 new QuicClientSession(connection_,
[email protected]cbd731e2013-10-24 00:20:39214 scoped_ptr<DatagramClientSocket>(socket),
215 writer_.Pass(), NULL,
[email protected]3021a5f2014-07-23 01:40:40216 &crypto_client_stream_factory_,
[email protected]89f79a42014-03-19 11:30:01217 make_scoped_ptr((QuicServerInfo*)NULL),
[email protected]3021a5f2014-07-23 01:40:40218 QuicServerId(kServerHostname, kServerPort,
219 false, PRIVACY_MODE_DISABLED),
220 DefaultQuicConfig(), &crypto_config_,
[email protected]65768442014-06-06 23:37:03221 base::MessageLoop::current()->
222 message_loop_proxy().get(),
[email protected]ce7bb1412014-05-17 15:51:33223 NULL));
[email protected]3021a5f2014-07-23 01:40:40224 session_->InitializeSession();
[email protected]ed3fc15d2013-03-08 18:37:44225 session_->GetCryptoStream()->CryptoConnect();
[email protected]8ba81212013-05-03 13:11:48226 EXPECT_TRUE(session_->IsCryptoHandshakeConfirmed());
[email protected]6cca996b2013-01-25 07:43:36227 stream_.reset(use_closing_stream_ ?
[email protected]0b2294d32013-08-02 00:46:36228 new AutoClosingStream(session_->GetWeakPtr()) :
229 new QuicHttpStream(session_->GetWeakPtr()));
[email protected]98a9d1252014-04-04 00:43:59230 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(20));
[email protected]6cca996b2013-01-25 07:43:36231 }
232
[email protected]1e960032013-12-20 19:00:20233 void SetRequest(const std::string& method,
234 const std::string& path,
235 RequestPriority priority) {
236 request_headers_ = maker_.GetRequestHeaders(method, "http", path);
[email protected]6cca996b2013-01-25 07:43:36237 }
238
[email protected]1e960032013-12-20 19:00:20239 void SetResponse(const std::string& status, const std::string& body) {
240 response_headers_ = maker_.GetResponseHeaders(status);
[email protected]92bf17c2014-03-03 21:14:03241 response_data_ = body;
[email protected]6cca996b2013-01-25 07:43:36242 }
[email protected]f702d572012-12-04 15:56:20243
[email protected]1e960032013-12-20 19:00:20244 scoped_ptr<QuicEncryptedPacket> ConstructDataPacket(
[email protected]f702d572012-12-04 15:56:20245 QuicPacketSequenceNumber sequence_number,
[email protected]e8ff26842013-03-22 21:02:05246 bool should_include_version,
[email protected]f702d572012-12-04 15:56:20247 bool fin,
248 QuicStreamOffset offset,
249 base::StringPiece data) {
[email protected]1e960032013-12-20 19:00:20250 return maker_.MakeDataPacket(
251 sequence_number, stream_id_, should_include_version, fin, offset, data);
[email protected]f702d572012-12-04 15:56:20252 }
253
[email protected]1e960032013-12-20 19:00:20254 scoped_ptr<QuicEncryptedPacket> ConstructRequestHeadersPacket(
255 QuicPacketSequenceNumber sequence_number,
256 bool fin) {
257 return maker_.MakeRequestHeadersPacket(
258 sequence_number, stream_id_, kIncludeVersion, fin, request_headers_);
259 }
260
261 scoped_ptr<QuicEncryptedPacket> ConstructResponseHeadersPacket(
262 QuicPacketSequenceNumber sequence_number,
263 bool fin) {
264 return maker_.MakeResponseHeadersPacket(
265 sequence_number, stream_id_, !kIncludeVersion, fin, response_headers_);
266 }
267
268 scoped_ptr<QuicEncryptedPacket> ConstructRstStreamPacket(
[email protected]06ff5152013-08-29 01:03:05269 QuicPacketSequenceNumber sequence_number) {
[email protected]1e960032013-12-20 19:00:20270 return maker_.MakeRstPacket(
[email protected]51cc1342014-04-18 23:44:37271 sequence_number, true, stream_id_,
272 AdjustErrorForVersion(QUIC_RST_FLOW_CONTROL_ACCOUNTING, GetParam()));
[email protected]06ff5152013-08-29 01:03:05273 }
274
[email protected]c5e1aca2014-01-30 04:03:04275 scoped_ptr<QuicEncryptedPacket> ConstructAckAndRstStreamPacket(
276 QuicPacketSequenceNumber sequence_number) {
277 return maker_.MakeAckAndRstPacket(
278 sequence_number, !kIncludeVersion, stream_id_, QUIC_STREAM_CANCELLED,
[email protected]08da9adb2014-04-24 08:33:31279 2, 1, !kIncludeCongestionFeedback);
[email protected]c5e1aca2014-01-30 04:03:04280 }
281
[email protected]1e960032013-12-20 19:00:20282 scoped_ptr<QuicEncryptedPacket> ConstructAckPacket(
[email protected]f702d572012-12-04 15:56:20283 QuicPacketSequenceNumber sequence_number,
[email protected]63534512012-12-23 18:49:00284 QuicPacketSequenceNumber largest_received,
285 QuicPacketSequenceNumber least_unacked) {
[email protected]1e960032013-12-20 19:00:20286 return maker_.MakeAckPacket(sequence_number, largest_received,
287 least_unacked, !kIncludeCongestionFeedback);
[email protected]63534512012-12-23 18:49:00288 }
289
[email protected]f702d572012-12-04 15:56:20290 BoundNetLog net_log_;
[email protected]63534512012-12-23 18:49:00291 bool use_closing_stream_;
[email protected]fee17f72013-02-03 07:47:41292 MockSendAlgorithm* send_algorithm_;
293 TestReceiveAlgorithm* receive_algorithm_;
[email protected]f702d572012-12-04 15:56:20294 scoped_refptr<TestTaskRunner> runner_;
[email protected]4356f0f2013-04-07 00:58:17295 scoped_ptr<MockWrite[]> mock_writes_;
[email protected]f702d572012-12-04 15:56:20296 MockClock clock_;
297 TestQuicConnection* connection_;
[email protected]2cfc6bb82013-10-27 03:40:44298 scoped_ptr<QuicConnectionHelper> helper_;
[email protected]f702d572012-12-04 15:56:20299 testing::StrictMock<MockConnectionVisitor> visitor_;
300 scoped_ptr<QuicHttpStream> stream_;
[email protected]cbd731e2013-10-24 00:20:39301 scoped_ptr<QuicDefaultPacketWriter> writer_;
[email protected]f702d572012-12-04 15:56:20302 scoped_ptr<QuicClientSession> session_;
[email protected]ef95114d2013-04-17 17:57:01303 QuicCryptoClientConfig crypto_config_;
[email protected]f702d572012-12-04 15:56:20304 TestCompletionCallback callback_;
305 HttpRequestInfo request_;
306 HttpRequestHeaders headers_;
307 HttpResponseInfo response_;
308 scoped_refptr<IOBufferWithSize> read_buffer_;
[email protected]1e960032013-12-20 19:00:20309 SpdyHeaderBlock request_headers_;
310 SpdyHeaderBlock response_headers_;
[email protected]6cca996b2013-01-25 07:43:36311 std::string request_data_;
312 std::string response_data_;
[email protected]f702d572012-12-04 15:56:20313
314 private:
[email protected]3aa9ca72014-02-27 19:39:43315 const QuicConnectionId connection_id_;
[email protected]1e960032013-12-20 19:00:20316 const QuicStreamId stream_id_;
317 QuicTestPacketMaker maker_;
[email protected]f702d572012-12-04 15:56:20318 IPEndPoint self_addr_;
319 IPEndPoint peer_addr_;
[email protected]457d6952013-12-13 09:24:58320 MockRandom random_generator_;
[email protected]e8ff26842013-03-22 21:02:05321 MockCryptoClientStreamFactory crypto_client_stream_factory_;
[email protected]f702d572012-12-04 15:56:20322 scoped_ptr<StaticSocketDataProvider> socket_data_;
323 std::vector<PacketToWrite> writes_;
324};
325
[email protected]1e960032013-12-20 19:00:20326INSTANTIATE_TEST_CASE_P(Version, QuicHttpStreamTest,
327 ::testing::ValuesIn(QuicSupportedVersions()));
328
329TEST_P(QuicHttpStreamTest, RenewStreamForAuth) {
[email protected]ed3fc15d2013-03-08 18:37:44330 Initialize();
[email protected]f702d572012-12-04 15:56:20331 EXPECT_EQ(NULL, stream_->RenewStreamForAuth());
332}
333
[email protected]1e960032013-12-20 19:00:20334TEST_P(QuicHttpStreamTest, CanFindEndOfResponse) {
[email protected]ed3fc15d2013-03-08 18:37:44335 Initialize();
[email protected]f702d572012-12-04 15:56:20336 EXPECT_TRUE(stream_->CanFindEndOfResponse());
337}
338
[email protected]1e960032013-12-20 19:00:20339TEST_P(QuicHttpStreamTest, IsConnectionReusable) {
[email protected]ed3fc15d2013-03-08 18:37:44340 Initialize();
[email protected]f702d572012-12-04 15:56:20341 EXPECT_FALSE(stream_->IsConnectionReusable());
342}
343
[email protected]1e960032013-12-20 19:00:20344TEST_P(QuicHttpStreamTest, GetRequest) {
345 SetRequest("GET", "/", DEFAULT_PRIORITY);
[email protected]92bf17c2014-03-03 21:14:03346 AddWrite(ConstructRequestHeadersPacket(1, kFin));
[email protected]f702d572012-12-04 15:56:20347 Initialize();
348
349 request_.method = "GET";
350 request_.url = GURL("http://www.google.com/");
351
[email protected]262eec82013-03-19 21:01:36352 EXPECT_EQ(OK, stream_->InitializeStream(&request_, DEFAULT_PRIORITY,
353 net_log_, callback_.callback()));
[email protected]f702d572012-12-04 15:56:20354 EXPECT_EQ(OK, stream_->SendRequest(headers_, &response_,
355 callback_.callback()));
[email protected]f702d572012-12-04 15:56:20356
357 // Ack the request.
[email protected]1e960032013-12-20 19:00:20358 ProcessPacket(ConstructAckPacket(1, 0, 0));
[email protected]f702d572012-12-04 15:56:20359
360 EXPECT_EQ(ERR_IO_PENDING,
361 stream_->ReadResponseHeaders(callback_.callback()));
362
[email protected]1e960032013-12-20 19:00:20363 SetResponse("404 Not Found", std::string());
[email protected]92bf17c2014-03-03 21:14:03364 ProcessPacket(ConstructResponseHeadersPacket(2, kFin));
[email protected]f702d572012-12-04 15:56:20365
366 // Now that the headers have been processed, the callback will return.
367 EXPECT_EQ(OK, callback_.WaitForResult());
[email protected]cadac622013-06-11 16:46:36368 ASSERT_TRUE(response_.headers.get());
[email protected]f702d572012-12-04 15:56:20369 EXPECT_EQ(404, response_.headers->response_code());
370 EXPECT_TRUE(response_.headers->HasHeaderValue("Content-Type", "text/plain"));
[email protected]6ed67432014-06-24 01:53:53371 EXPECT_FALSE(response_.response_time.is_null());
372 EXPECT_FALSE(response_.request_time.is_null());
[email protected]f702d572012-12-04 15:56:20373
374 // There is no body, so this should return immediately.
375 EXPECT_EQ(0, stream_->ReadResponseBody(read_buffer_.get(),
376 read_buffer_->size(),
377 callback_.callback()));
378 EXPECT_TRUE(stream_->IsResponseBodyComplete());
379 EXPECT_TRUE(AtEof());
380}
381
[email protected]3e7dca62013-09-10 16:14:23382// Regression test for http://crbug.com/288128
[email protected]1e960032013-12-20 19:00:20383TEST_P(QuicHttpStreamTest, GetRequestLargeResponse) {
384 SetRequest("GET", "/", DEFAULT_PRIORITY);
[email protected]92bf17c2014-03-03 21:14:03385 AddWrite(ConstructRequestHeadersPacket(1, kFin));
[email protected]3e7dca62013-09-10 16:14:23386 Initialize();
387
388 request_.method = "GET";
389 request_.url = GURL("http://www.google.com/");
390
391 EXPECT_EQ(OK, stream_->InitializeStream(&request_, DEFAULT_PRIORITY,
392 net_log_, callback_.callback()));
393 EXPECT_EQ(OK, stream_->SendRequest(headers_, &response_,
394 callback_.callback()));
[email protected]3e7dca62013-09-10 16:14:23395
396 // Ack the request.
[email protected]1e960032013-12-20 19:00:20397 ProcessPacket(ConstructAckPacket(1, 0, 0));
[email protected]3e7dca62013-09-10 16:14:23398
399 EXPECT_EQ(ERR_IO_PENDING,
400 stream_->ReadResponseHeaders(callback_.callback()));
401
402 SpdyHeaderBlock headers;
403 headers[":status"] = "200 OK";
404 headers[":version"] = "HTTP/1.1";
405 headers["content-type"] = "text/plain";
406 headers["big6"] = std::string(10000, 'x'); // Lots of x's.
407
408 std::string response = SpdyUtils::SerializeUncompressedHeaders(headers);
409 EXPECT_LT(4096u, response.length());
410 stream_->OnDataReceived(response.data(), response.length());
411 stream_->OnClose(QUIC_NO_ERROR);
412
413 // Now that the headers have been processed, the callback will return.
414 EXPECT_EQ(OK, callback_.WaitForResult());
415 ASSERT_TRUE(response_.headers.get());
416 EXPECT_EQ(200, response_.headers->response_code());
417 EXPECT_TRUE(response_.headers->HasHeaderValue("Content-Type", "text/plain"));
418
419 // There is no body, so this should return immediately.
420 EXPECT_EQ(0, stream_->ReadResponseBody(read_buffer_.get(),
421 read_buffer_->size(),
422 callback_.callback()));
423 EXPECT_TRUE(stream_->IsResponseBodyComplete());
424 EXPECT_TRUE(AtEof());
425}
426
[email protected]1e960032013-12-20 19:00:20427TEST_P(QuicHttpStreamTest, SendPostRequest) {
428 SetRequest("POST", "/", DEFAULT_PRIORITY);
[email protected]92bf17c2014-03-03 21:14:03429 AddWrite(ConstructRequestHeadersPacket(1, !kFin));
430 AddWrite(ConstructDataPacket(2, kIncludeVersion, kFin, 0, kUploadData));
[email protected]1e960032013-12-20 19:00:20431 AddWrite(ConstructAckPacket(3, 3, 1));
[email protected]f702d572012-12-04 15:56:20432
433 Initialize();
434
[email protected]b2d26cfd2012-12-11 10:36:06435 ScopedVector<UploadElementReader> element_readers;
436 element_readers.push_back(
437 new UploadBytesElementReader(kUploadData, strlen(kUploadData)));
[email protected]96c77a72013-09-24 09:49:20438 UploadDataStream upload_data_stream(element_readers.Pass(), 0);
[email protected]f702d572012-12-04 15:56:20439 request_.method = "POST";
440 request_.url = GURL("http://www.google.com/");
441 request_.upload_data_stream = &upload_data_stream;
[email protected]4db27d82012-12-20 11:50:24442 ASSERT_EQ(OK, request_.upload_data_stream->Init(CompletionCallback()));
[email protected]f702d572012-12-04 15:56:20443
[email protected]262eec82013-03-19 21:01:36444 EXPECT_EQ(OK, stream_->InitializeStream(&request_, DEFAULT_PRIORITY,
445 net_log_, callback_.callback()));
[email protected]f702d572012-12-04 15:56:20446 EXPECT_EQ(OK, stream_->SendRequest(headers_, &response_,
447 callback_.callback()));
[email protected]f702d572012-12-04 15:56:20448
449 // Ack both packets in the request.
[email protected]1e960032013-12-20 19:00:20450 ProcessPacket(ConstructAckPacket(1, 0, 0));
[email protected]f702d572012-12-04 15:56:20451
452 // Send the response headers (but not the body).
[email protected]1e960032013-12-20 19:00:20453 SetResponse("200 OK", std::string());
[email protected]92bf17c2014-03-03 21:14:03454 ProcessPacket(ConstructResponseHeadersPacket(2, !kFin));
[email protected]f702d572012-12-04 15:56:20455
456 // Since the headers have already arrived, this should return immediately.
457 EXPECT_EQ(OK, stream_->ReadResponseHeaders(callback_.callback()));
[email protected]cadac622013-06-11 16:46:36458 ASSERT_TRUE(response_.headers.get());
[email protected]f702d572012-12-04 15:56:20459 EXPECT_EQ(200, response_.headers->response_code());
460 EXPECT_TRUE(response_.headers->HasHeaderValue("Content-Type", "text/plain"));
461
462 // Send the response body.
463 const char kResponseBody[] = "Hello world!";
[email protected]92bf17c2014-03-03 21:14:03464 ProcessPacket(ConstructDataPacket(3, false, kFin, 0, kResponseBody));
[email protected]f702d572012-12-04 15:56:20465 // Since the body has already arrived, this should return immediately.
466 EXPECT_EQ(static_cast<int>(strlen(kResponseBody)),
467 stream_->ReadResponseBody(read_buffer_.get(), read_buffer_->size(),
468 callback_.callback()));
469
470 EXPECT_TRUE(stream_->IsResponseBodyComplete());
471 EXPECT_TRUE(AtEof());
472}
473
[email protected]1e960032013-12-20 19:00:20474TEST_P(QuicHttpStreamTest, SendChunkedPostRequest) {
475 SetRequest("POST", "/", DEFAULT_PRIORITY);
[email protected]c9e49a02013-02-26 05:56:47476 size_t chunk_size = strlen(kUploadData);
[email protected]92bf17c2014-03-03 21:14:03477 AddWrite(ConstructRequestHeadersPacket(1, !kFin));
478 AddWrite(ConstructDataPacket(2, kIncludeVersion, !kFin, 0, kUploadData));
479 AddWrite(ConstructDataPacket(3, kIncludeVersion, kFin, chunk_size,
480 kUploadData));
[email protected]1e960032013-12-20 19:00:20481 AddWrite(ConstructAckPacket(4, 3, 1));
[email protected]c9e49a02013-02-26 05:56:47482 Initialize();
483
484 UploadDataStream upload_data_stream(UploadDataStream::CHUNKED, 0);
485 upload_data_stream.AppendChunk(kUploadData, chunk_size, false);
486
487 request_.method = "POST";
488 request_.url = GURL("http://www.google.com/");
489 request_.upload_data_stream = &upload_data_stream;
490 ASSERT_EQ(OK, request_.upload_data_stream->Init(CompletionCallback()));
491
[email protected]262eec82013-03-19 21:01:36492 ASSERT_EQ(OK, stream_->InitializeStream(&request_, DEFAULT_PRIORITY,
493 net_log_, callback_.callback()));
[email protected]c9e49a02013-02-26 05:56:47494 ASSERT_EQ(ERR_IO_PENDING, stream_->SendRequest(headers_, &response_,
495 callback_.callback()));
[email protected]c9e49a02013-02-26 05:56:47496
497 upload_data_stream.AppendChunk(kUploadData, chunk_size, true);
498
499 // Ack both packets in the request.
[email protected]1e960032013-12-20 19:00:20500 ProcessPacket(ConstructAckPacket(1, 0, 0));
[email protected]c9e49a02013-02-26 05:56:47501
502 // Send the response headers (but not the body).
[email protected]1e960032013-12-20 19:00:20503 SetResponse("200 OK", std::string());
[email protected]92bf17c2014-03-03 21:14:03504 ProcessPacket(ConstructResponseHeadersPacket(2, !kFin));
[email protected]c9e49a02013-02-26 05:56:47505
506 // Since the headers have already arrived, this should return immediately.
507 ASSERT_EQ(OK, stream_->ReadResponseHeaders(callback_.callback()));
[email protected]cadac622013-06-11 16:46:36508 ASSERT_TRUE(response_.headers.get());
[email protected]c9e49a02013-02-26 05:56:47509 EXPECT_EQ(200, response_.headers->response_code());
510 EXPECT_TRUE(response_.headers->HasHeaderValue("Content-Type", "text/plain"));
511
512 // Send the response body.
513 const char kResponseBody[] = "Hello world!";
[email protected]1e960032013-12-20 19:00:20514 ProcessPacket(ConstructDataPacket(3, false, kFin, response_data_.length(),
515 kResponseBody));
[email protected]c9e49a02013-02-26 05:56:47516
517 // Since the body has already arrived, this should return immediately.
518 ASSERT_EQ(static_cast<int>(strlen(kResponseBody)),
519 stream_->ReadResponseBody(read_buffer_.get(), read_buffer_->size(),
520 callback_.callback()));
521
522 EXPECT_TRUE(stream_->IsResponseBodyComplete());
523 EXPECT_TRUE(AtEof());
524}
525
[email protected]1e960032013-12-20 19:00:20526TEST_P(QuicHttpStreamTest, DestroyedEarly) {
527 SetRequest("GET", "/", DEFAULT_PRIORITY);
[email protected]92bf17c2014-03-03 21:14:03528 AddWrite(ConstructRequestHeadersPacket(1, kFin));
[email protected]c5e1aca2014-01-30 04:03:04529 AddWrite(ConstructAckAndRstStreamPacket(2));
[email protected]63534512012-12-23 18:49:00530 use_closing_stream_ = true;
531 Initialize();
532
533 request_.method = "GET";
534 request_.url = GURL("http://www.google.com/");
535
[email protected]262eec82013-03-19 21:01:36536 EXPECT_EQ(OK, stream_->InitializeStream(&request_, DEFAULT_PRIORITY,
537 net_log_, callback_.callback()));
[email protected]63534512012-12-23 18:49:00538 EXPECT_EQ(OK, stream_->SendRequest(headers_, &response_,
[email protected]24e5bc52013-09-18 15:36:58539 callback_.callback()));
[email protected]63534512012-12-23 18:49:00540
541 // Ack the request.
[email protected]1e960032013-12-20 19:00:20542 ProcessPacket(ConstructAckPacket(1, 0, 0));
[email protected]63534512012-12-23 18:49:00543 EXPECT_EQ(ERR_IO_PENDING,
544 stream_->ReadResponseHeaders(callback_.callback()));
545
546 // Send the response with a body.
[email protected]1e960032013-12-20 19:00:20547 SetResponse("404 OK", "hello world!");
[email protected]63534512012-12-23 18:49:00548 // In the course of processing this packet, the QuicHttpStream close itself.
[email protected]92bf17c2014-03-03 21:14:03549 ProcessPacket(ConstructResponseHeadersPacket(2, kFin));
[email protected]63534512012-12-23 18:49:00550
551 EXPECT_TRUE(AtEof());
552}
553
[email protected]1e960032013-12-20 19:00:20554TEST_P(QuicHttpStreamTest, Priority) {
555 SetRequest("GET", "/", MEDIUM);
[email protected]92bf17c2014-03-03 21:14:03556 AddWrite(ConstructRequestHeadersPacket(1, kFin));
[email protected]c5e1aca2014-01-30 04:03:04557 AddWrite(ConstructAckAndRstStreamPacket(2));
[email protected]24e5bc52013-09-18 15:36:58558 use_closing_stream_ = true;
559 Initialize();
560
561 request_.method = "GET";
562 request_.url = GURL("http://www.google.com/");
563
564 EXPECT_EQ(OK, stream_->InitializeStream(&request_, MEDIUM,
565 net_log_, callback_.callback()));
566
567 // Check that priority is highest.
568 QuicReliableClientStream* reliable_stream =
569 QuicHttpStreamPeer::GetQuicReliableClientStream(stream_.get());
570 DCHECK(reliable_stream);
[email protected]9f0dcd4e2014-01-16 15:58:14571 DCHECK_EQ(QuicWriteBlockedList::kHighestPriority,
[email protected]24e5bc52013-09-18 15:36:58572 reliable_stream->EffectivePriority());
573
574 EXPECT_EQ(OK, stream_->SendRequest(headers_, &response_,
575 callback_.callback()));
[email protected]24e5bc52013-09-18 15:36:58576
577 // Check that priority has now dropped back to MEDIUM.
578 DCHECK_EQ(MEDIUM, ConvertQuicPriorityToRequestPriority(
579 reliable_stream->EffectivePriority()));
580
581 // Ack the request.
[email protected]1e960032013-12-20 19:00:20582 ProcessPacket(ConstructAckPacket(1, 0, 0));
[email protected]24e5bc52013-09-18 15:36:58583 EXPECT_EQ(ERR_IO_PENDING,
584 stream_->ReadResponseHeaders(callback_.callback()));
585
586 // Send the response with a body.
[email protected]1e960032013-12-20 19:00:20587 SetResponse("404 OK", "hello world!");
[email protected]24e5bc52013-09-18 15:36:58588 // In the course of processing this packet, the QuicHttpStream close itself.
[email protected]92bf17c2014-03-03 21:14:03589 ProcessPacket(ConstructResponseHeadersPacket(2, kFin));
[email protected]24e5bc52013-09-18 15:36:58590
591 EXPECT_TRUE(AtEof());
592}
593
[email protected]e1cca9a2013-09-20 17:14:44594// Regression test for http://crbug.com/294870
[email protected]1e960032013-12-20 19:00:20595TEST_P(QuicHttpStreamTest, CheckPriorityWithNoDelegate) {
596 SetRequest("GET", "/", MEDIUM);
[email protected]e1cca9a2013-09-20 17:14:44597 use_closing_stream_ = true;
[email protected]459a7402014-02-10 12:58:52598
[email protected]08da9adb2014-04-24 08:33:31599 AddWrite(ConstructRstStreamPacket(1));
[email protected]459a7402014-02-10 12:58:52600
[email protected]e1cca9a2013-09-20 17:14:44601 Initialize();
602
603 request_.method = "GET";
604 request_.url = GURL("http://www.google.com/");
605
606 EXPECT_EQ(OK, stream_->InitializeStream(&request_, MEDIUM,
607 net_log_, callback_.callback()));
608
609 // Check that priority is highest.
610 QuicReliableClientStream* reliable_stream =
611 QuicHttpStreamPeer::GetQuicReliableClientStream(stream_.get());
612 DCHECK(reliable_stream);
613 QuicReliableClientStream::Delegate* delegate = reliable_stream->GetDelegate();
614 DCHECK(delegate);
[email protected]9f0dcd4e2014-01-16 15:58:14615 DCHECK_EQ(QuicWriteBlockedList::kHighestPriority,
[email protected]e1cca9a2013-09-20 17:14:44616 reliable_stream->EffectivePriority());
617
618 // Set Delegate to NULL and make sure EffectivePriority returns highest
619 // priority.
620 reliable_stream->SetDelegate(NULL);
[email protected]9f0dcd4e2014-01-16 15:58:14621 DCHECK_EQ(QuicWriteBlockedList::kHighestPriority,
[email protected]e1cca9a2013-09-20 17:14:44622 reliable_stream->EffectivePriority());
623 reliable_stream->SetDelegate(delegate);
624}
625
[email protected]f702d572012-12-04 15:56:20626} // namespace test
[email protected]f702d572012-12-04 15:56:20627} // namespace net