| 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 1108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1119 | 1119 |
| 1120 SimpleGetRunner get_runner; | 1120 SimpleGetRunner get_runner; |
| 1121 get_runner.AddRead(response); | 1121 get_runner.AddRead(response); |
| 1122 get_runner.SetupParserAndSendRequest(); | 1122 get_runner.SetupParserAndSendRequest(); |
| 1123 get_runner.ReadHeaders(); | 1123 get_runner.ReadHeaders(); |
| 1124 EXPECT_EQ(0, get_runner.parser()->received_bytes()); | 1124 EXPECT_EQ(0, get_runner.parser()->received_bytes()); |
| 1125 int response_size = response.size(); | 1125 int response_size = response.size(); |
| 1126 int read_lengths[] = {response_size, 0}; | 1126 int read_lengths[] = {response_size, 0}; |
| 1127 get_runner.ReadBody(response_size, read_lengths); | 1127 get_runner.ReadBody(response_size, read_lengths); |
| 1128 EXPECT_EQ(response_size, get_runner.parser()->received_bytes()); | 1128 EXPECT_EQ(response_size, get_runner.parser()->received_bytes()); |
| 1129 EXPECT_EQ(HttpResponseInfo::CONNECTION_INFO_HTTP0_9, |
| 1130 get_runner.response_info()->connection_info); |
| 1129 } | 1131 } |
| 1130 | 1132 |
| 1131 // Test basic case where there is no keep-alive or extra data from the socket, | 1133 // Test basic case where there is no keep-alive or extra data from the socket, |
| 1132 // and the entire response is received in a single read. | 1134 // and the entire response is received in a single read. |
| 1133 TEST(HttpStreamParser, ReceivedBytesNormal) { | 1135 TEST(HttpStreamParser, ReceivedBytesNormal) { |
| 1134 std::string headers = "HTTP/1.1 200 OK\r\n" | 1136 std::string headers = |
| 1137 "HTTP/1.0 200 OK\r\n" |
| 1135 "Content-Length: 7\r\n\r\n"; | 1138 "Content-Length: 7\r\n\r\n"; |
| 1136 std::string body = "content"; | 1139 std::string body = "content"; |
| 1137 std::string response = headers + body; | 1140 std::string response = headers + body; |
| 1138 | 1141 |
| 1139 SimpleGetRunner get_runner; | 1142 SimpleGetRunner get_runner; |
| 1140 get_runner.AddRead(response); | 1143 get_runner.AddRead(response); |
| 1141 get_runner.SetupParserAndSendRequest(); | 1144 get_runner.SetupParserAndSendRequest(); |
| 1142 get_runner.ReadHeaders(); | 1145 get_runner.ReadHeaders(); |
| 1143 int64_t headers_size = headers.size(); | 1146 int64_t headers_size = headers.size(); |
| 1144 EXPECT_EQ(headers_size, get_runner.parser()->received_bytes()); | 1147 EXPECT_EQ(headers_size, get_runner.parser()->received_bytes()); |
| 1145 int body_size = body.size(); | 1148 int body_size = body.size(); |
| 1146 int read_lengths[] = {body_size, 0}; | 1149 int read_lengths[] = {body_size, 0}; |
| 1147 get_runner.ReadBody(body_size, read_lengths); | 1150 get_runner.ReadBody(body_size, read_lengths); |
| 1148 int64_t response_size = response.size(); | 1151 int64_t response_size = response.size(); |
| 1149 EXPECT_EQ(response_size, get_runner.parser()->received_bytes()); | 1152 EXPECT_EQ(response_size, get_runner.parser()->received_bytes()); |
| 1153 EXPECT_EQ(HttpResponseInfo::CONNECTION_INFO_HTTP1_0, |
| 1154 get_runner.response_info()->connection_info); |
| 1150 } | 1155 } |
| 1151 | 1156 |
| 1152 // Test that bytes that represent "next" response are not counted | 1157 // Test that bytes that represent "next" response are not counted |
| 1153 // as current response "received_bytes". | 1158 // as current response "received_bytes". |
| 1154 TEST(HttpStreamParser, ReceivedBytesExcludesNextResponse) { | 1159 TEST(HttpStreamParser, ReceivedBytesExcludesNextResponse) { |
| 1155 std::string headers = "HTTP/1.1 200 OK\r\n" | 1160 std::string headers = "HTTP/1.1 200 OK\r\n" |
| 1156 "Content-Length: 8\r\n\r\n"; | 1161 "Content-Length: 8\r\n\r\n"; |
| 1157 std::string body = "content8"; | 1162 std::string body = "content8"; |
| 1158 std::string response = headers + body; | 1163 std::string response = headers + body; |
| 1159 std::string next_response = "HTTP/1.1 200 OK\r\n\r\nFOO"; | 1164 std::string next_response = "HTTP/1.1 200 OK\r\n\r\nFOO"; |
| 1160 std::string data = response + next_response; | 1165 std::string data = response + next_response; |
| 1161 | 1166 |
| 1162 SimpleGetRunner get_runner; | 1167 SimpleGetRunner get_runner; |
| 1163 get_runner.AddRead(data); | 1168 get_runner.AddRead(data); |
| 1164 get_runner.SetupParserAndSendRequest(); | 1169 get_runner.SetupParserAndSendRequest(); |
| 1165 get_runner.ReadHeaders(); | 1170 get_runner.ReadHeaders(); |
| 1166 EXPECT_EQ(39, get_runner.parser()->received_bytes()); | 1171 EXPECT_EQ(39, get_runner.parser()->received_bytes()); |
| 1167 int64_t headers_size = headers.size(); | 1172 int64_t headers_size = headers.size(); |
| 1168 EXPECT_EQ(headers_size, get_runner.parser()->received_bytes()); | 1173 EXPECT_EQ(headers_size, get_runner.parser()->received_bytes()); |
| 1169 int body_size = body.size(); | 1174 int body_size = body.size(); |
| 1170 int read_lengths[] = {body_size, 0}; | 1175 int read_lengths[] = {body_size, 0}; |
| 1171 get_runner.ReadBody(body_size, read_lengths); | 1176 get_runner.ReadBody(body_size, read_lengths); |
| 1172 int64_t response_size = response.size(); | 1177 int64_t response_size = response.size(); |
| 1173 EXPECT_EQ(response_size, get_runner.parser()->received_bytes()); | 1178 EXPECT_EQ(response_size, get_runner.parser()->received_bytes()); |
| 1174 int64_t next_response_size = next_response.size(); | 1179 int64_t next_response_size = next_response.size(); |
| 1175 EXPECT_EQ(next_response_size, get_runner.read_buffer()->offset()); | 1180 EXPECT_EQ(next_response_size, get_runner.read_buffer()->offset()); |
| 1181 EXPECT_EQ(HttpResponseInfo::CONNECTION_INFO_HTTP1_1, |
| 1182 get_runner.response_info()->connection_info); |
| 1176 } | 1183 } |
| 1177 | 1184 |
| 1178 // Test that "received_bytes" calculation works fine when last read | 1185 // Test that "received_bytes" calculation works fine when last read |
| 1179 // contains more data than requested by user. | 1186 // contains more data than requested by user. |
| 1180 // We send data in two reads: | 1187 // We send data in two reads: |
| 1181 // 1) Headers + beginning of response | 1188 // 1) Headers + beginning of response |
| 1182 // 2) remaining part of response + next response start | 1189 // 2) remaining part of response + next response start |
| 1183 // We setup user read buffer so it fully accepts the beginnig of response | 1190 // We setup user read buffer so it fully accepts the beginnig of response |
| 1184 // body, but it is larger that remaining part of body. | 1191 // body, but it is larger that remaining part of body. |
| 1185 TEST(HttpStreamParser, ReceivedBytesMultiReadExcludesNextResponse) { | 1192 TEST(HttpStreamParser, ReceivedBytesMultiReadExcludesNextResponse) { |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1387 ASSERT_EQ(kBodySize, parser.ReadResponseBody( | 1394 ASSERT_EQ(kBodySize, parser.ReadResponseBody( |
| 1388 body_buffer.get(), kBodySize, callback.callback())); | 1395 body_buffer.get(), kBodySize, callback.callback())); |
| 1389 | 1396 |
| 1390 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes()); | 1397 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes()); |
| 1391 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)), parser.received_bytes()); | 1398 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)), parser.received_bytes()); |
| 1392 } | 1399 } |
| 1393 | 1400 |
| 1394 } // namespace | 1401 } // namespace |
| 1395 | 1402 |
| 1396 } // namespace net | 1403 } // namespace net |
| OLD | NEW |