Redirect back to signin page if we complete signin but don't have password.
[chromium-blink-merge.git] / net / url_request / url_fetcher_impl_unittest.cc
blob5e22ce06a072f76b7c6aea19485de8e8187a8fc1
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 <string>
9 #include "base/bind.h"
10 #include "base/file_util.h"
11 #include "base/files/scoped_temp_dir.h"
12 #include "base/message_loop_proxy.h"
13 #include "base/stringprintf.h"
14 #include "base/synchronization/waitable_event.h"
15 #include "base/threading/thread.h"
16 #include "build/build_config.h"
17 #include "crypto/nss_util.h"
18 #include "net/base/mock_host_resolver.h"
19 #include "net/base/network_change_notifier.h"
20 #include "net/http/http_response_headers.h"
21 #include "net/test/test_server.h"
22 #include "net/url_request/url_fetcher_delegate.h"
23 #include "net/url_request/url_request_context_getter.h"
24 #include "net/url_request/url_request_test_util.h"
25 #include "net/url_request/url_request_throttler_manager.h"
26 #include "testing/gtest/include/gtest/gtest.h"
28 #if defined(USE_NSS) || defined(OS_IOS)
29 #include "net/ocsp/nss_ocsp.h"
30 #endif
32 namespace net {
34 using base::Time;
35 using base::TimeDelta;
37 // TODO(eroman): Add a regression test for http://crbug.com/40505.
39 namespace {
41 // TODO(akalin): Move all the test data to somewhere under net/.
42 const base::FilePath::CharType kDocRoot[] =
43 FILE_PATH_LITERAL("chrome/test/data");
44 const char kTestServerFilePrefix[] = "files/";
46 class ThrottlingTestURLRequestContext : public TestURLRequestContext {
47 public:
48 ThrottlingTestURLRequestContext() : TestURLRequestContext(true) {
49 set_throttler_manager(&throttler_manager_);
50 Init();
51 DCHECK(throttler_manager() != NULL);
54 private:
55 URLRequestThrottlerManager throttler_manager_;
58 class ThrottlingTestURLRequestContextGetter
59 : public TestURLRequestContextGetter {
60 public:
61 ThrottlingTestURLRequestContextGetter(
62 base::MessageLoopProxy* io_message_loop_proxy,
63 TestURLRequestContext* request_context)
64 : TestURLRequestContextGetter(io_message_loop_proxy),
65 context_(request_context) {
68 // TestURLRequestContextGetter:
69 virtual TestURLRequestContext* GetURLRequestContext() OVERRIDE {
70 return context_;
73 protected:
74 virtual ~ThrottlingTestURLRequestContextGetter() {}
76 TestURLRequestContext* const context_;
79 } // namespace
81 class URLFetcherTest : public testing::Test,
82 public URLFetcherDelegate {
83 public:
84 URLFetcherTest()
85 : fetcher_(NULL),
86 context_(NULL) {
89 static int GetNumFetcherCores() {
90 return URLFetcherImpl::GetNumFetcherCores();
93 // Creates a URLFetcher, using the program's main thread to do IO.
94 virtual void CreateFetcher(const GURL& url);
96 // URLFetcherDelegate:
97 // Subclasses that override this should either call this function or
98 // CleanupAfterFetchComplete() at the end of their processing, depending on
99 // whether they want to check for a non-empty HTTP 200 response or not.
100 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE;
102 // Deletes |fetcher| and terminates the message loop.
103 void CleanupAfterFetchComplete();
105 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy() {
106 return io_message_loop_proxy_;
109 TestURLRequestContext* request_context() {
110 return context_.get();
113 protected:
114 // testing::Test:
115 virtual void SetUp() OVERRIDE {
116 testing::Test::SetUp();
118 context_.reset(new ThrottlingTestURLRequestContext());
119 io_message_loop_proxy_ = base::MessageLoopProxy::current();
121 #if defined(USE_NSS) || defined(OS_IOS)
122 crypto::EnsureNSSInit();
123 EnsureNSSHttpIOInit();
124 #endif
127 virtual void TearDown() OVERRIDE {
128 #if defined(USE_NSS) || defined(OS_IOS)
129 ShutdownNSSHttpIO();
130 #endif
133 // URLFetcher is designed to run on the main UI thread, but in our tests
134 // we assume that the current thread is the IO thread where the URLFetcher
135 // dispatches its requests to. When we wish to simulate being used from
136 // a UI thread, we dispatch a worker thread to do so.
137 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_;
139 URLFetcherImpl* fetcher_;
140 scoped_ptr<TestURLRequestContext> context_;
143 // A test fixture that uses a MockHostResolver, so that name resolutions can
144 // be manipulated by the tests to keep connections in the resolving state.
145 class URLFetcherMockDnsTest : public URLFetcherTest {
146 public:
147 // testing::Test:
148 virtual void SetUp() OVERRIDE;
150 // URLFetcherTest:
151 virtual void CreateFetcher(const GURL& url) OVERRIDE;
153 // URLFetcherDelegate:
154 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE;
156 protected:
157 GURL test_url_;
158 scoped_ptr<TestServer> test_server_;
159 MockHostResolver resolver_;
160 scoped_ptr<URLFetcher> completed_fetcher_;
163 void URLFetcherTest::CreateFetcher(const GURL& url) {
164 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this);
165 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
166 io_message_loop_proxy(), request_context()));
167 fetcher_->Start();
170 void URLFetcherTest::OnURLFetchComplete(const URLFetcher* source) {
171 EXPECT_TRUE(source->GetStatus().is_success());
172 EXPECT_EQ(200, source->GetResponseCode()); // HTTP OK
174 std::string data;
175 EXPECT_TRUE(source->GetResponseAsString(&data));
176 EXPECT_FALSE(data.empty());
178 CleanupAfterFetchComplete();
181 void URLFetcherTest::CleanupAfterFetchComplete() {
182 delete fetcher_; // Have to delete this here and not in the destructor,
183 // because the destructor won't necessarily run on the
184 // same thread that CreateFetcher() did.
186 io_message_loop_proxy()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
187 // If the current message loop is not the IO loop, it will be shut down when
188 // the main loop returns and this thread subsequently goes out of scope.
191 void URLFetcherMockDnsTest::SetUp() {
192 URLFetcherTest::SetUp();
194 resolver_.set_ondemand_mode(true);
195 resolver_.rules()->AddRule("example.com", "127.0.0.1");
197 context_.reset(new TestURLRequestContext(true));
198 context_->set_host_resolver(&resolver_);
199 context_->Init();
201 test_server_.reset(new TestServer(TestServer::TYPE_HTTP,
202 TestServer::kLocalhost,
203 base::FilePath(kDocRoot)));
204 ASSERT_TRUE(test_server_->Start());
206 // test_server_.GetURL() returns a URL with 127.0.0.1 (kLocalhost), that is
207 // immediately resolved by the MockHostResolver. Use a hostname instead to
208 // trigger an async resolve.
209 test_url_ = GURL(
210 base::StringPrintf("http://example.com:%d/defaultresponse",
211 test_server_->host_port_pair().port()));
212 ASSERT_TRUE(test_url_.is_valid());
215 void URLFetcherMockDnsTest::CreateFetcher(const GURL& url) {
216 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this);
217 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
218 io_message_loop_proxy(), request_context()));
221 void URLFetcherMockDnsTest::OnURLFetchComplete(const URLFetcher* source) {
222 io_message_loop_proxy()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
223 ASSERT_EQ(fetcher_, source);
224 EXPECT_EQ(test_url_, source->GetOriginalURL());
225 completed_fetcher_.reset(fetcher_);
228 namespace {
230 // Version of URLFetcherTest that does a POST instead
231 class URLFetcherPostTest : public URLFetcherTest {
232 public:
233 // URLFetcherTest:
234 virtual void CreateFetcher(const GURL& url) OVERRIDE;
236 // URLFetcherDelegate:
237 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE;
240 // Version of URLFetcherTest that does a POST instead with empty upload body
241 class URLFetcherEmptyPostTest : public URLFetcherTest {
242 public:
243 // URLFetcherTest:
244 virtual void CreateFetcher(const GURL& url) OVERRIDE;
246 // URLFetcherDelegate:
247 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE;
250 // Version of URLFetcherTest that tests download progress reports.
251 class URLFetcherDownloadProgressTest : public URLFetcherTest {
252 public:
253 URLFetcherDownloadProgressTest()
254 : previous_progress_(0),
255 expected_total_(0) {
258 // URLFetcherTest:
259 virtual void CreateFetcher(const GURL& url) OVERRIDE;
261 // URLFetcherDelegate:
262 virtual void OnURLFetchDownloadProgress(const URLFetcher* source,
263 int64 current,
264 int64 total) OVERRIDE;
266 protected:
267 // Download progress returned by the previous callback.
268 int64 previous_progress_;
269 // Size of the file being downloaded, known in advance (provided by each test
270 // case).
271 int64 expected_total_;
274 // Version of URLFetcherTest that tests progress reports at cancellation.
275 class URLFetcherDownloadProgressCancelTest : public URLFetcherTest {
276 public:
277 // URLFetcherTest:
278 virtual void CreateFetcher(const GURL& url) OVERRIDE;
280 // URLFetcherDelegate:
281 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE;
282 virtual void OnURLFetchDownloadProgress(const URLFetcher* source,
283 int64 current,
284 int64 total) OVERRIDE;
285 protected:
286 bool cancelled_;
289 // Version of URLFetcherTest that tests upload progress reports.
290 class URLFetcherUploadProgressTest : public URLFetcherTest {
291 public:
292 // URLFetcherTest:
293 virtual void CreateFetcher(const GURL& url) OVERRIDE;
295 // URLFetcherDelegate:
296 virtual void OnURLFetchUploadProgress(const URLFetcher* source,
297 int64 current,
298 int64 total) OVERRIDE;
299 protected:
300 int64 previous_progress_;
301 std::string chunk_;
302 int64 number_of_chunks_added_;
305 // Version of URLFetcherTest that tests headers.
306 class URLFetcherHeadersTest : public URLFetcherTest {
307 public:
308 // URLFetcherDelegate:
309 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE;
312 // Version of URLFetcherTest that tests SocketAddress.
313 class URLFetcherSocketAddressTest : public URLFetcherTest {
314 public:
315 // URLFetcherDelegate:
316 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE;
317 protected:
318 std::string expected_host_;
319 uint16 expected_port_;
322 // Version of URLFetcherTest that tests stopping on a redirect.
323 class URLFetcherStopOnRedirectTest : public URLFetcherTest {
324 public:
325 URLFetcherStopOnRedirectTest();
326 virtual ~URLFetcherStopOnRedirectTest();
328 // URLFetcherTest:
329 virtual void CreateFetcher(const GURL& url) OVERRIDE;
331 // URLFetcherDelegate:
332 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE;
334 protected:
335 // The URL we should be redirected to.
336 static const char* kRedirectTarget;
338 bool callback_called_; // Set to true in OnURLFetchComplete().
341 // Version of URLFetcherTest that tests overload protection.
342 class URLFetcherProtectTest : public URLFetcherTest {
343 public:
344 // URLFetcherTest:
345 virtual void CreateFetcher(const GURL& url) OVERRIDE;
347 // URLFetcherDelegate:
348 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE;
349 private:
350 Time start_time_;
353 // Version of URLFetcherTest that tests overload protection, when responses
354 // passed through.
355 class URLFetcherProtectTestPassedThrough : public URLFetcherTest {
356 public:
357 // URLFetcherTest:
358 virtual void CreateFetcher(const GURL& url) OVERRIDE;
360 // URLFetcherDelegate:
361 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE;
362 private:
363 Time start_time_;
366 // Version of URLFetcherTest that tests bad HTTPS requests.
367 class URLFetcherBadHTTPSTest : public URLFetcherTest {
368 public:
369 URLFetcherBadHTTPSTest();
371 // URLFetcherDelegate:
372 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE;
374 private:
375 base::FilePath cert_dir_;
378 // Version of URLFetcherTest that tests request cancellation on shutdown.
379 class URLFetcherCancelTest : public URLFetcherTest {
380 public:
381 // URLFetcherTest:
382 virtual void CreateFetcher(const GURL& url) OVERRIDE;
384 // URLFetcherDelegate:
385 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE;
387 void CancelRequest();
390 // Version of TestURLRequestContext that posts a Quit task to the IO
391 // thread once it is deleted.
392 class CancelTestURLRequestContext : public ThrottlingTestURLRequestContext {
393 public:
394 explicit CancelTestURLRequestContext() {
397 private:
398 virtual ~CancelTestURLRequestContext() {
399 // The d'tor should execute in the IO thread. Post the quit task to the
400 // current thread.
401 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
405 class CancelTestURLRequestContextGetter
406 : public TestURLRequestContextGetter {
407 public:
408 CancelTestURLRequestContextGetter(
409 base::MessageLoopProxy* io_message_loop_proxy,
410 const GURL& throttle_for_url)
411 : TestURLRequestContextGetter(io_message_loop_proxy),
412 io_message_loop_proxy_(io_message_loop_proxy),
413 context_created_(false, false),
414 throttle_for_url_(throttle_for_url) {
417 // TestURLRequestContextGetter:
418 virtual TestURLRequestContext* GetURLRequestContext() OVERRIDE {
419 if (!context_.get()) {
420 context_.reset(new CancelTestURLRequestContext());
421 DCHECK(context_->throttler_manager());
423 // Registers an entry for test url. The backoff time is calculated by:
424 // new_backoff = 2.0 * old_backoff + 0
425 // The initial backoff is 2 seconds and maximum backoff is 4 seconds.
426 // Maximum retries allowed is set to 2.
427 scoped_refptr<URLRequestThrottlerEntry> entry(
428 new URLRequestThrottlerEntry(
429 context_->throttler_manager(),
430 "", 200, 3, 2000, 2.0, 0.0, 4000));
431 context_->throttler_manager()->OverrideEntryForTests(
432 throttle_for_url_, entry);
434 context_created_.Signal();
436 return context_.get();
439 virtual scoped_refptr<base::MessageLoopProxy> GetIOMessageLoopProxy() const {
440 return io_message_loop_proxy_;
443 void WaitForContextCreation() {
444 context_created_.Wait();
447 protected:
448 virtual ~CancelTestURLRequestContextGetter() {}
450 private:
451 scoped_ptr<TestURLRequestContext> context_;
452 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_;
453 base::WaitableEvent context_created_;
454 GURL throttle_for_url_;
457 // Version of URLFetcherTest that tests retying the same request twice.
458 class URLFetcherMultipleAttemptTest : public URLFetcherTest {
459 public:
460 // URLFetcherDelegate:
461 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE;
462 private:
463 std::string data_;
466 class URLFetcherFileTest : public URLFetcherTest {
467 public:
468 URLFetcherFileTest() : take_ownership_of_file_(false),
469 expected_file_error_(base::PLATFORM_FILE_OK) {}
471 void CreateFetcherForFile(const GURL& url, const base::FilePath& file_path);
472 void CreateFetcherForTempFile(const GURL& url);
474 // URLFetcherDelegate:
475 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE;
477 protected:
478 base::FilePath expected_file_;
479 base::FilePath file_path_;
481 // Set by the test. Used in OnURLFetchComplete() to decide if
482 // the URLFetcher should own the temp file, so that we can test
483 // disowning prevents the file from being deleted.
484 bool take_ownership_of_file_;
486 // Expected file error code for the test.
487 // PLATFORM_FILE_OK when expecting success.
488 base::PlatformFileError expected_file_error_;
491 void URLFetcherPostTest::CreateFetcher(const GURL& url) {
492 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this);
493 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
494 io_message_loop_proxy(), request_context()));
495 fetcher_->SetUploadData("application/x-www-form-urlencoded",
496 "bobsyeruncle");
497 fetcher_->Start();
500 void URLFetcherPostTest::OnURLFetchComplete(const URLFetcher* source) {
501 std::string data;
502 EXPECT_TRUE(source->GetResponseAsString(&data));
503 EXPECT_EQ(std::string("bobsyeruncle"), data);
504 URLFetcherTest::OnURLFetchComplete(source);
507 void URLFetcherEmptyPostTest::CreateFetcher(const GURL& url) {
508 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this);
509 fetcher_->SetRequestContext(new TestURLRequestContextGetter(
510 io_message_loop_proxy()));
511 fetcher_->SetUploadData("text/plain", "");
512 fetcher_->Start();
515 void URLFetcherEmptyPostTest::OnURLFetchComplete(const URLFetcher* source) {
516 EXPECT_TRUE(source->GetStatus().is_success());
517 EXPECT_EQ(200, source->GetResponseCode()); // HTTP OK
519 std::string data;
520 EXPECT_TRUE(source->GetResponseAsString(&data));
521 EXPECT_TRUE(data.empty());
523 CleanupAfterFetchComplete();
524 // Do not call the super class method URLFetcherTest::OnURLFetchComplete,
525 // since it expects a non-empty response.
528 void URLFetcherDownloadProgressTest::CreateFetcher(const GURL& url) {
529 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this);
530 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
531 io_message_loop_proxy(), request_context()));
532 fetcher_->Start();
535 void URLFetcherDownloadProgressTest::OnURLFetchDownloadProgress(
536 const URLFetcher* source, int64 progress, int64 total) {
537 // Increasing between 0 and total.
538 EXPECT_LE(0, progress);
539 EXPECT_GE(total, progress);
540 EXPECT_LE(previous_progress_, progress);
541 EXPECT_EQ(expected_total_, total);
542 previous_progress_ = progress;
545 void URLFetcherDownloadProgressCancelTest::CreateFetcher(const GURL& url) {
546 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this);
547 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
548 io_message_loop_proxy(), request_context()));
549 cancelled_ = false;
550 fetcher_->Start();
553 void URLFetcherDownloadProgressCancelTest::OnURLFetchDownloadProgress(
554 const URLFetcher* source, int64 current, int64 total) {
555 EXPECT_FALSE(cancelled_);
556 if (!cancelled_) {
557 cancelled_ = true;
558 CleanupAfterFetchComplete();
562 void URLFetcherDownloadProgressCancelTest::OnURLFetchComplete(
563 const URLFetcher* source) {
564 // Should have been cancelled.
565 ADD_FAILURE();
566 CleanupAfterFetchComplete();
569 void URLFetcherUploadProgressTest::CreateFetcher(const GURL& url) {
570 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this);
571 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
572 io_message_loop_proxy(), request_context()));
573 previous_progress_ = 0;
574 // Large enough data to require more than one read from UploadDataStream.
575 chunk_.assign(1<<16, 'a');
576 // Use chunked upload to wait for a timer event of progress notification.
577 fetcher_->SetChunkedUpload("application/x-www-form-urlencoded");
578 fetcher_->Start();
579 number_of_chunks_added_ = 1;
580 fetcher_->AppendChunkToUpload(chunk_, false);
583 void URLFetcherUploadProgressTest::OnURLFetchUploadProgress(
584 const URLFetcher* source, int64 current, int64 total) {
585 // Increasing between 0 and total.
586 EXPECT_LE(0, current);
587 EXPECT_GE(static_cast<int64>(chunk_.size()) * number_of_chunks_added_,
588 current);
589 EXPECT_LE(previous_progress_, current);
590 previous_progress_ = current;
591 EXPECT_EQ(-1, total);
593 if (number_of_chunks_added_ < 2) {
594 number_of_chunks_added_ += 1;
595 fetcher_->AppendChunkToUpload(chunk_, true);
599 void URLFetcherHeadersTest::OnURLFetchComplete(
600 const URLFetcher* source) {
601 std::string header;
602 EXPECT_TRUE(source->GetResponseHeaders()->GetNormalizedHeader("cache-control",
603 &header));
604 EXPECT_EQ("private", header);
605 URLFetcherTest::OnURLFetchComplete(source);
608 void URLFetcherSocketAddressTest::OnURLFetchComplete(
609 const URLFetcher* source) {
610 EXPECT_EQ("127.0.0.1", source->GetSocketAddress().host());
611 EXPECT_EQ(expected_port_, source->GetSocketAddress().port());
612 URLFetcherTest::OnURLFetchComplete(source);
615 // static
616 const char* URLFetcherStopOnRedirectTest::kRedirectTarget =
617 "http://redirect.target.com";
619 URLFetcherStopOnRedirectTest::URLFetcherStopOnRedirectTest()
620 : callback_called_(false) {
623 URLFetcherStopOnRedirectTest::~URLFetcherStopOnRedirectTest() {
626 void URLFetcherStopOnRedirectTest::CreateFetcher(const GURL& url) {
627 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this);
628 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
629 io_message_loop_proxy(), request_context()));
630 fetcher_->SetStopOnRedirect(true);
631 fetcher_->Start();
634 void URLFetcherStopOnRedirectTest::OnURLFetchComplete(
635 const URLFetcher* source) {
636 callback_called_ = true;
637 EXPECT_EQ(GURL(kRedirectTarget), source->GetURL());
638 EXPECT_EQ(URLRequestStatus::CANCELED, source->GetStatus().status());
639 EXPECT_EQ(ERR_ABORTED, source->GetStatus().error());
640 EXPECT_EQ(301, source->GetResponseCode());
641 CleanupAfterFetchComplete();
644 void URLFetcherProtectTest::CreateFetcher(const GURL& url) {
645 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this);
646 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
647 io_message_loop_proxy(), request_context()));
648 start_time_ = Time::Now();
649 fetcher_->SetMaxRetriesOn5xx(11);
650 fetcher_->Start();
653 void URLFetcherProtectTest::OnURLFetchComplete(const URLFetcher* source) {
654 const TimeDelta one_second = TimeDelta::FromMilliseconds(1000);
655 if (source->GetResponseCode() >= 500) {
656 // Now running ServerUnavailable test.
657 // It takes more than 1 second to finish all 11 requests.
658 EXPECT_TRUE(Time::Now() - start_time_ >= one_second);
659 EXPECT_TRUE(source->GetStatus().is_success());
660 std::string data;
661 EXPECT_TRUE(source->GetResponseAsString(&data));
662 EXPECT_FALSE(data.empty());
663 CleanupAfterFetchComplete();
664 } else {
665 // Now running Overload test.
666 static int count = 0;
667 count++;
668 if (count < 20) {
669 fetcher_->SetRequestContext(
670 new ThrottlingTestURLRequestContextGetter(
671 io_message_loop_proxy(), request_context()));
672 fetcher_->Start();
673 } else {
674 // We have already sent 20 requests continuously. And we expect that
675 // it takes more than 1 second due to the overload protection settings.
676 EXPECT_TRUE(Time::Now() - start_time_ >= one_second);
677 URLFetcherTest::OnURLFetchComplete(source);
682 void URLFetcherProtectTestPassedThrough::CreateFetcher(const GURL& url) {
683 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this);
684 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
685 io_message_loop_proxy(), request_context()));
686 fetcher_->SetAutomaticallyRetryOn5xx(false);
687 start_time_ = Time::Now();
688 fetcher_->SetMaxRetriesOn5xx(11);
689 fetcher_->Start();
692 void URLFetcherProtectTestPassedThrough::OnURLFetchComplete(
693 const URLFetcher* source) {
694 const TimeDelta one_minute = TimeDelta::FromMilliseconds(60000);
695 if (source->GetResponseCode() >= 500) {
696 // Now running ServerUnavailable test.
697 // It should get here on the first attempt, so almost immediately and
698 // *not* to attempt to execute all 11 requests (2.5 minutes).
699 EXPECT_TRUE(Time::Now() - start_time_ < one_minute);
700 EXPECT_TRUE(source->GetStatus().is_success());
701 // Check that suggested back off time is bigger than 0.
702 EXPECT_GT(fetcher_->GetBackoffDelay().InMicroseconds(), 0);
703 std::string data;
704 EXPECT_TRUE(source->GetResponseAsString(&data));
705 EXPECT_FALSE(data.empty());
706 } else {
707 // We should not get here!
708 ADD_FAILURE();
711 CleanupAfterFetchComplete();
715 URLFetcherBadHTTPSTest::URLFetcherBadHTTPSTest() {
716 PathService::Get(base::DIR_SOURCE_ROOT, &cert_dir_);
717 cert_dir_ = cert_dir_.AppendASCII("chrome");
718 cert_dir_ = cert_dir_.AppendASCII("test");
719 cert_dir_ = cert_dir_.AppendASCII("data");
720 cert_dir_ = cert_dir_.AppendASCII("ssl");
721 cert_dir_ = cert_dir_.AppendASCII("certificates");
724 // The "server certificate expired" error should result in automatic
725 // cancellation of the request by
726 // URLRequest::Delegate::OnSSLCertificateError.
727 void URLFetcherBadHTTPSTest::OnURLFetchComplete(
728 const URLFetcher* source) {
729 // This part is different from URLFetcherTest::OnURLFetchComplete
730 // because this test expects the request to be cancelled.
731 EXPECT_EQ(URLRequestStatus::CANCELED, source->GetStatus().status());
732 EXPECT_EQ(ERR_ABORTED, source->GetStatus().error());
733 EXPECT_EQ(-1, source->GetResponseCode());
734 EXPECT_TRUE(source->GetCookies().empty());
735 std::string data;
736 EXPECT_TRUE(source->GetResponseAsString(&data));
737 EXPECT_TRUE(data.empty());
738 CleanupAfterFetchComplete();
741 void URLFetcherCancelTest::CreateFetcher(const GURL& url) {
742 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this);
743 CancelTestURLRequestContextGetter* context_getter =
744 new CancelTestURLRequestContextGetter(io_message_loop_proxy(),
745 url);
746 fetcher_->SetRequestContext(context_getter);
747 fetcher_->SetMaxRetriesOn5xx(2);
748 fetcher_->Start();
749 // We need to wait for the creation of the URLRequestContext, since we
750 // rely on it being destroyed as a signal to end the test.
751 context_getter->WaitForContextCreation();
752 CancelRequest();
755 void URLFetcherCancelTest::OnURLFetchComplete(
756 const URLFetcher* source) {
757 // We should have cancelled the request before completion.
758 ADD_FAILURE();
759 CleanupAfterFetchComplete();
762 void URLFetcherCancelTest::CancelRequest() {
763 delete fetcher_;
764 // The URLFetcher's test context will post a Quit task once it is
765 // deleted. So if this test simply hangs, it means cancellation
766 // did not work.
769 void URLFetcherMultipleAttemptTest::OnURLFetchComplete(
770 const URLFetcher* source) {
771 EXPECT_TRUE(source->GetStatus().is_success());
772 EXPECT_EQ(200, source->GetResponseCode()); // HTTP OK
773 std::string data;
774 EXPECT_TRUE(source->GetResponseAsString(&data));
775 EXPECT_FALSE(data.empty());
776 if (!data.empty() && data_.empty()) {
777 data_ = data;
778 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
779 io_message_loop_proxy(), request_context()));
780 fetcher_->Start();
781 } else {
782 EXPECT_EQ(data, data_);
783 CleanupAfterFetchComplete();
787 void URLFetcherFileTest::CreateFetcherForFile(const GURL& url,
788 const base::FilePath& file_path) {
789 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this);
790 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
791 io_message_loop_proxy(), request_context()));
793 // Use the IO message loop to do the file operations in this test.
794 fetcher_->SaveResponseToFileAtPath(file_path, io_message_loop_proxy());
795 fetcher_->Start();
798 void URLFetcherFileTest::CreateFetcherForTempFile(const GURL& url) {
799 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this);
800 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
801 io_message_loop_proxy(), request_context()));
803 // Use the IO message loop to do the file operations in this test.
804 fetcher_->SaveResponseToTemporaryFile(io_message_loop_proxy());
805 fetcher_->Start();
808 void URLFetcherFileTest::OnURLFetchComplete(const URLFetcher* source) {
809 if (expected_file_error_ == base::PLATFORM_FILE_OK) {
810 EXPECT_TRUE(source->GetStatus().is_success());
811 EXPECT_EQ(source->GetResponseCode(), 200);
813 base::PlatformFileError error_code = base::PLATFORM_FILE_OK;
814 EXPECT_FALSE(fetcher_->FileErrorOccurred(&error_code));
816 EXPECT_TRUE(source->GetResponseAsFilePath(
817 take_ownership_of_file_, &file_path_));
819 EXPECT_TRUE(file_util::ContentsEqual(expected_file_, file_path_));
820 } else {
821 base::PlatformFileError error_code = base::PLATFORM_FILE_OK;
822 EXPECT_TRUE(fetcher_->FileErrorOccurred(&error_code));
823 EXPECT_EQ(expected_file_error_, error_code);
825 CleanupAfterFetchComplete();
828 TEST_F(URLFetcherTest, SameThreadsTest) {
829 TestServer test_server(TestServer::TYPE_HTTP,
830 TestServer::kLocalhost,
831 base::FilePath(kDocRoot));
832 ASSERT_TRUE(test_server.Start());
834 // Create the fetcher on the main thread. Since IO will happen on the main
835 // thread, this will test URLFetcher's ability to do everything on one
836 // thread.
837 CreateFetcher(test_server.GetURL("defaultresponse"));
839 MessageLoop::current()->Run();
842 TEST_F(URLFetcherTest, DifferentThreadsTest) {
843 TestServer test_server(TestServer::TYPE_HTTP,
844 TestServer::kLocalhost,
845 base::FilePath(kDocRoot));
846 ASSERT_TRUE(test_server.Start());
848 // Create a separate thread that will create the URLFetcher. The current
849 // (main) thread will do the IO, and when the fetch is complete it will
850 // terminate the main thread's message loop; then the other thread's
851 // message loop will be shut down automatically as the thread goes out of
852 // scope.
853 base::Thread t("URLFetcher test thread");
854 ASSERT_TRUE(t.Start());
855 t.message_loop()->PostTask(
856 FROM_HERE,
857 base::Bind(&URLFetcherTest::CreateFetcher,
858 base::Unretained(this),
859 test_server.GetURL("defaultresponse")));
861 MessageLoop::current()->Run();
864 void CancelAllOnIO() {
865 EXPECT_EQ(1, URLFetcherTest::GetNumFetcherCores());
866 URLFetcherImpl::CancelAll();
867 EXPECT_EQ(0, URLFetcherTest::GetNumFetcherCores());
870 // Tests to make sure CancelAll() will successfully cancel existing URLFetchers.
871 TEST_F(URLFetcherTest, CancelAll) {
872 TestServer test_server(TestServer::TYPE_HTTP,
873 TestServer::kLocalhost,
874 base::FilePath(kDocRoot));
875 ASSERT_TRUE(test_server.Start());
876 EXPECT_EQ(0, GetNumFetcherCores());
878 CreateFetcher(test_server.GetURL("defaultresponse"));
879 io_message_loop_proxy()->PostTaskAndReply(
880 FROM_HERE,
881 base::Bind(&CancelAllOnIO),
882 MessageLoop::QuitClosure());
883 MessageLoop::current()->Run();
884 EXPECT_EQ(0, GetNumFetcherCores());
885 delete fetcher_;
888 TEST_F(URLFetcherMockDnsTest, DontRetryOnNetworkChangedByDefault) {
889 EXPECT_EQ(0, GetNumFetcherCores());
890 EXPECT_FALSE(resolver_.has_pending_requests());
892 // This posts a task to start the fetcher.
893 CreateFetcher(test_url_);
894 fetcher_->Start();
895 EXPECT_EQ(0, GetNumFetcherCores());
896 MessageLoop::current()->RunUntilIdle();
898 // The fetcher is now running, but is pending the host resolve.
899 EXPECT_EQ(1, GetNumFetcherCores());
900 EXPECT_TRUE(resolver_.has_pending_requests());
901 ASSERT_FALSE(completed_fetcher_);
903 // A network change notification aborts the connect job.
904 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
905 MessageLoop::current()->RunUntilIdle();
906 EXPECT_EQ(0, GetNumFetcherCores());
907 EXPECT_FALSE(resolver_.has_pending_requests());
908 ASSERT_TRUE(completed_fetcher_);
910 // And the owner of the fetcher gets the ERR_NETWORK_CHANGED error.
911 EXPECT_EQ(ERR_NETWORK_CHANGED, completed_fetcher_->GetStatus().error());
914 TEST_F(URLFetcherMockDnsTest, RetryOnNetworkChangedAndFail) {
915 EXPECT_EQ(0, GetNumFetcherCores());
916 EXPECT_FALSE(resolver_.has_pending_requests());
918 // This posts a task to start the fetcher.
919 CreateFetcher(test_url_);
920 fetcher_->SetAutomaticallyRetryOnNetworkChanges(3);
921 fetcher_->Start();
922 EXPECT_EQ(0, GetNumFetcherCores());
923 MessageLoop::current()->RunUntilIdle();
925 // The fetcher is now running, but is pending the host resolve.
926 EXPECT_EQ(1, GetNumFetcherCores());
927 EXPECT_TRUE(resolver_.has_pending_requests());
928 ASSERT_FALSE(completed_fetcher_);
930 // Make it fail 3 times.
931 for (int i = 0; i < 3; ++i) {
932 // A network change notification aborts the connect job.
933 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
934 MessageLoop::current()->RunUntilIdle();
936 // But the fetcher retries automatically.
937 EXPECT_EQ(1, GetNumFetcherCores());
938 EXPECT_TRUE(resolver_.has_pending_requests());
939 ASSERT_FALSE(completed_fetcher_);
942 // A 4th failure doesn't trigger another retry, and propagates the error
943 // to the owner of the fetcher.
944 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
945 MessageLoop::current()->RunUntilIdle();
946 EXPECT_EQ(0, GetNumFetcherCores());
947 EXPECT_FALSE(resolver_.has_pending_requests());
948 ASSERT_TRUE(completed_fetcher_);
950 // And the owner of the fetcher gets the ERR_NETWORK_CHANGED error.
951 EXPECT_EQ(ERR_NETWORK_CHANGED, completed_fetcher_->GetStatus().error());
954 TEST_F(URLFetcherMockDnsTest, RetryOnNetworkChangedAndSucceed) {
955 EXPECT_EQ(0, GetNumFetcherCores());
956 EXPECT_FALSE(resolver_.has_pending_requests());
958 // This posts a task to start the fetcher.
959 CreateFetcher(test_url_);
960 fetcher_->SetAutomaticallyRetryOnNetworkChanges(3);
961 fetcher_->Start();
962 EXPECT_EQ(0, GetNumFetcherCores());
963 MessageLoop::current()->RunUntilIdle();
965 // The fetcher is now running, but is pending the host resolve.
966 EXPECT_EQ(1, GetNumFetcherCores());
967 EXPECT_TRUE(resolver_.has_pending_requests());
968 ASSERT_FALSE(completed_fetcher_);
970 // Make it fail 3 times.
971 for (int i = 0; i < 3; ++i) {
972 // A network change notification aborts the connect job.
973 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
974 MessageLoop::current()->RunUntilIdle();
976 // But the fetcher retries automatically.
977 EXPECT_EQ(1, GetNumFetcherCores());
978 EXPECT_TRUE(resolver_.has_pending_requests());
979 ASSERT_FALSE(completed_fetcher_);
982 // Now let it succeed by resolving the pending request.
983 resolver_.ResolveAllPending();
984 MessageLoop::current()->Run();
986 // URLFetcherMockDnsTest::OnURLFetchComplete() will quit the loop.
987 EXPECT_EQ(0, GetNumFetcherCores());
988 EXPECT_FALSE(resolver_.has_pending_requests());
989 ASSERT_TRUE(completed_fetcher_);
991 // This time the request succeeded.
992 EXPECT_EQ(OK, completed_fetcher_->GetStatus().error());
993 EXPECT_EQ(200, completed_fetcher_->GetResponseCode());
996 TEST_F(URLFetcherPostTest, Basic) {
997 TestServer test_server(TestServer::TYPE_HTTP,
998 TestServer::kLocalhost,
999 base::FilePath(kDocRoot));
1000 ASSERT_TRUE(test_server.Start());
1002 CreateFetcher(test_server.GetURL("echo"));
1003 MessageLoop::current()->Run();
1006 TEST_F(URLFetcherEmptyPostTest, Basic) {
1007 TestServer test_server(TestServer::TYPE_HTTP,
1008 TestServer::kLocalhost,
1009 base::FilePath(kDocRoot));
1010 ASSERT_TRUE(test_server.Start());
1012 CreateFetcher(test_server.GetURL("echo"));
1013 MessageLoop::current()->Run();
1016 TEST_F(URLFetcherUploadProgressTest, Basic) {
1017 TestServer test_server(TestServer::TYPE_HTTP,
1018 TestServer::kLocalhost,
1019 base::FilePath(kDocRoot));
1020 ASSERT_TRUE(test_server.Start());
1022 CreateFetcher(test_server.GetURL("echo"));
1023 MessageLoop::current()->Run();
1026 TEST_F(URLFetcherDownloadProgressTest, Basic) {
1027 TestServer test_server(TestServer::TYPE_HTTP,
1028 TestServer::kLocalhost,
1029 base::FilePath(kDocRoot));
1030 ASSERT_TRUE(test_server.Start());
1032 // Get a file large enough to require more than one read into
1033 // URLFetcher::Core's IOBuffer.
1034 static const char kFileToFetch[] = "animate1.gif";
1035 // Hardcoded file size - it cannot be easily fetched when a remote http server
1036 // is used for testing.
1037 static const int64 kFileSize = 19021;
1039 expected_total_ = kFileSize;
1041 CreateFetcher(test_server.GetURL(
1042 std::string(kTestServerFilePrefix) + kFileToFetch));
1044 MessageLoop::current()->Run();
1047 TEST_F(URLFetcherDownloadProgressCancelTest, CancelWhileProgressReport) {
1048 TestServer test_server(TestServer::TYPE_HTTP,
1049 TestServer::kLocalhost,
1050 base::FilePath(kDocRoot));
1051 ASSERT_TRUE(test_server.Start());
1053 // Get a file large enough to require more than one read into
1054 // URLFetcher::Core's IOBuffer.
1055 static const char kFileToFetch[] = "animate1.gif";
1056 CreateFetcher(test_server.GetURL(
1057 std::string(kTestServerFilePrefix) + kFileToFetch));
1059 MessageLoop::current()->Run();
1062 TEST_F(URLFetcherHeadersTest, Headers) {
1063 TestServer test_server(
1064 TestServer::TYPE_HTTP,
1065 TestServer::kLocalhost,
1066 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
1067 ASSERT_TRUE(test_server.Start());
1069 CreateFetcher(test_server.GetURL("files/with-headers.html"));
1070 MessageLoop::current()->Run();
1071 // The actual tests are in the URLFetcherHeadersTest fixture.
1074 TEST_F(URLFetcherSocketAddressTest, SocketAddress) {
1075 TestServer test_server(
1076 TestServer::TYPE_HTTP,
1077 TestServer::kLocalhost,
1078 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
1079 ASSERT_TRUE(test_server.Start());
1080 expected_port_ = test_server.host_port_pair().port();
1082 // Reusing "with-headers.html" but doesn't really matter.
1083 CreateFetcher(test_server.GetURL("files/with-headers.html"));
1084 MessageLoop::current()->Run();
1085 // The actual tests are in the URLFetcherSocketAddressTest fixture.
1088 TEST_F(URLFetcherStopOnRedirectTest, StopOnRedirect) {
1089 TestServer test_server(TestServer::TYPE_HTTP,
1090 TestServer::kLocalhost,
1091 base::FilePath(kDocRoot));
1092 ASSERT_TRUE(test_server.Start());
1094 CreateFetcher(
1095 test_server.GetURL(std::string("server-redirect?") + kRedirectTarget));
1096 MessageLoop::current()->Run();
1097 EXPECT_TRUE(callback_called_);
1100 TEST_F(URLFetcherProtectTest, Overload) {
1101 TestServer test_server(TestServer::TYPE_HTTP,
1102 TestServer::kLocalhost,
1103 base::FilePath(kDocRoot));
1104 ASSERT_TRUE(test_server.Start());
1106 GURL url(test_server.GetURL("defaultresponse"));
1108 // Registers an entry for test url. It only allows 3 requests to be sent
1109 // in 200 milliseconds.
1110 scoped_refptr<URLRequestThrottlerEntry> entry(
1111 new URLRequestThrottlerEntry(
1112 request_context()->throttler_manager(),
1113 "", 200, 3, 1, 2.0, 0.0, 256));
1114 request_context()->throttler_manager()->OverrideEntryForTests(url, entry);
1116 CreateFetcher(url);
1118 MessageLoop::current()->Run();
1121 TEST_F(URLFetcherProtectTest, ServerUnavailable) {
1122 TestServer test_server(TestServer::TYPE_HTTP,
1123 TestServer::kLocalhost,
1124 base::FilePath(kDocRoot));
1125 ASSERT_TRUE(test_server.Start());
1127 GURL url(test_server.GetURL("files/server-unavailable.html"));
1129 // Registers an entry for test url. The backoff time is calculated by:
1130 // new_backoff = 2.0 * old_backoff + 0
1131 // and maximum backoff time is 256 milliseconds.
1132 // Maximum retries allowed is set to 11.
1133 scoped_refptr<URLRequestThrottlerEntry> entry(
1134 new URLRequestThrottlerEntry(
1135 request_context()->throttler_manager(),
1136 "", 200, 3, 1, 2.0, 0.0, 256));
1137 request_context()->throttler_manager()->OverrideEntryForTests(url, entry);
1139 CreateFetcher(url);
1141 MessageLoop::current()->Run();
1144 TEST_F(URLFetcherProtectTestPassedThrough, ServerUnavailablePropagateResponse) {
1145 TestServer test_server(TestServer::TYPE_HTTP,
1146 TestServer::kLocalhost,
1147 base::FilePath(kDocRoot));
1148 ASSERT_TRUE(test_server.Start());
1150 GURL url(test_server.GetURL("files/server-unavailable.html"));
1152 // Registers an entry for test url. The backoff time is calculated by:
1153 // new_backoff = 2.0 * old_backoff + 0
1154 // and maximum backoff time is 150000 milliseconds.
1155 // Maximum retries allowed is set to 11.
1156 scoped_refptr<URLRequestThrottlerEntry> entry(
1157 new URLRequestThrottlerEntry(
1158 request_context()->throttler_manager(),
1159 "", 200, 3, 100, 2.0, 0.0, 150000));
1160 // Total time if *not* for not doing automatic backoff would be 150s.
1161 // In reality it should be "as soon as server responds".
1162 request_context()->throttler_manager()->OverrideEntryForTests(url, entry);
1164 CreateFetcher(url);
1166 MessageLoop::current()->Run();
1169 TEST_F(URLFetcherBadHTTPSTest, BadHTTPSTest) {
1170 TestServer::SSLOptions ssl_options(
1171 TestServer::SSLOptions::CERT_EXPIRED);
1172 TestServer test_server(TestServer::TYPE_HTTPS,
1173 ssl_options,
1174 base::FilePath(kDocRoot));
1175 ASSERT_TRUE(test_server.Start());
1177 CreateFetcher(test_server.GetURL("defaultresponse"));
1178 MessageLoop::current()->Run();
1181 TEST_F(URLFetcherCancelTest, ReleasesContext) {
1182 TestServer test_server(TestServer::TYPE_HTTP,
1183 TestServer::kLocalhost,
1184 base::FilePath(kDocRoot));
1185 ASSERT_TRUE(test_server.Start());
1187 GURL url(test_server.GetURL("files/server-unavailable.html"));
1189 // Create a separate thread that will create the URLFetcher. The current
1190 // (main) thread will do the IO, and when the fetch is complete it will
1191 // terminate the main thread's message loop; then the other thread's
1192 // message loop will be shut down automatically as the thread goes out of
1193 // scope.
1194 base::Thread t("URLFetcher test thread");
1195 ASSERT_TRUE(t.Start());
1196 t.message_loop()->PostTask(
1197 FROM_HERE,
1198 base::Bind(&URLFetcherCancelTest::CreateFetcher,
1199 base::Unretained(this), url));
1201 MessageLoop::current()->Run();
1204 TEST_F(URLFetcherCancelTest, CancelWhileDelayedStartTaskPending) {
1205 TestServer test_server(TestServer::TYPE_HTTP,
1206 TestServer::kLocalhost,
1207 base::FilePath(kDocRoot));
1208 ASSERT_TRUE(test_server.Start());
1210 GURL url(test_server.GetURL("files/server-unavailable.html"));
1212 // Register an entry for test url.
1213 // Using a sliding window of 4 seconds, and max of 1 request, under a fast
1214 // run we expect to have a 4 second delay when posting the Start task.
1215 scoped_refptr<URLRequestThrottlerEntry> entry(
1216 new URLRequestThrottlerEntry(
1217 request_context()->throttler_manager(),
1218 "", 4000, 1, 2000, 2.0, 0.0, 4000));
1219 request_context()->throttler_manager()->OverrideEntryForTests(url, entry);
1220 // Fake that a request has just started.
1221 entry->ReserveSendingTimeForNextRequest(base::TimeTicks());
1223 // The next request we try to send will be delayed by ~4 seconds.
1224 // The slower the test runs, the less the delay will be (since it takes the
1225 // time difference from now).
1227 base::Thread t("URLFetcher test thread");
1228 ASSERT_TRUE(t.Start());
1229 t.message_loop()->PostTask(
1230 FROM_HERE,
1231 base::Bind(&URLFetcherTest::CreateFetcher, base::Unretained(this), url));
1233 MessageLoop::current()->Run();
1236 TEST_F(URLFetcherMultipleAttemptTest, SameData) {
1237 TestServer test_server(TestServer::TYPE_HTTP,
1238 TestServer::kLocalhost,
1239 base::FilePath(kDocRoot));
1240 ASSERT_TRUE(test_server.Start());
1242 // Create the fetcher on the main thread. Since IO will happen on the main
1243 // thread, this will test URLFetcher's ability to do everything on one
1244 // thread.
1245 CreateFetcher(test_server.GetURL("defaultresponse"));
1247 MessageLoop::current()->Run();
1250 TEST_F(URLFetcherFileTest, SmallGet) {
1251 TestServer test_server(TestServer::TYPE_HTTP,
1252 TestServer::kLocalhost,
1253 base::FilePath(kDocRoot));
1254 ASSERT_TRUE(test_server.Start());
1256 base::ScopedTempDir temp_dir;
1257 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
1259 // Get a small file.
1260 static const char kFileToFetch[] = "simple.html";
1261 expected_file_ = test_server.GetDocumentRoot().AppendASCII(kFileToFetch);
1262 CreateFetcherForFile(
1263 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch),
1264 temp_dir.path().AppendASCII(kFileToFetch));
1266 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit().
1268 ASSERT_FALSE(file_util::PathExists(file_path_))
1269 << file_path_.value() << " not removed.";
1272 TEST_F(URLFetcherFileTest, LargeGet) {
1273 TestServer test_server(TestServer::TYPE_HTTP,
1274 TestServer::kLocalhost,
1275 base::FilePath(kDocRoot));
1276 ASSERT_TRUE(test_server.Start());
1278 base::ScopedTempDir temp_dir;
1279 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
1281 // Get a file large enough to require more than one read into
1282 // URLFetcher::Core's IOBuffer.
1283 static const char kFileToFetch[] = "animate1.gif";
1284 expected_file_ = test_server.GetDocumentRoot().AppendASCII(kFileToFetch);
1285 CreateFetcherForFile(
1286 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch),
1287 temp_dir.path().AppendASCII(kFileToFetch));
1289 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit().
1292 TEST_F(URLFetcherFileTest, CanTakeOwnershipOfFile) {
1293 TestServer test_server(TestServer::TYPE_HTTP,
1294 TestServer::kLocalhost,
1295 base::FilePath(kDocRoot));
1296 ASSERT_TRUE(test_server.Start());
1298 base::ScopedTempDir temp_dir;
1299 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
1301 // Get a small file.
1302 static const char kFileToFetch[] = "simple.html";
1303 expected_file_ = test_server.GetDocumentRoot().AppendASCII(kFileToFetch);
1304 CreateFetcherForFile(
1305 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch),
1306 temp_dir.path().AppendASCII(kFileToFetch));
1308 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit().
1310 MessageLoop::current()->RunUntilIdle();
1311 ASSERT_FALSE(file_util::PathExists(file_path_))
1312 << file_path_.value() << " not removed.";
1316 TEST_F(URLFetcherFileTest, OverwriteExistingFile) {
1317 TestServer test_server(TestServer::TYPE_HTTP,
1318 TestServer::kLocalhost,
1319 base::FilePath(kDocRoot));
1320 ASSERT_TRUE(test_server.Start());
1322 base::ScopedTempDir temp_dir;
1323 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
1325 // Create a file before trying to fetch.
1326 static const char kFileToFetch[] = "simple.html";
1327 static const char kData[] = "abcdefghijklmnopqrstuvwxyz";
1328 file_path_ = temp_dir.path().AppendASCII(kFileToFetch);
1329 const int data_size = arraysize(kData);
1330 ASSERT_EQ(file_util::WriteFile(file_path_, kData, data_size), data_size);
1331 ASSERT_TRUE(file_util::PathExists(file_path_));
1332 expected_file_ = test_server.GetDocumentRoot().AppendASCII(kFileToFetch);
1333 ASSERT_FALSE(file_util::ContentsEqual(file_path_, expected_file_));
1335 // Get a small file.
1336 CreateFetcherForFile(
1337 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch),
1338 file_path_);
1340 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit().
1343 TEST_F(URLFetcherFileTest, TryToOverwriteDirectory) {
1344 TestServer test_server(TestServer::TYPE_HTTP,
1345 TestServer::kLocalhost,
1346 base::FilePath(kDocRoot));
1347 ASSERT_TRUE(test_server.Start());
1349 base::ScopedTempDir temp_dir;
1350 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
1352 // Create a directory before trying to fetch.
1353 static const char kFileToFetch[] = "simple.html";
1354 file_path_ = temp_dir.path().AppendASCII(kFileToFetch);
1355 ASSERT_TRUE(file_util::CreateDirectory(file_path_));
1356 ASSERT_TRUE(file_util::PathExists(file_path_));
1358 // Get a small file.
1359 expected_file_error_ = base::PLATFORM_FILE_ERROR_ACCESS_DENIED;
1360 expected_file_ = test_server.GetDocumentRoot().AppendASCII(kFileToFetch);
1361 CreateFetcherForFile(
1362 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch),
1363 file_path_);
1365 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit().
1367 MessageLoop::current()->RunUntilIdle();
1370 TEST_F(URLFetcherFileTest, SmallGetToTempFile) {
1371 TestServer test_server(TestServer::TYPE_HTTP,
1372 TestServer::kLocalhost,
1373 base::FilePath(kDocRoot));
1374 ASSERT_TRUE(test_server.Start());
1376 // Get a small file.
1377 static const char kFileToFetch[] = "simple.html";
1378 expected_file_ = test_server.GetDocumentRoot().AppendASCII(kFileToFetch);
1379 CreateFetcherForTempFile(
1380 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch));
1382 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit().
1384 ASSERT_FALSE(file_util::PathExists(file_path_))
1385 << file_path_.value() << " not removed.";
1388 TEST_F(URLFetcherFileTest, LargeGetToTempFile) {
1389 TestServer test_server(TestServer::TYPE_HTTP,
1390 TestServer::kLocalhost,
1391 base::FilePath(kDocRoot));
1392 ASSERT_TRUE(test_server.Start());
1394 // Get a file large enough to require more than one read into
1395 // URLFetcher::Core's IOBuffer.
1396 static const char kFileToFetch[] = "animate1.gif";
1397 expected_file_ = test_server.GetDocumentRoot().AppendASCII(kFileToFetch);
1398 CreateFetcherForTempFile(test_server.GetURL(
1399 std::string(kTestServerFilePrefix) + kFileToFetch));
1401 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit().
1404 TEST_F(URLFetcherFileTest, CanTakeOwnershipOfTempFile) {
1405 TestServer test_server(TestServer::TYPE_HTTP,
1406 TestServer::kLocalhost,
1407 base::FilePath(kDocRoot));
1408 ASSERT_TRUE(test_server.Start());
1410 // Get a small file.
1411 static const char kFileToFetch[] = "simple.html";
1412 expected_file_ = test_server.GetDocumentRoot().AppendASCII(kFileToFetch);
1413 CreateFetcherForTempFile(test_server.GetURL(
1414 std::string(kTestServerFilePrefix) + kFileToFetch));
1416 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit().
1418 MessageLoop::current()->RunUntilIdle();
1419 ASSERT_FALSE(file_util::PathExists(file_path_))
1420 << file_path_.value() << " not removed.";
1423 } // namespace
1425 } // namespace net