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