Roll src/third_party/WebKit 8b42d1d:744641d (svn 186770:186771)
[chromium-blink-merge.git] / net / url_request / url_fetcher_impl_unittest.cc
blobfce7ace52848e518e0e93238d6abf4d5b8e92344
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"
7 #include <algorithm>
8 #include <string>
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/network_change_notifier.h"
21 #include "net/dns/mock_host_resolver.h"
22 #include "net/http/http_response_headers.h"
23 #include "net/test/spawned_test_server/spawned_test_server.h"
24 #include "net/url_request/url_fetcher_delegate.h"
25 #include "net/url_request/url_request_context_getter.h"
26 #include "net/url_request/url_request_test_util.h"
27 #include "net/url_request/url_request_throttler_manager.h"
28 #include "testing/gtest/include/gtest/gtest.h"
30 #if defined(USE_NSS) || defined(OS_IOS)
31 #include "net/ocsp/nss_ocsp.h"
32 #endif
34 namespace net {
36 using base::Time;
37 using base::TimeDelta;
39 // TODO(eroman): Add a regression test for http://crbug.com/40505.
41 namespace {
43 // TODO(akalin): Move all the test data to somewhere under net/.
44 const base::FilePath::CharType kDocRoot[] =
45 FILE_PATH_LITERAL("net/data/url_fetcher_impl_unittest");
46 const char kTestServerFilePrefix[] = "files/";
48 class ThrottlingTestURLRequestContext : public TestURLRequestContext {
49 public:
50 ThrottlingTestURLRequestContext() : TestURLRequestContext(true) {
51 set_throttler_manager(&throttler_manager_);
52 Init();
53 DCHECK(throttler_manager() != NULL);
56 private:
57 URLRequestThrottlerManager throttler_manager_;
60 class ThrottlingTestURLRequestContextGetter
61 : public TestURLRequestContextGetter {
62 public:
63 ThrottlingTestURLRequestContextGetter(
64 base::MessageLoopProxy* io_message_loop_proxy,
65 TestURLRequestContext* request_context)
66 : TestURLRequestContextGetter(io_message_loop_proxy),
67 context_(request_context) {
70 // TestURLRequestContextGetter:
71 TestURLRequestContext* GetURLRequestContext() override { return context_; }
73 protected:
74 ~ThrottlingTestURLRequestContextGetter() override {}
76 TestURLRequestContext* const context_;
79 } // namespace
81 class URLFetcherTest : public testing::Test,
82 public URLFetcherDelegate {
83 public:
84 URLFetcherTest() : fetcher_(NULL) {}
86 static int GetNumFetcherCores() {
87 return URLFetcherImpl::GetNumFetcherCores();
90 // Creates a URLFetcher, using the program's main thread to do IO.
91 virtual void CreateFetcher(const GURL& url);
93 // URLFetcherDelegate:
94 // Subclasses that override this should either call this function or
95 // CleanupAfterFetchComplete() at the end of their processing, depending on
96 // whether they want to check for a non-empty HTTP 200 response or not.
97 void OnURLFetchComplete(const URLFetcher* source) override;
99 // Deletes |fetcher| and terminates the message loop.
100 void CleanupAfterFetchComplete();
102 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy() {
103 return io_message_loop_proxy_;
106 TestURLRequestContext* request_context() {
107 return context_.get();
110 protected:
111 // testing::Test:
112 void SetUp() override {
113 testing::Test::SetUp();
115 context_.reset(new ThrottlingTestURLRequestContext());
116 io_message_loop_proxy_ = base::MessageLoopProxy::current();
118 #if defined(USE_NSS) || defined(OS_IOS)
119 crypto::EnsureNSSInit();
120 EnsureNSSHttpIOInit();
121 #endif
124 void TearDown() override {
125 #if defined(USE_NSS) || defined(OS_IOS)
126 ShutdownNSSHttpIO();
127 #endif
130 // URLFetcher is designed to run on the main UI thread, but in our tests
131 // we assume that the current thread is the IO thread where the URLFetcher
132 // dispatches its requests to. When we wish to simulate being used from
133 // a UI thread, we dispatch a worker thread to do so.
134 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_;
136 URLFetcherImpl* fetcher_;
137 scoped_ptr<TestURLRequestContext> context_;
140 // A test fixture that uses a MockHostResolver, so that name resolutions can
141 // be manipulated by the tests to keep connections in the resolving state.
142 class URLFetcherMockDnsTest : public URLFetcherTest {
143 public:
144 // testing::Test:
145 void SetUp() override;
147 // URLFetcherTest:
148 void CreateFetcher(const GURL& url) override;
150 // URLFetcherDelegate:
151 void OnURLFetchComplete(const URLFetcher* source) override;
153 protected:
154 GURL test_url_;
155 scoped_ptr<SpawnedTestServer> test_server_;
156 MockHostResolver resolver_;
157 scoped_ptr<URLFetcher> completed_fetcher_;
160 void URLFetcherTest::CreateFetcher(const GURL& url) {
161 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this);
162 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
163 io_message_loop_proxy().get(), request_context()));
164 fetcher_->Start();
167 void URLFetcherTest::OnURLFetchComplete(const URLFetcher* source) {
168 EXPECT_TRUE(source->GetStatus().is_success());
169 EXPECT_EQ(200, source->GetResponseCode()); // HTTP OK
171 std::string data;
172 EXPECT_TRUE(source->GetResponseAsString(&data));
173 EXPECT_FALSE(data.empty());
175 CleanupAfterFetchComplete();
178 void URLFetcherTest::CleanupAfterFetchComplete() {
179 delete fetcher_; // Have to delete this here and not in the destructor,
180 // because the destructor won't necessarily run on the
181 // same thread that CreateFetcher() did.
183 io_message_loop_proxy()->PostTask(FROM_HERE,
184 base::MessageLoop::QuitClosure());
185 // If the current message loop is not the IO loop, it will be shut down when
186 // the main loop returns and this thread subsequently goes out of scope.
189 void URLFetcherMockDnsTest::SetUp() {
190 URLFetcherTest::SetUp();
192 resolver_.set_ondemand_mode(true);
193 resolver_.rules()->AddRule("example.com", "127.0.0.1");
195 context_.reset(new TestURLRequestContext(true));
196 context_->set_host_resolver(&resolver_);
197 context_->Init();
199 test_server_.reset(new SpawnedTestServer(SpawnedTestServer::TYPE_HTTP,
200 SpawnedTestServer::kLocalhost,
201 base::FilePath(kDocRoot)));
202 ASSERT_TRUE(test_server_->Start());
204 // test_server_.GetURL() returns a URL with 127.0.0.1 (kLocalhost), that is
205 // immediately resolved by the MockHostResolver. Use a hostname instead to
206 // trigger an async resolve.
207 test_url_ = GURL(
208 base::StringPrintf("http://example.com:%d/defaultresponse",
209 test_server_->host_port_pair().port()));
210 ASSERT_TRUE(test_url_.is_valid());
213 void URLFetcherMockDnsTest::CreateFetcher(const GURL& url) {
214 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this);
215 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
216 io_message_loop_proxy().get(), request_context()));
219 void URLFetcherMockDnsTest::OnURLFetchComplete(const URLFetcher* source) {
220 io_message_loop_proxy()->PostTask(FROM_HERE,
221 base::MessageLoop::QuitClosure());
222 ASSERT_EQ(fetcher_, source);
223 EXPECT_EQ(test_url_, source->GetOriginalURL());
224 completed_fetcher_.reset(fetcher_);
227 namespace {
229 // Version of URLFetcherTest that does a POST instead
230 class URLFetcherPostTest : public URLFetcherTest {
231 public:
232 // URLFetcherTest:
233 void CreateFetcher(const GURL& url) override;
235 // URLFetcherDelegate:
236 void OnURLFetchComplete(const URLFetcher* source) override;
239 // Version of URLFetcherTest that does a POST of a file using
240 // SetUploadDataStream
241 class URLFetcherPostFileTest : public URLFetcherTest {
242 public:
243 URLFetcherPostFileTest();
245 void SetUploadRange(uint64 range_offset, uint64 range_length) {
246 range_offset_ = range_offset;
247 range_length_ = range_length;
250 // URLFetcherTest:
251 void CreateFetcher(const GURL& url) override;
253 // URLFetcherDelegate:
254 void OnURLFetchComplete(const URLFetcher* source) override;
256 private:
257 base::FilePath path_;
258 uint64 range_offset_;
259 uint64 range_length_;
262 // Version of URLFetcherTest that does a POST instead with empty upload body
263 class URLFetcherEmptyPostTest : public URLFetcherTest {
264 public:
265 // URLFetcherTest:
266 void CreateFetcher(const GURL& url) override;
268 // URLFetcherDelegate:
269 void OnURLFetchComplete(const URLFetcher* source) override;
272 // Version of URLFetcherTest that tests download progress reports.
273 class URLFetcherDownloadProgressTest : public URLFetcherTest {
274 public:
275 URLFetcherDownloadProgressTest()
276 : previous_progress_(0),
277 expected_total_(0) {
280 // URLFetcherTest:
281 void CreateFetcher(const GURL& url) override;
283 // URLFetcherDelegate:
284 void OnURLFetchDownloadProgress(const URLFetcher* source,
285 int64 current,
286 int64 total) override;
288 protected:
289 // Download progress returned by the previous callback.
290 int64 previous_progress_;
291 // Size of the file being downloaded, known in advance (provided by each test
292 // case).
293 int64 expected_total_;
296 // Version of URLFetcherTest that tests progress reports at cancellation.
297 class URLFetcherDownloadProgressCancelTest : public URLFetcherTest {
298 public:
299 // URLFetcherTest:
300 void CreateFetcher(const GURL& url) override;
302 // URLFetcherDelegate:
303 void OnURLFetchComplete(const URLFetcher* source) override;
304 void OnURLFetchDownloadProgress(const URLFetcher* source,
305 int64 current,
306 int64 total) override;
308 protected:
309 bool cancelled_;
312 // Version of URLFetcherTest that tests upload progress reports.
313 class URLFetcherUploadProgressTest : public URLFetcherTest {
314 public:
315 // URLFetcherTest:
316 void CreateFetcher(const GURL& url) override;
318 // URLFetcherDelegate:
319 void OnURLFetchUploadProgress(const URLFetcher* source,
320 int64 current,
321 int64 total) override;
323 protected:
324 int64 previous_progress_;
325 std::string chunk_;
326 int64 number_of_chunks_added_;
329 // Version of URLFetcherTest that tests headers.
330 class URLFetcherHeadersTest : public URLFetcherTest {
331 public:
332 // URLFetcherDelegate:
333 void OnURLFetchComplete(const URLFetcher* source) override;
336 // Version of URLFetcherTest that tests SocketAddress.
337 class URLFetcherSocketAddressTest : public URLFetcherTest {
338 public:
339 // URLFetcherDelegate:
340 void OnURLFetchComplete(const URLFetcher* source) override;
342 protected:
343 std::string expected_host_;
344 uint16 expected_port_;
347 // Version of URLFetcherTest that tests stopping on a redirect.
348 class URLFetcherStopOnRedirectTest : public URLFetcherTest {
349 public:
350 URLFetcherStopOnRedirectTest();
351 ~URLFetcherStopOnRedirectTest() override;
353 // URLFetcherTest:
354 void CreateFetcher(const GURL& url) override;
356 // URLFetcherDelegate:
357 void OnURLFetchComplete(const URLFetcher* source) override;
359 protected:
360 // The URL we should be redirected to.
361 static const char* kRedirectTarget;
363 bool callback_called_; // Set to true in OnURLFetchComplete().
366 // Version of URLFetcherTest that tests overload protection.
367 class URLFetcherProtectTest : public URLFetcherTest {
368 public:
369 // URLFetcherTest:
370 void CreateFetcher(const GURL& url) override;
372 // URLFetcherDelegate:
373 void OnURLFetchComplete(const URLFetcher* source) override;
375 private:
376 Time start_time_;
379 // Version of URLFetcherTest that tests overload protection, when responses
380 // passed through.
381 class URLFetcherProtectTestPassedThrough : public URLFetcherTest {
382 public:
383 // URLFetcherTest:
384 void CreateFetcher(const GURL& url) override;
386 // URLFetcherDelegate:
387 void OnURLFetchComplete(const URLFetcher* source) override;
389 private:
390 Time start_time_;
393 // Version of URLFetcherTest that tests bad HTTPS requests.
394 class URLFetcherBadHTTPSTest : public URLFetcherTest {
395 public:
396 URLFetcherBadHTTPSTest();
398 // URLFetcherDelegate:
399 void OnURLFetchComplete(const URLFetcher* source) override;
401 private:
402 base::FilePath cert_dir_;
405 // Version of URLFetcherTest that tests request cancellation on shutdown.
406 class URLFetcherCancelTest : public URLFetcherTest {
407 public:
408 // URLFetcherTest:
409 void CreateFetcher(const GURL& url) override;
411 // URLFetcherDelegate:
412 void OnURLFetchComplete(const URLFetcher* source) override;
414 void CancelRequest();
417 // Version of TestURLRequestContext that posts a Quit task to the IO
418 // thread once it is deleted.
419 class CancelTestURLRequestContext : public ThrottlingTestURLRequestContext {
420 public:
421 explicit CancelTestURLRequestContext() {
424 private:
425 ~CancelTestURLRequestContext() override {
426 // The d'tor should execute in the IO thread. Post the quit task to the
427 // current thread.
428 base::MessageLoop::current()->PostTask(FROM_HERE,
429 base::MessageLoop::QuitClosure());
433 class CancelTestURLRequestContextGetter
434 : public TestURLRequestContextGetter {
435 public:
436 CancelTestURLRequestContextGetter(
437 base::MessageLoopProxy* io_message_loop_proxy,
438 const GURL& throttle_for_url)
439 : TestURLRequestContextGetter(io_message_loop_proxy),
440 io_message_loop_proxy_(io_message_loop_proxy),
441 context_created_(false, false),
442 throttle_for_url_(throttle_for_url) {
445 // TestURLRequestContextGetter:
446 TestURLRequestContext* GetURLRequestContext() override {
447 if (!context_.get()) {
448 context_.reset(new CancelTestURLRequestContext());
449 DCHECK(context_->throttler_manager());
451 // Registers an entry for test url. The backoff time is calculated by:
452 // new_backoff = 2.0 * old_backoff + 0
453 // The initial backoff is 2 seconds and maximum backoff is 4 seconds.
454 // Maximum retries allowed is set to 2.
455 scoped_refptr<URLRequestThrottlerEntry> entry(
456 new URLRequestThrottlerEntry(context_->throttler_manager(),
457 std::string(),
458 200,
460 2000,
461 2.0,
462 0.0,
463 4000));
464 context_->throttler_manager()
465 ->OverrideEntryForTests(throttle_for_url_, entry.get());
467 context_created_.Signal();
469 return context_.get();
472 virtual scoped_refptr<base::MessageLoopProxy> GetIOMessageLoopProxy() const {
473 return io_message_loop_proxy_;
476 void WaitForContextCreation() {
477 context_created_.Wait();
480 protected:
481 ~CancelTestURLRequestContextGetter() override {}
483 private:
484 scoped_ptr<TestURLRequestContext> context_;
485 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_;
486 base::WaitableEvent context_created_;
487 GURL throttle_for_url_;
490 // Version of URLFetcherTest that tests retying the same request twice.
491 class URLFetcherMultipleAttemptTest : public URLFetcherTest {
492 public:
493 // URLFetcherDelegate:
494 void OnURLFetchComplete(const URLFetcher* source) override;
496 private:
497 std::string data_;
500 class URLFetcherFileTest : public URLFetcherTest {
501 public:
502 URLFetcherFileTest() : take_ownership_of_file_(false),
503 expected_file_error_(OK) {}
505 void CreateFetcherForFile(const GURL& url, const base::FilePath& file_path);
506 void CreateFetcherForTempFile(const GURL& url);
508 // URLFetcherDelegate:
509 void OnURLFetchComplete(const URLFetcher* source) override;
511 protected:
512 base::FilePath expected_file_;
513 base::FilePath file_path_;
515 // Set by the test. Used in OnURLFetchComplete() to decide if
516 // the URLFetcher should own the temp file, so that we can test
517 // disowning prevents the file from being deleted.
518 bool take_ownership_of_file_;
520 // Expected file error code for the test. OK when expecting success.
521 int expected_file_error_;
524 void URLFetcherPostTest::CreateFetcher(const GURL& url) {
525 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this);
526 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
527 io_message_loop_proxy().get(), request_context()));
528 fetcher_->SetUploadData("application/x-www-form-urlencoded", "bobsyeruncle");
529 fetcher_->Start();
532 void URLFetcherPostTest::OnURLFetchComplete(const URLFetcher* source) {
533 std::string data;
534 EXPECT_TRUE(source->GetResponseAsString(&data));
535 EXPECT_EQ(std::string("bobsyeruncle"), data);
536 URLFetcherTest::OnURLFetchComplete(source);
539 URLFetcherPostFileTest::URLFetcherPostFileTest()
540 : range_offset_(0),
541 range_length_(kuint64max) {
542 PathService::Get(base::DIR_SOURCE_ROOT, &path_);
543 path_ = path_.Append(FILE_PATH_LITERAL("net"));
544 path_ = path_.Append(FILE_PATH_LITERAL("data"));
545 path_ = path_.Append(FILE_PATH_LITERAL("url_request_unittest"));
546 path_ = path_.Append(FILE_PATH_LITERAL("BullRunSpeech.txt"));
549 void URLFetcherPostFileTest::CreateFetcher(const GURL& url) {
550 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this);
551 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
552 io_message_loop_proxy().get(), request_context()));
553 fetcher_->SetUploadFilePath("application/x-www-form-urlencoded",
554 path_,
555 range_offset_,
556 range_length_,
557 base::MessageLoopProxy::current());
558 fetcher_->Start();
561 void URLFetcherPostFileTest::OnURLFetchComplete(const URLFetcher* source) {
562 std::string expected;
563 ASSERT_TRUE(base::ReadFileToString(path_, &expected));
564 ASSERT_LE(range_offset_, expected.size());
565 uint64 expected_size =
566 std::min(range_length_, expected.size() - range_offset_);
568 std::string data;
569 EXPECT_TRUE(source->GetResponseAsString(&data));
570 EXPECT_EQ(expected.substr(range_offset_, expected_size), data);
571 URLFetcherTest::OnURLFetchComplete(source);
574 void URLFetcherEmptyPostTest::CreateFetcher(const GURL& url) {
575 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this);
576 fetcher_->SetRequestContext(new TestURLRequestContextGetter(
577 io_message_loop_proxy()));
578 fetcher_->SetUploadData("text/plain", std::string());
579 fetcher_->Start();
582 void URLFetcherEmptyPostTest::OnURLFetchComplete(const URLFetcher* source) {
583 EXPECT_TRUE(source->GetStatus().is_success());
584 EXPECT_EQ(200, source->GetResponseCode()); // HTTP OK
586 std::string data;
587 EXPECT_TRUE(source->GetResponseAsString(&data));
588 EXPECT_TRUE(data.empty());
590 CleanupAfterFetchComplete();
591 // Do not call the super class method URLFetcherTest::OnURLFetchComplete,
592 // since it expects a non-empty response.
595 void URLFetcherDownloadProgressTest::CreateFetcher(const GURL& url) {
596 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this);
597 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
598 io_message_loop_proxy().get(), request_context()));
599 fetcher_->Start();
602 void URLFetcherDownloadProgressTest::OnURLFetchDownloadProgress(
603 const URLFetcher* source, int64 progress, int64 total) {
604 // Increasing between 0 and total.
605 EXPECT_LE(0, progress);
606 EXPECT_GE(total, progress);
607 EXPECT_LE(previous_progress_, progress);
608 EXPECT_EQ(expected_total_, total);
609 previous_progress_ = progress;
612 void URLFetcherDownloadProgressCancelTest::CreateFetcher(const GURL& url) {
613 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this);
614 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
615 io_message_loop_proxy().get(), request_context()));
616 cancelled_ = false;
617 fetcher_->Start();
620 void URLFetcherDownloadProgressCancelTest::OnURLFetchDownloadProgress(
621 const URLFetcher* source, int64 current, int64 total) {
622 EXPECT_FALSE(cancelled_);
623 if (!cancelled_) {
624 cancelled_ = true;
625 CleanupAfterFetchComplete();
629 void URLFetcherDownloadProgressCancelTest::OnURLFetchComplete(
630 const URLFetcher* source) {
631 // Should have been cancelled.
632 ADD_FAILURE();
633 CleanupAfterFetchComplete();
636 void URLFetcherUploadProgressTest::CreateFetcher(const GURL& url) {
637 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this);
638 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
639 io_message_loop_proxy().get(), request_context()));
640 previous_progress_ = 0;
641 // Large enough data to require more than one read from UploadDataStream.
642 chunk_.assign(1<<16, 'a');
643 // Use chunked upload to wait for a timer event of progress notification.
644 fetcher_->SetChunkedUpload("application/x-www-form-urlencoded");
645 fetcher_->Start();
646 number_of_chunks_added_ = 1;
647 fetcher_->AppendChunkToUpload(chunk_, false);
650 void URLFetcherUploadProgressTest::OnURLFetchUploadProgress(
651 const URLFetcher* source, int64 current, int64 total) {
652 // Increasing between 0 and total.
653 EXPECT_LE(0, current);
654 EXPECT_GE(static_cast<int64>(chunk_.size()) * number_of_chunks_added_,
655 current);
656 EXPECT_LE(previous_progress_, current);
657 previous_progress_ = current;
658 EXPECT_EQ(-1, total);
660 if (number_of_chunks_added_ < 2) {
661 number_of_chunks_added_ += 1;
662 fetcher_->AppendChunkToUpload(chunk_, true);
666 void URLFetcherHeadersTest::OnURLFetchComplete(
667 const URLFetcher* source) {
668 std::string header;
669 EXPECT_TRUE(source->GetResponseHeaders()->GetNormalizedHeader("cache-control",
670 &header));
671 EXPECT_EQ("private", header);
672 URLFetcherTest::OnURLFetchComplete(source);
675 void URLFetcherSocketAddressTest::OnURLFetchComplete(
676 const URLFetcher* source) {
677 EXPECT_EQ("127.0.0.1", source->GetSocketAddress().host());
678 EXPECT_EQ(expected_port_, source->GetSocketAddress().port());
679 URLFetcherTest::OnURLFetchComplete(source);
682 // static
683 const char* URLFetcherStopOnRedirectTest::kRedirectTarget =
684 "http://redirect.target.com";
686 URLFetcherStopOnRedirectTest::URLFetcherStopOnRedirectTest()
687 : callback_called_(false) {
690 URLFetcherStopOnRedirectTest::~URLFetcherStopOnRedirectTest() {
693 void URLFetcherStopOnRedirectTest::CreateFetcher(const GURL& url) {
694 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this);
695 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
696 io_message_loop_proxy().get(), request_context()));
697 fetcher_->SetStopOnRedirect(true);
698 fetcher_->Start();
701 void URLFetcherStopOnRedirectTest::OnURLFetchComplete(
702 const URLFetcher* source) {
703 callback_called_ = true;
704 EXPECT_EQ(GURL(kRedirectTarget), source->GetURL());
705 EXPECT_EQ(URLRequestStatus::CANCELED, source->GetStatus().status());
706 EXPECT_EQ(ERR_ABORTED, source->GetStatus().error());
707 EXPECT_EQ(301, source->GetResponseCode());
708 CleanupAfterFetchComplete();
711 void URLFetcherProtectTest::CreateFetcher(const GURL& url) {
712 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this);
713 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
714 io_message_loop_proxy().get(), request_context()));
715 start_time_ = Time::Now();
716 fetcher_->SetMaxRetriesOn5xx(11);
717 fetcher_->Start();
720 void URLFetcherProtectTest::OnURLFetchComplete(const URLFetcher* source) {
721 const TimeDelta one_second = TimeDelta::FromMilliseconds(1000);
722 if (source->GetResponseCode() >= 500) {
723 // Now running ServerUnavailable test.
724 // It takes more than 1 second to finish all 11 requests.
725 EXPECT_TRUE(Time::Now() - start_time_ >= one_second);
726 EXPECT_TRUE(source->GetStatus().is_success());
727 std::string data;
728 EXPECT_TRUE(source->GetResponseAsString(&data));
729 EXPECT_FALSE(data.empty());
730 CleanupAfterFetchComplete();
731 } else {
732 // Now running Overload test.
733 static int count = 0;
734 count++;
735 if (count < 20) {
736 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
737 io_message_loop_proxy().get(), request_context()));
738 fetcher_->Start();
739 } else {
740 // We have already sent 20 requests continuously. And we expect that
741 // it takes more than 1 second due to the overload protection settings.
742 EXPECT_TRUE(Time::Now() - start_time_ >= one_second);
743 URLFetcherTest::OnURLFetchComplete(source);
748 void URLFetcherProtectTestPassedThrough::CreateFetcher(const GURL& url) {
749 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this);
750 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
751 io_message_loop_proxy().get(), request_context()));
752 fetcher_->SetAutomaticallyRetryOn5xx(false);
753 start_time_ = Time::Now();
754 fetcher_->SetMaxRetriesOn5xx(11);
755 fetcher_->Start();
758 void URLFetcherProtectTestPassedThrough::OnURLFetchComplete(
759 const URLFetcher* source) {
760 const TimeDelta one_minute = TimeDelta::FromMilliseconds(60000);
761 if (source->GetResponseCode() >= 500) {
762 // Now running ServerUnavailable test.
763 // It should get here on the first attempt, so almost immediately and
764 // *not* to attempt to execute all 11 requests (2.5 minutes).
765 EXPECT_TRUE(Time::Now() - start_time_ < one_minute);
766 EXPECT_TRUE(source->GetStatus().is_success());
767 // Check that suggested back off time is bigger than 0.
768 EXPECT_GT(fetcher_->GetBackoffDelay().InMicroseconds(), 0);
769 std::string data;
770 EXPECT_TRUE(source->GetResponseAsString(&data));
771 EXPECT_FALSE(data.empty());
772 } else {
773 // We should not get here!
774 ADD_FAILURE();
777 CleanupAfterFetchComplete();
781 URLFetcherBadHTTPSTest::URLFetcherBadHTTPSTest() {
782 PathService::Get(base::DIR_SOURCE_ROOT, &cert_dir_);
783 cert_dir_ = cert_dir_.AppendASCII("chrome");
784 cert_dir_ = cert_dir_.AppendASCII("test");
785 cert_dir_ = cert_dir_.AppendASCII("data");
786 cert_dir_ = cert_dir_.AppendASCII("ssl");
787 cert_dir_ = cert_dir_.AppendASCII("certificates");
790 // The "server certificate expired" error should result in automatic
791 // cancellation of the request by
792 // URLRequest::Delegate::OnSSLCertificateError.
793 void URLFetcherBadHTTPSTest::OnURLFetchComplete(
794 const URLFetcher* source) {
795 // This part is different from URLFetcherTest::OnURLFetchComplete
796 // because this test expects the request to be cancelled.
797 EXPECT_EQ(URLRequestStatus::CANCELED, source->GetStatus().status());
798 EXPECT_EQ(ERR_ABORTED, source->GetStatus().error());
799 EXPECT_EQ(-1, source->GetResponseCode());
800 EXPECT_TRUE(source->GetCookies().empty());
801 std::string data;
802 EXPECT_TRUE(source->GetResponseAsString(&data));
803 EXPECT_TRUE(data.empty());
804 CleanupAfterFetchComplete();
807 void URLFetcherCancelTest::CreateFetcher(const GURL& url) {
808 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this);
809 CancelTestURLRequestContextGetter* context_getter =
810 new CancelTestURLRequestContextGetter(io_message_loop_proxy().get(), url);
811 fetcher_->SetRequestContext(context_getter);
812 fetcher_->SetMaxRetriesOn5xx(2);
813 fetcher_->Start();
814 // We need to wait for the creation of the URLRequestContext, since we
815 // rely on it being destroyed as a signal to end the test.
816 context_getter->WaitForContextCreation();
817 CancelRequest();
820 void URLFetcherCancelTest::OnURLFetchComplete(
821 const URLFetcher* source) {
822 // We should have cancelled the request before completion.
823 ADD_FAILURE();
824 CleanupAfterFetchComplete();
827 void URLFetcherCancelTest::CancelRequest() {
828 delete fetcher_;
829 // The URLFetcher's test context will post a Quit task once it is
830 // deleted. So if this test simply hangs, it means cancellation
831 // did not work.
834 void URLFetcherMultipleAttemptTest::OnURLFetchComplete(
835 const URLFetcher* source) {
836 EXPECT_TRUE(source->GetStatus().is_success());
837 EXPECT_EQ(200, source->GetResponseCode()); // HTTP OK
838 std::string data;
839 EXPECT_TRUE(source->GetResponseAsString(&data));
840 EXPECT_FALSE(data.empty());
841 if (!data.empty() && data_.empty()) {
842 data_ = data;
843 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
844 io_message_loop_proxy().get(), request_context()));
845 fetcher_->Start();
846 } else {
847 EXPECT_EQ(data, data_);
848 CleanupAfterFetchComplete();
852 void URLFetcherFileTest::CreateFetcherForFile(const GURL& url,
853 const base::FilePath& file_path) {
854 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this);
855 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
856 io_message_loop_proxy().get(), request_context()));
858 // Use the IO message loop to do the file operations in this test.
859 fetcher_->SaveResponseToFileAtPath(file_path, io_message_loop_proxy());
860 fetcher_->Start();
863 void URLFetcherFileTest::CreateFetcherForTempFile(const GURL& url) {
864 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this);
865 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
866 io_message_loop_proxy().get(), request_context()));
868 // Use the IO message loop to do the file operations in this test.
869 fetcher_->SaveResponseToTemporaryFile(io_message_loop_proxy());
870 fetcher_->Start();
873 void URLFetcherFileTest::OnURLFetchComplete(const URLFetcher* source) {
874 if (expected_file_error_ == OK) {
875 EXPECT_TRUE(source->GetStatus().is_success());
876 EXPECT_EQ(OK, source->GetStatus().error());
877 EXPECT_EQ(200, source->GetResponseCode());
879 EXPECT_TRUE(source->GetResponseAsFilePath(
880 take_ownership_of_file_, &file_path_));
882 EXPECT_TRUE(base::ContentsEqual(expected_file_, file_path_));
883 } else {
884 EXPECT_FALSE(source->GetStatus().is_success());
885 EXPECT_EQ(expected_file_error_, source->GetStatus().error());
887 CleanupAfterFetchComplete();
890 TEST_F(URLFetcherTest, SameThreadsTest) {
891 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP,
892 SpawnedTestServer::kLocalhost,
893 base::FilePath(kDocRoot));
894 ASSERT_TRUE(test_server.Start());
896 // Create the fetcher on the main thread. Since IO will happen on the main
897 // thread, this will test URLFetcher's ability to do everything on one
898 // thread.
899 CreateFetcher(test_server.GetURL("defaultresponse"));
901 base::MessageLoop::current()->Run();
904 TEST_F(URLFetcherTest, DifferentThreadsTest) {
905 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP,
906 SpawnedTestServer::kLocalhost,
907 base::FilePath(kDocRoot));
908 ASSERT_TRUE(test_server.Start());
910 // Create a separate thread that will create the URLFetcher. The current
911 // (main) thread will do the IO, and when the fetch is complete it will
912 // terminate the main thread's message loop; then the other thread's
913 // message loop will be shut down automatically as the thread goes out of
914 // scope.
915 base::Thread t("URLFetcher test thread");
916 ASSERT_TRUE(t.Start());
917 t.message_loop()->PostTask(
918 FROM_HERE,
919 base::Bind(&URLFetcherTest::CreateFetcher,
920 base::Unretained(this),
921 test_server.GetURL("defaultresponse")));
923 base::MessageLoop::current()->Run();
926 void CancelAllOnIO() {
927 EXPECT_EQ(1, URLFetcherTest::GetNumFetcherCores());
928 URLFetcherImpl::CancelAll();
929 EXPECT_EQ(0, URLFetcherTest::GetNumFetcherCores());
932 // Tests to make sure CancelAll() will successfully cancel existing URLFetchers.
933 TEST_F(URLFetcherTest, CancelAll) {
934 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP,
935 SpawnedTestServer::kLocalhost,
936 base::FilePath(kDocRoot));
937 ASSERT_TRUE(test_server.Start());
938 EXPECT_EQ(0, GetNumFetcherCores());
940 CreateFetcher(test_server.GetURL("defaultresponse"));
941 io_message_loop_proxy()->PostTaskAndReply(
942 FROM_HERE, base::Bind(&CancelAllOnIO), base::MessageLoop::QuitClosure());
943 base::MessageLoop::current()->Run();
944 EXPECT_EQ(0, GetNumFetcherCores());
945 delete fetcher_;
948 TEST_F(URLFetcherMockDnsTest, DontRetryOnNetworkChangedByDefault) {
949 EXPECT_EQ(0, GetNumFetcherCores());
950 EXPECT_FALSE(resolver_.has_pending_requests());
952 // This posts a task to start the fetcher.
953 CreateFetcher(test_url_);
954 fetcher_->Start();
955 EXPECT_EQ(0, GetNumFetcherCores());
956 base::MessageLoop::current()->RunUntilIdle();
958 // The fetcher is now running, but is pending the host resolve.
959 EXPECT_EQ(1, GetNumFetcherCores());
960 EXPECT_TRUE(resolver_.has_pending_requests());
961 ASSERT_FALSE(completed_fetcher_);
963 // A network change notification aborts the connect job.
964 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
965 base::MessageLoop::current()->RunUntilIdle();
966 EXPECT_EQ(0, GetNumFetcherCores());
967 EXPECT_FALSE(resolver_.has_pending_requests());
968 ASSERT_TRUE(completed_fetcher_);
970 // And the owner of the fetcher gets the ERR_NETWORK_CHANGED error.
971 EXPECT_EQ(ERR_NETWORK_CHANGED, completed_fetcher_->GetStatus().error());
974 TEST_F(URLFetcherMockDnsTest, RetryOnNetworkChangedAndFail) {
975 EXPECT_EQ(0, GetNumFetcherCores());
976 EXPECT_FALSE(resolver_.has_pending_requests());
978 // This posts a task to start the fetcher.
979 CreateFetcher(test_url_);
980 fetcher_->SetAutomaticallyRetryOnNetworkChanges(3);
981 fetcher_->Start();
982 EXPECT_EQ(0, GetNumFetcherCores());
983 base::MessageLoop::current()->RunUntilIdle();
985 // The fetcher is now running, but is pending the host resolve.
986 EXPECT_EQ(1, GetNumFetcherCores());
987 EXPECT_TRUE(resolver_.has_pending_requests());
988 ASSERT_FALSE(completed_fetcher_);
990 // Make it fail 3 times.
991 for (int i = 0; i < 3; ++i) {
992 // A network change notification aborts the connect job.
993 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
994 base::MessageLoop::current()->RunUntilIdle();
996 // But the fetcher retries automatically.
997 EXPECT_EQ(1, GetNumFetcherCores());
998 EXPECT_TRUE(resolver_.has_pending_requests());
999 ASSERT_FALSE(completed_fetcher_);
1002 // A 4th failure doesn't trigger another retry, and propagates the error
1003 // to the owner of the fetcher.
1004 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
1005 base::MessageLoop::current()->RunUntilIdle();
1006 EXPECT_EQ(0, GetNumFetcherCores());
1007 EXPECT_FALSE(resolver_.has_pending_requests());
1008 ASSERT_TRUE(completed_fetcher_);
1010 // And the owner of the fetcher gets the ERR_NETWORK_CHANGED error.
1011 EXPECT_EQ(ERR_NETWORK_CHANGED, completed_fetcher_->GetStatus().error());
1014 TEST_F(URLFetcherMockDnsTest, RetryOnNetworkChangedAndSucceed) {
1015 EXPECT_EQ(0, GetNumFetcherCores());
1016 EXPECT_FALSE(resolver_.has_pending_requests());
1018 // This posts a task to start the fetcher.
1019 CreateFetcher(test_url_);
1020 fetcher_->SetAutomaticallyRetryOnNetworkChanges(3);
1021 fetcher_->Start();
1022 EXPECT_EQ(0, GetNumFetcherCores());
1023 base::MessageLoop::current()->RunUntilIdle();
1025 // The fetcher is now running, but is pending the host resolve.
1026 EXPECT_EQ(1, GetNumFetcherCores());
1027 EXPECT_TRUE(resolver_.has_pending_requests());
1028 ASSERT_FALSE(completed_fetcher_);
1030 // Make it fail 3 times.
1031 for (int i = 0; i < 3; ++i) {
1032 // A network change notification aborts the connect job.
1033 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
1034 base::MessageLoop::current()->RunUntilIdle();
1036 // But the fetcher retries automatically.
1037 EXPECT_EQ(1, GetNumFetcherCores());
1038 EXPECT_TRUE(resolver_.has_pending_requests());
1039 ASSERT_FALSE(completed_fetcher_);
1042 // Now let it succeed by resolving the pending request.
1043 resolver_.ResolveAllPending();
1044 base::MessageLoop::current()->Run();
1046 // URLFetcherMockDnsTest::OnURLFetchComplete() will quit the loop.
1047 EXPECT_EQ(0, GetNumFetcherCores());
1048 EXPECT_FALSE(resolver_.has_pending_requests());
1049 ASSERT_TRUE(completed_fetcher_);
1051 // This time the request succeeded.
1052 EXPECT_EQ(OK, completed_fetcher_->GetStatus().error());
1053 EXPECT_EQ(200, completed_fetcher_->GetResponseCode());
1056 TEST_F(URLFetcherPostTest, Basic) {
1057 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP,
1058 SpawnedTestServer::kLocalhost,
1059 base::FilePath(kDocRoot));
1060 ASSERT_TRUE(test_server.Start());
1062 CreateFetcher(test_server.GetURL("echo"));
1063 base::MessageLoop::current()->Run();
1066 TEST_F(URLFetcherPostFileTest, Basic) {
1067 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP,
1068 SpawnedTestServer::kLocalhost,
1069 base::FilePath(kDocRoot));
1070 ASSERT_TRUE(test_server.Start());
1072 CreateFetcher(test_server.GetURL("echo"));
1073 base::MessageLoop::current()->Run();
1076 TEST_F(URLFetcherPostFileTest, Range) {
1077 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP,
1078 SpawnedTestServer::kLocalhost,
1079 base::FilePath(kDocRoot));
1080 ASSERT_TRUE(test_server.Start());
1082 SetUploadRange(30, 100);
1084 CreateFetcher(test_server.GetURL("echo"));
1085 base::MessageLoop::current()->Run();
1088 TEST_F(URLFetcherEmptyPostTest, Basic) {
1089 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP,
1090 SpawnedTestServer::kLocalhost,
1091 base::FilePath(kDocRoot));
1092 ASSERT_TRUE(test_server.Start());
1094 CreateFetcher(test_server.GetURL("echo"));
1095 base::MessageLoop::current()->Run();
1098 TEST_F(URLFetcherUploadProgressTest, Basic) {
1099 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP,
1100 SpawnedTestServer::kLocalhost,
1101 base::FilePath(kDocRoot));
1102 ASSERT_TRUE(test_server.Start());
1104 CreateFetcher(test_server.GetURL("echo"));
1105 base::MessageLoop::current()->Run();
1108 TEST_F(URLFetcherDownloadProgressTest, Basic) {
1109 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP,
1110 SpawnedTestServer::kLocalhost,
1111 base::FilePath(kDocRoot));
1112 ASSERT_TRUE(test_server.Start());
1114 // Get a file large enough to require more than one read into
1115 // URLFetcher::Core's IOBuffer.
1116 static const char kFileToFetch[] = "animate1.gif";
1117 // Hardcoded file size - it cannot be easily fetched when a remote http server
1118 // is used for testing.
1119 static const int64 kFileSize = 19021;
1121 expected_total_ = kFileSize;
1123 CreateFetcher(test_server.GetURL(
1124 std::string(kTestServerFilePrefix) + kFileToFetch));
1126 base::MessageLoop::current()->Run();
1129 TEST_F(URLFetcherDownloadProgressCancelTest, CancelWhileProgressReport) {
1130 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP,
1131 SpawnedTestServer::kLocalhost,
1132 base::FilePath(kDocRoot));
1133 ASSERT_TRUE(test_server.Start());
1135 // Get a file large enough to require more than one read into
1136 // URLFetcher::Core's IOBuffer.
1137 static const char kFileToFetch[] = "animate1.gif";
1138 CreateFetcher(test_server.GetURL(
1139 std::string(kTestServerFilePrefix) + kFileToFetch));
1141 base::MessageLoop::current()->Run();
1144 TEST_F(URLFetcherHeadersTest, Headers) {
1145 SpawnedTestServer test_server(
1146 SpawnedTestServer::TYPE_HTTP,
1147 SpawnedTestServer::kLocalhost,
1148 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
1149 ASSERT_TRUE(test_server.Start());
1151 CreateFetcher(test_server.GetURL("files/with-headers.html"));
1152 base::MessageLoop::current()->Run();
1153 // The actual tests are in the URLFetcherHeadersTest fixture.
1156 TEST_F(URLFetcherSocketAddressTest, SocketAddress) {
1157 SpawnedTestServer test_server(
1158 SpawnedTestServer::TYPE_HTTP,
1159 SpawnedTestServer::kLocalhost,
1160 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
1161 ASSERT_TRUE(test_server.Start());
1162 expected_port_ = test_server.host_port_pair().port();
1164 // Reusing "with-headers.html" but doesn't really matter.
1165 CreateFetcher(test_server.GetURL("files/with-headers.html"));
1166 base::MessageLoop::current()->Run();
1167 // The actual tests are in the URLFetcherSocketAddressTest fixture.
1170 TEST_F(URLFetcherStopOnRedirectTest, StopOnRedirect) {
1171 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP,
1172 SpawnedTestServer::kLocalhost,
1173 base::FilePath(kDocRoot));
1174 ASSERT_TRUE(test_server.Start());
1176 CreateFetcher(
1177 test_server.GetURL(std::string("server-redirect?") + kRedirectTarget));
1178 base::MessageLoop::current()->Run();
1179 EXPECT_TRUE(callback_called_);
1182 TEST_F(URLFetcherProtectTest, Overload) {
1183 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP,
1184 SpawnedTestServer::kLocalhost,
1185 base::FilePath(kDocRoot));
1186 ASSERT_TRUE(test_server.Start());
1188 GURL url(test_server.GetURL("defaultresponse"));
1190 // Registers an entry for test url. It only allows 3 requests to be sent
1191 // in 200 milliseconds.
1192 scoped_refptr<URLRequestThrottlerEntry> entry(
1193 new URLRequestThrottlerEntry(request_context()->throttler_manager(),
1194 std::string(),
1195 200,
1198 2.0,
1199 0.0,
1200 256));
1201 request_context()->throttler_manager()
1202 ->OverrideEntryForTests(url, entry.get());
1204 CreateFetcher(url);
1206 base::MessageLoop::current()->Run();
1209 TEST_F(URLFetcherProtectTest, ServerUnavailable) {
1210 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP,
1211 SpawnedTestServer::kLocalhost,
1212 base::FilePath(kDocRoot));
1213 ASSERT_TRUE(test_server.Start());
1215 GURL url(test_server.GetURL("files/server-unavailable.html"));
1217 // Registers an entry for test url. The backoff time is calculated by:
1218 // new_backoff = 2.0 * old_backoff + 0
1219 // and maximum backoff time is 256 milliseconds.
1220 // Maximum retries allowed is set to 11.
1221 scoped_refptr<URLRequestThrottlerEntry> entry(
1222 new URLRequestThrottlerEntry(request_context()->throttler_manager(),
1223 std::string(),
1224 200,
1227 2.0,
1228 0.0,
1229 256));
1230 request_context()->throttler_manager()
1231 ->OverrideEntryForTests(url, entry.get());
1233 CreateFetcher(url);
1235 base::MessageLoop::current()->Run();
1238 TEST_F(URLFetcherProtectTestPassedThrough, ServerUnavailablePropagateResponse) {
1239 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP,
1240 SpawnedTestServer::kLocalhost,
1241 base::FilePath(kDocRoot));
1242 ASSERT_TRUE(test_server.Start());
1244 GURL url(test_server.GetURL("files/server-unavailable.html"));
1246 // Registers an entry for test url. The backoff time is calculated by:
1247 // new_backoff = 2.0 * old_backoff + 0
1248 // and maximum backoff time is 150000 milliseconds.
1249 // Maximum retries allowed is set to 11.
1250 scoped_refptr<URLRequestThrottlerEntry> entry(
1251 new URLRequestThrottlerEntry(request_context()->throttler_manager(),
1252 std::string(),
1253 200,
1255 100,
1256 2.0,
1257 0.0,
1258 150000));
1259 // Total time if *not* for not doing automatic backoff would be 150s.
1260 // In reality it should be "as soon as server responds".
1261 request_context()->throttler_manager()
1262 ->OverrideEntryForTests(url, entry.get());
1264 CreateFetcher(url);
1266 base::MessageLoop::current()->Run();
1269 TEST_F(URLFetcherBadHTTPSTest, BadHTTPSTest) {
1270 SpawnedTestServer::SSLOptions ssl_options(
1271 SpawnedTestServer::SSLOptions::CERT_EXPIRED);
1272 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTPS,
1273 ssl_options,
1274 base::FilePath(kDocRoot));
1275 ASSERT_TRUE(test_server.Start());
1277 CreateFetcher(test_server.GetURL("defaultresponse"));
1278 base::MessageLoop::current()->Run();
1281 TEST_F(URLFetcherCancelTest, ReleasesContext) {
1282 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP,
1283 SpawnedTestServer::kLocalhost,
1284 base::FilePath(kDocRoot));
1285 ASSERT_TRUE(test_server.Start());
1287 GURL url(test_server.GetURL("files/server-unavailable.html"));
1289 // Create a separate thread that will create the URLFetcher. The current
1290 // (main) thread will do the IO, and when the fetch is complete it will
1291 // terminate the main thread's message loop; then the other thread's
1292 // message loop will be shut down automatically as the thread goes out of
1293 // scope.
1294 base::Thread t("URLFetcher test thread");
1295 ASSERT_TRUE(t.Start());
1296 t.message_loop()->PostTask(
1297 FROM_HERE,
1298 base::Bind(&URLFetcherCancelTest::CreateFetcher,
1299 base::Unretained(this), url));
1301 base::MessageLoop::current()->Run();
1304 TEST_F(URLFetcherCancelTest, CancelWhileDelayedStartTaskPending) {
1305 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP,
1306 SpawnedTestServer::kLocalhost,
1307 base::FilePath(kDocRoot));
1308 ASSERT_TRUE(test_server.Start());
1310 GURL url(test_server.GetURL("files/server-unavailable.html"));
1312 // Register an entry for test url.
1313 // Using a sliding window of 4 seconds, and max of 1 request, under a fast
1314 // run we expect to have a 4 second delay when posting the Start task.
1315 scoped_refptr<URLRequestThrottlerEntry> entry(
1316 new URLRequestThrottlerEntry(request_context()->throttler_manager(),
1317 std::string(),
1318 4000,
1320 2000,
1321 2.0,
1322 0.0,
1323 4000));
1324 request_context()->throttler_manager()
1325 ->OverrideEntryForTests(url, entry.get());
1326 // Fake that a request has just started.
1327 entry->ReserveSendingTimeForNextRequest(base::TimeTicks());
1329 // The next request we try to send will be delayed by ~4 seconds.
1330 // The slower the test runs, the less the delay will be (since it takes the
1331 // time difference from now).
1333 base::Thread t("URLFetcher test thread");
1334 ASSERT_TRUE(t.Start());
1335 t.message_loop()->PostTask(
1336 FROM_HERE,
1337 base::Bind(&URLFetcherTest::CreateFetcher, base::Unretained(this), url));
1339 base::MessageLoop::current()->Run();
1342 TEST_F(URLFetcherMultipleAttemptTest, SameData) {
1343 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP,
1344 SpawnedTestServer::kLocalhost,
1345 base::FilePath(kDocRoot));
1346 ASSERT_TRUE(test_server.Start());
1348 // Create the fetcher on the main thread. Since IO will happen on the main
1349 // thread, this will test URLFetcher's ability to do everything on one
1350 // thread.
1351 CreateFetcher(test_server.GetURL("defaultresponse"));
1353 base::MessageLoop::current()->Run();
1356 TEST_F(URLFetcherFileTest, SmallGet) {
1357 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP,
1358 SpawnedTestServer::kLocalhost,
1359 base::FilePath(kDocRoot));
1360 ASSERT_TRUE(test_server.Start());
1362 base::ScopedTempDir temp_dir;
1363 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
1365 // Get a small file.
1366 static const char kFileToFetch[] = "simple.html";
1367 expected_file_ = test_server.GetDocumentRoot().AppendASCII(kFileToFetch);
1368 CreateFetcherForFile(
1369 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch),
1370 temp_dir.path().AppendASCII(kFileToFetch));
1372 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit().
1374 ASSERT_FALSE(base::PathExists(file_path_))
1375 << file_path_.value() << " not removed.";
1378 TEST_F(URLFetcherFileTest, LargeGet) {
1379 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP,
1380 SpawnedTestServer::kLocalhost,
1381 base::FilePath(kDocRoot));
1382 ASSERT_TRUE(test_server.Start());
1384 base::ScopedTempDir temp_dir;
1385 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
1387 // Get a file large enough to require more than one read into
1388 // URLFetcher::Core's IOBuffer.
1389 static const char kFileToFetch[] = "animate1.gif";
1390 expected_file_ = test_server.GetDocumentRoot().AppendASCII(kFileToFetch);
1391 CreateFetcherForFile(
1392 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch),
1393 temp_dir.path().AppendASCII(kFileToFetch));
1395 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit().
1398 TEST_F(URLFetcherFileTest, SavedOutputFileOwnerhisp) {
1399 // If the caller takes the ownership of the output file, the file should
1400 // persist even after URLFetcher is gone. If not, the file must be deleted.
1401 const bool kTake[] = {false, true};
1402 for (size_t i = 0; i < arraysize(kTake); ++i) {
1403 take_ownership_of_file_ = kTake[i];
1404 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP,
1405 SpawnedTestServer::kLocalhost,
1406 base::FilePath(kDocRoot));
1407 ASSERT_TRUE(test_server.Start());
1409 base::ScopedTempDir temp_dir;
1410 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
1412 // Get a small file.
1413 static const char kFileToFetch[] = "simple.html";
1414 expected_file_ = test_server.GetDocumentRoot().AppendASCII(kFileToFetch);
1415 CreateFetcherForFile(
1416 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch),
1417 temp_dir.path().AppendASCII(kFileToFetch));
1419 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit().
1421 base::MessageLoop::current()->RunUntilIdle();
1422 ASSERT_EQ(kTake[i], base::PathExists(file_path_)) <<
1423 "FilePath: " << file_path_.value();
1427 TEST_F(URLFetcherFileTest, OverwriteExistingFile) {
1428 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP,
1429 SpawnedTestServer::kLocalhost,
1430 base::FilePath(kDocRoot));
1431 ASSERT_TRUE(test_server.Start());
1433 base::ScopedTempDir temp_dir;
1434 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
1436 // Create a file before trying to fetch.
1437 static const char kFileToFetch[] = "simple.html";
1438 std::string data(10000, '?'); // Meant to be larger than simple.html.
1439 file_path_ = temp_dir.path().AppendASCII(kFileToFetch);
1440 ASSERT_EQ(static_cast<int>(data.size()),
1441 base::WriteFile(file_path_, data.data(), data.size()));
1442 ASSERT_TRUE(base::PathExists(file_path_));
1443 expected_file_ = test_server.GetDocumentRoot().AppendASCII(kFileToFetch);
1444 ASSERT_FALSE(base::ContentsEqual(file_path_, expected_file_));
1446 // Get a small file.
1447 CreateFetcherForFile(
1448 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch),
1449 file_path_);
1451 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit().
1454 TEST_F(URLFetcherFileTest, TryToOverwriteDirectory) {
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 // Create a directory before trying to fetch.
1464 static const char kFileToFetch[] = "simple.html";
1465 file_path_ = temp_dir.path().AppendASCII(kFileToFetch);
1466 ASSERT_TRUE(base::CreateDirectory(file_path_));
1467 ASSERT_TRUE(base::PathExists(file_path_));
1469 // Get a small file.
1470 expected_file_error_ = ERR_ACCESS_DENIED;
1471 expected_file_ = test_server.GetDocumentRoot().AppendASCII(kFileToFetch);
1472 CreateFetcherForFile(
1473 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch),
1474 file_path_);
1476 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit().
1478 base::MessageLoop::current()->RunUntilIdle();
1481 TEST_F(URLFetcherFileTest, SmallGetToTempFile) {
1482 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP,
1483 SpawnedTestServer::kLocalhost,
1484 base::FilePath(kDocRoot));
1485 ASSERT_TRUE(test_server.Start());
1487 // Get a small file.
1488 static const char kFileToFetch[] = "simple.html";
1489 expected_file_ = test_server.GetDocumentRoot().AppendASCII(kFileToFetch);
1490 CreateFetcherForTempFile(
1491 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch));
1493 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit().
1495 ASSERT_FALSE(base::PathExists(file_path_))
1496 << file_path_.value() << " not removed.";
1499 TEST_F(URLFetcherFileTest, LargeGetToTempFile) {
1500 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP,
1501 SpawnedTestServer::kLocalhost,
1502 base::FilePath(kDocRoot));
1503 ASSERT_TRUE(test_server.Start());
1505 // Get a file large enough to require more than one read into
1506 // URLFetcher::Core's IOBuffer.
1507 static const char kFileToFetch[] = "animate1.gif";
1508 expected_file_ = test_server.GetDocumentRoot().AppendASCII(kFileToFetch);
1509 CreateFetcherForTempFile(test_server.GetURL(
1510 std::string(kTestServerFilePrefix) + kFileToFetch));
1512 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit().
1515 TEST_F(URLFetcherFileTest, SavedOutputTempFileOwnerhisp) {
1516 // If the caller takes the ownership of the temp file, the file should persist
1517 // even after URLFetcher is gone. If not, the file must be deleted.
1518 const bool kTake[] = {false, true};
1519 for (size_t i = 0; i < arraysize(kTake); ++i) {
1520 take_ownership_of_file_ = kTake[i];
1522 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP,
1523 SpawnedTestServer::kLocalhost,
1524 base::FilePath(kDocRoot));
1525 ASSERT_TRUE(test_server.Start());
1527 // Get a small file.
1528 static const char kFileToFetch[] = "simple.html";
1529 expected_file_ = test_server.GetDocumentRoot().AppendASCII(kFileToFetch);
1530 CreateFetcherForTempFile(test_server.GetURL(
1531 std::string(kTestServerFilePrefix) + kFileToFetch));
1533 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit().
1535 base::MessageLoop::current()->RunUntilIdle();
1536 ASSERT_EQ(kTake[i], base::PathExists(file_path_)) <<
1537 "FilePath: " << file_path_.value();
1541 } // namespace
1543 } // namespace net