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 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 // Assume the file contents is small enough to be stored in the data pipe. | 158 // Assume the file contents is small enough to be stored in the data pipe. |
159 request.url = net::URLRequestMockHTTPJob::GetMockUrl("hello.html"); | 159 request.url = net::URLRequestMockHTTPJob::GetMockUrl("hello.html"); |
160 request.method = "GET"; | 160 request.method = "GET"; |
161 // |resource_type| can't be a frame type. It is because when PlzNavigate is | 161 // |resource_type| can't be a frame type. It is because when PlzNavigate is |
162 // enabled, the url scheme of frame type requests from the renderer process | 162 // enabled, the url scheme of frame type requests from the renderer process |
163 // must be blob scheme. | 163 // must be blob scheme. |
164 request.resource_type = RESOURCE_TYPE_XHR; | 164 request.resource_type = RESOURCE_TYPE_XHR; |
165 // Need to set |request_initiator| for non main frame type request. | 165 // Need to set |request_initiator| for non main frame type request. |
166 request.request_initiator = url::Origin(); | 166 request.request_initiator = url::Origin(); |
167 factory_->CreateLoaderAndStart(mojo::MakeRequest(&loader), kRoutingId, | 167 factory_->CreateLoaderAndStart(mojo::MakeRequest(&loader), kRoutingId, |
168 kRequestId, request, | 168 kRequestId, 0, request, |
169 client.CreateInterfacePtr()); | 169 client.CreateInterfacePtr()); |
170 | 170 |
171 ASSERT_FALSE(client.has_received_response()); | 171 ASSERT_FALSE(client.has_received_response()); |
172 ASSERT_FALSE(client.response_body().is_valid()); | 172 ASSERT_FALSE(client.response_body().is_valid()); |
173 ASSERT_FALSE(client.has_received_completion()); | 173 ASSERT_FALSE(client.has_received_completion()); |
174 | 174 |
175 client.RunUntilResponseReceived(); | 175 client.RunUntilResponseReceived(); |
176 | 176 |
177 net::URLRequest* url_request = | 177 net::URLRequest* url_request = |
178 rdh_.GetURLRequest(GlobalRequestID(kChildId, kRequestId)); | 178 rdh_.GetURLRequest(GlobalRequestID(kChildId, kRequestId)); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 net::URLRequestFailedJob::AddUrlHandler(); | 233 net::URLRequestFailedJob::AddUrlHandler(); |
234 request.url = net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase( | 234 request.url = net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase( |
235 net::URLRequestFailedJob::START, net::ERR_TIMED_OUT); | 235 net::URLRequestFailedJob::START, net::ERR_TIMED_OUT); |
236 request.method = "GET"; | 236 request.method = "GET"; |
237 // |resource_type| can't be a frame type. It is because when PlzNavigate is | 237 // |resource_type| can't be a frame type. It is because when PlzNavigate is |
238 // enabled, the url scheme of frame type requests from the renderer process | 238 // enabled, the url scheme of frame type requests from the renderer process |
239 // must be blob scheme. | 239 // must be blob scheme. |
240 request.resource_type = RESOURCE_TYPE_XHR; | 240 request.resource_type = RESOURCE_TYPE_XHR; |
241 // Need to set |request_initiator| for non main frame type request. | 241 // Need to set |request_initiator| for non main frame type request. |
242 request.request_initiator = url::Origin(); | 242 request.request_initiator = url::Origin(); |
243 factory_->CreateLoaderAndStart(mojo::MakeRequest(&loader), 2, 1, request, | 243 factory_->CreateLoaderAndStart(mojo::MakeRequest(&loader), 2, 1, 0, request, |
244 client.CreateInterfacePtr()); | 244 client.CreateInterfacePtr()); |
245 | 245 |
246 client.RunUntilComplete(); | 246 client.RunUntilComplete(); |
247 ASSERT_FALSE(client.has_received_response()); | 247 ASSERT_FALSE(client.has_received_response()); |
248 ASSERT_FALSE(client.response_body().is_valid()); | 248 ASSERT_FALSE(client.response_body().is_valid()); |
249 | 249 |
250 EXPECT_EQ(net::ERR_TIMED_OUT, client.completion_status().error_code); | 250 EXPECT_EQ(net::ERR_TIMED_OUT, client.completion_status().error_code); |
251 EXPECT_EQ(0, client.completion_status().encoded_data_length); | 251 EXPECT_EQ(0, client.completion_status().encoded_data_length); |
252 EXPECT_EQ(0, client.completion_status().encoded_body_length); | 252 EXPECT_EQ(0, client.completion_status().encoded_body_length); |
253 } | 253 } |
254 | 254 |
255 // In this case, the loading fails after receiving a response. | 255 // In this case, the loading fails after receiving a response. |
256 TEST_P(URLLoaderFactoryImplTest, GetFailedResponse2) { | 256 TEST_P(URLLoaderFactoryImplTest, GetFailedResponse2) { |
257 NavigationResourceThrottle::set_ui_checks_always_succeed_for_testing(true); | 257 NavigationResourceThrottle::set_ui_checks_always_succeed_for_testing(true); |
258 mojom::URLLoaderAssociatedPtr loader; | 258 mojom::URLLoaderAssociatedPtr loader; |
259 ResourceRequest request; | 259 ResourceRequest request; |
260 TestURLLoaderClient client; | 260 TestURLLoaderClient client; |
261 net::URLRequestFailedJob::AddUrlHandler(); | 261 net::URLRequestFailedJob::AddUrlHandler(); |
262 request.url = net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase( | 262 request.url = net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase( |
263 net::URLRequestFailedJob::READ_ASYNC, net::ERR_TIMED_OUT); | 263 net::URLRequestFailedJob::READ_ASYNC, net::ERR_TIMED_OUT); |
264 request.method = "GET"; | 264 request.method = "GET"; |
265 // |resource_type| can't be a frame type. It is because when PlzNavigate is | 265 // |resource_type| can't be a frame type. It is because when PlzNavigate is |
266 // enabled, the url scheme of frame type requests from the renderer process | 266 // enabled, the url scheme of frame type requests from the renderer process |
267 // must be blob scheme. | 267 // must be blob scheme. |
268 request.resource_type = RESOURCE_TYPE_XHR; | 268 request.resource_type = RESOURCE_TYPE_XHR; |
269 // Need to set |request_initiator| for non main frame type request. | 269 // Need to set |request_initiator| for non main frame type request. |
270 request.request_initiator = url::Origin(); | 270 request.request_initiator = url::Origin(); |
271 factory_->CreateLoaderAndStart(mojo::MakeRequest(&loader), 2, 1, request, | 271 factory_->CreateLoaderAndStart(mojo::MakeRequest(&loader), 2, 1, 0, request, |
272 client.CreateInterfacePtr()); | 272 client.CreateInterfacePtr()); |
273 | 273 |
274 client.RunUntilComplete(); | 274 client.RunUntilComplete(); |
275 ASSERT_FALSE(client.has_received_response()); | 275 ASSERT_FALSE(client.has_received_response()); |
276 ASSERT_FALSE(client.response_body().is_valid()); | 276 ASSERT_FALSE(client.response_body().is_valid()); |
277 | 277 |
278 EXPECT_EQ(net::ERR_TIMED_OUT, client.completion_status().error_code); | 278 EXPECT_EQ(net::ERR_TIMED_OUT, client.completion_status().error_code); |
279 EXPECT_GT(client.completion_status().encoded_data_length, 0); | 279 EXPECT_GT(client.completion_status().encoded_data_length, 0); |
280 EXPECT_EQ(0, client.completion_status().encoded_body_length); | 280 EXPECT_EQ(0, client.completion_status().encoded_body_length); |
281 } | 281 } |
282 | 282 |
283 // This test tests a case where resource loading is cancelled before started. | 283 // This test tests a case where resource loading is cancelled before started. |
284 TEST_P(URLLoaderFactoryImplTest, InvalidURL) { | 284 TEST_P(URLLoaderFactoryImplTest, InvalidURL) { |
285 mojom::URLLoaderAssociatedPtr loader; | 285 mojom::URLLoaderAssociatedPtr loader; |
286 ResourceRequest request; | 286 ResourceRequest request; |
287 TestURLLoaderClient client; | 287 TestURLLoaderClient client; |
288 request.url = GURL(); | 288 request.url = GURL(); |
289 request.method = "GET"; | 289 request.method = "GET"; |
290 // |resource_type| can't be a frame type. It is because when PlzNavigate is | 290 // |resource_type| can't be a frame type. It is because when PlzNavigate is |
291 // enabled, the url scheme of frame type requests from the renderer process | 291 // enabled, the url scheme of frame type requests from the renderer process |
292 // must be blob scheme. | 292 // must be blob scheme. |
293 request.resource_type = RESOURCE_TYPE_XHR; | 293 request.resource_type = RESOURCE_TYPE_XHR; |
294 // Need to set |request_initiator| for non main frame type request. | 294 // Need to set |request_initiator| for non main frame type request. |
295 request.request_initiator = url::Origin(); | 295 request.request_initiator = url::Origin(); |
296 ASSERT_FALSE(request.url.is_valid()); | 296 ASSERT_FALSE(request.url.is_valid()); |
297 factory_->CreateLoaderAndStart(mojo::MakeRequest(&loader), 2, 1, request, | 297 factory_->CreateLoaderAndStart(mojo::MakeRequest(&loader), 2, 1, 0, request, |
298 client.CreateInterfacePtr()); | 298 client.CreateInterfacePtr()); |
299 | 299 |
300 client.RunUntilComplete(); | 300 client.RunUntilComplete(); |
301 ASSERT_FALSE(client.has_received_response()); | 301 ASSERT_FALSE(client.has_received_response()); |
302 ASSERT_FALSE(client.response_body().is_valid()); | 302 ASSERT_FALSE(client.response_body().is_valid()); |
303 | 303 |
304 EXPECT_EQ(net::ERR_ABORTED, client.completion_status().error_code); | 304 EXPECT_EQ(net::ERR_ABORTED, client.completion_status().error_code); |
305 } | 305 } |
306 | 306 |
307 // This test tests a case where resource loading is cancelled before started. | 307 // This test tests a case where resource loading is cancelled before started. |
308 TEST_P(URLLoaderFactoryImplTest, ShouldNotRequestURL) { | 308 TEST_P(URLLoaderFactoryImplTest, ShouldNotRequestURL) { |
309 mojom::URLLoaderAssociatedPtr loader; | 309 mojom::URLLoaderAssociatedPtr loader; |
310 RejectingResourceDispatcherHostDelegate rdh_delegate; | 310 RejectingResourceDispatcherHostDelegate rdh_delegate; |
311 rdh_.SetDelegate(&rdh_delegate); | 311 rdh_.SetDelegate(&rdh_delegate); |
312 ResourceRequest request; | 312 ResourceRequest request; |
313 TestURLLoaderClient client; | 313 TestURLLoaderClient client; |
314 request.url = GURL("http://localhost/"); | 314 request.url = GURL("http://localhost/"); |
315 request.method = "GET"; | 315 request.method = "GET"; |
316 // |resource_type| can't be a frame type. It is because when PlzNavigate is | 316 // |resource_type| can't be a frame type. It is because when PlzNavigate is |
317 // enabled, the url scheme of frame type requests from the renderer process | 317 // enabled, the url scheme of frame type requests from the renderer process |
318 // must be blob scheme. | 318 // must be blob scheme. |
319 request.resource_type = RESOURCE_TYPE_XHR; | 319 request.resource_type = RESOURCE_TYPE_XHR; |
320 // Need to set |request_initiator| for non main frame type request. | 320 // Need to set |request_initiator| for non main frame type request. |
321 request.request_initiator = url::Origin(); | 321 request.request_initiator = url::Origin(); |
322 factory_->CreateLoaderAndStart(mojo::MakeRequest(&loader), 2, 1, request, | 322 factory_->CreateLoaderAndStart(mojo::MakeRequest(&loader), 2, 1, 0, request, |
323 client.CreateInterfacePtr()); | 323 client.CreateInterfacePtr()); |
324 | 324 |
325 client.RunUntilComplete(); | 325 client.RunUntilComplete(); |
326 rdh_.SetDelegate(nullptr); | 326 rdh_.SetDelegate(nullptr); |
327 | 327 |
328 ASSERT_FALSE(client.has_received_response()); | 328 ASSERT_FALSE(client.has_received_response()); |
329 ASSERT_FALSE(client.response_body().is_valid()); | 329 ASSERT_FALSE(client.response_body().is_valid()); |
330 | 330 |
331 EXPECT_EQ(net::ERR_ABORTED, client.completion_status().error_code); | 331 EXPECT_EQ(net::ERR_ABORTED, client.completion_status().error_code); |
332 } | 332 } |
333 | 333 |
334 TEST_P(URLLoaderFactoryImplTest, DownloadToFile) { | 334 TEST_P(URLLoaderFactoryImplTest, DownloadToFile) { |
335 constexpr int32_t kRoutingId = 1; | 335 constexpr int32_t kRoutingId = 1; |
336 constexpr int32_t kRequestId = 2; | 336 constexpr int32_t kRequestId = 2; |
337 | 337 |
338 mojom::URLLoaderAssociatedPtr loader; | 338 mojom::URLLoaderAssociatedPtr loader; |
339 base::FilePath root; | 339 base::FilePath root; |
340 PathService::Get(DIR_TEST_DATA, &root); | 340 PathService::Get(DIR_TEST_DATA, &root); |
341 net::URLRequestMockHTTPJob::AddUrlHandlers(root, | 341 net::URLRequestMockHTTPJob::AddUrlHandlers(root, |
342 BrowserThread::GetBlockingPool()); | 342 BrowserThread::GetBlockingPool()); |
343 | 343 |
344 ResourceRequest request; | 344 ResourceRequest request; |
345 TestURLLoaderClient client; | 345 TestURLLoaderClient client; |
346 request.url = net::URLRequestMockHTTPJob::GetMockUrl("hello.html"); | 346 request.url = net::URLRequestMockHTTPJob::GetMockUrl("hello.html"); |
347 request.method = "GET"; | 347 request.method = "GET"; |
348 request.resource_type = RESOURCE_TYPE_XHR; | 348 request.resource_type = RESOURCE_TYPE_XHR; |
349 request.download_to_file = true; | 349 request.download_to_file = true; |
350 request.request_initiator = url::Origin(); | 350 request.request_initiator = url::Origin(); |
351 factory_->CreateLoaderAndStart(mojo::MakeRequest(&loader), kRoutingId, | 351 factory_->CreateLoaderAndStart(mojo::MakeRequest(&loader), kRoutingId, |
352 kRequestId, request, | 352 kRequestId, 0, request, |
353 client.CreateInterfacePtr()); | 353 client.CreateInterfacePtr()); |
354 ASSERT_FALSE(client.has_received_response()); | 354 ASSERT_FALSE(client.has_received_response()); |
355 ASSERT_FALSE(client.has_data_downloaded()); | 355 ASSERT_FALSE(client.has_data_downloaded()); |
356 ASSERT_FALSE(client.has_received_completion()); | 356 ASSERT_FALSE(client.has_received_completion()); |
357 | 357 |
358 client.RunUntilResponseReceived(); | 358 client.RunUntilResponseReceived(); |
359 | 359 |
360 net::URLRequest* url_request = | 360 net::URLRequest* url_request = |
361 rdh_.GetURLRequest(GlobalRequestID(kChildId, kRequestId)); | 361 rdh_.GetURLRequest(GlobalRequestID(kChildId, kRequestId)); |
362 ASSERT_TRUE(url_request); | 362 ASSERT_TRUE(url_request); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
409 net::URLRequestSlowDownloadJob::AddUrlHandler(); | 409 net::URLRequestSlowDownloadJob::AddUrlHandler(); |
410 | 410 |
411 ResourceRequest request; | 411 ResourceRequest request; |
412 TestURLLoaderClient client; | 412 TestURLLoaderClient client; |
413 request.url = GURL(net::URLRequestSlowDownloadJob::kKnownSizeUrl); | 413 request.url = GURL(net::URLRequestSlowDownloadJob::kKnownSizeUrl); |
414 request.method = "GET"; | 414 request.method = "GET"; |
415 request.resource_type = RESOURCE_TYPE_XHR; | 415 request.resource_type = RESOURCE_TYPE_XHR; |
416 request.download_to_file = true; | 416 request.download_to_file = true; |
417 request.request_initiator = url::Origin(); | 417 request.request_initiator = url::Origin(); |
418 factory_->CreateLoaderAndStart(mojo::MakeRequest(&loader), kRoutingId, | 418 factory_->CreateLoaderAndStart(mojo::MakeRequest(&loader), kRoutingId, |
419 kRequestId, request, | 419 kRequestId, 0, request, |
420 client.CreateInterfacePtr()); | 420 client.CreateInterfacePtr()); |
421 ASSERT_FALSE(client.has_received_response()); | 421 ASSERT_FALSE(client.has_received_response()); |
422 ASSERT_FALSE(client.has_data_downloaded()); | 422 ASSERT_FALSE(client.has_data_downloaded()); |
423 ASSERT_FALSE(client.has_received_completion()); | 423 ASSERT_FALSE(client.has_received_completion()); |
424 | 424 |
425 client.RunUntilResponseReceived(); | 425 client.RunUntilResponseReceived(); |
426 | 426 |
427 net::URLRequest* url_request = | 427 net::URLRequest* url_request = |
428 rdh_.GetURLRequest(GlobalRequestID(kChildId, kRequestId)); | 428 rdh_.GetURLRequest(GlobalRequestID(kChildId, kRequestId)); |
429 ASSERT_TRUE(url_request); | 429 ASSERT_TRUE(url_request); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
472 request.url = net::URLRequestMockHTTPJob::GetMockUrl("gzip-content.svgz"); | 472 request.url = net::URLRequestMockHTTPJob::GetMockUrl("gzip-content.svgz"); |
473 request.method = "GET"; | 473 request.method = "GET"; |
474 // |resource_type| can't be a frame type. It is because when PlzNavigate is | 474 // |resource_type| can't be a frame type. It is because when PlzNavigate is |
475 // enabled, the url scheme of frame type requests from the renderer process | 475 // enabled, the url scheme of frame type requests from the renderer process |
476 // must be blob scheme. | 476 // must be blob scheme. |
477 request.resource_type = RESOURCE_TYPE_XHR; | 477 request.resource_type = RESOURCE_TYPE_XHR; |
478 // Need to set |request_initiator| for non main frame type request. | 478 // Need to set |request_initiator| for non main frame type request. |
479 request.request_initiator = url::Origin(); | 479 request.request_initiator = url::Origin(); |
480 request.report_raw_headers = true; | 480 request.report_raw_headers = true; |
481 factory_->CreateLoaderAndStart(mojo::MakeRequest(&loader), kRoutingId, | 481 factory_->CreateLoaderAndStart(mojo::MakeRequest(&loader), kRoutingId, |
482 kRequestId, request, | 482 kRequestId, 0, request, |
483 client.CreateInterfacePtr()); | 483 client.CreateInterfacePtr()); |
484 | 484 |
485 client.RunUntilComplete(); | 485 client.RunUntilComplete(); |
486 | 486 |
487 std::string contents; | 487 std::string contents; |
488 while (true) { | 488 while (true) { |
489 char buffer[16]; | 489 char buffer[16]; |
490 uint32_t read_size = sizeof(buffer); | 490 uint32_t read_size = sizeof(buffer); |
491 MojoResult r = mojo::ReadDataRaw(client.response_body(), buffer, &read_size, | 491 MojoResult r = mojo::ReadDataRaw(client.response_body(), buffer, &read_size, |
492 MOJO_READ_DATA_FLAG_NONE); | 492 MOJO_READ_DATA_FLAG_NONE); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
532 request.url = net::URLRequestFailedJob::GetMockHttpUrl(net::ERR_IO_PENDING); | 532 request.url = net::URLRequestFailedJob::GetMockHttpUrl(net::ERR_IO_PENDING); |
533 request.method = "GET"; | 533 request.method = "GET"; |
534 request.is_main_frame = true; | 534 request.is_main_frame = true; |
535 // |resource_type| can't be a frame type. It is because when PlzNavigate is | 535 // |resource_type| can't be a frame type. It is because when PlzNavigate is |
536 // enabled, the url scheme of frame type requests from the renderer process | 536 // enabled, the url scheme of frame type requests from the renderer process |
537 // must be blob scheme. | 537 // must be blob scheme. |
538 request.resource_type = RESOURCE_TYPE_XHR; | 538 request.resource_type = RESOURCE_TYPE_XHR; |
539 // Need to set |request_initiator| for non main frame type request. | 539 // Need to set |request_initiator| for non main frame type request. |
540 request.request_initiator = url::Origin(); | 540 request.request_initiator = url::Origin(); |
541 factory_->CreateLoaderAndStart(mojo::MakeRequest(&loader), kRoutingId, | 541 factory_->CreateLoaderAndStart(mojo::MakeRequest(&loader), kRoutingId, |
542 kRequestId, request, | 542 kRequestId, 0, request, |
543 client.CreateInterfacePtr()); | 543 client.CreateInterfacePtr()); |
544 | 544 |
545 base::RunLoop().RunUntilIdle(); | 545 base::RunLoop().RunUntilIdle(); |
546 ASSERT_TRUE(rdh_.GetURLRequest(GlobalRequestID(kChildId, kRequestId))); | 546 ASSERT_TRUE(rdh_.GetURLRequest(GlobalRequestID(kChildId, kRequestId))); |
547 ASSERT_FALSE(client.has_received_response()); | 547 ASSERT_FALSE(client.has_received_response()); |
548 ASSERT_FALSE(client.response_body().is_valid()); | 548 ASSERT_FALSE(client.response_body().is_valid()); |
549 ASSERT_FALSE(client.has_received_completion()); | 549 ASSERT_FALSE(client.has_received_completion()); |
550 | 550 |
551 loader = nullptr; | 551 loader = nullptr; |
552 base::RunLoop().RunUntilIdle(); | 552 base::RunLoop().RunUntilIdle(); |
553 ASSERT_FALSE(rdh_.GetURLRequest(GlobalRequestID(kChildId, kRequestId))); | 553 ASSERT_FALSE(rdh_.GetURLRequest(GlobalRequestID(kChildId, kRequestId))); |
554 } | 554 } |
555 | 555 |
556 INSTANTIATE_TEST_CASE_P(URLLoaderFactoryImplTest, | 556 INSTANTIATE_TEST_CASE_P(URLLoaderFactoryImplTest, |
557 URLLoaderFactoryImplTest, | 557 URLLoaderFactoryImplTest, |
558 ::testing::Values(128, 32 * 1024)); | 558 ::testing::Values(128, 32 * 1024)); |
559 | 559 |
560 } // namespace | 560 } // namespace |
561 | 561 |
562 } // namespace content | 562 } // namespace content |
OLD | NEW |