OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/http/http_stream_parser.h" | 5 #include "net/http/http_stream_parser.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
12 #include "base/strings/string_piece.h" | 12 #include "base/strings/string_piece.h" |
13 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
14 #include "net/base/io_buffer.h" | 14 #include "net/base/io_buffer.h" |
15 #include "net/base/net_errors.h" | 15 #include "net/base/net_errors.h" |
16 #include "net/base/test_completion_callback.h" | 16 #include "net/base/test_completion_callback.h" |
17 #include "net/base/upload_bytes_element_reader.h" | 17 #include "net/base/upload_bytes_element_reader.h" |
18 #include "net/base/upload_data_stream.h" | 18 #include "net/base/upload_data_stream.h" |
19 #include "net/base/upload_file_element_reader.h" | 19 #include "net/base/upload_file_element_reader.h" |
20 #include "net/http/http_request_headers.h" | 20 #include "net/http/http_request_headers.h" |
21 #include "net/http/http_request_info.h" | 21 #include "net/http/http_request_info.h" |
| 22 #include "net/http/http_response_headers.h" |
22 #include "net/http/http_response_info.h" | 23 #include "net/http/http_response_info.h" |
23 #include "net/socket/client_socket_handle.h" | 24 #include "net/socket/client_socket_handle.h" |
24 #include "net/socket/socket_test_util.h" | 25 #include "net/socket/socket_test_util.h" |
25 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
26 #include "url/gurl.h" | 27 #include "url/gurl.h" |
27 | 28 |
28 namespace net { | 29 namespace net { |
29 | 30 |
30 const size_t kOutputSize = 1024; // Just large enough for this test. | 31 const size_t kOutputSize = 1024; // Just large enough for this test. |
31 // The number of bytes that can fit in a buffer of kOutputSize. | 32 // The number of bytes that can fit in a buffer of kOutputSize. |
(...skipping 383 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
415 EXPECT_TRUE(response_info.headers.get()); | 416 EXPECT_TRUE(response_info.headers.get()); |
416 } else { | 417 } else { |
417 EXPECT_EQ(ERR_RESPONSE_HEADERS_TRUNCATED, rv); | 418 EXPECT_EQ(ERR_RESPONSE_HEADERS_TRUNCATED, rv); |
418 EXPECT_FALSE(response_info.headers.get()); | 419 EXPECT_FALSE(response_info.headers.get()); |
419 } | 420 } |
420 } | 421 } |
421 } | 422 } |
422 } | 423 } |
423 } | 424 } |
424 | 425 |
| 426 // Confirm that on 101 response, the headers are parsed but the data that |
| 427 // follows remains in the buffer. |
| 428 TEST(HttpStreamParser, Websocket101Response) { |
| 429 MockRead reads[] = { |
| 430 MockRead(SYNCHRONOUS, 1, |
| 431 "HTTP/1.1 101 Switching Protocols\r\n" |
| 432 "Upgrade: websocket\r\n" |
| 433 "Connection: Upgrade\r\n" |
| 434 "\r\n" |
| 435 "a fake websocket frame"), |
| 436 }; |
| 437 |
| 438 MockWrite writes[] = { |
| 439 MockWrite(SYNCHRONOUS, 0, "GET / HTTP/1.1\r\n\r\n"), |
| 440 }; |
| 441 |
| 442 DeterministicSocketData data(reads, arraysize(reads), |
| 443 writes, arraysize(writes)); |
| 444 data.set_connect_data(MockConnect(SYNCHRONOUS, OK)); |
| 445 data.SetStop(2); |
| 446 |
| 447 scoped_ptr<DeterministicMockTCPClientSocket> transport( |
| 448 new DeterministicMockTCPClientSocket(NULL, &data)); |
| 449 data.set_delegate(transport->AsWeakPtr()); |
| 450 |
| 451 TestCompletionCallback callback; |
| 452 int rv = transport->Connect(callback.callback()); |
| 453 rv = callback.GetResult(rv); |
| 454 ASSERT_EQ(OK, rv); |
| 455 |
| 456 scoped_ptr<ClientSocketHandle> socket_handle(new ClientSocketHandle); |
| 457 socket_handle->SetSocket(transport.PassAs<StreamSocket>()); |
| 458 |
| 459 HttpRequestInfo request_info; |
| 460 request_info.method = "GET"; |
| 461 request_info.url = GURL("http://localhost"); |
| 462 request_info.load_flags = LOAD_NORMAL; |
| 463 |
| 464 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer); |
| 465 HttpStreamParser parser( |
| 466 socket_handle.get(), &request_info, read_buffer.get(), BoundNetLog()); |
| 467 |
| 468 HttpRequestHeaders request_headers; |
| 469 HttpResponseInfo response_info; |
| 470 rv = parser.SendRequest("GET / HTTP/1.1\r\n", request_headers, |
| 471 &response_info, callback.callback()); |
| 472 ASSERT_EQ(OK, rv); |
| 473 |
| 474 rv = parser.ReadResponseHeaders(callback.callback()); |
| 475 EXPECT_EQ(OK, rv); |
| 476 ASSERT_TRUE(response_info.headers.get()); |
| 477 EXPECT_EQ(101, response_info.headers->response_code()); |
| 478 EXPECT_TRUE(response_info.headers->HasHeaderValue("Connection", "Upgrade")); |
| 479 EXPECT_TRUE(response_info.headers->HasHeaderValue("Upgrade", "websocket")); |
| 480 EXPECT_EQ(read_buffer->capacity(), read_buffer->offset()); |
| 481 EXPECT_EQ("a fake websocket frame", |
| 482 base::StringPiece(read_buffer->StartOfBuffer(), |
| 483 read_buffer->capacity())); |
| 484 } |
| 485 |
| 486 |
425 } // namespace net | 487 } // namespace net |
OLD | NEW |