| 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); | |
| 210 } | 206 } |
| 211 | 207 |
| 212 TEST_P(URLLoaderFactoryImplTest, GetFailedResponse) { | 208 TEST_P(URLLoaderFactoryImplTest, GetFailedResponse) { |
| 213 NavigationResourceThrottle::set_ui_checks_always_succeed_for_testing(true); | 209 NavigationResourceThrottle::set_ui_checks_always_succeed_for_testing(true); |
| 214 mojom::URLLoaderAssociatedPtr loader; | 210 mojom::URLLoaderAssociatedPtr loader; |
| 215 ResourceRequest request; | 211 ResourceRequest request; |
| 216 TestURLLoaderClient client; | 212 TestURLLoaderClient client; |
| 217 net::URLRequestFailedJob::AddUrlHandler(); | 213 net::URLRequestFailedJob::AddUrlHandler(); |
| 218 request.url = net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase( | 214 request.url = net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase( |
| 219 net::URLRequestFailedJob::START, net::ERR_TIMED_OUT); | 215 net::URLRequestFailedJob::START, net::ERR_TIMED_OUT); |
| 220 request.method = "GET"; | 216 request.method = "GET"; |
| 221 factory_->CreateLoaderAndStart( | 217 factory_->CreateLoaderAndStart( |
| 222 mojo::GetProxy(&loader, factory_.associated_group()), 2, 1, request, | 218 mojo::GetProxy(&loader, factory_.associated_group()), 2, 1, request, |
| 223 client.CreateRemoteAssociatedPtrInfo(factory_.associated_group())); | 219 client.CreateRemoteAssociatedPtrInfo(factory_.associated_group())); |
| 224 | 220 |
| 225 client.RunUntilComplete(); | 221 client.RunUntilComplete(); |
| 226 ASSERT_FALSE(client.has_received_response()); | 222 ASSERT_FALSE(client.has_received_response()); |
| 227 ASSERT_FALSE(client.response_body().is_valid()); | 223 ASSERT_FALSE(client.response_body().is_valid()); |
| 228 | 224 |
| 229 EXPECT_EQ(net::ERR_TIMED_OUT, client.completion_status().error_code); | 225 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); | |
| 256 } | 226 } |
| 257 | 227 |
| 258 // This test tests a case where resource loading is cancelled before started. | 228 // This test tests a case where resource loading is cancelled before started. |
| 259 TEST_P(URLLoaderFactoryImplTest, InvalidURL) { | 229 TEST_P(URLLoaderFactoryImplTest, InvalidURL) { |
| 260 mojom::URLLoaderAssociatedPtr loader; | 230 mojom::URLLoaderAssociatedPtr loader; |
| 261 ResourceRequest request; | 231 ResourceRequest request; |
| 262 TestURLLoaderClient client; | 232 TestURLLoaderClient client; |
| 263 request.url = GURL(); | 233 request.url = GURL(); |
| 264 request.method = "GET"; | 234 request.method = "GET"; |
| 265 ASSERT_FALSE(request.url.is_valid()); | 235 ASSERT_FALSE(request.url.is_valid()); |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 351 | 321 |
| 352 EXPECT_EQ(static_cast<int64_t>(contents.size()), | 322 EXPECT_EQ(static_cast<int64_t>(contents.size()), |
| 353 client.download_data_length()); | 323 client.download_data_length()); |
| 354 EXPECT_EQ(static_cast<int64_t>(contents.size()), | 324 EXPECT_EQ(static_cast<int64_t>(contents.size()), |
| 355 client.encoded_download_data_length()); | 325 client.encoded_download_data_length()); |
| 356 | 326 |
| 357 std::string expected; | 327 std::string expected; |
| 358 base::ReadFileToString( | 328 base::ReadFileToString( |
| 359 root.Append(base::FilePath(FILE_PATH_LITERAL("hello.html"))), &expected); | 329 root.Append(base::FilePath(FILE_PATH_LITERAL("hello.html"))), &expected); |
| 360 EXPECT_EQ(expected, contents); | 330 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); | |
| 365 } | 331 } |
| 366 | 332 |
| 367 TEST_P(URLLoaderFactoryImplTest, DownloadToFileFailure) { | 333 TEST_P(URLLoaderFactoryImplTest, DownloadToFileFailure) { |
| 368 constexpr int32_t kRoutingId = 1; | 334 constexpr int32_t kRoutingId = 1; |
| 369 constexpr int32_t kRequestId = 2; | 335 constexpr int32_t kRequestId = 2; |
| 370 | 336 |
| 371 mojom::URLLoaderAssociatedPtr loader; | 337 mojom::URLLoaderAssociatedPtr loader; |
| 372 base::FilePath root; | 338 base::FilePath root; |
| 373 PathService::Get(DIR_TEST_DATA, &root); | 339 PathService::Get(DIR_TEST_DATA, &root); |
| 374 net::URLRequestSlowDownloadJob::AddUrlHandler(); | 340 net::URLRequestSlowDownloadJob::AddUrlHandler(); |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 454 ASSERT_FALSE(rdh_.GetURLRequest(GlobalRequestID(kChildId, kRequestId))); | 420 ASSERT_FALSE(rdh_.GetURLRequest(GlobalRequestID(kChildId, kRequestId))); |
| 455 } | 421 } |
| 456 | 422 |
| 457 INSTANTIATE_TEST_CASE_P(URLLoaderFactoryImplTest, | 423 INSTANTIATE_TEST_CASE_P(URLLoaderFactoryImplTest, |
| 458 URLLoaderFactoryImplTest, | 424 URLLoaderFactoryImplTest, |
| 459 ::testing::Values(128, 32 * 1024)); | 425 ::testing::Values(128, 32 * 1024)); |
| 460 | 426 |
| 461 } // namespace | 427 } // namespace |
| 462 | 428 |
| 463 } // namespace content | 429 } // namespace content |
| OLD | NEW |