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.
5 #include "third_party/libaddressinput/chromium/chrome_downloader_impl.h"
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"
14 static const char kFakeUrl
[] = "https://example.com";
15 static const char kFakeInsecureUrl
[] = "http://example.com";
17 class ChromeDownloaderImplTest
: public testing::Test
{
19 ChromeDownloaderImplTest()
20 : fake_factory_(&factory_
),
22 virtual ~ChromeDownloaderImplTest() {}
25 // Sets the response for the download.
26 void SetFakeResponse(const std::string
& payload
, net::HttpStatusCode code
) {
27 fake_factory_
.SetFakeResponse(url_
,
30 net::URLRequestStatus::SUCCESS
);
33 // Kicks off the 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();
42 void set_url(const GURL
& url
) { url_
= url
; }
43 const std::string
& data() { return *data_
; }
44 bool success() { return success_
; }
47 scoped_ptr
<ChromeDownloaderImpl::Callback
> BuildCallback() {
48 return ::i18n::addressinput::BuildScopedPtrCallback(
49 this, &ChromeDownloaderImplTest::OnDownloaded
);
52 // Callback for when download is finished.
53 void OnDownloaded(bool success
,
54 const std::string
& url
,
55 scoped_ptr
<std::string
> data
) {
60 base::MessageLoop loop_
;
61 net::URLFetcherImplFactory factory_
;
62 net::FakeURLFetcherFactory fake_factory_
;
64 scoped_ptr
<std::string
> data_
;
68 TEST_F(ChromeDownloaderImplTest
, Success
) {
69 const char kFakePayload
[] = "ham hock";
70 set_url(GURL(kFakeUrl
));
71 SetFakeResponse(kFakePayload
, net::HTTP_OK
);
73 EXPECT_TRUE(success());
74 EXPECT_EQ(kFakePayload
, data());
77 TEST_F(ChromeDownloaderImplTest
, Failure
) {
78 const char kFakePayload
[] = "ham hock";
79 set_url(GURL(kFakeUrl
));
80 SetFakeResponse(kFakePayload
, net::HTTP_INTERNAL_SERVER_ERROR
);
82 EXPECT_FALSE(success());
83 EXPECT_EQ(std::string(), data());
86 TEST_F(ChromeDownloaderImplTest
, RejectsInsecureScheme
) {
87 const char kFakePayload
[] = "ham hock";
88 set_url(GURL(kFakeInsecureUrl
));
89 SetFakeResponse(kFakePayload
, net::HTTP_OK
);
91 EXPECT_FALSE(success());
92 EXPECT_EQ(std::string(), data());
95 } // namespace autofill