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/file_util.h"
12 #include "base/files/scoped_temp_dir.h"
13 #include "base/message_loop/message_loop_proxy.h"
14 #include "base/strings/stringprintf.h"
15 #include "base/synchronization/waitable_event.h"
16 #include "base/threading/thread.h"
17 #include "build/build_config.h"
18 #include "crypto/nss_util.h"
19 #include "net/base/network_change_notifier.h"
20 #include "net/dns/mock_host_resolver.h"
21 #include "net/http/http_response_headers.h"
22 #include "net/test/spawned_test_server/spawned_test_server.h"
23 #include "net/url_request/url_fetcher_delegate.h"
24 #include "net/url_request/url_request_context_getter.h"
25 #include "net/url_request/url_request_test_util.h"
26 #include "net/url_request/url_request_throttler_manager.h"
27 #include "testing/gtest/include/gtest/gtest.h"
29 #if defined(USE_NSS) || defined(OS_IOS)
30 #include "net/ocsp/nss_ocsp.h"
36 using base::TimeDelta
;
38 // TODO(eroman): Add a regression test for http://crbug.com/40505.
42 // TODO(akalin): Move all the test data to somewhere under net/.
43 const base::FilePath::CharType kDocRoot
[] =
44 FILE_PATH_LITERAL("chrome/test/data");
45 const char kTestServerFilePrefix
[] = "files/";
47 class ThrottlingTestURLRequestContext
: public TestURLRequestContext
{
49 ThrottlingTestURLRequestContext() : TestURLRequestContext(true) {
50 set_throttler_manager(&throttler_manager_
);
52 DCHECK(throttler_manager() != NULL
);
56 URLRequestThrottlerManager throttler_manager_
;
59 class ThrottlingTestURLRequestContextGetter
60 : public TestURLRequestContextGetter
{
62 ThrottlingTestURLRequestContextGetter(
63 base::MessageLoopProxy
* io_message_loop_proxy
,
64 TestURLRequestContext
* request_context
)
65 : TestURLRequestContextGetter(io_message_loop_proxy
),
66 context_(request_context
) {
69 // TestURLRequestContextGetter:
70 virtual TestURLRequestContext
* GetURLRequestContext() OVERRIDE
{
75 virtual ~ThrottlingTestURLRequestContextGetter() {}
77 TestURLRequestContext
* const context_
;
82 class URLFetcherTest
: public testing::Test
,
83 public URLFetcherDelegate
{
85 URLFetcherTest() : fetcher_(NULL
) {}
87 static int GetNumFetcherCores() {
88 return URLFetcherImpl::GetNumFetcherCores();
91 // Creates a URLFetcher, using the program's main thread to do IO.
92 virtual void CreateFetcher(const GURL
& url
);
94 // URLFetcherDelegate:
95 // Subclasses that override this should either call this function or
96 // CleanupAfterFetchComplete() at the end of their processing, depending on
97 // whether they want to check for a non-empty HTTP 200 response or not.
98 virtual void OnURLFetchComplete(const URLFetcher
* source
) OVERRIDE
;
100 // Deletes |fetcher| and terminates the message loop.
101 void CleanupAfterFetchComplete();
103 scoped_refptr
<base::MessageLoopProxy
> io_message_loop_proxy() {
104 return io_message_loop_proxy_
;
107 TestURLRequestContext
* request_context() {
108 return context_
.get();
113 virtual void SetUp() OVERRIDE
{
114 testing::Test::SetUp();
116 context_
.reset(new ThrottlingTestURLRequestContext());
117 io_message_loop_proxy_
= base::MessageLoopProxy::current();
119 #if defined(USE_NSS) || defined(OS_IOS)
120 crypto::EnsureNSSInit();
121 EnsureNSSHttpIOInit();
125 virtual void TearDown() OVERRIDE
{
126 #if defined(USE_NSS) || defined(OS_IOS)
131 // URLFetcher is designed to run on the main UI thread, but in our tests
132 // we assume that the current thread is the IO thread where the URLFetcher
133 // dispatches its requests to. When we wish to simulate being used from
134 // a UI thread, we dispatch a worker thread to do so.
135 scoped_refptr
<base::MessageLoopProxy
> io_message_loop_proxy_
;
137 URLFetcherImpl
* fetcher_
;
138 scoped_ptr
<TestURLRequestContext
> context_
;
141 // A test fixture that uses a MockHostResolver, so that name resolutions can
142 // be manipulated by the tests to keep connections in the resolving state.
143 class URLFetcherMockDnsTest
: public URLFetcherTest
{
146 virtual void SetUp() OVERRIDE
;
149 virtual void CreateFetcher(const GURL
& url
) OVERRIDE
;
151 // URLFetcherDelegate:
152 virtual void OnURLFetchComplete(const URLFetcher
* source
) OVERRIDE
;
156 scoped_ptr
<SpawnedTestServer
> test_server_
;
157 MockHostResolver resolver_
;
158 scoped_ptr
<URLFetcher
> completed_fetcher_
;
161 void URLFetcherTest::CreateFetcher(const GURL
& url
) {
162 fetcher_
= new URLFetcherImpl(url
, URLFetcher::GET
, this);
163 fetcher_
->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
164 io_message_loop_proxy().get(), request_context()));
168 void URLFetcherTest::OnURLFetchComplete(const URLFetcher
* source
) {
169 EXPECT_TRUE(source
->GetStatus().is_success());
170 EXPECT_EQ(200, source
->GetResponseCode()); // HTTP OK
173 EXPECT_TRUE(source
->GetResponseAsString(&data
));
174 EXPECT_FALSE(data
.empty());
176 CleanupAfterFetchComplete();
179 void URLFetcherTest::CleanupAfterFetchComplete() {
180 delete fetcher_
; // Have to delete this here and not in the destructor,
181 // because the destructor won't necessarily run on the
182 // same thread that CreateFetcher() did.
184 io_message_loop_proxy()->PostTask(FROM_HERE
,
185 base::MessageLoop::QuitClosure());
186 // If the current message loop is not the IO loop, it will be shut down when
187 // the main loop returns and this thread subsequently goes out of scope.
190 void URLFetcherMockDnsTest::SetUp() {
191 URLFetcherTest::SetUp();
193 resolver_
.set_ondemand_mode(true);
194 resolver_
.rules()->AddRule("example.com", "127.0.0.1");
196 context_
.reset(new TestURLRequestContext(true));
197 context_
->set_host_resolver(&resolver_
);
200 test_server_
.reset(new SpawnedTestServer(SpawnedTestServer::TYPE_HTTP
,
201 SpawnedTestServer::kLocalhost
,
202 base::FilePath(kDocRoot
)));
203 ASSERT_TRUE(test_server_
->Start());
205 // test_server_.GetURL() returns a URL with 127.0.0.1 (kLocalhost), that is
206 // immediately resolved by the MockHostResolver. Use a hostname instead to
207 // trigger an async resolve.
209 base::StringPrintf("http://example.com:%d/defaultresponse",
210 test_server_
->host_port_pair().port()));
211 ASSERT_TRUE(test_url_
.is_valid());
214 void URLFetcherMockDnsTest::CreateFetcher(const GURL
& url
) {
215 fetcher_
= new URLFetcherImpl(url
, URLFetcher::GET
, this);
216 fetcher_
->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
217 io_message_loop_proxy().get(), request_context()));
220 void URLFetcherMockDnsTest::OnURLFetchComplete(const URLFetcher
* source
) {
221 io_message_loop_proxy()->PostTask(FROM_HERE
,
222 base::MessageLoop::QuitClosure());
223 ASSERT_EQ(fetcher_
, source
);
224 EXPECT_EQ(test_url_
, source
->GetOriginalURL());
225 completed_fetcher_
.reset(fetcher_
);
230 // Version of URLFetcherTest that does a POST instead
231 class URLFetcherPostTest
: public URLFetcherTest
{
234 virtual void CreateFetcher(const GURL
& url
) OVERRIDE
;
236 // URLFetcherDelegate:
237 virtual void OnURLFetchComplete(const URLFetcher
* source
) OVERRIDE
;
240 // Version of URLFetcherTest that does a POST of a file using
241 // SetUploadDataStream
242 class URLFetcherPostFileTest
: public URLFetcherTest
{
244 URLFetcherPostFileTest();
246 void SetUploadRange(uint64 range_offset
, uint64 range_length
) {
247 range_offset_
= range_offset
;
248 range_length_
= range_length
;
252 virtual void CreateFetcher(const GURL
& url
) OVERRIDE
;
254 // URLFetcherDelegate:
255 virtual void OnURLFetchComplete(const URLFetcher
* source
) OVERRIDE
;
258 base::FilePath path_
;
259 uint64 range_offset_
;
260 uint64 range_length_
;
263 // Version of URLFetcherTest that does a POST instead with empty upload body
264 class URLFetcherEmptyPostTest
: public URLFetcherTest
{
267 virtual void CreateFetcher(const GURL
& url
) OVERRIDE
;
269 // URLFetcherDelegate:
270 virtual void OnURLFetchComplete(const URLFetcher
* source
) OVERRIDE
;
273 // Version of URLFetcherTest that tests download progress reports.
274 class URLFetcherDownloadProgressTest
: public URLFetcherTest
{
276 URLFetcherDownloadProgressTest()
277 : previous_progress_(0),
282 virtual void CreateFetcher(const GURL
& url
) OVERRIDE
;
284 // URLFetcherDelegate:
285 virtual void OnURLFetchDownloadProgress(const URLFetcher
* source
,
287 int64 total
) OVERRIDE
;
290 // Download progress returned by the previous callback.
291 int64 previous_progress_
;
292 // Size of the file being downloaded, known in advance (provided by each test
294 int64 expected_total_
;
297 // Version of URLFetcherTest that tests progress reports at cancellation.
298 class URLFetcherDownloadProgressCancelTest
: public URLFetcherTest
{
301 virtual void CreateFetcher(const GURL
& url
) OVERRIDE
;
303 // URLFetcherDelegate:
304 virtual void OnURLFetchComplete(const URLFetcher
* source
) OVERRIDE
;
305 virtual void OnURLFetchDownloadProgress(const URLFetcher
* source
,
307 int64 total
) OVERRIDE
;
312 // Version of URLFetcherTest that tests upload progress reports.
313 class URLFetcherUploadProgressTest
: public URLFetcherTest
{
316 virtual void CreateFetcher(const GURL
& url
) OVERRIDE
;
318 // URLFetcherDelegate:
319 virtual void OnURLFetchUploadProgress(const URLFetcher
* source
,
321 int64 total
) OVERRIDE
;
323 int64 previous_progress_
;
325 int64 number_of_chunks_added_
;
328 // Version of URLFetcherTest that tests headers.
329 class URLFetcherHeadersTest
: public URLFetcherTest
{
331 // URLFetcherDelegate:
332 virtual void OnURLFetchComplete(const URLFetcher
* source
) OVERRIDE
;
335 // Version of URLFetcherTest that tests SocketAddress.
336 class URLFetcherSocketAddressTest
: public URLFetcherTest
{
338 // URLFetcherDelegate:
339 virtual void OnURLFetchComplete(const URLFetcher
* source
) OVERRIDE
;
341 std::string expected_host_
;
342 uint16 expected_port_
;
345 // Version of URLFetcherTest that tests stopping on a redirect.
346 class URLFetcherStopOnRedirectTest
: public URLFetcherTest
{
348 URLFetcherStopOnRedirectTest();
349 virtual ~URLFetcherStopOnRedirectTest();
352 virtual void CreateFetcher(const GURL
& url
) OVERRIDE
;
354 // URLFetcherDelegate:
355 virtual void OnURLFetchComplete(const URLFetcher
* source
) OVERRIDE
;
358 // The URL we should be redirected to.
359 static const char* kRedirectTarget
;
361 bool callback_called_
; // Set to true in OnURLFetchComplete().
364 // Version of URLFetcherTest that tests overload protection.
365 class URLFetcherProtectTest
: public URLFetcherTest
{
368 virtual void CreateFetcher(const GURL
& url
) OVERRIDE
;
370 // URLFetcherDelegate:
371 virtual void OnURLFetchComplete(const URLFetcher
* source
) OVERRIDE
;
376 // Version of URLFetcherTest that tests overload protection, when responses
378 class URLFetcherProtectTestPassedThrough
: public URLFetcherTest
{
381 virtual void CreateFetcher(const GURL
& url
) OVERRIDE
;
383 // URLFetcherDelegate:
384 virtual void OnURLFetchComplete(const URLFetcher
* source
) OVERRIDE
;
389 // Version of URLFetcherTest that tests bad HTTPS requests.
390 class URLFetcherBadHTTPSTest
: public URLFetcherTest
{
392 URLFetcherBadHTTPSTest();
394 // URLFetcherDelegate:
395 virtual void OnURLFetchComplete(const URLFetcher
* source
) OVERRIDE
;
398 base::FilePath cert_dir_
;
401 // Version of URLFetcherTest that tests request cancellation on shutdown.
402 class URLFetcherCancelTest
: public URLFetcherTest
{
405 virtual void CreateFetcher(const GURL
& url
) OVERRIDE
;
407 // URLFetcherDelegate:
408 virtual void OnURLFetchComplete(const URLFetcher
* source
) OVERRIDE
;
410 void CancelRequest();
413 // Version of TestURLRequestContext that posts a Quit task to the IO
414 // thread once it is deleted.
415 class CancelTestURLRequestContext
: public ThrottlingTestURLRequestContext
{
417 explicit CancelTestURLRequestContext() {
421 virtual ~CancelTestURLRequestContext() {
422 // The d'tor should execute in the IO thread. Post the quit task to the
424 base::MessageLoop::current()->PostTask(FROM_HERE
,
425 base::MessageLoop::QuitClosure());
429 class CancelTestURLRequestContextGetter
430 : public TestURLRequestContextGetter
{
432 CancelTestURLRequestContextGetter(
433 base::MessageLoopProxy
* io_message_loop_proxy
,
434 const GURL
& throttle_for_url
)
435 : TestURLRequestContextGetter(io_message_loop_proxy
),
436 io_message_loop_proxy_(io_message_loop_proxy
),
437 context_created_(false, false),
438 throttle_for_url_(throttle_for_url
) {
441 // TestURLRequestContextGetter:
442 virtual TestURLRequestContext
* GetURLRequestContext() OVERRIDE
{
443 if (!context_
.get()) {
444 context_
.reset(new CancelTestURLRequestContext());
445 DCHECK(context_
->throttler_manager());
447 // Registers an entry for test url. The backoff time is calculated by:
448 // new_backoff = 2.0 * old_backoff + 0
449 // The initial backoff is 2 seconds and maximum backoff is 4 seconds.
450 // Maximum retries allowed is set to 2.
451 scoped_refptr
<URLRequestThrottlerEntry
> entry(
452 new URLRequestThrottlerEntry(context_
->throttler_manager(),
460 context_
->throttler_manager()
461 ->OverrideEntryForTests(throttle_for_url_
, entry
.get());
463 context_created_
.Signal();
465 return context_
.get();
468 virtual scoped_refptr
<base::MessageLoopProxy
> GetIOMessageLoopProxy() const {
469 return io_message_loop_proxy_
;
472 void WaitForContextCreation() {
473 context_created_
.Wait();
477 virtual ~CancelTestURLRequestContextGetter() {}
480 scoped_ptr
<TestURLRequestContext
> context_
;
481 scoped_refptr
<base::MessageLoopProxy
> io_message_loop_proxy_
;
482 base::WaitableEvent context_created_
;
483 GURL throttle_for_url_
;
486 // Version of URLFetcherTest that tests retying the same request twice.
487 class URLFetcherMultipleAttemptTest
: public URLFetcherTest
{
489 // URLFetcherDelegate:
490 virtual void OnURLFetchComplete(const URLFetcher
* source
) OVERRIDE
;
495 class URLFetcherFileTest
: public URLFetcherTest
{
497 URLFetcherFileTest() : take_ownership_of_file_(false),
498 expected_file_error_(OK
) {}
500 void CreateFetcherForFile(const GURL
& url
, const base::FilePath
& file_path
);
501 void CreateFetcherForTempFile(const GURL
& url
);
503 // URLFetcherDelegate:
504 virtual void OnURLFetchComplete(const URLFetcher
* source
) OVERRIDE
;
507 base::FilePath expected_file_
;
508 base::FilePath file_path_
;
510 // Set by the test. Used in OnURLFetchComplete() to decide if
511 // the URLFetcher should own the temp file, so that we can test
512 // disowning prevents the file from being deleted.
513 bool take_ownership_of_file_
;
515 // Expected file error code for the test. OK when expecting success.
516 int expected_file_error_
;
519 void URLFetcherPostTest::CreateFetcher(const GURL
& url
) {
520 fetcher_
= new URLFetcherImpl(url
, URLFetcher::POST
, this);
521 fetcher_
->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
522 io_message_loop_proxy().get(), request_context()));
523 fetcher_
->SetUploadData("application/x-www-form-urlencoded", "bobsyeruncle");
527 void URLFetcherPostTest::OnURLFetchComplete(const URLFetcher
* source
) {
529 EXPECT_TRUE(source
->GetResponseAsString(&data
));
530 EXPECT_EQ(std::string("bobsyeruncle"), data
);
531 URLFetcherTest::OnURLFetchComplete(source
);
534 URLFetcherPostFileTest::URLFetcherPostFileTest()
536 range_length_(kuint64max
) {
537 PathService::Get(base::DIR_SOURCE_ROOT
, &path_
);
538 path_
= path_
.Append(FILE_PATH_LITERAL("net"));
539 path_
= path_
.Append(FILE_PATH_LITERAL("data"));
540 path_
= path_
.Append(FILE_PATH_LITERAL("url_request_unittest"));
541 path_
= path_
.Append(FILE_PATH_LITERAL("BullRunSpeech.txt"));
544 void URLFetcherPostFileTest::CreateFetcher(const GURL
& url
) {
545 fetcher_
= new URLFetcherImpl(url
, URLFetcher::POST
, this);
546 fetcher_
->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
547 io_message_loop_proxy().get(), request_context()));
548 fetcher_
->SetUploadFilePath("application/x-www-form-urlencoded",
552 base::MessageLoopProxy::current());
556 void URLFetcherPostFileTest::OnURLFetchComplete(const URLFetcher
* source
) {
557 std::string expected
;
558 ASSERT_TRUE(base::ReadFileToString(path_
, &expected
));
559 ASSERT_LE(range_offset_
, expected
.size());
560 uint64 expected_size
=
561 std::min(range_length_
, expected
.size() - range_offset_
);
564 EXPECT_TRUE(source
->GetResponseAsString(&data
));
565 EXPECT_EQ(expected
.substr(range_offset_
, expected_size
), data
);
566 URLFetcherTest::OnURLFetchComplete(source
);
569 void URLFetcherEmptyPostTest::CreateFetcher(const GURL
& url
) {
570 fetcher_
= new URLFetcherImpl(url
, URLFetcher::POST
, this);
571 fetcher_
->SetRequestContext(new TestURLRequestContextGetter(
572 io_message_loop_proxy()));
573 fetcher_
->SetUploadData("text/plain", std::string());
577 void URLFetcherEmptyPostTest::OnURLFetchComplete(const URLFetcher
* source
) {
578 EXPECT_TRUE(source
->GetStatus().is_success());
579 EXPECT_EQ(200, source
->GetResponseCode()); // HTTP OK
582 EXPECT_TRUE(source
->GetResponseAsString(&data
));
583 EXPECT_TRUE(data
.empty());
585 CleanupAfterFetchComplete();
586 // Do not call the super class method URLFetcherTest::OnURLFetchComplete,
587 // since it expects a non-empty response.
590 void URLFetcherDownloadProgressTest::CreateFetcher(const GURL
& url
) {
591 fetcher_
= new URLFetcherImpl(url
, URLFetcher::GET
, this);
592 fetcher_
->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
593 io_message_loop_proxy().get(), request_context()));
597 void URLFetcherDownloadProgressTest::OnURLFetchDownloadProgress(
598 const URLFetcher
* source
, int64 progress
, int64 total
) {
599 // Increasing between 0 and total.
600 EXPECT_LE(0, progress
);
601 EXPECT_GE(total
, progress
);
602 EXPECT_LE(previous_progress_
, progress
);
603 EXPECT_EQ(expected_total_
, total
);
604 previous_progress_
= progress
;
607 void URLFetcherDownloadProgressCancelTest::CreateFetcher(const GURL
& url
) {
608 fetcher_
= new URLFetcherImpl(url
, URLFetcher::GET
, this);
609 fetcher_
->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
610 io_message_loop_proxy().get(), request_context()));
615 void URLFetcherDownloadProgressCancelTest::OnURLFetchDownloadProgress(
616 const URLFetcher
* source
, int64 current
, int64 total
) {
617 EXPECT_FALSE(cancelled_
);
620 CleanupAfterFetchComplete();
624 void URLFetcherDownloadProgressCancelTest::OnURLFetchComplete(
625 const URLFetcher
* source
) {
626 // Should have been cancelled.
628 CleanupAfterFetchComplete();
631 void URLFetcherUploadProgressTest::CreateFetcher(const GURL
& url
) {
632 fetcher_
= new URLFetcherImpl(url
, URLFetcher::POST
, this);
633 fetcher_
->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
634 io_message_loop_proxy().get(), request_context()));
635 previous_progress_
= 0;
636 // Large enough data to require more than one read from UploadDataStream.
637 chunk_
.assign(1<<16, 'a');
638 // Use chunked upload to wait for a timer event of progress notification.
639 fetcher_
->SetChunkedUpload("application/x-www-form-urlencoded");
641 number_of_chunks_added_
= 1;
642 fetcher_
->AppendChunkToUpload(chunk_
, false);
645 void URLFetcherUploadProgressTest::OnURLFetchUploadProgress(
646 const URLFetcher
* source
, int64 current
, int64 total
) {
647 // Increasing between 0 and total.
648 EXPECT_LE(0, current
);
649 EXPECT_GE(static_cast<int64
>(chunk_
.size()) * number_of_chunks_added_
,
651 EXPECT_LE(previous_progress_
, current
);
652 previous_progress_
= current
;
653 EXPECT_EQ(-1, total
);
655 if (number_of_chunks_added_
< 2) {
656 number_of_chunks_added_
+= 1;
657 fetcher_
->AppendChunkToUpload(chunk_
, true);
661 void URLFetcherHeadersTest::OnURLFetchComplete(
662 const URLFetcher
* source
) {
664 EXPECT_TRUE(source
->GetResponseHeaders()->GetNormalizedHeader("cache-control",
666 EXPECT_EQ("private", header
);
667 URLFetcherTest::OnURLFetchComplete(source
);
670 void URLFetcherSocketAddressTest::OnURLFetchComplete(
671 const URLFetcher
* source
) {
672 EXPECT_EQ("127.0.0.1", source
->GetSocketAddress().host());
673 EXPECT_EQ(expected_port_
, source
->GetSocketAddress().port());
674 URLFetcherTest::OnURLFetchComplete(source
);
678 const char* URLFetcherStopOnRedirectTest::kRedirectTarget
=
679 "http://redirect.target.com";
681 URLFetcherStopOnRedirectTest::URLFetcherStopOnRedirectTest()
682 : callback_called_(false) {
685 URLFetcherStopOnRedirectTest::~URLFetcherStopOnRedirectTest() {
688 void URLFetcherStopOnRedirectTest::CreateFetcher(const GURL
& url
) {
689 fetcher_
= new URLFetcherImpl(url
, URLFetcher::GET
, this);
690 fetcher_
->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
691 io_message_loop_proxy().get(), request_context()));
692 fetcher_
->SetStopOnRedirect(true);
696 void URLFetcherStopOnRedirectTest::OnURLFetchComplete(
697 const URLFetcher
* source
) {
698 callback_called_
= true;
699 EXPECT_EQ(GURL(kRedirectTarget
), source
->GetURL());
700 EXPECT_EQ(URLRequestStatus::CANCELED
, source
->GetStatus().status());
701 EXPECT_EQ(ERR_ABORTED
, source
->GetStatus().error());
702 EXPECT_EQ(301, source
->GetResponseCode());
703 CleanupAfterFetchComplete();
706 void URLFetcherProtectTest::CreateFetcher(const GURL
& url
) {
707 fetcher_
= new URLFetcherImpl(url
, URLFetcher::GET
, this);
708 fetcher_
->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
709 io_message_loop_proxy().get(), request_context()));
710 start_time_
= Time::Now();
711 fetcher_
->SetMaxRetriesOn5xx(11);
715 void URLFetcherProtectTest::OnURLFetchComplete(const URLFetcher
* source
) {
716 const TimeDelta one_second
= TimeDelta::FromMilliseconds(1000);
717 if (source
->GetResponseCode() >= 500) {
718 // Now running ServerUnavailable test.
719 // It takes more than 1 second to finish all 11 requests.
720 EXPECT_TRUE(Time::Now() - start_time_
>= one_second
);
721 EXPECT_TRUE(source
->GetStatus().is_success());
723 EXPECT_TRUE(source
->GetResponseAsString(&data
));
724 EXPECT_FALSE(data
.empty());
725 CleanupAfterFetchComplete();
727 // Now running Overload test.
728 static int count
= 0;
731 fetcher_
->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
732 io_message_loop_proxy().get(), request_context()));
735 // We have already sent 20 requests continuously. And we expect that
736 // it takes more than 1 second due to the overload protection settings.
737 EXPECT_TRUE(Time::Now() - start_time_
>= one_second
);
738 URLFetcherTest::OnURLFetchComplete(source
);
743 void URLFetcherProtectTestPassedThrough::CreateFetcher(const GURL
& url
) {
744 fetcher_
= new URLFetcherImpl(url
, URLFetcher::GET
, this);
745 fetcher_
->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
746 io_message_loop_proxy().get(), request_context()));
747 fetcher_
->SetAutomaticallyRetryOn5xx(false);
748 start_time_
= Time::Now();
749 fetcher_
->SetMaxRetriesOn5xx(11);
753 void URLFetcherProtectTestPassedThrough::OnURLFetchComplete(
754 const URLFetcher
* source
) {
755 const TimeDelta one_minute
= TimeDelta::FromMilliseconds(60000);
756 if (source
->GetResponseCode() >= 500) {
757 // Now running ServerUnavailable test.
758 // It should get here on the first attempt, so almost immediately and
759 // *not* to attempt to execute all 11 requests (2.5 minutes).
760 EXPECT_TRUE(Time::Now() - start_time_
< one_minute
);
761 EXPECT_TRUE(source
->GetStatus().is_success());
762 // Check that suggested back off time is bigger than 0.
763 EXPECT_GT(fetcher_
->GetBackoffDelay().InMicroseconds(), 0);
765 EXPECT_TRUE(source
->GetResponseAsString(&data
));
766 EXPECT_FALSE(data
.empty());
768 // We should not get here!
772 CleanupAfterFetchComplete();
776 URLFetcherBadHTTPSTest::URLFetcherBadHTTPSTest() {
777 PathService::Get(base::DIR_SOURCE_ROOT
, &cert_dir_
);
778 cert_dir_
= cert_dir_
.AppendASCII("chrome");
779 cert_dir_
= cert_dir_
.AppendASCII("test");
780 cert_dir_
= cert_dir_
.AppendASCII("data");
781 cert_dir_
= cert_dir_
.AppendASCII("ssl");
782 cert_dir_
= cert_dir_
.AppendASCII("certificates");
785 // The "server certificate expired" error should result in automatic
786 // cancellation of the request by
787 // URLRequest::Delegate::OnSSLCertificateError.
788 void URLFetcherBadHTTPSTest::OnURLFetchComplete(
789 const URLFetcher
* source
) {
790 // This part is different from URLFetcherTest::OnURLFetchComplete
791 // because this test expects the request to be cancelled.
792 EXPECT_EQ(URLRequestStatus::CANCELED
, source
->GetStatus().status());
793 EXPECT_EQ(ERR_ABORTED
, source
->GetStatus().error());
794 EXPECT_EQ(-1, source
->GetResponseCode());
795 EXPECT_TRUE(source
->GetCookies().empty());
797 EXPECT_TRUE(source
->GetResponseAsString(&data
));
798 EXPECT_TRUE(data
.empty());
799 CleanupAfterFetchComplete();
802 void URLFetcherCancelTest::CreateFetcher(const GURL
& url
) {
803 fetcher_
= new URLFetcherImpl(url
, URLFetcher::GET
, this);
804 CancelTestURLRequestContextGetter
* context_getter
=
805 new CancelTestURLRequestContextGetter(io_message_loop_proxy().get(), url
);
806 fetcher_
->SetRequestContext(context_getter
);
807 fetcher_
->SetMaxRetriesOn5xx(2);
809 // We need to wait for the creation of the URLRequestContext, since we
810 // rely on it being destroyed as a signal to end the test.
811 context_getter
->WaitForContextCreation();
815 void URLFetcherCancelTest::OnURLFetchComplete(
816 const URLFetcher
* source
) {
817 // We should have cancelled the request before completion.
819 CleanupAfterFetchComplete();
822 void URLFetcherCancelTest::CancelRequest() {
824 // The URLFetcher's test context will post a Quit task once it is
825 // deleted. So if this test simply hangs, it means cancellation
829 void URLFetcherMultipleAttemptTest::OnURLFetchComplete(
830 const URLFetcher
* source
) {
831 EXPECT_TRUE(source
->GetStatus().is_success());
832 EXPECT_EQ(200, source
->GetResponseCode()); // HTTP OK
834 EXPECT_TRUE(source
->GetResponseAsString(&data
));
835 EXPECT_FALSE(data
.empty());
836 if (!data
.empty() && data_
.empty()) {
838 fetcher_
->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
839 io_message_loop_proxy().get(), request_context()));
842 EXPECT_EQ(data
, data_
);
843 CleanupAfterFetchComplete();
847 void URLFetcherFileTest::CreateFetcherForFile(const GURL
& url
,
848 const base::FilePath
& file_path
) {
849 fetcher_
= new URLFetcherImpl(url
, URLFetcher::GET
, this);
850 fetcher_
->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
851 io_message_loop_proxy().get(), request_context()));
853 // Use the IO message loop to do the file operations in this test.
854 fetcher_
->SaveResponseToFileAtPath(file_path
, io_message_loop_proxy());
858 void URLFetcherFileTest::CreateFetcherForTempFile(const GURL
& url
) {
859 fetcher_
= new URLFetcherImpl(url
, URLFetcher::GET
, this);
860 fetcher_
->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
861 io_message_loop_proxy().get(), request_context()));
863 // Use the IO message loop to do the file operations in this test.
864 fetcher_
->SaveResponseToTemporaryFile(io_message_loop_proxy());
868 void URLFetcherFileTest::OnURLFetchComplete(const URLFetcher
* source
) {
869 if (expected_file_error_
== OK
) {
870 EXPECT_TRUE(source
->GetStatus().is_success());
871 EXPECT_EQ(OK
, source
->GetStatus().error());
872 EXPECT_EQ(200, source
->GetResponseCode());
874 EXPECT_TRUE(source
->GetResponseAsFilePath(
875 take_ownership_of_file_
, &file_path_
));
877 EXPECT_TRUE(base::ContentsEqual(expected_file_
, file_path_
));
879 EXPECT_FALSE(source
->GetStatus().is_success());
880 EXPECT_EQ(expected_file_error_
, source
->GetStatus().error());
882 CleanupAfterFetchComplete();
885 TEST_F(URLFetcherTest
, SameThreadsTest
) {
886 SpawnedTestServer
test_server(SpawnedTestServer::TYPE_HTTP
,
887 SpawnedTestServer::kLocalhost
,
888 base::FilePath(kDocRoot
));
889 ASSERT_TRUE(test_server
.Start());
891 // Create the fetcher on the main thread. Since IO will happen on the main
892 // thread, this will test URLFetcher's ability to do everything on one
894 CreateFetcher(test_server
.GetURL("defaultresponse"));
896 base::MessageLoop::current()->Run();
899 TEST_F(URLFetcherTest
, DifferentThreadsTest
) {
900 SpawnedTestServer
test_server(SpawnedTestServer::TYPE_HTTP
,
901 SpawnedTestServer::kLocalhost
,
902 base::FilePath(kDocRoot
));
903 ASSERT_TRUE(test_server
.Start());
905 // Create a separate thread that will create the URLFetcher. The current
906 // (main) thread will do the IO, and when the fetch is complete it will
907 // terminate the main thread's message loop; then the other thread's
908 // message loop will be shut down automatically as the thread goes out of
910 base::Thread
t("URLFetcher test thread");
911 ASSERT_TRUE(t
.Start());
912 t
.message_loop()->PostTask(
914 base::Bind(&URLFetcherTest::CreateFetcher
,
915 base::Unretained(this),
916 test_server
.GetURL("defaultresponse")));
918 base::MessageLoop::current()->Run();
921 void CancelAllOnIO() {
922 EXPECT_EQ(1, URLFetcherTest::GetNumFetcherCores());
923 URLFetcherImpl::CancelAll();
924 EXPECT_EQ(0, URLFetcherTest::GetNumFetcherCores());
927 // Tests to make sure CancelAll() will successfully cancel existing URLFetchers.
928 TEST_F(URLFetcherTest
, CancelAll
) {
929 SpawnedTestServer
test_server(SpawnedTestServer::TYPE_HTTP
,
930 SpawnedTestServer::kLocalhost
,
931 base::FilePath(kDocRoot
));
932 ASSERT_TRUE(test_server
.Start());
933 EXPECT_EQ(0, GetNumFetcherCores());
935 CreateFetcher(test_server
.GetURL("defaultresponse"));
936 io_message_loop_proxy()->PostTaskAndReply(
937 FROM_HERE
, base::Bind(&CancelAllOnIO
), base::MessageLoop::QuitClosure());
938 base::MessageLoop::current()->Run();
939 EXPECT_EQ(0, GetNumFetcherCores());
943 TEST_F(URLFetcherMockDnsTest
, DontRetryOnNetworkChangedByDefault
) {
944 EXPECT_EQ(0, GetNumFetcherCores());
945 EXPECT_FALSE(resolver_
.has_pending_requests());
947 // This posts a task to start the fetcher.
948 CreateFetcher(test_url_
);
950 EXPECT_EQ(0, GetNumFetcherCores());
951 base::MessageLoop::current()->RunUntilIdle();
953 // The fetcher is now running, but is pending the host resolve.
954 EXPECT_EQ(1, GetNumFetcherCores());
955 EXPECT_TRUE(resolver_
.has_pending_requests());
956 ASSERT_FALSE(completed_fetcher_
);
958 // A network change notification aborts the connect job.
959 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
960 base::MessageLoop::current()->RunUntilIdle();
961 EXPECT_EQ(0, GetNumFetcherCores());
962 EXPECT_FALSE(resolver_
.has_pending_requests());
963 ASSERT_TRUE(completed_fetcher_
);
965 // And the owner of the fetcher gets the ERR_NETWORK_CHANGED error.
966 EXPECT_EQ(ERR_NETWORK_CHANGED
, completed_fetcher_
->GetStatus().error());
969 TEST_F(URLFetcherMockDnsTest
, RetryOnNetworkChangedAndFail
) {
970 EXPECT_EQ(0, GetNumFetcherCores());
971 EXPECT_FALSE(resolver_
.has_pending_requests());
973 // This posts a task to start the fetcher.
974 CreateFetcher(test_url_
);
975 fetcher_
->SetAutomaticallyRetryOnNetworkChanges(3);
977 EXPECT_EQ(0, GetNumFetcherCores());
978 base::MessageLoop::current()->RunUntilIdle();
980 // The fetcher is now running, but is pending the host resolve.
981 EXPECT_EQ(1, GetNumFetcherCores());
982 EXPECT_TRUE(resolver_
.has_pending_requests());
983 ASSERT_FALSE(completed_fetcher_
);
985 // Make it fail 3 times.
986 for (int i
= 0; i
< 3; ++i
) {
987 // A network change notification aborts the connect job.
988 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
989 base::MessageLoop::current()->RunUntilIdle();
991 // But the fetcher retries automatically.
992 EXPECT_EQ(1, GetNumFetcherCores());
993 EXPECT_TRUE(resolver_
.has_pending_requests());
994 ASSERT_FALSE(completed_fetcher_
);
997 // A 4th failure doesn't trigger another retry, and propagates the error
998 // to the owner of the fetcher.
999 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
1000 base::MessageLoop::current()->RunUntilIdle();
1001 EXPECT_EQ(0, GetNumFetcherCores());
1002 EXPECT_FALSE(resolver_
.has_pending_requests());
1003 ASSERT_TRUE(completed_fetcher_
);
1005 // And the owner of the fetcher gets the ERR_NETWORK_CHANGED error.
1006 EXPECT_EQ(ERR_NETWORK_CHANGED
, completed_fetcher_
->GetStatus().error());
1009 TEST_F(URLFetcherMockDnsTest
, RetryOnNetworkChangedAndSucceed
) {
1010 EXPECT_EQ(0, GetNumFetcherCores());
1011 EXPECT_FALSE(resolver_
.has_pending_requests());
1013 // This posts a task to start the fetcher.
1014 CreateFetcher(test_url_
);
1015 fetcher_
->SetAutomaticallyRetryOnNetworkChanges(3);
1017 EXPECT_EQ(0, GetNumFetcherCores());
1018 base::MessageLoop::current()->RunUntilIdle();
1020 // The fetcher is now running, but is pending the host resolve.
1021 EXPECT_EQ(1, GetNumFetcherCores());
1022 EXPECT_TRUE(resolver_
.has_pending_requests());
1023 ASSERT_FALSE(completed_fetcher_
);
1025 // Make it fail 3 times.
1026 for (int i
= 0; i
< 3; ++i
) {
1027 // A network change notification aborts the connect job.
1028 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
1029 base::MessageLoop::current()->RunUntilIdle();
1031 // But the fetcher retries automatically.
1032 EXPECT_EQ(1, GetNumFetcherCores());
1033 EXPECT_TRUE(resolver_
.has_pending_requests());
1034 ASSERT_FALSE(completed_fetcher_
);
1037 // Now let it succeed by resolving the pending request.
1038 resolver_
.ResolveAllPending();
1039 base::MessageLoop::current()->Run();
1041 // URLFetcherMockDnsTest::OnURLFetchComplete() will quit the loop.
1042 EXPECT_EQ(0, GetNumFetcherCores());
1043 EXPECT_FALSE(resolver_
.has_pending_requests());
1044 ASSERT_TRUE(completed_fetcher_
);
1046 // This time the request succeeded.
1047 EXPECT_EQ(OK
, completed_fetcher_
->GetStatus().error());
1048 EXPECT_EQ(200, completed_fetcher_
->GetResponseCode());
1051 TEST_F(URLFetcherPostTest
, Basic
) {
1052 SpawnedTestServer
test_server(SpawnedTestServer::TYPE_HTTP
,
1053 SpawnedTestServer::kLocalhost
,
1054 base::FilePath(kDocRoot
));
1055 ASSERT_TRUE(test_server
.Start());
1057 CreateFetcher(test_server
.GetURL("echo"));
1058 base::MessageLoop::current()->Run();
1061 TEST_F(URLFetcherPostFileTest
, Basic
) {
1062 SpawnedTestServer
test_server(SpawnedTestServer::TYPE_HTTP
,
1063 SpawnedTestServer::kLocalhost
,
1064 base::FilePath(kDocRoot
));
1065 ASSERT_TRUE(test_server
.Start());
1067 CreateFetcher(test_server
.GetURL("echo"));
1068 base::MessageLoop::current()->Run();
1071 TEST_F(URLFetcherPostFileTest
, Range
) {
1072 SpawnedTestServer
test_server(SpawnedTestServer::TYPE_HTTP
,
1073 SpawnedTestServer::kLocalhost
,
1074 base::FilePath(kDocRoot
));
1075 ASSERT_TRUE(test_server
.Start());
1077 SetUploadRange(30, 100);
1079 CreateFetcher(test_server
.GetURL("echo"));
1080 base::MessageLoop::current()->Run();
1083 TEST_F(URLFetcherEmptyPostTest
, Basic
) {
1084 SpawnedTestServer
test_server(SpawnedTestServer::TYPE_HTTP
,
1085 SpawnedTestServer::kLocalhost
,
1086 base::FilePath(kDocRoot
));
1087 ASSERT_TRUE(test_server
.Start());
1089 CreateFetcher(test_server
.GetURL("echo"));
1090 base::MessageLoop::current()->Run();
1093 TEST_F(URLFetcherUploadProgressTest
, Basic
) {
1094 SpawnedTestServer
test_server(SpawnedTestServer::TYPE_HTTP
,
1095 SpawnedTestServer::kLocalhost
,
1096 base::FilePath(kDocRoot
));
1097 ASSERT_TRUE(test_server
.Start());
1099 CreateFetcher(test_server
.GetURL("echo"));
1100 base::MessageLoop::current()->Run();
1103 TEST_F(URLFetcherDownloadProgressTest
, Basic
) {
1104 SpawnedTestServer
test_server(SpawnedTestServer::TYPE_HTTP
,
1105 SpawnedTestServer::kLocalhost
,
1106 base::FilePath(kDocRoot
));
1107 ASSERT_TRUE(test_server
.Start());
1109 // Get a file large enough to require more than one read into
1110 // URLFetcher::Core's IOBuffer.
1111 static const char kFileToFetch
[] = "animate1.gif";
1112 // Hardcoded file size - it cannot be easily fetched when a remote http server
1113 // is used for testing.
1114 static const int64 kFileSize
= 19021;
1116 expected_total_
= kFileSize
;
1118 CreateFetcher(test_server
.GetURL(
1119 std::string(kTestServerFilePrefix
) + kFileToFetch
));
1121 base::MessageLoop::current()->Run();
1124 TEST_F(URLFetcherDownloadProgressCancelTest
, CancelWhileProgressReport
) {
1125 SpawnedTestServer
test_server(SpawnedTestServer::TYPE_HTTP
,
1126 SpawnedTestServer::kLocalhost
,
1127 base::FilePath(kDocRoot
));
1128 ASSERT_TRUE(test_server
.Start());
1130 // Get a file large enough to require more than one read into
1131 // URLFetcher::Core's IOBuffer.
1132 static const char kFileToFetch
[] = "animate1.gif";
1133 CreateFetcher(test_server
.GetURL(
1134 std::string(kTestServerFilePrefix
) + kFileToFetch
));
1136 base::MessageLoop::current()->Run();
1139 TEST_F(URLFetcherHeadersTest
, Headers
) {
1140 SpawnedTestServer
test_server(
1141 SpawnedTestServer::TYPE_HTTP
,
1142 SpawnedTestServer::kLocalhost
,
1143 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
1144 ASSERT_TRUE(test_server
.Start());
1146 CreateFetcher(test_server
.GetURL("files/with-headers.html"));
1147 base::MessageLoop::current()->Run();
1148 // The actual tests are in the URLFetcherHeadersTest fixture.
1151 TEST_F(URLFetcherSocketAddressTest
, SocketAddress
) {
1152 SpawnedTestServer
test_server(
1153 SpawnedTestServer::TYPE_HTTP
,
1154 SpawnedTestServer::kLocalhost
,
1155 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
1156 ASSERT_TRUE(test_server
.Start());
1157 expected_port_
= test_server
.host_port_pair().port();
1159 // Reusing "with-headers.html" but doesn't really matter.
1160 CreateFetcher(test_server
.GetURL("files/with-headers.html"));
1161 base::MessageLoop::current()->Run();
1162 // The actual tests are in the URLFetcherSocketAddressTest fixture.
1165 TEST_F(URLFetcherStopOnRedirectTest
, StopOnRedirect
) {
1166 SpawnedTestServer
test_server(SpawnedTestServer::TYPE_HTTP
,
1167 SpawnedTestServer::kLocalhost
,
1168 base::FilePath(kDocRoot
));
1169 ASSERT_TRUE(test_server
.Start());
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 SpawnedTestServer
test_server(SpawnedTestServer::TYPE_HTTP
,
1179 SpawnedTestServer::kLocalhost
,
1180 base::FilePath(kDocRoot
));
1181 ASSERT_TRUE(test_server
.Start());
1183 GURL
url(test_server
.GetURL("defaultresponse"));
1185 // Registers an entry for test url. It only allows 3 requests to be sent
1186 // in 200 milliseconds.
1187 scoped_refptr
<URLRequestThrottlerEntry
> entry(
1188 new URLRequestThrottlerEntry(request_context()->throttler_manager(),
1196 request_context()->throttler_manager()
1197 ->OverrideEntryForTests(url
, entry
.get());
1201 base::MessageLoop::current()->Run();
1204 TEST_F(URLFetcherProtectTest
, ServerUnavailable
) {
1205 SpawnedTestServer
test_server(SpawnedTestServer::TYPE_HTTP
,
1206 SpawnedTestServer::kLocalhost
,
1207 base::FilePath(kDocRoot
));
1208 ASSERT_TRUE(test_server
.Start());
1210 GURL
url(test_server
.GetURL("files/server-unavailable.html"));
1212 // Registers an entry for test url. The backoff time is calculated by:
1213 // new_backoff = 2.0 * old_backoff + 0
1214 // and maximum backoff time is 256 milliseconds.
1215 // Maximum retries allowed is set to 11.
1216 scoped_refptr
<URLRequestThrottlerEntry
> entry(
1217 new URLRequestThrottlerEntry(request_context()->throttler_manager(),
1225 request_context()->throttler_manager()
1226 ->OverrideEntryForTests(url
, entry
.get());
1230 base::MessageLoop::current()->Run();
1233 TEST_F(URLFetcherProtectTestPassedThrough
, ServerUnavailablePropagateResponse
) {
1234 SpawnedTestServer
test_server(SpawnedTestServer::TYPE_HTTP
,
1235 SpawnedTestServer::kLocalhost
,
1236 base::FilePath(kDocRoot
));
1237 ASSERT_TRUE(test_server
.Start());
1239 GURL
url(test_server
.GetURL("files/server-unavailable.html"));
1241 // Registers an entry for test url. The backoff time is calculated by:
1242 // new_backoff = 2.0 * old_backoff + 0
1243 // and maximum backoff time is 150000 milliseconds.
1244 // Maximum retries allowed is set to 11.
1245 scoped_refptr
<URLRequestThrottlerEntry
> entry(
1246 new URLRequestThrottlerEntry(request_context()->throttler_manager(),
1254 // Total time if *not* for not doing automatic backoff would be 150s.
1255 // In reality it should be "as soon as server responds".
1256 request_context()->throttler_manager()
1257 ->OverrideEntryForTests(url
, entry
.get());
1261 base::MessageLoop::current()->Run();
1264 TEST_F(URLFetcherBadHTTPSTest
, BadHTTPSTest
) {
1265 SpawnedTestServer::SSLOptions
ssl_options(
1266 SpawnedTestServer::SSLOptions::CERT_EXPIRED
);
1267 SpawnedTestServer
test_server(SpawnedTestServer::TYPE_HTTPS
,
1269 base::FilePath(kDocRoot
));
1270 ASSERT_TRUE(test_server
.Start());
1272 CreateFetcher(test_server
.GetURL("defaultresponse"));
1273 base::MessageLoop::current()->Run();
1276 TEST_F(URLFetcherCancelTest
, ReleasesContext
) {
1277 SpawnedTestServer
test_server(SpawnedTestServer::TYPE_HTTP
,
1278 SpawnedTestServer::kLocalhost
,
1279 base::FilePath(kDocRoot
));
1280 ASSERT_TRUE(test_server
.Start());
1282 GURL
url(test_server
.GetURL("files/server-unavailable.html"));
1284 // Create a separate thread that will create the URLFetcher. The current
1285 // (main) thread will do the IO, and when the fetch is complete it will
1286 // terminate the main thread's message loop; then the other thread's
1287 // message loop will be shut down automatically as the thread goes out of
1289 base::Thread
t("URLFetcher test thread");
1290 ASSERT_TRUE(t
.Start());
1291 t
.message_loop()->PostTask(
1293 base::Bind(&URLFetcherCancelTest::CreateFetcher
,
1294 base::Unretained(this), url
));
1296 base::MessageLoop::current()->Run();
1299 TEST_F(URLFetcherCancelTest
, CancelWhileDelayedStartTaskPending
) {
1300 SpawnedTestServer
test_server(SpawnedTestServer::TYPE_HTTP
,
1301 SpawnedTestServer::kLocalhost
,
1302 base::FilePath(kDocRoot
));
1303 ASSERT_TRUE(test_server
.Start());
1305 GURL
url(test_server
.GetURL("files/server-unavailable.html"));
1307 // Register an entry for test url.
1308 // Using a sliding window of 4 seconds, and max of 1 request, under a fast
1309 // run we expect to have a 4 second delay when posting the Start task.
1310 scoped_refptr
<URLRequestThrottlerEntry
> entry(
1311 new URLRequestThrottlerEntry(request_context()->throttler_manager(),
1319 request_context()->throttler_manager()
1320 ->OverrideEntryForTests(url
, entry
.get());
1321 // Fake that a request has just started.
1322 entry
->ReserveSendingTimeForNextRequest(base::TimeTicks());
1324 // The next request we try to send will be delayed by ~4 seconds.
1325 // The slower the test runs, the less the delay will be (since it takes the
1326 // time difference from now).
1328 base::Thread
t("URLFetcher test thread");
1329 ASSERT_TRUE(t
.Start());
1330 t
.message_loop()->PostTask(
1332 base::Bind(&URLFetcherTest::CreateFetcher
, base::Unretained(this), url
));
1334 base::MessageLoop::current()->Run();
1337 TEST_F(URLFetcherMultipleAttemptTest
, SameData
) {
1338 SpawnedTestServer
test_server(SpawnedTestServer::TYPE_HTTP
,
1339 SpawnedTestServer::kLocalhost
,
1340 base::FilePath(kDocRoot
));
1341 ASSERT_TRUE(test_server
.Start());
1343 // Create the fetcher on the main thread. Since IO will happen on the main
1344 // thread, this will test URLFetcher's ability to do everything on one
1346 CreateFetcher(test_server
.GetURL("defaultresponse"));
1348 base::MessageLoop::current()->Run();
1351 TEST_F(URLFetcherFileTest
, SmallGet
) {
1352 SpawnedTestServer
test_server(SpawnedTestServer::TYPE_HTTP
,
1353 SpawnedTestServer::kLocalhost
,
1354 base::FilePath(kDocRoot
));
1355 ASSERT_TRUE(test_server
.Start());
1357 base::ScopedTempDir temp_dir
;
1358 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
1360 // Get a small file.
1361 static const char kFileToFetch
[] = "simple.html";
1362 expected_file_
= test_server
.GetDocumentRoot().AppendASCII(kFileToFetch
);
1363 CreateFetcherForFile(
1364 test_server
.GetURL(std::string(kTestServerFilePrefix
) + kFileToFetch
),
1365 temp_dir
.path().AppendASCII(kFileToFetch
));
1367 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit().
1369 ASSERT_FALSE(base::PathExists(file_path_
))
1370 << file_path_
.value() << " not removed.";
1373 TEST_F(URLFetcherFileTest
, LargeGet
) {
1374 SpawnedTestServer
test_server(SpawnedTestServer::TYPE_HTTP
,
1375 SpawnedTestServer::kLocalhost
,
1376 base::FilePath(kDocRoot
));
1377 ASSERT_TRUE(test_server
.Start());
1379 base::ScopedTempDir temp_dir
;
1380 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
1382 // Get a file large enough to require more than one read into
1383 // URLFetcher::Core's IOBuffer.
1384 static const char kFileToFetch
[] = "animate1.gif";
1385 expected_file_
= test_server
.GetDocumentRoot().AppendASCII(kFileToFetch
);
1386 CreateFetcherForFile(
1387 test_server
.GetURL(std::string(kTestServerFilePrefix
) + kFileToFetch
),
1388 temp_dir
.path().AppendASCII(kFileToFetch
));
1390 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit().
1393 TEST_F(URLFetcherFileTest
, SavedOutputFileOwnerhisp
) {
1394 // If the caller takes the ownership of the output file, the file should
1395 // persist even after URLFetcher is gone. If not, the file must be deleted.
1396 const bool kTake
[] = {false, true};
1397 for (size_t i
= 0; i
< arraysize(kTake
); ++i
) {
1398 take_ownership_of_file_
= kTake
[i
];
1399 SpawnedTestServer
test_server(SpawnedTestServer::TYPE_HTTP
,
1400 SpawnedTestServer::kLocalhost
,
1401 base::FilePath(kDocRoot
));
1402 ASSERT_TRUE(test_server
.Start());
1404 base::ScopedTempDir temp_dir
;
1405 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
1407 // Get a small file.
1408 static const char kFileToFetch
[] = "simple.html";
1409 expected_file_
= test_server
.GetDocumentRoot().AppendASCII(kFileToFetch
);
1410 CreateFetcherForFile(
1411 test_server
.GetURL(std::string(kTestServerFilePrefix
) + kFileToFetch
),
1412 temp_dir
.path().AppendASCII(kFileToFetch
));
1414 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit().
1416 base::MessageLoop::current()->RunUntilIdle();
1417 ASSERT_EQ(kTake
[i
], base::PathExists(file_path_
)) <<
1418 "FilePath: " << file_path_
.value();
1422 TEST_F(URLFetcherFileTest
, OverwriteExistingFile
) {
1423 SpawnedTestServer
test_server(SpawnedTestServer::TYPE_HTTP
,
1424 SpawnedTestServer::kLocalhost
,
1425 base::FilePath(kDocRoot
));
1426 ASSERT_TRUE(test_server
.Start());
1428 base::ScopedTempDir temp_dir
;
1429 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
1431 // Create a file before trying to fetch.
1432 static const char kFileToFetch
[] = "simple.html";
1433 std::string
data(10000, '?'); // Meant to be larger than simple.html.
1434 file_path_
= temp_dir
.path().AppendASCII(kFileToFetch
);
1435 ASSERT_EQ(static_cast<int>(data
.size()),
1436 base::WriteFile(file_path_
, data
.data(), data
.size()));
1437 ASSERT_TRUE(base::PathExists(file_path_
));
1438 expected_file_
= test_server
.GetDocumentRoot().AppendASCII(kFileToFetch
);
1439 ASSERT_FALSE(base::ContentsEqual(file_path_
, expected_file_
));
1441 // Get a small file.
1442 CreateFetcherForFile(
1443 test_server
.GetURL(std::string(kTestServerFilePrefix
) + kFileToFetch
),
1446 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit().
1449 TEST_F(URLFetcherFileTest
, TryToOverwriteDirectory
) {
1450 SpawnedTestServer
test_server(SpawnedTestServer::TYPE_HTTP
,
1451 SpawnedTestServer::kLocalhost
,
1452 base::FilePath(kDocRoot
));
1453 ASSERT_TRUE(test_server
.Start());
1455 base::ScopedTempDir temp_dir
;
1456 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
1458 // Create a directory before trying to fetch.
1459 static const char kFileToFetch
[] = "simple.html";
1460 file_path_
= temp_dir
.path().AppendASCII(kFileToFetch
);
1461 ASSERT_TRUE(base::CreateDirectory(file_path_
));
1462 ASSERT_TRUE(base::PathExists(file_path_
));
1464 // Get a small file.
1465 expected_file_error_
= ERR_ACCESS_DENIED
;
1466 expected_file_
= test_server
.GetDocumentRoot().AppendASCII(kFileToFetch
);
1467 CreateFetcherForFile(
1468 test_server
.GetURL(std::string(kTestServerFilePrefix
) + kFileToFetch
),
1471 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit().
1473 base::MessageLoop::current()->RunUntilIdle();
1476 TEST_F(URLFetcherFileTest
, SmallGetToTempFile
) {
1477 SpawnedTestServer
test_server(SpawnedTestServer::TYPE_HTTP
,
1478 SpawnedTestServer::kLocalhost
,
1479 base::FilePath(kDocRoot
));
1480 ASSERT_TRUE(test_server
.Start());
1482 // Get a small file.
1483 static const char kFileToFetch
[] = "simple.html";
1484 expected_file_
= test_server
.GetDocumentRoot().AppendASCII(kFileToFetch
);
1485 CreateFetcherForTempFile(
1486 test_server
.GetURL(std::string(kTestServerFilePrefix
) + kFileToFetch
));
1488 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit().
1490 ASSERT_FALSE(base::PathExists(file_path_
))
1491 << file_path_
.value() << " not removed.";
1494 TEST_F(URLFetcherFileTest
, LargeGetToTempFile
) {
1495 SpawnedTestServer
test_server(SpawnedTestServer::TYPE_HTTP
,
1496 SpawnedTestServer::kLocalhost
,
1497 base::FilePath(kDocRoot
));
1498 ASSERT_TRUE(test_server
.Start());
1500 // Get a file large enough to require more than one read into
1501 // URLFetcher::Core's IOBuffer.
1502 static const char kFileToFetch
[] = "animate1.gif";
1503 expected_file_
= test_server
.GetDocumentRoot().AppendASCII(kFileToFetch
);
1504 CreateFetcherForTempFile(test_server
.GetURL(
1505 std::string(kTestServerFilePrefix
) + kFileToFetch
));
1507 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit().
1510 TEST_F(URLFetcherFileTest
, SavedOutputTempFileOwnerhisp
) {
1511 // If the caller takes the ownership of the temp file, the file should persist
1512 // even after URLFetcher is gone. If not, the file must be deleted.
1513 const bool kTake
[] = {false, true};
1514 for (size_t i
= 0; i
< arraysize(kTake
); ++i
) {
1515 take_ownership_of_file_
= kTake
[i
];
1517 SpawnedTestServer
test_server(SpawnedTestServer::TYPE_HTTP
,
1518 SpawnedTestServer::kLocalhost
,
1519 base::FilePath(kDocRoot
));
1520 ASSERT_TRUE(test_server
.Start());
1522 // Get a small file.
1523 static const char kFileToFetch
[] = "simple.html";
1524 expected_file_
= test_server
.GetDocumentRoot().AppendASCII(kFileToFetch
);
1525 CreateFetcherForTempFile(test_server
.GetURL(
1526 std::string(kTestServerFilePrefix
) + kFileToFetch
));
1528 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit().
1530 base::MessageLoop::current()->RunUntilIdle();
1531 ASSERT_EQ(kTake
[i
], base::PathExists(file_path_
)) <<
1532 "FilePath: " << file_path_
.value();