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
3 // found in the LICENSE file.
5 #include "content/browser/loader/resource_loader.h"
7 #include "base/files/file.h"
8 #include "base/files/file_util.h"
9 #include "base/message_loop/message_loop_proxy.h"
10 #include "base/run_loop.h"
11 #include "content/browser/browser_thread_impl.h"
12 #include "content/browser/loader/redirect_to_file_resource_handler.h"
13 #include "content/browser/loader/resource_loader_delegate.h"
14 #include "content/public/browser/resource_request_info.h"
15 #include "content/public/common/resource_response.h"
16 #include "content/public/test/mock_resource_context.h"
17 #include "content/public/test/test_browser_thread_bundle.h"
18 #include "content/test/test_content_browser_client.h"
19 #include "ipc/ipc_message.h"
20 #include "net/base/io_buffer.h"
21 #include "net/base/mock_file_stream.h"
22 #include "net/base/request_priority.h"
23 #include "net/cert/x509_certificate.h"
24 #include "net/ssl/client_cert_store.h"
25 #include "net/ssl/ssl_cert_request_info.h"
26 #include "net/url_request/url_request.h"
27 #include "net/url_request/url_request_job_factory_impl.h"
28 #include "net/url_request/url_request_test_job.h"
29 #include "net/url_request/url_request_test_util.h"
30 #include "storage/common/blob/shareable_file_reference.h"
31 #include "testing/gtest/include/gtest/gtest.h"
33 using storage::ShareableFileReference
;
38 // Stub client certificate store that returns a preset list of certificates for
39 // each request and records the arguments of the most recent request for later
41 class ClientCertStoreStub
: public net::ClientCertStore
{
43 ClientCertStoreStub(const net::CertificateList
& certs
)
47 ~ClientCertStoreStub() override
{}
49 // Returns |cert_authorities| field of the certificate request passed in the
50 // most recent call to GetClientCerts().
51 // TODO(ppi): Make the stub independent from the internal representation of
52 // SSLCertRequestInfo. For now it seems that we cannot neither save the
53 // scoped_refptr<> (since it is never passed to us) nor copy the entire
54 // CertificateRequestInfo (since there is no copy constructor).
55 std::vector
<std::string
> requested_authorities() {
56 return requested_authorities_
;
59 // Returns the number of calls to GetClientCerts().
61 return request_count_
;
64 // net::ClientCertStore:
65 void GetClientCerts(const net::SSLCertRequestInfo
& cert_request_info
,
66 net::CertificateList
* selected_certs
,
67 const base::Closure
& callback
) override
{
69 requested_authorities_
= cert_request_info
.cert_authorities
;
70 *selected_certs
= response_
;
75 const net::CertificateList response_
;
77 std::vector
<std::string
> requested_authorities_
;
80 // Arbitrary read buffer size.
81 const int kReadBufSize
= 1024;
83 // Dummy implementation of ResourceHandler, instance of which is needed to
84 // initialize ResourceLoader.
85 class ResourceHandlerStub
: public ResourceHandler
{
87 explicit ResourceHandlerStub(net::URLRequest
* request
)
88 : ResourceHandler(request
),
89 read_buffer_(new net::IOBuffer(kReadBufSize
)),
90 defer_request_on_will_start_(false),
92 cancel_on_read_completed_(false),
94 received_on_will_read_(false),
96 received_response_completed_(false),
97 total_bytes_downloaded_(0) {
100 // If true, defers the resource load in OnWillStart.
101 void set_defer_request_on_will_start(bool defer_request_on_will_start
) {
102 defer_request_on_will_start_
= defer_request_on_will_start
;
105 // If true, expect OnWillRead / OnReadCompleted pairs for handling
106 // data. Otherwise, expect OnDataDownloaded.
107 void set_expect_reads(bool expect_reads
) { expect_reads_
= expect_reads
; }
109 // If true, cancel the request in OnReadCompleted by returning false.
110 void set_cancel_on_read_completed(bool cancel_on_read_completed
) {
111 cancel_on_read_completed_
= cancel_on_read_completed
;
114 // If true, cancel the request in OnReadCompleted by returning false.
115 void set_defer_eof(bool defer_eof
) { defer_eof_
= defer_eof
; }
117 const GURL
& start_url() const { return start_url_
; }
118 ResourceResponse
* response() const { return response_
.get(); }
119 bool received_response_completed() const {
120 return received_response_completed_
;
122 const net::URLRequestStatus
& status() const { return status_
; }
123 int total_bytes_downloaded() const { return total_bytes_downloaded_
; }
126 controller()->Resume();
129 // ResourceHandler implementation:
130 bool OnUploadProgress(uint64 position
, uint64 size
) override
{
135 bool OnRequestRedirected(const net::RedirectInfo
& redirect_info
,
136 ResourceResponse
* response
,
137 bool* defer
) override
{
142 bool OnResponseStarted(ResourceResponse
* response
, bool* defer
) override
{
143 EXPECT_FALSE(response_
.get());
144 response_
= response
;
148 bool OnWillStart(const GURL
& url
, bool* defer
) override
{
149 EXPECT_TRUE(start_url_
.is_empty());
151 *defer
= defer_request_on_will_start_
;
155 bool OnBeforeNetworkStart(const GURL
& url
, bool* defer
) override
{
159 bool OnWillRead(scoped_refptr
<net::IOBuffer
>* buf
,
161 int min_size
) override
{
162 EXPECT_TRUE(expect_reads_
);
163 EXPECT_FALSE(received_on_will_read_
);
164 EXPECT_FALSE(received_eof_
);
165 EXPECT_FALSE(received_response_completed_
);
168 *buf_size
= kReadBufSize
;
169 received_on_will_read_
= true;
173 bool OnReadCompleted(int bytes_read
, bool* defer
) override
{
174 EXPECT_TRUE(received_on_will_read_
);
175 EXPECT_TRUE(expect_reads_
);
176 EXPECT_FALSE(received_response_completed_
);
178 if (bytes_read
== 0) {
179 received_eof_
= true;
186 // Need another OnWillRead() call before seeing an OnReadCompleted().
187 received_on_will_read_
= false;
189 return !cancel_on_read_completed_
;
192 void OnResponseCompleted(const net::URLRequestStatus
& status
,
193 const std::string
& security_info
,
194 bool* defer
) override
{
195 EXPECT_FALSE(received_response_completed_
);
196 if (status
.is_success() && expect_reads_
)
197 EXPECT_TRUE(received_eof_
);
199 received_response_completed_
= true;
203 void OnDataDownloaded(int bytes_downloaded
) override
{
204 EXPECT_FALSE(expect_reads_
);
205 total_bytes_downloaded_
+= bytes_downloaded
;
209 scoped_refptr
<net::IOBuffer
> read_buffer_
;
211 bool defer_request_on_will_start_
;
213 bool cancel_on_read_completed_
;
217 scoped_refptr
<ResourceResponse
> response_
;
218 bool received_on_will_read_
;
220 bool received_response_completed_
;
221 net::URLRequestStatus status_
;
222 int total_bytes_downloaded_
;
225 // Test browser client that captures calls to SelectClientCertificates and
226 // records the arguments of the most recent call for later inspection.
227 class SelectCertificateBrowserClient
: public TestContentBrowserClient
{
229 SelectCertificateBrowserClient() : call_count_(0) {}
231 void SelectClientCertificate(
232 int render_process_id
,
234 net::SSLCertRequestInfo
* cert_request_info
,
235 const base::Callback
<void(net::X509Certificate
*)>& callback
) override
{
237 passed_certs_
= cert_request_info
->client_certs
;
244 net::CertificateList
passed_certs() {
245 return passed_certs_
;
249 net::CertificateList passed_certs_
;
253 class ResourceContextStub
: public MockResourceContext
{
255 explicit ResourceContextStub(net::URLRequestContext
* test_request_context
)
256 : MockResourceContext(test_request_context
) {}
258 scoped_ptr
<net::ClientCertStore
> CreateClientCertStore() override
{
259 return dummy_cert_store_
.Pass();
262 void SetClientCertStore(scoped_ptr
<net::ClientCertStore
> store
) {
263 dummy_cert_store_
= store
.Pass();
267 scoped_ptr
<net::ClientCertStore
> dummy_cert_store_
;
270 // Fails to create a temporary file with the given error.
271 void CreateTemporaryError(
272 base::File::Error error
,
273 const CreateTemporaryFileStreamCallback
& callback
) {
274 base::MessageLoop::current()->PostTask(
276 base::Bind(callback
, error
, base::Passed(scoped_ptr
<net::FileStream
>()),
277 scoped_refptr
<ShareableFileReference
>()));
282 class ResourceLoaderTest
: public testing::Test
,
283 public ResourceLoaderDelegate
{
286 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP
),
287 resource_context_(&test_url_request_context_
),
288 raw_ptr_resource_handler_(NULL
),
289 raw_ptr_to_request_(NULL
) {
290 job_factory_
.SetProtocolHandler(
291 "test", net::URLRequestTestJob::CreateProtocolHandler());
292 test_url_request_context_
.set_job_factory(&job_factory_
);
295 GURL
test_url() const {
296 return net::URLRequestTestJob::test_url_1();
299 std::string
test_data() const {
300 return net::URLRequestTestJob::test_data_1();
303 virtual scoped_ptr
<ResourceHandler
> WrapResourceHandler(
304 scoped_ptr
<ResourceHandlerStub
> leaf_handler
,
305 net::URLRequest
* request
) {
306 return leaf_handler
.Pass();
309 void SetUp() override
{
310 const int kRenderProcessId
= 1;
311 const int kRenderViewId
= 2;
313 scoped_ptr
<net::URLRequest
> request(
314 resource_context_
.GetRequestContext()->CreateRequest(
316 net::DEFAULT_PRIORITY
,
318 NULL
/* cookie_store */));
319 raw_ptr_to_request_
= request
.get();
320 ResourceRequestInfo::AllocateForTesting(request
.get(),
321 RESOURCE_TYPE_MAIN_FRAME
,
326 true, // is_main_frame
327 false, // parent_is_main_frame
328 true, // allow_download
330 scoped_ptr
<ResourceHandlerStub
> resource_handler(
331 new ResourceHandlerStub(request
.get()));
332 raw_ptr_resource_handler_
= resource_handler
.get();
333 loader_
.reset(new ResourceLoader(
335 WrapResourceHandler(resource_handler
.Pass(), raw_ptr_to_request_
),
339 // ResourceLoaderDelegate:
340 ResourceDispatcherHostLoginDelegate
* CreateLoginDelegate(
341 ResourceLoader
* loader
,
342 net::AuthChallengeInfo
* auth_info
) override
{
345 bool HandleExternalProtocol(ResourceLoader
* loader
,
346 const GURL
& url
) override
{
349 void DidStartRequest(ResourceLoader
* loader
) override
{}
350 void DidReceiveRedirect(ResourceLoader
* loader
,
351 const GURL
& new_url
) override
{}
352 void DidReceiveResponse(ResourceLoader
* loader
) override
{}
353 void DidFinishLoading(ResourceLoader
* loader
) override
{}
355 content::TestBrowserThreadBundle thread_bundle_
;
357 net::URLRequestJobFactoryImpl job_factory_
;
358 net::TestURLRequestContext test_url_request_context_
;
359 ResourceContextStub resource_context_
;
361 // The ResourceLoader owns the URLRequest and the ResourceHandler.
362 ResourceHandlerStub
* raw_ptr_resource_handler_
;
363 net::URLRequest
* raw_ptr_to_request_
;
364 scoped_ptr
<ResourceLoader
> loader_
;
367 // Verifies if a call to net::UrlRequest::Delegate::OnCertificateRequested()
368 // causes client cert store to be queried for certificates and if the returned
369 // certificates are correctly passed to the content browser client for
371 TEST_F(ResourceLoaderTest
, ClientCertStoreLookup
) {
372 // Set up the test client cert store.
373 net::CertificateList
dummy_certs(1, scoped_refptr
<net::X509Certificate
>(
374 new net::X509Certificate("test", "test", base::Time(), base::Time())));
375 scoped_ptr
<ClientCertStoreStub
> test_store(
376 new ClientCertStoreStub(dummy_certs
));
377 EXPECT_EQ(0, test_store
->request_count());
379 // Ownership of the |test_store| is about to be turned over to ResourceLoader.
380 // We need to keep raw pointer copies to access these objects later.
381 ClientCertStoreStub
* raw_ptr_to_store
= test_store
.get();
382 resource_context_
.SetClientCertStore(test_store
.Pass());
384 // Prepare a dummy certificate request.
385 scoped_refptr
<net::SSLCertRequestInfo
> cert_request_info(
386 new net::SSLCertRequestInfo());
387 std::vector
<std::string
> dummy_authority(1, "dummy");
388 cert_request_info
->cert_authorities
= dummy_authority
;
390 // Plug in test content browser client.
391 SelectCertificateBrowserClient test_client
;
392 ContentBrowserClient
* old_client
= SetBrowserClientForTesting(&test_client
);
394 // Everything is set up. Trigger the resource loader certificate request event
395 // and run the message loop.
396 loader_
->OnCertificateRequested(raw_ptr_to_request_
, cert_request_info
.get());
397 base::RunLoop().RunUntilIdle();
399 // Restore the original content browser client.
400 SetBrowserClientForTesting(old_client
);
402 // Check if the test store was queried against correct |cert_authorities|.
403 EXPECT_EQ(1, raw_ptr_to_store
->request_count());
404 EXPECT_EQ(dummy_authority
, raw_ptr_to_store
->requested_authorities());
406 // Check if the retrieved certificates were passed to the content browser
408 EXPECT_EQ(1, test_client
.call_count());
409 EXPECT_EQ(dummy_certs
, test_client
.passed_certs());
412 // Verifies if a call to net::URLRequest::Delegate::OnCertificateRequested()
413 // on a platform with a NULL client cert store still calls the content browser
414 // client for selection.
415 TEST_F(ResourceLoaderTest
, ClientCertStoreNull
) {
416 // Prepare a dummy certificate request.
417 scoped_refptr
<net::SSLCertRequestInfo
> cert_request_info(
418 new net::SSLCertRequestInfo());
419 std::vector
<std::string
> dummy_authority(1, "dummy");
420 cert_request_info
->cert_authorities
= dummy_authority
;
422 // Plug in test content browser client.
423 SelectCertificateBrowserClient test_client
;
424 ContentBrowserClient
* old_client
= SetBrowserClientForTesting(&test_client
);
426 // Everything is set up. Trigger the resource loader certificate request event
427 // and run the message loop.
428 loader_
->OnCertificateRequested(raw_ptr_to_request_
, cert_request_info
.get());
429 base::RunLoop().RunUntilIdle();
431 // Restore the original content browser client.
432 SetBrowserClientForTesting(old_client
);
434 // Check if the SelectClientCertificate was called on the content browser
436 EXPECT_EQ(1, test_client
.call_count());
437 EXPECT_EQ(net::CertificateList(), test_client
.passed_certs());
440 TEST_F(ResourceLoaderTest
, ResumeCancelledRequest
) {
441 raw_ptr_resource_handler_
->set_defer_request_on_will_start(true);
443 loader_
->StartRequest();
444 loader_
->CancelRequest(true);
445 static_cast<ResourceController
*>(loader_
.get())->Resume();
448 // Tests that no invariants are broken if a ResourceHandler cancels during
450 TEST_F(ResourceLoaderTest
, CancelOnReadCompleted
) {
451 raw_ptr_resource_handler_
->set_cancel_on_read_completed(true);
453 loader_
->StartRequest();
454 base::RunLoop().RunUntilIdle();
456 EXPECT_EQ(test_url(), raw_ptr_resource_handler_
->start_url());
457 EXPECT_TRUE(raw_ptr_resource_handler_
->received_response_completed());
458 EXPECT_EQ(net::URLRequestStatus::CANCELED
,
459 raw_ptr_resource_handler_
->status().status());
462 // Tests that no invariants are broken if a ResourceHandler defers EOF.
463 TEST_F(ResourceLoaderTest
, DeferEOF
) {
464 raw_ptr_resource_handler_
->set_defer_eof(true);
466 loader_
->StartRequest();
467 base::RunLoop().RunUntilIdle();
469 EXPECT_EQ(test_url(), raw_ptr_resource_handler_
->start_url());
470 EXPECT_FALSE(raw_ptr_resource_handler_
->received_response_completed());
472 raw_ptr_resource_handler_
->Resume();
473 base::RunLoop().RunUntilIdle();
475 EXPECT_TRUE(raw_ptr_resource_handler_
->received_response_completed());
476 EXPECT_EQ(net::URLRequestStatus::SUCCESS
,
477 raw_ptr_resource_handler_
->status().status());
480 class ResourceLoaderRedirectToFileTest
: public ResourceLoaderTest
{
482 ResourceLoaderRedirectToFileTest()
483 : file_stream_(NULL
),
484 redirect_to_file_resource_handler_(NULL
) {
487 base::FilePath
temp_path() const { return temp_path_
; }
488 ShareableFileReference
* deletable_file() const {
489 return deletable_file_
.get();
491 net::testing::MockFileStream
* file_stream() const { return file_stream_
; }
492 RedirectToFileResourceHandler
* redirect_to_file_resource_handler() const {
493 return redirect_to_file_resource_handler_
;
496 void ReleaseLoader() {
498 deletable_file_
= NULL
;
502 scoped_ptr
<ResourceHandler
> WrapResourceHandler(
503 scoped_ptr
<ResourceHandlerStub
> leaf_handler
,
504 net::URLRequest
* request
) override
{
505 leaf_handler
->set_expect_reads(false);
507 // Make a temporary file.
508 CHECK(base::CreateTemporaryFile(&temp_path_
));
509 int flags
= base::File::FLAG_WRITE
| base::File::FLAG_TEMPORARY
|
510 base::File::FLAG_CREATE_ALWAYS
| base::File::FLAG_ASYNC
;
511 base::File
file(temp_path_
, flags
);
512 CHECK(file
.IsValid());
514 // Create mock file streams and a ShareableFileReference.
515 scoped_ptr
<net::testing::MockFileStream
> file_stream(
516 new net::testing::MockFileStream(file
.Pass(),
517 base::MessageLoopProxy::current()));
518 file_stream_
= file_stream
.get();
519 deletable_file_
= ShareableFileReference::GetOrCreate(
521 ShareableFileReference::DELETE_ON_FINAL_RELEASE
,
522 BrowserThread::GetMessageLoopProxyForThread(
523 BrowserThread::FILE).get());
525 // Inject them into the handler.
526 scoped_ptr
<RedirectToFileResourceHandler
> handler(
527 new RedirectToFileResourceHandler(leaf_handler
.Pass(), request
));
528 redirect_to_file_resource_handler_
= handler
.get();
529 handler
->SetCreateTemporaryFileStreamFunctionForTesting(
530 base::Bind(&ResourceLoaderRedirectToFileTest::PostCallback
,
531 base::Unretained(this),
532 base::Passed(&file_stream
)));
533 return handler
.Pass();
538 scoped_ptr
<net::FileStream
> file_stream
,
539 const CreateTemporaryFileStreamCallback
& callback
) {
540 base::MessageLoop::current()->PostTask(
542 base::Bind(callback
, base::File::FILE_OK
,
543 base::Passed(&file_stream
), deletable_file_
));
546 base::FilePath temp_path_
;
547 scoped_refptr
<ShareableFileReference
> deletable_file_
;
548 // These are owned by the ResourceLoader.
549 net::testing::MockFileStream
* file_stream_
;
550 RedirectToFileResourceHandler
* redirect_to_file_resource_handler_
;
553 // Tests that a RedirectToFileResourceHandler works and forwards everything
555 TEST_F(ResourceLoaderRedirectToFileTest
, Basic
) {
556 // Run it to completion.
557 loader_
->StartRequest();
558 base::RunLoop().RunUntilIdle();
560 // Check that the handler forwarded all information to the downstream handler.
561 EXPECT_EQ(temp_path(),
562 raw_ptr_resource_handler_
->response()->head
.download_file_path
);
563 EXPECT_EQ(test_url(), raw_ptr_resource_handler_
->start_url());
564 EXPECT_TRUE(raw_ptr_resource_handler_
->received_response_completed());
565 EXPECT_EQ(net::URLRequestStatus::SUCCESS
,
566 raw_ptr_resource_handler_
->status().status());
567 EXPECT_EQ(test_data().size(), static_cast<size_t>(
568 raw_ptr_resource_handler_
->total_bytes_downloaded()));
570 // Check that the data was written to the file.
571 std::string contents
;
572 ASSERT_TRUE(base::ReadFileToString(temp_path(), &contents
));
573 EXPECT_EQ(test_data(), contents
);
575 // Release the loader and the saved reference to file. The file should be gone
578 base::RunLoop().RunUntilIdle();
579 EXPECT_FALSE(base::PathExists(temp_path()));
582 // Tests that RedirectToFileResourceHandler handles errors in creating the
584 TEST_F(ResourceLoaderRedirectToFileTest
, CreateTemporaryError
) {
585 // Swap out the create temporary function.
586 redirect_to_file_resource_handler()->
587 SetCreateTemporaryFileStreamFunctionForTesting(
588 base::Bind(&CreateTemporaryError
, base::File::FILE_ERROR_FAILED
));
590 // Run it to completion.
591 loader_
->StartRequest();
592 base::RunLoop().RunUntilIdle();
594 // To downstream, the request was canceled.
595 EXPECT_TRUE(raw_ptr_resource_handler_
->received_response_completed());
596 EXPECT_EQ(net::URLRequestStatus::CANCELED
,
597 raw_ptr_resource_handler_
->status().status());
598 EXPECT_EQ(0, raw_ptr_resource_handler_
->total_bytes_downloaded());
601 // Tests that RedirectToFileResourceHandler handles synchronous write errors.
602 TEST_F(ResourceLoaderRedirectToFileTest
, WriteError
) {
603 file_stream()->set_forced_error(net::ERR_FAILED
);
605 // Run it to completion.
606 loader_
->StartRequest();
607 base::RunLoop().RunUntilIdle();
609 // To downstream, the request was canceled sometime after it started, but
610 // before any data was written.
611 EXPECT_EQ(temp_path(),
612 raw_ptr_resource_handler_
->response()->head
.download_file_path
);
613 EXPECT_EQ(test_url(), raw_ptr_resource_handler_
->start_url());
614 EXPECT_TRUE(raw_ptr_resource_handler_
->received_response_completed());
615 EXPECT_EQ(net::URLRequestStatus::CANCELED
,
616 raw_ptr_resource_handler_
->status().status());
617 EXPECT_EQ(0, raw_ptr_resource_handler_
->total_bytes_downloaded());
619 // Release the loader. The file should be gone now.
621 base::RunLoop().RunUntilIdle();
622 EXPECT_FALSE(base::PathExists(temp_path()));
625 // Tests that RedirectToFileResourceHandler handles asynchronous write errors.
626 TEST_F(ResourceLoaderRedirectToFileTest
, WriteErrorAsync
) {
627 file_stream()->set_forced_error_async(net::ERR_FAILED
);
629 // Run it to completion.
630 loader_
->StartRequest();
631 base::RunLoop().RunUntilIdle();
633 // To downstream, the request was canceled sometime after it started, but
634 // before any data was written.
635 EXPECT_EQ(temp_path(),
636 raw_ptr_resource_handler_
->response()->head
.download_file_path
);
637 EXPECT_EQ(test_url(), raw_ptr_resource_handler_
->start_url());
638 EXPECT_TRUE(raw_ptr_resource_handler_
->received_response_completed());
639 EXPECT_EQ(net::URLRequestStatus::CANCELED
,
640 raw_ptr_resource_handler_
->status().status());
641 EXPECT_EQ(0, raw_ptr_resource_handler_
->total_bytes_downloaded());
643 // Release the loader. The file should be gone now.
645 base::RunLoop().RunUntilIdle();
646 EXPECT_FALSE(base::PathExists(temp_path()));
649 // Tests that RedirectToFileHandler defers completion if there are outstanding
650 // writes and accounts for errors which occur in that time.
651 TEST_F(ResourceLoaderRedirectToFileTest
, DeferCompletion
) {
652 // Program the MockFileStream to error asynchronously, but throttle the
654 file_stream()->set_forced_error_async(net::ERR_FAILED
);
655 file_stream()->ThrottleCallbacks();
657 // Run it as far as it will go.
658 loader_
->StartRequest();
659 base::RunLoop().RunUntilIdle();
661 // At this point, the request should have completed.
662 EXPECT_EQ(net::URLRequestStatus::SUCCESS
,
663 raw_ptr_to_request_
->status().status());
665 // However, the resource loader stack is stuck somewhere after receiving the
667 EXPECT_EQ(temp_path(),
668 raw_ptr_resource_handler_
->response()->head
.download_file_path
);
669 EXPECT_EQ(test_url(), raw_ptr_resource_handler_
->start_url());
670 EXPECT_FALSE(raw_ptr_resource_handler_
->received_response_completed());
671 EXPECT_EQ(0, raw_ptr_resource_handler_
->total_bytes_downloaded());
673 // Now, release the floodgates.
674 file_stream()->ReleaseCallbacks();
675 base::RunLoop().RunUntilIdle();
677 // Although the URLRequest was successful, the leaf handler sees a failure
678 // because the write never completed.
679 EXPECT_TRUE(raw_ptr_resource_handler_
->received_response_completed());
680 EXPECT_EQ(net::URLRequestStatus::CANCELED
,
681 raw_ptr_resource_handler_
->status().status());
683 // Release the loader. The file should be gone now.
685 base::RunLoop().RunUntilIdle();
686 EXPECT_FALSE(base::PathExists(temp_path()));
689 // Tests that a RedirectToFileResourceHandler behaves properly when the
690 // downstream handler defers OnWillStart.
691 TEST_F(ResourceLoaderRedirectToFileTest
, DownstreamDeferStart
) {
692 // Defer OnWillStart.
693 raw_ptr_resource_handler_
->set_defer_request_on_will_start(true);
695 // Run as far as we'll go.
696 loader_
->StartRequest();
697 base::RunLoop().RunUntilIdle();
699 // The request should have stopped at OnWillStart.
700 EXPECT_EQ(test_url(), raw_ptr_resource_handler_
->start_url());
701 EXPECT_FALSE(raw_ptr_resource_handler_
->response());
702 EXPECT_FALSE(raw_ptr_resource_handler_
->received_response_completed());
703 EXPECT_EQ(0, raw_ptr_resource_handler_
->total_bytes_downloaded());
705 // Now resume the request. Now we complete.
706 raw_ptr_resource_handler_
->Resume();
707 base::RunLoop().RunUntilIdle();
709 // Check that the handler forwarded all information to the downstream handler.
710 EXPECT_EQ(temp_path(),
711 raw_ptr_resource_handler_
->response()->head
.download_file_path
);
712 EXPECT_EQ(test_url(), raw_ptr_resource_handler_
->start_url());
713 EXPECT_TRUE(raw_ptr_resource_handler_
->received_response_completed());
714 EXPECT_EQ(net::URLRequestStatus::SUCCESS
,
715 raw_ptr_resource_handler_
->status().status());
716 EXPECT_EQ(test_data().size(), static_cast<size_t>(
717 raw_ptr_resource_handler_
->total_bytes_downloaded()));
719 // Check that the data was written to the file.
720 std::string contents
;
721 ASSERT_TRUE(base::ReadFileToString(temp_path(), &contents
));
722 EXPECT_EQ(test_data(), contents
);
724 // Release the loader. The file should be gone now.
726 base::RunLoop().RunUntilIdle();
727 EXPECT_FALSE(base::PathExists(temp_path()));
730 } // namespace content