Home | History | Annotate | Download | only in chromium
      1 // Copyright 2014 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.
      4 
      5 #include "third_party/libaddressinput/chromium/chrome_downloader_impl.h"
      6 
      7 #include "base/message_loop/message_loop_proxy.h"
      8 #include "net/url_request/test_url_fetcher_factory.h"
      9 #include "net/url_request/url_request_test_util.h"
     10 #include "testing/gtest/include/gtest/gtest.h"
     11 
     12 namespace autofill {
     13 
     14 static const char kFakeUrl[] = "https://example.com";
     15 static const char kFakeInsecureUrl[] = "http://example.com";
     16 
     17 class ChromeDownloaderImplTest : public testing::Test {
     18  public:
     19   ChromeDownloaderImplTest()
     20       : fake_factory_(&factory_),
     21         success_(false) {}
     22   virtual ~ChromeDownloaderImplTest() {}
     23 
     24  protected:
     25   // Sets the response for the download.
     26   void SetFakeResponse(const std::string& payload, net::HttpStatusCode code) {
     27     fake_factory_.SetFakeResponse(url_,
     28                                   payload,
     29                                   code,
     30                                   net::URLRequestStatus::SUCCESS);
     31   }
     32 
     33   // Kicks off the download.
     34   void Download() {
     35     net::TestURLRequestContextGetter* getter =
     36         new net::TestURLRequestContextGetter(base::MessageLoopProxy::current());
     37     ChromeDownloaderImpl impl(getter);
     38     impl.Download(url_.spec(), BuildCallback());
     39     base::MessageLoop::current()->RunUntilIdle();
     40   }
     41 
     42   void set_url(const GURL& url) { url_ = url; }
     43   const std::string& data() { return *data_; }
     44   bool success() { return success_; }
     45 
     46  private:
     47   scoped_ptr<ChromeDownloaderImpl::Callback> BuildCallback() {
     48     return ::i18n::addressinput::BuildScopedPtrCallback(
     49         this, &ChromeDownloaderImplTest::OnDownloaded);
     50   }
     51 
     52   // Callback for when download is finished.
     53   void OnDownloaded(bool success,
     54                     const std::string& url,
     55                     scoped_ptr<std::string> data) {
     56     success_ = success;
     57     data_ = data.Pass();
     58   }
     59 
     60   base::MessageLoop loop_;
     61   net::URLFetcherImplFactory factory_;
     62   net::FakeURLFetcherFactory fake_factory_;
     63   GURL url_;
     64   scoped_ptr<std::string> data_;
     65   bool success_;
     66 };
     67 
     68 TEST_F(ChromeDownloaderImplTest, Success) {
     69   const char kFakePayload[] = "ham hock";
     70   set_url(GURL(kFakeUrl));
     71   SetFakeResponse(kFakePayload, net::HTTP_OK);
     72   Download();
     73   EXPECT_TRUE(success());
     74   EXPECT_EQ(kFakePayload, data());
     75 }
     76 
     77 TEST_F(ChromeDownloaderImplTest, Failure) {
     78   const char kFakePayload[] = "ham hock";
     79   set_url(GURL(kFakeUrl));
     80   SetFakeResponse(kFakePayload, net::HTTP_INTERNAL_SERVER_ERROR);
     81   Download();
     82   EXPECT_FALSE(success());
     83   EXPECT_EQ(std::string(), data());
     84 }
     85 
     86 TEST_F(ChromeDownloaderImplTest, RejectsInsecureScheme) {
     87   const char kFakePayload[] = "ham hock";
     88   set_url(GURL(kFakeInsecureUrl));
     89   SetFakeResponse(kFakePayload, net::HTTP_OK);
     90   Download();
     91   EXPECT_FALSE(success());
     92   EXPECT_EQ(std::string(), data());
     93 }
     94 
     95 }  // namespace autofill
     96