OLD | NEW |
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" |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 | 113 |
114 // A network delegate that lets tests check that a report | 114 // A network delegate that lets tests check that a report |
115 // 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 |
116 // 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 |
117 // the uploaded data is as expected. | 117 // the uploaded data is as expected. |
118 class TestReportSenderNetworkDelegate : public NetworkDelegateImpl { | 118 class TestReportSenderNetworkDelegate : public NetworkDelegateImpl { |
119 public: | 119 public: |
120 TestReportSenderNetworkDelegate() | 120 TestReportSenderNetworkDelegate() |
121 : url_request_destroyed_callback_(base::Bind(&base::DoNothing)), | 121 : url_request_destroyed_callback_(base::Bind(&base::DoNothing)), |
122 all_url_requests_destroyed_callback_(base::Bind(&base::DoNothing)), | 122 all_url_requests_destroyed_callback_(base::Bind(&base::DoNothing)), |
123 num_requests_(0), | 123 num_requests_(0) {} |
124 expect_cookies_(false) {} | |
125 | 124 |
126 void ExpectReport(const std::string& report) { | 125 void ExpectReport(const std::string& report) { |
127 expect_reports_.insert(report); | 126 expect_reports_.insert(report); |
128 } | 127 } |
129 | 128 |
130 void set_all_url_requests_destroyed_callback(const base::Closure& callback) { | 129 void set_all_url_requests_destroyed_callback(const base::Closure& callback) { |
131 all_url_requests_destroyed_callback_ = callback; | 130 all_url_requests_destroyed_callback_ = callback; |
132 } | 131 } |
133 | 132 |
134 void set_url_request_destroyed_callback(const base::Closure& callback) { | 133 void set_url_request_destroyed_callback(const base::Closure& callback) { |
135 url_request_destroyed_callback_ = callback; | 134 url_request_destroyed_callback_ = callback; |
136 } | 135 } |
137 | 136 |
138 void set_expect_url(const GURL& expect_url) { expect_url_ = expect_url; } | 137 void set_expect_url(const GURL& expect_url) { expect_url_ = expect_url; } |
139 | 138 |
140 size_t num_requests() const { return num_requests_; } | 139 size_t num_requests() const { return num_requests_; } |
141 | 140 |
142 // Sets whether cookies are expected to be sent on requests. | |
143 void set_expect_cookies(bool expect_cookies) { | |
144 expect_cookies_ = expect_cookies; | |
145 } | |
146 | |
147 void set_expected_content_type(const std::string& content_type) { | 141 void set_expected_content_type(const std::string& content_type) { |
148 expected_content_type_ = content_type; | 142 expected_content_type_ = content_type; |
149 } | 143 } |
150 | 144 |
151 // NetworkDelegateImpl implementation. | 145 // NetworkDelegateImpl implementation. |
152 int OnBeforeURLRequest(URLRequest* request, | 146 int OnBeforeURLRequest(URLRequest* request, |
153 const CompletionCallback& callback, | 147 const CompletionCallback& callback, |
154 GURL* new_url) override { | 148 GURL* new_url) override { |
155 num_requests_++; | 149 num_requests_++; |
156 EXPECT_EQ(expect_url_, request->url()); | 150 EXPECT_EQ(expect_url_, request->url()); |
157 EXPECT_STRCASEEQ("POST", request->method().data()); | 151 EXPECT_STRCASEEQ("POST", request->method().data()); |
158 | 152 EXPECT_TRUE(request->load_flags() & LOAD_DO_NOT_SEND_COOKIES); |
159 if (expect_cookies_) { | 153 EXPECT_TRUE(request->load_flags() & LOAD_DO_NOT_SAVE_COOKIES); |
160 EXPECT_FALSE(request->load_flags() & LOAD_DO_NOT_SEND_COOKIES); | |
161 EXPECT_FALSE(request->load_flags() & LOAD_DO_NOT_SAVE_COOKIES); | |
162 } else { | |
163 EXPECT_TRUE(request->load_flags() & LOAD_DO_NOT_SEND_COOKIES); | |
164 EXPECT_TRUE(request->load_flags() & LOAD_DO_NOT_SAVE_COOKIES); | |
165 } | |
166 | 154 |
167 const HttpRequestHeaders& extra_headers = request->extra_request_headers(); | 155 const HttpRequestHeaders& extra_headers = request->extra_request_headers(); |
168 std::string content_type; | 156 std::string content_type; |
169 EXPECT_TRUE(extra_headers.GetHeader(HttpRequestHeaders::kContentType, | 157 EXPECT_TRUE(extra_headers.GetHeader(HttpRequestHeaders::kContentType, |
170 &content_type)); | 158 &content_type)); |
171 EXPECT_EQ(expected_content_type_, content_type); | 159 EXPECT_EQ(expected_content_type_, content_type); |
172 | 160 |
173 CheckUploadData(*request, &expect_reports_); | 161 CheckUploadData(*request, &expect_reports_); |
174 | 162 |
175 // Unconditionally return OK, since the sender ignores the results | 163 // Unconditionally return OK, since the sender ignores the results |
176 // anyway. | 164 // anyway. |
177 return OK; | 165 return OK; |
178 } | 166 } |
179 | 167 |
180 void OnURLRequestDestroyed(URLRequest* request) override { | 168 void OnURLRequestDestroyed(URLRequest* request) override { |
181 url_request_destroyed_callback_.Run(); | 169 url_request_destroyed_callback_.Run(); |
182 if (expect_reports_.empty()) | 170 if (expect_reports_.empty()) |
183 all_url_requests_destroyed_callback_.Run(); | 171 all_url_requests_destroyed_callback_.Run(); |
184 } | 172 } |
185 | 173 |
186 private: | 174 private: |
187 base::Closure url_request_destroyed_callback_; | 175 base::Closure url_request_destroyed_callback_; |
188 base::Closure all_url_requests_destroyed_callback_; | 176 base::Closure all_url_requests_destroyed_callback_; |
189 size_t num_requests_; | 177 size_t num_requests_; |
190 GURL expect_url_; | 178 GURL expect_url_; |
191 std::set<std::string> expect_reports_; | 179 std::set<std::string> expect_reports_; |
192 bool expect_cookies_; | |
193 std::string expected_content_type_; | 180 std::string expected_content_type_; |
194 | 181 |
195 DISALLOW_COPY_AND_ASSIGN(TestReportSenderNetworkDelegate); | 182 DISALLOW_COPY_AND_ASSIGN(TestReportSenderNetworkDelegate); |
196 }; | 183 }; |
197 | 184 |
198 class ReportSenderTest : public ::testing::Test { | 185 class ReportSenderTest : public ::testing::Test { |
199 public: | 186 public: |
200 ReportSenderTest() : context_(true) { | 187 ReportSenderTest() : context_(true) { |
201 context_.set_network_delegate(&network_delegate_); | 188 context_.set_network_delegate(&network_delegate_); |
202 context_.Init(); | 189 context_.Init(); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
256 TestReportSenderNetworkDelegate network_delegate_; | 243 TestReportSenderNetworkDelegate network_delegate_; |
257 | 244 |
258 private: | 245 private: |
259 TestURLRequestContext context_; | 246 TestURLRequestContext context_; |
260 }; | 247 }; |
261 | 248 |
262 // Test that ReportSender::Send creates a URLRequest for the | 249 // Test that ReportSender::Send creates a URLRequest for the |
263 // endpoint and sends the expected data. | 250 // endpoint and sends the expected data. |
264 TEST_F(ReportSenderTest, SendsRequest) { | 251 TEST_F(ReportSenderTest, SendsRequest) { |
265 GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); | 252 GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); |
266 ReportSender reporter(context(), ReportSender::DO_NOT_SEND_COOKIES); | 253 ReportSender reporter(context()); |
267 SendReport(&reporter, kDummyReport, url, 0); | 254 SendReport(&reporter, kDummyReport, url, 0); |
268 } | 255 } |
269 | 256 |
270 TEST_F(ReportSenderTest, SendMultipleReportsSequentially) { | 257 TEST_F(ReportSenderTest, SendMultipleReportsSequentially) { |
271 GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); | 258 GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); |
272 ReportSender reporter(context(), ReportSender::DO_NOT_SEND_COOKIES); | 259 ReportSender reporter(context()); |
273 SendReport(&reporter, kDummyReport, url, 0); | 260 SendReport(&reporter, kDummyReport, url, 0); |
274 SendReport(&reporter, kDummyReport, url, 1); | 261 SendReport(&reporter, kDummyReport, url, 1); |
275 } | 262 } |
276 | 263 |
277 TEST_F(ReportSenderTest, SendMultipleReportsSimultaneously) { | 264 TEST_F(ReportSenderTest, SendMultipleReportsSimultaneously) { |
278 base::RunLoop run_loop; | 265 base::RunLoop run_loop; |
279 network_delegate_.set_all_url_requests_destroyed_callback( | 266 network_delegate_.set_all_url_requests_destroyed_callback( |
280 run_loop.QuitClosure()); | 267 run_loop.QuitClosure()); |
281 | 268 |
282 GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); | 269 GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); |
283 network_delegate_.set_expect_url(url); | 270 network_delegate_.set_expect_url(url); |
284 network_delegate_.ExpectReport(kDummyReport); | 271 network_delegate_.ExpectReport(kDummyReport); |
285 network_delegate_.ExpectReport(kSecondDummyReport); | 272 network_delegate_.ExpectReport(kSecondDummyReport); |
286 network_delegate_.set_expected_content_type("application/foobar"); | 273 network_delegate_.set_expected_content_type("application/foobar"); |
287 | 274 |
288 ReportSender reporter(context(), ReportSender::DO_NOT_SEND_COOKIES); | 275 ReportSender reporter(context()); |
289 | 276 |
290 EXPECT_EQ(0u, network_delegate_.num_requests()); | 277 EXPECT_EQ(0u, network_delegate_.num_requests()); |
291 | 278 |
292 reporter.Send(url, "application/foobar", kDummyReport, | 279 reporter.Send(url, "application/foobar", kDummyReport, |
293 base::Callback<void()>(), | 280 base::Callback<void()>(), |
294 base::Callback<void(const GURL&, int, int)>()); | 281 base::Callback<void(const GURL&, int, int)>()); |
295 reporter.Send(url, "application/foobar", kSecondDummyReport, | 282 reporter.Send(url, "application/foobar", kSecondDummyReport, |
296 base::Callback<void()>(), | 283 base::Callback<void()>(), |
297 base::Callback<void(const GURL&, int, int)>()); | 284 base::Callback<void(const GURL&, int, int)>()); |
298 | 285 |
(...skipping 10 matching lines...) Expand all Loading... |
309 &MarkURLRequestDestroyed, base::Unretained(&url_request_destroyed))); | 296 &MarkURLRequestDestroyed, base::Unretained(&url_request_destroyed))); |
310 | 297 |
311 GURL url = URLRequestFailedJob::GetMockHttpUrlWithFailurePhase( | 298 GURL url = URLRequestFailedJob::GetMockHttpUrlWithFailurePhase( |
312 URLRequestFailedJob::START, ERR_IO_PENDING); | 299 URLRequestFailedJob::START, ERR_IO_PENDING); |
313 network_delegate_.set_expect_url(url); | 300 network_delegate_.set_expect_url(url); |
314 network_delegate_.ExpectReport(kDummyReport); | 301 network_delegate_.ExpectReport(kDummyReport); |
315 network_delegate_.set_expected_content_type("application/foobar"); | 302 network_delegate_.set_expected_content_type("application/foobar"); |
316 | 303 |
317 EXPECT_EQ(0u, network_delegate_.num_requests()); | 304 EXPECT_EQ(0u, network_delegate_.num_requests()); |
318 | 305 |
319 std::unique_ptr<ReportSender> reporter( | 306 std::unique_ptr<ReportSender> reporter(new ReportSender(context())); |
320 new ReportSender(context(), ReportSender::DO_NOT_SEND_COOKIES)); | |
321 reporter->Send(url, "application/foobar", kDummyReport, | 307 reporter->Send(url, "application/foobar", kDummyReport, |
322 base::Callback<void()>(), | 308 base::Callback<void()>(), |
323 base::Callback<void(const GURL&, int, int)>()); | 309 base::Callback<void(const GURL&, int, int)>()); |
324 reporter.reset(); | 310 reporter.reset(); |
325 | 311 |
326 EXPECT_EQ(1u, network_delegate_.num_requests()); | 312 EXPECT_EQ(1u, network_delegate_.num_requests()); |
327 EXPECT_TRUE(url_request_destroyed); | 313 EXPECT_TRUE(url_request_destroyed); |
328 } | 314 } |
329 | 315 |
330 // Test that a request that returns an error gets cleaned up. | 316 // Test that a request that returns an error gets cleaned up. |
331 TEST_F(ReportSenderTest, ErroredRequestGetsDeleted) { | 317 TEST_F(ReportSenderTest, ErroredRequestGetsDeleted) { |
332 GURL url = URLRequestFailedJob::GetMockHttpsUrl(ERR_FAILED); | 318 GURL url = URLRequestFailedJob::GetMockHttpsUrl(ERR_FAILED); |
333 ReportSender reporter(context(), ReportSender::DO_NOT_SEND_COOKIES); | 319 ReportSender reporter(context()); |
334 // SendReport will block until the URLRequest is destroyed. | 320 // SendReport will block until the URLRequest is destroyed. |
335 SendReport(&reporter, kDummyReport, url, 0); | 321 SendReport(&reporter, kDummyReport, url, 0); |
336 } | 322 } |
337 | 323 |
338 // Test that the error callback, if provided, gets called when a request | 324 // Test that the error callback, if provided, gets called when a request |
339 // returns an error and the success callback doesn't get called. | 325 // returns an error and the success callback doesn't get called. |
340 TEST_F(ReportSenderTest, ErroredRequestCallsErrorCallback) { | 326 TEST_F(ReportSenderTest, ErroredRequestCallsErrorCallback) { |
341 bool error_callback_called = false; | 327 bool error_callback_called = false; |
342 bool success_callback_called = false; | 328 bool success_callback_called = false; |
343 const GURL url = URLRequestFailedJob::GetMockHttpsUrl(ERR_FAILED); | 329 const GURL url = URLRequestFailedJob::GetMockHttpsUrl(ERR_FAILED); |
344 ReportSender reporter(context(), ReportSender::DO_NOT_SEND_COOKIES); | 330 ReportSender reporter(context()); |
345 // SendReport will block until the URLRequest is destroyed. | 331 // SendReport will block until the URLRequest is destroyed. |
346 SendReport(&reporter, kDummyReport, url, 0, | 332 SendReport(&reporter, kDummyReport, url, 0, |
347 base::Bind(SuccessCallback, &success_callback_called), | 333 base::Bind(SuccessCallback, &success_callback_called), |
348 base::Bind(ErrorCallback, &error_callback_called)); | 334 base::Bind(ErrorCallback, &error_callback_called)); |
349 EXPECT_TRUE(error_callback_called); | 335 EXPECT_TRUE(error_callback_called); |
350 EXPECT_FALSE(success_callback_called); | 336 EXPECT_FALSE(success_callback_called); |
351 } | 337 } |
352 | 338 |
353 // Test that the error callback, if provided, gets called when a request | 339 // 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 | 340 // finishes successfully but results in a server error, and the success callback |
355 // doesn't get called. | 341 // doesn't get called. |
356 TEST_F(ReportSenderTest, BadResponseCodeCallsErrorCallback) { | 342 TEST_F(ReportSenderTest, BadResponseCodeCallsErrorCallback) { |
357 bool error_callback_called = false; | 343 bool error_callback_called = false; |
358 bool success_callback_called = false; | 344 bool success_callback_called = false; |
359 const GURL url(std::string("http://") + kServerErrorHostname); | 345 const GURL url(std::string("http://") + kServerErrorHostname); |
360 ReportSender reporter(context(), ReportSender::DO_NOT_SEND_COOKIES); | 346 ReportSender reporter(context()); |
361 // SendReport will block until the URLRequest is destroyed. | 347 // SendReport will block until the URLRequest is destroyed. |
362 SendReport(&reporter, kDummyReport, url, 0, | 348 SendReport(&reporter, kDummyReport, url, 0, |
363 base::Bind(SuccessCallback, &success_callback_called), | 349 base::Bind(SuccessCallback, &success_callback_called), |
364 base::Bind(ServerErrorResponseCallback, &error_callback_called)); | 350 base::Bind(ServerErrorResponseCallback, &error_callback_called)); |
365 EXPECT_TRUE(error_callback_called); | 351 EXPECT_TRUE(error_callback_called); |
366 EXPECT_FALSE(success_callback_called); | 352 EXPECT_FALSE(success_callback_called); |
367 } | 353 } |
368 | 354 |
369 // Test that the error callback does not get called and the success callback | 355 // Test that the error callback does not get called and the success callback |
370 /// gets called when a request does not return an error. | 356 /// gets called when a request does not return an error. |
371 TEST_F(ReportSenderTest, SuccessfulRequestCallsSuccessCallback) { | 357 TEST_F(ReportSenderTest, SuccessfulRequestCallsSuccessCallback) { |
372 bool error_callback_called = false; | 358 bool error_callback_called = false; |
373 bool success_callback_called = false; | 359 bool success_callback_called = false; |
374 const GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); | 360 const GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); |
375 ReportSender reporter(context(), ReportSender::DO_NOT_SEND_COOKIES); | 361 ReportSender reporter(context()); |
376 SendReport(&reporter, kDummyReport, url, 0, | 362 SendReport(&reporter, kDummyReport, url, 0, |
377 base::Bind(SuccessCallback, &success_callback_called), | 363 base::Bind(SuccessCallback, &success_callback_called), |
378 base::Bind(ErrorCallback, &error_callback_called)); | 364 base::Bind(ErrorCallback, &error_callback_called)); |
379 EXPECT_FALSE(error_callback_called); | 365 EXPECT_FALSE(error_callback_called); |
380 EXPECT_TRUE(success_callback_called); | 366 EXPECT_TRUE(success_callback_called); |
381 } | 367 } |
382 | 368 |
383 // Test that cookies are sent or not sent according to the error | |
384 // reporter's cookies preference. | |
385 | |
386 TEST_F(ReportSenderTest, SendCookiesPreference) { | |
387 GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); | |
388 ReportSender reporter(context(), ReportSender::SEND_COOKIES); | |
389 | |
390 network_delegate_.set_expect_cookies(true); | |
391 SendReport(&reporter, kDummyReport, url, 0); | |
392 } | |
393 | |
394 TEST_F(ReportSenderTest, DoNotSendCookiesPreference) { | |
395 GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); | |
396 ReportSender reporter(context(), ReportSender::DO_NOT_SEND_COOKIES); | |
397 | |
398 network_delegate_.set_expect_cookies(false); | |
399 SendReport(&reporter, kDummyReport, url, 0); | |
400 } | |
401 | |
402 } // namespace | 369 } // namespace |
403 } // namespace net | 370 } // namespace net |
OLD | NEW |