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