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 "net/base/net_errors.h"
10 #include "net/base/test_completion_callback.h"
11 #include "net/base/upload_bytes_element_reader.h"
12 #include "net/base/upload_data_stream.h"
13 #include "net/http/http_response_headers.h"
14 #include "net/http/transport_security_state.h"
15 #include "net/quic/congestion_control/receive_algorithm_interface.h"
16 #include "net/quic/congestion_control/send_algorithm_interface.h"
17 #include "net/quic/crypto/crypto_protocol.h"
18 #include "net/quic/crypto/quic_decrypter.h"
19 #include "net/quic/crypto/quic_encrypter.h"
20 #include "net/quic/crypto/quic_server_info.h"
21 #include "net/quic/quic_client_session.h"
22 #include "net/quic/quic_connection.h"
23 #include "net/quic/quic_connection_helper.h"
24 #include "net/quic/quic_default_packet_writer.h"
25 #include "net/quic/quic_http_utils.h"
26 #include "net/quic/quic_reliable_client_stream.h"
27 #include "net/quic/quic_write_blocked_list.h"
28 #include "net/quic/spdy_utils.h"
29 #include "net/quic/test_tools/mock_clock.h"
30 #include "net/quic/test_tools/mock_crypto_client_stream_factory.h"
31 #include "net/quic/test_tools/mock_random.h"
32 #include "net/quic/test_tools/quic_connection_peer.h"
33 #include "net/quic/test_tools/quic_test_packet_maker.h"
34 #include "net/quic/test_tools/quic_test_utils.h"
35 #include "net/quic/test_tools/test_task_runner.h"
36 #include "net/socket/socket_test_util.h"
37 #include "net/spdy/spdy_frame_builder.h"
38 #include "net/spdy/spdy_framer.h"
39 #include "net/spdy/spdy_http_utils.h"
40 #include "net/spdy/spdy_protocol.h"
41 #include "testing/gmock/include/gmock/gmock.h"
42 #include "testing/gtest/include/gtest/gtest.h"
45 using testing::AnyNumber
;
46 using testing::Return
;
52 const char kUploadData
[] = "Really nifty data!";
53 const char kServerHostname
[] = "www.google.com";
54 const uint16 kServerPort
= 80;
56 class TestQuicConnection
: public QuicConnection
{
58 TestQuicConnection(const QuicVersionVector
& versions
,
59 QuicConnectionId connection_id
,
61 QuicConnectionHelper
* helper
,
62 const QuicConnection::PacketWriterFactory
& writer_factory
)
63 : QuicConnection(connection_id
,
67 true /* owns_writer */,
68 false /* is_server */,
72 void SetSendAlgorithm(SendAlgorithmInterface
* send_algorithm
) {
73 QuicConnectionPeer::SetSendAlgorithm(this, send_algorithm
);
76 void SetReceiveAlgorithm(ReceiveAlgorithmInterface
* receive_algorithm
) {
77 QuicConnectionPeer::SetReceiveAlgorithm(this, receive_algorithm
);
81 class TestReceiveAlgorithm
: public ReceiveAlgorithmInterface
{
83 virtual bool GenerateCongestionFeedback(
84 QuicCongestionFeedbackFrame
* /*congestion_feedback*/) {
88 MOCK_METHOD3(RecordIncomingPacket
,
89 void(QuicByteCount
, QuicPacketSequenceNumber
, QuicTime
));
92 // Subclass of QuicHttpStream that closes itself when the first piece of data
94 class AutoClosingStream
: public QuicHttpStream
{
96 explicit AutoClosingStream(const base::WeakPtr
<QuicClientSession
>& session
)
97 : QuicHttpStream(session
) {
100 virtual int OnDataReceived(const char* data
, int length
) OVERRIDE
{
106 class TestPacketWriterFactory
: public QuicConnection::PacketWriterFactory
{
108 explicit TestPacketWriterFactory(DatagramClientSocket
* socket
)
110 virtual ~TestPacketWriterFactory() {}
112 virtual QuicPacketWriter
* Create(QuicConnection
* connection
) const OVERRIDE
{
113 return new QuicDefaultPacketWriter(socket_
);
117 DatagramClientSocket
* socket_
;
122 class QuicHttpStreamPeer
{
124 static QuicReliableClientStream
* GetQuicReliableClientStream(
125 QuicHttpStream
* stream
) {
126 return stream
->stream_
;
130 class QuicHttpStreamTest
: public ::testing::TestWithParam
<QuicVersion
> {
132 static const bool kFin
= true;
133 static const bool kIncludeVersion
= true;
134 static const bool kIncludeCongestionFeedback
= true;
136 // Holds a packet to be written to the wire, and the IO mode that should
137 // be used by the mock socket when performing the write.
138 struct PacketToWrite
{
139 PacketToWrite(IoMode mode
, QuicEncryptedPacket
* packet
)
144 QuicEncryptedPacket
* packet
;
148 : net_log_(BoundNetLog()),
149 use_closing_stream_(false),
150 read_buffer_(new IOBufferWithSize(4096)),
152 stream_id_(kClientDataStreamId1
),
153 maker_(GetParam(), connection_id_
, &clock_
),
154 random_generator_(0) {
156 CHECK(ParseIPLiteralToNumber("192.0.2.33", &ip
));
157 peer_addr_
= IPEndPoint(ip
, 443);
158 self_addr_
= IPEndPoint(ip
, 8435);
159 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(20));
162 ~QuicHttpStreamTest() {
163 session_
->CloseSessionOnError(ERR_ABORTED
);
164 for (size_t i
= 0; i
< writes_
.size(); i
++) {
165 delete writes_
[i
].packet
;
169 // Adds a packet to the list of expected writes.
170 void AddWrite(scoped_ptr
<QuicEncryptedPacket
> packet
) {
171 writes_
.push_back(PacketToWrite(SYNCHRONOUS
, packet
.release()));
174 // Returns the packet to be written at position |pos|.
175 QuicEncryptedPacket
* GetWrite(size_t pos
) {
176 return writes_
[pos
].packet
;
180 return socket_data_
->at_read_eof() && socket_data_
->at_write_eof();
183 void ProcessPacket(scoped_ptr
<QuicEncryptedPacket
> packet
) {
184 connection_
->ProcessUdpPacket(self_addr_
, peer_addr_
, *packet
);
187 // Configures the test fixture to use the list of expected writes.
189 mock_writes_
.reset(new MockWrite
[writes_
.size()]);
190 for (size_t i
= 0; i
< writes_
.size(); i
++) {
191 mock_writes_
[i
] = MockWrite(writes_
[i
].mode
,
192 writes_
[i
].packet
->data(),
193 writes_
[i
].packet
->length());
196 socket_data_
.reset(new StaticSocketDataProvider(NULL
, 0, mock_writes_
.get(),
199 MockUDPClientSocket
* socket
= new MockUDPClientSocket(socket_data_
.get(),
201 socket
->Connect(peer_addr_
);
202 runner_
= new TestTaskRunner(&clock_
);
203 send_algorithm_
= new MockSendAlgorithm();
204 receive_algorithm_
= new TestReceiveAlgorithm();
205 EXPECT_CALL(*receive_algorithm_
, RecordIncomingPacket(_
, _
, _
)).
207 EXPECT_CALL(*send_algorithm_
,
208 OnPacketSent(_
, _
, _
, _
, _
)).WillRepeatedly(Return(true));
209 EXPECT_CALL(*send_algorithm_
, RetransmissionDelay()).WillRepeatedly(
210 Return(QuicTime::Delta::Zero()));
211 EXPECT_CALL(*send_algorithm_
, GetCongestionWindow()).WillRepeatedly(
212 Return(kMaxPacketSize
));
213 EXPECT_CALL(*send_algorithm_
, TimeUntilSend(_
, _
, _
)).
214 WillRepeatedly(Return(QuicTime::Delta::Zero()));
215 EXPECT_CALL(*send_algorithm_
, BandwidthEstimate()).WillRepeatedly(
216 Return(QuicBandwidth::Zero()));
217 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
)).Times(AnyNumber());
218 helper_
.reset(new QuicConnectionHelper(runner_
.get(), &clock_
,
219 &random_generator_
));
220 TestPacketWriterFactory
writer_factory(socket
);
221 connection_
= new TestQuicConnection(SupportedVersions(GetParam()),
222 connection_id_
, peer_addr_
,
223 helper_
.get(), writer_factory
);
224 connection_
->set_visitor(&visitor_
);
225 connection_
->SetSendAlgorithm(send_algorithm_
);
226 connection_
->SetReceiveAlgorithm(receive_algorithm_
);
227 crypto_config_
.SetDefaults();
229 new QuicClientSession(connection_
,
230 scoped_ptr
<DatagramClientSocket
>(socket
),
232 &transport_security_state_
,
233 make_scoped_ptr((QuicServerInfo
*)NULL
),
235 base::MessageLoop::current()->
236 message_loop_proxy().get(),
238 session_
->InitializeSession(QuicServerId(kServerHostname
, kServerPort
,
239 false, PRIVACY_MODE_DISABLED
),
241 &crypto_client_stream_factory_
);
242 session_
->GetCryptoStream()->CryptoConnect();
243 EXPECT_TRUE(session_
->IsCryptoHandshakeConfirmed());
244 stream_
.reset(use_closing_stream_
?
245 new AutoClosingStream(session_
->GetWeakPtr()) :
246 new QuicHttpStream(session_
->GetWeakPtr()));
249 void SetRequest(const std::string
& method
,
250 const std::string
& path
,
251 RequestPriority priority
) {
252 request_headers_
= maker_
.GetRequestHeaders(method
, "http", path
);
255 void SetResponse(const std::string
& status
, const std::string
& body
) {
256 response_headers_
= maker_
.GetResponseHeaders(status
);
257 response_data_
= body
;
260 scoped_ptr
<QuicEncryptedPacket
> ConstructDataPacket(
261 QuicPacketSequenceNumber sequence_number
,
262 bool should_include_version
,
264 QuicStreamOffset offset
,
265 base::StringPiece data
) {
266 return maker_
.MakeDataPacket(
267 sequence_number
, stream_id_
, should_include_version
, fin
, offset
, data
);
270 scoped_ptr
<QuicEncryptedPacket
> ConstructRequestHeadersPacket(
271 QuicPacketSequenceNumber sequence_number
,
273 return maker_
.MakeRequestHeadersPacket(
274 sequence_number
, stream_id_
, kIncludeVersion
, fin
, request_headers_
);
277 scoped_ptr
<QuicEncryptedPacket
> ConstructResponseHeadersPacket(
278 QuicPacketSequenceNumber sequence_number
,
280 return maker_
.MakeResponseHeadersPacket(
281 sequence_number
, stream_id_
, !kIncludeVersion
, fin
, response_headers_
);
284 scoped_ptr
<QuicEncryptedPacket
> ConstructRstStreamPacket(
285 QuicPacketSequenceNumber sequence_number
) {
286 return maker_
.MakeRstPacket(
287 sequence_number
, true, stream_id_
,
288 AdjustErrorForVersion(QUIC_RST_FLOW_CONTROL_ACCOUNTING
, GetParam()));
291 scoped_ptr
<QuicEncryptedPacket
> ConstructAckAndRstStreamPacket(
292 QuicPacketSequenceNumber sequence_number
) {
293 return maker_
.MakeAckAndRstPacket(
294 sequence_number
, !kIncludeVersion
, stream_id_
, QUIC_STREAM_CANCELLED
,
295 2, 1, !kIncludeCongestionFeedback
);
298 scoped_ptr
<QuicEncryptedPacket
> ConstructAckPacket(
299 QuicPacketSequenceNumber sequence_number
,
300 QuicPacketSequenceNumber largest_received
,
301 QuicPacketSequenceNumber least_unacked
) {
302 return maker_
.MakeAckPacket(sequence_number
, largest_received
,
303 least_unacked
, !kIncludeCongestionFeedback
);
306 BoundNetLog net_log_
;
307 bool use_closing_stream_
;
308 MockSendAlgorithm
* send_algorithm_
;
309 TestReceiveAlgorithm
* receive_algorithm_
;
310 scoped_refptr
<TestTaskRunner
> runner_
;
311 scoped_ptr
<MockWrite
[]> mock_writes_
;
313 TestQuicConnection
* connection_
;
314 scoped_ptr
<QuicConnectionHelper
> helper_
;
315 testing::StrictMock
<MockConnectionVisitor
> visitor_
;
316 scoped_ptr
<QuicHttpStream
> stream_
;
317 TransportSecurityState transport_security_state_
;
318 scoped_ptr
<QuicClientSession
> session_
;
319 QuicCryptoClientConfig crypto_config_
;
320 TestCompletionCallback callback_
;
321 HttpRequestInfo request_
;
322 HttpRequestHeaders headers_
;
323 HttpResponseInfo response_
;
324 scoped_refptr
<IOBufferWithSize
> read_buffer_
;
325 SpdyHeaderBlock request_headers_
;
326 SpdyHeaderBlock response_headers_
;
327 std::string request_data_
;
328 std::string response_data_
;
331 const QuicConnectionId connection_id_
;
332 const QuicStreamId stream_id_
;
333 QuicTestPacketMaker maker_
;
334 IPEndPoint self_addr_
;
335 IPEndPoint peer_addr_
;
336 MockRandom random_generator_
;
337 MockCryptoClientStreamFactory crypto_client_stream_factory_
;
338 scoped_ptr
<StaticSocketDataProvider
> socket_data_
;
339 std::vector
<PacketToWrite
> writes_
;
342 INSTANTIATE_TEST_CASE_P(Version
, QuicHttpStreamTest
,
343 ::testing::ValuesIn(QuicSupportedVersions()));
345 TEST_P(QuicHttpStreamTest
, RenewStreamForAuth
) {
347 EXPECT_EQ(NULL
, stream_
->RenewStreamForAuth());
350 TEST_P(QuicHttpStreamTest
, CanFindEndOfResponse
) {
352 EXPECT_TRUE(stream_
->CanFindEndOfResponse());
355 TEST_P(QuicHttpStreamTest
, IsConnectionReusable
) {
357 EXPECT_FALSE(stream_
->IsConnectionReusable());
360 TEST_P(QuicHttpStreamTest
, GetRequest
) {
361 SetRequest("GET", "/", DEFAULT_PRIORITY
);
362 AddWrite(ConstructRequestHeadersPacket(1, kFin
));
365 request_
.method
= "GET";
366 request_
.url
= GURL("http://www.google.com/");
368 EXPECT_EQ(OK
, stream_
->InitializeStream(&request_
, DEFAULT_PRIORITY
,
369 net_log_
, callback_
.callback()));
370 EXPECT_EQ(OK
, stream_
->SendRequest(headers_
, &response_
,
371 callback_
.callback()));
374 ProcessPacket(ConstructAckPacket(1, 0, 0));
376 EXPECT_EQ(ERR_IO_PENDING
,
377 stream_
->ReadResponseHeaders(callback_
.callback()));
379 SetResponse("404 Not Found", std::string());
380 ProcessPacket(ConstructResponseHeadersPacket(2, kFin
));
382 // Now that the headers have been processed, the callback will return.
383 EXPECT_EQ(OK
, callback_
.WaitForResult());
384 ASSERT_TRUE(response_
.headers
.get());
385 EXPECT_EQ(404, response_
.headers
->response_code());
386 EXPECT_TRUE(response_
.headers
->HasHeaderValue("Content-Type", "text/plain"));
387 EXPECT_FALSE(response_
.response_time
.is_null());
388 EXPECT_FALSE(response_
.request_time
.is_null());
390 // There is no body, so this should return immediately.
391 EXPECT_EQ(0, stream_
->ReadResponseBody(read_buffer_
.get(),
392 read_buffer_
->size(),
393 callback_
.callback()));
394 EXPECT_TRUE(stream_
->IsResponseBodyComplete());
395 EXPECT_TRUE(AtEof());
398 // Regression test for http://crbug.com/288128
399 TEST_P(QuicHttpStreamTest
, GetRequestLargeResponse
) {
400 SetRequest("GET", "/", DEFAULT_PRIORITY
);
401 AddWrite(ConstructRequestHeadersPacket(1, kFin
));
404 request_
.method
= "GET";
405 request_
.url
= GURL("http://www.google.com/");
407 EXPECT_EQ(OK
, stream_
->InitializeStream(&request_
, DEFAULT_PRIORITY
,
408 net_log_
, callback_
.callback()));
409 EXPECT_EQ(OK
, stream_
->SendRequest(headers_
, &response_
,
410 callback_
.callback()));
413 ProcessPacket(ConstructAckPacket(1, 0, 0));
415 EXPECT_EQ(ERR_IO_PENDING
,
416 stream_
->ReadResponseHeaders(callback_
.callback()));
418 SpdyHeaderBlock headers
;
419 headers
[":status"] = "200 OK";
420 headers
[":version"] = "HTTP/1.1";
421 headers
["content-type"] = "text/plain";
422 headers
["big6"] = std::string(10000, 'x'); // Lots of x's.
424 std::string response
= SpdyUtils::SerializeUncompressedHeaders(headers
);
425 EXPECT_LT(4096u, response
.length());
426 stream_
->OnDataReceived(response
.data(), response
.length());
427 stream_
->OnClose(QUIC_NO_ERROR
);
429 // Now that the headers have been processed, the callback will return.
430 EXPECT_EQ(OK
, callback_
.WaitForResult());
431 ASSERT_TRUE(response_
.headers
.get());
432 EXPECT_EQ(200, response_
.headers
->response_code());
433 EXPECT_TRUE(response_
.headers
->HasHeaderValue("Content-Type", "text/plain"));
435 // There is no body, so this should return immediately.
436 EXPECT_EQ(0, stream_
->ReadResponseBody(read_buffer_
.get(),
437 read_buffer_
->size(),
438 callback_
.callback()));
439 EXPECT_TRUE(stream_
->IsResponseBodyComplete());
440 EXPECT_TRUE(AtEof());
443 TEST_P(QuicHttpStreamTest
, SendPostRequest
) {
444 SetRequest("POST", "/", DEFAULT_PRIORITY
);
445 AddWrite(ConstructRequestHeadersPacket(1, !kFin
));
446 AddWrite(ConstructDataPacket(2, kIncludeVersion
, kFin
, 0, kUploadData
));
447 AddWrite(ConstructAckPacket(3, 3, 1));
451 ScopedVector
<UploadElementReader
> element_readers
;
452 element_readers
.push_back(
453 new UploadBytesElementReader(kUploadData
, strlen(kUploadData
)));
454 UploadDataStream
upload_data_stream(element_readers
.Pass(), 0);
455 request_
.method
= "POST";
456 request_
.url
= GURL("http://www.google.com/");
457 request_
.upload_data_stream
= &upload_data_stream
;
458 ASSERT_EQ(OK
, request_
.upload_data_stream
->Init(CompletionCallback()));
460 EXPECT_EQ(OK
, stream_
->InitializeStream(&request_
, DEFAULT_PRIORITY
,
461 net_log_
, callback_
.callback()));
462 EXPECT_EQ(OK
, stream_
->SendRequest(headers_
, &response_
,
463 callback_
.callback()));
465 // Ack both packets in the request.
466 ProcessPacket(ConstructAckPacket(1, 0, 0));
468 // Send the response headers (but not the body).
469 SetResponse("200 OK", std::string());
470 ProcessPacket(ConstructResponseHeadersPacket(2, !kFin
));
472 // Since the headers have already arrived, this should return immediately.
473 EXPECT_EQ(OK
, stream_
->ReadResponseHeaders(callback_
.callback()));
474 ASSERT_TRUE(response_
.headers
.get());
475 EXPECT_EQ(200, response_
.headers
->response_code());
476 EXPECT_TRUE(response_
.headers
->HasHeaderValue("Content-Type", "text/plain"));
478 // Send the response body.
479 const char kResponseBody
[] = "Hello world!";
480 ProcessPacket(ConstructDataPacket(3, false, kFin
, 0, kResponseBody
));
481 // Since the body has already arrived, this should return immediately.
482 EXPECT_EQ(static_cast<int>(strlen(kResponseBody
)),
483 stream_
->ReadResponseBody(read_buffer_
.get(), read_buffer_
->size(),
484 callback_
.callback()));
486 EXPECT_TRUE(stream_
->IsResponseBodyComplete());
487 EXPECT_TRUE(AtEof());
490 TEST_P(QuicHttpStreamTest
, SendChunkedPostRequest
) {
491 SetRequest("POST", "/", DEFAULT_PRIORITY
);
492 size_t chunk_size
= strlen(kUploadData
);
493 AddWrite(ConstructRequestHeadersPacket(1, !kFin
));
494 AddWrite(ConstructDataPacket(2, kIncludeVersion
, !kFin
, 0, kUploadData
));
495 AddWrite(ConstructDataPacket(3, kIncludeVersion
, kFin
, chunk_size
,
497 AddWrite(ConstructAckPacket(4, 3, 1));
500 UploadDataStream
upload_data_stream(UploadDataStream::CHUNKED
, 0);
501 upload_data_stream
.AppendChunk(kUploadData
, chunk_size
, false);
503 request_
.method
= "POST";
504 request_
.url
= GURL("http://www.google.com/");
505 request_
.upload_data_stream
= &upload_data_stream
;
506 ASSERT_EQ(OK
, request_
.upload_data_stream
->Init(CompletionCallback()));
508 ASSERT_EQ(OK
, stream_
->InitializeStream(&request_
, DEFAULT_PRIORITY
,
509 net_log_
, callback_
.callback()));
510 ASSERT_EQ(ERR_IO_PENDING
, stream_
->SendRequest(headers_
, &response_
,
511 callback_
.callback()));
513 upload_data_stream
.AppendChunk(kUploadData
, chunk_size
, true);
515 // Ack both packets in the request.
516 ProcessPacket(ConstructAckPacket(1, 0, 0));
518 // Send the response headers (but not the body).
519 SetResponse("200 OK", std::string());
520 ProcessPacket(ConstructResponseHeadersPacket(2, !kFin
));
522 // Since the headers have already arrived, this should return immediately.
523 ASSERT_EQ(OK
, stream_
->ReadResponseHeaders(callback_
.callback()));
524 ASSERT_TRUE(response_
.headers
.get());
525 EXPECT_EQ(200, response_
.headers
->response_code());
526 EXPECT_TRUE(response_
.headers
->HasHeaderValue("Content-Type", "text/plain"));
528 // Send the response body.
529 const char kResponseBody
[] = "Hello world!";
530 ProcessPacket(ConstructDataPacket(3, false, kFin
, response_data_
.length(),
533 // Since the body has already arrived, this should return immediately.
534 ASSERT_EQ(static_cast<int>(strlen(kResponseBody
)),
535 stream_
->ReadResponseBody(read_buffer_
.get(), read_buffer_
->size(),
536 callback_
.callback()));
538 EXPECT_TRUE(stream_
->IsResponseBodyComplete());
539 EXPECT_TRUE(AtEof());
542 TEST_P(QuicHttpStreamTest
, SendChunkedPostRequestWithFinalEmptyDataPacket
) {
543 SetRequest("POST", "/", DEFAULT_PRIORITY
);
544 size_t chunk_size
= strlen(kUploadData
);
545 AddWrite(ConstructRequestHeadersPacket(1, !kFin
));
546 AddWrite(ConstructDataPacket(2, kIncludeVersion
, !kFin
, 0, kUploadData
));
547 AddWrite(ConstructDataPacket(3, kIncludeVersion
, kFin
, chunk_size
, ""));
548 AddWrite(ConstructAckPacket(4, 3, 1));
551 UploadDataStream
upload_data_stream(UploadDataStream::CHUNKED
, 0);
552 upload_data_stream
.AppendChunk(kUploadData
, chunk_size
, false);
554 request_
.method
= "POST";
555 request_
.url
= GURL("http://www.google.com/");
556 request_
.upload_data_stream
= &upload_data_stream
;
557 ASSERT_EQ(OK
, request_
.upload_data_stream
->Init(CompletionCallback()));
559 ASSERT_EQ(OK
, stream_
->InitializeStream(&request_
, DEFAULT_PRIORITY
,
560 net_log_
, callback_
.callback()));
561 ASSERT_EQ(ERR_IO_PENDING
, stream_
->SendRequest(headers_
, &response_
,
562 callback_
.callback()));
564 upload_data_stream
.AppendChunk(NULL
, 0, true);
566 ProcessPacket(ConstructAckPacket(1, 0, 0));
568 // Send the response headers (but not the body).
569 SetResponse("200 OK", std::string());
570 ProcessPacket(ConstructResponseHeadersPacket(2, !kFin
));
572 // Since the headers have already arrived, this should return immediately.
573 ASSERT_EQ(OK
, stream_
->ReadResponseHeaders(callback_
.callback()));
574 ASSERT_TRUE(response_
.headers
.get());
575 EXPECT_EQ(200, response_
.headers
->response_code());
576 EXPECT_TRUE(response_
.headers
->HasHeaderValue("Content-Type", "text/plain"));
578 // Send the response body.
579 const char kResponseBody
[] = "Hello world!";
580 ProcessPacket(ConstructDataPacket(3, false, kFin
, response_data_
.length(),
583 // Since the body has already arrived, this should return immediately.
584 ASSERT_EQ(static_cast<int>(strlen(kResponseBody
)),
585 stream_
->ReadResponseBody(read_buffer_
.get(), read_buffer_
->size(),
586 callback_
.callback()));
588 EXPECT_TRUE(stream_
->IsResponseBodyComplete());
589 EXPECT_TRUE(AtEof());
592 TEST_P(QuicHttpStreamTest
, SendChunkedPostRequestWithOneEmptyDataPacket
) {
593 SetRequest("POST", "/", DEFAULT_PRIORITY
);
594 AddWrite(ConstructRequestHeadersPacket(1, !kFin
));
595 AddWrite(ConstructDataPacket(2, kIncludeVersion
, kFin
, 0, ""));
596 AddWrite(ConstructAckPacket(3, 3, 1));
599 UploadDataStream
upload_data_stream(UploadDataStream::CHUNKED
, 0);
601 request_
.method
= "POST";
602 request_
.url
= GURL("http://www.google.com/");
603 request_
.upload_data_stream
= &upload_data_stream
;
604 ASSERT_EQ(OK
, request_
.upload_data_stream
->Init(CompletionCallback()));
606 ASSERT_EQ(OK
, stream_
->InitializeStream(&request_
, DEFAULT_PRIORITY
,
607 net_log_
, callback_
.callback()));
608 ASSERT_EQ(ERR_IO_PENDING
, stream_
->SendRequest(headers_
, &response_
,
609 callback_
.callback()));
611 upload_data_stream
.AppendChunk(NULL
, 0, true);
613 ProcessPacket(ConstructAckPacket(1, 0, 0));
615 // Send the response headers (but not the body).
616 SetResponse("200 OK", std::string());
617 ProcessPacket(ConstructResponseHeadersPacket(2, !kFin
));
619 // Since the headers have already arrived, this should return immediately.
620 ASSERT_EQ(OK
, stream_
->ReadResponseHeaders(callback_
.callback()));
621 ASSERT_TRUE(response_
.headers
.get());
622 EXPECT_EQ(200, response_
.headers
->response_code());
623 EXPECT_TRUE(response_
.headers
->HasHeaderValue("Content-Type", "text/plain"));
625 // Send the response body.
626 const char kResponseBody
[] = "Hello world!";
627 ProcessPacket(ConstructDataPacket(3, false, kFin
, response_data_
.length(),
630 // Since the body has already arrived, this should return immediately.
631 ASSERT_EQ(static_cast<int>(strlen(kResponseBody
)),
632 stream_
->ReadResponseBody(read_buffer_
.get(), read_buffer_
->size(),
633 callback_
.callback()));
635 EXPECT_TRUE(stream_
->IsResponseBodyComplete());
636 EXPECT_TRUE(AtEof());
639 TEST_P(QuicHttpStreamTest
, DestroyedEarly
) {
640 SetRequest("GET", "/", DEFAULT_PRIORITY
);
641 AddWrite(ConstructRequestHeadersPacket(1, kFin
));
642 AddWrite(ConstructAckAndRstStreamPacket(2));
643 use_closing_stream_
= true;
646 request_
.method
= "GET";
647 request_
.url
= GURL("http://www.google.com/");
649 EXPECT_EQ(OK
, stream_
->InitializeStream(&request_
, DEFAULT_PRIORITY
,
650 net_log_
, callback_
.callback()));
651 EXPECT_EQ(OK
, stream_
->SendRequest(headers_
, &response_
,
652 callback_
.callback()));
655 ProcessPacket(ConstructAckPacket(1, 0, 0));
656 EXPECT_EQ(ERR_IO_PENDING
,
657 stream_
->ReadResponseHeaders(callback_
.callback()));
659 // Send the response with a body.
660 SetResponse("404 OK", "hello world!");
661 // In the course of processing this packet, the QuicHttpStream close itself.
662 ProcessPacket(ConstructResponseHeadersPacket(2, kFin
));
664 EXPECT_TRUE(AtEof());
667 TEST_P(QuicHttpStreamTest
, Priority
) {
668 SetRequest("GET", "/", MEDIUM
);
669 AddWrite(ConstructRequestHeadersPacket(1, kFin
));
670 AddWrite(ConstructAckAndRstStreamPacket(2));
671 use_closing_stream_
= true;
674 request_
.method
= "GET";
675 request_
.url
= GURL("http://www.google.com/");
677 EXPECT_EQ(OK
, stream_
->InitializeStream(&request_
, MEDIUM
,
678 net_log_
, callback_
.callback()));
680 // Check that priority is highest.
681 QuicReliableClientStream
* reliable_stream
=
682 QuicHttpStreamPeer::GetQuicReliableClientStream(stream_
.get());
683 DCHECK(reliable_stream
);
684 DCHECK_EQ(QuicWriteBlockedList::kHighestPriority
,
685 reliable_stream
->EffectivePriority());
687 EXPECT_EQ(OK
, stream_
->SendRequest(headers_
, &response_
,
688 callback_
.callback()));
690 // Check that priority has now dropped back to MEDIUM.
691 DCHECK_EQ(MEDIUM
, ConvertQuicPriorityToRequestPriority(
692 reliable_stream
->EffectivePriority()));
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 EXPECT_TRUE(AtEof());
707 // Regression test for http://crbug.com/294870
708 TEST_P(QuicHttpStreamTest
, CheckPriorityWithNoDelegate
) {
709 SetRequest("GET", "/", MEDIUM
);
710 use_closing_stream_
= true;
712 AddWrite(ConstructRstStreamPacket(1));
716 request_
.method
= "GET";
717 request_
.url
= GURL("http://www.google.com/");
719 EXPECT_EQ(OK
, stream_
->InitializeStream(&request_
, MEDIUM
,
720 net_log_
, callback_
.callback()));
722 // Check that priority is highest.
723 QuicReliableClientStream
* reliable_stream
=
724 QuicHttpStreamPeer::GetQuicReliableClientStream(stream_
.get());
725 DCHECK(reliable_stream
);
726 QuicReliableClientStream::Delegate
* delegate
= reliable_stream
->GetDelegate();
728 DCHECK_EQ(QuicWriteBlockedList::kHighestPriority
,
729 reliable_stream
->EffectivePriority());
731 // Set Delegate to NULL and make sure EffectivePriority returns highest
733 reliable_stream
->SetDelegate(NULL
);
734 DCHECK_EQ(QuicWriteBlockedList::kHighestPriority
,
735 reliable_stream
->EffectivePriority());
736 reliable_stream
->SetDelegate(delegate
);