Revert "Fix broken channel icon in chrome://help on CrOS" and try again
[chromium-blink-merge.git] / net / http / http_stream_parser_unittest.cc
blob76f998a0e97a78d3c9db169712bbdc6a197961e1
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/http/http_stream_parser.h"
7 #include <algorithm>
8 #include <string>
9 #include <vector>
11 #include "base/files/file_path.h"
12 #include "base/files/file_util.h"
13 #include "base/files/scoped_temp_dir.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/run_loop.h"
16 #include "base/strings/string_piece.h"
17 #include "base/strings/stringprintf.h"
18 #include "base/thread_task_runner_handle.h"
19 #include "net/base/chunked_upload_data_stream.h"
20 #include "net/base/elements_upload_data_stream.h"
21 #include "net/base/io_buffer.h"
22 #include "net/base/net_errors.h"
23 #include "net/base/test_completion_callback.h"
24 #include "net/base/upload_bytes_element_reader.h"
25 #include "net/base/upload_file_element_reader.h"
26 #include "net/http/http_request_headers.h"
27 #include "net/http/http_request_info.h"
28 #include "net/http/http_response_headers.h"
29 #include "net/http/http_response_info.h"
30 #include "net/socket/client_socket_handle.h"
31 #include "net/socket/socket_test_util.h"
32 #include "testing/gtest/include/gtest/gtest.h"
33 #include "url/gurl.h"
35 namespace net {
37 namespace {
39 const size_t kOutputSize = 1024; // Just large enough for this test.
40 // The number of bytes that can fit in a buffer of kOutputSize.
41 const size_t kMaxPayloadSize =
42 kOutputSize - HttpStreamParser::kChunkHeaderFooterSize;
44 // Helper method to create a connected ClientSocketHandle using |data|.
45 // Modifies |data|.
46 scoped_ptr<ClientSocketHandle> CreateConnectedSocketHandle(
47 SequencedSocketData* data) {
48 data->set_connect_data(MockConnect(SYNCHRONOUS, OK));
50 scoped_ptr<MockTCPClientSocket> socket(
51 new MockTCPClientSocket(net::AddressList(), nullptr, data));
52 data->set_socket(socket.get());
54 TestCompletionCallback callback;
55 EXPECT_EQ(OK, socket->Connect(callback.callback()));
57 scoped_ptr<ClientSocketHandle> socket_handle(new ClientSocketHandle);
58 socket_handle->SetSocket(socket.Pass());
59 return socket_handle.Pass();
62 // The empty payload is how the last chunk is encoded.
63 TEST(HttpStreamParser, EncodeChunk_EmptyPayload) {
64 char output[kOutputSize];
66 const base::StringPiece kPayload = "";
67 const base::StringPiece kExpected = "0\r\n\r\n";
68 const int num_bytes_written =
69 HttpStreamParser::EncodeChunk(kPayload, output, sizeof(output));
70 ASSERT_EQ(kExpected.size(), static_cast<size_t>(num_bytes_written));
71 EXPECT_EQ(kExpected, base::StringPiece(output, num_bytes_written));
74 TEST(HttpStreamParser, EncodeChunk_ShortPayload) {
75 char output[kOutputSize];
77 const std::string kPayload("foo\x00\x11\x22", 6);
78 // 11 = payload size + sizeof("6") + CRLF x 2.
79 const std::string kExpected("6\r\nfoo\x00\x11\x22\r\n", 11);
80 const int num_bytes_written =
81 HttpStreamParser::EncodeChunk(kPayload, output, sizeof(output));
82 ASSERT_EQ(kExpected.size(), static_cast<size_t>(num_bytes_written));
83 EXPECT_EQ(kExpected, base::StringPiece(output, num_bytes_written));
86 TEST(HttpStreamParser, EncodeChunk_LargePayload) {
87 char output[kOutputSize];
89 const std::string kPayload(1000, '\xff'); // '\xff' x 1000.
90 // 3E8 = 1000 in hex.
91 const std::string kExpected = "3E8\r\n" + kPayload + "\r\n";
92 const int num_bytes_written =
93 HttpStreamParser::EncodeChunk(kPayload, output, sizeof(output));
94 ASSERT_EQ(kExpected.size(), static_cast<size_t>(num_bytes_written));
95 EXPECT_EQ(kExpected, base::StringPiece(output, num_bytes_written));
98 TEST(HttpStreamParser, EncodeChunk_FullPayload) {
99 char output[kOutputSize];
101 const std::string kPayload(kMaxPayloadSize, '\xff');
102 // 3F4 = 1012 in hex.
103 const std::string kExpected = "3F4\r\n" + kPayload + "\r\n";
104 const int num_bytes_written =
105 HttpStreamParser::EncodeChunk(kPayload, output, sizeof(output));
106 ASSERT_EQ(kExpected.size(), static_cast<size_t>(num_bytes_written));
107 EXPECT_EQ(kExpected, base::StringPiece(output, num_bytes_written));
110 TEST(HttpStreamParser, EncodeChunk_TooLargePayload) {
111 char output[kOutputSize];
113 // The payload is one byte larger the output buffer size.
114 const std::string kPayload(kMaxPayloadSize + 1, '\xff');
115 const int num_bytes_written =
116 HttpStreamParser::EncodeChunk(kPayload, output, sizeof(output));
117 ASSERT_EQ(ERR_INVALID_ARGUMENT, num_bytes_written);
120 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_NoBody) {
121 // Shouldn't be merged if upload data is non-existent.
122 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody(
123 "some header", NULL));
126 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_EmptyBody) {
127 ScopedVector<UploadElementReader> element_readers;
128 scoped_ptr<UploadDataStream> body(
129 new ElementsUploadDataStream(element_readers.Pass(), 0));
130 ASSERT_EQ(OK, body->Init(CompletionCallback()));
131 // Shouldn't be merged if upload data is empty.
132 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody(
133 "some header", body.get()));
136 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_ChunkedBody) {
137 const std::string payload = "123";
138 scoped_ptr<ChunkedUploadDataStream> body(new ChunkedUploadDataStream(0));
139 body->AppendData(payload.data(), payload.size(), true);
140 ASSERT_EQ(OK, body->Init(TestCompletionCallback().callback()));
141 // Shouldn't be merged if upload data carries chunked data.
142 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody(
143 "some header", body.get()));
146 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_FileBody) {
148 ScopedVector<UploadElementReader> element_readers;
150 // Create an empty temporary file.
151 base::ScopedTempDir temp_dir;
152 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
153 base::FilePath temp_file_path;
154 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.path(),
155 &temp_file_path));
157 element_readers.push_back(
158 new UploadFileElementReader(base::ThreadTaskRunnerHandle::Get().get(),
159 temp_file_path, 0, 0, base::Time()));
161 scoped_ptr<UploadDataStream> body(
162 new ElementsUploadDataStream(element_readers.Pass(), 0));
163 TestCompletionCallback callback;
164 ASSERT_EQ(ERR_IO_PENDING, body->Init(callback.callback()));
165 ASSERT_EQ(OK, callback.WaitForResult());
166 // Shouldn't be merged if upload data carries a file, as it's not in-memory.
167 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody(
168 "some header", body.get()));
170 // UploadFileElementReaders may post clean-up tasks on destruction.
171 base::RunLoop().RunUntilIdle();
174 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_SmallBodyInMemory) {
175 ScopedVector<UploadElementReader> element_readers;
176 const std::string payload = "123";
177 element_readers.push_back(new UploadBytesElementReader(
178 payload.data(), payload.size()));
180 scoped_ptr<UploadDataStream> body(
181 new ElementsUploadDataStream(element_readers.Pass(), 0));
182 ASSERT_EQ(OK, body->Init(CompletionCallback()));
183 // Yes, should be merged if the in-memory body is small here.
184 ASSERT_TRUE(HttpStreamParser::ShouldMergeRequestHeadersAndBody(
185 "some header", body.get()));
188 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_LargeBodyInMemory) {
189 ScopedVector<UploadElementReader> element_readers;
190 const std::string payload(10000, 'a'); // 'a' x 10000.
191 element_readers.push_back(new UploadBytesElementReader(
192 payload.data(), payload.size()));
194 scoped_ptr<UploadDataStream> body(
195 new ElementsUploadDataStream(element_readers.Pass(), 0));
196 ASSERT_EQ(OK, body->Init(CompletionCallback()));
197 // Shouldn't be merged if the in-memory body is large here.
198 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody(
199 "some header", body.get()));
202 // Test to ensure the HttpStreamParser state machine does not get confused
203 // when sending a request with a chunked body with only one chunk that becomes
204 // available asynchronously.
205 TEST(HttpStreamParser, AsyncSingleChunkAndAsyncSocket) {
206 static const char kChunk[] = "Chunk";
208 MockWrite writes[] = {
209 MockWrite(ASYNC, 0,
210 "GET /one.html HTTP/1.1\r\n"
211 "Transfer-Encoding: chunked\r\n\r\n"),
212 MockWrite(ASYNC, 1, "5\r\nChunk\r\n"),
213 MockWrite(ASYNC, 2, "0\r\n\r\n"),
216 // The size of the response body, as reflected in the Content-Length of the
217 // MockRead below.
218 static const int kBodySize = 8;
220 MockRead reads[] = {
221 MockRead(ASYNC, 3, "HTTP/1.1 200 OK\r\n"),
222 MockRead(ASYNC, 4, "Content-Length: 8\r\n\r\n"),
223 MockRead(ASYNC, 5, "one.html"),
224 MockRead(SYNCHRONOUS, 0, 6), // EOF
227 ChunkedUploadDataStream upload_stream(0);
228 ASSERT_EQ(OK, upload_stream.Init(TestCompletionCallback().callback()));
230 SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes));
231 scoped_ptr<ClientSocketHandle> socket_handle =
232 CreateConnectedSocketHandle(&data);
234 HttpRequestInfo request_info;
235 request_info.method = "GET";
236 request_info.url = GURL("http://localhost");
237 request_info.upload_data_stream = &upload_stream;
239 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer);
240 HttpStreamParser parser(socket_handle.get(), &request_info, read_buffer.get(),
241 BoundNetLog());
243 HttpRequestHeaders request_headers;
244 request_headers.SetHeader("Transfer-Encoding", "chunked");
246 HttpResponseInfo response_info;
247 TestCompletionCallback callback;
248 // This will attempt to Write() the initial request and headers, which will
249 // complete asynchronously.
250 ASSERT_EQ(ERR_IO_PENDING,
251 parser.SendRequest("GET /one.html HTTP/1.1\r\n", request_headers,
252 &response_info, callback.callback()));
254 // Complete the initial request write. Callback should not have been invoked.
255 base::RunLoop().RunUntilIdle();
256 ASSERT_FALSE(callback.have_result());
258 // Now append the only chunk and wait for the callback.
259 upload_stream.AppendData(kChunk, arraysize(kChunk) - 1, true);
260 ASSERT_EQ(OK, callback.WaitForResult());
262 // Attempt to read the response status and the response headers.
263 ASSERT_EQ(ERR_IO_PENDING, parser.ReadResponseHeaders(callback.callback()));
264 ASSERT_GT(callback.WaitForResult(), 0);
266 // Finally, attempt to read the response body.
267 scoped_refptr<IOBuffer> body_buffer(new IOBuffer(kBodySize));
268 ASSERT_EQ(ERR_IO_PENDING,
269 parser.ReadResponseBody(body_buffer.get(), kBodySize,
270 callback.callback()));
271 ASSERT_EQ(kBodySize, callback.WaitForResult());
274 // Test to ensure the HttpStreamParser state machine does not get confused
275 // when sending a request with a chunked body with only one chunk that is
276 // available synchronously.
277 TEST(HttpStreamParser, SyncSingleChunkAndAsyncSocket) {
278 static const char kChunk[] = "Chunk";
280 MockWrite writes[] = {
281 MockWrite(ASYNC, 0,
282 "GET /one.html HTTP/1.1\r\n"
283 "Transfer-Encoding: chunked\r\n\r\n"),
284 MockWrite(ASYNC, 1, "5\r\nChunk\r\n"),
285 MockWrite(ASYNC, 2, "0\r\n\r\n"),
288 // The size of the response body, as reflected in the Content-Length of the
289 // MockRead below.
290 static const int kBodySize = 8;
292 MockRead reads[] = {
293 MockRead(ASYNC, 3, "HTTP/1.1 200 OK\r\n"),
294 MockRead(ASYNC, 4, "Content-Length: 8\r\n\r\n"),
295 MockRead(ASYNC, 5, "one.html"),
296 MockRead(SYNCHRONOUS, 0, 6), // EOF
299 ChunkedUploadDataStream upload_stream(0);
300 ASSERT_EQ(OK, upload_stream.Init(TestCompletionCallback().callback()));
301 // Append the only chunk.
302 upload_stream.AppendData(kChunk, arraysize(kChunk) - 1, true);
304 SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes));
305 scoped_ptr<ClientSocketHandle> socket_handle =
306 CreateConnectedSocketHandle(&data);
308 HttpRequestInfo request_info;
309 request_info.method = "GET";
310 request_info.url = GURL("http://localhost");
311 request_info.upload_data_stream = &upload_stream;
313 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer);
314 HttpStreamParser parser(socket_handle.get(), &request_info, read_buffer.get(),
315 BoundNetLog());
317 HttpRequestHeaders request_headers;
318 request_headers.SetHeader("Transfer-Encoding", "chunked");
320 HttpResponseInfo response_info;
321 TestCompletionCallback callback;
322 // This will attempt to Write() the initial request and headers, which will
323 // complete asynchronously.
324 ASSERT_EQ(ERR_IO_PENDING,
325 parser.SendRequest("GET /one.html HTTP/1.1\r\n", request_headers,
326 &response_info, callback.callback()));
327 ASSERT_EQ(OK, callback.WaitForResult());
329 // Attempt to read the response status and the response headers.
330 ASSERT_EQ(ERR_IO_PENDING, parser.ReadResponseHeaders(callback.callback()));
331 ASSERT_GT(callback.WaitForResult(), 0);
333 // Finally, attempt to read the response body.
334 scoped_refptr<IOBuffer> body_buffer(new IOBuffer(kBodySize));
335 ASSERT_EQ(ERR_IO_PENDING,
336 parser.ReadResponseBody(body_buffer.get(), kBodySize,
337 callback.callback()));
338 ASSERT_EQ(kBodySize, callback.WaitForResult());
341 // Test to ensure the HttpStreamParser state machine does not get confused
342 // when sending a request with a chunked body, where chunks become available
343 // asynchronously, over a socket where writes may also complete
344 // asynchronously.
345 // This is a regression test for http://crbug.com/132243
346 TEST(HttpStreamParser, AsyncChunkAndAsyncSocketWithMultipleChunks) {
347 // The chunks that will be written in the request, as reflected in the
348 // MockWrites below.
349 static const char kChunk1[] = "Chunk 1";
350 static const char kChunk2[] = "Chunky 2";
351 static const char kChunk3[] = "Test 3";
353 MockWrite writes[] = {
354 MockWrite(ASYNC, 0,
355 "GET /one.html HTTP/1.1\r\n"
356 "Transfer-Encoding: chunked\r\n\r\n"),
357 MockWrite(ASYNC, 1, "7\r\nChunk 1\r\n"),
358 MockWrite(ASYNC, 2, "8\r\nChunky 2\r\n"),
359 MockWrite(ASYNC, 3, "6\r\nTest 3\r\n"),
360 MockWrite(ASYNC, 4, "0\r\n\r\n"),
363 // The size of the response body, as reflected in the Content-Length of the
364 // MockRead below.
365 static const int kBodySize = 8;
367 MockRead reads[] = {
368 MockRead(ASYNC, 5, "HTTP/1.1 200 OK\r\n"),
369 MockRead(ASYNC, 6, "Content-Length: 8\r\n\r\n"),
370 MockRead(ASYNC, 7, "one.html"),
371 MockRead(SYNCHRONOUS, 0, 8), // EOF
374 ChunkedUploadDataStream upload_stream(0);
375 upload_stream.AppendData(kChunk1, arraysize(kChunk1) - 1, false);
376 ASSERT_EQ(OK, upload_stream.Init(TestCompletionCallback().callback()));
378 SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes));
379 scoped_ptr<ClientSocketHandle> socket_handle =
380 CreateConnectedSocketHandle(&data);
382 HttpRequestInfo request_info;
383 request_info.method = "GET";
384 request_info.url = GURL("http://localhost");
385 request_info.upload_data_stream = &upload_stream;
387 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer);
388 HttpStreamParser parser(
389 socket_handle.get(), &request_info, read_buffer.get(), BoundNetLog());
391 HttpRequestHeaders request_headers;
392 request_headers.SetHeader("Transfer-Encoding", "chunked");
394 HttpResponseInfo response_info;
395 TestCompletionCallback callback;
396 // This will attempt to Write() the initial request and headers, which will
397 // complete asynchronously.
398 ASSERT_EQ(ERR_IO_PENDING,
399 parser.SendRequest("GET /one.html HTTP/1.1\r\n", request_headers,
400 &response_info, callback.callback()));
401 ASSERT_FALSE(callback.have_result());
403 // Sending the request and the first chunk completes.
404 base::RunLoop().RunUntilIdle();
405 ASSERT_FALSE(callback.have_result());
407 // Now append another chunk.
408 upload_stream.AppendData(kChunk2, arraysize(kChunk2) - 1, false);
409 ASSERT_FALSE(callback.have_result());
411 // Add the final chunk, while the write for the second is still pending,
412 // which should not confuse the state machine.
413 upload_stream.AppendData(kChunk3, arraysize(kChunk3) - 1, true);
414 ASSERT_FALSE(callback.have_result());
416 // Wait for writes to complete.
417 ASSERT_EQ(OK, callback.WaitForResult());
419 // Attempt to read the response status and the response headers.
420 ASSERT_EQ(ERR_IO_PENDING, parser.ReadResponseHeaders(callback.callback()));
421 ASSERT_GT(callback.WaitForResult(), 0);
423 // Finally, attempt to read the response body.
424 scoped_refptr<IOBuffer> body_buffer(new IOBuffer(kBodySize));
425 ASSERT_EQ(ERR_IO_PENDING,
426 parser.ReadResponseBody(body_buffer.get(), kBodySize,
427 callback.callback()));
428 ASSERT_EQ(kBodySize, callback.WaitForResult());
431 // Test to ensure the HttpStreamParser state machine does not get confused
432 // when there's only one "chunk" with 0 bytes, and is received from the
433 // UploadStream only after sending the request headers successfully.
434 TEST(HttpStreamParser, AsyncEmptyChunkedUpload) {
435 MockWrite writes[] = {
436 MockWrite(ASYNC, 0,
437 "GET /one.html HTTP/1.1\r\n"
438 "Transfer-Encoding: chunked\r\n\r\n"),
439 MockWrite(ASYNC, 1, "0\r\n\r\n"),
442 // The size of the response body, as reflected in the Content-Length of the
443 // MockRead below.
444 const int kBodySize = 8;
446 MockRead reads[] = {
447 MockRead(ASYNC, 2, "HTTP/1.1 200 OK\r\n"),
448 MockRead(ASYNC, 3, "Content-Length: 8\r\n\r\n"),
449 MockRead(ASYNC, 4, "one.html"),
450 MockRead(SYNCHRONOUS, 0, 5), // EOF
453 ChunkedUploadDataStream upload_stream(0);
454 ASSERT_EQ(OK, upload_stream.Init(TestCompletionCallback().callback()));
456 SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes));
457 scoped_ptr<ClientSocketHandle> socket_handle =
458 CreateConnectedSocketHandle(&data);
460 HttpRequestInfo request_info;
461 request_info.method = "GET";
462 request_info.url = GURL("http://localhost");
463 request_info.upload_data_stream = &upload_stream;
465 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer);
466 HttpStreamParser parser(socket_handle.get(), &request_info, read_buffer.get(),
467 BoundNetLog());
469 HttpRequestHeaders request_headers;
470 request_headers.SetHeader("Transfer-Encoding", "chunked");
472 HttpResponseInfo response_info;
473 TestCompletionCallback callback;
474 // This will attempt to Write() the initial request and headers, which will
475 // complete asynchronously.
476 ASSERT_EQ(ERR_IO_PENDING,
477 parser.SendRequest("GET /one.html HTTP/1.1\r\n", request_headers,
478 &response_info, callback.callback()));
480 // Now append the terminal 0-byte "chunk".
481 upload_stream.AppendData(nullptr, 0, true);
482 ASSERT_FALSE(callback.have_result());
484 ASSERT_EQ(OK, callback.WaitForResult());
486 // Attempt to read the response status and the response headers.
487 ASSERT_EQ(ERR_IO_PENDING, parser.ReadResponseHeaders(callback.callback()));
488 ASSERT_GT(callback.WaitForResult(), 0);
490 // Finally, attempt to read the response body.
491 scoped_refptr<IOBuffer> body_buffer(new IOBuffer(kBodySize));
492 ASSERT_EQ(ERR_IO_PENDING,
493 parser.ReadResponseBody(body_buffer.get(), kBodySize,
494 callback.callback()));
495 ASSERT_EQ(kBodySize, callback.WaitForResult());
498 // Test to ensure the HttpStreamParser state machine does not get confused
499 // when there's only one "chunk" with 0 bytes, which was already appended before
500 // the request was started.
501 TEST(HttpStreamParser, SyncEmptyChunkedUpload) {
502 MockWrite writes[] = {
503 MockWrite(ASYNC, 0,
504 "GET /one.html HTTP/1.1\r\n"
505 "Transfer-Encoding: chunked\r\n\r\n"),
506 MockWrite(ASYNC, 1, "0\r\n\r\n"),
509 // The size of the response body, as reflected in the Content-Length of the
510 // MockRead below.
511 const int kBodySize = 8;
513 MockRead reads[] = {
514 MockRead(ASYNC, 2, "HTTP/1.1 200 OK\r\n"),
515 MockRead(ASYNC, 3, "Content-Length: 8\r\n\r\n"),
516 MockRead(ASYNC, 4, "one.html"),
517 MockRead(SYNCHRONOUS, 0, 5), // EOF
520 ChunkedUploadDataStream upload_stream(0);
521 ASSERT_EQ(OK, upload_stream.Init(TestCompletionCallback().callback()));
522 // Append final empty chunk.
523 upload_stream.AppendData(nullptr, 0, true);
525 SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes));
526 scoped_ptr<ClientSocketHandle> socket_handle =
527 CreateConnectedSocketHandle(&data);
529 HttpRequestInfo request_info;
530 request_info.method = "GET";
531 request_info.url = GURL("http://localhost");
532 request_info.upload_data_stream = &upload_stream;
534 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer);
535 HttpStreamParser parser(socket_handle.get(), &request_info, read_buffer.get(),
536 BoundNetLog());
538 HttpRequestHeaders request_headers;
539 request_headers.SetHeader("Transfer-Encoding", "chunked");
541 HttpResponseInfo response_info;
542 TestCompletionCallback callback;
543 // This will attempt to Write() the initial request and headers, which will
544 // complete asynchronously.
545 ASSERT_EQ(ERR_IO_PENDING,
546 parser.SendRequest("GET /one.html HTTP/1.1\r\n", request_headers,
547 &response_info, callback.callback()));
549 // Complete writing the request headers and body.
550 ASSERT_EQ(OK, callback.WaitForResult());
552 // Attempt to read the response status and the response headers.
553 ASSERT_EQ(ERR_IO_PENDING, parser.ReadResponseHeaders(callback.callback()));
554 ASSERT_GT(callback.WaitForResult(), 0);
556 // Finally, attempt to read the response body.
557 scoped_refptr<IOBuffer> body_buffer(new IOBuffer(kBodySize));
558 ASSERT_EQ(ERR_IO_PENDING,
559 parser.ReadResponseBody(body_buffer.get(), kBodySize,
560 callback.callback()));
561 ASSERT_EQ(kBodySize, callback.WaitForResult());
564 TEST(HttpStreamParser, TruncatedHeaders) {
565 MockRead truncated_status_reads[] = {
566 MockRead(SYNCHRONOUS, 1, "HTTP/1.1 20"),
567 MockRead(SYNCHRONOUS, 0, 2), // EOF
570 MockRead truncated_after_status_reads[] = {
571 MockRead(SYNCHRONOUS, 1, "HTTP/1.1 200 Ok\r\n"),
572 MockRead(SYNCHRONOUS, 0, 2), // EOF
575 MockRead truncated_in_header_reads[] = {
576 MockRead(SYNCHRONOUS, 1, "HTTP/1.1 200 Ok\r\nHead"),
577 MockRead(SYNCHRONOUS, 0, 2), // EOF
580 MockRead truncated_after_header_reads[] = {
581 MockRead(SYNCHRONOUS, 1, "HTTP/1.1 200 Ok\r\nHeader: foo\r\n"),
582 MockRead(SYNCHRONOUS, 0, 2), // EOF
585 MockRead truncated_after_final_newline_reads[] = {
586 MockRead(SYNCHRONOUS, 1, "HTTP/1.1 200 Ok\r\nHeader: foo\r\n\r"),
587 MockRead(SYNCHRONOUS, 0, 2), // EOF
590 MockRead not_truncated_reads[] = {
591 MockRead(SYNCHRONOUS, 1, "HTTP/1.1 200 Ok\r\nHeader: foo\r\n\r\n"),
592 MockRead(SYNCHRONOUS, 0, 2), // EOF
595 MockRead* reads[] = {
596 truncated_status_reads,
597 truncated_after_status_reads,
598 truncated_in_header_reads,
599 truncated_after_header_reads,
600 truncated_after_final_newline_reads,
601 not_truncated_reads,
604 MockWrite writes[] = {
605 MockWrite(SYNCHRONOUS, 0, "GET / HTTP/1.1\r\n\r\n"),
608 enum {
609 HTTP = 0,
610 HTTPS,
611 NUM_PROTOCOLS,
614 for (size_t protocol = 0; protocol < NUM_PROTOCOLS; protocol++) {
615 SCOPED_TRACE(protocol);
617 for (size_t i = 0; i < arraysize(reads); i++) {
618 SCOPED_TRACE(i);
619 SequencedSocketData data(reads[i], 2, writes, arraysize(writes));
620 scoped_ptr<ClientSocketHandle> socket_handle(
621 CreateConnectedSocketHandle(&data));
623 HttpRequestInfo request_info;
624 request_info.method = "GET";
625 if (protocol == HTTP) {
626 request_info.url = GURL("http://localhost");
627 } else {
628 request_info.url = GURL("https://localhost");
630 request_info.load_flags = LOAD_NORMAL;
632 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer);
633 HttpStreamParser parser(
634 socket_handle.get(), &request_info, read_buffer.get(), BoundNetLog());
636 HttpRequestHeaders request_headers;
637 HttpResponseInfo response_info;
638 TestCompletionCallback callback;
639 ASSERT_EQ(OK, parser.SendRequest("GET / HTTP/1.1\r\n", request_headers,
640 &response_info, callback.callback()));
642 int rv = parser.ReadResponseHeaders(callback.callback());
643 if (i == arraysize(reads) - 1) {
644 EXPECT_EQ(OK, rv);
645 EXPECT_TRUE(response_info.headers.get());
646 } else {
647 if (protocol == HTTP) {
648 EXPECT_EQ(ERR_CONNECTION_CLOSED, rv);
649 EXPECT_TRUE(response_info.headers.get());
650 } else {
651 EXPECT_EQ(ERR_RESPONSE_HEADERS_TRUNCATED, rv);
652 EXPECT_FALSE(response_info.headers.get());
659 // Confirm that on 101 response, the headers are parsed but the data that
660 // follows remains in the buffer.
661 TEST(HttpStreamParser, Websocket101Response) {
662 MockRead reads[] = {
663 MockRead(SYNCHRONOUS, 1,
664 "HTTP/1.1 101 Switching Protocols\r\n"
665 "Upgrade: websocket\r\n"
666 "Connection: Upgrade\r\n"
667 "\r\n"
668 "a fake websocket frame"),
671 MockWrite writes[] = {
672 MockWrite(SYNCHRONOUS, 0, "GET / HTTP/1.1\r\n\r\n"),
675 SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes));
676 scoped_ptr<ClientSocketHandle> socket_handle =
677 CreateConnectedSocketHandle(&data);
679 HttpRequestInfo request_info;
680 request_info.method = "GET";
681 request_info.url = GURL("http://localhost");
682 request_info.load_flags = LOAD_NORMAL;
684 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer);
685 HttpStreamParser parser(
686 socket_handle.get(), &request_info, read_buffer.get(), BoundNetLog());
688 HttpRequestHeaders request_headers;
689 HttpResponseInfo response_info;
690 TestCompletionCallback callback;
691 ASSERT_EQ(OK, parser.SendRequest("GET / HTTP/1.1\r\n", request_headers,
692 &response_info, callback.callback()));
694 EXPECT_EQ(OK, parser.ReadResponseHeaders(callback.callback()));
695 ASSERT_TRUE(response_info.headers.get());
696 EXPECT_EQ(101, response_info.headers->response_code());
697 EXPECT_TRUE(response_info.headers->HasHeaderValue("Connection", "Upgrade"));
698 EXPECT_TRUE(response_info.headers->HasHeaderValue("Upgrade", "websocket"));
699 EXPECT_EQ(read_buffer->capacity(), read_buffer->offset());
700 EXPECT_EQ("a fake websocket frame",
701 base::StringPiece(read_buffer->StartOfBuffer(),
702 read_buffer->capacity()));
705 // Helper class for constructing HttpStreamParser and running GET requests.
706 class SimpleGetRunner {
707 public:
708 SimpleGetRunner() : read_buffer_(new GrowableIOBuffer), sequence_number_(0) {
709 writes_.push_back(MockWrite(
710 SYNCHRONOUS, sequence_number_++, "GET / HTTP/1.1\r\n\r\n"));
713 HttpStreamParser* parser() { return parser_.get(); }
714 GrowableIOBuffer* read_buffer() { return read_buffer_.get(); }
715 HttpResponseInfo* response_info() { return &response_info_; }
717 void AddInitialData(const std::string& data) {
718 int offset = read_buffer_->offset();
719 int size = data.size();
720 read_buffer_->SetCapacity(offset + size);
721 memcpy(read_buffer_->StartOfBuffer() + offset, data.data(), size);
722 read_buffer_->set_offset(offset + size);
725 void AddRead(const std::string& data) {
726 reads_.push_back(MockRead(SYNCHRONOUS, sequence_number_++, data.data()));
729 void SetupParserAndSendRequest() {
730 reads_.push_back(MockRead(SYNCHRONOUS, 0, sequence_number_++)); // EOF
732 data_.reset(new SequencedSocketData(&reads_.front(), reads_.size(),
733 &writes_.front(), writes_.size()));
734 socket_handle_ = CreateConnectedSocketHandle(data_.get());
736 request_info_.method = "GET";
737 request_info_.url = GURL("http://localhost");
738 request_info_.load_flags = LOAD_NORMAL;
740 parser_.reset(new HttpStreamParser(
741 socket_handle_.get(), &request_info_, read_buffer(), BoundNetLog()));
743 TestCompletionCallback callback;
744 ASSERT_EQ(OK, parser_->SendRequest("GET / HTTP/1.1\r\n", request_headers_,
745 &response_info_, callback.callback()));
748 void ReadHeaders() {
749 TestCompletionCallback callback;
750 EXPECT_EQ(OK, parser_->ReadResponseHeaders(callback.callback()));
753 void ReadBody(int user_buf_len, int* read_lengths) {
754 TestCompletionCallback callback;
755 scoped_refptr<IOBuffer> buffer = new IOBuffer(user_buf_len);
756 int rv;
757 int i = 0;
758 while (true) {
759 rv = parser_->ReadResponseBody(
760 buffer.get(), user_buf_len, callback.callback());
761 EXPECT_EQ(read_lengths[i], rv);
762 i++;
763 if (rv <= 0)
764 return;
768 private:
769 HttpRequestHeaders request_headers_;
770 HttpResponseInfo response_info_;
771 HttpRequestInfo request_info_;
772 scoped_refptr<GrowableIOBuffer> read_buffer_;
773 std::vector<MockRead> reads_;
774 std::vector<MockWrite> writes_;
775 scoped_ptr<ClientSocketHandle> socket_handle_;
776 scoped_ptr<SequencedSocketData> data_;
777 scoped_ptr<HttpStreamParser> parser_;
778 int sequence_number_;
781 // Test that HTTP/0.9 response size is correctly calculated.
782 TEST(HttpStreamParser, ReceivedBytesNoHeaders) {
783 std::string response = "hello\r\nworld\r\n";
785 SimpleGetRunner get_runner;
786 get_runner.AddRead(response);
787 get_runner.SetupParserAndSendRequest();
788 get_runner.ReadHeaders();
789 EXPECT_EQ(0, get_runner.parser()->received_bytes());
790 int response_size = response.size();
791 int read_lengths[] = {response_size, 0};
792 get_runner.ReadBody(response_size, read_lengths);
793 EXPECT_EQ(response_size, get_runner.parser()->received_bytes());
796 // Test basic case where there is no keep-alive or extra data from the socket,
797 // and the entire response is received in a single read.
798 TEST(HttpStreamParser, ReceivedBytesNormal) {
799 std::string headers = "HTTP/1.1 200 OK\r\n"
800 "Content-Length: 7\r\n\r\n";
801 std::string body = "content";
802 std::string response = headers + body;
804 SimpleGetRunner get_runner;
805 get_runner.AddRead(response);
806 get_runner.SetupParserAndSendRequest();
807 get_runner.ReadHeaders();
808 int64 headers_size = headers.size();
809 EXPECT_EQ(headers_size, get_runner.parser()->received_bytes());
810 int body_size = body.size();
811 int read_lengths[] = {body_size, 0};
812 get_runner.ReadBody(body_size, read_lengths);
813 int64 response_size = response.size();
814 EXPECT_EQ(response_size, get_runner.parser()->received_bytes());
817 // Test that bytes that represent "next" response are not counted
818 // as current response "received_bytes".
819 TEST(HttpStreamParser, ReceivedBytesExcludesNextResponse) {
820 std::string headers = "HTTP/1.1 200 OK\r\n"
821 "Content-Length: 8\r\n\r\n";
822 std::string body = "content8";
823 std::string response = headers + body;
824 std::string next_response = "HTTP/1.1 200 OK\r\n\r\nFOO";
825 std::string data = response + next_response;
827 SimpleGetRunner get_runner;
828 get_runner.AddRead(data);
829 get_runner.SetupParserAndSendRequest();
830 get_runner.ReadHeaders();
831 EXPECT_EQ(39, get_runner.parser()->received_bytes());
832 int64 headers_size = headers.size();
833 EXPECT_EQ(headers_size, get_runner.parser()->received_bytes());
834 int body_size = body.size();
835 int read_lengths[] = {body_size, 0};
836 get_runner.ReadBody(body_size, read_lengths);
837 int64 response_size = response.size();
838 EXPECT_EQ(response_size, get_runner.parser()->received_bytes());
839 int64 next_response_size = next_response.size();
840 EXPECT_EQ(next_response_size, get_runner.read_buffer()->offset());
843 // Test that "received_bytes" calculation works fine when last read
844 // contains more data than requested by user.
845 // We send data in two reads:
846 // 1) Headers + beginning of response
847 // 2) remaining part of response + next response start
848 // We setup user read buffer so it fully accepts the beginnig of response
849 // body, but it is larger that remaining part of body.
850 TEST(HttpStreamParser, ReceivedBytesMultiReadExcludesNextResponse) {
851 std::string headers = "HTTP/1.1 200 OK\r\n"
852 "Content-Length: 36\r\n\r\n";
853 int64 user_buf_len = 32;
854 std::string body_start = std::string(user_buf_len, '#');
855 int body_start_size = body_start.size();
856 EXPECT_EQ(user_buf_len, body_start_size);
857 std::string response_start = headers + body_start;
858 std::string body_end = "abcd";
859 std::string next_response = "HTTP/1.1 200 OK\r\n\r\nFOO";
860 std::string response_end = body_end + next_response;
862 SimpleGetRunner get_runner;
863 get_runner.AddRead(response_start);
864 get_runner.AddRead(response_end);
865 get_runner.SetupParserAndSendRequest();
866 get_runner.ReadHeaders();
867 int64 headers_size = headers.size();
868 EXPECT_EQ(headers_size, get_runner.parser()->received_bytes());
869 int body_end_size = body_end.size();
870 int read_lengths[] = {body_start_size, body_end_size, 0};
871 get_runner.ReadBody(body_start_size, read_lengths);
872 int64 response_size = response_start.size() + body_end_size;
873 EXPECT_EQ(response_size, get_runner.parser()->received_bytes());
874 int64 next_response_size = next_response.size();
875 EXPECT_EQ(next_response_size, get_runner.read_buffer()->offset());
878 // Test that "received_bytes" calculation works fine when there is no
879 // network activity at all; that is when all data is read from read buffer.
880 // In this case read buffer contains two responses. We expect that only
881 // bytes that correspond to the first one are taken into account.
882 TEST(HttpStreamParser, ReceivedBytesFromReadBufExcludesNextResponse) {
883 std::string headers = "HTTP/1.1 200 OK\r\n"
884 "Content-Length: 7\r\n\r\n";
885 std::string body = "content";
886 std::string response = headers + body;
887 std::string next_response = "HTTP/1.1 200 OK\r\n\r\nFOO";
888 std::string data = response + next_response;
890 SimpleGetRunner get_runner;
891 get_runner.AddInitialData(data);
892 get_runner.SetupParserAndSendRequest();
893 get_runner.ReadHeaders();
894 int64 headers_size = headers.size();
895 EXPECT_EQ(headers_size, get_runner.parser()->received_bytes());
896 int body_size = body.size();
897 int read_lengths[] = {body_size, 0};
898 get_runner.ReadBody(body_size, read_lengths);
899 int64 response_size = response.size();
900 EXPECT_EQ(response_size, get_runner.parser()->received_bytes());
901 int64 next_response_size = next_response.size();
902 EXPECT_EQ(next_response_size, get_runner.read_buffer()->offset());
905 // Test calculating "received_bytes" when part of request has been already
906 // loaded and placed to read buffer by previous stream parser.
907 TEST(HttpStreamParser, ReceivedBytesUseReadBuf) {
908 std::string buffer = "HTTP/1.1 200 OK\r\n";
909 std::string remaining_headers = "Content-Length: 7\r\n\r\n";
910 int64 headers_size = buffer.size() + remaining_headers.size();
911 std::string body = "content";
912 std::string response = remaining_headers + body;
914 SimpleGetRunner get_runner;
915 get_runner.AddInitialData(buffer);
916 get_runner.AddRead(response);
917 get_runner.SetupParserAndSendRequest();
918 get_runner.ReadHeaders();
919 EXPECT_EQ(headers_size, get_runner.parser()->received_bytes());
920 int body_size = body.size();
921 int read_lengths[] = {body_size, 0};
922 get_runner.ReadBody(body_size, read_lengths);
923 EXPECT_EQ(headers_size + body_size, get_runner.parser()->received_bytes());
924 EXPECT_EQ(0, get_runner.read_buffer()->offset());
927 // Test the case when the resulting read_buf contains both unused bytes and
928 // bytes ejected by chunked-encoding filter.
929 TEST(HttpStreamParser, ReceivedBytesChunkedTransferExcludesNextResponse) {
930 std::string response = "HTTP/1.1 200 OK\r\n"
931 "Transfer-Encoding: chunked\r\n\r\n"
932 "7\r\nChunk 1\r\n"
933 "8\r\nChunky 2\r\n"
934 "6\r\nTest 3\r\n"
935 "0\r\n\r\n";
936 std::string next_response = "foo bar\r\n";
937 std::string data = response + next_response;
939 SimpleGetRunner get_runner;
940 get_runner.AddInitialData(data);
941 get_runner.SetupParserAndSendRequest();
942 get_runner.ReadHeaders();
943 int read_lengths[] = {4, 3, 6, 2, 6, 0};
944 get_runner.ReadBody(7, read_lengths);
945 int64 response_size = response.size();
946 EXPECT_EQ(response_size, get_runner.parser()->received_bytes());
947 int64 next_response_size = next_response.size();
948 EXPECT_EQ(next_response_size, get_runner.read_buffer()->offset());
951 // Test that data transfered in multiple reads is correctly processed.
952 // We feed data into 4-bytes reads. Also we set length of read
953 // buffer to 5-bytes to test all possible buffer misaligments.
954 TEST(HttpStreamParser, ReceivedBytesMultipleReads) {
955 std::string headers = "HTTP/1.1 200 OK\r\n"
956 "Content-Length: 33\r\n\r\n";
957 std::string body = "foo bar baz\r\n"
958 "sputnik mir babushka";
959 std::string response = headers + body;
961 size_t receive_length = 4;
962 std::vector<std::string> blocks;
963 for (size_t i = 0; i < response.size(); i += receive_length) {
964 size_t length = std::min(receive_length, response.size() - i);
965 blocks.push_back(response.substr(i, length));
968 SimpleGetRunner get_runner;
969 for (std::vector<std::string>::size_type i = 0; i < blocks.size(); ++i)
970 get_runner.AddRead(blocks[i]);
971 get_runner.SetupParserAndSendRequest();
972 get_runner.ReadHeaders();
973 int64 headers_size = headers.size();
974 EXPECT_EQ(headers_size, get_runner.parser()->received_bytes());
975 int read_lengths[] = {1, 4, 4, 4, 4, 4, 4, 4, 4, 0};
976 get_runner.ReadBody(receive_length + 1, read_lengths);
977 int64 response_size = response.size();
978 EXPECT_EQ(response_size, get_runner.parser()->received_bytes());
981 // Test that "continue" HTTP header is counted as "received_bytes".
982 TEST(HttpStreamParser, ReceivedBytesIncludesContinueHeader) {
983 std::string status100 = "HTTP/1.1 100 OK\r\n\r\n";
984 std::string headers = "HTTP/1.1 200 OK\r\n"
985 "Content-Length: 7\r\n\r\n";
986 int64 headers_size = status100.size() + headers.size();
987 std::string body = "content";
988 std::string response = headers + body;
990 SimpleGetRunner get_runner;
991 get_runner.AddRead(status100);
992 get_runner.AddRead(response);
993 get_runner.SetupParserAndSendRequest();
994 get_runner.ReadHeaders();
995 EXPECT_EQ(100, get_runner.response_info()->headers->response_code());
996 int64 status100_size = status100.size();
997 EXPECT_EQ(status100_size, get_runner.parser()->received_bytes());
998 get_runner.ReadHeaders();
999 EXPECT_EQ(200, get_runner.response_info()->headers->response_code());
1000 EXPECT_EQ(headers_size, get_runner.parser()->received_bytes());
1001 int64 response_size = headers_size + body.size();
1002 int body_size = body.size();
1003 int read_lengths[] = {body_size, 0};
1004 get_runner.ReadBody(body_size, read_lengths);
1005 EXPECT_EQ(response_size, get_runner.parser()->received_bytes());
1008 // Test that an HttpStreamParser can be read from after it's received headers
1009 // and data structures owned by its owner have been deleted. This happens
1010 // when a ResponseBodyDrainer is used.
1011 TEST(HttpStreamParser, ReadAfterUnownedObjectsDestroyed) {
1012 MockWrite writes[] = {
1013 MockWrite(SYNCHRONOUS, 0,
1014 "GET /foo.html HTTP/1.1\r\n\r\n"),
1017 const int kBodySize = 1;
1018 MockRead reads[] = {
1019 MockRead(SYNCHRONOUS, 1, "HTTP/1.1 200 OK\r\n"),
1020 MockRead(SYNCHRONOUS, 2, "Content-Length: 1\r\n\r\n"),
1021 MockRead(SYNCHRONOUS, 3, "Connection: Keep-Alive\r\n\r\n"),
1022 MockRead(SYNCHRONOUS, 4, "1"),
1023 MockRead(SYNCHRONOUS, 0, 5), // EOF
1026 SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes));
1027 scoped_ptr<ClientSocketHandle> socket_handle =
1028 CreateConnectedSocketHandle(&data);
1030 scoped_ptr<HttpRequestInfo> request_info(new HttpRequestInfo());
1031 request_info->method = "GET";
1032 request_info->url = GURL("http://somewhere/foo.html");
1034 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer);
1035 HttpStreamParser parser(socket_handle.get(), request_info.get(),
1036 read_buffer.get(), BoundNetLog());
1038 scoped_ptr<HttpRequestHeaders> request_headers(new HttpRequestHeaders());
1039 scoped_ptr<HttpResponseInfo> response_info(new HttpResponseInfo());
1040 TestCompletionCallback callback;
1041 ASSERT_EQ(OK, parser.SendRequest("GET /foo.html HTTP/1.1\r\n",
1042 *request_headers, response_info.get(), callback.callback()));
1043 ASSERT_EQ(OK, parser.ReadResponseHeaders(callback.callback()));
1045 // If the object that owns the HttpStreamParser is deleted, it takes the
1046 // objects passed to the HttpStreamParser with it.
1047 request_info.reset();
1048 request_headers.reset();
1049 response_info.reset();
1051 scoped_refptr<IOBuffer> body_buffer(new IOBuffer(kBodySize));
1052 ASSERT_EQ(kBodySize, parser.ReadResponseBody(
1053 body_buffer.get(), kBodySize, callback.callback()));
1056 } // namespace
1058 } // namespace net