Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(24)

Side by Side Diff: net/url_request/report_sender_unittest.cc

Issue 2648713002: Add response code to the success callback of ReportSender (Closed)
Patch Set: eroman and estark comments Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/url_request/report_sender.cc ('k') | net/url_request/url_request_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/url_request/report_sender.h" 5 #include "net/url_request/report_sender.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "base/run_loop.h" 10 #include "base/run_loop.h"
11 #include "base/threading/thread_task_runner_handle.h" 11 #include "base/threading/thread_task_runner_handle.h"
12 #include "net/base/load_flags.h" 12 #include "net/base/load_flags.h"
13 #include "net/base/network_delegate_impl.h" 13 #include "net/base/network_delegate_impl.h"
14 #include "net/base/upload_bytes_element_reader.h" 14 #include "net/base/upload_bytes_element_reader.h"
15 #include "net/base/upload_data_stream.h" 15 #include "net/base/upload_data_stream.h"
16 #include "net/base/upload_element_reader.h" 16 #include "net/base/upload_element_reader.h"
17 #include "net/http/http_response_headers.h"
18 #include "net/http/http_status_code.h"
17 #include "net/test/url_request/url_request_failed_job.h" 19 #include "net/test/url_request/url_request_failed_job.h"
18 #include "net/test/url_request/url_request_mock_data_job.h" 20 #include "net/test/url_request/url_request_mock_data_job.h"
21 #include "net/test/url_request/url_request_mock_http_job.h"
19 #include "net/url_request/url_request_filter.h" 22 #include "net/url_request/url_request_filter.h"
20 #include "net/url_request/url_request_test_util.h" 23 #include "net/url_request/url_request_test_util.h"
21 #include "testing/gtest/include/gtest/gtest.h" 24 #include "testing/gtest/include/gtest/gtest.h"
22 25
23 namespace net { 26 namespace net {
24 namespace { 27 namespace {
25 28
26 const char kDummyReport[] = "foo.test"; 29 const char kDummyReport[] = "foo.test";
27 const char kSecondDummyReport[] = "foo2.test"; 30 const char kSecondDummyReport[] = "foo2.test";
31 const char kServerErrorHostname[] = "mock.server.error";
28 32
29 void MarkURLRequestDestroyed(bool* url_request_destroyed) { 33 void MarkURLRequestDestroyed(bool* url_request_destroyed) {
30 *url_request_destroyed = true; 34 *url_request_destroyed = true;
31 } 35 }
32 36
33 // Checks that data uploaded in the request matches the test report 37 // Checks that data uploaded in the request matches the test report
34 // data. Erases the sent reports from |expect_reports|. 38 // data. Erases the sent reports from |expect_reports|.
35 void CheckUploadData(const URLRequest& request, 39 void CheckUploadData(const URLRequest& request,
36 std::set<std::string>* expect_reports) { 40 std::set<std::string>* expect_reports) {
37 const UploadDataStream* upload = request.get_upload(); 41 const UploadDataStream* upload = request.get_upload();
38 ASSERT_TRUE(upload); 42 ASSERT_TRUE(upload);
39 ASSERT_TRUE(upload->GetElementReaders()); 43 ASSERT_TRUE(upload->GetElementReaders());
40 ASSERT_EQ(1u, upload->GetElementReaders()->size()); 44 ASSERT_EQ(1u, upload->GetElementReaders()->size());
41 45
42 const UploadBytesElementReader* reader = 46 const UploadBytesElementReader* reader =
43 (*upload->GetElementReaders())[0]->AsBytesReader(); 47 (*upload->GetElementReaders())[0]->AsBytesReader();
44 ASSERT_TRUE(reader); 48 ASSERT_TRUE(reader);
45 std::string upload_data(reader->bytes(), reader->length()); 49 std::string upload_data(reader->bytes(), reader->length());
46 50
47 EXPECT_EQ(1u, expect_reports->erase(upload_data)); 51 EXPECT_EQ(1u, expect_reports->erase(upload_data));
48 } 52 }
49 53
50 // Provides an error callback for report sending that sets |called| to 54 // Error callback for a report with a net error.
51 // true. 55 void ErrorCallback(bool* called,
52 void ErrorCallback(bool* called, const GURL& report_uri, int net_error) { 56 const GURL& report_uri,
57 int net_error,
58 int http_response_code) {
53 EXPECT_NE(OK, net_error); 59 EXPECT_NE(OK, net_error);
60 EXPECT_EQ(-1, http_response_code);
54 *called = true; 61 *called = true;
55 } 62 }
56 63
64 // Error callback for a report with a non-200 HTTP response code and no net
65 // errors.
66 void ServerErrorResponseCallback(bool* called,
67 const GURL& report_uri,
68 int net_error,
69 int http_response_code) {
70 EXPECT_EQ(OK, net_error);
71 EXPECT_EQ(HTTP_INTERNAL_SERVER_ERROR, http_response_code);
72 *called = true;
73 }
74
57 void SuccessCallback(bool* called) { 75 void SuccessCallback(bool* called) {
58 *called = true; 76 *called = true;
59 } 77 }
60 78
79 // URLRequestJob that returns an HTTP 500 response.
80 class MockServerErrorJob : public URLRequestJob {
81 public:
82 MockServerErrorJob(URLRequest* request, NetworkDelegate* network_delegate)
83 : URLRequestJob(request, network_delegate) {}
84 ~MockServerErrorJob() override {}
85
86 protected:
87 void GetResponseInfo(HttpResponseInfo* info) override {
88 info->headers = new HttpResponseHeaders(
89 "HTTP/1.1 500 Internal Server Error\n"
90 "Content-type: text/plain\n"
91 "Content-Length: 0\n");
92 }
93 void Start() override { NotifyHeadersComplete(); }
94
95 private:
96 DISALLOW_COPY_AND_ASSIGN(MockServerErrorJob);
97 };
98
99 class MockServerErrorJobInterceptor : public URLRequestInterceptor {
100 public:
101 MockServerErrorJobInterceptor() {}
102 ~MockServerErrorJobInterceptor() override {}
103
104 URLRequestJob* MaybeInterceptRequest(
105 URLRequest* request,
106 NetworkDelegate* network_delegate) const override {
107 return new MockServerErrorJob(request, network_delegate);
108 }
109
110 private:
111 DISALLOW_COPY_AND_ASSIGN(MockServerErrorJobInterceptor);
112 };
113
61 // A network delegate that lets tests check that a report 114 // A network delegate that lets tests check that a report
62 // was sent. It counts the number of requests and lets tests register a 115 // was sent. It counts the number of requests and lets tests register a
63 // callback to run when the request is destroyed. It also checks that 116 // callback to run when the request is destroyed. It also checks that
64 // the uploaded data is as expected. 117 // the uploaded data is as expected.
65 class TestReportSenderNetworkDelegate : public NetworkDelegateImpl { 118 class TestReportSenderNetworkDelegate : public NetworkDelegateImpl {
66 public: 119 public:
67 TestReportSenderNetworkDelegate() 120 TestReportSenderNetworkDelegate()
68 : url_request_destroyed_callback_(base::Bind(&base::DoNothing)), 121 : url_request_destroyed_callback_(base::Bind(&base::DoNothing)),
69 all_url_requests_destroyed_callback_(base::Bind(&base::DoNothing)), 122 all_url_requests_destroyed_callback_(base::Bind(&base::DoNothing)),
70 num_requests_(0), 123 num_requests_(0),
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 class ReportSenderTest : public ::testing::Test { 198 class ReportSenderTest : public ::testing::Test {
146 public: 199 public:
147 ReportSenderTest() : context_(true) { 200 ReportSenderTest() : context_(true) {
148 context_.set_network_delegate(&network_delegate_); 201 context_.set_network_delegate(&network_delegate_);
149 context_.Init(); 202 context_.Init();
150 } 203 }
151 204
152 void SetUp() override { 205 void SetUp() override {
153 URLRequestFailedJob::AddUrlHandler(); 206 URLRequestFailedJob::AddUrlHandler();
154 URLRequestMockDataJob::AddUrlHandler(); 207 URLRequestMockDataJob::AddUrlHandler();
208 URLRequestFilter::GetInstance()->AddHostnameInterceptor(
209 "http", kServerErrorHostname,
210 std::unique_ptr<URLRequestInterceptor>(
211 new MockServerErrorJobInterceptor()));
155 } 212 }
156 213
157 void TearDown() override { URLRequestFilter::GetInstance()->ClearHandlers(); } 214 void TearDown() override { URLRequestFilter::GetInstance()->ClearHandlers(); }
158 215
159 TestURLRequestContext* context() { return &context_; } 216 TestURLRequestContext* context() { return &context_; }
160 217
161 protected: 218 protected:
162 void SendReport( 219 void SendReport(
163 ReportSender* reporter, 220 ReportSender* reporter,
164 const std::string& report, 221 const std::string& report,
165 const GURL& url, 222 const GURL& url,
166 size_t request_sequence_number, 223 size_t request_sequence_number,
167 const base::Callback<void()>& success_callback, 224 const base::Callback<void()>& success_callback,
168 const base::Callback<void(const GURL&, int)>& error_callback) { 225 const base::Callback<void(const GURL&, int, int)>& error_callback) {
169 base::RunLoop run_loop; 226 base::RunLoop run_loop;
170 network_delegate_.set_url_request_destroyed_callback( 227 network_delegate_.set_url_request_destroyed_callback(
171 run_loop.QuitClosure()); 228 run_loop.QuitClosure());
172 229
173 network_delegate_.set_expect_url(url); 230 network_delegate_.set_expect_url(url);
174 network_delegate_.ExpectReport(report); 231 network_delegate_.ExpectReport(report);
175 network_delegate_.set_expected_content_type("application/foobar"); 232 network_delegate_.set_expected_content_type("application/foobar");
176 233
177 EXPECT_EQ(request_sequence_number, network_delegate_.num_requests()); 234 EXPECT_EQ(request_sequence_number, network_delegate_.num_requests());
178 235
179 reporter->Send(url, "application/foobar", report, success_callback, 236 reporter->Send(url, "application/foobar", report, success_callback,
180 error_callback); 237 error_callback);
181 238
182 // The report is sent asynchronously, so wait for the report's 239 // The report is sent asynchronously, so wait for the report's
183 // URLRequest to be destroyed before checking that the report was 240 // URLRequest to be destroyed before checking that the report was
184 // sent. 241 // sent.
185 run_loop.Run(); 242 run_loop.Run();
186 243
187 EXPECT_EQ(request_sequence_number + 1, network_delegate_.num_requests()); 244 EXPECT_EQ(request_sequence_number + 1, network_delegate_.num_requests());
188 } 245 }
189 246
190 void SendReport(ReportSender* reporter, 247 void SendReport(ReportSender* reporter,
191 const std::string& report, 248 const std::string& report,
192 const GURL& url, 249 const GURL& url,
193 size_t request_sequence_number) { 250 size_t request_sequence_number) {
194 SendReport(reporter, report, url, request_sequence_number, base::Closure(), 251 SendReport(reporter, report, url, request_sequence_number,
195 base::Callback<void(const GURL&, int)>()); 252 base::Callback<void()>(),
253 base::Callback<void(const GURL&, int, int)>());
196 } 254 }
197 255
198 TestReportSenderNetworkDelegate network_delegate_; 256 TestReportSenderNetworkDelegate network_delegate_;
199 257
200 private: 258 private:
201 TestURLRequestContext context_; 259 TestURLRequestContext context_;
202 }; 260 };
203 261
204 // Test that ReportSender::Send creates a URLRequest for the 262 // Test that ReportSender::Send creates a URLRequest for the
205 // endpoint and sends the expected data. 263 // endpoint and sends the expected data.
(...skipping 18 matching lines...) Expand all
224 GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); 282 GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1);
225 network_delegate_.set_expect_url(url); 283 network_delegate_.set_expect_url(url);
226 network_delegate_.ExpectReport(kDummyReport); 284 network_delegate_.ExpectReport(kDummyReport);
227 network_delegate_.ExpectReport(kSecondDummyReport); 285 network_delegate_.ExpectReport(kSecondDummyReport);
228 network_delegate_.set_expected_content_type("application/foobar"); 286 network_delegate_.set_expected_content_type("application/foobar");
229 287
230 ReportSender reporter(context(), ReportSender::DO_NOT_SEND_COOKIES); 288 ReportSender reporter(context(), ReportSender::DO_NOT_SEND_COOKIES);
231 289
232 EXPECT_EQ(0u, network_delegate_.num_requests()); 290 EXPECT_EQ(0u, network_delegate_.num_requests());
233 291
234 reporter.Send(url, "application/foobar", kDummyReport, base::Closure(), 292 reporter.Send(url, "application/foobar", kDummyReport,
235 base::Callback<void(const GURL&, int)>()); 293 base::Callback<void()>(),
236 reporter.Send(url, "application/foobar", kSecondDummyReport, base::Closure(), 294 base::Callback<void(const GURL&, int, int)>());
237 base::Callback<void(const GURL&, int)>()); 295 reporter.Send(url, "application/foobar", kSecondDummyReport,
296 base::Callback<void()>(),
297 base::Callback<void(const GURL&, int, int)>());
238 298
239 run_loop.Run(); 299 run_loop.Run();
240 300
241 EXPECT_EQ(2u, network_delegate_.num_requests()); 301 EXPECT_EQ(2u, network_delegate_.num_requests());
242 } 302 }
243 303
244 // Test that pending URLRequests get cleaned up when the report sender 304 // Test that pending URLRequests get cleaned up when the report sender
245 // is deleted. 305 // is deleted.
246 TEST_F(ReportSenderTest, PendingRequestGetsDeleted) { 306 TEST_F(ReportSenderTest, PendingRequestGetsDeleted) {
247 bool url_request_destroyed = false; 307 bool url_request_destroyed = false;
248 network_delegate_.set_url_request_destroyed_callback(base::Bind( 308 network_delegate_.set_url_request_destroyed_callback(base::Bind(
249 &MarkURLRequestDestroyed, base::Unretained(&url_request_destroyed))); 309 &MarkURLRequestDestroyed, base::Unretained(&url_request_destroyed)));
250 310
251 GURL url = URLRequestFailedJob::GetMockHttpUrlWithFailurePhase( 311 GURL url = URLRequestFailedJob::GetMockHttpUrlWithFailurePhase(
252 URLRequestFailedJob::START, ERR_IO_PENDING); 312 URLRequestFailedJob::START, ERR_IO_PENDING);
253 network_delegate_.set_expect_url(url); 313 network_delegate_.set_expect_url(url);
254 network_delegate_.ExpectReport(kDummyReport); 314 network_delegate_.ExpectReport(kDummyReport);
255 network_delegate_.set_expected_content_type("application/foobar"); 315 network_delegate_.set_expected_content_type("application/foobar");
256 316
257 EXPECT_EQ(0u, network_delegate_.num_requests()); 317 EXPECT_EQ(0u, network_delegate_.num_requests());
258 318
259 std::unique_ptr<ReportSender> reporter( 319 std::unique_ptr<ReportSender> reporter(
260 new ReportSender(context(), ReportSender::DO_NOT_SEND_COOKIES)); 320 new ReportSender(context(), ReportSender::DO_NOT_SEND_COOKIES));
261 reporter->Send(url, "application/foobar", kDummyReport, base::Closure(), 321 reporter->Send(url, "application/foobar", kDummyReport,
262 base::Callback<void(const GURL&, int)>()); 322 base::Callback<void()>(),
323 base::Callback<void(const GURL&, int, int)>());
263 reporter.reset(); 324 reporter.reset();
264 325
265 EXPECT_EQ(1u, network_delegate_.num_requests()); 326 EXPECT_EQ(1u, network_delegate_.num_requests());
266 EXPECT_TRUE(url_request_destroyed); 327 EXPECT_TRUE(url_request_destroyed);
267 } 328 }
268 329
269 // Test that a request that returns an error gets cleaned up. 330 // Test that a request that returns an error gets cleaned up.
270 TEST_F(ReportSenderTest, ErroredRequestGetsDeleted) { 331 TEST_F(ReportSenderTest, ErroredRequestGetsDeleted) {
271 GURL url = URLRequestFailedJob::GetMockHttpsUrl(ERR_FAILED); 332 GURL url = URLRequestFailedJob::GetMockHttpsUrl(ERR_FAILED);
272 ReportSender reporter(context(), ReportSender::DO_NOT_SEND_COOKIES); 333 ReportSender reporter(context(), ReportSender::DO_NOT_SEND_COOKIES);
273 // SendReport will block until the URLRequest is destroyed. 334 // SendReport will block until the URLRequest is destroyed.
274 SendReport(&reporter, kDummyReport, url, 0); 335 SendReport(&reporter, kDummyReport, url, 0);
275 } 336 }
276 337
277 // Test that the error callback, if provided, gets called when a request 338 // Test that the error callback, if provided, gets called when a request
278 // returns an error and the success callback doesn't get called. 339 // returns an error and the success callback doesn't get called.
279 TEST_F(ReportSenderTest, ErroredRequestCallsErrorCallback) { 340 TEST_F(ReportSenderTest, ErroredRequestCallsErrorCallback) {
280 bool error_callback_called = false; 341 bool error_callback_called = false;
281 bool success_callback_called = false; 342 bool success_callback_called = false;
282 GURL url = URLRequestFailedJob::GetMockHttpsUrl(ERR_FAILED); 343 const GURL url = URLRequestFailedJob::GetMockHttpsUrl(ERR_FAILED);
283 ReportSender reporter(context(), ReportSender::DO_NOT_SEND_COOKIES); 344 ReportSender reporter(context(), ReportSender::DO_NOT_SEND_COOKIES);
284 // SendReport will block until the URLRequest is destroyed. 345 // SendReport will block until the URLRequest is destroyed.
285 SendReport(&reporter, kDummyReport, url, 0, 346 SendReport(&reporter, kDummyReport, url, 0,
286 base::Bind(SuccessCallback, &success_callback_called), 347 base::Bind(SuccessCallback, &success_callback_called),
287 base::Bind(ErrorCallback, &error_callback_called)); 348 base::Bind(ErrorCallback, &error_callback_called));
288 EXPECT_TRUE(error_callback_called); 349 EXPECT_TRUE(error_callback_called);
289 EXPECT_FALSE(success_callback_called); 350 EXPECT_FALSE(success_callback_called);
290 } 351 }
291 352
353 // Test that the error callback, if provided, gets called when a request
354 // finishes successfully but results in a server error, and the success callback
355 // doesn't get called.
356 TEST_F(ReportSenderTest, BadResponseCodeCallsErrorCallback) {
357 bool error_callback_called = false;
358 bool success_callback_called = false;
359 const GURL url(std::string("http://") + kServerErrorHostname);
360 ReportSender reporter(context(), ReportSender::DO_NOT_SEND_COOKIES);
361 // SendReport will block until the URLRequest is destroyed.
362 SendReport(&reporter, kDummyReport, url, 0,
363 base::Bind(SuccessCallback, &success_callback_called),
364 base::Bind(ServerErrorResponseCallback, &error_callback_called));
365 EXPECT_TRUE(error_callback_called);
366 EXPECT_FALSE(success_callback_called);
367 }
368
292 // Test that the error callback does not get called and the success callback 369 // Test that the error callback does not get called and the success callback
293 /// gets called when a request does not return an error. 370 /// gets called when a request does not return an error.
294 TEST_F(ReportSenderTest, SuccessfulRequestCallsSuccessCallback) { 371 TEST_F(ReportSenderTest, SuccessfulRequestCallsSuccessCallback) {
295 bool error_callback_called = false; 372 bool error_callback_called = false;
296 bool success_callback_called = false; 373 bool success_callback_called = false;
297 GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); 374 const GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1);
298 ReportSender reporter(context(), ReportSender::DO_NOT_SEND_COOKIES); 375 ReportSender reporter(context(), ReportSender::DO_NOT_SEND_COOKIES);
299 SendReport(&reporter, kDummyReport, url, 0, 376 SendReport(&reporter, kDummyReport, url, 0,
300 base::Bind(SuccessCallback, &success_callback_called), 377 base::Bind(SuccessCallback, &success_callback_called),
301 base::Bind(ErrorCallback, &error_callback_called)); 378 base::Bind(ErrorCallback, &error_callback_called));
302 EXPECT_FALSE(error_callback_called); 379 EXPECT_FALSE(error_callback_called);
303 EXPECT_TRUE(success_callback_called); 380 EXPECT_TRUE(success_callback_called);
304 } 381 }
305 382
306 // Test that cookies are sent or not sent according to the error 383 // Test that cookies are sent or not sent according to the error
307 // reporter's cookies preference. 384 // reporter's cookies preference.
308 385
309 TEST_F(ReportSenderTest, SendCookiesPreference) { 386 TEST_F(ReportSenderTest, SendCookiesPreference) {
310 GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); 387 GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1);
311 ReportSender reporter(context(), ReportSender::SEND_COOKIES); 388 ReportSender reporter(context(), ReportSender::SEND_COOKIES);
312 389
313 network_delegate_.set_expect_cookies(true); 390 network_delegate_.set_expect_cookies(true);
314 SendReport(&reporter, kDummyReport, url, 0); 391 SendReport(&reporter, kDummyReport, url, 0);
315 } 392 }
316 393
317 TEST_F(ReportSenderTest, DoNotSendCookiesPreference) { 394 TEST_F(ReportSenderTest, DoNotSendCookiesPreference) {
318 GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); 395 GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1);
319 ReportSender reporter(context(), ReportSender::DO_NOT_SEND_COOKIES); 396 ReportSender reporter(context(), ReportSender::DO_NOT_SEND_COOKIES);
320 397
321 network_delegate_.set_expect_cookies(false); 398 network_delegate_.set_expect_cookies(false);
322 SendReport(&reporter, kDummyReport, url, 0); 399 SendReport(&reporter, kDummyReport, url, 0);
323 } 400 }
324 401
325 } // namespace 402 } // namespace
326 } // namespace net 403 } // namespace net
OLDNEW
« no previous file with comments | « net/url_request/report_sender.cc ('k') | net/url_request/url_request_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698