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/strings/stringprintf.h"
16 #include "base/synchronization/waitable_event.h"
17 #include "base/threading/thread.h"
18 #include "build/build_config.h"
19 #include "crypto/nss_util.h"
20 #include "net/base/elements_upload_data_stream.h"
21 #include "net/base/network_change_notifier.h"
22 #include "net/base/upload_bytes_element_reader.h"
23 #include "net/base/upload_element_reader.h"
24 #include "net/base/upload_file_element_reader.h"
25 #include "net/dns/mock_host_resolver.h"
26 #include "net/http/http_response_headers.h"
27 #include "net/test/spawned_test_server/spawned_test_server.h"
28 #include "net/url_request/url_fetcher_delegate.h"
29 #include "net/url_request/url_request_context_getter.h"
30 #include "net/url_request/url_request_test_util.h"
31 #include "net/url_request/url_request_throttler_manager.h"
32 #include "testing/gtest/include/gtest/gtest.h"
34 #if defined(USE_NSS) || defined(OS_IOS)
35 #include "net/ocsp/nss_ocsp.h"
41 using base::TimeDelta
;
43 // TODO(eroman): Add a regression test for http://crbug.com/40505.
47 // TODO(akalin): Move all the test data to somewhere under net/.
48 const base::FilePath::CharType kDocRoot
[] =
49 FILE_PATH_LITERAL("net/data/url_fetcher_impl_unittest");
50 const char kTestServerFilePrefix
[] = "files/";
52 class ThrottlingTestURLRequestContext
: public TestURLRequestContext
{
54 ThrottlingTestURLRequestContext() : TestURLRequestContext(true) {
55 set_throttler_manager(&throttler_manager_
);
57 DCHECK(throttler_manager() != NULL
);
61 URLRequestThrottlerManager throttler_manager_
;
64 class ThrottlingTestURLRequestContextGetter
65 : public TestURLRequestContextGetter
{
67 ThrottlingTestURLRequestContextGetter(
68 base::MessageLoopProxy
* io_message_loop_proxy
,
69 TestURLRequestContext
* request_context
)
70 : TestURLRequestContextGetter(io_message_loop_proxy
),
71 context_(request_context
) {
74 // TestURLRequestContextGetter:
75 TestURLRequestContext
* GetURLRequestContext() override
{ return context_
; }
78 ~ThrottlingTestURLRequestContextGetter() override
{}
80 TestURLRequestContext
* const context_
;
85 class URLFetcherTest
: public testing::Test
,
86 public URLFetcherDelegate
{
88 URLFetcherTest() : fetcher_(NULL
), expected_status_code_(200) {}
90 static int GetNumFetcherCores() {
91 return URLFetcherImpl::GetNumFetcherCores();
94 // Creates a URLFetcher, using the program's main thread to do IO.
95 virtual void CreateFetcher(const GURL
& url
);
97 // URLFetcherDelegate:
98 // Subclasses that override this should either call this function or
99 // CleanupAfterFetchComplete() at the end of their processing, depending on
100 // whether they want to check for a non-empty HTTP 200 response or not.
101 void OnURLFetchComplete(const URLFetcher
* source
) override
;
103 // Deletes |fetcher| and terminates the message loop.
104 void CleanupAfterFetchComplete();
106 scoped_refptr
<base::MessageLoopProxy
> io_message_loop_proxy() {
107 return io_message_loop_proxy_
;
110 TestURLRequestContext
* request_context() {
111 return context_
.get();
116 void SetUp() override
{
117 testing::Test::SetUp();
119 context_
.reset(new ThrottlingTestURLRequestContext());
120 io_message_loop_proxy_
= base::MessageLoopProxy::current();
122 #if defined(USE_NSS) || defined(OS_IOS)
123 crypto::EnsureNSSInit();
124 EnsureNSSHttpIOInit();
128 void TearDown() override
{
129 #if defined(USE_NSS) || defined(OS_IOS)
134 // URLFetcher is designed to run on the main UI thread, but in our tests
135 // we assume that the current thread is the IO thread where the URLFetcher
136 // dispatches its requests to. When we wish to simulate being used from
137 // a UI thread, we dispatch a worker thread to do so.
138 scoped_refptr
<base::MessageLoopProxy
> io_message_loop_proxy_
;
140 URLFetcherImpl
* fetcher_
;
141 scoped_ptr
<TestURLRequestContext
> context_
;
142 int expected_status_code_
;
145 // A test fixture that uses a MockHostResolver, so that name resolutions can
146 // be manipulated by the tests to keep connections in the resolving state.
147 class URLFetcherMockDnsTest
: public URLFetcherTest
{
150 void SetUp() override
;
153 void CreateFetcher(const GURL
& url
) override
;
155 // URLFetcherDelegate:
156 void OnURLFetchComplete(const URLFetcher
* source
) override
;
160 scoped_ptr
<SpawnedTestServer
> test_server_
;
161 MockHostResolver resolver_
;
162 scoped_ptr
<URLFetcher
> completed_fetcher_
;
165 void URLFetcherTest::CreateFetcher(const GURL
& url
) {
166 fetcher_
= new URLFetcherImpl(url
, URLFetcher::GET
, this);
167 fetcher_
->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
168 io_message_loop_proxy().get(), request_context()));
172 void URLFetcherTest::OnURLFetchComplete(const URLFetcher
* source
) {
173 EXPECT_TRUE(source
->GetStatus().is_success());
174 EXPECT_EQ(expected_status_code_
, source
->GetResponseCode()); // HTTP OK
177 EXPECT_TRUE(source
->GetResponseAsString(&data
));
178 EXPECT_FALSE(data
.empty());
180 CleanupAfterFetchComplete();
183 void URLFetcherTest::CleanupAfterFetchComplete() {
184 delete fetcher_
; // Have to delete this here and not in the destructor,
185 // because the destructor won't necessarily run on the
186 // same thread that CreateFetcher() did.
188 io_message_loop_proxy()->PostTask(FROM_HERE
,
189 base::MessageLoop::QuitClosure());
190 // If the current message loop is not the IO loop, it will be shut down when
191 // the main loop returns and this thread subsequently goes out of scope.
194 void URLFetcherMockDnsTest::SetUp() {
195 URLFetcherTest::SetUp();
197 resolver_
.set_ondemand_mode(true);
198 resolver_
.rules()->AddRule("example.com", "127.0.0.1");
200 context_
.reset(new TestURLRequestContext(true));
201 context_
->set_host_resolver(&resolver_
);
204 test_server_
.reset(new SpawnedTestServer(SpawnedTestServer::TYPE_HTTP
,
205 SpawnedTestServer::kLocalhost
,
206 base::FilePath(kDocRoot
)));
207 ASSERT_TRUE(test_server_
->Start());
209 // test_server_.GetURL() returns a URL with 127.0.0.1 (kLocalhost), that is
210 // immediately resolved by the MockHostResolver. Use a hostname instead to
211 // trigger an async resolve.
213 base::StringPrintf("http://example.com:%d/defaultresponse",
214 test_server_
->host_port_pair().port()));
215 ASSERT_TRUE(test_url_
.is_valid());
218 void URLFetcherMockDnsTest::CreateFetcher(const GURL
& url
) {
219 fetcher_
= new URLFetcherImpl(url
, URLFetcher::GET
, this);
220 fetcher_
->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
221 io_message_loop_proxy().get(), request_context()));
224 void URLFetcherMockDnsTest::OnURLFetchComplete(const URLFetcher
* source
) {
225 io_message_loop_proxy()->PostTask(FROM_HERE
,
226 base::MessageLoop::QuitClosure());
227 ASSERT_EQ(fetcher_
, source
);
228 EXPECT_EQ(test_url_
, source
->GetOriginalURL());
229 completed_fetcher_
.reset(fetcher_
);
234 // Version of URLFetcherTest that does a POST instead
235 class URLFetcherPostTest
: public URLFetcherTest
{
238 void CreateFetcher(const GURL
& url
) override
;
240 // URLFetcherDelegate:
241 void OnURLFetchComplete(const URLFetcher
* source
) override
;
244 // Version of URLFetcherTest that does a POST of a file using
245 // SetUploadDataStream
246 class URLFetcherPostFileTest
: public URLFetcherTest
{
248 URLFetcherPostFileTest();
250 void SetUploadRange(uint64 range_offset
, uint64 range_length
) {
251 range_offset_
= range_offset
;
252 range_length_
= range_length
;
256 void CreateFetcher(const GURL
& url
) override
;
258 // URLFetcherDelegate:
259 void OnURLFetchComplete(const URLFetcher
* source
) override
;
262 base::FilePath path_
;
263 uint64 range_offset_
;
264 uint64 range_length_
;
267 class URLFetcherSetUploadFactoryTest
: public URLFetcherTest
{
269 URLFetcherSetUploadFactoryTest() : create_stream_count_(0) {}
272 void CreateFetcher(const GURL
& url
) override
;
274 // URLFetcherDelegate:
275 void OnURLFetchComplete(const URLFetcher
* source
) override
;
277 // Callback passed to URLFetcher to create upload stream.
278 scoped_ptr
<UploadDataStream
> CreateUploadStream() {
279 ++create_stream_count_
;
280 const std::string
str("bobsyeruncle\n");
281 std::vector
<char> buffer(str
.begin(), str
.end());
282 return ElementsUploadDataStream::CreateWithReader(
283 scoped_ptr
<UploadElementReader
>(
284 new UploadOwnedBytesElementReader(&buffer
)),
288 size_t create_stream_count() const { return create_stream_count_
; }
291 // Count of calling CreateStream.
292 size_t create_stream_count_
;
295 // Version of URLFetcherTest that does a POST instead with empty upload body
296 class URLFetcherEmptyPostTest
: public URLFetcherTest
{
299 void CreateFetcher(const GURL
& url
) override
;
301 // URLFetcherDelegate:
302 void OnURLFetchComplete(const URLFetcher
* source
) override
;
305 // Version of URLFetcherTest that tests download progress reports.
306 class URLFetcherDownloadProgressTest
: public URLFetcherTest
{
308 URLFetcherDownloadProgressTest()
309 : previous_progress_(0),
314 void CreateFetcher(const GURL
& url
) override
;
316 // URLFetcherDelegate:
317 void OnURLFetchDownloadProgress(const URLFetcher
* source
,
319 int64 total
) override
;
322 // Download progress returned by the previous callback.
323 int64 previous_progress_
;
324 // Size of the file being downloaded, known in advance (provided by each test
326 int64 expected_total_
;
329 // Version of URLFetcherTest that tests progress reports at cancellation.
330 class URLFetcherDownloadProgressCancelTest
: public URLFetcherTest
{
333 void CreateFetcher(const GURL
& url
) override
;
335 // URLFetcherDelegate:
336 void OnURLFetchComplete(const URLFetcher
* source
) override
;
337 void OnURLFetchDownloadProgress(const URLFetcher
* source
,
339 int64 total
) override
;
345 // Version of URLFetcherTest that tests upload progress reports.
346 class URLFetcherUploadProgressTest
: public URLFetcherTest
{
349 void CreateFetcher(const GURL
& url
) override
;
351 // URLFetcherDelegate:
352 void OnURLFetchUploadProgress(const URLFetcher
* source
,
354 int64 total
) override
;
357 int64 previous_progress_
;
359 int64 number_of_chunks_added_
;
362 // Version of URLFetcherTest that tests headers.
363 class URLFetcherHeadersTest
: public URLFetcherTest
{
365 // URLFetcherDelegate:
366 void OnURLFetchComplete(const URLFetcher
* source
) override
;
369 // Version of URLFetcherTest that tests SocketAddress.
370 class URLFetcherSocketAddressTest
: public URLFetcherTest
{
372 // URLFetcherDelegate:
373 void OnURLFetchComplete(const URLFetcher
* source
) override
;
376 std::string expected_host_
;
377 uint16 expected_port_
;
380 // Version of URLFetcherTest that tests stopping on a redirect.
381 class URLFetcherStopOnRedirectTest
: public URLFetcherTest
{
383 URLFetcherStopOnRedirectTest();
384 ~URLFetcherStopOnRedirectTest() override
;
387 void CreateFetcher(const GURL
& url
) override
;
389 // URLFetcherDelegate:
390 void OnURLFetchComplete(const URLFetcher
* source
) override
;
393 // The URL we should be redirected to.
394 static const char* kRedirectTarget
;
396 bool callback_called_
; // Set to true in OnURLFetchComplete().
399 // Version of URLFetcherTest that tests overload protection.
400 class URLFetcherProtectTest
: public URLFetcherTest
{
403 void CreateFetcher(const GURL
& url
) override
;
405 // URLFetcherDelegate:
406 void OnURLFetchComplete(const URLFetcher
* source
) override
;
412 // Version of URLFetcherTest that tests overload protection, when responses
414 class URLFetcherProtectTestPassedThrough
: public URLFetcherTest
{
417 void CreateFetcher(const GURL
& url
) override
;
419 // URLFetcherDelegate:
420 void OnURLFetchComplete(const URLFetcher
* source
) override
;
426 // Version of URLFetcherTest that tests bad HTTPS requests.
427 class URLFetcherBadHTTPSTest
: public URLFetcherTest
{
429 URLFetcherBadHTTPSTest();
431 // URLFetcherDelegate:
432 void OnURLFetchComplete(const URLFetcher
* source
) override
;
435 base::FilePath cert_dir_
;
438 // Version of URLFetcherTest that tests request cancellation on shutdown.
439 class URLFetcherCancelTest
: public URLFetcherTest
{
442 void CreateFetcher(const GURL
& url
) override
;
444 // URLFetcherDelegate:
445 void OnURLFetchComplete(const URLFetcher
* source
) override
;
447 void CancelRequest();
450 // Version of TestURLRequestContext that posts a Quit task to the IO
451 // thread once it is deleted.
452 class CancelTestURLRequestContext
: public ThrottlingTestURLRequestContext
{
454 explicit CancelTestURLRequestContext() {
458 ~CancelTestURLRequestContext() override
{
459 // The d'tor should execute in the IO thread. Post the quit task to the
461 base::MessageLoop::current()->PostTask(FROM_HERE
,
462 base::MessageLoop::QuitClosure());
466 class CancelTestURLRequestContextGetter
467 : public TestURLRequestContextGetter
{
469 CancelTestURLRequestContextGetter(
470 base::MessageLoopProxy
* io_message_loop_proxy
,
471 const GURL
& throttle_for_url
)
472 : TestURLRequestContextGetter(io_message_loop_proxy
),
473 io_message_loop_proxy_(io_message_loop_proxy
),
474 context_created_(false, false),
475 throttle_for_url_(throttle_for_url
) {
478 // TestURLRequestContextGetter:
479 TestURLRequestContext
* GetURLRequestContext() override
{
480 if (!context_
.get()) {
481 context_
.reset(new CancelTestURLRequestContext());
482 DCHECK(context_
->throttler_manager());
484 // Registers an entry for test url. The backoff time is calculated by:
485 // new_backoff = 2.0 * old_backoff + 0
486 // The initial backoff is 2 seconds and maximum backoff is 4 seconds.
487 // Maximum retries allowed is set to 2.
488 scoped_refptr
<URLRequestThrottlerEntry
> entry(
489 new URLRequestThrottlerEntry(context_
->throttler_manager(),
497 context_
->throttler_manager()
498 ->OverrideEntryForTests(throttle_for_url_
, entry
.get());
500 context_created_
.Signal();
502 return context_
.get();
505 virtual scoped_refptr
<base::MessageLoopProxy
> GetIOMessageLoopProxy() const {
506 return io_message_loop_proxy_
;
509 void WaitForContextCreation() {
510 context_created_
.Wait();
514 ~CancelTestURLRequestContextGetter() override
{}
517 scoped_ptr
<TestURLRequestContext
> context_
;
518 scoped_refptr
<base::MessageLoopProxy
> io_message_loop_proxy_
;
519 base::WaitableEvent context_created_
;
520 GURL throttle_for_url_
;
523 // Version of URLFetcherTest that tests retying the same request twice.
524 class URLFetcherMultipleAttemptTest
: public URLFetcherTest
{
526 // URLFetcherDelegate:
527 void OnURLFetchComplete(const URLFetcher
* source
) override
;
533 class URLFetcherFileTest
: public URLFetcherTest
{
535 URLFetcherFileTest() : take_ownership_of_file_(false),
536 expected_file_error_(OK
) {}
538 void CreateFetcherForFile(const GURL
& url
, const base::FilePath
& file_path
);
539 void CreateFetcherForTempFile(const GURL
& url
);
541 // URLFetcherDelegate:
542 void OnURLFetchComplete(const URLFetcher
* source
) override
;
545 base::FilePath expected_file_
;
546 base::FilePath file_path_
;
548 // Set by the test. Used in OnURLFetchComplete() to decide if
549 // the URLFetcher should own the temp file, so that we can test
550 // disowning prevents the file from being deleted.
551 bool take_ownership_of_file_
;
553 // Expected file error code for the test. OK when expecting success.
554 int expected_file_error_
;
557 void URLFetcherPostTest::CreateFetcher(const GURL
& url
) {
558 fetcher_
= new URLFetcherImpl(url
, URLFetcher::POST
, this);
559 fetcher_
->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
560 io_message_loop_proxy().get(), request_context()));
561 fetcher_
->SetUploadData("application/x-www-form-urlencoded", "bobsyeruncle");
565 void URLFetcherPostTest::OnURLFetchComplete(const URLFetcher
* source
) {
567 EXPECT_TRUE(source
->GetResponseAsString(&data
));
568 EXPECT_EQ(std::string("bobsyeruncle"), data
);
569 URLFetcherTest::OnURLFetchComplete(source
);
572 URLFetcherPostFileTest::URLFetcherPostFileTest()
574 range_length_(kuint64max
) {
575 PathService::Get(base::DIR_SOURCE_ROOT
, &path_
);
576 path_
= path_
.Append(FILE_PATH_LITERAL("net"));
577 path_
= path_
.Append(FILE_PATH_LITERAL("data"));
578 path_
= path_
.Append(FILE_PATH_LITERAL("url_request_unittest"));
579 path_
= path_
.Append(FILE_PATH_LITERAL("BullRunSpeech.txt"));
582 void URLFetcherPostFileTest::CreateFetcher(const GURL
& url
) {
583 fetcher_
= new URLFetcherImpl(url
, URLFetcher::POST
, this);
584 fetcher_
->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
585 io_message_loop_proxy().get(), request_context()));
586 fetcher_
->SetUploadFilePath("application/x-www-form-urlencoded",
590 base::MessageLoopProxy::current());
594 void URLFetcherPostFileTest::OnURLFetchComplete(const URLFetcher
* source
) {
595 std::string expected
;
596 ASSERT_TRUE(base::ReadFileToString(path_
, &expected
));
597 ASSERT_LE(range_offset_
, expected
.size());
598 uint64 expected_size
=
599 std::min(range_length_
, expected
.size() - range_offset_
);
602 EXPECT_TRUE(source
->GetResponseAsString(&data
));
603 EXPECT_EQ(expected
.substr(range_offset_
, expected_size
), data
);
604 URLFetcherTest::OnURLFetchComplete(source
);
607 void URLFetcherSetUploadFactoryTest::CreateFetcher(const GURL
& url
) {
608 fetcher_
= new URLFetcherImpl(url
, URLFetcher::POST
, this);
609 fetcher_
->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
610 io_message_loop_proxy().get(), request_context()));
611 fetcher_
->SetUploadStreamFactory(
613 base::Bind(&URLFetcherSetUploadFactoryTest::CreateUploadStream
,
614 base::Unretained(this)));
615 fetcher_
->SetAutomaticallyRetryOn5xx(true);
616 fetcher_
->SetMaxRetriesOn5xx(1);
620 void URLFetcherSetUploadFactoryTest::OnURLFetchComplete(
621 const URLFetcher
* source
) {
623 EXPECT_TRUE(source
->GetResponseAsString(&data
));
624 EXPECT_EQ("bobsyeruncle\n", data
);
625 URLFetcherTest::OnURLFetchComplete(source
);
628 void URLFetcherEmptyPostTest::CreateFetcher(const GURL
& url
) {
629 fetcher_
= new URLFetcherImpl(url
, URLFetcher::POST
, this);
630 fetcher_
->SetRequestContext(new TestURLRequestContextGetter(
631 io_message_loop_proxy()));
632 fetcher_
->SetUploadData("text/plain", std::string());
636 void URLFetcherEmptyPostTest::OnURLFetchComplete(const URLFetcher
* source
) {
637 EXPECT_TRUE(source
->GetStatus().is_success());
638 EXPECT_EQ(200, source
->GetResponseCode()); // HTTP OK
641 EXPECT_TRUE(source
->GetResponseAsString(&data
));
642 EXPECT_TRUE(data
.empty());
644 CleanupAfterFetchComplete();
645 // Do not call the super class method URLFetcherTest::OnURLFetchComplete,
646 // since it expects a non-empty response.
649 void URLFetcherDownloadProgressTest::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()));
656 void URLFetcherDownloadProgressTest::OnURLFetchDownloadProgress(
657 const URLFetcher
* source
, int64 progress
, int64 total
) {
658 // Increasing between 0 and total.
659 EXPECT_LE(0, progress
);
660 EXPECT_GE(total
, progress
);
661 EXPECT_LE(previous_progress_
, progress
);
662 EXPECT_EQ(expected_total_
, total
);
663 previous_progress_
= progress
;
666 void URLFetcherDownloadProgressCancelTest::CreateFetcher(const GURL
& url
) {
667 fetcher_
= new URLFetcherImpl(url
, URLFetcher::GET
, this);
668 fetcher_
->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
669 io_message_loop_proxy().get(), request_context()));
674 void URLFetcherDownloadProgressCancelTest::OnURLFetchDownloadProgress(
675 const URLFetcher
* source
, int64 current
, int64 total
) {
676 EXPECT_FALSE(cancelled_
);
679 CleanupAfterFetchComplete();
683 void URLFetcherDownloadProgressCancelTest::OnURLFetchComplete(
684 const URLFetcher
* source
) {
685 // Should have been cancelled.
687 CleanupAfterFetchComplete();
690 void URLFetcherUploadProgressTest::CreateFetcher(const GURL
& url
) {
691 fetcher_
= new URLFetcherImpl(url
, URLFetcher::POST
, this);
692 fetcher_
->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
693 io_message_loop_proxy().get(), request_context()));
694 previous_progress_
= 0;
695 // Large enough data to require more than one read from UploadDataStream.
696 chunk_
.assign(1<<16, 'a');
697 // Use chunked upload to wait for a timer event of progress notification.
698 fetcher_
->SetChunkedUpload("application/x-www-form-urlencoded");
700 number_of_chunks_added_
= 1;
701 fetcher_
->AppendChunkToUpload(chunk_
, false);
704 void URLFetcherUploadProgressTest::OnURLFetchUploadProgress(
705 const URLFetcher
* source
, int64 current
, int64 total
) {
706 // Increasing between 0 and total.
707 EXPECT_LE(0, current
);
708 EXPECT_GE(static_cast<int64
>(chunk_
.size()) * number_of_chunks_added_
,
710 EXPECT_LE(previous_progress_
, current
);
711 previous_progress_
= current
;
712 EXPECT_EQ(-1, total
);
714 if (number_of_chunks_added_
< 2) {
715 number_of_chunks_added_
+= 1;
716 fetcher_
->AppendChunkToUpload(chunk_
, true);
720 void URLFetcherHeadersTest::OnURLFetchComplete(
721 const URLFetcher
* source
) {
723 EXPECT_TRUE(source
->GetResponseHeaders()->GetNormalizedHeader("cache-control",
725 EXPECT_EQ("private", header
);
726 URLFetcherTest::OnURLFetchComplete(source
);
729 void URLFetcherSocketAddressTest::OnURLFetchComplete(
730 const URLFetcher
* source
) {
731 EXPECT_EQ("127.0.0.1", source
->GetSocketAddress().host());
732 EXPECT_EQ(expected_port_
, source
->GetSocketAddress().port());
733 URLFetcherTest::OnURLFetchComplete(source
);
737 const char* URLFetcherStopOnRedirectTest::kRedirectTarget
=
738 "http://redirect.target.com";
740 URLFetcherStopOnRedirectTest::URLFetcherStopOnRedirectTest()
741 : callback_called_(false) {
744 URLFetcherStopOnRedirectTest::~URLFetcherStopOnRedirectTest() {
747 void URLFetcherStopOnRedirectTest::CreateFetcher(const GURL
& url
) {
748 fetcher_
= new URLFetcherImpl(url
, URLFetcher::GET
, this);
749 fetcher_
->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
750 io_message_loop_proxy().get(), request_context()));
751 fetcher_
->SetStopOnRedirect(true);
755 void URLFetcherStopOnRedirectTest::OnURLFetchComplete(
756 const URLFetcher
* source
) {
757 callback_called_
= true;
758 EXPECT_EQ(GURL(kRedirectTarget
), source
->GetURL());
759 EXPECT_EQ(URLRequestStatus::CANCELED
, source
->GetStatus().status());
760 EXPECT_EQ(ERR_ABORTED
, source
->GetStatus().error());
761 EXPECT_EQ(301, source
->GetResponseCode());
762 CleanupAfterFetchComplete();
765 void URLFetcherProtectTest::CreateFetcher(const GURL
& url
) {
766 fetcher_
= new URLFetcherImpl(url
, URLFetcher::GET
, this);
767 fetcher_
->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
768 io_message_loop_proxy().get(), request_context()));
769 start_time_
= Time::Now();
770 fetcher_
->SetMaxRetriesOn5xx(11);
774 void URLFetcherProtectTest::OnURLFetchComplete(const URLFetcher
* source
) {
775 const TimeDelta one_second
= TimeDelta::FromMilliseconds(1000);
776 if (source
->GetResponseCode() >= 500) {
777 // Now running ServerUnavailable test.
778 // It takes more than 1 second to finish all 11 requests.
779 EXPECT_TRUE(Time::Now() - start_time_
>= one_second
);
780 EXPECT_TRUE(source
->GetStatus().is_success());
782 EXPECT_TRUE(source
->GetResponseAsString(&data
));
783 EXPECT_FALSE(data
.empty());
784 CleanupAfterFetchComplete();
786 // Now running Overload test.
787 static int count
= 0;
790 fetcher_
->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
791 io_message_loop_proxy().get(), request_context()));
794 // We have already sent 20 requests continuously. And we expect that
795 // it takes more than 1 second due to the overload protection settings.
796 EXPECT_TRUE(Time::Now() - start_time_
>= one_second
);
797 URLFetcherTest::OnURLFetchComplete(source
);
802 void URLFetcherProtectTestPassedThrough::CreateFetcher(const GURL
& url
) {
803 fetcher_
= new URLFetcherImpl(url
, URLFetcher::GET
, this);
804 fetcher_
->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
805 io_message_loop_proxy().get(), request_context()));
806 fetcher_
->SetAutomaticallyRetryOn5xx(false);
807 start_time_
= Time::Now();
808 fetcher_
->SetMaxRetriesOn5xx(11);
812 void URLFetcherProtectTestPassedThrough::OnURLFetchComplete(
813 const URLFetcher
* source
) {
814 const TimeDelta one_minute
= TimeDelta::FromMilliseconds(60000);
815 if (source
->GetResponseCode() >= 500) {
816 // Now running ServerUnavailable test.
817 // It should get here on the first attempt, so almost immediately and
818 // *not* to attempt to execute all 11 requests (2.5 minutes).
819 EXPECT_TRUE(Time::Now() - start_time_
< one_minute
);
820 EXPECT_TRUE(source
->GetStatus().is_success());
821 // Check that suggested back off time is bigger than 0.
822 EXPECT_GT(fetcher_
->GetBackoffDelay().InMicroseconds(), 0);
824 EXPECT_TRUE(source
->GetResponseAsString(&data
));
825 EXPECT_FALSE(data
.empty());
827 // We should not get here!
831 CleanupAfterFetchComplete();
835 URLFetcherBadHTTPSTest::URLFetcherBadHTTPSTest() {
836 PathService::Get(base::DIR_SOURCE_ROOT
, &cert_dir_
);
837 cert_dir_
= cert_dir_
.AppendASCII("chrome");
838 cert_dir_
= cert_dir_
.AppendASCII("test");
839 cert_dir_
= cert_dir_
.AppendASCII("data");
840 cert_dir_
= cert_dir_
.AppendASCII("ssl");
841 cert_dir_
= cert_dir_
.AppendASCII("certificates");
844 // The "server certificate expired" error should result in automatic
845 // cancellation of the request by
846 // URLRequest::Delegate::OnSSLCertificateError.
847 void URLFetcherBadHTTPSTest::OnURLFetchComplete(
848 const URLFetcher
* source
) {
849 // This part is different from URLFetcherTest::OnURLFetchComplete
850 // because this test expects the request to be cancelled.
851 EXPECT_EQ(URLRequestStatus::CANCELED
, source
->GetStatus().status());
852 EXPECT_EQ(ERR_ABORTED
, source
->GetStatus().error());
853 EXPECT_EQ(-1, source
->GetResponseCode());
854 EXPECT_TRUE(source
->GetCookies().empty());
856 EXPECT_TRUE(source
->GetResponseAsString(&data
));
857 EXPECT_TRUE(data
.empty());
858 CleanupAfterFetchComplete();
861 void URLFetcherCancelTest::CreateFetcher(const GURL
& url
) {
862 fetcher_
= new URLFetcherImpl(url
, URLFetcher::GET
, this);
863 CancelTestURLRequestContextGetter
* context_getter
=
864 new CancelTestURLRequestContextGetter(io_message_loop_proxy().get(), url
);
865 fetcher_
->SetRequestContext(context_getter
);
866 fetcher_
->SetMaxRetriesOn5xx(2);
868 // We need to wait for the creation of the URLRequestContext, since we
869 // rely on it being destroyed as a signal to end the test.
870 context_getter
->WaitForContextCreation();
874 void URLFetcherCancelTest::OnURLFetchComplete(
875 const URLFetcher
* source
) {
876 // We should have cancelled the request before completion.
878 CleanupAfterFetchComplete();
881 void URLFetcherCancelTest::CancelRequest() {
883 // The URLFetcher's test context will post a Quit task once it is
884 // deleted. So if this test simply hangs, it means cancellation
888 void URLFetcherMultipleAttemptTest::OnURLFetchComplete(
889 const URLFetcher
* source
) {
890 EXPECT_TRUE(source
->GetStatus().is_success());
891 EXPECT_EQ(200, source
->GetResponseCode()); // HTTP OK
893 EXPECT_TRUE(source
->GetResponseAsString(&data
));
894 EXPECT_FALSE(data
.empty());
895 if (!data
.empty() && data_
.empty()) {
897 fetcher_
->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
898 io_message_loop_proxy().get(), request_context()));
901 EXPECT_EQ(data
, data_
);
902 CleanupAfterFetchComplete();
906 void URLFetcherFileTest::CreateFetcherForFile(const GURL
& url
,
907 const base::FilePath
& file_path
) {
908 fetcher_
= new URLFetcherImpl(url
, URLFetcher::GET
, this);
909 fetcher_
->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
910 io_message_loop_proxy().get(), request_context()));
912 // Use the IO message loop to do the file operations in this test.
913 fetcher_
->SaveResponseToFileAtPath(file_path
, io_message_loop_proxy());
917 void URLFetcherFileTest::CreateFetcherForTempFile(const GURL
& url
) {
918 fetcher_
= new URLFetcherImpl(url
, URLFetcher::GET
, this);
919 fetcher_
->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
920 io_message_loop_proxy().get(), request_context()));
922 // Use the IO message loop to do the file operations in this test.
923 fetcher_
->SaveResponseToTemporaryFile(io_message_loop_proxy());
927 void URLFetcherFileTest::OnURLFetchComplete(const URLFetcher
* source
) {
928 if (expected_file_error_
== OK
) {
929 EXPECT_TRUE(source
->GetStatus().is_success());
930 EXPECT_EQ(OK
, source
->GetStatus().error());
931 EXPECT_EQ(200, source
->GetResponseCode());
933 EXPECT_TRUE(source
->GetResponseAsFilePath(
934 take_ownership_of_file_
, &file_path_
));
936 EXPECT_TRUE(base::ContentsEqual(expected_file_
, file_path_
));
938 EXPECT_FALSE(source
->GetStatus().is_success());
939 EXPECT_EQ(expected_file_error_
, source
->GetStatus().error());
941 CleanupAfterFetchComplete();
944 TEST_F(URLFetcherTest
, SameThreadsTest
) {
945 SpawnedTestServer
test_server(SpawnedTestServer::TYPE_HTTP
,
946 SpawnedTestServer::kLocalhost
,
947 base::FilePath(kDocRoot
));
948 ASSERT_TRUE(test_server
.Start());
950 // Create the fetcher on the main thread. Since IO will happen on the main
951 // thread, this will test URLFetcher's ability to do everything on one
953 CreateFetcher(test_server
.GetURL("defaultresponse"));
955 base::MessageLoop::current()->Run();
958 TEST_F(URLFetcherTest
, DifferentThreadsTest
) {
959 SpawnedTestServer
test_server(SpawnedTestServer::TYPE_HTTP
,
960 SpawnedTestServer::kLocalhost
,
961 base::FilePath(kDocRoot
));
962 ASSERT_TRUE(test_server
.Start());
964 // Create a separate thread that will create the URLFetcher. The current
965 // (main) thread will do the IO, and when the fetch is complete it will
966 // terminate the main thread's message loop; then the other thread's
967 // message loop will be shut down automatically as the thread goes out of
969 base::Thread
t("URLFetcher test thread");
970 ASSERT_TRUE(t
.Start());
971 t
.message_loop()->PostTask(
973 base::Bind(&URLFetcherTest::CreateFetcher
,
974 base::Unretained(this),
975 test_server
.GetURL("defaultresponse")));
977 base::MessageLoop::current()->Run();
980 void CancelAllOnIO() {
981 EXPECT_EQ(1, URLFetcherTest::GetNumFetcherCores());
982 URLFetcherImpl::CancelAll();
983 EXPECT_EQ(0, URLFetcherTest::GetNumFetcherCores());
986 // Tests to make sure CancelAll() will successfully cancel existing URLFetchers.
987 TEST_F(URLFetcherTest
, CancelAll
) {
988 SpawnedTestServer
test_server(SpawnedTestServer::TYPE_HTTP
,
989 SpawnedTestServer::kLocalhost
,
990 base::FilePath(kDocRoot
));
991 ASSERT_TRUE(test_server
.Start());
992 EXPECT_EQ(0, GetNumFetcherCores());
994 CreateFetcher(test_server
.GetURL("defaultresponse"));
995 io_message_loop_proxy()->PostTaskAndReply(
996 FROM_HERE
, base::Bind(&CancelAllOnIO
), base::MessageLoop::QuitClosure());
997 base::MessageLoop::current()->Run();
998 EXPECT_EQ(0, GetNumFetcherCores());
1002 TEST_F(URLFetcherMockDnsTest
, DontRetryOnNetworkChangedByDefault
) {
1003 EXPECT_EQ(0, GetNumFetcherCores());
1004 EXPECT_FALSE(resolver_
.has_pending_requests());
1006 // This posts a task to start the fetcher.
1007 CreateFetcher(test_url_
);
1009 EXPECT_EQ(0, GetNumFetcherCores());
1010 base::MessageLoop::current()->RunUntilIdle();
1012 // The fetcher is now running, but is pending the host resolve.
1013 EXPECT_EQ(1, GetNumFetcherCores());
1014 EXPECT_TRUE(resolver_
.has_pending_requests());
1015 ASSERT_FALSE(completed_fetcher_
);
1017 // A network change notification aborts the connect job.
1018 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
1019 base::MessageLoop::current()->RunUntilIdle();
1020 EXPECT_EQ(0, GetNumFetcherCores());
1021 EXPECT_FALSE(resolver_
.has_pending_requests());
1022 ASSERT_TRUE(completed_fetcher_
);
1024 // And the owner of the fetcher gets the ERR_NETWORK_CHANGED error.
1025 EXPECT_EQ(ERR_NETWORK_CHANGED
, completed_fetcher_
->GetStatus().error());
1028 TEST_F(URLFetcherMockDnsTest
, RetryOnNetworkChangedAndFail
) {
1029 EXPECT_EQ(0, GetNumFetcherCores());
1030 EXPECT_FALSE(resolver_
.has_pending_requests());
1032 // This posts a task to start the fetcher.
1033 CreateFetcher(test_url_
);
1034 fetcher_
->SetAutomaticallyRetryOnNetworkChanges(3);
1036 EXPECT_EQ(0, GetNumFetcherCores());
1037 base::MessageLoop::current()->RunUntilIdle();
1039 // The fetcher is now running, but is pending the host resolve.
1040 EXPECT_EQ(1, GetNumFetcherCores());
1041 EXPECT_TRUE(resolver_
.has_pending_requests());
1042 ASSERT_FALSE(completed_fetcher_
);
1044 // Make it fail 3 times.
1045 for (int i
= 0; i
< 3; ++i
) {
1046 // A network change notification aborts the connect job.
1047 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
1048 base::MessageLoop::current()->RunUntilIdle();
1050 // But the fetcher retries automatically.
1051 EXPECT_EQ(1, GetNumFetcherCores());
1052 EXPECT_TRUE(resolver_
.has_pending_requests());
1053 ASSERT_FALSE(completed_fetcher_
);
1056 // A 4th failure doesn't trigger another retry, and propagates the error
1057 // to the owner of the fetcher.
1058 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
1059 base::MessageLoop::current()->RunUntilIdle();
1060 EXPECT_EQ(0, GetNumFetcherCores());
1061 EXPECT_FALSE(resolver_
.has_pending_requests());
1062 ASSERT_TRUE(completed_fetcher_
);
1064 // And the owner of the fetcher gets the ERR_NETWORK_CHANGED error.
1065 EXPECT_EQ(ERR_NETWORK_CHANGED
, completed_fetcher_
->GetStatus().error());
1068 TEST_F(URLFetcherMockDnsTest
, RetryOnNetworkChangedAndSucceed
) {
1069 EXPECT_EQ(0, GetNumFetcherCores());
1070 EXPECT_FALSE(resolver_
.has_pending_requests());
1072 // This posts a task to start the fetcher.
1073 CreateFetcher(test_url_
);
1074 fetcher_
->SetAutomaticallyRetryOnNetworkChanges(3);
1076 EXPECT_EQ(0, GetNumFetcherCores());
1077 base::MessageLoop::current()->RunUntilIdle();
1079 // The fetcher is now running, but is pending the host resolve.
1080 EXPECT_EQ(1, GetNumFetcherCores());
1081 EXPECT_TRUE(resolver_
.has_pending_requests());
1082 ASSERT_FALSE(completed_fetcher_
);
1084 // Make it fail 3 times.
1085 for (int i
= 0; i
< 3; ++i
) {
1086 // A network change notification aborts the connect job.
1087 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
1088 base::MessageLoop::current()->RunUntilIdle();
1090 // But the fetcher retries automatically.
1091 EXPECT_EQ(1, GetNumFetcherCores());
1092 EXPECT_TRUE(resolver_
.has_pending_requests());
1093 ASSERT_FALSE(completed_fetcher_
);
1096 // Now let it succeed by resolving the pending request.
1097 resolver_
.ResolveAllPending();
1098 base::MessageLoop::current()->Run();
1100 // URLFetcherMockDnsTest::OnURLFetchComplete() will quit the loop.
1101 EXPECT_EQ(0, GetNumFetcherCores());
1102 EXPECT_FALSE(resolver_
.has_pending_requests());
1103 ASSERT_TRUE(completed_fetcher_
);
1105 // This time the request succeeded.
1106 EXPECT_EQ(OK
, completed_fetcher_
->GetStatus().error());
1107 EXPECT_EQ(200, completed_fetcher_
->GetResponseCode());
1110 TEST_F(URLFetcherPostTest
, Basic
) {
1111 SpawnedTestServer
test_server(SpawnedTestServer::TYPE_HTTP
,
1112 SpawnedTestServer::kLocalhost
,
1113 base::FilePath(kDocRoot
));
1114 ASSERT_TRUE(test_server
.Start());
1116 CreateFetcher(test_server
.GetURL("echo"));
1117 base::MessageLoop::current()->Run();
1120 TEST_F(URLFetcherPostFileTest
, Basic
) {
1121 SpawnedTestServer
test_server(SpawnedTestServer::TYPE_HTTP
,
1122 SpawnedTestServer::kLocalhost
,
1123 base::FilePath(kDocRoot
));
1124 ASSERT_TRUE(test_server
.Start());
1126 CreateFetcher(test_server
.GetURL("echo"));
1127 base::MessageLoop::current()->Run();
1130 TEST_F(URLFetcherPostFileTest
, Range
) {
1131 SpawnedTestServer
test_server(SpawnedTestServer::TYPE_HTTP
,
1132 SpawnedTestServer::kLocalhost
,
1133 base::FilePath(kDocRoot
));
1134 ASSERT_TRUE(test_server
.Start());
1136 SetUploadRange(30, 100);
1138 CreateFetcher(test_server
.GetURL("echo"));
1139 base::MessageLoop::current()->Run();
1142 TEST_F(URLFetcherSetUploadFactoryTest
, Basic
) {
1143 SpawnedTestServer
test_server(SpawnedTestServer::TYPE_HTTP
,
1144 SpawnedTestServer::kLocalhost
,
1145 base::FilePath(kDocRoot
));
1146 ASSERT_TRUE(test_server
.Start());
1148 CreateFetcher(test_server
.GetURL("echo"));
1149 base::MessageLoop::current()->Run();
1150 ASSERT_EQ(1u, create_stream_count());
1153 TEST_F(URLFetcherSetUploadFactoryTest
, Retry
) {
1154 SpawnedTestServer
test_server(SpawnedTestServer::TYPE_HTTP
,
1155 SpawnedTestServer::kLocalhost
,
1156 base::FilePath(kDocRoot
));
1157 ASSERT_TRUE(test_server
.Start());
1158 expected_status_code_
= 500;
1159 CreateFetcher(test_server
.GetURL("echo?status=500"));
1160 base::MessageLoop::current()->Run();
1161 ASSERT_EQ(2u, create_stream_count());
1164 TEST_F(URLFetcherEmptyPostTest
, Basic
) {
1165 SpawnedTestServer
test_server(SpawnedTestServer::TYPE_HTTP
,
1166 SpawnedTestServer::kLocalhost
,
1167 base::FilePath(kDocRoot
));
1168 ASSERT_TRUE(test_server
.Start());
1170 CreateFetcher(test_server
.GetURL("echo"));
1171 base::MessageLoop::current()->Run();
1174 TEST_F(URLFetcherUploadProgressTest
, Basic
) {
1175 SpawnedTestServer
test_server(SpawnedTestServer::TYPE_HTTP
,
1176 SpawnedTestServer::kLocalhost
,
1177 base::FilePath(kDocRoot
));
1178 ASSERT_TRUE(test_server
.Start());
1180 CreateFetcher(test_server
.GetURL("echo"));
1181 base::MessageLoop::current()->Run();
1184 TEST_F(URLFetcherDownloadProgressTest
, Basic
) {
1185 SpawnedTestServer
test_server(SpawnedTestServer::TYPE_HTTP
,
1186 SpawnedTestServer::kLocalhost
,
1187 base::FilePath(kDocRoot
));
1188 ASSERT_TRUE(test_server
.Start());
1190 // Get a file large enough to require more than one read into
1191 // URLFetcher::Core's IOBuffer.
1192 static const char kFileToFetch
[] = "animate1.gif";
1193 // Hardcoded file size - it cannot be easily fetched when a remote http server
1194 // is used for testing.
1195 static const int64 kFileSize
= 19021;
1197 expected_total_
= kFileSize
;
1199 CreateFetcher(test_server
.GetURL(
1200 std::string(kTestServerFilePrefix
) + kFileToFetch
));
1202 base::MessageLoop::current()->Run();
1205 TEST_F(URLFetcherDownloadProgressCancelTest
, CancelWhileProgressReport
) {
1206 SpawnedTestServer
test_server(SpawnedTestServer::TYPE_HTTP
,
1207 SpawnedTestServer::kLocalhost
,
1208 base::FilePath(kDocRoot
));
1209 ASSERT_TRUE(test_server
.Start());
1211 // Get a file large enough to require more than one read into
1212 // URLFetcher::Core's IOBuffer.
1213 static const char kFileToFetch
[] = "animate1.gif";
1214 CreateFetcher(test_server
.GetURL(
1215 std::string(kTestServerFilePrefix
) + kFileToFetch
));
1217 base::MessageLoop::current()->Run();
1220 TEST_F(URLFetcherHeadersTest
, Headers
) {
1221 SpawnedTestServer
test_server(
1222 SpawnedTestServer::TYPE_HTTP
,
1223 SpawnedTestServer::kLocalhost
,
1224 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
1225 ASSERT_TRUE(test_server
.Start());
1227 CreateFetcher(test_server
.GetURL("files/with-headers.html"));
1228 base::MessageLoop::current()->Run();
1229 // The actual tests are in the URLFetcherHeadersTest fixture.
1232 TEST_F(URLFetcherSocketAddressTest
, SocketAddress
) {
1233 SpawnedTestServer
test_server(
1234 SpawnedTestServer::TYPE_HTTP
,
1235 SpawnedTestServer::kLocalhost
,
1236 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
1237 ASSERT_TRUE(test_server
.Start());
1238 expected_port_
= test_server
.host_port_pair().port();
1240 // Reusing "with-headers.html" but doesn't really matter.
1241 CreateFetcher(test_server
.GetURL("files/with-headers.html"));
1242 base::MessageLoop::current()->Run();
1243 // The actual tests are in the URLFetcherSocketAddressTest fixture.
1246 TEST_F(URLFetcherStopOnRedirectTest
, StopOnRedirect
) {
1247 SpawnedTestServer
test_server(SpawnedTestServer::TYPE_HTTP
,
1248 SpawnedTestServer::kLocalhost
,
1249 base::FilePath(kDocRoot
));
1250 ASSERT_TRUE(test_server
.Start());
1253 test_server
.GetURL(std::string("server-redirect?") + kRedirectTarget
));
1254 base::MessageLoop::current()->Run();
1255 EXPECT_TRUE(callback_called_
);
1258 TEST_F(URLFetcherProtectTest
, Overload
) {
1259 SpawnedTestServer
test_server(SpawnedTestServer::TYPE_HTTP
,
1260 SpawnedTestServer::kLocalhost
,
1261 base::FilePath(kDocRoot
));
1262 ASSERT_TRUE(test_server
.Start());
1264 GURL
url(test_server
.GetURL("defaultresponse"));
1266 // Registers an entry for test url. It only allows 3 requests to be sent
1267 // in 200 milliseconds.
1268 scoped_refptr
<URLRequestThrottlerEntry
> entry(
1269 new URLRequestThrottlerEntry(request_context()->throttler_manager(),
1277 request_context()->throttler_manager()
1278 ->OverrideEntryForTests(url
, entry
.get());
1282 base::MessageLoop::current()->Run();
1285 TEST_F(URLFetcherProtectTest
, ServerUnavailable
) {
1286 SpawnedTestServer
test_server(SpawnedTestServer::TYPE_HTTP
,
1287 SpawnedTestServer::kLocalhost
,
1288 base::FilePath(kDocRoot
));
1289 ASSERT_TRUE(test_server
.Start());
1291 GURL
url(test_server
.GetURL("files/server-unavailable.html"));
1293 // Registers an entry for test url. The backoff time is calculated by:
1294 // new_backoff = 2.0 * old_backoff + 0
1295 // and maximum backoff time is 256 milliseconds.
1296 // Maximum retries allowed is set to 11.
1297 scoped_refptr
<URLRequestThrottlerEntry
> entry(
1298 new URLRequestThrottlerEntry(request_context()->throttler_manager(),
1306 request_context()->throttler_manager()
1307 ->OverrideEntryForTests(url
, entry
.get());
1311 base::MessageLoop::current()->Run();
1314 TEST_F(URLFetcherProtectTestPassedThrough
, ServerUnavailablePropagateResponse
) {
1315 SpawnedTestServer
test_server(SpawnedTestServer::TYPE_HTTP
,
1316 SpawnedTestServer::kLocalhost
,
1317 base::FilePath(kDocRoot
));
1318 ASSERT_TRUE(test_server
.Start());
1320 GURL
url(test_server
.GetURL("files/server-unavailable.html"));
1322 // Registers an entry for test url. The backoff time is calculated by:
1323 // new_backoff = 2.0 * old_backoff + 0
1324 // and maximum backoff time is 150000 milliseconds.
1325 // Maximum retries allowed is set to 11.
1326 scoped_refptr
<URLRequestThrottlerEntry
> entry(
1327 new URLRequestThrottlerEntry(request_context()->throttler_manager(),
1335 // Total time if *not* for not doing automatic backoff would be 150s.
1336 // In reality it should be "as soon as server responds".
1337 request_context()->throttler_manager()
1338 ->OverrideEntryForTests(url
, entry
.get());
1342 base::MessageLoop::current()->Run();
1345 TEST_F(URLFetcherBadHTTPSTest
, BadHTTPSTest
) {
1346 SpawnedTestServer::SSLOptions
ssl_options(
1347 SpawnedTestServer::SSLOptions::CERT_EXPIRED
);
1348 SpawnedTestServer
test_server(SpawnedTestServer::TYPE_HTTPS
,
1350 base::FilePath(kDocRoot
));
1351 ASSERT_TRUE(test_server
.Start());
1353 CreateFetcher(test_server
.GetURL("defaultresponse"));
1354 base::MessageLoop::current()->Run();
1357 TEST_F(URLFetcherCancelTest
, ReleasesContext
) {
1358 SpawnedTestServer
test_server(SpawnedTestServer::TYPE_HTTP
,
1359 SpawnedTestServer::kLocalhost
,
1360 base::FilePath(kDocRoot
));
1361 ASSERT_TRUE(test_server
.Start());
1363 GURL
url(test_server
.GetURL("files/server-unavailable.html"));
1365 // Create a separate thread that will create the URLFetcher. The current
1366 // (main) thread will do the IO, and when the fetch is complete it will
1367 // terminate the main thread's message loop; then the other thread's
1368 // message loop will be shut down automatically as the thread goes out of
1370 base::Thread
t("URLFetcher test thread");
1371 ASSERT_TRUE(t
.Start());
1372 t
.message_loop()->PostTask(
1374 base::Bind(&URLFetcherCancelTest::CreateFetcher
,
1375 base::Unretained(this), url
));
1377 base::MessageLoop::current()->Run();
1380 TEST_F(URLFetcherCancelTest
, CancelWhileDelayedStartTaskPending
) {
1381 SpawnedTestServer
test_server(SpawnedTestServer::TYPE_HTTP
,
1382 SpawnedTestServer::kLocalhost
,
1383 base::FilePath(kDocRoot
));
1384 ASSERT_TRUE(test_server
.Start());
1386 GURL
url(test_server
.GetURL("files/server-unavailable.html"));
1388 // Register an entry for test url.
1389 // Using a sliding window of 4 seconds, and max of 1 request, under a fast
1390 // run we expect to have a 4 second delay when posting the Start task.
1391 scoped_refptr
<URLRequestThrottlerEntry
> entry(
1392 new URLRequestThrottlerEntry(request_context()->throttler_manager(),
1400 request_context()->throttler_manager()
1401 ->OverrideEntryForTests(url
, entry
.get());
1402 // Fake that a request has just started.
1403 entry
->ReserveSendingTimeForNextRequest(base::TimeTicks());
1405 // The next request we try to send will be delayed by ~4 seconds.
1406 // The slower the test runs, the less the delay will be (since it takes the
1407 // time difference from now).
1409 base::Thread
t("URLFetcher test thread");
1410 ASSERT_TRUE(t
.Start());
1411 t
.message_loop()->PostTask(
1413 base::Bind(&URLFetcherTest::CreateFetcher
, base::Unretained(this), url
));
1415 base::MessageLoop::current()->Run();
1418 TEST_F(URLFetcherMultipleAttemptTest
, SameData
) {
1419 SpawnedTestServer
test_server(SpawnedTestServer::TYPE_HTTP
,
1420 SpawnedTestServer::kLocalhost
,
1421 base::FilePath(kDocRoot
));
1422 ASSERT_TRUE(test_server
.Start());
1424 // Create the fetcher on the main thread. Since IO will happen on the main
1425 // thread, this will test URLFetcher's ability to do everything on one
1427 CreateFetcher(test_server
.GetURL("defaultresponse"));
1429 base::MessageLoop::current()->Run();
1432 TEST_F(URLFetcherFileTest
, SmallGet
) {
1433 SpawnedTestServer
test_server(SpawnedTestServer::TYPE_HTTP
,
1434 SpawnedTestServer::kLocalhost
,
1435 base::FilePath(kDocRoot
));
1436 ASSERT_TRUE(test_server
.Start());
1438 base::ScopedTempDir temp_dir
;
1439 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
1441 // Get a small file.
1442 static const char kFileToFetch
[] = "simple.html";
1443 expected_file_
= test_server
.GetDocumentRoot().AppendASCII(kFileToFetch
);
1444 CreateFetcherForFile(
1445 test_server
.GetURL(std::string(kTestServerFilePrefix
) + kFileToFetch
),
1446 temp_dir
.path().AppendASCII(kFileToFetch
));
1448 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit().
1450 ASSERT_FALSE(base::PathExists(file_path_
))
1451 << file_path_
.value() << " not removed.";
1454 TEST_F(URLFetcherFileTest
, LargeGet
) {
1455 SpawnedTestServer
test_server(SpawnedTestServer::TYPE_HTTP
,
1456 SpawnedTestServer::kLocalhost
,
1457 base::FilePath(kDocRoot
));
1458 ASSERT_TRUE(test_server
.Start());
1460 base::ScopedTempDir temp_dir
;
1461 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
1463 // Get a file large enough to require more than one read into
1464 // URLFetcher::Core's IOBuffer.
1465 static const char kFileToFetch
[] = "animate1.gif";
1466 expected_file_
= test_server
.GetDocumentRoot().AppendASCII(kFileToFetch
);
1467 CreateFetcherForFile(
1468 test_server
.GetURL(std::string(kTestServerFilePrefix
) + kFileToFetch
),
1469 temp_dir
.path().AppendASCII(kFileToFetch
));
1471 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit().
1474 TEST_F(URLFetcherFileTest
, SavedOutputFileOwnerhisp
) {
1475 // If the caller takes the ownership of the output file, the file should
1476 // persist even after URLFetcher is gone. If not, the file must be deleted.
1477 const bool kTake
[] = {false, true};
1478 for (size_t i
= 0; i
< arraysize(kTake
); ++i
) {
1479 take_ownership_of_file_
= kTake
[i
];
1480 SpawnedTestServer
test_server(SpawnedTestServer::TYPE_HTTP
,
1481 SpawnedTestServer::kLocalhost
,
1482 base::FilePath(kDocRoot
));
1483 ASSERT_TRUE(test_server
.Start());
1485 base::ScopedTempDir temp_dir
;
1486 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
1488 // Get a small file.
1489 static const char kFileToFetch
[] = "simple.html";
1490 expected_file_
= test_server
.GetDocumentRoot().AppendASCII(kFileToFetch
);
1491 CreateFetcherForFile(
1492 test_server
.GetURL(std::string(kTestServerFilePrefix
) + kFileToFetch
),
1493 temp_dir
.path().AppendASCII(kFileToFetch
));
1495 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit().
1497 base::MessageLoop::current()->RunUntilIdle();
1498 ASSERT_EQ(kTake
[i
], base::PathExists(file_path_
)) <<
1499 "FilePath: " << file_path_
.value();
1503 TEST_F(URLFetcherFileTest
, OverwriteExistingFile
) {
1504 SpawnedTestServer
test_server(SpawnedTestServer::TYPE_HTTP
,
1505 SpawnedTestServer::kLocalhost
,
1506 base::FilePath(kDocRoot
));
1507 ASSERT_TRUE(test_server
.Start());
1509 base::ScopedTempDir temp_dir
;
1510 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
1512 // Create a file before trying to fetch.
1513 static const char kFileToFetch
[] = "simple.html";
1514 std::string
data(10000, '?'); // Meant to be larger than simple.html.
1515 file_path_
= temp_dir
.path().AppendASCII(kFileToFetch
);
1516 ASSERT_EQ(static_cast<int>(data
.size()),
1517 base::WriteFile(file_path_
, data
.data(), data
.size()));
1518 ASSERT_TRUE(base::PathExists(file_path_
));
1519 expected_file_
= test_server
.GetDocumentRoot().AppendASCII(kFileToFetch
);
1520 ASSERT_FALSE(base::ContentsEqual(file_path_
, expected_file_
));
1522 // Get a small file.
1523 CreateFetcherForFile(
1524 test_server
.GetURL(std::string(kTestServerFilePrefix
) + kFileToFetch
),
1527 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit().
1530 TEST_F(URLFetcherFileTest
, TryToOverwriteDirectory
) {
1531 SpawnedTestServer
test_server(SpawnedTestServer::TYPE_HTTP
,
1532 SpawnedTestServer::kLocalhost
,
1533 base::FilePath(kDocRoot
));
1534 ASSERT_TRUE(test_server
.Start());
1536 base::ScopedTempDir temp_dir
;
1537 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
1539 // Create a directory before trying to fetch.
1540 static const char kFileToFetch
[] = "simple.html";
1541 file_path_
= temp_dir
.path().AppendASCII(kFileToFetch
);
1542 ASSERT_TRUE(base::CreateDirectory(file_path_
));
1543 ASSERT_TRUE(base::PathExists(file_path_
));
1545 // Get a small file.
1546 expected_file_error_
= ERR_ACCESS_DENIED
;
1547 expected_file_
= test_server
.GetDocumentRoot().AppendASCII(kFileToFetch
);
1548 CreateFetcherForFile(
1549 test_server
.GetURL(std::string(kTestServerFilePrefix
) + kFileToFetch
),
1552 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit().
1554 base::MessageLoop::current()->RunUntilIdle();
1557 TEST_F(URLFetcherFileTest
, SmallGetToTempFile
) {
1558 SpawnedTestServer
test_server(SpawnedTestServer::TYPE_HTTP
,
1559 SpawnedTestServer::kLocalhost
,
1560 base::FilePath(kDocRoot
));
1561 ASSERT_TRUE(test_server
.Start());
1563 // Get a small file.
1564 static const char kFileToFetch
[] = "simple.html";
1565 expected_file_
= test_server
.GetDocumentRoot().AppendASCII(kFileToFetch
);
1566 CreateFetcherForTempFile(
1567 test_server
.GetURL(std::string(kTestServerFilePrefix
) + kFileToFetch
));
1569 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit().
1571 ASSERT_FALSE(base::PathExists(file_path_
))
1572 << file_path_
.value() << " not removed.";
1575 TEST_F(URLFetcherFileTest
, LargeGetToTempFile
) {
1576 SpawnedTestServer
test_server(SpawnedTestServer::TYPE_HTTP
,
1577 SpawnedTestServer::kLocalhost
,
1578 base::FilePath(kDocRoot
));
1579 ASSERT_TRUE(test_server
.Start());
1581 // Get a file large enough to require more than one read into
1582 // URLFetcher::Core's IOBuffer.
1583 static const char kFileToFetch
[] = "animate1.gif";
1584 expected_file_
= test_server
.GetDocumentRoot().AppendASCII(kFileToFetch
);
1585 CreateFetcherForTempFile(test_server
.GetURL(
1586 std::string(kTestServerFilePrefix
) + kFileToFetch
));
1588 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit().
1591 TEST_F(URLFetcherFileTest
, SavedOutputTempFileOwnerhisp
) {
1592 // If the caller takes the ownership of the temp file, the file should persist
1593 // even after URLFetcher is gone. If not, the file must be deleted.
1594 const bool kTake
[] = {false, true};
1595 for (size_t i
= 0; i
< arraysize(kTake
); ++i
) {
1596 take_ownership_of_file_
= kTake
[i
];
1598 SpawnedTestServer
test_server(SpawnedTestServer::TYPE_HTTP
,
1599 SpawnedTestServer::kLocalhost
,
1600 base::FilePath(kDocRoot
));
1601 ASSERT_TRUE(test_server
.Start());
1603 // Get a small file.
1604 static const char kFileToFetch
[] = "simple.html";
1605 expected_file_
= test_server
.GetDocumentRoot().AppendASCII(kFileToFetch
);
1606 CreateFetcherForTempFile(test_server
.GetURL(
1607 std::string(kTestServerFilePrefix
) + kFileToFetch
));
1609 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit().
1611 base::MessageLoop::current()->RunUntilIdle();
1612 ASSERT_EQ(kTake
[i
], base::PathExists(file_path_
)) <<
1613 "FilePath: " << file_path_
.value();