1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/location.h"
#include "base/single_thread_task_runner.h"
#include "base/thread_task_runner_handle.h"
#include "chrome/browser/local_discovery/privet_url_fetcher.h"
#include "net/url_request/test_url_fetcher_factory.h"
#include "net/url_request/url_request_test_util.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using testing::StrictMock;
namespace local_discovery {
namespace {
const char kSamplePrivetURL[] =
"http://10.0.0.8:7676/privet/register?action=start";
const char kSamplePrivetToken[] = "MyToken";
const char kEmptyPrivetToken[] = "\"\"";
const char kSampleParsableJSON[] = "{ \"hello\" : 2 }";
const char kSampleUnparsableJSON[] = "{ \"hello\" : }";
const char kSampleJSONWithError[] = "{ \"error\" : \"unittest_example\" }";
class MockPrivetURLFetcherDelegate : public PrivetURLFetcher::Delegate {
public:
MockPrivetURLFetcherDelegate() : raw_mode_(false) {
}
~MockPrivetURLFetcherDelegate() override {
}
void OnError(PrivetURLFetcher* fetcher,
PrivetURLFetcher::ErrorType error) override {
OnErrorInternal(error);
}
MOCK_METHOD1(OnErrorInternal, void(PrivetURLFetcher::ErrorType error));
void OnParsedJson(PrivetURLFetcher* fetcher,
const base::DictionaryValue& value,
bool has_error) override {
saved_value_.reset(value.DeepCopy());
OnParsedJsonInternal(has_error);
}
MOCK_METHOD1(OnParsedJsonInternal, void(bool has_error));
virtual void OnNeedPrivetToken(
PrivetURLFetcher* fetcher,
const PrivetURLFetcher::TokenCallback& callback) {
}
bool OnRawData(PrivetURLFetcher* fetcher,
bool response_is_file,
const std::string& data,
const base::FilePath& response_file) {
if (!raw_mode_) return false;
if (response_is_file) {
EXPECT_TRUE(response_file != base::FilePath());
OnFileInternal();
} else {
OnRawDataInternal(data);
}
return true;
}
MOCK_METHOD1(OnRawDataInternal, void(const std::string& data));
MOCK_METHOD0(OnFileInternal, void());
const base::DictionaryValue* saved_value() { return saved_value_.get(); }
void SetRawMode(bool raw_mode) {
raw_mode_ = raw_mode;
}
private:
scoped_ptr<base::DictionaryValue> saved_value_;
bool raw_mode_;
};
class PrivetURLFetcherTest : public ::testing::Test {
public:
PrivetURLFetcherTest() {
request_context_ = new net::TestURLRequestContextGetter(
base::ThreadTaskRunnerHandle::Get());
privet_urlfetcher_.reset(new PrivetURLFetcher(
GURL(kSamplePrivetURL),
net::URLFetcher::POST,
request_context_.get(),
&delegate_));
PrivetURLFetcher::SetTokenForHost(GURL(kSamplePrivetURL).GetOrigin().spec(),
kSamplePrivetToken);
}
virtual ~PrivetURLFetcherTest() {
}
void RunFor(base::TimeDelta time_period) {
base::CancelableCallback<void()> callback(base::Bind(
&PrivetURLFetcherTest::Stop, base::Unretained(this)));
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE, callback.callback(), time_period);
base::MessageLoop::current()->Run();
callback.Cancel();
}
void Stop() { base::MessageLoop::current()->QuitWhenIdle(); }
protected:
base::MessageLoop loop_;
scoped_refptr<net::TestURLRequestContextGetter> request_context_;
net::TestURLFetcherFactory fetcher_factory_;
scoped_ptr<PrivetURLFetcher> privet_urlfetcher_;
StrictMock<MockPrivetURLFetcherDelegate> delegate_;
};
TEST_F(PrivetURLFetcherTest, FetchSuccess) {
privet_urlfetcher_->Start();
net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher != NULL);
fetcher->SetResponseString(kSampleParsableJSON);
fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
net::OK));
fetcher->set_response_code(200);
EXPECT_CALL(delegate_, OnParsedJsonInternal(false));
fetcher->delegate()->OnURLFetchComplete(fetcher);
const base::DictionaryValue* value = delegate_.saved_value();
int hello_value;
ASSERT_TRUE(value != NULL);
ASSERT_TRUE(value->GetInteger("hello", &hello_value));
EXPECT_EQ(2, hello_value);
}
TEST_F(PrivetURLFetcherTest, HTTP503Retry) {
privet_urlfetcher_->Start();
net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher != NULL);
fetcher->SetResponseString(kSampleParsableJSON);
fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
net::OK));
fetcher->set_response_code(503);
fetcher->delegate()->OnURLFetchComplete(fetcher);
RunFor(base::TimeDelta::FromSeconds(7));
fetcher = fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher != NULL);
fetcher->SetResponseString(kSampleParsableJSON);
fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
net::OK));
fetcher->set_response_code(200);
EXPECT_CALL(delegate_, OnParsedJsonInternal(false));
fetcher->delegate()->OnURLFetchComplete(fetcher);
}
TEST_F(PrivetURLFetcherTest, ResponseCodeError) {
privet_urlfetcher_->Start();
net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher != NULL);
fetcher->SetResponseString(kSampleParsableJSON);
fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
net::OK));
fetcher->set_response_code(404);
EXPECT_CALL(delegate_,
OnErrorInternal(PrivetURLFetcher::RESPONSE_CODE_ERROR));
fetcher->delegate()->OnURLFetchComplete(fetcher);
}
TEST_F(PrivetURLFetcherTest, JsonParseError) {
privet_urlfetcher_->Start();
net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher != NULL);
fetcher->SetResponseString(kSampleUnparsableJSON);
fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
net::OK));
fetcher->set_response_code(200);
EXPECT_CALL(delegate_,
OnErrorInternal(PrivetURLFetcher::JSON_PARSE_ERROR));
fetcher->delegate()->OnURLFetchComplete(fetcher);
}
TEST_F(PrivetURLFetcherTest, Header) {
privet_urlfetcher_->Start();
net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher != NULL);
net::HttpRequestHeaders headers;
fetcher->GetExtraRequestHeaders(&headers);
std::string header_token;
ASSERT_TRUE(headers.GetHeader("X-Privet-Token", &header_token));
EXPECT_EQ(kSamplePrivetToken, header_token);
}
TEST_F(PrivetURLFetcherTest, Header2) {
PrivetURLFetcher::SetTokenForHost(GURL(kSamplePrivetURL).GetOrigin().spec(),
"");
privet_urlfetcher_->SendEmptyPrivetToken();
privet_urlfetcher_->Start();
net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher != NULL);
net::HttpRequestHeaders headers;
fetcher->GetExtraRequestHeaders(&headers);
std::string header_token;
ASSERT_TRUE(headers.GetHeader("X-Privet-Token", &header_token));
EXPECT_EQ(kEmptyPrivetToken, header_token);
}
TEST_F(PrivetURLFetcherTest, AlwaysSendEmpty) {
PrivetURLFetcher::SetTokenForHost(GURL(kSamplePrivetURL).GetOrigin().spec(),
"SampleToken");
privet_urlfetcher_->SendEmptyPrivetToken();
privet_urlfetcher_->Start();
net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher != NULL);
net::HttpRequestHeaders headers;
fetcher->GetExtraRequestHeaders(&headers);
std::string header_token;
ASSERT_TRUE(headers.GetHeader("X-Privet-Token", &header_token));
EXPECT_EQ(kEmptyPrivetToken, header_token);
}
TEST_F(PrivetURLFetcherTest, FetchHasError) {
privet_urlfetcher_->Start();
net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher != NULL);
fetcher->SetResponseString(kSampleJSONWithError);
fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
net::OK));
fetcher->set_response_code(200);
EXPECT_CALL(delegate_, OnParsedJsonInternal(true));
fetcher->delegate()->OnURLFetchComplete(fetcher);
}
TEST_F(PrivetURLFetcherTest, FetcherRawData) {
delegate_.SetRawMode(true);
privet_urlfetcher_->Start();
net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher != NULL);
fetcher->SetResponseString(kSampleJSONWithError);
fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
net::OK));
fetcher->set_response_code(200);
EXPECT_CALL(delegate_, OnRawDataInternal(kSampleJSONWithError));
fetcher->delegate()->OnURLFetchComplete(fetcher);
}
TEST_F(PrivetURLFetcherTest, RangeRequest) {
delegate_.SetRawMode(true);
privet_urlfetcher_->SetByteRange(200, 300);
privet_urlfetcher_->Start();
net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher != NULL);
net::HttpRequestHeaders headers;
fetcher->GetExtraRequestHeaders(&headers);
std::string header_range;
ASSERT_TRUE(headers.GetHeader("Range", &header_range));
EXPECT_EQ("bytes=200-300", header_range);
}
TEST_F(PrivetURLFetcherTest, FetcherToFile) {
delegate_.SetRawMode(true);
privet_urlfetcher_->SaveResponseToFile();
privet_urlfetcher_->Start();
net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher != NULL);
fetcher->SetResponseFilePath(
base::FilePath(FILE_PATH_LITERAL("sample/file")));
fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
net::OK));
fetcher->set_response_code(200);
EXPECT_CALL(delegate_, OnFileInternal());
fetcher->delegate()->OnURLFetchComplete(fetcher);
}
} // namespace
} // namespace local_discovery
|