1 // Copyright 2013 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 "chrome/browser/local_discovery/privet_url_fetcher.h"
6 #include "net/url_request/test_url_fetcher_factory.h"
7 #include "net/url_request/url_request_test_util.h"
8 #include "testing/gmock/include/gmock/gmock.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 using testing::StrictMock
;
13 namespace local_discovery
{
17 const char kSamplePrivetURL
[] =
18 "http://10.0.0.8:7676/privet/register?action=start";
19 const char kSamplePrivetToken
[] = "MyToken";
20 const char kEmptyPrivetToken
[] = "\"\"";
22 const char kSampleParsableJSON
[] = "{ \"hello\" : 2 }";
23 const char kSampleUnparsableJSON
[] = "{ \"hello\" : }";
24 const char kSampleJSONWithError
[] = "{ \"error\" : \"unittest_example\" }";
26 class MockPrivetURLFetcherDelegate
: public PrivetURLFetcher::Delegate
{
28 virtual void OnError(PrivetURLFetcher
* fetcher
,
29 PrivetURLFetcher::ErrorType error
) OVERRIDE
{
30 OnErrorInternal(error
);
33 MOCK_METHOD1(OnErrorInternal
, void(PrivetURLFetcher::ErrorType error
));
35 virtual void OnParsedJson(PrivetURLFetcher
* fetcher
,
36 const base::DictionaryValue
* value
,
37 bool has_error
) OVERRIDE
{
38 saved_value_
.reset(value
->DeepCopy());
39 OnParsedJsonInternal(has_error
);
42 MOCK_METHOD1(OnParsedJsonInternal
, void(bool has_error
));
44 virtual void OnNeedPrivetToken(
45 PrivetURLFetcher
* fetcher
,
46 const PrivetURLFetcher::TokenCallback
& callback
) {
49 const base::DictionaryValue
* saved_value() { return saved_value_
.get(); }
52 scoped_ptr
<base::DictionaryValue
> saved_value_
;
55 class PrivetURLFetcherTest
: public ::testing::Test
{
57 PrivetURLFetcherTest() {
58 request_context_
= new net::TestURLRequestContextGetter(
59 base::MessageLoopProxy::current());
60 privet_urlfetcher_
.reset(new PrivetURLFetcher(
62 GURL(kSamplePrivetURL
),
63 net::URLFetcher::POST
,
64 request_context_
.get(),
67 virtual ~PrivetURLFetcherTest() {
70 void RunFor(base::TimeDelta time_period
) {
71 base::CancelableCallback
<void()> callback(base::Bind(
72 &PrivetURLFetcherTest::Stop
, base::Unretained(this)));
73 base::MessageLoop::current()->PostDelayedTask(
74 FROM_HERE
, callback
.callback(), time_period
);
76 base::MessageLoop::current()->Run();
81 base::MessageLoop::current()->Quit();
85 base::MessageLoop loop_
;
86 scoped_refptr
<net::TestURLRequestContextGetter
> request_context_
;
87 net::TestURLFetcherFactory fetcher_factory_
;
88 scoped_ptr
<PrivetURLFetcher
> privet_urlfetcher_
;
89 StrictMock
<MockPrivetURLFetcherDelegate
> delegate_
;
92 TEST_F(PrivetURLFetcherTest
, FetchSuccess
) {
93 privet_urlfetcher_
->Start();
94 net::TestURLFetcher
* fetcher
= fetcher_factory_
.GetFetcherByID(0);
95 ASSERT_TRUE(fetcher
!= NULL
);
96 fetcher
->SetResponseString(kSampleParsableJSON
);
97 fetcher
->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS
,
99 fetcher
->set_response_code(200);
101 EXPECT_CALL(delegate_
, OnParsedJsonInternal(false));
102 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
104 const base::DictionaryValue
* value
= delegate_
.saved_value();
106 ASSERT_TRUE(value
!= NULL
);
107 ASSERT_TRUE(value
->GetInteger("hello", &hello_value
));
108 EXPECT_EQ(2, hello_value
);
111 TEST_F(PrivetURLFetcherTest
, HTTP503Retry
) {
112 privet_urlfetcher_
->Start();
113 net::TestURLFetcher
* fetcher
= fetcher_factory_
.GetFetcherByID(0);
114 ASSERT_TRUE(fetcher
!= NULL
);
115 fetcher
->SetResponseString(kSampleParsableJSON
);
116 fetcher
->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS
,
118 fetcher
->set_response_code(503);
120 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
122 RunFor(base::TimeDelta::FromSeconds(7));
123 fetcher
= fetcher_factory_
.GetFetcherByID(0);
125 ASSERT_TRUE(fetcher
!= NULL
);
126 fetcher
->SetResponseString(kSampleParsableJSON
);
127 fetcher
->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS
,
129 fetcher
->set_response_code(200);
131 EXPECT_CALL(delegate_
, OnParsedJsonInternal(false));
132 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
135 TEST_F(PrivetURLFetcherTest
, ResponseCodeError
) {
136 privet_urlfetcher_
->Start();
137 net::TestURLFetcher
* fetcher
= fetcher_factory_
.GetFetcherByID(0);
138 ASSERT_TRUE(fetcher
!= NULL
);
139 fetcher
->SetResponseString(kSampleParsableJSON
);
140 fetcher
->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS
,
142 fetcher
->set_response_code(404);
144 EXPECT_CALL(delegate_
,
145 OnErrorInternal(PrivetURLFetcher::RESPONSE_CODE_ERROR
));
146 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
149 TEST_F(PrivetURLFetcherTest
, JsonParseError
) {
150 privet_urlfetcher_
->Start();
151 net::TestURLFetcher
* fetcher
= fetcher_factory_
.GetFetcherByID(0);
152 ASSERT_TRUE(fetcher
!= NULL
);
153 fetcher
->SetResponseString(kSampleUnparsableJSON
);
154 fetcher
->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS
,
156 fetcher
->set_response_code(200);
158 EXPECT_CALL(delegate_
,
159 OnErrorInternal(PrivetURLFetcher::JSON_PARSE_ERROR
));
160 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
163 TEST_F(PrivetURLFetcherTest
, Header
) {
164 privet_urlfetcher_
->Start();
165 net::TestURLFetcher
* fetcher
= fetcher_factory_
.GetFetcherByID(0);
166 ASSERT_TRUE(fetcher
!= NULL
);
167 net::HttpRequestHeaders headers
;
168 fetcher
->GetExtraRequestHeaders(&headers
);
170 std::string header_token
;
171 ASSERT_TRUE(headers
.GetHeader("X-Privet-Token", &header_token
));
172 EXPECT_EQ(kSamplePrivetToken
, header_token
);
175 TEST_F(PrivetURLFetcherTest
, Header2
) {
176 privet_urlfetcher_
.reset(new PrivetURLFetcher(
178 GURL(kSamplePrivetURL
),
179 net::URLFetcher::POST
,
180 request_context_
.get(),
183 privet_urlfetcher_
->AllowEmptyPrivetToken();
184 privet_urlfetcher_
->Start();
186 net::TestURLFetcher
* fetcher
= fetcher_factory_
.GetFetcherByID(0);
187 ASSERT_TRUE(fetcher
!= NULL
);
188 net::HttpRequestHeaders headers
;
189 fetcher
->GetExtraRequestHeaders(&headers
);
191 std::string header_token
;
192 ASSERT_TRUE(headers
.GetHeader("X-Privet-Token", &header_token
));
193 EXPECT_EQ(kEmptyPrivetToken
, header_token
);
196 TEST_F(PrivetURLFetcherTest
, FetchHasError
) {
197 privet_urlfetcher_
->Start();
198 net::TestURLFetcher
* fetcher
= fetcher_factory_
.GetFetcherByID(0);
199 ASSERT_TRUE(fetcher
!= NULL
);
200 fetcher
->SetResponseString(kSampleJSONWithError
);
201 fetcher
->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS
,
203 fetcher
->set_response_code(200);
205 EXPECT_CALL(delegate_
, OnParsedJsonInternal(true));
206 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
211 } // namespace local_discovery