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 <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <memory> | 10 #include <memory> |
(...skipping 1109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1120 HttpRequestInfo request_info_; | 1120 HttpRequestInfo request_info_; |
1121 scoped_refptr<GrowableIOBuffer> read_buffer_; | 1121 scoped_refptr<GrowableIOBuffer> read_buffer_; |
1122 std::vector<MockRead> reads_; | 1122 std::vector<MockRead> reads_; |
1123 std::vector<MockWrite> writes_; | 1123 std::vector<MockWrite> writes_; |
1124 std::unique_ptr<ClientSocketHandle> socket_handle_; | 1124 std::unique_ptr<ClientSocketHandle> socket_handle_; |
1125 std::unique_ptr<SequencedSocketData> data_; | 1125 std::unique_ptr<SequencedSocketData> data_; |
1126 std::unique_ptr<HttpStreamParser> parser_; | 1126 std::unique_ptr<HttpStreamParser> parser_; |
1127 int sequence_number_; | 1127 int sequence_number_; |
1128 }; | 1128 }; |
1129 | 1129 |
1130 // Test that HTTP/0.9 response size is correctly calculated. | 1130 // Test that HTTP/0.9 response correctly results in an error. |
1131 TEST(HttpStreamParser, ReceivedBytesNoHeaders) { | 1131 TEST(HttpStreamParser, ReceivedBytesNoHeaders) { |
1132 std::string response = "hello\r\nworld\r\n"; | 1132 MockWrite writes[] = { |
| 1133 MockWrite(SYNCHRONOUS, 0, "GET / HTTP/1.1\r\n\r\n"), |
| 1134 }; |
1133 | 1135 |
1134 SimpleGetRunner get_runner; | 1136 MockRead reads[] = { |
1135 get_runner.AddRead(response); | 1137 MockRead(SYNCHRONOUS, 1, "hello\r\nworld\r\n"), |
1136 get_runner.SetupParserAndSendRequest(); | 1138 MockRead(SYNCHRONOUS, OK, 2), |
1137 get_runner.ReadHeaders(); | 1139 }; |
1138 EXPECT_EQ(0, get_runner.parser()->received_bytes()); | 1140 |
1139 int response_size = response.size(); | 1141 SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes)); |
1140 int read_lengths[] = {response_size, 0}; | 1142 std::unique_ptr<ClientSocketHandle> socket_handle = |
1141 get_runner.ReadBody(response_size, read_lengths); | 1143 CreateConnectedSocketHandle(&data); |
1142 EXPECT_EQ(response_size, get_runner.parser()->received_bytes()); | 1144 |
1143 EXPECT_EQ(HttpResponseInfo::CONNECTION_INFO_HTTP0_9, | 1145 HttpRequestInfo request; |
1144 get_runner.response_info()->connection_info); | 1146 request.method = "GET"; |
| 1147 request.url = GURL("http://localhost"); |
| 1148 |
| 1149 HttpStreamParser parser(socket_handle.get(), &request, new GrowableIOBuffer(), |
| 1150 BoundNetLog()); |
| 1151 |
| 1152 HttpRequestHeaders headers; |
| 1153 HttpResponseInfo response; |
| 1154 TestCompletionCallback callback; |
| 1155 int result = parser.SendRequest("GET / HTTP/1.1\r\n", headers, &response, |
| 1156 callback.callback()); |
| 1157 EXPECT_THAT(callback.GetResult(result), IsOk()); |
| 1158 result = parser.ReadResponseHeaders(callback.callback()); |
| 1159 EXPECT_THAT(callback.GetResult(result), IsError(ERR_INVALID_HTTP_RESPONSE)); |
1145 } | 1160 } |
1146 | 1161 |
1147 // Test basic case where there is no keep-alive or extra data from the socket, | 1162 // Test basic case where there is no keep-alive or extra data from the socket, |
1148 // and the entire response is received in a single read. | 1163 // and the entire response is received in a single read. |
1149 TEST(HttpStreamParser, ReceivedBytesNormal) { | 1164 TEST(HttpStreamParser, ReceivedBytesNormal) { |
1150 std::string headers = | 1165 std::string headers = |
1151 "HTTP/1.0 200 OK\r\n" | 1166 "HTTP/1.0 200 OK\r\n" |
1152 "Content-Length: 7\r\n\r\n"; | 1167 "Content-Length: 7\r\n\r\n"; |
1153 std::string body = "content"; | 1168 std::string body = "content"; |
1154 std::string response = headers + body; | 1169 std::string response = headers + body; |
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1408 ASSERT_EQ(kBodySize, parser.ReadResponseBody( | 1423 ASSERT_EQ(kBodySize, parser.ReadResponseBody( |
1409 body_buffer.get(), kBodySize, callback.callback())); | 1424 body_buffer.get(), kBodySize, callback.callback())); |
1410 | 1425 |
1411 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes()); | 1426 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes()); |
1412 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)), parser.received_bytes()); | 1427 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)), parser.received_bytes()); |
1413 } | 1428 } |
1414 | 1429 |
1415 } // namespace | 1430 } // namespace |
1416 | 1431 |
1417 } // namespace net | 1432 } // namespace net |
OLD | NEW |