Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(73)

Side by Side Diff: content/browser/loader/url_loader_factory_impl_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698