blob: 7fef0c5d4847af5c7a243d77c4e74be2242647e9 [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]f702d572012-12-04 15:56:2019#include "net/quic/quic_client_session.h"
20#include "net/quic/quic_connection.h"
21#include "net/quic/quic_connection_helper.h"
[email protected]cbd731e2013-10-24 00:20:3922#include "net/quic/quic_default_packet_writer.h"
[email protected]24e5bc52013-09-18 15:36:5823#include "net/quic/quic_http_utils.h"
24#include "net/quic/quic_reliable_client_stream.h"
[email protected]9f0dcd4e2014-01-16 15:58:1425#include "net/quic/quic_write_blocked_list.h"
[email protected]3e7dca62013-09-10 16:14:2326#include "net/quic/spdy_utils.h"
[email protected]f702d572012-12-04 15:56:2027#include "net/quic/test_tools/mock_clock.h"
[email protected]e8ff26842013-03-22 21:02:0528#include "net/quic/test_tools/mock_crypto_client_stream_factory.h"
[email protected]9db443912013-02-25 05:27:0329#include "net/quic/test_tools/mock_random.h"
[email protected]b1f287d2012-12-22 17:25:3930#include "net/quic/test_tools/quic_connection_peer.h"
[email protected]1e960032013-12-20 19:00:2031#include "net/quic/test_tools/quic_test_packet_maker.h"
[email protected]f702d572012-12-04 15:56:2032#include "net/quic/test_tools/quic_test_utils.h"
33#include "net/quic/test_tools/test_task_runner.h"
34#include "net/socket/socket_test_util.h"
[email protected]6cca996b2013-01-25 07:43:3635#include "net/spdy/spdy_frame_builder.h"
36#include "net/spdy/spdy_framer.h"
37#include "net/spdy/spdy_http_utils.h"
38#include "net/spdy/spdy_protocol.h"
[email protected]f702d572012-12-04 15:56:2039#include "testing/gmock/include/gmock/gmock.h"
40#include "testing/gtest/include/gtest/gtest.h"
41
42using testing::_;
[email protected]06ff5152013-08-29 01:03:0543using testing::AnyNumber;
44using testing::Return;
[email protected]f702d572012-12-04 15:56:2045
46namespace net {
[email protected]f702d572012-12-04 15:56:2047namespace test {
[email protected]f702d572012-12-04 15:56:2048namespace {
49
50const char kUploadData[] = "hello world!";
51
52class TestQuicConnection : public QuicConnection {
53 public:
[email protected]1e960032013-12-20 19:00:2054 TestQuicConnection(const QuicVersionVector& versions,
[email protected]3aa9ca72014-02-27 19:39:4355 QuicConnectionId connection_id,
[email protected]f702d572012-12-04 15:56:2056 IPEndPoint address,
[email protected]cbd731e2013-10-24 00:20:3957 QuicConnectionHelper* helper,
58 QuicPacketWriter* writer)
[email protected]3aa9ca72014-02-27 19:39:4359 : QuicConnection(connection_id, address, helper, writer, false,
60 versions) {
[email protected]f702d572012-12-04 15:56:2061 }
62
[email protected]fee17f72013-02-03 07:47:4163 void SetSendAlgorithm(SendAlgorithmInterface* send_algorithm) {
64 QuicConnectionPeer::SetSendAlgorithm(this, send_algorithm);
[email protected]f702d572012-12-04 15:56:2065 }
[email protected]26f3f8e2012-12-13 21:07:1966
[email protected]fee17f72013-02-03 07:47:4167 void SetReceiveAlgorithm(ReceiveAlgorithmInterface* receive_algorithm) {
68 QuicConnectionPeer::SetReceiveAlgorithm(this, receive_algorithm);
[email protected]26f3f8e2012-12-13 21:07:1969 }
70};
71
[email protected]fee17f72013-02-03 07:47:4172class TestReceiveAlgorithm : public ReceiveAlgorithmInterface {
[email protected]26f3f8e2012-12-13 21:07:1973 public:
[email protected]1e960032013-12-20 19:00:2074 virtual bool GenerateCongestionFeedback(
75 QuicCongestionFeedbackFrame* /*congestion_feedback*/) {
76 return false;
[email protected]26f3f8e2012-12-13 21:07:1977 }
78
[email protected]6ae6e342014-02-06 02:21:4279 MOCK_METHOD3(RecordIncomingPacket,
80 void(QuicByteCount, QuicPacketSequenceNumber, QuicTime));
[email protected]f702d572012-12-04 15:56:2081};
82
[email protected]63534512012-12-23 18:49:0083// Subclass of QuicHttpStream that closes itself when the first piece of data
84// is received.
85class AutoClosingStream : public QuicHttpStream {
86 public:
[email protected]0b2294d32013-08-02 00:46:3687 explicit AutoClosingStream(const base::WeakPtr<QuicClientSession>& session)
88 : QuicHttpStream(session) {
[email protected]63534512012-12-23 18:49:0089 }
90
[email protected]46fadfd2013-02-06 09:40:1691 virtual int OnDataReceived(const char* data, int length) OVERRIDE {
[email protected]63534512012-12-23 18:49:0092 Close(false);
93 return OK;
94 }
95};
96
[email protected]f702d572012-12-04 15:56:2097} // namespace
98
[email protected]24e5bc52013-09-18 15:36:5899class QuicHttpStreamPeer {
100 public:
101 static QuicReliableClientStream* GetQuicReliableClientStream(
102 QuicHttpStream* stream) {
103 return stream->stream_;
104 }
105};
106
[email protected]1e960032013-12-20 19:00:20107class QuicHttpStreamTest : public ::testing::TestWithParam<QuicVersion> {
[email protected]f702d572012-12-04 15:56:20108 protected:
[email protected]1e960032013-12-20 19:00:20109 static const bool kFin = true;
110 static const bool kIncludeVersion = true;
111 static const bool kIncludeCongestionFeedback = true;
112
[email protected]f702d572012-12-04 15:56:20113 // Holds a packet to be written to the wire, and the IO mode that should
114 // be used by the mock socket when performing the write.
115 struct PacketToWrite {
116 PacketToWrite(IoMode mode, QuicEncryptedPacket* packet)
117 : mode(mode),
118 packet(packet) {
119 }
120 IoMode mode;
121 QuicEncryptedPacket* packet;
122 };
123
124 QuicHttpStreamTest()
125 : net_log_(BoundNetLog()),
[email protected]63534512012-12-23 18:49:00126 use_closing_stream_(false),
[email protected]f702d572012-12-04 15:56:20127 read_buffer_(new IOBufferWithSize(4096)),
[email protected]3aa9ca72014-02-27 19:39:43128 connection_id_(2),
[email protected]92bf17c2014-03-03 21:14:03129 stream_id_(5),
[email protected]3aa9ca72014-02-27 19:39:43130 maker_(GetParam(), connection_id_),
[email protected]1e960032013-12-20 19:00:20131 random_generator_(0) {
[email protected]f702d572012-12-04 15:56:20132 IPAddressNumber ip;
133 CHECK(ParseIPLiteralToNumber("192.0.2.33", &ip));
134 peer_addr_ = IPEndPoint(ip, 443);
135 self_addr_ = IPEndPoint(ip, 8435);
[email protected]f702d572012-12-04 15:56:20136 }
137
138 ~QuicHttpStreamTest() {
[email protected]4d283b32013-10-17 12:57:27139 session_->CloseSessionOnError(ERR_ABORTED);
[email protected]f702d572012-12-04 15:56:20140 for (size_t i = 0; i < writes_.size(); i++) {
141 delete writes_[i].packet;
142 }
143 }
144
145 // Adds a packet to the list of expected writes.
[email protected]1e960032013-12-20 19:00:20146 void AddWrite(scoped_ptr<QuicEncryptedPacket> packet) {
147 writes_.push_back(PacketToWrite(SYNCHRONOUS, packet.release()));
[email protected]f702d572012-12-04 15:56:20148 }
149
150 // Returns the packet to be written at position |pos|.
151 QuicEncryptedPacket* GetWrite(size_t pos) {
152 return writes_[pos].packet;
153 }
154
155 bool AtEof() {
156 return socket_data_->at_read_eof() && socket_data_->at_write_eof();
157 }
158
[email protected]1e960032013-12-20 19:00:20159 void ProcessPacket(scoped_ptr<QuicEncryptedPacket> packet) {
160 connection_->ProcessUdpPacket(self_addr_, peer_addr_, *packet);
[email protected]f702d572012-12-04 15:56:20161 }
162
163 // Configures the test fixture to use the list of expected writes.
164 void Initialize() {
165 mock_writes_.reset(new MockWrite[writes_.size()]);
166 for (size_t i = 0; i < writes_.size(); i++) {
167 mock_writes_[i] = MockWrite(writes_[i].mode,
168 writes_[i].packet->data(),
169 writes_[i].packet->length());
170 };
171
172 socket_data_.reset(new StaticSocketDataProvider(NULL, 0, mock_writes_.get(),
173 writes_.size()));
174
[email protected]e13201d82012-12-12 05:00:32175 MockUDPClientSocket* socket = new MockUDPClientSocket(socket_data_.get(),
176 net_log_.net_log());
177 socket->Connect(peer_addr_);
[email protected]f702d572012-12-04 15:56:20178 runner_ = new TestTaskRunner(&clock_);
[email protected]fee17f72013-02-03 07:47:41179 send_algorithm_ = new MockSendAlgorithm();
[email protected]1e960032013-12-20 19:00:20180 receive_algorithm_ = new TestReceiveAlgorithm();
[email protected]6ae6e342014-02-06 02:21:42181 EXPECT_CALL(*receive_algorithm_, RecordIncomingPacket(_, _, _)).
[email protected]06ff5152013-08-29 01:03:05182 Times(AnyNumber());
[email protected]efecff92013-09-24 07:49:23183 EXPECT_CALL(*send_algorithm_,
[email protected]c0680202013-12-18 04:52:34184 OnPacketSent(_, _, _, _, _)).WillRepeatedly(Return(true));
[email protected]48878092013-07-26 14:51:56185 EXPECT_CALL(*send_algorithm_, RetransmissionDelay()).WillRepeatedly(
[email protected]06ff5152013-08-29 01:03:05186 Return(QuicTime::Delta::Zero()));
[email protected]575cce62013-08-03 02:06:43187 EXPECT_CALL(*send_algorithm_, TimeUntilSend(_, _, _, _)).
[email protected]06ff5152013-08-29 01:03:05188 WillRepeatedly(Return(QuicTime::Delta::Zero()));
[email protected]ea825e02013-08-21 18:12:45189 EXPECT_CALL(*send_algorithm_, BandwidthEstimate()).WillRepeatedly(
[email protected]06ff5152013-08-29 01:03:05190 Return(QuicBandwidth::Zero()));
[email protected]62f90632013-11-01 13:07:11191 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)).Times(AnyNumber());
[email protected]2cfc6bb82013-10-27 03:40:44192 helper_.reset(new QuicConnectionHelper(runner_.get(), &clock_,
193 &random_generator_));
[email protected]cbd731e2013-10-24 00:20:39194 writer_.reset(new QuicDefaultPacketWriter(socket));
[email protected]3aa9ca72014-02-27 19:39:43195 connection_ = new TestQuicConnection(SupportedVersions(GetParam()),
196 connection_id_, peer_addr_,
197 helper_.get(), writer_.get());
[email protected]f702d572012-12-04 15:56:20198 connection_->set_visitor(&visitor_);
[email protected]fee17f72013-02-03 07:47:41199 connection_->SetSendAlgorithm(send_algorithm_);
200 connection_->SetReceiveAlgorithm(receive_algorithm_);
[email protected]ef95114d2013-04-17 17:57:01201 crypto_config_.SetDefaults();
[email protected]18ccfdb2013-08-15 00:13:44202 session_.reset(
203 new QuicClientSession(connection_,
[email protected]cbd731e2013-10-24 00:20:39204 scoped_ptr<DatagramClientSocket>(socket),
205 writer_.Pass(), NULL,
[email protected]18ccfdb2013-08-15 00:13:44206 &crypto_client_stream_factory_,
207 "www.google.com", DefaultQuicConfig(),
208 &crypto_config_, NULL));
[email protected]ed3fc15d2013-03-08 18:37:44209 session_->GetCryptoStream()->CryptoConnect();
[email protected]8ba81212013-05-03 13:11:48210 EXPECT_TRUE(session_->IsCryptoHandshakeConfirmed());
[email protected]6cca996b2013-01-25 07:43:36211 stream_.reset(use_closing_stream_ ?
[email protected]0b2294d32013-08-02 00:46:36212 new AutoClosingStream(session_->GetWeakPtr()) :
213 new QuicHttpStream(session_->GetWeakPtr()));
[email protected]6cca996b2013-01-25 07:43:36214 }
215
[email protected]1e960032013-12-20 19:00:20216 void SetRequest(const std::string& method,
217 const std::string& path,
218 RequestPriority priority) {
219 request_headers_ = maker_.GetRequestHeaders(method, "http", path);
[email protected]6cca996b2013-01-25 07:43:36220 }
221
[email protected]1e960032013-12-20 19:00:20222 void SetResponse(const std::string& status, const std::string& body) {
223 response_headers_ = maker_.GetResponseHeaders(status);
[email protected]92bf17c2014-03-03 21:14:03224 response_data_ = body;
[email protected]6cca996b2013-01-25 07:43:36225 }
[email protected]f702d572012-12-04 15:56:20226
[email protected]1e960032013-12-20 19:00:20227 scoped_ptr<QuicEncryptedPacket> ConstructDataPacket(
[email protected]f702d572012-12-04 15:56:20228 QuicPacketSequenceNumber sequence_number,
[email protected]e8ff26842013-03-22 21:02:05229 bool should_include_version,
[email protected]f702d572012-12-04 15:56:20230 bool fin,
231 QuicStreamOffset offset,
232 base::StringPiece data) {
[email protected]1e960032013-12-20 19:00:20233 return maker_.MakeDataPacket(
234 sequence_number, stream_id_, should_include_version, fin, offset, data);
[email protected]f702d572012-12-04 15:56:20235 }
236
[email protected]1e960032013-12-20 19:00:20237 scoped_ptr<QuicEncryptedPacket> ConstructRequestHeadersPacket(
238 QuicPacketSequenceNumber sequence_number,
239 bool fin) {
240 return maker_.MakeRequestHeadersPacket(
241 sequence_number, stream_id_, kIncludeVersion, fin, request_headers_);
242 }
243
244 scoped_ptr<QuicEncryptedPacket> ConstructResponseHeadersPacket(
245 QuicPacketSequenceNumber sequence_number,
246 bool fin) {
247 return maker_.MakeResponseHeadersPacket(
248 sequence_number, stream_id_, !kIncludeVersion, fin, response_headers_);
249 }
250
251 scoped_ptr<QuicEncryptedPacket> ConstructRstStreamPacket(
[email protected]06ff5152013-08-29 01:03:05252 QuicPacketSequenceNumber sequence_number) {
[email protected]1e960032013-12-20 19:00:20253 return maker_.MakeRstPacket(
[email protected]459a7402014-02-10 12:58:52254 sequence_number, true, stream_id_, QUIC_STREAM_NO_ERROR);
[email protected]06ff5152013-08-29 01:03:05255 }
256
[email protected]c5e1aca2014-01-30 04:03:04257 scoped_ptr<QuicEncryptedPacket> ConstructAckAndRstStreamPacket(
258 QuicPacketSequenceNumber sequence_number) {
259 return maker_.MakeAckAndRstPacket(
260 sequence_number, !kIncludeVersion, stream_id_, QUIC_STREAM_CANCELLED,
261 1, 1, !kIncludeCongestionFeedback);
262 }
263
[email protected]1e960032013-12-20 19:00:20264 scoped_ptr<QuicEncryptedPacket> ConstructAckPacket(
[email protected]f702d572012-12-04 15:56:20265 QuicPacketSequenceNumber sequence_number,
[email protected]63534512012-12-23 18:49:00266 QuicPacketSequenceNumber largest_received,
267 QuicPacketSequenceNumber least_unacked) {
[email protected]1e960032013-12-20 19:00:20268 return maker_.MakeAckPacket(sequence_number, largest_received,
269 least_unacked, !kIncludeCongestionFeedback);
[email protected]63534512012-12-23 18:49:00270 }
271
[email protected]f702d572012-12-04 15:56:20272 BoundNetLog net_log_;
[email protected]63534512012-12-23 18:49:00273 bool use_closing_stream_;
[email protected]fee17f72013-02-03 07:47:41274 MockSendAlgorithm* send_algorithm_;
275 TestReceiveAlgorithm* receive_algorithm_;
[email protected]f702d572012-12-04 15:56:20276 scoped_refptr<TestTaskRunner> runner_;
[email protected]4356f0f2013-04-07 00:58:17277 scoped_ptr<MockWrite[]> mock_writes_;
[email protected]f702d572012-12-04 15:56:20278 MockClock clock_;
279 TestQuicConnection* connection_;
[email protected]2cfc6bb82013-10-27 03:40:44280 scoped_ptr<QuicConnectionHelper> helper_;
[email protected]f702d572012-12-04 15:56:20281 testing::StrictMock<MockConnectionVisitor> visitor_;
282 scoped_ptr<QuicHttpStream> stream_;
[email protected]cbd731e2013-10-24 00:20:39283 scoped_ptr<QuicDefaultPacketWriter> writer_;
[email protected]f702d572012-12-04 15:56:20284 scoped_ptr<QuicClientSession> session_;
[email protected]ef95114d2013-04-17 17:57:01285 QuicCryptoClientConfig crypto_config_;
[email protected]f702d572012-12-04 15:56:20286 TestCompletionCallback callback_;
287 HttpRequestInfo request_;
288 HttpRequestHeaders headers_;
289 HttpResponseInfo response_;
290 scoped_refptr<IOBufferWithSize> read_buffer_;
[email protected]1e960032013-12-20 19:00:20291 SpdyHeaderBlock request_headers_;
292 SpdyHeaderBlock response_headers_;
[email protected]6cca996b2013-01-25 07:43:36293 std::string request_data_;
294 std::string response_data_;
[email protected]f702d572012-12-04 15:56:20295
296 private:
[email protected]3aa9ca72014-02-27 19:39:43297 const QuicConnectionId connection_id_;
[email protected]1e960032013-12-20 19:00:20298 const QuicStreamId stream_id_;
299 QuicTestPacketMaker maker_;
[email protected]f702d572012-12-04 15:56:20300 IPEndPoint self_addr_;
301 IPEndPoint peer_addr_;
[email protected]457d6952013-12-13 09:24:58302 MockRandom random_generator_;
[email protected]e8ff26842013-03-22 21:02:05303 MockCryptoClientStreamFactory crypto_client_stream_factory_;
[email protected]f702d572012-12-04 15:56:20304 scoped_ptr<StaticSocketDataProvider> socket_data_;
305 std::vector<PacketToWrite> writes_;
306};
307
[email protected]1e960032013-12-20 19:00:20308INSTANTIATE_TEST_CASE_P(Version, QuicHttpStreamTest,
309 ::testing::ValuesIn(QuicSupportedVersions()));
310
311TEST_P(QuicHttpStreamTest, RenewStreamForAuth) {
[email protected]ed3fc15d2013-03-08 18:37:44312 Initialize();
[email protected]f702d572012-12-04 15:56:20313 EXPECT_EQ(NULL, stream_->RenewStreamForAuth());
314}
315
[email protected]1e960032013-12-20 19:00:20316TEST_P(QuicHttpStreamTest, CanFindEndOfResponse) {
[email protected]ed3fc15d2013-03-08 18:37:44317 Initialize();
[email protected]f702d572012-12-04 15:56:20318 EXPECT_TRUE(stream_->CanFindEndOfResponse());
319}
320
[email protected]1e960032013-12-20 19:00:20321TEST_P(QuicHttpStreamTest, IsConnectionReusable) {
[email protected]ed3fc15d2013-03-08 18:37:44322 Initialize();
[email protected]f702d572012-12-04 15:56:20323 EXPECT_FALSE(stream_->IsConnectionReusable());
324}
325
[email protected]1e960032013-12-20 19:00:20326TEST_P(QuicHttpStreamTest, GetRequest) {
327 SetRequest("GET", "/", DEFAULT_PRIORITY);
[email protected]92bf17c2014-03-03 21:14:03328 AddWrite(ConstructRequestHeadersPacket(1, kFin));
[email protected]f702d572012-12-04 15:56:20329 Initialize();
330
331 request_.method = "GET";
332 request_.url = GURL("http://www.google.com/");
333
[email protected]262eec82013-03-19 21:01:36334 EXPECT_EQ(OK, stream_->InitializeStream(&request_, DEFAULT_PRIORITY,
335 net_log_, callback_.callback()));
[email protected]f702d572012-12-04 15:56:20336 EXPECT_EQ(OK, stream_->SendRequest(headers_, &response_,
337 callback_.callback()));
338 EXPECT_EQ(&response_, stream_->GetResponseInfo());
339
340 // Ack the request.
[email protected]1e960032013-12-20 19:00:20341 ProcessPacket(ConstructAckPacket(1, 0, 0));
[email protected]f702d572012-12-04 15:56:20342
343 EXPECT_EQ(ERR_IO_PENDING,
344 stream_->ReadResponseHeaders(callback_.callback()));
345
[email protected]1e960032013-12-20 19:00:20346 SetResponse("404 Not Found", std::string());
[email protected]92bf17c2014-03-03 21:14:03347 ProcessPacket(ConstructResponseHeadersPacket(2, kFin));
[email protected]f702d572012-12-04 15:56:20348
349 // Now that the headers have been processed, the callback will return.
350 EXPECT_EQ(OK, callback_.WaitForResult());
[email protected]cadac622013-06-11 16:46:36351 ASSERT_TRUE(response_.headers.get());
[email protected]f702d572012-12-04 15:56:20352 EXPECT_EQ(404, response_.headers->response_code());
353 EXPECT_TRUE(response_.headers->HasHeaderValue("Content-Type", "text/plain"));
354
355 // There is no body, so this should return immediately.
356 EXPECT_EQ(0, stream_->ReadResponseBody(read_buffer_.get(),
357 read_buffer_->size(),
358 callback_.callback()));
359 EXPECT_TRUE(stream_->IsResponseBodyComplete());
360 EXPECT_TRUE(AtEof());
361}
362
[email protected]3e7dca62013-09-10 16:14:23363// Regression test for http://crbug.com/288128
[email protected]1e960032013-12-20 19:00:20364TEST_P(QuicHttpStreamTest, GetRequestLargeResponse) {
365 SetRequest("GET", "/", DEFAULT_PRIORITY);
[email protected]92bf17c2014-03-03 21:14:03366 AddWrite(ConstructRequestHeadersPacket(1, kFin));
[email protected]3e7dca62013-09-10 16:14:23367 Initialize();
368
369 request_.method = "GET";
370 request_.url = GURL("http://www.google.com/");
371
372 EXPECT_EQ(OK, stream_->InitializeStream(&request_, DEFAULT_PRIORITY,
373 net_log_, callback_.callback()));
374 EXPECT_EQ(OK, stream_->SendRequest(headers_, &response_,
375 callback_.callback()));
376 EXPECT_EQ(&response_, stream_->GetResponseInfo());
377
378 // Ack the request.
[email protected]1e960032013-12-20 19:00:20379 ProcessPacket(ConstructAckPacket(1, 0, 0));
[email protected]3e7dca62013-09-10 16:14:23380
381 EXPECT_EQ(ERR_IO_PENDING,
382 stream_->ReadResponseHeaders(callback_.callback()));
383
384 SpdyHeaderBlock headers;
385 headers[":status"] = "200 OK";
386 headers[":version"] = "HTTP/1.1";
387 headers["content-type"] = "text/plain";
388 headers["big6"] = std::string(10000, 'x'); // Lots of x's.
389
390 std::string response = SpdyUtils::SerializeUncompressedHeaders(headers);
391 EXPECT_LT(4096u, response.length());
392 stream_->OnDataReceived(response.data(), response.length());
393 stream_->OnClose(QUIC_NO_ERROR);
394
395 // Now that the headers have been processed, the callback will return.
396 EXPECT_EQ(OK, callback_.WaitForResult());
397 ASSERT_TRUE(response_.headers.get());
398 EXPECT_EQ(200, response_.headers->response_code());
399 EXPECT_TRUE(response_.headers->HasHeaderValue("Content-Type", "text/plain"));
400
401 // There is no body, so this should return immediately.
402 EXPECT_EQ(0, stream_->ReadResponseBody(read_buffer_.get(),
403 read_buffer_->size(),
404 callback_.callback()));
405 EXPECT_TRUE(stream_->IsResponseBodyComplete());
406 EXPECT_TRUE(AtEof());
407}
408
[email protected]1e960032013-12-20 19:00:20409TEST_P(QuicHttpStreamTest, SendPostRequest) {
410 SetRequest("POST", "/", DEFAULT_PRIORITY);
[email protected]92bf17c2014-03-03 21:14:03411 AddWrite(ConstructRequestHeadersPacket(1, !kFin));
412 AddWrite(ConstructDataPacket(2, kIncludeVersion, kFin, 0, kUploadData));
[email protected]1e960032013-12-20 19:00:20413 AddWrite(ConstructAckPacket(3, 3, 1));
[email protected]f702d572012-12-04 15:56:20414
415 Initialize();
416
[email protected]b2d26cfd2012-12-11 10:36:06417 ScopedVector<UploadElementReader> element_readers;
418 element_readers.push_back(
419 new UploadBytesElementReader(kUploadData, strlen(kUploadData)));
[email protected]96c77a72013-09-24 09:49:20420 UploadDataStream upload_data_stream(element_readers.Pass(), 0);
[email protected]f702d572012-12-04 15:56:20421 request_.method = "POST";
422 request_.url = GURL("http://www.google.com/");
423 request_.upload_data_stream = &upload_data_stream;
[email protected]4db27d82012-12-20 11:50:24424 ASSERT_EQ(OK, request_.upload_data_stream->Init(CompletionCallback()));
[email protected]f702d572012-12-04 15:56:20425
[email protected]262eec82013-03-19 21:01:36426 EXPECT_EQ(OK, stream_->InitializeStream(&request_, DEFAULT_PRIORITY,
427 net_log_, callback_.callback()));
[email protected]f702d572012-12-04 15:56:20428 EXPECT_EQ(OK, stream_->SendRequest(headers_, &response_,
429 callback_.callback()));
430 EXPECT_EQ(&response_, stream_->GetResponseInfo());
431
432 // Ack both packets in the request.
[email protected]1e960032013-12-20 19:00:20433 ProcessPacket(ConstructAckPacket(1, 0, 0));
[email protected]f702d572012-12-04 15:56:20434
435 // Send the response headers (but not the body).
[email protected]1e960032013-12-20 19:00:20436 SetResponse("200 OK", std::string());
[email protected]92bf17c2014-03-03 21:14:03437 ProcessPacket(ConstructResponseHeadersPacket(2, !kFin));
[email protected]f702d572012-12-04 15:56:20438
439 // Since the headers have already arrived, this should return immediately.
440 EXPECT_EQ(OK, stream_->ReadResponseHeaders(callback_.callback()));
[email protected]cadac622013-06-11 16:46:36441 ASSERT_TRUE(response_.headers.get());
[email protected]f702d572012-12-04 15:56:20442 EXPECT_EQ(200, response_.headers->response_code());
443 EXPECT_TRUE(response_.headers->HasHeaderValue("Content-Type", "text/plain"));
444
445 // Send the response body.
446 const char kResponseBody[] = "Hello world!";
[email protected]92bf17c2014-03-03 21:14:03447 ProcessPacket(ConstructDataPacket(3, false, kFin, 0, kResponseBody));
[email protected]f702d572012-12-04 15:56:20448 // Since the body has already arrived, this should return immediately.
449 EXPECT_EQ(static_cast<int>(strlen(kResponseBody)),
450 stream_->ReadResponseBody(read_buffer_.get(), read_buffer_->size(),
451 callback_.callback()));
452
453 EXPECT_TRUE(stream_->IsResponseBodyComplete());
454 EXPECT_TRUE(AtEof());
455}
456
[email protected]1e960032013-12-20 19:00:20457TEST_P(QuicHttpStreamTest, SendChunkedPostRequest) {
458 SetRequest("POST", "/", DEFAULT_PRIORITY);
[email protected]c9e49a02013-02-26 05:56:47459 size_t chunk_size = strlen(kUploadData);
[email protected]92bf17c2014-03-03 21:14:03460 AddWrite(ConstructRequestHeadersPacket(1, !kFin));
461 AddWrite(ConstructDataPacket(2, kIncludeVersion, !kFin, 0, kUploadData));
462 AddWrite(ConstructDataPacket(3, kIncludeVersion, kFin, chunk_size,
463 kUploadData));
[email protected]1e960032013-12-20 19:00:20464 AddWrite(ConstructAckPacket(4, 3, 1));
[email protected]c9e49a02013-02-26 05:56:47465 Initialize();
466
467 UploadDataStream upload_data_stream(UploadDataStream::CHUNKED, 0);
468 upload_data_stream.AppendChunk(kUploadData, chunk_size, false);
469
470 request_.method = "POST";
471 request_.url = GURL("http://www.google.com/");
472 request_.upload_data_stream = &upload_data_stream;
473 ASSERT_EQ(OK, request_.upload_data_stream->Init(CompletionCallback()));
474
[email protected]262eec82013-03-19 21:01:36475 ASSERT_EQ(OK, stream_->InitializeStream(&request_, DEFAULT_PRIORITY,
476 net_log_, callback_.callback()));
[email protected]c9e49a02013-02-26 05:56:47477 ASSERT_EQ(ERR_IO_PENDING, stream_->SendRequest(headers_, &response_,
478 callback_.callback()));
479 EXPECT_EQ(&response_, stream_->GetResponseInfo());
480
481 upload_data_stream.AppendChunk(kUploadData, chunk_size, true);
482
483 // Ack both packets in the request.
[email protected]1e960032013-12-20 19:00:20484 ProcessPacket(ConstructAckPacket(1, 0, 0));
[email protected]c9e49a02013-02-26 05:56:47485
486 // Send the response headers (but not the body).
[email protected]1e960032013-12-20 19:00:20487 SetResponse("200 OK", std::string());
[email protected]92bf17c2014-03-03 21:14:03488 ProcessPacket(ConstructResponseHeadersPacket(2, !kFin));
[email protected]c9e49a02013-02-26 05:56:47489
490 // Since the headers have already arrived, this should return immediately.
491 ASSERT_EQ(OK, stream_->ReadResponseHeaders(callback_.callback()));
[email protected]cadac622013-06-11 16:46:36492 ASSERT_TRUE(response_.headers.get());
[email protected]c9e49a02013-02-26 05:56:47493 EXPECT_EQ(200, response_.headers->response_code());
494 EXPECT_TRUE(response_.headers->HasHeaderValue("Content-Type", "text/plain"));
495
496 // Send the response body.
497 const char kResponseBody[] = "Hello world!";
[email protected]1e960032013-12-20 19:00:20498 ProcessPacket(ConstructDataPacket(3, false, kFin, response_data_.length(),
499 kResponseBody));
[email protected]c9e49a02013-02-26 05:56:47500
501 // Since the body has already arrived, this should return immediately.
502 ASSERT_EQ(static_cast<int>(strlen(kResponseBody)),
503 stream_->ReadResponseBody(read_buffer_.get(), read_buffer_->size(),
504 callback_.callback()));
505
506 EXPECT_TRUE(stream_->IsResponseBodyComplete());
507 EXPECT_TRUE(AtEof());
508}
509
[email protected]1e960032013-12-20 19:00:20510TEST_P(QuicHttpStreamTest, DestroyedEarly) {
511 SetRequest("GET", "/", DEFAULT_PRIORITY);
[email protected]92bf17c2014-03-03 21:14:03512 AddWrite(ConstructRequestHeadersPacket(1, kFin));
[email protected]c5e1aca2014-01-30 04:03:04513 AddWrite(ConstructAckAndRstStreamPacket(2));
[email protected]63534512012-12-23 18:49:00514 use_closing_stream_ = true;
515 Initialize();
516
517 request_.method = "GET";
518 request_.url = GURL("http://www.google.com/");
519
[email protected]262eec82013-03-19 21:01:36520 EXPECT_EQ(OK, stream_->InitializeStream(&request_, DEFAULT_PRIORITY,
521 net_log_, callback_.callback()));
[email protected]63534512012-12-23 18:49:00522 EXPECT_EQ(OK, stream_->SendRequest(headers_, &response_,
[email protected]24e5bc52013-09-18 15:36:58523 callback_.callback()));
[email protected]63534512012-12-23 18:49:00524 EXPECT_EQ(&response_, stream_->GetResponseInfo());
525
526 // Ack the request.
[email protected]1e960032013-12-20 19:00:20527 ProcessPacket(ConstructAckPacket(1, 0, 0));
[email protected]63534512012-12-23 18:49:00528 EXPECT_EQ(ERR_IO_PENDING,
529 stream_->ReadResponseHeaders(callback_.callback()));
530
531 // Send the response with a body.
[email protected]1e960032013-12-20 19:00:20532 SetResponse("404 OK", "hello world!");
[email protected]63534512012-12-23 18:49:00533 // In the course of processing this packet, the QuicHttpStream close itself.
[email protected]92bf17c2014-03-03 21:14:03534 ProcessPacket(ConstructResponseHeadersPacket(2, kFin));
[email protected]63534512012-12-23 18:49:00535
536 EXPECT_TRUE(AtEof());
537}
538
[email protected]1e960032013-12-20 19:00:20539TEST_P(QuicHttpStreamTest, Priority) {
540 SetRequest("GET", "/", MEDIUM);
[email protected]92bf17c2014-03-03 21:14:03541 AddWrite(ConstructRequestHeadersPacket(1, kFin));
[email protected]c5e1aca2014-01-30 04:03:04542 AddWrite(ConstructAckAndRstStreamPacket(2));
[email protected]24e5bc52013-09-18 15:36:58543 use_closing_stream_ = true;
544 Initialize();
545
546 request_.method = "GET";
547 request_.url = GURL("http://www.google.com/");
548
549 EXPECT_EQ(OK, stream_->InitializeStream(&request_, MEDIUM,
550 net_log_, callback_.callback()));
551
552 // Check that priority is highest.
553 QuicReliableClientStream* reliable_stream =
554 QuicHttpStreamPeer::GetQuicReliableClientStream(stream_.get());
555 DCHECK(reliable_stream);
[email protected]9f0dcd4e2014-01-16 15:58:14556 DCHECK_EQ(QuicWriteBlockedList::kHighestPriority,
[email protected]24e5bc52013-09-18 15:36:58557 reliable_stream->EffectivePriority());
558
559 EXPECT_EQ(OK, stream_->SendRequest(headers_, &response_,
560 callback_.callback()));
561 EXPECT_EQ(&response_, stream_->GetResponseInfo());
562
563 // Check that priority has now dropped back to MEDIUM.
564 DCHECK_EQ(MEDIUM, ConvertQuicPriorityToRequestPriority(
565 reliable_stream->EffectivePriority()));
566
567 // Ack the request.
[email protected]1e960032013-12-20 19:00:20568 ProcessPacket(ConstructAckPacket(1, 0, 0));
[email protected]24e5bc52013-09-18 15:36:58569 EXPECT_EQ(ERR_IO_PENDING,
570 stream_->ReadResponseHeaders(callback_.callback()));
571
572 // Send the response with a body.
[email protected]1e960032013-12-20 19:00:20573 SetResponse("404 OK", "hello world!");
[email protected]24e5bc52013-09-18 15:36:58574 // In the course of processing this packet, the QuicHttpStream close itself.
[email protected]92bf17c2014-03-03 21:14:03575 ProcessPacket(ConstructResponseHeadersPacket(2, kFin));
[email protected]24e5bc52013-09-18 15:36:58576
577 EXPECT_TRUE(AtEof());
578}
579
[email protected]e1cca9a2013-09-20 17:14:44580// Regression test for http://crbug.com/294870
[email protected]1e960032013-12-20 19:00:20581TEST_P(QuicHttpStreamTest, CheckPriorityWithNoDelegate) {
582 SetRequest("GET", "/", MEDIUM);
[email protected]e1cca9a2013-09-20 17:14:44583 use_closing_stream_ = true;
[email protected]459a7402014-02-10 12:58:52584
585 if (GetParam() > QUIC_VERSION_13) {
586 AddWrite(ConstructRstStreamPacket(1));
587 }
588
[email protected]e1cca9a2013-09-20 17:14:44589 Initialize();
590
591 request_.method = "GET";
592 request_.url = GURL("http://www.google.com/");
593
594 EXPECT_EQ(OK, stream_->InitializeStream(&request_, MEDIUM,
595 net_log_, callback_.callback()));
596
597 // Check that priority is highest.
598 QuicReliableClientStream* reliable_stream =
599 QuicHttpStreamPeer::GetQuicReliableClientStream(stream_.get());
600 DCHECK(reliable_stream);
601 QuicReliableClientStream::Delegate* delegate = reliable_stream->GetDelegate();
602 DCHECK(delegate);
[email protected]9f0dcd4e2014-01-16 15:58:14603 DCHECK_EQ(QuicWriteBlockedList::kHighestPriority,
[email protected]e1cca9a2013-09-20 17:14:44604 reliable_stream->EffectivePriority());
605
606 // Set Delegate to NULL and make sure EffectivePriority returns highest
607 // priority.
608 reliable_stream->SetDelegate(NULL);
[email protected]9f0dcd4e2014-01-16 15:58:14609 DCHECK_EQ(QuicWriteBlockedList::kHighestPriority,
[email protected]e1cca9a2013-09-20 17:14:44610 reliable_stream->EffectivePriority());
611 reliable_stream->SetDelegate(delegate);
612}
613
[email protected]f702d572012-12-04 15:56:20614} // namespace test
[email protected]f702d572012-12-04 15:56:20615} // namespace net