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

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

Issue 2851493003: Remove unused CookiePreferences from report sender and never send cookies (Closed)
Patch Set: Fix iOS build Created 3 years, 7 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
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"
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
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()
68 : url_request_destroyed_callback_(base::Bind(&base::DoNothing)), 68 : url_request_destroyed_callback_(base::Bind(&base::DoNothing)),
69 all_url_requests_destroyed_callback_(base::Bind(&base::DoNothing)), 69 all_url_requests_destroyed_callback_(base::Bind(&base::DoNothing)),
70 num_requests_(0), 70 num_requests_(0) {}
71 expect_cookies_(false) {}
72 71
73 void ExpectReport(const std::string& report) { 72 void ExpectReport(const std::string& report) {
74 expect_reports_.insert(report); 73 expect_reports_.insert(report);
75 } 74 }
76 75
77 void set_all_url_requests_destroyed_callback(const base::Closure& callback) { 76 void set_all_url_requests_destroyed_callback(const base::Closure& callback) {
78 all_url_requests_destroyed_callback_ = callback; 77 all_url_requests_destroyed_callback_ = callback;
79 } 78 }
80 79
81 void set_url_request_destroyed_callback(const base::Closure& callback) { 80 void set_url_request_destroyed_callback(const base::Closure& callback) {
82 url_request_destroyed_callback_ = callback; 81 url_request_destroyed_callback_ = callback;
83 } 82 }
84 83
85 void set_expect_url(const GURL& expect_url) { expect_url_ = expect_url; } 84 void set_expect_url(const GURL& expect_url) { expect_url_ = expect_url; }
86 85
87 size_t num_requests() const { return num_requests_; } 86 size_t num_requests() const { return num_requests_; }
88 87
89 // Sets whether cookies are expected to be sent on requests.
90 void set_expect_cookies(bool expect_cookies) {
91 expect_cookies_ = expect_cookies;
92 }
93
94 void set_expected_content_type(const std::string& content_type) { 88 void set_expected_content_type(const std::string& content_type) {
95 expected_content_type_ = content_type; 89 expected_content_type_ = content_type;
96 } 90 }
97 91
98 // NetworkDelegateImpl implementation. 92 // NetworkDelegateImpl implementation.
99 int OnBeforeURLRequest(URLRequest* request, 93 int OnBeforeURLRequest(URLRequest* request,
100 const CompletionCallback& callback, 94 const CompletionCallback& callback,
101 GURL* new_url) override { 95 GURL* new_url) override {
102 num_requests_++; 96 num_requests_++;
103 EXPECT_EQ(expect_url_, request->url()); 97 EXPECT_EQ(expect_url_, request->url());
104 EXPECT_STRCASEEQ("POST", request->method().data()); 98 EXPECT_STRCASEEQ("POST", request->method().data());
105 99 EXPECT_TRUE(request->load_flags() & LOAD_DO_NOT_SEND_COOKIES);
106 if (expect_cookies_) { 100 EXPECT_TRUE(request->load_flags() & LOAD_DO_NOT_SAVE_COOKIES);
107 EXPECT_FALSE(request->load_flags() & LOAD_DO_NOT_SEND_COOKIES);
108 EXPECT_FALSE(request->load_flags() & LOAD_DO_NOT_SAVE_COOKIES);
109 } else {
110 EXPECT_TRUE(request->load_flags() & LOAD_DO_NOT_SEND_COOKIES);
111 EXPECT_TRUE(request->load_flags() & LOAD_DO_NOT_SAVE_COOKIES);
112 }
113 101
114 const HttpRequestHeaders& extra_headers = request->extra_request_headers(); 102 const HttpRequestHeaders& extra_headers = request->extra_request_headers();
115 std::string content_type; 103 std::string content_type;
116 EXPECT_TRUE(extra_headers.GetHeader(HttpRequestHeaders::kContentType, 104 EXPECT_TRUE(extra_headers.GetHeader(HttpRequestHeaders::kContentType,
117 &content_type)); 105 &content_type));
118 EXPECT_EQ(expected_content_type_, content_type); 106 EXPECT_EQ(expected_content_type_, content_type);
119 107
120 CheckUploadData(*request, &expect_reports_); 108 CheckUploadData(*request, &expect_reports_);
121 109
122 // Unconditionally return OK, since the sender ignores the results 110 // Unconditionally return OK, since the sender ignores the results
123 // anyway. 111 // anyway.
124 return OK; 112 return OK;
125 } 113 }
126 114
127 void OnURLRequestDestroyed(URLRequest* request) override { 115 void OnURLRequestDestroyed(URLRequest* request) override {
128 url_request_destroyed_callback_.Run(); 116 url_request_destroyed_callback_.Run();
129 if (expect_reports_.empty()) 117 if (expect_reports_.empty())
130 all_url_requests_destroyed_callback_.Run(); 118 all_url_requests_destroyed_callback_.Run();
131 } 119 }
132 120
133 private: 121 private:
134 base::Closure url_request_destroyed_callback_; 122 base::Closure url_request_destroyed_callback_;
135 base::Closure all_url_requests_destroyed_callback_; 123 base::Closure all_url_requests_destroyed_callback_;
136 size_t num_requests_; 124 size_t num_requests_;
137 GURL expect_url_; 125 GURL expect_url_;
138 std::set<std::string> expect_reports_; 126 std::set<std::string> expect_reports_;
139 bool expect_cookies_;
140 std::string expected_content_type_; 127 std::string expected_content_type_;
141 128
142 DISALLOW_COPY_AND_ASSIGN(TestReportSenderNetworkDelegate); 129 DISALLOW_COPY_AND_ASSIGN(TestReportSenderNetworkDelegate);
143 }; 130 };
144 131
145 class ReportSenderTest : public ::testing::Test { 132 class ReportSenderTest : public ::testing::Test {
146 public: 133 public:
147 ReportSenderTest() : context_(true) { 134 ReportSenderTest() : context_(true) {
148 context_.set_network_delegate(&network_delegate_); 135 context_.set_network_delegate(&network_delegate_);
149 context_.Init(); 136 context_.Init();
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
198 TestReportSenderNetworkDelegate network_delegate_; 185 TestReportSenderNetworkDelegate network_delegate_;
199 186
200 private: 187 private:
201 TestURLRequestContext context_; 188 TestURLRequestContext context_;
202 }; 189 };
203 190
204 // Test that ReportSender::Send creates a URLRequest for the 191 // Test that ReportSender::Send creates a URLRequest for the
205 // endpoint and sends the expected data. 192 // endpoint and sends the expected data.
206 TEST_F(ReportSenderTest, SendsRequest) { 193 TEST_F(ReportSenderTest, SendsRequest) {
207 GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); 194 GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1);
208 ReportSender reporter(context(), ReportSender::DO_NOT_SEND_COOKIES); 195 ReportSender reporter(context());
209 SendReport(&reporter, kDummyReport, url, 0); 196 SendReport(&reporter, kDummyReport, url, 0);
210 } 197 }
211 198
212 TEST_F(ReportSenderTest, SendMultipleReportsSequentially) { 199 TEST_F(ReportSenderTest, SendMultipleReportsSequentially) {
213 GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); 200 GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1);
214 ReportSender reporter(context(), ReportSender::DO_NOT_SEND_COOKIES); 201 ReportSender reporter(context());
215 SendReport(&reporter, kDummyReport, url, 0); 202 SendReport(&reporter, kDummyReport, url, 0);
216 SendReport(&reporter, kDummyReport, url, 1); 203 SendReport(&reporter, kDummyReport, url, 1);
217 } 204 }
218 205
219 TEST_F(ReportSenderTest, SendMultipleReportsSimultaneously) { 206 TEST_F(ReportSenderTest, SendMultipleReportsSimultaneously) {
220 base::RunLoop run_loop; 207 base::RunLoop run_loop;
221 network_delegate_.set_all_url_requests_destroyed_callback( 208 network_delegate_.set_all_url_requests_destroyed_callback(
222 run_loop.QuitClosure()); 209 run_loop.QuitClosure());
223 210
224 GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); 211 GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1);
225 network_delegate_.set_expect_url(url); 212 network_delegate_.set_expect_url(url);
226 network_delegate_.ExpectReport(kDummyReport); 213 network_delegate_.ExpectReport(kDummyReport);
227 network_delegate_.ExpectReport(kSecondDummyReport); 214 network_delegate_.ExpectReport(kSecondDummyReport);
228 network_delegate_.set_expected_content_type("application/foobar"); 215 network_delegate_.set_expected_content_type("application/foobar");
229 216
230 ReportSender reporter(context(), ReportSender::DO_NOT_SEND_COOKIES); 217 ReportSender reporter(context());
231 218
232 EXPECT_EQ(0u, network_delegate_.num_requests()); 219 EXPECT_EQ(0u, network_delegate_.num_requests());
233 220
234 reporter.Send(url, "application/foobar", kDummyReport, base::Closure(), 221 reporter.Send(url, "application/foobar", kDummyReport, base::Closure(),
235 base::Callback<void(const GURL&, int)>()); 222 base::Callback<void(const GURL&, int)>());
236 reporter.Send(url, "application/foobar", kSecondDummyReport, base::Closure(), 223 reporter.Send(url, "application/foobar", kSecondDummyReport, base::Closure(),
237 base::Callback<void(const GURL&, int)>()); 224 base::Callback<void(const GURL&, int)>());
238 225
239 run_loop.Run(); 226 run_loop.Run();
240 227
241 EXPECT_EQ(2u, network_delegate_.num_requests()); 228 EXPECT_EQ(2u, network_delegate_.num_requests());
242 } 229 }
243 230
244 // Test that pending URLRequests get cleaned up when the report sender 231 // Test that pending URLRequests get cleaned up when the report sender
245 // is deleted. 232 // is deleted.
246 TEST_F(ReportSenderTest, PendingRequestGetsDeleted) { 233 TEST_F(ReportSenderTest, PendingRequestGetsDeleted) {
247 bool url_request_destroyed = false; 234 bool url_request_destroyed = false;
248 network_delegate_.set_url_request_destroyed_callback(base::Bind( 235 network_delegate_.set_url_request_destroyed_callback(base::Bind(
249 &MarkURLRequestDestroyed, base::Unretained(&url_request_destroyed))); 236 &MarkURLRequestDestroyed, base::Unretained(&url_request_destroyed)));
250 237
251 GURL url = URLRequestFailedJob::GetMockHttpUrlWithFailurePhase( 238 GURL url = URLRequestFailedJob::GetMockHttpUrlWithFailurePhase(
252 URLRequestFailedJob::START, ERR_IO_PENDING); 239 URLRequestFailedJob::START, ERR_IO_PENDING);
253 network_delegate_.set_expect_url(url); 240 network_delegate_.set_expect_url(url);
254 network_delegate_.ExpectReport(kDummyReport); 241 network_delegate_.ExpectReport(kDummyReport);
255 network_delegate_.set_expected_content_type("application/foobar"); 242 network_delegate_.set_expected_content_type("application/foobar");
256 243
257 EXPECT_EQ(0u, network_delegate_.num_requests()); 244 EXPECT_EQ(0u, network_delegate_.num_requests());
258 245
259 std::unique_ptr<ReportSender> reporter( 246 std::unique_ptr<ReportSender> reporter(new ReportSender(context()));
260 new ReportSender(context(), ReportSender::DO_NOT_SEND_COOKIES));
261 reporter->Send(url, "application/foobar", kDummyReport, base::Closure(), 247 reporter->Send(url, "application/foobar", kDummyReport, base::Closure(),
262 base::Callback<void(const GURL&, int)>()); 248 base::Callback<void(const GURL&, int)>());
263 reporter.reset(); 249 reporter.reset();
264 250
265 EXPECT_EQ(1u, network_delegate_.num_requests()); 251 EXPECT_EQ(1u, network_delegate_.num_requests());
266 EXPECT_TRUE(url_request_destroyed); 252 EXPECT_TRUE(url_request_destroyed);
267 } 253 }
268 254
269 // Test that a request that returns an error gets cleaned up. 255 // Test that a request that returns an error gets cleaned up.
270 TEST_F(ReportSenderTest, ErroredRequestGetsDeleted) { 256 TEST_F(ReportSenderTest, ErroredRequestGetsDeleted) {
271 GURL url = URLRequestFailedJob::GetMockHttpsUrl(ERR_FAILED); 257 GURL url = URLRequestFailedJob::GetMockHttpsUrl(ERR_FAILED);
272 ReportSender reporter(context(), ReportSender::DO_NOT_SEND_COOKIES); 258 ReportSender reporter(context());
273 // SendReport will block until the URLRequest is destroyed. 259 // SendReport will block until the URLRequest is destroyed.
274 SendReport(&reporter, kDummyReport, url, 0); 260 SendReport(&reporter, kDummyReport, url, 0);
275 } 261 }
276 262
277 // Test that the error callback, if provided, gets called when a request 263 // Test that the error callback, if provided, gets called when a request
278 // returns an error and the success callback doesn't get called. 264 // returns an error and the success callback doesn't get called.
279 TEST_F(ReportSenderTest, ErroredRequestCallsErrorCallback) { 265 TEST_F(ReportSenderTest, ErroredRequestCallsErrorCallback) {
280 bool error_callback_called = false; 266 bool error_callback_called = false;
281 bool success_callback_called = false; 267 bool success_callback_called = false;
282 GURL url = URLRequestFailedJob::GetMockHttpsUrl(ERR_FAILED); 268 GURL url = URLRequestFailedJob::GetMockHttpsUrl(ERR_FAILED);
283 ReportSender reporter(context(), ReportSender::DO_NOT_SEND_COOKIES); 269 ReportSender reporter(context());
284 // SendReport will block until the URLRequest is destroyed. 270 // SendReport will block until the URLRequest is destroyed.
285 SendReport(&reporter, kDummyReport, url, 0, 271 SendReport(&reporter, kDummyReport, url, 0,
286 base::Bind(SuccessCallback, &success_callback_called), 272 base::Bind(SuccessCallback, &success_callback_called),
287 base::Bind(ErrorCallback, &error_callback_called)); 273 base::Bind(ErrorCallback, &error_callback_called));
288 EXPECT_TRUE(error_callback_called); 274 EXPECT_TRUE(error_callback_called);
289 EXPECT_FALSE(success_callback_called); 275 EXPECT_FALSE(success_callback_called);
290 } 276 }
291 277
292 // Test that the error callback does not get called and the success callback 278 // Test that the error callback does not get called and the success callback
293 /// gets called when a request does not return an error. 279 /// gets called when a request does not return an error.
294 TEST_F(ReportSenderTest, SuccessfulRequestCallsSuccessCallback) { 280 TEST_F(ReportSenderTest, SuccessfulRequestCallsSuccessCallback) {
295 bool error_callback_called = false; 281 bool error_callback_called = false;
296 bool success_callback_called = false; 282 bool success_callback_called = false;
297 GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); 283 GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1);
298 ReportSender reporter(context(), ReportSender::DO_NOT_SEND_COOKIES); 284 ReportSender reporter(context());
299 SendReport(&reporter, kDummyReport, url, 0, 285 SendReport(&reporter, kDummyReport, url, 0,
300 base::Bind(SuccessCallback, &success_callback_called), 286 base::Bind(SuccessCallback, &success_callback_called),
301 base::Bind(ErrorCallback, &error_callback_called)); 287 base::Bind(ErrorCallback, &error_callback_called));
302 EXPECT_FALSE(error_callback_called); 288 EXPECT_FALSE(error_callback_called);
303 EXPECT_TRUE(success_callback_called); 289 EXPECT_TRUE(success_callback_called);
304 } 290 }
305 291
306 // Test that cookies are sent or not sent according to the error
307 // reporter's cookies preference.
308
309 TEST_F(ReportSenderTest, SendCookiesPreference) {
310 GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1);
311 ReportSender reporter(context(), ReportSender::SEND_COOKIES);
312
313 network_delegate_.set_expect_cookies(true);
314 SendReport(&reporter, kDummyReport, url, 0);
315 }
316
317 TEST_F(ReportSenderTest, DoNotSendCookiesPreference) {
318 GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1);
319 ReportSender reporter(context(), ReportSender::DO_NOT_SEND_COOKIES);
320
321 network_delegate_.set_expect_cookies(false);
322 SendReport(&reporter, kDummyReport, url, 0);
323 }
324
325 } // namespace 292 } // namespace
326 } // namespace net 293 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698