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_metadata_source.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 ChromeMetadataSourceTest
: public testing::Test
{
19 ChromeMetadataSourceTest()
20 : fake_factory_(&factory_
),
22 virtual ~ChromeMetadataSourceTest() {}
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 scoped_refptr
<net::TestURLRequestContextGetter
> getter(
36 new net::TestURLRequestContextGetter(
37 base::MessageLoopProxy::current()));
38 ChromeMetadataSource
impl(std::string(), getter
.get());
39 scoped_ptr
< ::i18n::addressinput::Source::Callback
> callback(
40 ::i18n::addressinput::BuildCallback(
41 this, &ChromeMetadataSourceTest::OnDownloaded
));
42 impl
.Get(url_
.spec(), *callback
);
43 base::MessageLoop::current()->RunUntilIdle();
46 void set_url(const GURL
& url
) { url_
= url
; }
47 bool success() const { return success_
; }
48 bool has_data() const { return !!data_
; }
50 const std::string
& data() const {
56 // Callback for when download is finished.
57 void OnDownloaded(bool success
,
58 const std::string
& url
,
60 ASSERT_FALSE(success
&& data
== NULL
);
65 base::MessageLoop loop_
;
66 net::URLFetcherImplFactory factory_
;
67 net::FakeURLFetcherFactory fake_factory_
;
69 scoped_ptr
<std::string
> data_
;
73 TEST_F(ChromeMetadataSourceTest
, Success
) {
74 const char kFakePayload
[] = "ham hock";
75 set_url(GURL(kFakeUrl
));
76 SetFakeResponse(kFakePayload
, net::HTTP_OK
);
78 EXPECT_TRUE(success());
79 EXPECT_EQ(kFakePayload
, data());
82 TEST_F(ChromeMetadataSourceTest
, Failure
) {
83 const char kFakePayload
[] = "ham hock";
84 set_url(GURL(kFakeUrl
));
85 SetFakeResponse(kFakePayload
, net::HTTP_INTERNAL_SERVER_ERROR
);
87 EXPECT_FALSE(success());
88 EXPECT_TRUE(!has_data() || data().empty());
91 TEST_F(ChromeMetadataSourceTest
, RejectsInsecureScheme
) {
92 const char kFakePayload
[] = "ham hock";
93 set_url(GURL(kFakeInsecureUrl
));
94 SetFakeResponse(kFakePayload
, net::HTTP_OK
);
96 EXPECT_FALSE(success());
97 EXPECT_TRUE(!has_data() || data().empty());
100 } // namespace autofill