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 MockPrivetURLFetcherDelegate() : raw_mode_(false) {
31 virtual ~MockPrivetURLFetcherDelegate() {
34 virtual void OnError(PrivetURLFetcher
* fetcher
,
35 PrivetURLFetcher::ErrorType error
) override
{
36 OnErrorInternal(error
);
39 MOCK_METHOD1(OnErrorInternal
, void(PrivetURLFetcher::ErrorType error
));
41 virtual void OnParsedJson(PrivetURLFetcher
* fetcher
,
42 const base::DictionaryValue
& value
,
43 bool has_error
) override
{
44 saved_value_
.reset(value
.DeepCopy());
45 OnParsedJsonInternal(has_error
);
48 MOCK_METHOD1(OnParsedJsonInternal
, void(bool has_error
));
50 virtual void OnNeedPrivetToken(
51 PrivetURLFetcher
* fetcher
,
52 const PrivetURLFetcher::TokenCallback
& callback
) {
55 bool OnRawData(PrivetURLFetcher
* fetcher
,
56 bool response_is_file
,
57 const std::string
& data
,
58 const base::FilePath
& response_file
) {
59 if (!raw_mode_
) return false;
61 if (response_is_file
) {
62 EXPECT_TRUE(response_file
!= base::FilePath());
65 OnRawDataInternal(data
);
71 MOCK_METHOD1(OnRawDataInternal
, void(std::string data
));
73 MOCK_METHOD0(OnFileInternal
, void());
75 const base::DictionaryValue
* saved_value() { return saved_value_
.get(); }
77 void SetRawMode(bool raw_mode
) {
81 std::string
GetAuthToken() { return "MyAuthToken"; }
84 scoped_ptr
<base::DictionaryValue
> saved_value_
;
88 class PrivetURLFetcherTest
: public ::testing::Test
{
90 PrivetURLFetcherTest() {
91 request_context_
= new net::TestURLRequestContextGetter(
92 base::MessageLoopProxy::current());
93 privet_urlfetcher_
.reset(new PrivetURLFetcher(
94 GURL(kSamplePrivetURL
),
95 net::URLFetcher::POST
,
96 request_context_
.get(),
99 PrivetURLFetcher::SetTokenForHost(GURL(kSamplePrivetURL
).GetOrigin().spec(),
102 virtual ~PrivetURLFetcherTest() {
105 void RunFor(base::TimeDelta time_period
) {
106 base::CancelableCallback
<void()> callback(base::Bind(
107 &PrivetURLFetcherTest::Stop
, base::Unretained(this)));
108 base::MessageLoop::current()->PostDelayedTask(
109 FROM_HERE
, callback
.callback(), time_period
);
111 base::MessageLoop::current()->Run();
116 base::MessageLoop::current()->Quit();
120 base::MessageLoop loop_
;
121 scoped_refptr
<net::TestURLRequestContextGetter
> request_context_
;
122 net::TestURLFetcherFactory fetcher_factory_
;
123 scoped_ptr
<PrivetURLFetcher
> privet_urlfetcher_
;
124 StrictMock
<MockPrivetURLFetcherDelegate
> delegate_
;
127 TEST_F(PrivetURLFetcherTest
, FetchSuccess
) {
128 privet_urlfetcher_
->Start();
129 net::TestURLFetcher
* fetcher
= fetcher_factory_
.GetFetcherByID(0);
130 ASSERT_TRUE(fetcher
!= NULL
);
131 fetcher
->SetResponseString(kSampleParsableJSON
);
132 fetcher
->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS
,
134 fetcher
->set_response_code(200);
136 EXPECT_CALL(delegate_
, OnParsedJsonInternal(false));
137 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
139 const base::DictionaryValue
* value
= delegate_
.saved_value();
141 ASSERT_TRUE(value
!= NULL
);
142 ASSERT_TRUE(value
->GetInteger("hello", &hello_value
));
143 EXPECT_EQ(2, hello_value
);
146 TEST_F(PrivetURLFetcherTest
, HTTP503Retry
) {
147 privet_urlfetcher_
->Start();
148 net::TestURLFetcher
* fetcher
= fetcher_factory_
.GetFetcherByID(0);
149 ASSERT_TRUE(fetcher
!= NULL
);
150 fetcher
->SetResponseString(kSampleParsableJSON
);
151 fetcher
->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS
,
153 fetcher
->set_response_code(503);
155 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
157 RunFor(base::TimeDelta::FromSeconds(7));
158 fetcher
= fetcher_factory_
.GetFetcherByID(0);
160 ASSERT_TRUE(fetcher
!= NULL
);
161 fetcher
->SetResponseString(kSampleParsableJSON
);
162 fetcher
->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS
,
164 fetcher
->set_response_code(200);
166 EXPECT_CALL(delegate_
, OnParsedJsonInternal(false));
167 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
170 TEST_F(PrivetURLFetcherTest
, ResponseCodeError
) {
171 privet_urlfetcher_
->Start();
172 net::TestURLFetcher
* fetcher
= fetcher_factory_
.GetFetcherByID(0);
173 ASSERT_TRUE(fetcher
!= NULL
);
174 fetcher
->SetResponseString(kSampleParsableJSON
);
175 fetcher
->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS
,
177 fetcher
->set_response_code(404);
179 EXPECT_CALL(delegate_
,
180 OnErrorInternal(PrivetURLFetcher::RESPONSE_CODE_ERROR
));
181 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
184 TEST_F(PrivetURLFetcherTest
, JsonParseError
) {
185 privet_urlfetcher_
->Start();
186 net::TestURLFetcher
* fetcher
= fetcher_factory_
.GetFetcherByID(0);
187 ASSERT_TRUE(fetcher
!= NULL
);
188 fetcher
->SetResponseString(kSampleUnparsableJSON
);
189 fetcher
->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS
,
191 fetcher
->set_response_code(200);
193 EXPECT_CALL(delegate_
,
194 OnErrorInternal(PrivetURLFetcher::JSON_PARSE_ERROR
));
195 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
198 TEST_F(PrivetURLFetcherTest
, Header
) {
199 privet_urlfetcher_
->Start();
200 net::TestURLFetcher
* fetcher
= fetcher_factory_
.GetFetcherByID(0);
201 ASSERT_TRUE(fetcher
!= NULL
);
202 net::HttpRequestHeaders headers
;
203 fetcher
->GetExtraRequestHeaders(&headers
);
205 std::string header_token
;
206 ASSERT_TRUE(headers
.GetHeader("X-Privet-Token", &header_token
));
207 EXPECT_EQ(kSamplePrivetToken
, header_token
);
210 TEST_F(PrivetURLFetcherTest
, Header2
) {
211 PrivetURLFetcher::SetTokenForHost(GURL(kSamplePrivetURL
).GetOrigin().spec(),
214 privet_urlfetcher_
->SendEmptyPrivetToken();
215 privet_urlfetcher_
->Start();
217 net::TestURLFetcher
* fetcher
= fetcher_factory_
.GetFetcherByID(0);
218 ASSERT_TRUE(fetcher
!= NULL
);
219 net::HttpRequestHeaders headers
;
220 fetcher
->GetExtraRequestHeaders(&headers
);
222 std::string header_token
;
223 ASSERT_TRUE(headers
.GetHeader("X-Privet-Token", &header_token
));
224 EXPECT_EQ(kEmptyPrivetToken
, header_token
);
227 TEST_F(PrivetURLFetcherTest
, AlwaysSendEmpty
) {
228 PrivetURLFetcher::SetTokenForHost(GURL(kSamplePrivetURL
).GetOrigin().spec(),
231 privet_urlfetcher_
->SendEmptyPrivetToken();
232 privet_urlfetcher_
->Start();
234 net::TestURLFetcher
* fetcher
= fetcher_factory_
.GetFetcherByID(0);
235 ASSERT_TRUE(fetcher
!= NULL
);
236 net::HttpRequestHeaders headers
;
237 fetcher
->GetExtraRequestHeaders(&headers
);
239 std::string header_token
;
240 ASSERT_TRUE(headers
.GetHeader("X-Privet-Token", &header_token
));
241 EXPECT_EQ(kEmptyPrivetToken
, header_token
);
244 TEST_F(PrivetURLFetcherTest
, FetchHasError
) {
245 privet_urlfetcher_
->Start();
246 net::TestURLFetcher
* fetcher
= fetcher_factory_
.GetFetcherByID(0);
247 ASSERT_TRUE(fetcher
!= NULL
);
248 fetcher
->SetResponseString(kSampleJSONWithError
);
249 fetcher
->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS
,
251 fetcher
->set_response_code(200);
253 EXPECT_CALL(delegate_
, OnParsedJsonInternal(true));
254 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
257 TEST_F(PrivetURLFetcherTest
, FetcherRawData
) {
258 delegate_
.SetRawMode(true);
259 privet_urlfetcher_
->Start();
260 net::TestURLFetcher
* fetcher
= fetcher_factory_
.GetFetcherByID(0);
261 ASSERT_TRUE(fetcher
!= NULL
);
262 fetcher
->SetResponseString(kSampleJSONWithError
);
263 fetcher
->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS
,
265 fetcher
->set_response_code(200);
267 EXPECT_CALL(delegate_
, OnRawDataInternal(kSampleJSONWithError
));
268 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
271 TEST_F(PrivetURLFetcherTest
, RangeRequest
) {
272 delegate_
.SetRawMode(true);
273 privet_urlfetcher_
->SetByteRange(200, 300);
274 privet_urlfetcher_
->Start();
275 net::TestURLFetcher
* fetcher
= fetcher_factory_
.GetFetcherByID(0);
276 ASSERT_TRUE(fetcher
!= NULL
);
277 net::HttpRequestHeaders headers
;
278 fetcher
->GetExtraRequestHeaders(&headers
);
280 std::string header_range
;
281 ASSERT_TRUE(headers
.GetHeader("Range", &header_range
));
282 EXPECT_EQ("bytes=200-300", header_range
);
285 TEST_F(PrivetURLFetcherTest
, FetcherToFile
) {
286 delegate_
.SetRawMode(true);
287 privet_urlfetcher_
->SaveResponseToFile();
288 privet_urlfetcher_
->Start();
289 net::TestURLFetcher
* fetcher
= fetcher_factory_
.GetFetcherByID(0);
290 ASSERT_TRUE(fetcher
!= NULL
);
291 fetcher
->SetResponseFilePath(
292 base::FilePath(FILE_PATH_LITERAL("sample/file")));
293 fetcher
->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS
,
295 fetcher
->set_response_code(200);
297 EXPECT_CALL(delegate_
, OnFileInternal());
298 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
301 TEST_F(PrivetURLFetcherTest
, V3Mode
) {
302 delegate_
.SetRawMode(true);
303 privet_urlfetcher_
->V3Mode();
304 privet_urlfetcher_
->Start();
305 net::TestURLFetcher
* fetcher
= fetcher_factory_
.GetFetcherByID(0);
306 ASSERT_TRUE(fetcher
!= NULL
);
307 fetcher
->SetResponseFilePath(
308 base::FilePath(FILE_PATH_LITERAL("sample/file")));
309 net::HttpRequestHeaders headers
;
310 fetcher
->GetExtraRequestHeaders(&headers
);
312 std::string header_token
;
313 ASSERT_FALSE(headers
.GetHeader("X-Privet-Token", &header_token
));
314 ASSERT_FALSE(headers
.GetHeader("X-Privet-Auth", &header_token
));
315 ASSERT_TRUE(headers
.GetHeader("Authorization", &header_token
));
316 ASSERT_EQ("MyAuthToken", header_token
);
321 } // namespace local_discovery