OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/resource_loader.h" | 5 #include "content/browser/loader/resource_loader.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
11 #include <utility> | 11 #include <utility> |
12 | 12 |
13 #include "base/files/file.h" | 13 #include "base/files/file.h" |
14 #include "base/files/file_util.h" | 14 #include "base/files/file_util.h" |
15 #include "base/location.h" | 15 #include "base/location.h" |
16 #include "base/macros.h" | 16 #include "base/macros.h" |
17 #include "base/memory/ptr_util.h" | 17 #include "base/memory/ptr_util.h" |
18 #include "base/memory/weak_ptr.h" | 18 #include "base/memory/weak_ptr.h" |
19 #include "base/run_loop.h" | 19 #include "base/run_loop.h" |
20 #include "base/single_thread_task_runner.h" | 20 #include "base/single_thread_task_runner.h" |
21 #include "base/threading/thread_task_runner_handle.h" | 21 #include "base/threading/thread_task_runner_handle.h" |
22 #include "content/browser/browser_thread_impl.h" | 22 #include "content/browser/browser_thread_impl.h" |
23 #include "content/browser/loader/redirect_to_file_resource_handler.h" | 23 #include "content/browser/loader/redirect_to_file_resource_handler.h" |
24 #include "content/browser/loader/resource_loader_delegate.h" | 24 #include "content/browser/loader/resource_loader_delegate.h" |
| 25 #include "content/browser/loader/test_resource_handler.h" |
25 #include "content/public/browser/client_certificate_delegate.h" | 26 #include "content/public/browser/client_certificate_delegate.h" |
26 #include "content/public/browser/resource_request_info.h" | 27 #include "content/public/browser/resource_request_info.h" |
27 #include "content/public/common/content_paths.h" | 28 #include "content/public/common/content_paths.h" |
28 #include "content/public/common/resource_response.h" | 29 #include "content/public/common/resource_response.h" |
29 #include "content/public/common/resource_type.h" | 30 #include "content/public/common/resource_type.h" |
30 #include "content/public/test/mock_resource_context.h" | 31 #include "content/public/test/mock_resource_context.h" |
31 #include "content/public/test/test_browser_context.h" | 32 #include "content/public/test/test_browser_context.h" |
32 #include "content/public/test/test_browser_thread_bundle.h" | 33 #include "content/public/test/test_browser_thread_bundle.h" |
33 #include "content/public/test/test_renderer_host.h" | 34 #include "content/public/test/test_renderer_host.h" |
34 #include "content/test/test_content_browser_client.h" | 35 #include "content/test/test_content_browser_client.h" |
35 #include "content/test/test_web_contents.h" | 36 #include "content/test/test_web_contents.h" |
36 #include "ipc/ipc_message.h" | 37 #include "ipc/ipc_message.h" |
37 #include "net/base/chunked_upload_data_stream.h" | 38 #include "net/base/chunked_upload_data_stream.h" |
38 #include "net/base/io_buffer.h" | 39 #include "net/base/io_buffer.h" |
39 #include "net/base/mock_file_stream.h" | 40 #include "net/base/mock_file_stream.h" |
40 #include "net/base/net_errors.h" | 41 #include "net/base/net_errors.h" |
41 #include "net/base/request_priority.h" | 42 #include "net/base/request_priority.h" |
42 #include "net/base/upload_bytes_element_reader.h" | 43 #include "net/base/upload_bytes_element_reader.h" |
43 #include "net/cert/x509_certificate.h" | 44 #include "net/cert/x509_certificate.h" |
44 #include "net/nqe/effective_connection_type.h" | 45 #include "net/nqe/effective_connection_type.h" |
45 #include "net/nqe/network_quality_estimator.h" | 46 #include "net/nqe/network_quality_estimator.h" |
46 #include "net/ssl/client_cert_store.h" | 47 #include "net/ssl/client_cert_store.h" |
47 #include "net/ssl/ssl_cert_request_info.h" | 48 #include "net/ssl/ssl_cert_request_info.h" |
48 #include "net/ssl/ssl_private_key.h" | 49 #include "net/ssl/ssl_private_key.h" |
49 #include "net/test/cert_test_util.h" | 50 #include "net/test/cert_test_util.h" |
50 #include "net/test/embedded_test_server/embedded_test_server.h" | 51 #include "net/test/embedded_test_server/embedded_test_server.h" |
51 #include "net/test/test_data_directory.h" | 52 #include "net/test/test_data_directory.h" |
| 53 #include "net/test/url_request/url_request_failed_job.h" |
52 #include "net/url_request/url_request.h" | 54 #include "net/url_request/url_request.h" |
53 #include "net/url_request/url_request_filter.h" | 55 #include "net/url_request/url_request_filter.h" |
54 #include "net/url_request/url_request_interceptor.h" | 56 #include "net/url_request/url_request_interceptor.h" |
55 #include "net/url_request/url_request_job_factory.h" | 57 #include "net/url_request/url_request_job_factory.h" |
56 #include "net/url_request/url_request_job_factory_impl.h" | 58 #include "net/url_request/url_request_job_factory_impl.h" |
57 #include "net/url_request/url_request_test_job.h" | 59 #include "net/url_request/url_request_test_job.h" |
58 #include "net/url_request/url_request_test_util.h" | 60 #include "net/url_request/url_request_test_util.h" |
59 #include "storage/browser/blob/shareable_file_reference.h" | 61 #include "storage/browser/blob/shareable_file_reference.h" |
60 #include "testing/gtest/include/gtest/gtest.h" | 62 #include "testing/gtest/include/gtest/gtest.h" |
61 | 63 |
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
258 : net::URLRequestTestJob::test_headers(); | 260 : net::URLRequestTestJob::test_headers(); |
259 return new MockHTTPSURLRequestJob(request, network_delegate, headers, | 261 return new MockHTTPSURLRequestJob(request, network_delegate, headers, |
260 "dummy response", true); | 262 "dummy response", true); |
261 } | 263 } |
262 | 264 |
263 private: | 265 private: |
264 bool redirect_; | 266 bool redirect_; |
265 }; | 267 }; |
266 | 268 |
267 // Arbitrary read buffer size. | 269 // Arbitrary read buffer size. |
268 const int kReadBufSize = 1024; | |
269 | |
270 // Dummy implementation of ResourceHandler, instance of which is needed to | |
271 // initialize ResourceLoader. | |
272 class ResourceHandlerStub : public ResourceHandler { | |
273 public: | |
274 explicit ResourceHandlerStub(net::URLRequest* request) | |
275 : ResourceHandler(request), | |
276 read_buffer_(new net::IOBuffer(kReadBufSize)), | |
277 defer_request_on_will_start_(false), | |
278 expect_reads_(true), | |
279 cancel_on_read_completed_(false), | |
280 defer_eof_(false), | |
281 received_on_will_read_(false), | |
282 received_eof_(false), | |
283 received_response_completed_(false), | |
284 received_request_redirected_(false), | |
285 total_bytes_downloaded_(0), | |
286 observed_effective_connection_type_( | |
287 net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN) {} | |
288 | |
289 // If true, defers the resource load in OnWillStart. | |
290 void set_defer_request_on_will_start(bool defer_request_on_will_start) { | |
291 defer_request_on_will_start_ = defer_request_on_will_start; | |
292 } | |
293 | |
294 // If true, expect OnWillRead / OnReadCompleted pairs for handling | |
295 // data. Otherwise, expect OnDataDownloaded. | |
296 void set_expect_reads(bool expect_reads) { expect_reads_ = expect_reads; } | |
297 | |
298 // If true, cancel the request in OnReadCompleted by returning false. | |
299 void set_cancel_on_read_completed(bool cancel_on_read_completed) { | |
300 cancel_on_read_completed_ = cancel_on_read_completed; | |
301 } | |
302 | |
303 // If true, cancel the request in OnReadCompleted by returning false. | |
304 void set_defer_eof(bool defer_eof) { defer_eof_ = defer_eof; } | |
305 | |
306 const GURL& start_url() const { return start_url_; } | |
307 ResourceResponse* response() const { return response_.get(); } | |
308 ResourceResponse* redirect_response() const { | |
309 return redirect_response_.get(); | |
310 } | |
311 bool received_response_completed() const { | |
312 return received_response_completed_; | |
313 } | |
314 bool received_request_redirected() const { | |
315 return received_request_redirected_; | |
316 } | |
317 const net::URLRequestStatus& status() const { return status_; } | |
318 int total_bytes_downloaded() const { return total_bytes_downloaded_; } | |
319 | |
320 net::EffectiveConnectionType observed_effective_connection_type() const { | |
321 return observed_effective_connection_type_; | |
322 } | |
323 | |
324 void Resume() { | |
325 controller()->Resume(); | |
326 } | |
327 | |
328 bool OnRequestRedirected(const net::RedirectInfo& redirect_info, | |
329 ResourceResponse* response, | |
330 bool* defer) override { | |
331 redirect_response_ = response; | |
332 received_request_redirected_ = true; | |
333 return true; | |
334 } | |
335 | |
336 bool OnResponseStarted(ResourceResponse* response, bool* defer) override { | |
337 EXPECT_FALSE(response_.get()); | |
338 response_ = response; | |
339 observed_effective_connection_type_ = | |
340 response->head.effective_connection_type; | |
341 return true; | |
342 } | |
343 | |
344 bool OnWillStart(const GURL& url, bool* defer) override { | |
345 EXPECT_TRUE(start_url_.is_empty()); | |
346 start_url_ = url; | |
347 if (defer_request_on_will_start_) { | |
348 *defer = true; | |
349 deferred_run_loop_.Quit(); | |
350 } | |
351 return true; | |
352 } | |
353 | |
354 bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, | |
355 int* buf_size, | |
356 int min_size) override { | |
357 EXPECT_TRUE(expect_reads_); | |
358 EXPECT_FALSE(received_on_will_read_); | |
359 EXPECT_FALSE(received_eof_); | |
360 EXPECT_FALSE(received_response_completed_); | |
361 | |
362 *buf = read_buffer_; | |
363 *buf_size = kReadBufSize; | |
364 received_on_will_read_ = true; | |
365 return true; | |
366 } | |
367 | |
368 bool OnReadCompleted(int bytes_read, bool* defer) override { | |
369 EXPECT_TRUE(received_on_will_read_); | |
370 EXPECT_TRUE(expect_reads_); | |
371 EXPECT_FALSE(received_response_completed_); | |
372 | |
373 if (bytes_read == 0) { | |
374 received_eof_ = true; | |
375 if (defer_eof_) { | |
376 defer_eof_ = false; | |
377 *defer = true; | |
378 deferred_run_loop_.Quit(); | |
379 } | |
380 } | |
381 | |
382 // Need another OnWillRead() call before seeing an OnReadCompleted(). | |
383 received_on_will_read_ = false; | |
384 | |
385 return !cancel_on_read_completed_; | |
386 } | |
387 | |
388 void OnResponseCompleted(const net::URLRequestStatus& status, | |
389 bool* defer) override { | |
390 EXPECT_FALSE(received_response_completed_); | |
391 if (status.is_success() && expect_reads_) | |
392 EXPECT_TRUE(received_eof_); | |
393 | |
394 received_response_completed_ = true; | |
395 status_ = status; | |
396 response_completed_run_loop_.Quit(); | |
397 } | |
398 | |
399 void OnDataDownloaded(int bytes_downloaded) override { | |
400 EXPECT_FALSE(expect_reads_); | |
401 total_bytes_downloaded_ += bytes_downloaded; | |
402 } | |
403 | |
404 // Waits for the the first deferred step to run, if there is one. | |
405 void WaitForDeferredStep() { | |
406 DCHECK(defer_request_on_will_start_ || defer_eof_); | |
407 deferred_run_loop_.Run(); | |
408 } | |
409 | |
410 // Waits until the response has completed. | |
411 void WaitForResponseComplete() { | |
412 response_completed_run_loop_.Run(); | |
413 EXPECT_TRUE(received_response_completed_); | |
414 } | |
415 | |
416 private: | |
417 scoped_refptr<net::IOBuffer> read_buffer_; | |
418 | |
419 bool defer_request_on_will_start_; | |
420 bool expect_reads_; | |
421 bool cancel_on_read_completed_; | |
422 bool defer_eof_; | |
423 | |
424 GURL start_url_; | |
425 scoped_refptr<ResourceResponse> response_; | |
426 scoped_refptr<ResourceResponse> redirect_response_; | |
427 bool received_on_will_read_; | |
428 bool received_eof_; | |
429 bool received_response_completed_; | |
430 bool received_request_redirected_; | |
431 net::URLRequestStatus status_; | |
432 int total_bytes_downloaded_; | |
433 base::RunLoop deferred_run_loop_; | |
434 base::RunLoop response_completed_run_loop_; | |
435 std::unique_ptr<base::RunLoop> wait_for_progress_run_loop_; | |
436 net::EffectiveConnectionType observed_effective_connection_type_; | |
437 }; | |
438 | |
439 // Test browser client that captures calls to SelectClientCertificates and | 270 // Test browser client that captures calls to SelectClientCertificates and |
440 // records the arguments of the most recent call for later inspection. | 271 // records the arguments of the most recent call for later inspection. |
441 class SelectCertificateBrowserClient : public TestContentBrowserClient { | 272 class SelectCertificateBrowserClient : public TestContentBrowserClient { |
442 public: | 273 public: |
443 SelectCertificateBrowserClient() : call_count_(0) {} | 274 SelectCertificateBrowserClient() : call_count_(0) {} |
444 | 275 |
445 // Waits until the first call to SelectClientCertificate. | 276 // Waits until the first call to SelectClientCertificate. |
446 void WaitForSelectCertificate() { | 277 void WaitForSelectCertificate() { |
447 select_certificate_run_loop_.Run(); | 278 select_certificate_run_loop_.Run(); |
448 // Process any pending messages - just so tests can check if | 279 // Process any pending messages - just so tests can check if |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
558 test_url_request_context_(true), | 389 test_url_request_context_(true), |
559 resource_context_(&test_url_request_context_), | 390 resource_context_(&test_url_request_context_), |
560 raw_ptr_resource_handler_(NULL), | 391 raw_ptr_resource_handler_(NULL), |
561 raw_ptr_to_request_(NULL) { | 392 raw_ptr_to_request_(NULL) { |
562 test_url_request_context_.set_job_factory(&job_factory_); | 393 test_url_request_context_.set_job_factory(&job_factory_); |
563 test_url_request_context_.set_network_quality_estimator( | 394 test_url_request_context_.set_network_quality_estimator( |
564 &network_quality_estimator_); | 395 &network_quality_estimator_); |
565 test_url_request_context_.Init(); | 396 test_url_request_context_.Init(); |
566 } | 397 } |
567 | 398 |
568 GURL test_url() const { return net::URLRequestTestJob::test_url_1(); } | 399 // URL with a response body of test_data(). |
| 400 GURL test_direct_url() const { return net::URLRequestTestJob::test_url_1(); } |
569 | 401 |
570 TestNetworkQualityEstimator* network_quality_estimator() { | 402 // URL that redirects to test_direct_url(). The ResourceLoader is set up to |
571 return &network_quality_estimator_; | 403 // use this URL by default. |
| 404 GURL test_redirect_url() const { |
| 405 return net::URLRequestTestJob::test_url_redirect_to_url_1(); |
572 } | 406 } |
573 | 407 |
574 std::string test_data() const { | 408 std::string test_data() const { |
575 return net::URLRequestTestJob::test_data_1(); | 409 return net::URLRequestTestJob::test_data_1(); |
576 } | 410 } |
577 | 411 |
| 412 TestNetworkQualityEstimator* network_quality_estimator() { |
| 413 return &network_quality_estimator_; |
| 414 } |
| 415 |
578 virtual std::unique_ptr<net::URLRequestJobFactory::ProtocolHandler> | 416 virtual std::unique_ptr<net::URLRequestJobFactory::ProtocolHandler> |
579 CreateProtocolHandler() { | 417 CreateProtocolHandler() { |
580 return net::URLRequestTestJob::CreateProtocolHandler(); | 418 return net::URLRequestTestJob::CreateProtocolHandler(); |
581 } | 419 } |
582 | 420 |
583 virtual std::unique_ptr<ResourceHandler> WrapResourceHandler( | 421 virtual std::unique_ptr<ResourceHandler> WrapResourceHandler( |
584 std::unique_ptr<ResourceHandlerStub> leaf_handler, | 422 std::unique_ptr<TestResourceHandler> leaf_handler, |
585 net::URLRequest* request) { | 423 net::URLRequest* request) { |
586 return std::move(leaf_handler); | 424 return std::move(leaf_handler); |
587 } | 425 } |
588 | 426 |
589 // Replaces loader_ with a new one for |request|. | 427 // Replaces loader_ with a new one for |request|. |
590 void SetUpResourceLoader(std::unique_ptr<net::URLRequest> request, | 428 void SetUpResourceLoader(std::unique_ptr<net::URLRequest> request, |
591 ResourceType resource_type, | 429 ResourceType resource_type, |
592 bool belongs_to_main_frame) { | 430 bool belongs_to_main_frame) { |
593 raw_ptr_to_request_ = request.get(); | 431 raw_ptr_to_request_ = request.get(); |
594 | 432 |
595 // A request marked as a main frame request must also belong to a main | 433 // A request marked as a main frame request must also belong to a main |
596 // frame. | 434 // frame. |
597 ASSERT_TRUE((resource_type != RESOURCE_TYPE_MAIN_FRAME) || | 435 ASSERT_TRUE((resource_type != RESOURCE_TYPE_MAIN_FRAME) || |
598 belongs_to_main_frame); | 436 belongs_to_main_frame); |
599 | 437 |
600 RenderFrameHost* rfh = web_contents_->GetMainFrame(); | 438 RenderFrameHost* rfh = web_contents_->GetMainFrame(); |
601 ResourceRequestInfo::AllocateForTesting( | 439 ResourceRequestInfo::AllocateForTesting( |
602 request.get(), resource_type, &resource_context_, | 440 request.get(), resource_type, &resource_context_, |
603 rfh->GetProcess()->GetID(), rfh->GetRenderViewHost()->GetRoutingID(), | 441 rfh->GetProcess()->GetID(), rfh->GetRenderViewHost()->GetRoutingID(), |
604 rfh->GetRoutingID(), belongs_to_main_frame, | 442 rfh->GetRoutingID(), belongs_to_main_frame, |
605 false /* parent_is_main_frame */, true /* allow_download */, | 443 false /* parent_is_main_frame */, true /* allow_download */, |
606 false /* is_async */, false /* is_using_lofi_ */); | 444 false /* is_async */, false /* is_using_lofi_ */); |
607 std::unique_ptr<ResourceHandlerStub> resource_handler( | 445 std::unique_ptr<TestResourceHandler> resource_handler( |
608 new ResourceHandlerStub(request.get())); | 446 new TestResourceHandler(nullptr, nullptr)); |
609 raw_ptr_resource_handler_ = resource_handler.get(); | 447 raw_ptr_resource_handler_ = resource_handler.get(); |
610 loader_.reset(new ResourceLoader( | 448 loader_.reset(new ResourceLoader( |
611 std::move(request), | 449 std::move(request), |
612 WrapResourceHandler(std::move(resource_handler), raw_ptr_to_request_), | 450 WrapResourceHandler(std::move(resource_handler), raw_ptr_to_request_), |
613 this)); | 451 this)); |
614 } | 452 } |
615 | 453 |
| 454 void SetUpResourceLoaderForUrl(const GURL& test_url) { |
| 455 std::unique_ptr<net::URLRequest> request( |
| 456 resource_context_.GetRequestContext()->CreateRequest( |
| 457 test_url, net::DEFAULT_PRIORITY, nullptr /* delegate */)); |
| 458 SetUpResourceLoader(std::move(request), RESOURCE_TYPE_MAIN_FRAME, true); |
| 459 } |
| 460 |
616 void SetUp() override { | 461 void SetUp() override { |
617 job_factory_.SetProtocolHandler("test", CreateProtocolHandler()); | 462 job_factory_.SetProtocolHandler("test", CreateProtocolHandler()); |
| 463 net::URLRequestFailedJob::AddUrlHandler(); |
618 | 464 |
619 browser_context_.reset(new TestBrowserContext()); | 465 browser_context_.reset(new TestBrowserContext()); |
620 scoped_refptr<SiteInstance> site_instance = | 466 scoped_refptr<SiteInstance> site_instance = |
621 SiteInstance::Create(browser_context_.get()); | 467 SiteInstance::Create(browser_context_.get()); |
622 web_contents_.reset( | 468 web_contents_.reset( |
623 TestWebContents::Create(browser_context_.get(), site_instance.get())); | 469 TestWebContents::Create(browser_context_.get(), site_instance.get())); |
624 | 470 SetUpResourceLoaderForUrl(test_redirect_url()); |
625 std::unique_ptr<net::URLRequest> request( | |
626 resource_context_.GetRequestContext()->CreateRequest( | |
627 test_url(), net::DEFAULT_PRIORITY, nullptr /* delegate */)); | |
628 SetUpResourceLoader(std::move(request), RESOURCE_TYPE_MAIN_FRAME, true); | |
629 } | 471 } |
630 | 472 |
631 void TearDown() override { | 473 void TearDown() override { |
632 // Destroy the WebContents and pump the event loop before destroying | 474 // Destroy the WebContents and pump the event loop before destroying |
633 // |rvh_test_enabler_| and |thread_bundle_|. This lets asynchronous cleanup | 475 // |rvh_test_enabler_| and |thread_bundle_|. This lets asynchronous cleanup |
634 // tasks complete. | 476 // tasks complete. |
635 web_contents_.reset(); | 477 web_contents_.reset(); |
| 478 |
| 479 // Clean up handlers. |
| 480 net::URLRequestFilter::GetInstance()->ClearHandlers(); |
| 481 |
636 base::RunLoop().RunUntilIdle(); | 482 base::RunLoop().RunUntilIdle(); |
637 } | 483 } |
638 | 484 |
639 void SetClientCertStore(std::unique_ptr<net::ClientCertStore> store) { | 485 void SetClientCertStore(std::unique_ptr<net::ClientCertStore> store) { |
640 dummy_cert_store_ = std::move(store); | 486 dummy_cert_store_ = std::move(store); |
641 } | 487 } |
642 | 488 |
643 // ResourceLoaderDelegate: | 489 // ResourceLoaderDelegate: |
644 ResourceDispatcherHostLoginDelegate* CreateLoginDelegate( | 490 ResourceDispatcherHostLoginDelegate* CreateLoginDelegate( |
645 ResourceLoader* loader, | 491 ResourceLoader* loader, |
646 net::AuthChallengeInfo* auth_info) override { | 492 net::AuthChallengeInfo* auth_info) override { |
647 return NULL; | 493 return NULL; |
648 } | 494 } |
649 bool HandleExternalProtocol(ResourceLoader* loader, | 495 bool HandleExternalProtocol(ResourceLoader* loader, |
650 const GURL& url) override { | 496 const GURL& url) override { |
| 497 EXPECT_EQ(loader, loader_.get()); |
651 return false; | 498 return false; |
652 } | 499 } |
653 void DidStartRequest(ResourceLoader* loader) override {} | 500 void DidStartRequest(ResourceLoader* loader) override { |
| 501 EXPECT_EQ(loader, loader_.get()); |
| 502 EXPECT_EQ(0, did_finish_loading_); |
| 503 EXPECT_EQ(0, did_start_request_); |
| 504 ++did_start_request_; |
| 505 } |
654 void DidReceiveRedirect(ResourceLoader* loader, | 506 void DidReceiveRedirect(ResourceLoader* loader, |
655 const GURL& new_url, | 507 const GURL& new_url, |
656 ResourceResponse* response) override {} | 508 ResourceResponse* response) override { |
657 void DidReceiveResponse(ResourceLoader* loader) override {} | 509 EXPECT_EQ(loader, loader_.get()); |
658 void DidFinishLoading(ResourceLoader* loader) override {} | 510 EXPECT_EQ(0, did_finish_loading_); |
| 511 EXPECT_EQ(0, did_receive_response_); |
| 512 EXPECT_EQ(1, did_start_request_); |
| 513 ++did_received_redirect_; |
| 514 } |
| 515 void DidReceiveResponse(ResourceLoader* loader) override { |
| 516 EXPECT_EQ(loader, loader_.get()); |
| 517 EXPECT_EQ(0, did_finish_loading_); |
| 518 EXPECT_EQ(0, did_receive_response_); |
| 519 EXPECT_EQ(1, did_start_request_); |
| 520 ++did_receive_response_; |
| 521 } |
| 522 void DidFinishLoading(ResourceLoader* loader) override { |
| 523 EXPECT_EQ(loader, loader_.get()); |
| 524 EXPECT_EQ(0, did_finish_loading_); |
| 525 ++did_finish_loading_; |
| 526 } |
659 std::unique_ptr<net::ClientCertStore> CreateClientCertStore( | 527 std::unique_ptr<net::ClientCertStore> CreateClientCertStore( |
660 ResourceLoader* loader) override { | 528 ResourceLoader* loader) override { |
661 return std::move(dummy_cert_store_); | 529 return std::move(dummy_cert_store_); |
662 } | 530 } |
663 | 531 |
664 TestBrowserThreadBundle thread_bundle_; | 532 TestBrowserThreadBundle thread_bundle_; |
665 RenderViewHostTestEnabler rvh_test_enabler_; | 533 RenderViewHostTestEnabler rvh_test_enabler_; |
666 | 534 |
| 535 // Record which ResourceDispatcherHostDelegate methods have been invoked. |
| 536 int did_start_request_ = 0; |
| 537 int did_received_redirect_ = 0; |
| 538 int did_receive_response_ = 0; |
| 539 int did_finish_loading_ = 0; |
| 540 |
667 net::URLRequestJobFactoryImpl job_factory_; | 541 net::URLRequestJobFactoryImpl job_factory_; |
668 TestNetworkQualityEstimator network_quality_estimator_; | 542 TestNetworkQualityEstimator network_quality_estimator_; |
669 net::TestURLRequestContext test_url_request_context_; | 543 net::TestURLRequestContext test_url_request_context_; |
670 MockResourceContext resource_context_; | 544 MockResourceContext resource_context_; |
671 std::unique_ptr<TestBrowserContext> browser_context_; | 545 std::unique_ptr<TestBrowserContext> browser_context_; |
672 std::unique_ptr<TestWebContents> web_contents_; | 546 std::unique_ptr<TestWebContents> web_contents_; |
673 std::unique_ptr<net::ClientCertStore> dummy_cert_store_; | 547 std::unique_ptr<net::ClientCertStore> dummy_cert_store_; |
674 | 548 |
675 // The ResourceLoader owns the URLRequest and the ResourceHandler. | 549 // The ResourceLoader owns the URLRequest and the ResourceHandler. |
676 ResourceHandlerStub* raw_ptr_resource_handler_; | 550 TestResourceHandler* raw_ptr_resource_handler_; |
677 net::URLRequest* raw_ptr_to_request_; | 551 net::URLRequest* raw_ptr_to_request_; |
678 std::unique_ptr<ResourceLoader> loader_; | 552 std::unique_ptr<ResourceLoader> loader_; |
679 }; | 553 }; |
680 | 554 |
681 class ClientCertResourceLoaderTest : public ResourceLoaderTest { | 555 class ClientCertResourceLoaderTest : public ResourceLoaderTest { |
682 protected: | 556 protected: |
683 std::unique_ptr<net::URLRequestJobFactory::ProtocolHandler> | 557 std::unique_ptr<net::URLRequestJobFactory::ProtocolHandler> |
684 CreateProtocolHandler() override { | 558 CreateProtocolHandler() override { |
685 return base::WrapUnique(new MockClientCertJobProtocolHandler); | 559 return base::WrapUnique(new MockClientCertJobProtocolHandler); |
686 } | 560 } |
| 561 |
| 562 void SetUp() override { |
| 563 ResourceLoaderTest::SetUp(); |
| 564 // These tests don't expect any redirects. |
| 565 SetUpResourceLoaderForUrl(test_direct_url()); |
| 566 } |
687 }; | 567 }; |
688 | 568 |
689 // A ResourceLoaderTest that intercepts https://example.test and | 569 // A ResourceLoaderTest that intercepts https://example.test and |
690 // https://example-redirect.test URLs and sets SSL info on the | 570 // https://example-redirect.test URLs and sets SSL info on the |
691 // responses. The latter serves a Location: header in the response. | 571 // responses. The latter serves a Location: header in the response. |
692 class HTTPSSecurityInfoResourceLoaderTest : public ResourceLoaderTest { | 572 class HTTPSSecurityInfoResourceLoaderTest : public ResourceLoaderTest { |
693 public: | 573 public: |
694 HTTPSSecurityInfoResourceLoaderTest() | 574 HTTPSSecurityInfoResourceLoaderTest() |
695 : ResourceLoaderTest(), | 575 : ResourceLoaderTest(), |
696 test_https_url_("https://example.test"), | 576 test_https_url_("https://example.test"), |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
733 SetClientCertStore(std::move(test_store)); | 613 SetClientCertStore(std::move(test_store)); |
734 | 614 |
735 // Plug in test content browser client. | 615 // Plug in test content browser client. |
736 SelectCertificateBrowserClient test_client; | 616 SelectCertificateBrowserClient test_client; |
737 ContentBrowserClient* old_client = SetBrowserClientForTesting(&test_client); | 617 ContentBrowserClient* old_client = SetBrowserClientForTesting(&test_client); |
738 | 618 |
739 // Start the request and wait for it to pause. | 619 // Start the request and wait for it to pause. |
740 loader_->StartRequest(); | 620 loader_->StartRequest(); |
741 test_client.WaitForSelectCertificate(); | 621 test_client.WaitForSelectCertificate(); |
742 | 622 |
743 EXPECT_FALSE(raw_ptr_resource_handler_->received_response_completed()); | 623 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_completed_called()); |
744 | 624 |
745 // Check if the test store was queried against correct |cert_authorities|. | 625 // Check if the test store was queried against correct |cert_authorities|. |
746 EXPECT_EQ(1, store_request_count); | 626 EXPECT_EQ(1, store_request_count); |
747 EXPECT_EQ(MockClientCertURLRequestJob::test_authorities(), | 627 EXPECT_EQ(MockClientCertURLRequestJob::test_authorities(), |
748 store_requested_authorities); | 628 store_requested_authorities); |
749 | 629 |
750 // Check if the retrieved certificates were passed to the content browser | 630 // Check if the retrieved certificates were passed to the content browser |
751 // client. | 631 // client. |
752 EXPECT_EQ(1, test_client.call_count()); | 632 EXPECT_EQ(1, test_client.call_count()); |
753 EXPECT_EQ(dummy_certs, test_client.passed_certs()); | 633 EXPECT_EQ(dummy_certs, test_client.passed_certs()); |
754 | 634 |
755 // Continue the request. | 635 // Continue the request. |
756 test_client.ContinueWithCertificate(nullptr); | 636 test_client.ContinueWithCertificate(nullptr); |
757 raw_ptr_resource_handler_->WaitForResponseComplete(); | 637 raw_ptr_resource_handler_->WaitUntilResponseComplete(); |
758 EXPECT_EQ(net::OK, raw_ptr_resource_handler_->status().error()); | 638 EXPECT_EQ(net::OK, raw_ptr_resource_handler_->final_status().error()); |
759 | 639 |
760 // Restore the original content browser client. | 640 // Restore the original content browser client. |
761 SetBrowserClientForTesting(old_client); | 641 SetBrowserClientForTesting(old_client); |
762 } | 642 } |
763 | 643 |
764 // Tests that client certificates are requested on a platform with NULL | 644 // Tests that client certificates are requested on a platform with NULL |
765 // ClientCertStore. | 645 // ClientCertStore. |
766 TEST_F(ClientCertResourceLoaderTest, WithNullStore) { | 646 TEST_F(ClientCertResourceLoaderTest, WithNullStore) { |
767 // Plug in test content browser client. | 647 // Plug in test content browser client. |
768 SelectCertificateBrowserClient test_client; | 648 SelectCertificateBrowserClient test_client; |
769 ContentBrowserClient* old_client = SetBrowserClientForTesting(&test_client); | 649 ContentBrowserClient* old_client = SetBrowserClientForTesting(&test_client); |
770 | 650 |
771 // Start the request and wait for it to pause. | 651 // Start the request and wait for it to pause. |
772 loader_->StartRequest(); | 652 loader_->StartRequest(); |
773 test_client.WaitForSelectCertificate(); | 653 test_client.WaitForSelectCertificate(); |
774 | 654 |
775 // Check if the SelectClientCertificate was called on the content browser | 655 // Check if the SelectClientCertificate was called on the content browser |
776 // client. | 656 // client. |
777 EXPECT_EQ(1, test_client.call_count()); | 657 EXPECT_EQ(1, test_client.call_count()); |
778 EXPECT_EQ(net::CertificateList(), test_client.passed_certs()); | 658 EXPECT_EQ(net::CertificateList(), test_client.passed_certs()); |
779 | 659 |
780 // Continue the request. | 660 // Continue the request. |
781 test_client.ContinueWithCertificate(nullptr); | 661 test_client.ContinueWithCertificate(nullptr); |
782 raw_ptr_resource_handler_->WaitForResponseComplete(); | 662 raw_ptr_resource_handler_->WaitUntilResponseComplete(); |
783 EXPECT_EQ(net::OK, raw_ptr_resource_handler_->status().error()); | 663 EXPECT_EQ(net::OK, raw_ptr_resource_handler_->final_status().error()); |
784 | 664 |
785 // Restore the original content browser client. | 665 // Restore the original content browser client. |
786 SetBrowserClientForTesting(old_client); | 666 SetBrowserClientForTesting(old_client); |
787 } | 667 } |
788 | 668 |
789 // Tests that the ContentBrowserClient may cancel a certificate request. | 669 // Tests that the ContentBrowserClient may cancel a certificate request. |
790 TEST_F(ClientCertResourceLoaderTest, CancelSelection) { | 670 TEST_F(ClientCertResourceLoaderTest, CancelSelection) { |
791 // Plug in test content browser client. | 671 // Plug in test content browser client. |
792 SelectCertificateBrowserClient test_client; | 672 SelectCertificateBrowserClient test_client; |
793 ContentBrowserClient* old_client = SetBrowserClientForTesting(&test_client); | 673 ContentBrowserClient* old_client = SetBrowserClientForTesting(&test_client); |
794 | 674 |
795 // Start the request and wait for it to pause. | 675 // Start the request and wait for it to pause. |
796 loader_->StartRequest(); | 676 loader_->StartRequest(); |
797 test_client.WaitForSelectCertificate(); | 677 test_client.WaitForSelectCertificate(); |
798 | 678 |
799 // Check if the SelectClientCertificate was called on the content browser | 679 // Check if the SelectClientCertificate was called on the content browser |
800 // client. | 680 // client. |
801 EXPECT_EQ(1, test_client.call_count()); | 681 EXPECT_EQ(1, test_client.call_count()); |
802 EXPECT_EQ(net::CertificateList(), test_client.passed_certs()); | 682 EXPECT_EQ(net::CertificateList(), test_client.passed_certs()); |
803 | 683 |
804 // Cancel the request. | 684 // Cancel the request. |
805 test_client.CancelCertificateSelection(); | 685 test_client.CancelCertificateSelection(); |
806 raw_ptr_resource_handler_->WaitForResponseComplete(); | 686 raw_ptr_resource_handler_->WaitUntilResponseComplete(); |
807 EXPECT_EQ(net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED, | 687 EXPECT_EQ(net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED, |
808 raw_ptr_resource_handler_->status().error()); | 688 raw_ptr_resource_handler_->final_status().error()); |
809 | 689 |
810 // Restore the original content browser client. | 690 // Restore the original content browser client. |
811 SetBrowserClientForTesting(old_client); | 691 SetBrowserClientForTesting(old_client); |
812 } | 692 } |
813 | 693 |
814 // Verifies that requests without WebContents attached abort. | 694 // Verifies that requests without WebContents attached abort. |
815 TEST_F(ClientCertResourceLoaderTest, NoWebContents) { | 695 TEST_F(ClientCertResourceLoaderTest, NoWebContents) { |
816 // Destroy the WebContents before starting the request. | 696 // Destroy the WebContents before starting the request. |
817 web_contents_.reset(); | 697 web_contents_.reset(); |
818 | 698 |
819 // Plug in test content browser client. | 699 // Plug in test content browser client. |
820 SelectCertificateBrowserClient test_client; | 700 SelectCertificateBrowserClient test_client; |
821 ContentBrowserClient* old_client = SetBrowserClientForTesting(&test_client); | 701 ContentBrowserClient* old_client = SetBrowserClientForTesting(&test_client); |
822 | 702 |
823 // Start the request and wait for it to complete. | 703 // Start the request and wait for it to complete. |
824 loader_->StartRequest(); | 704 loader_->StartRequest(); |
825 raw_ptr_resource_handler_->WaitForResponseComplete(); | 705 raw_ptr_resource_handler_->WaitUntilResponseComplete(); |
826 | 706 |
827 // Check that SelectClientCertificate wasn't called and the request aborted. | 707 // Check that SelectClientCertificate wasn't called and the request aborted. |
828 EXPECT_EQ(0, test_client.call_count()); | 708 EXPECT_EQ(0, test_client.call_count()); |
829 EXPECT_EQ(net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED, | 709 EXPECT_EQ(net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED, |
830 raw_ptr_resource_handler_->status().error()); | 710 raw_ptr_resource_handler_->final_status().error()); |
831 | 711 |
832 // Restore the original content browser client. | 712 // Restore the original content browser client. |
833 SetBrowserClientForTesting(old_client); | 713 SetBrowserClientForTesting(old_client); |
834 } | 714 } |
835 | 715 |
836 // Verifies that ClientCertStore's callback doesn't crash if called after the | 716 // Verifies that ClientCertStore's callback doesn't crash if called after the |
837 // loader is destroyed. | 717 // loader is destroyed. |
838 TEST_F(ClientCertResourceLoaderTest, StoreAsyncCancel) { | 718 TEST_F(ClientCertResourceLoaderTest, StoreAsyncCancel) { |
839 base::RunLoop loader_destroyed_run_loop; | 719 base::RunLoop loader_destroyed_run_loop; |
840 LoaderDestroyingCertStore* test_store = | 720 LoaderDestroyingCertStore* test_store = |
841 new LoaderDestroyingCertStore(&loader_, | 721 new LoaderDestroyingCertStore(&loader_, |
842 loader_destroyed_run_loop.QuitClosure()); | 722 loader_destroyed_run_loop.QuitClosure()); |
843 SetClientCertStore(base::WrapUnique(test_store)); | 723 SetClientCertStore(base::WrapUnique(test_store)); |
844 | 724 |
845 loader_->StartRequest(); | 725 loader_->StartRequest(); |
846 loader_destroyed_run_loop.Run(); | 726 loader_destroyed_run_loop.Run(); |
847 EXPECT_FALSE(loader_); | 727 EXPECT_FALSE(loader_); |
848 | 728 |
849 // Pump the event loop to ensure nothing asynchronous crashes either. | 729 // Pump the event loop to ensure nothing asynchronous crashes either. |
850 base::RunLoop().RunUntilIdle(); | 730 base::RunLoop().RunUntilIdle(); |
851 } | 731 } |
852 | 732 |
853 TEST_F(ResourceLoaderTest, ResumeCancelledRequest) { | 733 // Test the case the ResourceHandler defers nothing. |
854 raw_ptr_resource_handler_->set_defer_request_on_will_start(true); | 734 TEST_F(ResourceLoaderTest, SyncResourceHandler) { |
| 735 loader_->StartRequest(); |
| 736 raw_ptr_resource_handler_->WaitUntilResponseComplete(); |
| 737 EXPECT_EQ(1, did_start_request_); |
| 738 EXPECT_EQ(1, did_received_redirect_); |
| 739 EXPECT_EQ(1, did_receive_response_); |
| 740 EXPECT_EQ(1, did_finish_loading_); |
| 741 EXPECT_EQ(1, raw_ptr_resource_handler_->on_request_redirected_called()); |
| 742 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called()); |
| 743 EXPECT_EQ(net::OK, raw_ptr_resource_handler_->final_status().error()); |
| 744 EXPECT_EQ(test_data(), raw_ptr_resource_handler_->body()); |
| 745 } |
| 746 |
| 747 // Test the case the ResourceHandler defers everything. |
| 748 TEST_F(ResourceLoaderTest, AsyncResourceHandler) { |
| 749 raw_ptr_resource_handler_->set_defer_on_will_start(true); |
| 750 raw_ptr_resource_handler_->set_defer_on_request_redirected(true); |
| 751 raw_ptr_resource_handler_->set_defer_on_response_started(true); |
| 752 raw_ptr_resource_handler_->set_defer_on_read_completed(true); |
| 753 raw_ptr_resource_handler_->set_defer_on_read_eof(true); |
| 754 raw_ptr_resource_handler_->set_defer_on_response_completed(true); |
| 755 |
| 756 // Start and run until OnWillStart. |
| 757 loader_->StartRequest(); |
| 758 raw_ptr_resource_handler_->WaitUntilDeferred(); |
| 759 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_start_called()); |
| 760 |
| 761 // Spinning the message loop should not advance the state further. |
| 762 base::RunLoop().RunUntilIdle(); |
| 763 EXPECT_EQ(0, did_start_request_); |
| 764 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_start_called()); |
| 765 EXPECT_EQ(0, raw_ptr_resource_handler_->on_request_redirected_called()); |
| 766 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_completed_called()); |
| 767 |
| 768 // Resume and run until OnRequestRedirected. |
| 769 raw_ptr_resource_handler_->Resume(); |
| 770 raw_ptr_resource_handler_->WaitUntilDeferred(); |
| 771 EXPECT_EQ(1, raw_ptr_resource_handler_->on_request_redirected_called()); |
| 772 |
| 773 // Spinning the message loop should not advance the state further. |
| 774 base::RunLoop().RunUntilIdle(); |
| 775 EXPECT_EQ(1, did_received_redirect_); |
| 776 EXPECT_EQ(0, did_receive_response_); |
| 777 EXPECT_EQ(1, raw_ptr_resource_handler_->on_request_redirected_called()); |
| 778 EXPECT_EQ(0, raw_ptr_resource_handler_->on_will_read_called()); |
| 779 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_completed_called()); |
| 780 |
| 781 // Resume and run until OnResponseStarted. |
| 782 raw_ptr_resource_handler_->Resume(); |
| 783 raw_ptr_resource_handler_->WaitUntilDeferred(); |
| 784 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_started_called()); |
| 785 |
| 786 // Spinning the message loop should not advance the state further. |
| 787 base::RunLoop().RunUntilIdle(); |
| 788 EXPECT_EQ(1, did_receive_response_); |
| 789 EXPECT_EQ(0, did_finish_loading_); |
| 790 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_started_called()); |
| 791 EXPECT_EQ(0, raw_ptr_resource_handler_->on_will_read_called()); |
| 792 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_completed_called()); |
| 793 |
| 794 // Resume and run until OnReadCompleted. |
| 795 raw_ptr_resource_handler_->Resume(); |
| 796 raw_ptr_resource_handler_->WaitUntilDeferred(); |
| 797 EXPECT_EQ(1, raw_ptr_resource_handler_->on_read_completed_called()); |
| 798 |
| 799 // Spinning the message loop should not advance the state further. |
| 800 base::RunLoop().RunUntilIdle(); |
| 801 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_read_called()); |
| 802 EXPECT_EQ(1, raw_ptr_resource_handler_->on_read_completed_called()); |
| 803 EXPECT_EQ(0, raw_ptr_resource_handler_->on_read_eof()); |
| 804 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_completed_called()); |
| 805 |
| 806 // Resume and run until the final 0-byte read, signalling EOF. |
| 807 raw_ptr_resource_handler_->Resume(); |
| 808 raw_ptr_resource_handler_->WaitUntilDeferred(); |
| 809 EXPECT_EQ(1, raw_ptr_resource_handler_->on_read_eof()); |
| 810 |
| 811 // Spinning the message loop should not advance the state further. |
| 812 base::RunLoop().RunUntilIdle(); |
| 813 EXPECT_EQ(1, raw_ptr_resource_handler_->on_read_eof()); |
| 814 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_completed_called()); |
| 815 EXPECT_EQ(test_data(), raw_ptr_resource_handler_->body()); |
| 816 |
| 817 // Resume and run until OnResponseCompleted is called, which again defers the |
| 818 // request. |
| 819 raw_ptr_resource_handler_->Resume(); |
| 820 raw_ptr_resource_handler_->WaitUntilResponseComplete(); |
| 821 EXPECT_EQ(0, did_finish_loading_); |
| 822 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called()); |
| 823 EXPECT_EQ(net::OK, raw_ptr_resource_handler_->final_status().error()); |
| 824 EXPECT_EQ(test_data(), raw_ptr_resource_handler_->body()); |
| 825 |
| 826 // Resume and run until all pending tasks. Note that OnResponseCompleted was |
| 827 // invoked in the previous section, so can't use RunUntilCompleted(). |
| 828 raw_ptr_resource_handler_->Resume(); |
| 829 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called()); |
| 830 |
| 831 base::RunLoop().RunUntilIdle(); |
| 832 EXPECT_EQ(1, did_finish_loading_); |
| 833 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called()); |
| 834 EXPECT_EQ(net::OK, raw_ptr_resource_handler_->final_status().error()); |
| 835 EXPECT_EQ(test_data(), raw_ptr_resource_handler_->body()); |
| 836 } |
| 837 |
| 838 TEST_F(ResourceLoaderTest, SyncCancelOnWillStart) { |
| 839 raw_ptr_resource_handler_->set_on_will_start_result(false); |
| 840 |
| 841 loader_->StartRequest(); |
| 842 raw_ptr_resource_handler_->WaitUntilResponseComplete(); |
| 843 base::RunLoop().RunUntilIdle(); |
| 844 EXPECT_EQ(0, did_start_request_); |
| 845 EXPECT_EQ(1, did_finish_loading_); |
| 846 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_start_called()); |
| 847 EXPECT_EQ(0, raw_ptr_resource_handler_->on_request_redirected_called()); |
| 848 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_started_called()); |
| 849 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called()); |
| 850 |
| 851 EXPECT_EQ(net::ERR_ABORTED, |
| 852 raw_ptr_resource_handler_->final_status().error()); |
| 853 EXPECT_EQ("", raw_ptr_resource_handler_->body()); |
| 854 } |
| 855 |
| 856 TEST_F(ResourceLoaderTest, SyncCancelOnRequestRedirected) { |
| 857 raw_ptr_resource_handler_->set_on_request_redirected_result(false); |
| 858 |
| 859 loader_->StartRequest(); |
| 860 raw_ptr_resource_handler_->WaitUntilResponseComplete(); |
| 861 base::RunLoop().RunUntilIdle(); |
| 862 EXPECT_EQ(1, did_received_redirect_); |
| 863 EXPECT_EQ(0, did_receive_response_); |
| 864 EXPECT_EQ(1, did_finish_loading_); |
| 865 EXPECT_EQ(1, raw_ptr_resource_handler_->on_request_redirected_called()); |
| 866 EXPECT_EQ(0, raw_ptr_resource_handler_->on_will_read_called()); |
| 867 EXPECT_EQ(0, raw_ptr_resource_handler_->on_read_completed_called()); |
| 868 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called()); |
| 869 |
| 870 EXPECT_EQ(net::ERR_ABORTED, |
| 871 raw_ptr_resource_handler_->final_status().error()); |
| 872 EXPECT_EQ("", raw_ptr_resource_handler_->body()); |
| 873 } |
| 874 |
| 875 TEST_F(ResourceLoaderTest, SyncCancelOnResponseStarted) { |
| 876 raw_ptr_resource_handler_->set_on_response_started_result(false); |
| 877 |
| 878 loader_->StartRequest(); |
| 879 raw_ptr_resource_handler_->WaitUntilResponseComplete(); |
| 880 base::RunLoop().RunUntilIdle(); |
| 881 EXPECT_EQ(1, did_receive_response_); |
| 882 EXPECT_EQ(1, did_finish_loading_); |
| 883 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_started_called()); |
| 884 EXPECT_EQ(0, raw_ptr_resource_handler_->on_will_read_called()); |
| 885 EXPECT_EQ(0, raw_ptr_resource_handler_->on_read_completed_called()); |
| 886 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called()); |
| 887 |
| 888 EXPECT_EQ(net::ERR_ABORTED, |
| 889 raw_ptr_resource_handler_->final_status().error()); |
| 890 EXPECT_EQ("", raw_ptr_resource_handler_->body()); |
| 891 } |
| 892 |
| 893 TEST_F(ResourceLoaderTest, SyncCancelOnWillRead) { |
| 894 raw_ptr_resource_handler_->set_on_will_read_result(false); |
| 895 |
| 896 loader_->StartRequest(); |
| 897 raw_ptr_resource_handler_->WaitUntilResponseComplete(); |
| 898 base::RunLoop().RunUntilIdle(); |
| 899 EXPECT_EQ(1, did_receive_response_); |
| 900 EXPECT_EQ(1, did_finish_loading_); |
| 901 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_read_called()); |
| 902 EXPECT_EQ(0, raw_ptr_resource_handler_->on_read_completed_called()); |
| 903 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called()); |
| 904 |
| 905 EXPECT_EQ(net::ERR_ABORTED, |
| 906 raw_ptr_resource_handler_->final_status().error()); |
| 907 EXPECT_EQ("", raw_ptr_resource_handler_->body()); |
| 908 } |
| 909 |
| 910 TEST_F(ResourceLoaderTest, SyncCancelOnReadCompleted) { |
| 911 raw_ptr_resource_handler_->set_on_read_completed_result(false); |
| 912 |
| 913 loader_->StartRequest(); |
| 914 raw_ptr_resource_handler_->WaitUntilResponseComplete(); |
| 915 base::RunLoop().RunUntilIdle(); |
| 916 EXPECT_EQ(1, did_receive_response_); |
| 917 EXPECT_EQ(1, did_finish_loading_); |
| 918 EXPECT_EQ(1, raw_ptr_resource_handler_->on_read_completed_called()); |
| 919 EXPECT_EQ(0, raw_ptr_resource_handler_->on_read_eof()); |
| 920 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called()); |
| 921 |
| 922 EXPECT_EQ(net::ERR_ABORTED, |
| 923 raw_ptr_resource_handler_->final_status().error()); |
| 924 EXPECT_LT(0u, raw_ptr_resource_handler_->body().size()); |
| 925 } |
| 926 |
| 927 TEST_F(ResourceLoaderTest, SyncCancelOnReceivedEof) { |
| 928 raw_ptr_resource_handler_->set_on_on_read_eof_result(false); |
| 929 |
| 930 loader_->StartRequest(); |
| 931 raw_ptr_resource_handler_->WaitUntilResponseComplete(); |
| 932 base::RunLoop().RunUntilIdle(); |
| 933 EXPECT_EQ(1, did_receive_response_); |
| 934 EXPECT_EQ(1, did_finish_loading_); |
| 935 EXPECT_EQ(1, raw_ptr_resource_handler_->on_read_eof()); |
| 936 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called()); |
| 937 |
| 938 EXPECT_EQ(net::ERR_ABORTED, |
| 939 raw_ptr_resource_handler_->final_status().error()); |
| 940 EXPECT_EQ(test_data(), raw_ptr_resource_handler_->body()); |
| 941 } |
| 942 |
| 943 TEST_F(ResourceLoaderTest, AsyncCancelOnWillStart) { |
| 944 raw_ptr_resource_handler_->set_defer_on_will_start(true); |
| 945 |
| 946 loader_->StartRequest(); |
| 947 raw_ptr_resource_handler_->WaitUntilDeferred(); |
| 948 raw_ptr_resource_handler_->CancelWithError(net::ERR_FAILED); |
| 949 base::RunLoop().RunUntilIdle(); |
| 950 EXPECT_EQ(0, did_start_request_); |
| 951 EXPECT_EQ(1, did_finish_loading_); |
| 952 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_start_called()); |
| 953 EXPECT_EQ(0, raw_ptr_resource_handler_->on_request_redirected_called()); |
| 954 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_started_called()); |
| 955 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called()); |
| 956 |
| 957 EXPECT_EQ(net::ERR_FAILED, raw_ptr_resource_handler_->final_status().error()); |
| 958 EXPECT_EQ("", raw_ptr_resource_handler_->body()); |
| 959 } |
| 960 |
| 961 TEST_F(ResourceLoaderTest, AsyncCancelOnRequestRedirected) { |
| 962 raw_ptr_resource_handler_->set_defer_on_request_redirected(true); |
| 963 |
| 964 loader_->StartRequest(); |
| 965 raw_ptr_resource_handler_->WaitUntilDeferred(); |
| 966 raw_ptr_resource_handler_->CancelWithError(net::ERR_FAILED); |
| 967 base::RunLoop().RunUntilIdle(); |
| 968 EXPECT_EQ(1, did_received_redirect_); |
| 969 EXPECT_EQ(0, did_receive_response_); |
| 970 EXPECT_EQ(1, did_finish_loading_); |
| 971 EXPECT_EQ(1, raw_ptr_resource_handler_->on_request_redirected_called()); |
| 972 EXPECT_EQ(0, raw_ptr_resource_handler_->on_will_read_called()); |
| 973 EXPECT_EQ(0, raw_ptr_resource_handler_->on_read_completed_called()); |
| 974 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called()); |
| 975 |
| 976 EXPECT_EQ(net::ERR_FAILED, raw_ptr_resource_handler_->final_status().error()); |
| 977 EXPECT_EQ("", raw_ptr_resource_handler_->body()); |
| 978 } |
| 979 |
| 980 TEST_F(ResourceLoaderTest, AsyncCancelOnResponseStarted) { |
| 981 raw_ptr_resource_handler_->set_defer_on_response_started(true); |
| 982 |
| 983 loader_->StartRequest(); |
| 984 raw_ptr_resource_handler_->WaitUntilDeferred(); |
| 985 raw_ptr_resource_handler_->CancelWithError(net::ERR_FAILED); |
| 986 base::RunLoop().RunUntilIdle(); |
| 987 EXPECT_EQ(1, did_receive_response_); |
| 988 EXPECT_EQ(1, did_finish_loading_); |
| 989 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_started_called()); |
| 990 EXPECT_EQ(0, raw_ptr_resource_handler_->on_will_read_called()); |
| 991 EXPECT_EQ(0, raw_ptr_resource_handler_->on_read_completed_called()); |
| 992 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called()); |
| 993 |
| 994 EXPECT_EQ(net::ERR_FAILED, raw_ptr_resource_handler_->final_status().error()); |
| 995 EXPECT_EQ("", raw_ptr_resource_handler_->body()); |
| 996 } |
| 997 |
| 998 TEST_F(ResourceLoaderTest, AsyncCancelOnReadCompleted) { |
| 999 raw_ptr_resource_handler_->set_defer_on_read_completed(true); |
| 1000 |
| 1001 loader_->StartRequest(); |
| 1002 raw_ptr_resource_handler_->WaitUntilDeferred(); |
| 1003 raw_ptr_resource_handler_->CancelWithError(net::ERR_FAILED); |
| 1004 base::RunLoop().RunUntilIdle(); |
| 1005 EXPECT_EQ(1, did_receive_response_); |
| 1006 EXPECT_EQ(1, did_finish_loading_); |
| 1007 EXPECT_EQ(1, raw_ptr_resource_handler_->on_read_completed_called()); |
| 1008 EXPECT_EQ(0, raw_ptr_resource_handler_->on_read_eof()); |
| 1009 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called()); |
| 1010 |
| 1011 EXPECT_EQ(net::ERR_FAILED, raw_ptr_resource_handler_->final_status().error()); |
| 1012 EXPECT_LT(0u, raw_ptr_resource_handler_->body().size()); |
| 1013 } |
| 1014 |
| 1015 TEST_F(ResourceLoaderTest, AsyncCancelOnReceivedEof) { |
| 1016 raw_ptr_resource_handler_->set_defer_on_read_eof(true); |
| 1017 |
| 1018 loader_->StartRequest(); |
| 1019 raw_ptr_resource_handler_->WaitUntilDeferred(); |
| 1020 raw_ptr_resource_handler_->CancelWithError(net::ERR_FAILED); |
| 1021 base::RunLoop().RunUntilIdle(); |
| 1022 EXPECT_EQ(1, did_receive_response_); |
| 1023 EXPECT_EQ(1, did_finish_loading_); |
| 1024 EXPECT_EQ(1, raw_ptr_resource_handler_->on_read_eof()); |
| 1025 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called()); |
| 1026 |
| 1027 EXPECT_EQ(net::ERR_FAILED, raw_ptr_resource_handler_->final_status().error()); |
| 1028 EXPECT_EQ(test_data(), raw_ptr_resource_handler_->body()); |
| 1029 } |
| 1030 |
| 1031 TEST_F(ResourceLoaderTest, RequestFailsOnStart) { |
| 1032 SetUpResourceLoaderForUrl( |
| 1033 net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase( |
| 1034 net::URLRequestFailedJob::START, net::ERR_FAILED)); |
| 1035 |
| 1036 loader_->StartRequest(); |
| 1037 raw_ptr_resource_handler_->WaitUntilResponseComplete(); |
| 1038 base::RunLoop().RunUntilIdle(); |
| 1039 EXPECT_EQ(0, did_received_redirect_); |
| 1040 EXPECT_EQ(0, did_receive_response_); |
| 1041 EXPECT_EQ(1, did_finish_loading_); |
| 1042 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_start_called()); |
| 1043 EXPECT_EQ(0, raw_ptr_resource_handler_->on_request_redirected_called()); |
| 1044 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_started_called()); |
| 1045 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called()); |
| 1046 EXPECT_EQ(net::ERR_FAILED, raw_ptr_resource_handler_->final_status().error()); |
| 1047 EXPECT_EQ("", raw_ptr_resource_handler_->body()); |
| 1048 } |
| 1049 |
| 1050 TEST_F(ResourceLoaderTest, RequestFailsOnReadSync) { |
| 1051 SetUpResourceLoaderForUrl( |
| 1052 net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase( |
| 1053 net::URLRequestFailedJob::READ_SYNC, net::ERR_FAILED)); |
| 1054 |
| 1055 loader_->StartRequest(); |
| 1056 raw_ptr_resource_handler_->WaitUntilResponseComplete(); |
| 1057 base::RunLoop().RunUntilIdle(); |
| 1058 EXPECT_EQ(0, did_received_redirect_); |
| 1059 EXPECT_EQ(1, did_receive_response_); |
| 1060 EXPECT_EQ(1, did_finish_loading_); |
| 1061 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_start_called()); |
| 1062 EXPECT_EQ(0, raw_ptr_resource_handler_->on_request_redirected_called()); |
| 1063 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_started_called()); |
| 1064 EXPECT_EQ(0, raw_ptr_resource_handler_->on_read_completed_called()); |
| 1065 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called()); |
| 1066 EXPECT_EQ(net::ERR_FAILED, raw_ptr_resource_handler_->final_status().error()); |
| 1067 EXPECT_EQ("", raw_ptr_resource_handler_->body()); |
| 1068 } |
| 1069 |
| 1070 TEST_F(ResourceLoaderTest, RequestFailsOnReadAsync) { |
| 1071 SetUpResourceLoaderForUrl( |
| 1072 net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase( |
| 1073 net::URLRequestFailedJob::READ_ASYNC, net::ERR_FAILED)); |
| 1074 |
| 1075 loader_->StartRequest(); |
| 1076 raw_ptr_resource_handler_->WaitUntilResponseComplete(); |
| 1077 base::RunLoop().RunUntilIdle(); |
| 1078 EXPECT_EQ(0, did_received_redirect_); |
| 1079 EXPECT_EQ(1, did_receive_response_); |
| 1080 EXPECT_EQ(1, did_finish_loading_); |
| 1081 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_start_called()); |
| 1082 EXPECT_EQ(0, raw_ptr_resource_handler_->on_request_redirected_called()); |
| 1083 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_started_called()); |
| 1084 EXPECT_EQ(0, raw_ptr_resource_handler_->on_read_completed_called()); |
| 1085 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called()); |
| 1086 EXPECT_EQ(net::ERR_FAILED, raw_ptr_resource_handler_->final_status().error()); |
| 1087 EXPECT_EQ("", raw_ptr_resource_handler_->body()); |
| 1088 } |
| 1089 |
| 1090 TEST_F(ResourceLoaderTest, OutOfBandCancelDuringStart) { |
| 1091 SetUpResourceLoaderForUrl( |
| 1092 net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase( |
| 1093 net::URLRequestFailedJob::START, net::ERR_IO_PENDING)); |
| 1094 |
| 1095 loader_->StartRequest(); |
| 1096 base::RunLoop().RunUntilIdle(); |
| 1097 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_start_called()); |
| 1098 EXPECT_EQ(0, raw_ptr_resource_handler_->on_request_redirected_called()); |
| 1099 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_started_called()); |
| 1100 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_completed_called()); |
| 1101 |
| 1102 raw_ptr_resource_handler_->CancelWithError(net::ERR_FAILED); |
| 1103 raw_ptr_resource_handler_->WaitUntilResponseComplete(); |
| 1104 |
| 1105 EXPECT_EQ(0, did_received_redirect_); |
| 1106 EXPECT_EQ(0, did_receive_response_); |
| 1107 EXPECT_EQ(1, did_finish_loading_); |
| 1108 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_start_called()); |
| 1109 EXPECT_EQ(0, raw_ptr_resource_handler_->on_request_redirected_called()); |
| 1110 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_started_called()); |
| 1111 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called()); |
| 1112 EXPECT_EQ(net::ERR_FAILED, raw_ptr_resource_handler_->final_status().error()); |
| 1113 EXPECT_EQ("", raw_ptr_resource_handler_->body()); |
| 1114 } |
| 1115 |
| 1116 TEST_F(ResourceLoaderTest, OutOfBandCancelDuringRead) { |
| 1117 SetUpResourceLoaderForUrl( |
| 1118 net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase( |
| 1119 net::URLRequestFailedJob::READ_SYNC, net::ERR_IO_PENDING)); |
| 1120 |
| 1121 loader_->StartRequest(); |
| 1122 base::RunLoop().RunUntilIdle(); |
| 1123 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_start_called()); |
| 1124 EXPECT_EQ(0, raw_ptr_resource_handler_->on_request_redirected_called()); |
| 1125 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_started_called()); |
| 1126 EXPECT_EQ(0, raw_ptr_resource_handler_->on_read_completed_called()); |
| 1127 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_completed_called()); |
| 1128 |
| 1129 raw_ptr_resource_handler_->CancelWithError(net::ERR_FAILED); |
| 1130 raw_ptr_resource_handler_->WaitUntilResponseComplete(); |
| 1131 EXPECT_EQ(0, did_received_redirect_); |
| 1132 EXPECT_EQ(1, did_receive_response_); |
| 1133 EXPECT_EQ(1, did_finish_loading_); |
| 1134 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_start_called()); |
| 1135 EXPECT_EQ(0, raw_ptr_resource_handler_->on_request_redirected_called()); |
| 1136 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_started_called()); |
| 1137 EXPECT_EQ(0, raw_ptr_resource_handler_->on_read_completed_called()); |
| 1138 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called()); |
| 1139 EXPECT_EQ(net::ERR_FAILED, raw_ptr_resource_handler_->final_status().error()); |
| 1140 EXPECT_EQ("", raw_ptr_resource_handler_->body()); |
| 1141 } |
| 1142 |
| 1143 TEST_F(ResourceLoaderTest, ResumeCanceledRequest) { |
| 1144 raw_ptr_resource_handler_->set_defer_on_will_start(true); |
855 | 1145 |
856 loader_->StartRequest(); | 1146 loader_->StartRequest(); |
857 loader_->CancelRequest(true); | 1147 loader_->CancelRequest(true); |
858 static_cast<ResourceController*>(loader_.get())->Resume(); | 1148 static_cast<ResourceController*>(loader_.get())->Resume(); |
859 } | 1149 } |
860 | 1150 |
861 // Tests that no invariants are broken if a ResourceHandler cancels during | |
862 // OnReadCompleted. | |
863 TEST_F(ResourceLoaderTest, CancelOnReadCompleted) { | |
864 raw_ptr_resource_handler_->set_cancel_on_read_completed(true); | |
865 | |
866 loader_->StartRequest(); | |
867 raw_ptr_resource_handler_->WaitForResponseComplete(); | |
868 | |
869 EXPECT_EQ(test_url(), raw_ptr_resource_handler_->start_url()); | |
870 EXPECT_EQ(net::URLRequestStatus::CANCELED, | |
871 raw_ptr_resource_handler_->status().status()); | |
872 } | |
873 | |
874 // Tests that no invariants are broken if a ResourceHandler defers EOF. | |
875 TEST_F(ResourceLoaderTest, DeferEOF) { | |
876 raw_ptr_resource_handler_->set_defer_eof(true); | |
877 | |
878 loader_->StartRequest(); | |
879 raw_ptr_resource_handler_->WaitForDeferredStep(); | |
880 | |
881 EXPECT_EQ(test_url(), raw_ptr_resource_handler_->start_url()); | |
882 EXPECT_FALSE(raw_ptr_resource_handler_->received_response_completed()); | |
883 | |
884 raw_ptr_resource_handler_->Resume(); | |
885 raw_ptr_resource_handler_->WaitForResponseComplete(); | |
886 EXPECT_EQ(net::URLRequestStatus::SUCCESS, | |
887 raw_ptr_resource_handler_->status().status()); | |
888 } | |
889 | |
890 class ResourceLoaderRedirectToFileTest : public ResourceLoaderTest { | 1151 class ResourceLoaderRedirectToFileTest : public ResourceLoaderTest { |
891 public: | 1152 public: |
892 ResourceLoaderRedirectToFileTest() | 1153 ResourceLoaderRedirectToFileTest() |
893 : file_stream_(NULL), | 1154 : file_stream_(NULL), |
894 redirect_to_file_resource_handler_(NULL) { | 1155 redirect_to_file_resource_handler_(NULL) { |
895 } | 1156 } |
896 | 1157 |
897 ~ResourceLoaderRedirectToFileTest() override { | 1158 ~ResourceLoaderRedirectToFileTest() override { |
898 // Releasing the loader should result in destroying the file asynchronously. | 1159 // Releasing the loader should result in destroying the file asynchronously. |
899 file_stream_ = nullptr; | 1160 file_stream_ = nullptr; |
900 deletable_file_ = nullptr; | 1161 deletable_file_ = nullptr; |
901 loader_.reset(); | 1162 loader_.reset(); |
902 | 1163 |
903 // Wait for the task to delete the file to run, and make sure the file is | 1164 // Wait for the task to delete the file to run, and make sure the file is |
904 // cleaned up. | 1165 // cleaned up. |
905 base::RunLoop().RunUntilIdle(); | 1166 base::RunLoop().RunUntilIdle(); |
906 EXPECT_FALSE(base::PathExists(temp_path())); | 1167 EXPECT_FALSE(base::PathExists(temp_path())); |
907 } | 1168 } |
908 | 1169 |
909 base::FilePath temp_path() const { return temp_path_; } | 1170 base::FilePath temp_path() const { return temp_path_; } |
910 ShareableFileReference* deletable_file() const { | 1171 ShareableFileReference* deletable_file() const { |
911 return deletable_file_.get(); | 1172 return deletable_file_.get(); |
912 } | 1173 } |
913 net::testing::MockFileStream* file_stream() const { return file_stream_; } | 1174 net::testing::MockFileStream* file_stream() const { return file_stream_; } |
914 RedirectToFileResourceHandler* redirect_to_file_resource_handler() const { | 1175 RedirectToFileResourceHandler* redirect_to_file_resource_handler() const { |
915 return redirect_to_file_resource_handler_; | 1176 return redirect_to_file_resource_handler_; |
916 } | 1177 } |
917 | 1178 |
918 std::unique_ptr<ResourceHandler> WrapResourceHandler( | 1179 std::unique_ptr<ResourceHandler> WrapResourceHandler( |
919 std::unique_ptr<ResourceHandlerStub> leaf_handler, | 1180 std::unique_ptr<TestResourceHandler> leaf_handler, |
920 net::URLRequest* request) override { | 1181 net::URLRequest* request) override { |
921 leaf_handler->set_expect_reads(false); | 1182 leaf_handler->set_expect_on_data_downloaded(true); |
922 | 1183 |
923 // Make a temporary file. | 1184 // Make a temporary file. |
924 CHECK(base::CreateTemporaryFile(&temp_path_)); | 1185 CHECK(base::CreateTemporaryFile(&temp_path_)); |
925 int flags = base::File::FLAG_WRITE | base::File::FLAG_TEMPORARY | | 1186 int flags = base::File::FLAG_WRITE | base::File::FLAG_TEMPORARY | |
926 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_ASYNC; | 1187 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_ASYNC; |
927 base::File file(temp_path_, flags); | 1188 base::File file(temp_path_, flags); |
928 CHECK(file.IsValid()); | 1189 CHECK(file.IsValid()); |
929 | 1190 |
930 // Create mock file streams and a ShareableFileReference. | 1191 // Create mock file streams and a ShareableFileReference. |
931 std::unique_ptr<net::testing::MockFileStream> file_stream( | 1192 std::unique_ptr<net::testing::MockFileStream> file_stream( |
(...skipping 29 matching lines...) Expand all Loading... |
961 // These are owned by the ResourceLoader. | 1222 // These are owned by the ResourceLoader. |
962 net::testing::MockFileStream* file_stream_; | 1223 net::testing::MockFileStream* file_stream_; |
963 RedirectToFileResourceHandler* redirect_to_file_resource_handler_; | 1224 RedirectToFileResourceHandler* redirect_to_file_resource_handler_; |
964 }; | 1225 }; |
965 | 1226 |
966 // Tests that a RedirectToFileResourceHandler works and forwards everything | 1227 // Tests that a RedirectToFileResourceHandler works and forwards everything |
967 // downstream. | 1228 // downstream. |
968 TEST_F(ResourceLoaderRedirectToFileTest, Basic) { | 1229 TEST_F(ResourceLoaderRedirectToFileTest, Basic) { |
969 // Run it to completion. | 1230 // Run it to completion. |
970 loader_->StartRequest(); | 1231 loader_->StartRequest(); |
971 raw_ptr_resource_handler_->WaitForResponseComplete(); | 1232 raw_ptr_resource_handler_->WaitUntilResponseComplete(); |
972 | 1233 |
973 // Check that the handler forwarded all information to the downstream handler. | 1234 // Check that the handler forwarded all information to the downstream handler. |
974 EXPECT_EQ(temp_path(), | 1235 EXPECT_EQ( |
975 raw_ptr_resource_handler_->response()->head.download_file_path); | 1236 temp_path(), |
976 EXPECT_EQ(test_url(), raw_ptr_resource_handler_->start_url()); | 1237 raw_ptr_resource_handler_->resource_response()->head.download_file_path); |
| 1238 EXPECT_EQ(test_redirect_url(), raw_ptr_resource_handler_->start_url()); |
977 EXPECT_EQ(net::URLRequestStatus::SUCCESS, | 1239 EXPECT_EQ(net::URLRequestStatus::SUCCESS, |
978 raw_ptr_resource_handler_->status().status()); | 1240 raw_ptr_resource_handler_->final_status().status()); |
979 EXPECT_EQ(test_data().size(), static_cast<size_t>( | 1241 EXPECT_EQ(test_data().size(), static_cast<size_t>( |
980 raw_ptr_resource_handler_->total_bytes_downloaded())); | 1242 raw_ptr_resource_handler_->total_bytes_downloaded())); |
981 | 1243 |
982 // Check that the data was written to the file. | 1244 // Check that the data was written to the file. |
983 std::string contents; | 1245 std::string contents; |
984 ASSERT_TRUE(base::ReadFileToString(temp_path(), &contents)); | 1246 ASSERT_TRUE(base::ReadFileToString(temp_path(), &contents)); |
985 EXPECT_EQ(test_data(), contents); | 1247 EXPECT_EQ(test_data(), contents); |
986 } | 1248 } |
987 | 1249 |
988 // Tests that RedirectToFileResourceHandler handles errors in creating the | 1250 // Tests that RedirectToFileResourceHandler handles errors in creating the |
989 // temporary file. | 1251 // temporary file. |
990 TEST_F(ResourceLoaderRedirectToFileTest, CreateTemporaryError) { | 1252 TEST_F(ResourceLoaderRedirectToFileTest, CreateTemporaryError) { |
991 // Swap out the create temporary function. | 1253 // Swap out the create temporary function. |
992 redirect_to_file_resource_handler()-> | 1254 redirect_to_file_resource_handler()-> |
993 SetCreateTemporaryFileStreamFunctionForTesting( | 1255 SetCreateTemporaryFileStreamFunctionForTesting( |
994 base::Bind(&CreateTemporaryError, base::File::FILE_ERROR_FAILED)); | 1256 base::Bind(&CreateTemporaryError, base::File::FILE_ERROR_FAILED)); |
995 | 1257 |
996 // Run it to completion. | 1258 // Run it to completion. |
997 loader_->StartRequest(); | 1259 loader_->StartRequest(); |
998 raw_ptr_resource_handler_->WaitForResponseComplete(); | 1260 raw_ptr_resource_handler_->WaitUntilResponseComplete(); |
999 | 1261 |
1000 // To downstream, the request was canceled. | 1262 // To downstream, the request was canceled. |
1001 EXPECT_EQ(net::URLRequestStatus::CANCELED, | 1263 EXPECT_EQ(net::URLRequestStatus::CANCELED, |
1002 raw_ptr_resource_handler_->status().status()); | 1264 raw_ptr_resource_handler_->final_status().status()); |
1003 EXPECT_EQ(0, raw_ptr_resource_handler_->total_bytes_downloaded()); | 1265 EXPECT_EQ(0, raw_ptr_resource_handler_->total_bytes_downloaded()); |
1004 } | 1266 } |
1005 | 1267 |
1006 // Tests that RedirectToFileResourceHandler handles synchronous write errors. | 1268 // Tests that RedirectToFileResourceHandler handles synchronous write errors. |
1007 TEST_F(ResourceLoaderRedirectToFileTest, WriteError) { | 1269 TEST_F(ResourceLoaderRedirectToFileTest, WriteError) { |
1008 file_stream()->set_forced_error(net::ERR_FAILED); | 1270 file_stream()->set_forced_error(net::ERR_FAILED); |
1009 | 1271 |
1010 // Run it to completion. | 1272 // Run it to completion. |
1011 loader_->StartRequest(); | 1273 loader_->StartRequest(); |
1012 raw_ptr_resource_handler_->WaitForResponseComplete(); | 1274 raw_ptr_resource_handler_->WaitUntilResponseComplete(); |
1013 | 1275 |
1014 // To downstream, the request was canceled sometime after it started, but | 1276 // To downstream, the request was canceled sometime after it started, but |
1015 // before any data was written. | 1277 // before any data was written. |
1016 EXPECT_EQ(temp_path(), | 1278 EXPECT_EQ( |
1017 raw_ptr_resource_handler_->response()->head.download_file_path); | 1279 temp_path(), |
1018 EXPECT_EQ(test_url(), raw_ptr_resource_handler_->start_url()); | 1280 raw_ptr_resource_handler_->resource_response()->head.download_file_path); |
| 1281 EXPECT_EQ(test_redirect_url(), raw_ptr_resource_handler_->start_url()); |
1019 EXPECT_EQ(net::URLRequestStatus::CANCELED, | 1282 EXPECT_EQ(net::URLRequestStatus::CANCELED, |
1020 raw_ptr_resource_handler_->status().status()); | 1283 raw_ptr_resource_handler_->final_status().status()); |
1021 EXPECT_EQ(0, raw_ptr_resource_handler_->total_bytes_downloaded()); | 1284 EXPECT_EQ(0, raw_ptr_resource_handler_->total_bytes_downloaded()); |
1022 } | 1285 } |
1023 | 1286 |
1024 // Tests that RedirectToFileResourceHandler handles asynchronous write errors. | 1287 // Tests that RedirectToFileResourceHandler handles asynchronous write errors. |
1025 TEST_F(ResourceLoaderRedirectToFileTest, WriteErrorAsync) { | 1288 TEST_F(ResourceLoaderRedirectToFileTest, WriteErrorAsync) { |
1026 file_stream()->set_forced_error_async(net::ERR_FAILED); | 1289 file_stream()->set_forced_error_async(net::ERR_FAILED); |
1027 | 1290 |
1028 // Run it to completion. | 1291 // Run it to completion. |
1029 loader_->StartRequest(); | 1292 loader_->StartRequest(); |
1030 raw_ptr_resource_handler_->WaitForResponseComplete(); | 1293 raw_ptr_resource_handler_->WaitUntilResponseComplete(); |
1031 | 1294 |
1032 // To downstream, the request was canceled sometime after it started, but | 1295 // To downstream, the request was canceled sometime after it started, but |
1033 // before any data was written. | 1296 // before any data was written. |
1034 EXPECT_EQ(temp_path(), | 1297 EXPECT_EQ( |
1035 raw_ptr_resource_handler_->response()->head.download_file_path); | 1298 temp_path(), |
1036 EXPECT_EQ(test_url(), raw_ptr_resource_handler_->start_url()); | 1299 raw_ptr_resource_handler_->resource_response()->head.download_file_path); |
| 1300 EXPECT_EQ(test_redirect_url(), raw_ptr_resource_handler_->start_url()); |
1037 EXPECT_EQ(net::URLRequestStatus::CANCELED, | 1301 EXPECT_EQ(net::URLRequestStatus::CANCELED, |
1038 raw_ptr_resource_handler_->status().status()); | 1302 raw_ptr_resource_handler_->final_status().status()); |
1039 EXPECT_EQ(0, raw_ptr_resource_handler_->total_bytes_downloaded()); | 1303 EXPECT_EQ(0, raw_ptr_resource_handler_->total_bytes_downloaded()); |
1040 } | 1304 } |
1041 | 1305 |
1042 // Tests that RedirectToFileHandler defers completion if there are outstanding | 1306 // Tests that RedirectToFileHandler defers completion if there are outstanding |
1043 // writes and accounts for errors which occur in that time. | 1307 // writes and accounts for errors which occur in that time. |
1044 TEST_F(ResourceLoaderRedirectToFileTest, DeferCompletion) { | 1308 TEST_F(ResourceLoaderRedirectToFileTest, DeferCompletion) { |
1045 // Program the MockFileStream to error asynchronously, but throttle the | 1309 // Program the MockFileStream to error asynchronously, but throttle the |
1046 // callback. | 1310 // callback. |
1047 file_stream()->set_forced_error_async(net::ERR_FAILED); | 1311 file_stream()->set_forced_error_async(net::ERR_FAILED); |
1048 file_stream()->ThrottleCallbacks(); | 1312 file_stream()->ThrottleCallbacks(); |
1049 | 1313 |
1050 // Run it as far as it will go. | 1314 // Run it as far as it will go. |
1051 loader_->StartRequest(); | 1315 loader_->StartRequest(); |
1052 base::RunLoop().RunUntilIdle(); | 1316 base::RunLoop().RunUntilIdle(); |
1053 | 1317 |
1054 // At this point, the request should have completed. | 1318 // At this point, the request should have completed. |
1055 EXPECT_EQ(net::URLRequestStatus::SUCCESS, | 1319 EXPECT_EQ(net::URLRequestStatus::SUCCESS, |
1056 raw_ptr_to_request_->status().status()); | 1320 raw_ptr_to_request_->status().status()); |
1057 | 1321 |
1058 // However, the resource loader stack is stuck somewhere after receiving the | 1322 // However, the resource loader stack is stuck somewhere after receiving the |
1059 // response. | 1323 // response. |
1060 EXPECT_EQ(temp_path(), | 1324 EXPECT_EQ( |
1061 raw_ptr_resource_handler_->response()->head.download_file_path); | 1325 temp_path(), |
1062 EXPECT_EQ(test_url(), raw_ptr_resource_handler_->start_url()); | 1326 raw_ptr_resource_handler_->resource_response()->head.download_file_path); |
1063 EXPECT_FALSE(raw_ptr_resource_handler_->received_response_completed()); | 1327 EXPECT_EQ(test_redirect_url(), raw_ptr_resource_handler_->start_url()); |
| 1328 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_completed_called()); |
1064 EXPECT_EQ(0, raw_ptr_resource_handler_->total_bytes_downloaded()); | 1329 EXPECT_EQ(0, raw_ptr_resource_handler_->total_bytes_downloaded()); |
1065 | 1330 |
1066 // Now, release the floodgates. | 1331 // Now, release the floodgates. |
1067 file_stream()->ReleaseCallbacks(); | 1332 file_stream()->ReleaseCallbacks(); |
1068 raw_ptr_resource_handler_->WaitForResponseComplete(); | 1333 raw_ptr_resource_handler_->WaitUntilResponseComplete(); |
1069 | 1334 |
1070 // Although the URLRequest was successful, the leaf handler sees a failure | 1335 // Although the URLRequest was successful, the leaf handler sees a failure |
1071 // because the write never completed. | 1336 // because the write never completed. |
1072 EXPECT_EQ(net::URLRequestStatus::CANCELED, | 1337 EXPECT_EQ(net::URLRequestStatus::CANCELED, |
1073 raw_ptr_resource_handler_->status().status()); | 1338 raw_ptr_resource_handler_->final_status().status()); |
1074 } | 1339 } |
1075 | 1340 |
1076 // Tests that a RedirectToFileResourceHandler behaves properly when the | 1341 // Tests that a RedirectToFileResourceHandler behaves properly when the |
1077 // downstream handler defers OnWillStart. | 1342 // downstream handler defers OnWillStart. |
1078 TEST_F(ResourceLoaderRedirectToFileTest, DownstreamDeferStart) { | 1343 TEST_F(ResourceLoaderRedirectToFileTest, DownstreamDeferStart) { |
1079 // Defer OnWillStart. | 1344 // Defer OnWillStart. |
1080 raw_ptr_resource_handler_->set_defer_request_on_will_start(true); | 1345 raw_ptr_resource_handler_->set_defer_on_will_start(true); |
1081 | 1346 |
1082 // Run as far as we'll go. | 1347 // Run as far as we'll go. |
1083 loader_->StartRequest(); | 1348 loader_->StartRequest(); |
1084 raw_ptr_resource_handler_->WaitForDeferredStep(); | 1349 raw_ptr_resource_handler_->WaitUntilDeferred(); |
1085 | 1350 |
1086 // The request should have stopped at OnWillStart. | 1351 // The request should have stopped at OnWillStart. |
1087 EXPECT_EQ(test_url(), raw_ptr_resource_handler_->start_url()); | 1352 EXPECT_EQ(test_redirect_url(), raw_ptr_resource_handler_->start_url()); |
1088 EXPECT_FALSE(raw_ptr_resource_handler_->response()); | 1353 EXPECT_FALSE(raw_ptr_resource_handler_->resource_response()); |
1089 EXPECT_FALSE(raw_ptr_resource_handler_->received_response_completed()); | 1354 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_completed_called()); |
1090 EXPECT_EQ(0, raw_ptr_resource_handler_->total_bytes_downloaded()); | 1355 EXPECT_EQ(0, raw_ptr_resource_handler_->total_bytes_downloaded()); |
1091 | 1356 |
1092 // Now resume the request. Now we complete. | 1357 // Now resume the request. Now we complete. |
1093 raw_ptr_resource_handler_->Resume(); | 1358 raw_ptr_resource_handler_->Resume(); |
1094 raw_ptr_resource_handler_->WaitForResponseComplete(); | 1359 raw_ptr_resource_handler_->WaitUntilResponseComplete(); |
1095 | 1360 |
1096 // Check that the handler forwarded all information to the downstream handler. | 1361 // Check that the handler forwarded all information to the downstream handler. |
1097 EXPECT_EQ(temp_path(), | 1362 EXPECT_EQ( |
1098 raw_ptr_resource_handler_->response()->head.download_file_path); | 1363 temp_path(), |
1099 EXPECT_EQ(test_url(), raw_ptr_resource_handler_->start_url()); | 1364 raw_ptr_resource_handler_->resource_response()->head.download_file_path); |
| 1365 EXPECT_EQ(test_redirect_url(), raw_ptr_resource_handler_->start_url()); |
1100 EXPECT_EQ(net::URLRequestStatus::SUCCESS, | 1366 EXPECT_EQ(net::URLRequestStatus::SUCCESS, |
1101 raw_ptr_resource_handler_->status().status()); | 1367 raw_ptr_resource_handler_->final_status().status()); |
1102 EXPECT_EQ(test_data().size(), static_cast<size_t>( | 1368 EXPECT_EQ(test_data().size(), static_cast<size_t>( |
1103 raw_ptr_resource_handler_->total_bytes_downloaded())); | 1369 raw_ptr_resource_handler_->total_bytes_downloaded())); |
1104 | 1370 |
1105 // Check that the data was written to the file. | 1371 // Check that the data was written to the file. |
1106 std::string contents; | 1372 std::string contents; |
1107 ASSERT_TRUE(base::ReadFileToString(temp_path(), &contents)); | 1373 ASSERT_TRUE(base::ReadFileToString(temp_path(), &contents)); |
1108 EXPECT_EQ(test_data(), contents); | 1374 EXPECT_EQ(test_data(), contents); |
1109 } | 1375 } |
1110 | 1376 |
1111 class EffectiveConnectionTypeResourceLoaderTest : public ResourceLoaderTest { | 1377 class EffectiveConnectionTypeResourceLoaderTest : public ResourceLoaderTest { |
1112 public: | 1378 public: |
1113 void VerifyEffectiveConnectionType( | 1379 void VerifyEffectiveConnectionType( |
1114 ResourceType resource_type, | 1380 ResourceType resource_type, |
1115 bool belongs_to_main_frame, | 1381 bool belongs_to_main_frame, |
1116 net::EffectiveConnectionType set_type, | 1382 net::EffectiveConnectionType set_type, |
1117 net::EffectiveConnectionType expected_type) { | 1383 net::EffectiveConnectionType expected_type) { |
1118 network_quality_estimator()->set_effective_connection_type(set_type); | 1384 network_quality_estimator()->set_effective_connection_type(set_type); |
1119 | 1385 |
1120 // Start the request and wait for it to finish. | 1386 // Start the request and wait for it to finish. |
1121 std::unique_ptr<net::URLRequest> request( | 1387 std::unique_ptr<net::URLRequest> request( |
1122 resource_context_.GetRequestContext()->CreateRequest( | 1388 resource_context_.GetRequestContext()->CreateRequest( |
1123 test_url(), net::DEFAULT_PRIORITY, nullptr /* delegate */)); | 1389 test_redirect_url(), net::DEFAULT_PRIORITY, |
| 1390 nullptr /* delegate */)); |
1124 SetUpResourceLoader(std::move(request), resource_type, | 1391 SetUpResourceLoader(std::move(request), resource_type, |
1125 belongs_to_main_frame); | 1392 belongs_to_main_frame); |
1126 | 1393 |
1127 // Send the request and wait until it completes. | 1394 // Send the request and wait until it completes. |
1128 loader_->StartRequest(); | 1395 loader_->StartRequest(); |
1129 raw_ptr_resource_handler_->WaitForResponseComplete(); | 1396 raw_ptr_resource_handler_->WaitUntilResponseComplete(); |
1130 ASSERT_EQ(net::URLRequestStatus::SUCCESS, | 1397 ASSERT_EQ(net::URLRequestStatus::SUCCESS, |
1131 raw_ptr_to_request_->status().status()); | 1398 raw_ptr_to_request_->status().status()); |
1132 | 1399 |
1133 EXPECT_EQ(expected_type, | 1400 EXPECT_EQ(expected_type, raw_ptr_resource_handler_->resource_response() |
1134 raw_ptr_resource_handler_->observed_effective_connection_type()); | 1401 ->head.effective_connection_type); |
1135 } | 1402 } |
1136 }; | 1403 }; |
1137 | 1404 |
1138 // Tests that the effective connection type is set on main frame requests. | 1405 // Tests that the effective connection type is set on main frame requests. |
1139 TEST_F(EffectiveConnectionTypeResourceLoaderTest, Slow2G) { | 1406 TEST_F(EffectiveConnectionTypeResourceLoaderTest, Slow2G) { |
1140 VerifyEffectiveConnectionType(RESOURCE_TYPE_MAIN_FRAME, true, | 1407 VerifyEffectiveConnectionType(RESOURCE_TYPE_MAIN_FRAME, true, |
1141 net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G, | 1408 net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G, |
1142 net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G); | 1409 net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G); |
1143 } | 1410 } |
1144 | 1411 |
(...skipping 14 matching lines...) Expand all Loading... |
1159 | 1426 |
1160 // Tests that the effective connection type is not set on non-main frame | 1427 // Tests that the effective connection type is not set on non-main frame |
1161 // requests. | 1428 // requests. |
1162 TEST_F(EffectiveConnectionTypeResourceLoaderTest, DoesNotBelongToMainFrame) { | 1429 TEST_F(EffectiveConnectionTypeResourceLoaderTest, DoesNotBelongToMainFrame) { |
1163 VerifyEffectiveConnectionType(RESOURCE_TYPE_OBJECT, false, | 1430 VerifyEffectiveConnectionType(RESOURCE_TYPE_OBJECT, false, |
1164 net::EFFECTIVE_CONNECTION_TYPE_3G, | 1431 net::EFFECTIVE_CONNECTION_TYPE_3G, |
1165 net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN); | 1432 net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN); |
1166 } | 1433 } |
1167 | 1434 |
1168 } // namespace content | 1435 } // namespace content |
OLD | NEW |