OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "content/browser/loader/url_loader_factory_impl.h" | 5 #include "content/browser/loader/url_loader_factory_impl.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <string> | 10 #include <string> |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 break; | 196 break; |
197 if (r == MOJO_RESULT_SHOULD_WAIT) | 197 if (r == MOJO_RESULT_SHOULD_WAIT) |
198 continue; | 198 continue; |
199 ASSERT_EQ(MOJO_RESULT_OK, r); | 199 ASSERT_EQ(MOJO_RESULT_OK, r); |
200 contents += std::string(buffer, read_size); | 200 contents += std::string(buffer, read_size); |
201 } | 201 } |
202 std::string expected; | 202 std::string expected; |
203 base::ReadFileToString( | 203 base::ReadFileToString( |
204 root.Append(base::FilePath(FILE_PATH_LITERAL("hello.html"))), &expected); | 204 root.Append(base::FilePath(FILE_PATH_LITERAL("hello.html"))), &expected); |
205 EXPECT_EQ(expected, contents); | 205 EXPECT_EQ(expected, contents); |
| 206 EXPECT_EQ(static_cast<int64_t>(expected.size()), |
| 207 client.completion_status().encoded_data_length); |
| 208 EXPECT_EQ(static_cast<int64_t>(expected.size()), |
| 209 client.completion_status().encoded_body_length); |
206 } | 210 } |
207 | 211 |
208 TEST_P(URLLoaderFactoryImplTest, GetFailedResponse) { | 212 TEST_P(URLLoaderFactoryImplTest, GetFailedResponse) { |
209 NavigationResourceThrottle::set_ui_checks_always_succeed_for_testing(true); | 213 NavigationResourceThrottle::set_ui_checks_always_succeed_for_testing(true); |
210 mojom::URLLoaderAssociatedPtr loader; | 214 mojom::URLLoaderAssociatedPtr loader; |
211 ResourceRequest request; | 215 ResourceRequest request; |
212 TestURLLoaderClient client; | 216 TestURLLoaderClient client; |
213 net::URLRequestFailedJob::AddUrlHandler(); | 217 net::URLRequestFailedJob::AddUrlHandler(); |
214 request.url = net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase( | 218 request.url = net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase( |
215 net::URLRequestFailedJob::START, net::ERR_TIMED_OUT); | 219 net::URLRequestFailedJob::START, net::ERR_TIMED_OUT); |
216 request.method = "GET"; | 220 request.method = "GET"; |
217 factory_->CreateLoaderAndStart( | 221 factory_->CreateLoaderAndStart( |
218 mojo::GetProxy(&loader, factory_.associated_group()), 2, 1, request, | 222 mojo::GetProxy(&loader, factory_.associated_group()), 2, 1, request, |
219 client.CreateRemoteAssociatedPtrInfo(factory_.associated_group())); | 223 client.CreateRemoteAssociatedPtrInfo(factory_.associated_group())); |
220 | 224 |
221 client.RunUntilComplete(); | 225 client.RunUntilComplete(); |
222 ASSERT_FALSE(client.has_received_response()); | 226 ASSERT_FALSE(client.has_received_response()); |
223 ASSERT_FALSE(client.response_body().is_valid()); | 227 ASSERT_FALSE(client.response_body().is_valid()); |
224 | 228 |
225 EXPECT_EQ(net::ERR_TIMED_OUT, client.completion_status().error_code); | 229 EXPECT_EQ(net::ERR_TIMED_OUT, client.completion_status().error_code); |
| 230 EXPECT_EQ(0, client.completion_status().encoded_data_length); |
| 231 EXPECT_EQ(0, client.completion_status().encoded_body_length); |
| 232 } |
| 233 |
| 234 // In this case, the loading fails after receiving a response. |
| 235 TEST_P(URLLoaderFactoryImplTest, GetFailedResponse2) { |
| 236 NavigationResourceThrottle::set_ui_checks_always_succeed_for_testing(true); |
| 237 mojom::URLLoaderAssociatedPtr loader; |
| 238 ResourceRequest request; |
| 239 TestURLLoaderClient client; |
| 240 net::URLRequestFailedJob::AddUrlHandler(); |
| 241 request.url = net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase( |
| 242 net::URLRequestFailedJob::READ_ASYNC, net::ERR_TIMED_OUT); |
| 243 request.method = "GET"; |
| 244 request.is_main_frame = true; |
| 245 factory_->CreateLoaderAndStart( |
| 246 mojo::GetProxy(&loader, factory_.associated_group()), 2, 1, request, |
| 247 client.CreateRemoteAssociatedPtrInfo(factory_.associated_group())); |
| 248 |
| 249 client.RunUntilComplete(); |
| 250 ASSERT_FALSE(client.has_received_response()); |
| 251 ASSERT_TRUE(client.response_body().is_valid()); |
| 252 |
| 253 EXPECT_EQ(net::ERR_TIMED_OUT, client.completion_status().error_code); |
| 254 EXPECT_GT(client.completion_status().encoded_data_length, 0); |
| 255 EXPECT_EQ(0, client.completion_status().encoded_body_length); |
226 } | 256 } |
227 | 257 |
228 // This test tests a case where resource loading is cancelled before started. | 258 // This test tests a case where resource loading is cancelled before started. |
229 TEST_P(URLLoaderFactoryImplTest, InvalidURL) { | 259 TEST_P(URLLoaderFactoryImplTest, InvalidURL) { |
230 mojom::URLLoaderAssociatedPtr loader; | 260 mojom::URLLoaderAssociatedPtr loader; |
231 ResourceRequest request; | 261 ResourceRequest request; |
232 TestURLLoaderClient client; | 262 TestURLLoaderClient client; |
233 request.url = GURL(); | 263 request.url = GURL(); |
234 request.method = "GET"; | 264 request.method = "GET"; |
235 ASSERT_FALSE(request.url.is_valid()); | 265 ASSERT_FALSE(request.url.is_valid()); |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
321 | 351 |
322 EXPECT_EQ(static_cast<int64_t>(contents.size()), | 352 EXPECT_EQ(static_cast<int64_t>(contents.size()), |
323 client.download_data_length()); | 353 client.download_data_length()); |
324 EXPECT_EQ(static_cast<int64_t>(contents.size()), | 354 EXPECT_EQ(static_cast<int64_t>(contents.size()), |
325 client.encoded_download_data_length()); | 355 client.encoded_download_data_length()); |
326 | 356 |
327 std::string expected; | 357 std::string expected; |
328 base::ReadFileToString( | 358 base::ReadFileToString( |
329 root.Append(base::FilePath(FILE_PATH_LITERAL("hello.html"))), &expected); | 359 root.Append(base::FilePath(FILE_PATH_LITERAL("hello.html"))), &expected); |
330 EXPECT_EQ(expected, contents); | 360 EXPECT_EQ(expected, contents); |
| 361 EXPECT_EQ(static_cast<int64_t>(expected.size()), |
| 362 client.completion_status().encoded_data_length); |
| 363 EXPECT_EQ(static_cast<int64_t>(expected.size()), |
| 364 client.completion_status().encoded_body_length); |
331 } | 365 } |
332 | 366 |
333 TEST_P(URLLoaderFactoryImplTest, DownloadToFileFailure) { | 367 TEST_P(URLLoaderFactoryImplTest, DownloadToFileFailure) { |
334 constexpr int32_t kRoutingId = 1; | 368 constexpr int32_t kRoutingId = 1; |
335 constexpr int32_t kRequestId = 2; | 369 constexpr int32_t kRequestId = 2; |
336 | 370 |
337 mojom::URLLoaderAssociatedPtr loader; | 371 mojom::URLLoaderAssociatedPtr loader; |
338 base::FilePath root; | 372 base::FilePath root; |
339 PathService::Get(DIR_TEST_DATA, &root); | 373 PathService::Get(DIR_TEST_DATA, &root); |
340 net::URLRequestSlowDownloadJob::AddUrlHandler(); | 374 net::URLRequestSlowDownloadJob::AddUrlHandler(); |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
420 ASSERT_FALSE(rdh_.GetURLRequest(GlobalRequestID(kChildId, kRequestId))); | 454 ASSERT_FALSE(rdh_.GetURLRequest(GlobalRequestID(kChildId, kRequestId))); |
421 } | 455 } |
422 | 456 |
423 INSTANTIATE_TEST_CASE_P(URLLoaderFactoryImplTest, | 457 INSTANTIATE_TEST_CASE_P(URLLoaderFactoryImplTest, |
424 URLLoaderFactoryImplTest, | 458 URLLoaderFactoryImplTest, |
425 ::testing::Values(128, 32 * 1024)); | 459 ::testing::Values(128, 32 * 1024)); |
426 | 460 |
427 } // namespace | 461 } // namespace |
428 | 462 |
429 } // namespace content | 463 } // namespace content |
OLD | NEW |