forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreport_sender_unittest.cc
326 lines (264 loc) · 11.5 KB
/
report_sender_unittest.cc
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// Copyright 2015 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 "net/url_request/report_sender.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/macros.h"
#include "base/run_loop.h"
#include "base/threading/thread_task_runner_handle.h"
#include "net/base/load_flags.h"
#include "net/base/network_delegate_impl.h"
#include "net/base/upload_bytes_element_reader.h"
#include "net/base/upload_data_stream.h"
#include "net/base/upload_element_reader.h"
#include "net/test/url_request/url_request_failed_job.h"
#include "net/test/url_request/url_request_mock_data_job.h"
#include "net/url_request/url_request_filter.h"
#include "net/url_request/url_request_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace net {
namespace {
const char kDummyReport[] = "foo.test";
const char kSecondDummyReport[] = "foo2.test";
void MarkURLRequestDestroyed(bool* url_request_destroyed) {
*url_request_destroyed = true;
}
// Checks that data uploaded in the request matches the test report
// data. Erases the sent reports from |expect_reports|.
void CheckUploadData(const URLRequest& request,
std::set<std::string>* expect_reports) {
const UploadDataStream* upload = request.get_upload();
ASSERT_TRUE(upload);
ASSERT_TRUE(upload->GetElementReaders());
ASSERT_EQ(1u, upload->GetElementReaders()->size());
const UploadBytesElementReader* reader =
(*upload->GetElementReaders())[0]->AsBytesReader();
ASSERT_TRUE(reader);
std::string upload_data(reader->bytes(), reader->length());
EXPECT_EQ(1u, expect_reports->erase(upload_data));
}
// Provides an error callback for report sending that sets |called| to
// true.
void ErrorCallback(bool* called, const GURL& report_uri, int net_error) {
EXPECT_NE(OK, net_error);
*called = true;
}
void SuccessCallback(bool* called) {
*called = true;
}
// A network delegate that lets tests check that a report
// was sent. It counts the number of requests and lets tests register a
// callback to run when the request is destroyed. It also checks that
// the uploaded data is as expected.
class TestReportSenderNetworkDelegate : public NetworkDelegateImpl {
public:
TestReportSenderNetworkDelegate()
: url_request_destroyed_callback_(base::Bind(&base::DoNothing)),
all_url_requests_destroyed_callback_(base::Bind(&base::DoNothing)),
num_requests_(0),
expect_cookies_(false) {}
void ExpectReport(const std::string& report) {
expect_reports_.insert(report);
}
void set_all_url_requests_destroyed_callback(const base::Closure& callback) {
all_url_requests_destroyed_callback_ = callback;
}
void set_url_request_destroyed_callback(const base::Closure& callback) {
url_request_destroyed_callback_ = callback;
}
void set_expect_url(const GURL& expect_url) { expect_url_ = expect_url; }
size_t num_requests() const { return num_requests_; }
// Sets whether cookies are expected to be sent on requests.
void set_expect_cookies(bool expect_cookies) {
expect_cookies_ = expect_cookies;
}
void set_expected_content_type(const std::string& content_type) {
expected_content_type_ = content_type;
}
// NetworkDelegateImpl implementation.
int OnBeforeURLRequest(URLRequest* request,
const CompletionCallback& callback,
GURL* new_url) override {
num_requests_++;
EXPECT_EQ(expect_url_, request->url());
EXPECT_STRCASEEQ("POST", request->method().data());
if (expect_cookies_) {
EXPECT_FALSE(request->load_flags() & LOAD_DO_NOT_SEND_COOKIES);
EXPECT_FALSE(request->load_flags() & LOAD_DO_NOT_SAVE_COOKIES);
} else {
EXPECT_TRUE(request->load_flags() & LOAD_DO_NOT_SEND_COOKIES);
EXPECT_TRUE(request->load_flags() & LOAD_DO_NOT_SAVE_COOKIES);
}
const HttpRequestHeaders& extra_headers = request->extra_request_headers();
std::string content_type;
EXPECT_TRUE(extra_headers.GetHeader(HttpRequestHeaders::kContentType,
&content_type));
EXPECT_EQ(expected_content_type_, content_type);
CheckUploadData(*request, &expect_reports_);
// Unconditionally return OK, since the sender ignores the results
// anyway.
return OK;
}
void OnURLRequestDestroyed(URLRequest* request) override {
url_request_destroyed_callback_.Run();
if (expect_reports_.empty())
all_url_requests_destroyed_callback_.Run();
}
private:
base::Closure url_request_destroyed_callback_;
base::Closure all_url_requests_destroyed_callback_;
size_t num_requests_;
GURL expect_url_;
std::set<std::string> expect_reports_;
bool expect_cookies_;
std::string expected_content_type_;
DISALLOW_COPY_AND_ASSIGN(TestReportSenderNetworkDelegate);
};
class ReportSenderTest : public ::testing::Test {
public:
ReportSenderTest() : context_(true) {
context_.set_network_delegate(&network_delegate_);
context_.Init();
}
void SetUp() override {
URLRequestFailedJob::AddUrlHandler();
URLRequestMockDataJob::AddUrlHandler();
}
void TearDown() override { URLRequestFilter::GetInstance()->ClearHandlers(); }
TestURLRequestContext* context() { return &context_; }
protected:
void SendReport(
ReportSender* reporter,
const std::string& report,
const GURL& url,
size_t request_sequence_number,
const base::Callback<void()>& success_callback,
const base::Callback<void(const GURL&, int)>& error_callback) {
base::RunLoop run_loop;
network_delegate_.set_url_request_destroyed_callback(
run_loop.QuitClosure());
network_delegate_.set_expect_url(url);
network_delegate_.ExpectReport(report);
network_delegate_.set_expected_content_type("application/foobar");
EXPECT_EQ(request_sequence_number, network_delegate_.num_requests());
reporter->Send(url, "application/foobar", report, success_callback,
error_callback);
// The report is sent asynchronously, so wait for the report's
// URLRequest to be destroyed before checking that the report was
// sent.
run_loop.Run();
EXPECT_EQ(request_sequence_number + 1, network_delegate_.num_requests());
}
void SendReport(ReportSender* reporter,
const std::string& report,
const GURL& url,
size_t request_sequence_number) {
SendReport(reporter, report, url, request_sequence_number, base::Closure(),
base::Callback<void(const GURL&, int)>());
}
TestReportSenderNetworkDelegate network_delegate_;
private:
TestURLRequestContext context_;
};
// Test that ReportSender::Send creates a URLRequest for the
// endpoint and sends the expected data.
TEST_F(ReportSenderTest, SendsRequest) {
GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1);
ReportSender reporter(context(), ReportSender::DO_NOT_SEND_COOKIES);
SendReport(&reporter, kDummyReport, url, 0);
}
TEST_F(ReportSenderTest, SendMultipleReportsSequentially) {
GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1);
ReportSender reporter(context(), ReportSender::DO_NOT_SEND_COOKIES);
SendReport(&reporter, kDummyReport, url, 0);
SendReport(&reporter, kDummyReport, url, 1);
}
TEST_F(ReportSenderTest, SendMultipleReportsSimultaneously) {
base::RunLoop run_loop;
network_delegate_.set_all_url_requests_destroyed_callback(
run_loop.QuitClosure());
GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1);
network_delegate_.set_expect_url(url);
network_delegate_.ExpectReport(kDummyReport);
network_delegate_.ExpectReport(kSecondDummyReport);
network_delegate_.set_expected_content_type("application/foobar");
ReportSender reporter(context(), ReportSender::DO_NOT_SEND_COOKIES);
EXPECT_EQ(0u, network_delegate_.num_requests());
reporter.Send(url, "application/foobar", kDummyReport, base::Closure(),
base::Callback<void(const GURL&, int)>());
reporter.Send(url, "application/foobar", kSecondDummyReport, base::Closure(),
base::Callback<void(const GURL&, int)>());
run_loop.Run();
EXPECT_EQ(2u, network_delegate_.num_requests());
}
// Test that pending URLRequests get cleaned up when the report sender
// is deleted.
TEST_F(ReportSenderTest, PendingRequestGetsDeleted) {
bool url_request_destroyed = false;
network_delegate_.set_url_request_destroyed_callback(base::Bind(
&MarkURLRequestDestroyed, base::Unretained(&url_request_destroyed)));
GURL url = URLRequestFailedJob::GetMockHttpUrlWithFailurePhase(
URLRequestFailedJob::START, ERR_IO_PENDING);
network_delegate_.set_expect_url(url);
network_delegate_.ExpectReport(kDummyReport);
network_delegate_.set_expected_content_type("application/foobar");
EXPECT_EQ(0u, network_delegate_.num_requests());
std::unique_ptr<ReportSender> reporter(
new ReportSender(context(), ReportSender::DO_NOT_SEND_COOKIES));
reporter->Send(url, "application/foobar", kDummyReport, base::Closure(),
base::Callback<void(const GURL&, int)>());
reporter.reset();
EXPECT_EQ(1u, network_delegate_.num_requests());
EXPECT_TRUE(url_request_destroyed);
}
// Test that a request that returns an error gets cleaned up.
TEST_F(ReportSenderTest, ErroredRequestGetsDeleted) {
GURL url = URLRequestFailedJob::GetMockHttpsUrl(ERR_FAILED);
ReportSender reporter(context(), ReportSender::DO_NOT_SEND_COOKIES);
// SendReport will block until the URLRequest is destroyed.
SendReport(&reporter, kDummyReport, url, 0);
}
// Test that the error callback, if provided, gets called when a request
// returns an error and the success callback doesn't get called.
TEST_F(ReportSenderTest, ErroredRequestCallsErrorCallback) {
bool error_callback_called = false;
bool success_callback_called = false;
GURL url = URLRequestFailedJob::GetMockHttpsUrl(ERR_FAILED);
ReportSender reporter(context(), ReportSender::DO_NOT_SEND_COOKIES);
// SendReport will block until the URLRequest is destroyed.
SendReport(&reporter, kDummyReport, url, 0,
base::Bind(SuccessCallback, &success_callback_called),
base::Bind(ErrorCallback, &error_callback_called));
EXPECT_TRUE(error_callback_called);
EXPECT_FALSE(success_callback_called);
}
// Test that the error callback does not get called and the success callback
/// gets called when a request does not return an error.
TEST_F(ReportSenderTest, SuccessfulRequestCallsSuccessCallback) {
bool error_callback_called = false;
bool success_callback_called = false;
GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1);
ReportSender reporter(context(), ReportSender::DO_NOT_SEND_COOKIES);
SendReport(&reporter, kDummyReport, url, 0,
base::Bind(SuccessCallback, &success_callback_called),
base::Bind(ErrorCallback, &error_callback_called));
EXPECT_FALSE(error_callback_called);
EXPECT_TRUE(success_callback_called);
}
// Test that cookies are sent or not sent according to the error
// reporter's cookies preference.
TEST_F(ReportSenderTest, SendCookiesPreference) {
GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1);
ReportSender reporter(context(), ReportSender::SEND_COOKIES);
network_delegate_.set_expect_cookies(true);
SendReport(&reporter, kDummyReport, url, 0);
}
TEST_F(ReportSenderTest, DoNotSendCookiesPreference) {
GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1);
ReportSender reporter(context(), ReportSender::DO_NOT_SEND_COOKIES);
network_delegate_.set_expect_cookies(false);
SendReport(&reporter, kDummyReport, url, 0);
}
} // namespace
} // namespace net