1 // Copyright (c) 2012 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 "net/url_request/url_fetcher_impl.h"
10 #include "base/bind.h"
11 #include "base/files/file_util.h"
12 #include "base/files/scoped_temp_dir.h"
13 #include "base/message_loop/message_loop_proxy.h"
14 #include "base/path_service.h"
15 #include "base/run_loop.h"
16 #include "base/strings/stringprintf.h"
17 #include "base/synchronization/waitable_event.h"
18 #include "base/threading/thread.h"
19 #include "build/build_config.h"
20 #include "crypto/nss_util.h"
21 #include "net/base/elements_upload_data_stream.h"
22 #include "net/base/network_change_notifier.h"
23 #include "net/base/upload_bytes_element_reader.h"
24 #include "net/base/upload_element_reader.h"
25 #include "net/base/upload_file_element_reader.h"
26 #include "net/dns/mock_host_resolver.h"
27 #include "net/http/http_response_headers.h"
28 #include "net/test/spawned_test_server/spawned_test_server.h"
29 #include "net/url_request/url_fetcher_delegate.h"
30 #include "net/url_request/url_request_context_getter.h"
31 #include "net/url_request/url_request_test_util.h"
32 #include "net/url_request/url_request_throttler_manager.h"
33 #include "testing/gtest/include/gtest/gtest.h"
35 #if defined(USE_NSS) || defined(OS_IOS)
36 #include "net/ocsp/nss_ocsp.h"
42 using base::TimeDelta
;
44 // TODO(eroman): Add a regression test for http://crbug.com/40505.
48 // TODO(akalin): Move all the test data to somewhere under net/.
49 const base::FilePath::CharType kDocRoot
[] =
50 FILE_PATH_LITERAL("net/data/url_fetcher_impl_unittest");
51 const char kTestServerFilePrefix
[] = "files/";
53 // Request body for streams created by CreateUploadStream.
54 const char kCreateUploadStreamBody
[] = "rosebud";
56 base::FilePath
GetUploadFileTestPath() {
58 PathService::Get(base::DIR_SOURCE_ROOT
, &path
);
60 FILE_PATH_LITERAL("net/data/url_request_unittest/BullRunSpeech.txt"));
63 // Simple URLRequestDelegate that waits for the specified fetcher to complete.
64 // Can only be used once.
65 class WaitingURLFetcherDelegate
: public URLFetcherDelegate
{
67 WaitingURLFetcherDelegate() : fetcher_(nullptr) {}
69 void StartFetcherAndWait(URLFetcher
* fetcher
) {
70 EXPECT_FALSE(fetcher_
);
77 void OnURLFetchComplete(const URLFetcher
* source
) override
{
78 EXPECT_EQ(fetcher_
, source
);
84 base::RunLoop run_loop_
;
87 class ThrottlingTestURLRequestContext
: public TestURLRequestContext
{
89 ThrottlingTestURLRequestContext() : TestURLRequestContext(true) {
90 set_throttler_manager(&throttler_manager_
);
92 DCHECK(throttler_manager() != nullptr);
96 URLRequestThrottlerManager throttler_manager_
;
99 class ThrottlingTestURLRequestContextGetter
100 : public TestURLRequestContextGetter
{
102 ThrottlingTestURLRequestContextGetter(
103 base::MessageLoopProxy
* io_message_loop_proxy
,
104 TestURLRequestContext
* request_context
)
105 : TestURLRequestContextGetter(io_message_loop_proxy
),
106 context_(request_context
) {
109 // TestURLRequestContextGetter:
110 TestURLRequestContext
* GetURLRequestContext() override
{ return context_
; }
113 ~ThrottlingTestURLRequestContextGetter() override
{}
115 TestURLRequestContext
* const context_
;
120 class URLFetcherTest
: public testing::Test
,
121 public URLFetcherDelegate
{
124 : io_message_loop_proxy_(base::MessageLoopProxy::current()),
125 num_upload_streams_created_(0),
127 expected_status_code_(200) {}
129 static int GetNumFetcherCores() {
130 return URLFetcherImpl::GetNumFetcherCores();
133 // Creates a URLFetcher, using the program's main thread to do IO.
134 virtual void CreateFetcher(const GURL
& url
);
136 // URLFetcherDelegate:
137 // Subclasses that override this should either call this function or
138 // CleanupAfterFetchComplete() at the end of their processing, depending on
139 // whether they want to check for a non-empty HTTP 200 response or not.
140 void OnURLFetchComplete(const URLFetcher
* source
) override
;
142 // Deletes |fetcher| and terminates the message loop.
143 void CleanupAfterFetchComplete();
145 scoped_refptr
<base::MessageLoopProxy
> io_message_loop_proxy() {
146 return io_message_loop_proxy_
;
149 TestURLRequestContext
* request_context() {
150 return context_
.get();
153 // Callback passed to URLFetcher to create upload stream by some tests.
154 scoped_ptr
<UploadDataStream
> CreateUploadStream() {
155 ++num_upload_streams_created_
;
156 std::vector
<char> buffer(
157 kCreateUploadStreamBody
,
158 kCreateUploadStreamBody
+ strlen(kCreateUploadStreamBody
));
159 return ElementsUploadDataStream::CreateWithReader(
160 scoped_ptr
<UploadElementReader
>(
161 new UploadOwnedBytesElementReader(&buffer
)),
165 // Number of streams created by CreateUploadStream.
166 size_t num_upload_streams_created() const {
167 return num_upload_streams_created_
;
172 void SetUp() override
{
174 ASSERT_TRUE(test_server_
->Start());
176 context_
.reset(new ThrottlingTestURLRequestContext());
178 #if defined(USE_NSS) || defined(OS_IOS)
179 crypto::EnsureNSSInit();
180 EnsureNSSHttpIOInit();
184 void TearDown() override
{
185 #if defined(USE_NSS) || defined(OS_IOS)
190 // Initializes |test_server_| without starting it. Allows subclasses to use
191 // their own server configuration.
192 virtual void SetUpServer() {
193 test_server_
.reset(new SpawnedTestServer(SpawnedTestServer::TYPE_HTTP
,
194 SpawnedTestServer::kLocalhost
,
195 base::FilePath(kDocRoot
)));
198 // URLFetcher is designed to run on the main UI thread, but in our tests
199 // we assume that the current thread is the IO thread where the URLFetcher
200 // dispatches its requests to. When we wish to simulate being used from
201 // a UI thread, we dispatch a worker thread to do so.
202 scoped_refptr
<base::MessageLoopProxy
> io_message_loop_proxy_
;
204 scoped_ptr
<SpawnedTestServer
> test_server_
;
206 size_t num_upload_streams_created_
;
208 URLFetcherImpl
* fetcher_
;
209 scoped_ptr
<TestURLRequestContext
> context_
;
210 int expected_status_code_
;
213 // A test fixture that uses a MockHostResolver, so that name resolutions can
214 // be manipulated by the tests to keep connections in the resolving state.
215 class URLFetcherMockDnsTest
: public URLFetcherTest
{
218 void SetUp() override
;
221 void CreateFetcher(const GURL
& url
) override
;
223 // URLFetcherDelegate:
224 void OnURLFetchComplete(const URLFetcher
* source
) override
;
228 MockHostResolver resolver_
;
229 scoped_ptr
<URLFetcher
> completed_fetcher_
;
232 void URLFetcherTest::CreateFetcher(const GURL
& url
) {
233 fetcher_
= new URLFetcherImpl(url
, URLFetcher::GET
, this);
234 fetcher_
->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
235 io_message_loop_proxy().get(), request_context()));
239 void URLFetcherTest::OnURLFetchComplete(const URLFetcher
* source
) {
240 EXPECT_TRUE(source
->GetStatus().is_success());
241 EXPECT_EQ(expected_status_code_
, source
->GetResponseCode()); // HTTP OK
244 EXPECT_TRUE(source
->GetResponseAsString(&data
));
245 EXPECT_FALSE(data
.empty());
247 CleanupAfterFetchComplete();
250 void URLFetcherTest::CleanupAfterFetchComplete() {
251 delete fetcher_
; // Have to delete this here and not in the destructor,
252 // because the destructor won't necessarily run on the
253 // same thread that CreateFetcher() did.
255 io_message_loop_proxy()->PostTask(FROM_HERE
,
256 base::MessageLoop::QuitClosure());
257 // If the current message loop is not the IO loop, it will be shut down when
258 // the main loop returns and this thread subsequently goes out of scope.
261 void URLFetcherMockDnsTest::SetUp() {
262 URLFetcherTest::SetUp();
264 resolver_
.set_ondemand_mode(true);
265 resolver_
.rules()->AddRule("example.com", "127.0.0.1");
267 context_
.reset(new TestURLRequestContext(true));
268 context_
->set_host_resolver(&resolver_
);
271 // test_server_.GetURL() returns a URL with 127.0.0.1 (kLocalhost), that is
272 // immediately resolved by the MockHostResolver. Use a hostname instead to
273 // trigger an async resolve.
275 base::StringPrintf("http://example.com:%d/defaultresponse",
276 test_server_
->host_port_pair().port()));
277 ASSERT_TRUE(test_url_
.is_valid());
280 void URLFetcherMockDnsTest::CreateFetcher(const GURL
& url
) {
281 fetcher_
= new URLFetcherImpl(url
, URLFetcher::GET
, this);
282 fetcher_
->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
283 io_message_loop_proxy().get(), request_context()));
286 void URLFetcherMockDnsTest::OnURLFetchComplete(const URLFetcher
* source
) {
287 io_message_loop_proxy()->PostTask(FROM_HERE
,
288 base::MessageLoop::QuitClosure());
289 ASSERT_EQ(fetcher_
, source
);
290 EXPECT_EQ(test_url_
, source
->GetOriginalURL());
291 completed_fetcher_
.reset(fetcher_
);
296 // Version of URLFetcherTest that tests download progress reports.
297 class URLFetcherDownloadProgressTest
: public URLFetcherTest
{
299 URLFetcherDownloadProgressTest()
300 : previous_progress_(0),
305 void CreateFetcher(const GURL
& url
) override
;
307 // URLFetcherDelegate:
308 void OnURLFetchDownloadProgress(const URLFetcher
* source
,
310 int64 total
) override
;
313 // Download progress returned by the previous callback.
314 int64 previous_progress_
;
315 // Size of the file being downloaded, known in advance (provided by each test
317 int64 expected_total_
;
320 // Version of URLFetcherTest that tests progress reports at cancellation.
321 class URLFetcherDownloadProgressCancelTest
: public URLFetcherTest
{
324 void CreateFetcher(const GURL
& url
) override
;
326 // URLFetcherDelegate:
327 void OnURLFetchComplete(const URLFetcher
* source
) override
;
328 void OnURLFetchDownloadProgress(const URLFetcher
* source
,
330 int64 total
) override
;
336 // Version of URLFetcherTest that tests upload progress reports.
337 class URLFetcherUploadProgressTest
: public URLFetcherTest
{
340 void CreateFetcher(const GURL
& url
) override
;
342 // URLFetcherDelegate:
343 void OnURLFetchUploadProgress(const URLFetcher
* source
,
345 int64 total
) override
;
348 int64 previous_progress_
;
350 int64 number_of_chunks_added_
;
353 // Version of URLFetcherTest that tests headers.
354 class URLFetcherHeadersTest
: public URLFetcherTest
{
356 // URLFetcherDelegate:
357 void OnURLFetchComplete(const URLFetcher
* source
) override
;
360 // Version of URLFetcherTest that tests SocketAddress.
361 class URLFetcherSocketAddressTest
: public URLFetcherTest
{
363 // URLFetcherDelegate:
364 void OnURLFetchComplete(const URLFetcher
* source
) override
;
367 std::string expected_host_
;
368 uint16 expected_port_
;
371 // Version of URLFetcherTest that tests stopping on a redirect.
372 class URLFetcherStopOnRedirectTest
: public URLFetcherTest
{
374 URLFetcherStopOnRedirectTest();
375 ~URLFetcherStopOnRedirectTest() override
;
378 void CreateFetcher(const GURL
& url
) override
;
380 // URLFetcherDelegate:
381 void OnURLFetchComplete(const URLFetcher
* source
) override
;
384 // The URL we should be redirected to.
385 static const char* kRedirectTarget
;
387 bool callback_called_
; // Set to true in OnURLFetchComplete().
390 // Version of URLFetcherTest that tests overload protection.
391 class URLFetcherProtectTest
: public URLFetcherTest
{
394 void CreateFetcher(const GURL
& url
) override
;
396 // URLFetcherDelegate:
397 void OnURLFetchComplete(const URLFetcher
* source
) override
;
403 // Version of URLFetcherTest that tests overload protection, when responses
405 class URLFetcherProtectTestPassedThrough
: public URLFetcherTest
{
408 void CreateFetcher(const GURL
& url
) override
;
410 // URLFetcherDelegate:
411 void OnURLFetchComplete(const URLFetcher
* source
) override
;
417 // Version of URLFetcherTest that tests bad HTTPS requests.
418 class URLFetcherBadHTTPSTest
: public URLFetcherTest
{
420 URLFetcherBadHTTPSTest();
423 void SetUpServer() override
;
425 // URLFetcherDelegate:
426 void OnURLFetchComplete(const URLFetcher
* source
) override
;
429 base::FilePath cert_dir_
;
432 // Version of URLFetcherTest that tests request cancellation on shutdown.
433 class URLFetcherCancelTest
: public URLFetcherTest
{
436 void CreateFetcher(const GURL
& url
) override
;
438 // URLFetcherDelegate:
439 void OnURLFetchComplete(const URLFetcher
* source
) override
;
441 void CancelRequest();
444 // Version of TestURLRequestContext that posts a Quit task to the IO
445 // thread once it is deleted.
446 class CancelTestURLRequestContext
: public ThrottlingTestURLRequestContext
{
448 explicit CancelTestURLRequestContext() {
452 ~CancelTestURLRequestContext() override
{
453 // The d'tor should execute in the IO thread. Post the quit task to the
455 base::MessageLoop::current()->PostTask(FROM_HERE
,
456 base::MessageLoop::QuitClosure());
460 class CancelTestURLRequestContextGetter
461 : public TestURLRequestContextGetter
{
463 CancelTestURLRequestContextGetter(
464 base::MessageLoopProxy
* io_message_loop_proxy
,
465 const GURL
& throttle_for_url
)
466 : TestURLRequestContextGetter(io_message_loop_proxy
),
467 io_message_loop_proxy_(io_message_loop_proxy
),
468 context_created_(false, false),
469 throttle_for_url_(throttle_for_url
) {
472 // TestURLRequestContextGetter:
473 TestURLRequestContext
* GetURLRequestContext() override
{
474 if (!context_
.get()) {
475 context_
.reset(new CancelTestURLRequestContext());
476 DCHECK(context_
->throttler_manager());
478 // Registers an entry for test url. The backoff time is calculated by:
479 // new_backoff = 2.0 * old_backoff + 0
480 // The initial backoff is 2 seconds and maximum backoff is 4 seconds.
481 // Maximum retries allowed is set to 2.
482 scoped_refptr
<URLRequestThrottlerEntry
> entry(
483 new URLRequestThrottlerEntry(context_
->throttler_manager(),
491 context_
->throttler_manager()
492 ->OverrideEntryForTests(throttle_for_url_
, entry
.get());
494 context_created_
.Signal();
496 return context_
.get();
499 virtual scoped_refptr
<base::MessageLoopProxy
> GetIOMessageLoopProxy() const {
500 return io_message_loop_proxy_
;
503 void WaitForContextCreation() {
504 context_created_
.Wait();
508 ~CancelTestURLRequestContextGetter() override
{}
511 scoped_ptr
<TestURLRequestContext
> context_
;
512 scoped_refptr
<base::MessageLoopProxy
> io_message_loop_proxy_
;
513 base::WaitableEvent context_created_
;
514 GURL throttle_for_url_
;
517 // Version of URLFetcherTest that tests retying the same request twice.
518 class URLFetcherMultipleAttemptTest
: public URLFetcherTest
{
520 // URLFetcherDelegate:
521 void OnURLFetchComplete(const URLFetcher
* source
) override
;
527 class URLFetcherFileTest
: public URLFetcherTest
{
529 URLFetcherFileTest() : take_ownership_of_file_(false),
530 expected_file_error_(OK
) {}
532 void CreateFetcherForFile(const GURL
& url
, const base::FilePath
& file_path
);
533 void CreateFetcherForTempFile(const GURL
& url
);
535 // URLFetcherDelegate:
536 void OnURLFetchComplete(const URLFetcher
* source
) override
;
539 base::FilePath expected_file_
;
540 base::FilePath file_path_
;
542 // Set by the test. Used in OnURLFetchComplete() to decide if
543 // the URLFetcher should own the temp file, so that we can test
544 // disowning prevents the file from being deleted.
545 bool take_ownership_of_file_
;
547 // Expected file error code for the test. OK when expecting success.
548 int expected_file_error_
;
551 void URLFetcherDownloadProgressTest::CreateFetcher(const GURL
& url
) {
552 fetcher_
= new URLFetcherImpl(url
, URLFetcher::GET
, this);
553 fetcher_
->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
554 io_message_loop_proxy().get(), request_context()));
558 void URLFetcherDownloadProgressTest::OnURLFetchDownloadProgress(
559 const URLFetcher
* source
, int64 progress
, int64 total
) {
560 // Increasing between 0 and total.
561 EXPECT_LE(0, progress
);
562 EXPECT_GE(total
, progress
);
563 EXPECT_LE(previous_progress_
, progress
);
564 EXPECT_EQ(expected_total_
, total
);
565 previous_progress_
= progress
;
568 void URLFetcherDownloadProgressCancelTest::CreateFetcher(const GURL
& url
) {
569 fetcher_
= new URLFetcherImpl(url
, URLFetcher::GET
, this);
570 fetcher_
->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
571 io_message_loop_proxy().get(), request_context()));
576 void URLFetcherDownloadProgressCancelTest::OnURLFetchDownloadProgress(
577 const URLFetcher
* source
, int64 current
, int64 total
) {
578 EXPECT_FALSE(cancelled_
);
581 CleanupAfterFetchComplete();
585 void URLFetcherDownloadProgressCancelTest::OnURLFetchComplete(
586 const URLFetcher
* source
) {
587 // Should have been cancelled.
589 CleanupAfterFetchComplete();
592 void URLFetcherUploadProgressTest::CreateFetcher(const GURL
& url
) {
593 fetcher_
= new URLFetcherImpl(url
, URLFetcher::POST
, this);
594 fetcher_
->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
595 io_message_loop_proxy().get(), request_context()));
596 previous_progress_
= 0;
597 // Large enough data to require more than one read from UploadDataStream.
598 chunk_
.assign(1<<16, 'a');
599 // Use chunked upload to wait for a timer event of progress notification.
600 fetcher_
->SetChunkedUpload("application/x-www-form-urlencoded");
602 number_of_chunks_added_
= 1;
603 fetcher_
->AppendChunkToUpload(chunk_
, false);
606 void URLFetcherUploadProgressTest::OnURLFetchUploadProgress(
607 const URLFetcher
* source
, int64 current
, int64 total
) {
608 // Increasing between 0 and total.
609 EXPECT_LE(0, current
);
610 EXPECT_GE(static_cast<int64
>(chunk_
.size()) * number_of_chunks_added_
,
612 EXPECT_LE(previous_progress_
, current
);
613 previous_progress_
= current
;
614 EXPECT_EQ(-1, total
);
616 if (number_of_chunks_added_
< 2) {
617 number_of_chunks_added_
+= 1;
618 fetcher_
->AppendChunkToUpload(chunk_
, true);
622 void URLFetcherHeadersTest::OnURLFetchComplete(
623 const URLFetcher
* source
) {
625 EXPECT_TRUE(source
->GetResponseHeaders()->GetNormalizedHeader("cache-control",
627 EXPECT_EQ("private", header
);
628 URLFetcherTest::OnURLFetchComplete(source
);
631 void URLFetcherSocketAddressTest::OnURLFetchComplete(
632 const URLFetcher
* source
) {
633 EXPECT_EQ("127.0.0.1", source
->GetSocketAddress().host());
634 EXPECT_EQ(expected_port_
, source
->GetSocketAddress().port());
635 URLFetcherTest::OnURLFetchComplete(source
);
639 const char* URLFetcherStopOnRedirectTest::kRedirectTarget
=
640 "http://redirect.target.com";
642 URLFetcherStopOnRedirectTest::URLFetcherStopOnRedirectTest()
643 : callback_called_(false) {
646 URLFetcherStopOnRedirectTest::~URLFetcherStopOnRedirectTest() {
649 void URLFetcherStopOnRedirectTest::CreateFetcher(const GURL
& url
) {
650 fetcher_
= new URLFetcherImpl(url
, URLFetcher::GET
, this);
651 fetcher_
->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
652 io_message_loop_proxy().get(), request_context()));
653 fetcher_
->SetStopOnRedirect(true);
657 void URLFetcherStopOnRedirectTest::OnURLFetchComplete(
658 const URLFetcher
* source
) {
659 callback_called_
= true;
660 EXPECT_EQ(GURL(kRedirectTarget
), source
->GetURL());
661 EXPECT_EQ(URLRequestStatus::CANCELED
, source
->GetStatus().status());
662 EXPECT_EQ(ERR_ABORTED
, source
->GetStatus().error());
663 EXPECT_EQ(301, source
->GetResponseCode());
664 CleanupAfterFetchComplete();
667 void URLFetcherProtectTest::CreateFetcher(const GURL
& url
) {
668 fetcher_
= new URLFetcherImpl(url
, URLFetcher::GET
, this);
669 fetcher_
->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
670 io_message_loop_proxy().get(), request_context()));
671 start_time_
= Time::Now();
672 fetcher_
->SetMaxRetriesOn5xx(11);
676 void URLFetcherProtectTest::OnURLFetchComplete(const URLFetcher
* source
) {
677 const TimeDelta one_second
= TimeDelta::FromMilliseconds(1000);
678 if (source
->GetResponseCode() >= 500) {
679 // Now running ServerUnavailable test.
680 // It takes more than 1 second to finish all 11 requests.
681 EXPECT_TRUE(Time::Now() - start_time_
>= one_second
);
682 EXPECT_TRUE(source
->GetStatus().is_success());
684 EXPECT_TRUE(source
->GetResponseAsString(&data
));
685 EXPECT_FALSE(data
.empty());
686 CleanupAfterFetchComplete();
688 // Now running Overload test.
689 static int count
= 0;
692 fetcher_
->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
693 io_message_loop_proxy().get(), request_context()));
696 // We have already sent 20 requests continuously. And we expect that
697 // it takes more than 1 second due to the overload protection settings.
698 EXPECT_TRUE(Time::Now() - start_time_
>= one_second
);
699 URLFetcherTest::OnURLFetchComplete(source
);
704 void URLFetcherProtectTestPassedThrough::CreateFetcher(const GURL
& url
) {
705 fetcher_
= new URLFetcherImpl(url
, URLFetcher::GET
, this);
706 fetcher_
->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
707 io_message_loop_proxy().get(), request_context()));
708 fetcher_
->SetAutomaticallyRetryOn5xx(false);
709 start_time_
= Time::Now();
710 fetcher_
->SetMaxRetriesOn5xx(11);
714 void URLFetcherProtectTestPassedThrough::OnURLFetchComplete(
715 const URLFetcher
* source
) {
716 const TimeDelta one_minute
= TimeDelta::FromMilliseconds(60000);
717 if (source
->GetResponseCode() >= 500) {
718 // Now running ServerUnavailable test.
719 // It should get here on the first attempt, so almost immediately and
720 // *not* to attempt to execute all 11 requests (2.5 minutes).
721 EXPECT_TRUE(Time::Now() - start_time_
< one_minute
);
722 EXPECT_TRUE(source
->GetStatus().is_success());
723 // Check that suggested back off time is bigger than 0.
724 EXPECT_GT(fetcher_
->GetBackoffDelay().InMicroseconds(), 0);
726 EXPECT_TRUE(source
->GetResponseAsString(&data
));
727 EXPECT_FALSE(data
.empty());
729 // We should not get here!
733 CleanupAfterFetchComplete();
737 URLFetcherBadHTTPSTest::URLFetcherBadHTTPSTest() {
738 PathService::Get(base::DIR_SOURCE_ROOT
, &cert_dir_
);
739 cert_dir_
= cert_dir_
.AppendASCII("chrome");
740 cert_dir_
= cert_dir_
.AppendASCII("test");
741 cert_dir_
= cert_dir_
.AppendASCII("data");
742 cert_dir_
= cert_dir_
.AppendASCII("ssl");
743 cert_dir_
= cert_dir_
.AppendASCII("certificates");
746 void URLFetcherBadHTTPSTest::SetUpServer() {
747 SpawnedTestServer::SSLOptions
ssl_options(
748 SpawnedTestServer::SSLOptions::CERT_EXPIRED
);
749 test_server_
.reset(new SpawnedTestServer(
750 SpawnedTestServer::TYPE_HTTPS
, ssl_options
, base::FilePath(kDocRoot
)));
753 // The "server certificate expired" error should result in automatic
754 // cancellation of the request by
755 // URLRequest::Delegate::OnSSLCertificateError.
756 void URLFetcherBadHTTPSTest::OnURLFetchComplete(
757 const URLFetcher
* source
) {
758 // This part is different from URLFetcherTest::OnURLFetchComplete
759 // because this test expects the request to be cancelled.
760 EXPECT_EQ(URLRequestStatus::CANCELED
, source
->GetStatus().status());
761 EXPECT_EQ(ERR_ABORTED
, source
->GetStatus().error());
762 EXPECT_EQ(-1, source
->GetResponseCode());
763 EXPECT_TRUE(source
->GetCookies().empty());
765 EXPECT_TRUE(source
->GetResponseAsString(&data
));
766 EXPECT_TRUE(data
.empty());
767 CleanupAfterFetchComplete();
770 void URLFetcherCancelTest::CreateFetcher(const GURL
& url
) {
771 fetcher_
= new URLFetcherImpl(url
, URLFetcher::GET
, this);
772 CancelTestURLRequestContextGetter
* context_getter
=
773 new CancelTestURLRequestContextGetter(io_message_loop_proxy().get(), url
);
774 fetcher_
->SetRequestContext(context_getter
);
775 fetcher_
->SetMaxRetriesOn5xx(2);
777 // We need to wait for the creation of the URLRequestContext, since we
778 // rely on it being destroyed as a signal to end the test.
779 context_getter
->WaitForContextCreation();
783 void URLFetcherCancelTest::OnURLFetchComplete(
784 const URLFetcher
* source
) {
785 // We should have cancelled the request before completion.
787 CleanupAfterFetchComplete();
790 void URLFetcherCancelTest::CancelRequest() {
792 // The URLFetcher's test context will post a Quit task once it is
793 // deleted. So if this test simply hangs, it means cancellation
797 void URLFetcherMultipleAttemptTest::OnURLFetchComplete(
798 const URLFetcher
* source
) {
799 EXPECT_TRUE(source
->GetStatus().is_success());
800 EXPECT_EQ(200, source
->GetResponseCode()); // HTTP OK
802 EXPECT_TRUE(source
->GetResponseAsString(&data
));
803 EXPECT_FALSE(data
.empty());
804 if (!data
.empty() && data_
.empty()) {
806 fetcher_
->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
807 io_message_loop_proxy().get(), request_context()));
810 EXPECT_EQ(data
, data_
);
811 CleanupAfterFetchComplete();
815 void URLFetcherFileTest::CreateFetcherForFile(const GURL
& url
,
816 const base::FilePath
& file_path
) {
817 fetcher_
= new URLFetcherImpl(url
, URLFetcher::GET
, this);
818 fetcher_
->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
819 io_message_loop_proxy().get(), request_context()));
821 // Use the IO message loop to do the file operations in this test.
822 fetcher_
->SaveResponseToFileAtPath(file_path
, io_message_loop_proxy());
826 void URLFetcherFileTest::CreateFetcherForTempFile(const GURL
& url
) {
827 fetcher_
= new URLFetcherImpl(url
, URLFetcher::GET
, this);
828 fetcher_
->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
829 io_message_loop_proxy().get(), request_context()));
831 // Use the IO message loop to do the file operations in this test.
832 fetcher_
->SaveResponseToTemporaryFile(io_message_loop_proxy());
836 void URLFetcherFileTest::OnURLFetchComplete(const URLFetcher
* source
) {
837 if (expected_file_error_
== OK
) {
838 EXPECT_TRUE(source
->GetStatus().is_success());
839 EXPECT_EQ(OK
, source
->GetStatus().error());
840 EXPECT_EQ(200, source
->GetResponseCode());
842 EXPECT_TRUE(source
->GetResponseAsFilePath(
843 take_ownership_of_file_
, &file_path_
));
845 EXPECT_TRUE(base::ContentsEqual(expected_file_
, file_path_
));
847 EXPECT_FALSE(source
->GetStatus().is_success());
848 EXPECT_EQ(expected_file_error_
, source
->GetStatus().error());
850 CleanupAfterFetchComplete();
853 TEST_F(URLFetcherTest
, SameThreadsTest
) {
854 // Create the fetcher on the main thread. Since IO will happen on the main
855 // thread, this will test URLFetcher's ability to do everything on one
857 CreateFetcher(test_server_
->GetURL("defaultresponse"));
859 base::MessageLoop::current()->Run();
862 TEST_F(URLFetcherTest
, DifferentThreadsTest
) {
863 // Create a separate thread that will create the URLFetcher. The current
864 // (main) thread will do the IO, and when the fetch is complete it will
865 // terminate the main thread's message loop; then the other thread's
866 // message loop will be shut down automatically as the thread goes out of
868 base::Thread
t("URLFetcher test thread");
869 ASSERT_TRUE(t
.Start());
870 t
.message_loop()->PostTask(
872 base::Bind(&URLFetcherTest::CreateFetcher
, base::Unretained(this),
873 test_server_
->GetURL("defaultresponse")));
875 base::MessageLoop::current()->Run();
878 void CancelAllOnIO() {
879 EXPECT_EQ(1, URLFetcherTest::GetNumFetcherCores());
880 URLFetcherImpl::CancelAll();
881 EXPECT_EQ(0, URLFetcherTest::GetNumFetcherCores());
884 // Tests to make sure CancelAll() will successfully cancel existing URLFetchers.
885 TEST_F(URLFetcherTest
, CancelAll
) {
886 EXPECT_EQ(0, GetNumFetcherCores());
888 CreateFetcher(test_server_
->GetURL("defaultresponse"));
889 io_message_loop_proxy()->PostTaskAndReply(
890 FROM_HERE
, base::Bind(&CancelAllOnIO
), base::MessageLoop::QuitClosure());
891 base::MessageLoop::current()->Run();
892 EXPECT_EQ(0, GetNumFetcherCores());
896 TEST_F(URLFetcherMockDnsTest
, DontRetryOnNetworkChangedByDefault
) {
897 EXPECT_EQ(0, GetNumFetcherCores());
898 EXPECT_FALSE(resolver_
.has_pending_requests());
900 // This posts a task to start the fetcher.
901 CreateFetcher(test_url_
);
903 EXPECT_EQ(0, GetNumFetcherCores());
904 base::MessageLoop::current()->RunUntilIdle();
906 // The fetcher is now running, but is pending the host resolve.
907 EXPECT_EQ(1, GetNumFetcherCores());
908 EXPECT_TRUE(resolver_
.has_pending_requests());
909 ASSERT_FALSE(completed_fetcher_
);
911 // A network change notification aborts the connect job.
912 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
913 base::MessageLoop::current()->RunUntilIdle();
914 EXPECT_EQ(0, GetNumFetcherCores());
915 EXPECT_FALSE(resolver_
.has_pending_requests());
916 ASSERT_TRUE(completed_fetcher_
);
918 // And the owner of the fetcher gets the ERR_NETWORK_CHANGED error.
919 EXPECT_EQ(ERR_NETWORK_CHANGED
, completed_fetcher_
->GetStatus().error());
922 TEST_F(URLFetcherMockDnsTest
, RetryOnNetworkChangedAndFail
) {
923 EXPECT_EQ(0, GetNumFetcherCores());
924 EXPECT_FALSE(resolver_
.has_pending_requests());
926 // This posts a task to start the fetcher.
927 CreateFetcher(test_url_
);
928 fetcher_
->SetAutomaticallyRetryOnNetworkChanges(3);
930 EXPECT_EQ(0, GetNumFetcherCores());
931 base::MessageLoop::current()->RunUntilIdle();
933 // The fetcher is now running, but is pending the host resolve.
934 EXPECT_EQ(1, GetNumFetcherCores());
935 EXPECT_TRUE(resolver_
.has_pending_requests());
936 ASSERT_FALSE(completed_fetcher_
);
938 // Make it fail 3 times.
939 for (int i
= 0; i
< 3; ++i
) {
940 // A network change notification aborts the connect job.
941 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
942 base::MessageLoop::current()->RunUntilIdle();
944 // But the fetcher retries automatically.
945 EXPECT_EQ(1, GetNumFetcherCores());
946 EXPECT_TRUE(resolver_
.has_pending_requests());
947 ASSERT_FALSE(completed_fetcher_
);
950 // A 4th failure doesn't trigger another retry, and propagates the error
951 // to the owner of the fetcher.
952 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
953 base::MessageLoop::current()->RunUntilIdle();
954 EXPECT_EQ(0, GetNumFetcherCores());
955 EXPECT_FALSE(resolver_
.has_pending_requests());
956 ASSERT_TRUE(completed_fetcher_
);
958 // And the owner of the fetcher gets the ERR_NETWORK_CHANGED error.
959 EXPECT_EQ(ERR_NETWORK_CHANGED
, completed_fetcher_
->GetStatus().error());
962 TEST_F(URLFetcherMockDnsTest
, RetryOnNetworkChangedAndSucceed
) {
963 EXPECT_EQ(0, GetNumFetcherCores());
964 EXPECT_FALSE(resolver_
.has_pending_requests());
966 // This posts a task to start the fetcher.
967 CreateFetcher(test_url_
);
968 fetcher_
->SetAutomaticallyRetryOnNetworkChanges(3);
970 EXPECT_EQ(0, GetNumFetcherCores());
971 base::MessageLoop::current()->RunUntilIdle();
973 // The fetcher is now running, but is pending the host resolve.
974 EXPECT_EQ(1, GetNumFetcherCores());
975 EXPECT_TRUE(resolver_
.has_pending_requests());
976 ASSERT_FALSE(completed_fetcher_
);
978 // Make it fail 3 times.
979 for (int i
= 0; i
< 3; ++i
) {
980 // A network change notification aborts the connect job.
981 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
982 base::MessageLoop::current()->RunUntilIdle();
984 // But the fetcher retries automatically.
985 EXPECT_EQ(1, GetNumFetcherCores());
986 EXPECT_TRUE(resolver_
.has_pending_requests());
987 ASSERT_FALSE(completed_fetcher_
);
990 // Now let it succeed by resolving the pending request.
991 resolver_
.ResolveAllPending();
992 base::MessageLoop::current()->Run();
994 // URLFetcherMockDnsTest::OnURLFetchComplete() will quit the loop.
995 EXPECT_EQ(0, GetNumFetcherCores());
996 EXPECT_FALSE(resolver_
.has_pending_requests());
997 ASSERT_TRUE(completed_fetcher_
);
999 // This time the request succeeded.
1000 EXPECT_EQ(OK
, completed_fetcher_
->GetStatus().error());
1001 EXPECT_EQ(200, completed_fetcher_
->GetResponseCode());
1004 TEST_F(URLFetcherTest
, PostString
) {
1005 const char kUploadData
[] = "bobsyeruncle";
1007 WaitingURLFetcherDelegate delegate
;
1008 URLFetcherImpl
fetcher(test_server_
->GetURL("echo"), URLFetcher::POST
,
1010 fetcher
.SetRequestContext(new TrivialURLRequestContextGetter(
1011 request_context(), base::MessageLoopProxy::current()));
1012 fetcher
.SetUploadData("application/x-www-form-urlencoded", kUploadData
);
1013 delegate
.StartFetcherAndWait(&fetcher
);
1015 EXPECT_TRUE(fetcher
.GetStatus().is_success());
1016 EXPECT_EQ(200, fetcher
.GetResponseCode());
1018 EXPECT_TRUE(fetcher
.GetResponseAsString(&data
));
1019 EXPECT_EQ(kUploadData
, data
);
1022 TEST_F(URLFetcherTest
, PostEmptyString
) {
1023 const char kUploadData
[] = "";
1025 WaitingURLFetcherDelegate delegate
;
1026 URLFetcherImpl
fetcher(test_server_
->GetURL("echo"), URLFetcher::POST
,
1028 fetcher
.SetRequestContext(new TrivialURLRequestContextGetter(
1029 request_context(), base::MessageLoopProxy::current()));
1030 fetcher
.SetUploadData("application/x-www-form-urlencoded", kUploadData
);
1031 delegate
.StartFetcherAndWait(&fetcher
);
1033 EXPECT_TRUE(fetcher
.GetStatus().is_success());
1034 EXPECT_EQ(200, fetcher
.GetResponseCode());
1036 EXPECT_TRUE(fetcher
.GetResponseAsString(&data
));
1037 EXPECT_EQ(kUploadData
, data
);
1040 TEST_F(URLFetcherTest
, PostEntireFile
) {
1041 base::FilePath upload_path
= GetUploadFileTestPath();
1043 WaitingURLFetcherDelegate delegate
;
1044 URLFetcherImpl
fetcher(test_server_
->GetURL("echo"), URLFetcher::POST
, this);
1045 fetcher
.SetRequestContext(new TrivialURLRequestContextGetter(
1046 request_context(), base::MessageLoopProxy::current()));
1047 fetcher
.SetUploadFilePath("application/x-www-form-urlencoded", upload_path
, 0,
1048 kuint64max
, base::MessageLoopProxy::current());
1049 delegate
.StartFetcherAndWait(&fetcher
);
1051 EXPECT_TRUE(fetcher
.GetStatus().is_success());
1052 EXPECT_EQ(200, fetcher
.GetResponseCode());
1054 std::string expected
;
1055 ASSERT_TRUE(base::ReadFileToString(upload_path
, &expected
));
1057 EXPECT_TRUE(fetcher
.GetResponseAsString(&data
));
1058 EXPECT_EQ(expected
, data
);
1061 TEST_F(URLFetcherTest
, PostFileRange
) {
1062 const size_t kRangeStart
= 30;
1063 const size_t kRangeLength
= 100;
1064 base::FilePath upload_path
= GetUploadFileTestPath();
1066 WaitingURLFetcherDelegate delegate
;
1067 URLFetcherImpl
fetcher(test_server_
->GetURL("echo"), URLFetcher::POST
, this);
1068 fetcher
.SetRequestContext(new TrivialURLRequestContextGetter(
1069 request_context(), base::MessageLoopProxy::current()));
1070 fetcher
.SetUploadFilePath("application/x-www-form-urlencoded", upload_path
,
1071 kRangeStart
, kRangeLength
,
1072 base::MessageLoopProxy::current());
1073 delegate
.StartFetcherAndWait(&fetcher
);
1075 EXPECT_TRUE(fetcher
.GetStatus().is_success());
1076 EXPECT_EQ(200, fetcher
.GetResponseCode());
1078 std::string expected
;
1079 ASSERT_TRUE(base::ReadFileToString(upload_path
, &expected
));
1081 EXPECT_TRUE(fetcher
.GetResponseAsString(&data
));
1082 EXPECT_EQ(expected
.substr(kRangeStart
, kRangeLength
), data
);
1085 TEST_F(URLFetcherTest
, PostWithUploadStreamFactory
) {
1086 WaitingURLFetcherDelegate delegate
;
1087 URLFetcherImpl
fetcher(test_server_
->GetURL("echo"), URLFetcher::POST
,
1089 fetcher
.SetRequestContext(new TrivialURLRequestContextGetter(
1090 request_context(), base::MessageLoopProxy::current()));
1091 fetcher
.SetUploadStreamFactory(
1093 base::Bind(&URLFetcherTest::CreateUploadStream
, base::Unretained(this)));
1094 delegate
.StartFetcherAndWait(&fetcher
);
1096 EXPECT_TRUE(fetcher
.GetStatus().is_success());
1097 EXPECT_EQ(200, fetcher
.GetResponseCode());
1099 EXPECT_TRUE(fetcher
.GetResponseAsString(&data
));
1100 EXPECT_EQ(kCreateUploadStreamBody
, data
);
1101 EXPECT_EQ(1u, num_upload_streams_created());
1104 TEST_F(URLFetcherTest
, PostWithUploadStreamFactoryAndRetries
) {
1105 WaitingURLFetcherDelegate delegate
;
1106 URLFetcherImpl
fetcher(test_server_
->GetURL("echo?status=500"),
1107 URLFetcher::POST
, &delegate
);
1108 fetcher
.SetRequestContext(new TrivialURLRequestContextGetter(
1109 request_context(), base::MessageLoopProxy::current()));
1110 fetcher
.SetAutomaticallyRetryOn5xx(true);
1111 fetcher
.SetMaxRetriesOn5xx(1);
1112 fetcher
.SetUploadStreamFactory(
1114 base::Bind(&URLFetcherTest::CreateUploadStream
, base::Unretained(this)));
1115 delegate
.StartFetcherAndWait(&fetcher
);
1117 EXPECT_TRUE(fetcher
.GetStatus().is_success());
1118 EXPECT_EQ(500, fetcher
.GetResponseCode());
1120 EXPECT_TRUE(fetcher
.GetResponseAsString(&data
));
1121 EXPECT_EQ(kCreateUploadStreamBody
, data
);
1122 EXPECT_EQ(2u, num_upload_streams_created());
1125 TEST_F(URLFetcherUploadProgressTest
, Basic
) {
1126 CreateFetcher(test_server_
->GetURL("echo"));
1127 base::MessageLoop::current()->Run();
1130 TEST_F(URLFetcherDownloadProgressTest
, Basic
) {
1131 // Get a file large enough to require more than one read into
1132 // URLFetcher::Core's IOBuffer.
1133 static const char kFileToFetch
[] = "animate1.gif";
1134 // Hardcoded file size - it cannot be easily fetched when a remote http server
1135 // is used for testing.
1136 static const int64 kFileSize
= 19021;
1138 expected_total_
= kFileSize
;
1141 test_server_
->GetURL(std::string(kTestServerFilePrefix
) + kFileToFetch
));
1143 base::MessageLoop::current()->Run();
1146 TEST_F(URLFetcherDownloadProgressCancelTest
, CancelWhileProgressReport
) {
1147 // Get a file large enough to require more than one read into
1148 // URLFetcher::Core's IOBuffer.
1149 static const char kFileToFetch
[] = "animate1.gif";
1151 test_server_
->GetURL(std::string(kTestServerFilePrefix
) + kFileToFetch
));
1153 base::MessageLoop::current()->Run();
1156 TEST_F(URLFetcherHeadersTest
, Headers
) {
1157 CreateFetcher(test_server_
->GetURL("set-header?cache-control: private"));
1158 base::MessageLoop::current()->Run();
1159 // The actual tests are in the URLFetcherHeadersTest fixture.
1162 TEST_F(URLFetcherSocketAddressTest
, SocketAddress
) {
1163 expected_port_
= test_server_
->host_port_pair().port();
1165 CreateFetcher(test_server_
->GetURL("defaultresponse"));
1166 base::MessageLoop::current()->Run();
1167 // The actual tests are in the URLFetcherSocketAddressTest fixture.
1170 TEST_F(URLFetcherStopOnRedirectTest
, StopOnRedirect
) {
1172 test_server_
->GetURL(std::string("server-redirect?") + kRedirectTarget
));
1173 base::MessageLoop::current()->Run();
1174 EXPECT_TRUE(callback_called_
);
1177 TEST_F(URLFetcherProtectTest
, Overload
) {
1178 GURL
url(test_server_
->GetURL("defaultresponse"));
1180 // Registers an entry for test url. It only allows 3 requests to be sent
1181 // in 200 milliseconds.
1182 scoped_refptr
<URLRequestThrottlerEntry
> entry(
1183 new URLRequestThrottlerEntry(request_context()->throttler_manager(),
1191 request_context()->throttler_manager()
1192 ->OverrideEntryForTests(url
, entry
.get());
1196 base::MessageLoop::current()->Run();
1199 TEST_F(URLFetcherProtectTest
, ServerUnavailable
) {
1200 GURL
url(test_server_
->GetURL("files/server-unavailable.html"));
1202 // Registers an entry for test url. The backoff time is calculated by:
1203 // new_backoff = 2.0 * old_backoff + 0
1204 // and maximum backoff time is 256 milliseconds.
1205 // Maximum retries allowed is set to 11.
1206 scoped_refptr
<URLRequestThrottlerEntry
> entry(
1207 new URLRequestThrottlerEntry(request_context()->throttler_manager(),
1215 request_context()->throttler_manager()
1216 ->OverrideEntryForTests(url
, entry
.get());
1220 base::MessageLoop::current()->Run();
1223 TEST_F(URLFetcherProtectTestPassedThrough
, ServerUnavailablePropagateResponse
) {
1224 GURL
url(test_server_
->GetURL("files/server-unavailable.html"));
1226 // Registers an entry for test url. The backoff time is calculated by:
1227 // new_backoff = 2.0 * old_backoff + 0
1228 // and maximum backoff time is 150000 milliseconds.
1229 // Maximum retries allowed is set to 11.
1230 scoped_refptr
<URLRequestThrottlerEntry
> entry(
1231 new URLRequestThrottlerEntry(request_context()->throttler_manager(),
1239 // Total time if *not* for not doing automatic backoff would be 150s.
1240 // In reality it should be "as soon as server responds".
1241 request_context()->throttler_manager()
1242 ->OverrideEntryForTests(url
, entry
.get());
1246 base::MessageLoop::current()->Run();
1249 TEST_F(URLFetcherBadHTTPSTest
, BadHTTPSTest
) {
1250 CreateFetcher(test_server_
->GetURL("defaultresponse"));
1251 base::MessageLoop::current()->Run();
1254 TEST_F(URLFetcherCancelTest
, ReleasesContext
) {
1255 GURL
url(test_server_
->GetURL("files/server-unavailable.html"));
1257 // Create a separate thread that will create the URLFetcher. The current
1258 // (main) thread will do the IO, and when the fetch is complete it will
1259 // terminate the main thread's message loop; then the other thread's
1260 // message loop will be shut down automatically as the thread goes out of
1262 base::Thread
t("URLFetcher test thread");
1263 ASSERT_TRUE(t
.Start());
1264 t
.message_loop()->PostTask(
1266 base::Bind(&URLFetcherCancelTest::CreateFetcher
,
1267 base::Unretained(this), url
));
1269 base::MessageLoop::current()->Run();
1272 TEST_F(URLFetcherCancelTest
, CancelWhileDelayedStartTaskPending
) {
1273 GURL
url(test_server_
->GetURL("files/server-unavailable.html"));
1275 // Register an entry for test url.
1276 // Using a sliding window of 4 seconds, and max of 1 request, under a fast
1277 // run we expect to have a 4 second delay when posting the Start task.
1278 scoped_refptr
<URLRequestThrottlerEntry
> entry(
1279 new URLRequestThrottlerEntry(request_context()->throttler_manager(),
1287 request_context()->throttler_manager()
1288 ->OverrideEntryForTests(url
, entry
.get());
1289 // Fake that a request has just started.
1290 entry
->ReserveSendingTimeForNextRequest(base::TimeTicks());
1292 // The next request we try to send will be delayed by ~4 seconds.
1293 // The slower the test runs, the less the delay will be (since it takes the
1294 // time difference from now).
1296 base::Thread
t("URLFetcher test thread");
1297 ASSERT_TRUE(t
.Start());
1298 t
.message_loop()->PostTask(
1300 base::Bind(&URLFetcherTest::CreateFetcher
, base::Unretained(this), url
));
1302 base::MessageLoop::current()->Run();
1305 TEST_F(URLFetcherMultipleAttemptTest
, SameData
) {
1306 // Create the fetcher on the main thread. Since IO will happen on the main
1307 // thread, this will test URLFetcher's ability to do everything on one
1309 CreateFetcher(test_server_
->GetURL("defaultresponse"));
1311 base::MessageLoop::current()->Run();
1314 TEST_F(URLFetcherFileTest
, SmallGet
) {
1315 base::ScopedTempDir temp_dir
;
1316 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
1318 // Get a small file.
1319 static const char kFileToFetch
[] = "simple.html";
1320 expected_file_
= test_server_
->GetDocumentRoot().AppendASCII(kFileToFetch
);
1321 CreateFetcherForFile(
1322 test_server_
->GetURL(std::string(kTestServerFilePrefix
) + kFileToFetch
),
1323 temp_dir
.path().AppendASCII(kFileToFetch
));
1325 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit().
1327 ASSERT_FALSE(base::PathExists(file_path_
))
1328 << file_path_
.value() << " not removed.";
1331 TEST_F(URLFetcherFileTest
, LargeGet
) {
1332 base::ScopedTempDir temp_dir
;
1333 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
1335 // Get a file large enough to require more than one read into
1336 // URLFetcher::Core's IOBuffer.
1337 static const char kFileToFetch
[] = "animate1.gif";
1338 expected_file_
= test_server_
->GetDocumentRoot().AppendASCII(kFileToFetch
);
1339 CreateFetcherForFile(
1340 test_server_
->GetURL(std::string(kTestServerFilePrefix
) + kFileToFetch
),
1341 temp_dir
.path().AppendASCII(kFileToFetch
));
1343 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit().
1346 TEST_F(URLFetcherFileTest
, SavedOutputFileOwnerhisp
) {
1347 // If the caller takes the ownership of the output file, the file should
1348 // persist even after URLFetcher is gone. If not, the file must be deleted.
1349 const bool kTake
[] = {false, true};
1350 for (size_t i
= 0; i
< arraysize(kTake
); ++i
) {
1351 take_ownership_of_file_
= kTake
[i
];
1352 base::ScopedTempDir temp_dir
;
1353 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
1355 // Get a small file.
1356 static const char kFileToFetch
[] = "simple.html";
1357 expected_file_
= test_server_
->GetDocumentRoot().AppendASCII(kFileToFetch
);
1358 CreateFetcherForFile(
1359 test_server_
->GetURL(std::string(kTestServerFilePrefix
) + kFileToFetch
),
1360 temp_dir
.path().AppendASCII(kFileToFetch
));
1362 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit().
1364 base::MessageLoop::current()->RunUntilIdle();
1365 ASSERT_EQ(kTake
[i
], base::PathExists(file_path_
)) <<
1366 "FilePath: " << file_path_
.value();
1370 TEST_F(URLFetcherFileTest
, OverwriteExistingFile
) {
1371 base::ScopedTempDir temp_dir
;
1372 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
1374 // Create a file before trying to fetch.
1375 static const char kFileToFetch
[] = "simple.html";
1376 std::string
data(10000, '?'); // Meant to be larger than simple.html.
1377 file_path_
= temp_dir
.path().AppendASCII(kFileToFetch
);
1378 ASSERT_EQ(static_cast<int>(data
.size()),
1379 base::WriteFile(file_path_
, data
.data(), data
.size()));
1380 ASSERT_TRUE(base::PathExists(file_path_
));
1381 expected_file_
= test_server_
->GetDocumentRoot().AppendASCII(kFileToFetch
);
1382 ASSERT_FALSE(base::ContentsEqual(file_path_
, expected_file_
));
1384 // Get a small file.
1385 CreateFetcherForFile(
1386 test_server_
->GetURL(std::string(kTestServerFilePrefix
) + kFileToFetch
),
1389 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit().
1392 TEST_F(URLFetcherFileTest
, TryToOverwriteDirectory
) {
1393 base::ScopedTempDir temp_dir
;
1394 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
1396 // Create a directory before trying to fetch.
1397 static const char kFileToFetch
[] = "simple.html";
1398 file_path_
= temp_dir
.path().AppendASCII(kFileToFetch
);
1399 ASSERT_TRUE(base::CreateDirectory(file_path_
));
1400 ASSERT_TRUE(base::PathExists(file_path_
));
1402 // Get a small file.
1403 expected_file_error_
= ERR_ACCESS_DENIED
;
1404 expected_file_
= test_server_
->GetDocumentRoot().AppendASCII(kFileToFetch
);
1405 CreateFetcherForFile(
1406 test_server_
->GetURL(std::string(kTestServerFilePrefix
) + kFileToFetch
),
1409 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit().
1411 base::MessageLoop::current()->RunUntilIdle();
1414 TEST_F(URLFetcherFileTest
, SmallGetToTempFile
) {
1415 // Get a small file.
1416 static const char kFileToFetch
[] = "simple.html";
1417 expected_file_
= test_server_
->GetDocumentRoot().AppendASCII(kFileToFetch
);
1418 CreateFetcherForTempFile(
1419 test_server_
->GetURL(std::string(kTestServerFilePrefix
) + kFileToFetch
));
1421 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit().
1423 ASSERT_FALSE(base::PathExists(file_path_
))
1424 << file_path_
.value() << " not removed.";
1427 TEST_F(URLFetcherFileTest
, LargeGetToTempFile
) {
1428 // Get a file large enough to require more than one read into
1429 // URLFetcher::Core's IOBuffer.
1430 static const char kFileToFetch
[] = "animate1.gif";
1431 expected_file_
= test_server_
->GetDocumentRoot().AppendASCII(kFileToFetch
);
1432 CreateFetcherForTempFile(
1433 test_server_
->GetURL(std::string(kTestServerFilePrefix
) + kFileToFetch
));
1435 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit().
1438 TEST_F(URLFetcherFileTest
, SavedOutputTempFileOwnerhisp
) {
1439 // If the caller takes the ownership of the temp file, the file should persist
1440 // even after URLFetcher is gone. If not, the file must be deleted.
1441 const bool kTake
[] = {false, true};
1442 for (size_t i
= 0; i
< arraysize(kTake
); ++i
) {
1443 take_ownership_of_file_
= kTake
[i
];
1445 // Get a small file.
1446 static const char kFileToFetch
[] = "simple.html";
1447 expected_file_
= test_server_
->GetDocumentRoot().AppendASCII(kFileToFetch
);
1448 CreateFetcherForTempFile(test_server_
->GetURL(
1449 std::string(kTestServerFilePrefix
) + kFileToFetch
));
1451 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit().
1453 base::MessageLoop::current()->RunUntilIdle();
1454 ASSERT_EQ(kTake
[i
], base::PathExists(file_path_
)) <<
1455 "FilePath: " << file_path_
.value();