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.
5 #include "net/quic/quic_http_stream.h"
9 #include "base/thread_task_runner_handle.h"
10 #include "net/base/chunked_upload_data_stream.h"
11 #include "net/base/elements_upload_data_stream.h"
12 #include "net/base/net_errors.h"
13 #include "net/base/test_completion_callback.h"
14 #include "net/base/upload_bytes_element_reader.h"
15 #include "net/http/http_response_headers.h"
16 #include "net/http/transport_security_state.h"
17 #include "net/quic/congestion_control/send_algorithm_interface.h"
18 #include "net/quic/crypto/crypto_protocol.h"
19 #include "net/quic/crypto/quic_decrypter.h"
20 #include "net/quic/crypto/quic_encrypter.h"
21 #include "net/quic/crypto/quic_server_info.h"
22 #include "net/quic/quic_chromium_client_session.h"
23 #include "net/quic/quic_connection.h"
24 #include "net/quic/quic_connection_helper.h"
25 #include "net/quic/quic_default_packet_writer.h"
26 #include "net/quic/quic_flags.h"
27 #include "net/quic/quic_http_utils.h"
28 #include "net/quic/quic_reliable_client_stream.h"
29 #include "net/quic/quic_write_blocked_list.h"
30 #include "net/quic/spdy_utils.h"
31 #include "net/quic/test_tools/mock_clock.h"
32 #include "net/quic/test_tools/mock_crypto_client_stream_factory.h"
33 #include "net/quic/test_tools/mock_random.h"
34 #include "net/quic/test_tools/quic_connection_peer.h"
35 #include "net/quic/test_tools/quic_test_packet_maker.h"
36 #include "net/quic/test_tools/quic_test_utils.h"
37 #include "net/quic/test_tools/test_task_runner.h"
38 #include "net/socket/socket_test_util.h"
39 #include "net/spdy/spdy_frame_builder.h"
40 #include "net/spdy/spdy_framer.h"
41 #include "net/spdy/spdy_http_utils.h"
42 #include "net/spdy/spdy_protocol.h"
43 #include "testing/gmock/include/gmock/gmock.h"
44 #include "testing/gtest/include/gtest/gtest.h"
47 using testing::AnyNumber
;
48 using testing::Return
;
54 const char kUploadData
[] = "Really nifty data!";
55 const char kDefaultServerHostName
[] = "www.google.com";
56 const uint16 kDefaultServerPort
= 80;
58 class TestQuicConnection
: public QuicConnection
{
60 TestQuicConnection(const QuicVersionVector
& versions
,
61 QuicConnectionId connection_id
,
63 QuicConnectionHelper
* helper
,
64 const QuicConnection::PacketWriterFactory
& writer_factory
)
65 : QuicConnection(connection_id
,
69 true /* owns_writer */,
70 Perspective::IS_CLIENT
,
71 false /* is_secure */,
74 void SetSendAlgorithm(SendAlgorithmInterface
* send_algorithm
) {
75 QuicConnectionPeer::SetSendAlgorithm(this, send_algorithm
);
79 // Subclass of QuicHttpStream that closes itself when the first piece of data
81 class AutoClosingStream
: public QuicHttpStream
{
83 explicit AutoClosingStream(
84 const base::WeakPtr
<QuicChromiumClientSession
>& session
)
85 : QuicHttpStream(session
) {}
87 void OnHeadersAvailable(const SpdyHeaderBlock
& headers
) override
{
91 void OnDataAvailable() override
{ Close(false); }
94 class TestPacketWriterFactory
: public QuicConnection::PacketWriterFactory
{
96 explicit TestPacketWriterFactory(DatagramClientSocket
* socket
)
98 ~TestPacketWriterFactory() override
{}
100 QuicPacketWriter
* Create(QuicConnection
* connection
) const override
{
101 return new QuicDefaultPacketWriter(socket_
);
105 DatagramClientSocket
* socket_
;
110 class QuicHttpStreamPeer
{
112 static QuicReliableClientStream
* GetQuicReliableClientStream(
113 QuicHttpStream
* stream
) {
114 return stream
->stream_
;
118 class QuicHttpStreamTest
: public ::testing::TestWithParam
<QuicVersion
> {
120 static const bool kFin
= true;
121 static const bool kIncludeVersion
= true;
122 static const bool kIncludeCongestionFeedback
= true;
124 // Holds a packet to be written to the wire, and the IO mode that should
125 // be used by the mock socket when performing the write.
126 struct PacketToWrite
{
127 PacketToWrite(IoMode mode
, QuicEncryptedPacket
* packet
)
132 QuicEncryptedPacket
* packet
;
136 : net_log_(BoundNetLog()),
137 use_closing_stream_(false),
138 read_buffer_(new IOBufferWithSize(4096)),
140 stream_id_(kClientDataStreamId1
),
141 maker_(GetParam(), connection_id_
, &clock_
, kDefaultServerHostName
),
142 random_generator_(0) {
144 CHECK(ParseIPLiteralToNumber("192.0.2.33", &ip
));
145 peer_addr_
= IPEndPoint(ip
, 443);
146 self_addr_
= IPEndPoint(ip
, 8435);
147 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(20));
150 ~QuicHttpStreamTest() {
151 session_
->CloseSessionOnError(ERR_ABORTED
, QUIC_INTERNAL_ERROR
);
152 for (size_t i
= 0; i
< writes_
.size(); i
++) {
153 delete writes_
[i
].packet
;
157 // Adds a packet to the list of expected writes.
158 void AddWrite(scoped_ptr
<QuicEncryptedPacket
> packet
) {
159 writes_
.push_back(PacketToWrite(SYNCHRONOUS
, packet
.release()));
162 // Returns the packet to be written at position |pos|.
163 QuicEncryptedPacket
* GetWrite(size_t pos
) {
164 return writes_
[pos
].packet
;
168 return socket_data_
->AllReadDataConsumed() &&
169 socket_data_
->AllWriteDataConsumed();
172 void ProcessPacket(scoped_ptr
<QuicEncryptedPacket
> packet
) {
173 connection_
->ProcessUdpPacket(self_addr_
, peer_addr_
, *packet
);
176 // Configures the test fixture to use the list of expected writes.
178 mock_writes_
.reset(new MockWrite
[writes_
.size()]);
179 for (size_t i
= 0; i
< writes_
.size(); i
++) {
180 mock_writes_
[i
] = MockWrite(writes_
[i
].mode
,
181 writes_
[i
].packet
->data(),
182 writes_
[i
].packet
->length());
185 socket_data_
.reset(new StaticSocketDataProvider(
186 nullptr, 0, mock_writes_
.get(), writes_
.size()));
188 MockUDPClientSocket
* socket
= new MockUDPClientSocket(socket_data_
.get(),
190 socket
->Connect(peer_addr_
);
191 runner_
= new TestTaskRunner(&clock_
);
192 send_algorithm_
= new MockSendAlgorithm();
193 EXPECT_CALL(*send_algorithm_
, InRecovery()).WillRepeatedly(Return(false));
194 EXPECT_CALL(*send_algorithm_
, InSlowStart()).WillRepeatedly(Return(false));
195 EXPECT_CALL(*send_algorithm_
,
196 OnPacketSent(_
, _
, _
, _
, _
)).WillRepeatedly(Return(true));
197 EXPECT_CALL(*send_algorithm_
, RetransmissionDelay()).WillRepeatedly(
198 Return(QuicTime::Delta::Zero()));
199 EXPECT_CALL(*send_algorithm_
, GetCongestionWindow()).WillRepeatedly(
200 Return(kMaxPacketSize
));
201 EXPECT_CALL(*send_algorithm_
, TimeUntilSend(_
, _
, _
)).
202 WillRepeatedly(Return(QuicTime::Delta::Zero()));
203 EXPECT_CALL(*send_algorithm_
, BandwidthEstimate()).WillRepeatedly(
204 Return(QuicBandwidth::Zero()));
205 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
)).Times(AnyNumber());
206 helper_
.reset(new QuicConnectionHelper(runner_
.get(), &clock_
,
207 &random_generator_
));
208 TestPacketWriterFactory
writer_factory(socket
);
209 connection_
= new TestQuicConnection(SupportedVersions(GetParam()),
210 connection_id_
, peer_addr_
,
211 helper_
.get(), writer_factory
);
212 connection_
->set_visitor(&visitor_
);
213 connection_
->SetSendAlgorithm(send_algorithm_
);
214 session_
.reset(new QuicChromiumClientSession(
215 connection_
, scoped_ptr
<DatagramClientSocket
>(socket
),
216 /*stream_factory=*/nullptr, &crypto_client_stream_factory_
,
217 &transport_security_state_
, make_scoped_ptr((QuicServerInfo
*)nullptr),
218 QuicServerId(kDefaultServerHostName
, kDefaultServerPort
,
219 /*is_secure=*/false, PRIVACY_MODE_DISABLED
),
220 /*cert_verify_flags=*/0, DefaultQuicConfig(), &crypto_config_
,
221 "CONNECTION_UNKNOWN", base::TimeTicks::Now(),
222 base::ThreadTaskRunnerHandle::Get().get(), nullptr));
223 session_
->Initialize();
224 session_
->GetCryptoStream()->CryptoConnect();
225 EXPECT_TRUE(session_
->IsCryptoHandshakeConfirmed());
226 stream_
.reset(use_closing_stream_
?
227 new AutoClosingStream(session_
->GetWeakPtr()) :
228 new QuicHttpStream(session_
->GetWeakPtr()));
231 void SetRequest(const std::string
& method
,
232 const std::string
& path
,
233 RequestPriority priority
) {
234 request_headers_
= maker_
.GetRequestHeaders(method
, "http", path
);
237 void SetResponse(const std::string
& status
, const std::string
& body
) {
238 response_headers_
= maker_
.GetResponseHeaders(status
);
239 response_data_
= body
;
242 scoped_ptr
<QuicEncryptedPacket
> ConstructDataPacket(
243 QuicPacketSequenceNumber sequence_number
,
244 bool should_include_version
,
246 QuicStreamOffset offset
,
247 base::StringPiece data
) {
248 return maker_
.MakeDataPacket(sequence_number
, stream_id_
,
249 should_include_version
, fin
, offset
, data
);
252 scoped_ptr
<QuicEncryptedPacket
> ConstructRequestHeadersPacket(
253 QuicPacketSequenceNumber sequence_number
,
255 RequestPriority request_priority
) {
256 QuicPriority priority
=
257 ConvertRequestPriorityToQuicPriority(request_priority
);
258 return maker_
.MakeRequestHeadersPacket(sequence_number
, stream_id_
,
259 kIncludeVersion
, fin
, priority
,
263 scoped_ptr
<QuicEncryptedPacket
> ConstructResponseHeadersPacket(
264 QuicPacketSequenceNumber sequence_number
,
266 return maker_
.MakeResponseHeadersPacket(
267 sequence_number
, stream_id_
, !kIncludeVersion
, fin
, response_headers_
);
270 scoped_ptr
<QuicEncryptedPacket
> ConstructRstStreamPacket(
271 QuicPacketSequenceNumber sequence_number
) {
272 return maker_
.MakeRstPacket(
273 sequence_number
, true, stream_id_
,
274 AdjustErrorForVersion(QUIC_RST_ACKNOWLEDGEMENT
, GetParam()));
277 scoped_ptr
<QuicEncryptedPacket
> ConstructRstStreamCancelledPacket(
278 QuicPacketSequenceNumber sequence_number
) {
279 return maker_
.MakeRstPacket(sequence_number
, !kIncludeVersion
, stream_id_
,
280 QUIC_STREAM_CANCELLED
);
283 scoped_ptr
<QuicEncryptedPacket
> ConstructAckAndRstStreamPacket(
284 QuicPacketSequenceNumber sequence_number
) {
285 return maker_
.MakeAckAndRstPacket(
286 sequence_number
, !kIncludeVersion
, stream_id_
, QUIC_STREAM_CANCELLED
,
287 2, 1, !kIncludeCongestionFeedback
);
290 scoped_ptr
<QuicEncryptedPacket
> ConstructAckPacket(
291 QuicPacketSequenceNumber sequence_number
,
292 QuicPacketSequenceNumber largest_received
,
293 QuicPacketSequenceNumber least_unacked
) {
294 return maker_
.MakeAckPacket(sequence_number
, largest_received
,
295 least_unacked
, !kIncludeCongestionFeedback
);
298 BoundNetLog net_log_
;
299 bool use_closing_stream_
;
300 MockSendAlgorithm
* send_algorithm_
;
301 scoped_refptr
<TestTaskRunner
> runner_
;
302 scoped_ptr
<MockWrite
[]> mock_writes_
;
304 TestQuicConnection
* connection_
;
305 scoped_ptr
<QuicConnectionHelper
> helper_
;
306 testing::StrictMock
<MockConnectionVisitor
> visitor_
;
307 scoped_ptr
<QuicHttpStream
> stream_
;
308 TransportSecurityState transport_security_state_
;
309 scoped_ptr
<QuicChromiumClientSession
> session_
;
310 QuicCryptoClientConfig crypto_config_
;
311 TestCompletionCallback callback_
;
312 HttpRequestInfo request_
;
313 HttpRequestHeaders headers_
;
314 HttpResponseInfo response_
;
315 scoped_refptr
<IOBufferWithSize
> read_buffer_
;
316 SpdyHeaderBlock request_headers_
;
317 SpdyHeaderBlock response_headers_
;
318 std::string request_data_
;
319 std::string response_data_
;
322 const QuicConnectionId connection_id_
;
323 const QuicStreamId stream_id_
;
324 QuicTestPacketMaker maker_
;
325 IPEndPoint self_addr_
;
326 IPEndPoint peer_addr_
;
327 MockRandom random_generator_
;
328 MockCryptoClientStreamFactory crypto_client_stream_factory_
;
329 scoped_ptr
<StaticSocketDataProvider
> socket_data_
;
330 std::vector
<PacketToWrite
> writes_
;
333 INSTANTIATE_TEST_CASE_P(Version
, QuicHttpStreamTest
,
334 ::testing::ValuesIn(QuicSupportedVersions()));
336 TEST_P(QuicHttpStreamTest
, RenewStreamForAuth
) {
338 EXPECT_EQ(nullptr, stream_
->RenewStreamForAuth());
341 TEST_P(QuicHttpStreamTest
, CanFindEndOfResponse
) {
343 EXPECT_TRUE(stream_
->CanFindEndOfResponse());
346 TEST_P(QuicHttpStreamTest
, IsConnectionReusable
) {
348 EXPECT_FALSE(stream_
->IsConnectionReusable());
351 TEST_P(QuicHttpStreamTest
, GetRequest
) {
352 SetRequest("GET", "/", DEFAULT_PRIORITY
);
353 AddWrite(ConstructRequestHeadersPacket(1, kFin
, DEFAULT_PRIORITY
));
356 request_
.method
= "GET";
357 request_
.url
= GURL("http://www.google.com/");
359 EXPECT_EQ(OK
, stream_
->InitializeStream(&request_
, DEFAULT_PRIORITY
,
360 net_log_
, callback_
.callback()));
361 EXPECT_EQ(OK
, stream_
->SendRequest(headers_
, &response_
,
362 callback_
.callback()));
365 ProcessPacket(ConstructAckPacket(1, 0, 0));
367 EXPECT_EQ(ERR_IO_PENDING
,
368 stream_
->ReadResponseHeaders(callback_
.callback()));
370 SetResponse("404 Not Found", std::string());
371 ProcessPacket(ConstructResponseHeadersPacket(2, kFin
));
373 // Now that the headers have been processed, the callback will return.
374 EXPECT_EQ(OK
, callback_
.WaitForResult());
375 ASSERT_TRUE(response_
.headers
.get());
376 EXPECT_EQ(404, response_
.headers
->response_code());
377 EXPECT_TRUE(response_
.headers
->HasHeaderValue("Content-Type", "text/plain"));
378 EXPECT_FALSE(response_
.response_time
.is_null());
379 EXPECT_FALSE(response_
.request_time
.is_null());
381 // There is no body, so this should return immediately.
382 EXPECT_EQ(0, stream_
->ReadResponseBody(read_buffer_
.get(),
383 read_buffer_
->size(),
384 callback_
.callback()));
385 EXPECT_TRUE(stream_
->IsResponseBodyComplete());
386 EXPECT_TRUE(AtEof());
389 // Regression test for http://crbug.com/288128
390 TEST_P(QuicHttpStreamTest
, GetRequestLargeResponse
) {
391 SetRequest("GET", "/", DEFAULT_PRIORITY
);
392 AddWrite(ConstructRequestHeadersPacket(1, kFin
, DEFAULT_PRIORITY
));
395 request_
.method
= "GET";
396 request_
.url
= GURL("http://www.google.com/");
398 EXPECT_EQ(OK
, stream_
->InitializeStream(&request_
, DEFAULT_PRIORITY
,
399 net_log_
, callback_
.callback()));
400 EXPECT_EQ(OK
, stream_
->SendRequest(headers_
, &response_
,
401 callback_
.callback()));
404 ProcessPacket(ConstructAckPacket(1, 0, 0));
406 EXPECT_EQ(ERR_IO_PENDING
,
407 stream_
->ReadResponseHeaders(callback_
.callback()));
409 SpdyHeaderBlock headers
;
410 headers
[":status"] = "200 OK";
411 headers
[":version"] = "HTTP/1.1";
412 headers
["content-type"] = "text/plain";
413 headers
["big6"] = std::string(1000, 'x'); // Lots of x's.
415 response_headers_
= headers
;
416 ProcessPacket(ConstructResponseHeadersPacket(2, kFin
));
418 // Now that the headers have been processed, the callback will return.
419 EXPECT_EQ(OK
, callback_
.WaitForResult());
420 ASSERT_TRUE(response_
.headers
.get());
421 EXPECT_EQ(200, response_
.headers
->response_code());
422 EXPECT_TRUE(response_
.headers
->HasHeaderValue("Content-Type", "text/plain"));
424 // There is no body, so this should return immediately.
425 EXPECT_EQ(0, stream_
->ReadResponseBody(read_buffer_
.get(),
426 read_buffer_
->size(),
427 callback_
.callback()));
428 EXPECT_TRUE(stream_
->IsResponseBodyComplete());
429 EXPECT_TRUE(AtEof());
432 // Regression test for http://crbug.com/409101
433 TEST_P(QuicHttpStreamTest
, SessionClosedBeforeSendRequest
) {
434 SetRequest("GET", "/", DEFAULT_PRIORITY
);
437 request_
.method
= "GET";
438 request_
.url
= GURL("http://www.google.com/");
440 EXPECT_EQ(OK
, stream_
->InitializeStream(&request_
, DEFAULT_PRIORITY
,
441 net_log_
, callback_
.callback()));
443 session_
->connection()->CloseConnection(QUIC_NO_ERROR
, true);
445 EXPECT_EQ(ERR_CONNECTION_CLOSED
,
446 stream_
->SendRequest(headers_
, &response_
,
447 callback_
.callback()));
450 // Regression test for http://crbug.com/409871
451 TEST_P(QuicHttpStreamTest
, SessionClosedBeforeReadResponseHeaders
) {
452 SetRequest("GET", "/", DEFAULT_PRIORITY
);
453 AddWrite(ConstructRequestHeadersPacket(1, kFin
, DEFAULT_PRIORITY
));
456 request_
.method
= "GET";
457 request_
.url
= GURL("http://www.google.com/");
459 EXPECT_EQ(OK
, stream_
->InitializeStream(&request_
, DEFAULT_PRIORITY
,
460 net_log_
, callback_
.callback()));
462 EXPECT_EQ(OK
, stream_
->SendRequest(headers_
, &response_
,
463 callback_
.callback()));
465 session_
->connection()->CloseConnection(QUIC_NO_ERROR
, true);
467 EXPECT_NE(OK
, stream_
->ReadResponseHeaders(callback_
.callback()));
470 TEST_P(QuicHttpStreamTest
, SendPostRequest
) {
471 SetRequest("POST", "/", DEFAULT_PRIORITY
);
472 AddWrite(ConstructRequestHeadersPacket(1, !kFin
, DEFAULT_PRIORITY
));
473 AddWrite(ConstructDataPacket(2, kIncludeVersion
, kFin
, 0, kUploadData
));
474 AddWrite(ConstructAckPacket(3, 3, 1));
478 ScopedVector
<UploadElementReader
> element_readers
;
479 element_readers
.push_back(
480 new UploadBytesElementReader(kUploadData
, strlen(kUploadData
)));
481 ElementsUploadDataStream
upload_data_stream(element_readers
.Pass(), 0);
482 request_
.method
= "POST";
483 request_
.url
= GURL("http://www.google.com/");
484 request_
.upload_data_stream
= &upload_data_stream
;
485 ASSERT_EQ(OK
, request_
.upload_data_stream
->Init(CompletionCallback()));
487 EXPECT_EQ(OK
, stream_
->InitializeStream(&request_
, DEFAULT_PRIORITY
,
488 net_log_
, callback_
.callback()));
489 EXPECT_EQ(OK
, stream_
->SendRequest(headers_
, &response_
,
490 callback_
.callback()));
492 // Ack both packets in the request.
493 ProcessPacket(ConstructAckPacket(1, 0, 0));
495 // Send the response headers (but not the body).
496 SetResponse("200 OK", std::string());
497 ProcessPacket(ConstructResponseHeadersPacket(2, !kFin
));
499 // The headers have arrived, but they are delivered asynchronously.
500 EXPECT_EQ(ERR_IO_PENDING
, stream_
->ReadResponseHeaders(callback_
.callback()));
501 EXPECT_EQ(OK
, callback_
.WaitForResult());
502 ASSERT_TRUE(response_
.headers
.get());
503 EXPECT_EQ(200, response_
.headers
->response_code());
504 EXPECT_TRUE(response_
.headers
->HasHeaderValue("Content-Type", "text/plain"));
506 // Send the response body.
507 const char kResponseBody
[] = "Hello world!";
508 ProcessPacket(ConstructDataPacket(3, false, kFin
, 0, kResponseBody
));
509 // Since the body has already arrived, this should return immediately.
510 EXPECT_EQ(static_cast<int>(strlen(kResponseBody
)),
511 stream_
->ReadResponseBody(read_buffer_
.get(), read_buffer_
->size(),
512 callback_
.callback()));
514 EXPECT_TRUE(stream_
->IsResponseBodyComplete());
515 EXPECT_TRUE(AtEof());
518 TEST_P(QuicHttpStreamTest
, SendChunkedPostRequest
) {
519 SetRequest("POST", "/", DEFAULT_PRIORITY
);
520 size_t chunk_size
= strlen(kUploadData
);
521 AddWrite(ConstructRequestHeadersPacket(1, !kFin
, DEFAULT_PRIORITY
));
522 AddWrite(ConstructDataPacket(2, kIncludeVersion
, !kFin
, 0, kUploadData
));
523 AddWrite(ConstructDataPacket(3, kIncludeVersion
, kFin
, chunk_size
,
525 AddWrite(ConstructAckPacket(4, 3, 1));
528 ChunkedUploadDataStream
upload_data_stream(0);
529 upload_data_stream
.AppendData(kUploadData
, chunk_size
, false);
531 request_
.method
= "POST";
532 request_
.url
= GURL("http://www.google.com/");
533 request_
.upload_data_stream
= &upload_data_stream
;
534 ASSERT_EQ(OK
, request_
.upload_data_stream
->Init(
535 TestCompletionCallback().callback()));
537 ASSERT_EQ(OK
, stream_
->InitializeStream(&request_
, DEFAULT_PRIORITY
,
538 net_log_
, callback_
.callback()));
539 ASSERT_EQ(ERR_IO_PENDING
, stream_
->SendRequest(headers_
, &response_
,
540 callback_
.callback()));
542 upload_data_stream
.AppendData(kUploadData
, chunk_size
, true);
543 EXPECT_EQ(OK
, callback_
.WaitForResult());
545 // Ack both packets in the request.
546 ProcessPacket(ConstructAckPacket(1, 0, 0));
548 // Send the response headers (but not the body).
549 SetResponse("200 OK", std::string());
550 ProcessPacket(ConstructResponseHeadersPacket(2, !kFin
));
552 // The headers have arrived, but they are delivered asynchronously
553 EXPECT_EQ(ERR_IO_PENDING
, stream_
->ReadResponseHeaders(callback_
.callback()));
554 EXPECT_EQ(OK
, callback_
.WaitForResult());
555 ASSERT_TRUE(response_
.headers
.get());
556 EXPECT_EQ(200, response_
.headers
->response_code());
557 EXPECT_TRUE(response_
.headers
->HasHeaderValue("Content-Type", "text/plain"));
559 // Send the response body.
560 const char kResponseBody
[] = "Hello world!";
561 ProcessPacket(ConstructDataPacket(3, false, kFin
, response_data_
.length(),
564 // Since the body has already arrived, this should return immediately.
565 ASSERT_EQ(static_cast<int>(strlen(kResponseBody
)),
566 stream_
->ReadResponseBody(read_buffer_
.get(), read_buffer_
->size(),
567 callback_
.callback()));
569 EXPECT_TRUE(stream_
->IsResponseBodyComplete());
570 EXPECT_TRUE(AtEof());
573 TEST_P(QuicHttpStreamTest
, SendChunkedPostRequestWithFinalEmptyDataPacket
) {
574 SetRequest("POST", "/", DEFAULT_PRIORITY
);
575 size_t chunk_size
= strlen(kUploadData
);
576 AddWrite(ConstructRequestHeadersPacket(1, !kFin
, DEFAULT_PRIORITY
));
577 AddWrite(ConstructDataPacket(2, kIncludeVersion
, !kFin
, 0, kUploadData
));
578 AddWrite(ConstructDataPacket(3, kIncludeVersion
, kFin
, chunk_size
, ""));
579 AddWrite(ConstructAckPacket(4, 3, 1));
582 ChunkedUploadDataStream
upload_data_stream(0);
583 upload_data_stream
.AppendData(kUploadData
, chunk_size
, false);
585 request_
.method
= "POST";
586 request_
.url
= GURL("http://www.google.com/");
587 request_
.upload_data_stream
= &upload_data_stream
;
588 ASSERT_EQ(OK
, request_
.upload_data_stream
->Init(
589 TestCompletionCallback().callback()));
591 ASSERT_EQ(OK
, stream_
->InitializeStream(&request_
, DEFAULT_PRIORITY
,
592 net_log_
, callback_
.callback()));
593 ASSERT_EQ(ERR_IO_PENDING
, stream_
->SendRequest(headers_
, &response_
,
594 callback_
.callback()));
596 upload_data_stream
.AppendData(nullptr, 0, true);
597 EXPECT_EQ(OK
, callback_
.WaitForResult());
599 ProcessPacket(ConstructAckPacket(1, 0, 0));
601 // Send the response headers (but not the body).
602 SetResponse("200 OK", std::string());
603 ProcessPacket(ConstructResponseHeadersPacket(2, !kFin
));
605 // The headers have arrived, but they are delivered asynchronously
606 EXPECT_EQ(ERR_IO_PENDING
, stream_
->ReadResponseHeaders(callback_
.callback()));
607 EXPECT_EQ(OK
, callback_
.WaitForResult());
608 ASSERT_TRUE(response_
.headers
.get());
609 EXPECT_EQ(200, response_
.headers
->response_code());
610 EXPECT_TRUE(response_
.headers
->HasHeaderValue("Content-Type", "text/plain"));
612 // Send the response body.
613 const char kResponseBody
[] = "Hello world!";
614 ProcessPacket(ConstructDataPacket(3, false, kFin
, response_data_
.length(),
617 // The body has arrived, but it is delivered asynchronously
618 ASSERT_EQ(static_cast<int>(strlen(kResponseBody
)),
619 stream_
->ReadResponseBody(read_buffer_
.get(), read_buffer_
->size(),
620 callback_
.callback()));
621 EXPECT_TRUE(stream_
->IsResponseBodyComplete());
622 EXPECT_TRUE(AtEof());
625 TEST_P(QuicHttpStreamTest
, SendChunkedPostRequestWithOneEmptyDataPacket
) {
626 SetRequest("POST", "/", DEFAULT_PRIORITY
);
627 AddWrite(ConstructRequestHeadersPacket(1, !kFin
, DEFAULT_PRIORITY
));
628 AddWrite(ConstructDataPacket(2, kIncludeVersion
, kFin
, 0, ""));
629 AddWrite(ConstructAckPacket(3, 3, 1));
632 ChunkedUploadDataStream
upload_data_stream(0);
634 request_
.method
= "POST";
635 request_
.url
= GURL("http://www.google.com/");
636 request_
.upload_data_stream
= &upload_data_stream
;
637 ASSERT_EQ(OK
, request_
.upload_data_stream
->Init(
638 TestCompletionCallback().callback()));
640 ASSERT_EQ(OK
, stream_
->InitializeStream(&request_
, DEFAULT_PRIORITY
,
641 net_log_
, callback_
.callback()));
642 ASSERT_EQ(ERR_IO_PENDING
, stream_
->SendRequest(headers_
, &response_
,
643 callback_
.callback()));
645 upload_data_stream
.AppendData(nullptr, 0, true);
646 EXPECT_EQ(OK
, callback_
.WaitForResult());
648 ProcessPacket(ConstructAckPacket(1, 0, 0));
650 // Send the response headers (but not the body).
651 SetResponse("200 OK", std::string());
652 ProcessPacket(ConstructResponseHeadersPacket(2, !kFin
));
654 // The headers have arrived, but they are delivered asynchronously
655 EXPECT_EQ(ERR_IO_PENDING
, stream_
->ReadResponseHeaders(callback_
.callback()));
656 EXPECT_EQ(OK
, callback_
.WaitForResult());
657 ASSERT_TRUE(response_
.headers
.get());
658 EXPECT_EQ(200, response_
.headers
->response_code());
659 EXPECT_TRUE(response_
.headers
->HasHeaderValue("Content-Type", "text/plain"));
661 // Send the response body.
662 const char kResponseBody
[] = "Hello world!";
663 ProcessPacket(ConstructDataPacket(3, false, kFin
, response_data_
.length(),
666 // The body has arrived, but it is delivered asynchronously
667 ASSERT_EQ(static_cast<int>(strlen(kResponseBody
)),
668 stream_
->ReadResponseBody(read_buffer_
.get(), read_buffer_
->size(),
669 callback_
.callback()));
671 EXPECT_TRUE(stream_
->IsResponseBodyComplete());
672 EXPECT_TRUE(AtEof());
675 TEST_P(QuicHttpStreamTest
, DestroyedEarly
) {
676 SetRequest("GET", "/", DEFAULT_PRIORITY
);
677 AddWrite(ConstructRequestHeadersPacket(1, kFin
, DEFAULT_PRIORITY
));
678 if (!FLAGS_quic_process_frames_inline
) {
679 AddWrite(ConstructRstStreamCancelledPacket(2));
681 AddWrite(ConstructAckAndRstStreamPacket(2));
683 use_closing_stream_
= true;
686 request_
.method
= "GET";
687 request_
.url
= GURL("http://www.google.com/");
689 EXPECT_EQ(OK
, stream_
->InitializeStream(&request_
, DEFAULT_PRIORITY
,
690 net_log_
, callback_
.callback()));
691 EXPECT_EQ(OK
, stream_
->SendRequest(headers_
, &response_
,
692 callback_
.callback()));
695 ProcessPacket(ConstructAckPacket(1, 0, 0));
696 EXPECT_EQ(ERR_IO_PENDING
,
697 stream_
->ReadResponseHeaders(callback_
.callback()));
699 // Send the response with a body.
700 SetResponse("404 OK", "hello world!");
701 // In the course of processing this packet, the QuicHttpStream close itself.
702 ProcessPacket(ConstructResponseHeadersPacket(2, kFin
));
704 base::MessageLoop::current()->RunUntilIdle();
706 EXPECT_TRUE(AtEof());
709 TEST_P(QuicHttpStreamTest
, Priority
) {
710 SetRequest("GET", "/", MEDIUM
);
711 AddWrite(ConstructRequestHeadersPacket(1, kFin
, MEDIUM
));
712 if (!FLAGS_quic_process_frames_inline
) {
713 AddWrite(ConstructRstStreamCancelledPacket(2));
715 AddWrite(ConstructAckAndRstStreamPacket(2));
717 use_closing_stream_
= true;
720 request_
.method
= "GET";
721 request_
.url
= GURL("http://www.google.com/");
723 EXPECT_EQ(OK
, stream_
->InitializeStream(&request_
, MEDIUM
,
724 net_log_
, callback_
.callback()));
726 // Check that priority is highest.
727 QuicReliableClientStream
* reliable_stream
=
728 QuicHttpStreamPeer::GetQuicReliableClientStream(stream_
.get());
729 DCHECK(reliable_stream
);
730 DCHECK_EQ(QuicWriteBlockedList::kHighestPriority
,
731 reliable_stream
->EffectivePriority());
733 EXPECT_EQ(OK
, stream_
->SendRequest(headers_
, &response_
,
734 callback_
.callback()));
736 // Check that priority has now dropped back to MEDIUM.
737 DCHECK_EQ(MEDIUM
, ConvertQuicPriorityToRequestPriority(
738 reliable_stream
->EffectivePriority()));
741 ProcessPacket(ConstructAckPacket(1, 0, 0));
742 EXPECT_EQ(ERR_IO_PENDING
,
743 stream_
->ReadResponseHeaders(callback_
.callback()));
745 // Send the response with a body.
746 SetResponse("404 OK", "hello world!");
747 // In the course of processing this packet, the QuicHttpStream close itself.
748 ProcessPacket(ConstructResponseHeadersPacket(2, kFin
));
750 base::MessageLoop::current()->RunUntilIdle();
752 EXPECT_TRUE(AtEof());
755 // Regression test for http://crbug.com/294870
756 TEST_P(QuicHttpStreamTest
, CheckPriorityWithNoDelegate
) {
757 SetRequest("GET", "/", MEDIUM
);
758 use_closing_stream_
= true;
760 AddWrite(ConstructRstStreamPacket(1));
764 request_
.method
= "GET";
765 request_
.url
= GURL("http://www.google.com/");
767 EXPECT_EQ(OK
, stream_
->InitializeStream(&request_
, MEDIUM
,
768 net_log_
, callback_
.callback()));
770 // Check that priority is highest.
771 QuicReliableClientStream
* reliable_stream
=
772 QuicHttpStreamPeer::GetQuicReliableClientStream(stream_
.get());
773 DCHECK(reliable_stream
);
774 QuicReliableClientStream::Delegate
* delegate
= reliable_stream
->GetDelegate();
776 DCHECK_EQ(QuicWriteBlockedList::kHighestPriority
,
777 reliable_stream
->EffectivePriority());
779 // Set Delegate to nullptr and make sure EffectivePriority returns highest
781 reliable_stream
->SetDelegate(nullptr);
782 DCHECK_EQ(QuicWriteBlockedList::kHighestPriority
,
783 reliable_stream
->EffectivePriority());
784 reliable_stream
->SetDelegate(delegate
);