| 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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 EXPECT_EQ(1u, expect_reports->erase(upload_data)); | 47 EXPECT_EQ(1u, expect_reports->erase(upload_data)); |
| 48 } | 48 } |
| 49 | 49 |
| 50 // Provides an error callback for report sending that sets |called| to | 50 // Provides an error callback for report sending that sets |called| to |
| 51 // true. | 51 // true. |
| 52 void ErrorCallback(bool* called, const GURL& report_uri, int net_error) { | 52 void ErrorCallback(bool* called, const GURL& report_uri, int net_error) { |
| 53 EXPECT_NE(OK, net_error); | 53 EXPECT_NE(OK, net_error); |
| 54 *called = true; | 54 *called = true; |
| 55 } | 55 } |
| 56 | 56 |
| 57 void SuccessCallback(bool* called) { | 57 void SuccessCallback(bool* called, int response_code) { |
| 58 *called = true; | 58 *called = true; |
| 59 } | 59 } |
| 60 | 60 |
| 61 // A network delegate that lets tests check that a report | 61 // A network delegate that lets tests check that a report |
| 62 // was sent. It counts the number of requests and lets tests register a | 62 // 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 | 63 // callback to run when the request is destroyed. It also checks that |
| 64 // the uploaded data is as expected. | 64 // the uploaded data is as expected. |
| 65 class TestReportSenderNetworkDelegate : public NetworkDelegateImpl { | 65 class TestReportSenderNetworkDelegate : public NetworkDelegateImpl { |
| 66 public: | 66 public: |
| 67 TestReportSenderNetworkDelegate() | 67 TestReportSenderNetworkDelegate() |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 void TearDown() override { URLRequestFilter::GetInstance()->ClearHandlers(); } | 157 void TearDown() override { URLRequestFilter::GetInstance()->ClearHandlers(); } |
| 158 | 158 |
| 159 TestURLRequestContext* context() { return &context_; } | 159 TestURLRequestContext* context() { return &context_; } |
| 160 | 160 |
| 161 protected: | 161 protected: |
| 162 void SendReport( | 162 void SendReport( |
| 163 ReportSender* reporter, | 163 ReportSender* reporter, |
| 164 const std::string& report, | 164 const std::string& report, |
| 165 const GURL& url, | 165 const GURL& url, |
| 166 size_t request_sequence_number, | 166 size_t request_sequence_number, |
| 167 const base::Callback<void()>& success_callback, | 167 const base::Callback<void(int)>& success_callback, |
| 168 const base::Callback<void(const GURL&, int)>& error_callback) { | 168 const base::Callback<void(const GURL&, int)>& error_callback) { |
| 169 base::RunLoop run_loop; | 169 base::RunLoop run_loop; |
| 170 network_delegate_.set_url_request_destroyed_callback( | 170 network_delegate_.set_url_request_destroyed_callback( |
| 171 run_loop.QuitClosure()); | 171 run_loop.QuitClosure()); |
| 172 | 172 |
| 173 network_delegate_.set_expect_url(url); | 173 network_delegate_.set_expect_url(url); |
| 174 network_delegate_.ExpectReport(report); | 174 network_delegate_.ExpectReport(report); |
| 175 network_delegate_.set_expected_content_type("application/foobar"); | 175 network_delegate_.set_expected_content_type("application/foobar"); |
| 176 | 176 |
| 177 EXPECT_EQ(request_sequence_number, network_delegate_.num_requests()); | 177 EXPECT_EQ(request_sequence_number, network_delegate_.num_requests()); |
| 178 | 178 |
| 179 reporter->Send(url, "application/foobar", report, success_callback, | 179 reporter->Send(url, "application/foobar", report, success_callback, |
| 180 error_callback); | 180 error_callback); |
| 181 | 181 |
| 182 // The report is sent asynchronously, so wait for the report's | 182 // The report is sent asynchronously, so wait for the report's |
| 183 // URLRequest to be destroyed before checking that the report was | 183 // URLRequest to be destroyed before checking that the report was |
| 184 // sent. | 184 // sent. |
| 185 run_loop.Run(); | 185 run_loop.Run(); |
| 186 | 186 |
| 187 EXPECT_EQ(request_sequence_number + 1, network_delegate_.num_requests()); | 187 EXPECT_EQ(request_sequence_number + 1, network_delegate_.num_requests()); |
| 188 } | 188 } |
| 189 | 189 |
| 190 void SendReport(ReportSender* reporter, | 190 void SendReport(ReportSender* reporter, |
| 191 const std::string& report, | 191 const std::string& report, |
| 192 const GURL& url, | 192 const GURL& url, |
| 193 size_t request_sequence_number) { | 193 size_t request_sequence_number) { |
| 194 SendReport(reporter, report, url, request_sequence_number, base::Closure(), | 194 SendReport(reporter, report, url, request_sequence_number, |
| 195 base::Callback<void(int)>(), |
| 195 base::Callback<void(const GURL&, int)>()); | 196 base::Callback<void(const GURL&, int)>()); |
| 196 } | 197 } |
| 197 | 198 |
| 198 TestReportSenderNetworkDelegate network_delegate_; | 199 TestReportSenderNetworkDelegate network_delegate_; |
| 199 | 200 |
| 200 private: | 201 private: |
| 201 TestURLRequestContext context_; | 202 TestURLRequestContext context_; |
| 202 }; | 203 }; |
| 203 | 204 |
| 204 // Test that ReportSender::Send creates a URLRequest for the | 205 // Test that ReportSender::Send creates a URLRequest for the |
| (...skipping 19 matching lines...) Expand all Loading... |
| 224 GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); | 225 GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); |
| 225 network_delegate_.set_expect_url(url); | 226 network_delegate_.set_expect_url(url); |
| 226 network_delegate_.ExpectReport(kDummyReport); | 227 network_delegate_.ExpectReport(kDummyReport); |
| 227 network_delegate_.ExpectReport(kSecondDummyReport); | 228 network_delegate_.ExpectReport(kSecondDummyReport); |
| 228 network_delegate_.set_expected_content_type("application/foobar"); | 229 network_delegate_.set_expected_content_type("application/foobar"); |
| 229 | 230 |
| 230 ReportSender reporter(context(), ReportSender::DO_NOT_SEND_COOKIES); | 231 ReportSender reporter(context(), ReportSender::DO_NOT_SEND_COOKIES); |
| 231 | 232 |
| 232 EXPECT_EQ(0u, network_delegate_.num_requests()); | 233 EXPECT_EQ(0u, network_delegate_.num_requests()); |
| 233 | 234 |
| 234 reporter.Send(url, "application/foobar", kDummyReport, base::Closure(), | 235 reporter.Send(url, "application/foobar", kDummyReport, |
| 236 base::Callback<void(int)>(), |
| 235 base::Callback<void(const GURL&, int)>()); | 237 base::Callback<void(const GURL&, int)>()); |
| 236 reporter.Send(url, "application/foobar", kSecondDummyReport, base::Closure(), | 238 reporter.Send(url, "application/foobar", kSecondDummyReport, |
| 239 base::Callback<void(int)>(), |
| 237 base::Callback<void(const GURL&, int)>()); | 240 base::Callback<void(const GURL&, int)>()); |
| 238 | 241 |
| 239 run_loop.Run(); | 242 run_loop.Run(); |
| 240 | 243 |
| 241 EXPECT_EQ(2u, network_delegate_.num_requests()); | 244 EXPECT_EQ(2u, network_delegate_.num_requests()); |
| 242 } | 245 } |
| 243 | 246 |
| 244 // Test that pending URLRequests get cleaned up when the report sender | 247 // Test that pending URLRequests get cleaned up when the report sender |
| 245 // is deleted. | 248 // is deleted. |
| 246 TEST_F(ReportSenderTest, PendingRequestGetsDeleted) { | 249 TEST_F(ReportSenderTest, PendingRequestGetsDeleted) { |
| 247 bool url_request_destroyed = false; | 250 bool url_request_destroyed = false; |
| 248 network_delegate_.set_url_request_destroyed_callback(base::Bind( | 251 network_delegate_.set_url_request_destroyed_callback(base::Bind( |
| 249 &MarkURLRequestDestroyed, base::Unretained(&url_request_destroyed))); | 252 &MarkURLRequestDestroyed, base::Unretained(&url_request_destroyed))); |
| 250 | 253 |
| 251 GURL url = URLRequestFailedJob::GetMockHttpUrlWithFailurePhase( | 254 GURL url = URLRequestFailedJob::GetMockHttpUrlWithFailurePhase( |
| 252 URLRequestFailedJob::START, ERR_IO_PENDING); | 255 URLRequestFailedJob::START, ERR_IO_PENDING); |
| 253 network_delegate_.set_expect_url(url); | 256 network_delegate_.set_expect_url(url); |
| 254 network_delegate_.ExpectReport(kDummyReport); | 257 network_delegate_.ExpectReport(kDummyReport); |
| 255 network_delegate_.set_expected_content_type("application/foobar"); | 258 network_delegate_.set_expected_content_type("application/foobar"); |
| 256 | 259 |
| 257 EXPECT_EQ(0u, network_delegate_.num_requests()); | 260 EXPECT_EQ(0u, network_delegate_.num_requests()); |
| 258 | 261 |
| 259 std::unique_ptr<ReportSender> reporter( | 262 std::unique_ptr<ReportSender> reporter( |
| 260 new ReportSender(context(), ReportSender::DO_NOT_SEND_COOKIES)); | 263 new ReportSender(context(), ReportSender::DO_NOT_SEND_COOKIES)); |
| 261 reporter->Send(url, "application/foobar", kDummyReport, base::Closure(), | 264 reporter->Send(url, "application/foobar", kDummyReport, |
| 265 base::Callback<void(int)>(), |
| 262 base::Callback<void(const GURL&, int)>()); | 266 base::Callback<void(const GURL&, int)>()); |
| 263 reporter.reset(); | 267 reporter.reset(); |
| 264 | 268 |
| 265 EXPECT_EQ(1u, network_delegate_.num_requests()); | 269 EXPECT_EQ(1u, network_delegate_.num_requests()); |
| 266 EXPECT_TRUE(url_request_destroyed); | 270 EXPECT_TRUE(url_request_destroyed); |
| 267 } | 271 } |
| 268 | 272 |
| 269 // Test that a request that returns an error gets cleaned up. | 273 // Test that a request that returns an error gets cleaned up. |
| 270 TEST_F(ReportSenderTest, ErroredRequestGetsDeleted) { | 274 TEST_F(ReportSenderTest, ErroredRequestGetsDeleted) { |
| 271 GURL url = URLRequestFailedJob::GetMockHttpsUrl(ERR_FAILED); | 275 GURL url = URLRequestFailedJob::GetMockHttpsUrl(ERR_FAILED); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 317 TEST_F(ReportSenderTest, DoNotSendCookiesPreference) { | 321 TEST_F(ReportSenderTest, DoNotSendCookiesPreference) { |
| 318 GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); | 322 GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); |
| 319 ReportSender reporter(context(), ReportSender::DO_NOT_SEND_COOKIES); | 323 ReportSender reporter(context(), ReportSender::DO_NOT_SEND_COOKIES); |
| 320 | 324 |
| 321 network_delegate_.set_expect_cookies(false); | 325 network_delegate_.set_expect_cookies(false); |
| 322 SendReport(&reporter, kDummyReport, url, 0); | 326 SendReport(&reporter, kDummyReport, url, 0); |
| 323 } | 327 } |
| 324 | 328 |
| 325 } // namespace | 329 } // namespace |
| 326 } // namespace net | 330 } // namespace net |
| OLD | NEW |