OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/url_request_test_util.h" | 5 #include "net/url_request/url_request_test_util.h" |
6 | 6 |
7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/threading/thread.h" | 10 #include "base/threading/thread.h" |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
184 } | 184 } |
185 | 185 |
186 TestDelegate::TestDelegate() | 186 TestDelegate::TestDelegate() |
187 : cancel_in_rr_(false), | 187 : cancel_in_rr_(false), |
188 cancel_in_rs_(false), | 188 cancel_in_rs_(false), |
189 cancel_in_rd_(false), | 189 cancel_in_rd_(false), |
190 cancel_in_rd_pending_(false), | 190 cancel_in_rd_pending_(false), |
191 quit_on_complete_(true), | 191 quit_on_complete_(true), |
192 quit_on_redirect_(false), | 192 quit_on_redirect_(false), |
193 allow_certificate_errors_(false), | 193 allow_certificate_errors_(false), |
194 cookie_options_bit_mask_(0), | |
195 response_started_count_(0), | 194 response_started_count_(0), |
196 received_bytes_count_(0), | 195 received_bytes_count_(0), |
197 received_redirect_count_(0), | 196 received_redirect_count_(0), |
198 blocked_get_cookies_count_(0), | |
199 blocked_set_cookie_count_(0), | |
200 set_cookie_count_(0), | |
201 received_data_before_response_(false), | 197 received_data_before_response_(false), |
202 request_failed_(false), | 198 request_failed_(false), |
203 have_certificate_errors_(false), | 199 have_certificate_errors_(false), |
204 certificate_errors_are_fatal_(false), | 200 certificate_errors_are_fatal_(false), |
205 auth_required_(false), | 201 auth_required_(false), |
206 buf_(new net::IOBuffer(kBufferSize)) { | 202 buf_(new net::IOBuffer(kBufferSize)) { |
207 } | 203 } |
208 | 204 |
209 TestDelegate::~TestDelegate() {} | 205 TestDelegate::~TestDelegate() {} |
210 | 206 |
(...skipping 26 matching lines...) Expand all Loading... |
237 // independent of any possible errors, or whether it wants SSL errors to | 233 // independent of any possible errors, or whether it wants SSL errors to |
238 // cancel the request. | 234 // cancel the request. |
239 have_certificate_errors_ = true; | 235 have_certificate_errors_ = true; |
240 certificate_errors_are_fatal_ = fatal; | 236 certificate_errors_are_fatal_ = fatal; |
241 if (allow_certificate_errors_) | 237 if (allow_certificate_errors_) |
242 request->ContinueDespiteLastError(); | 238 request->ContinueDespiteLastError(); |
243 else | 239 else |
244 request->Cancel(); | 240 request->Cancel(); |
245 } | 241 } |
246 | 242 |
247 bool TestDelegate::CanGetCookies(const net::URLRequest* request, | |
248 const net::CookieList& cookie_list) const { | |
249 bool allow = true; | |
250 if (cookie_options_bit_mask_ & NO_GET_COOKIES) | |
251 allow = false; | |
252 | |
253 if (!allow) { | |
254 blocked_get_cookies_count_++; | |
255 } | |
256 | |
257 return allow; | |
258 } | |
259 | |
260 bool TestDelegate::CanSetCookie(const net::URLRequest* request, | |
261 const std::string& cookie_line, | |
262 net::CookieOptions* options) const { | |
263 bool allow = true; | |
264 if (cookie_options_bit_mask_ & NO_SET_COOKIE) | |
265 allow = false; | |
266 | |
267 if (cookie_options_bit_mask_ & FORCE_SESSION) | |
268 options->set_force_session(); | |
269 | |
270 | |
271 if (!allow) { | |
272 blocked_set_cookie_count_++; | |
273 } else { | |
274 set_cookie_count_++; | |
275 } | |
276 | |
277 return allow; | |
278 } | |
279 | |
280 void TestDelegate::OnResponseStarted(net::URLRequest* request) { | 243 void TestDelegate::OnResponseStarted(net::URLRequest* request) { |
281 // It doesn't make sense for the request to have IO pending at this point. | 244 // It doesn't make sense for the request to have IO pending at this point. |
282 DCHECK(!request->status().is_io_pending()); | 245 DCHECK(!request->status().is_io_pending()); |
283 | 246 |
284 response_started_count_++; | 247 response_started_count_++; |
285 if (cancel_in_rs_) { | 248 if (cancel_in_rs_) { |
286 request->Cancel(); | 249 request->Cancel(); |
287 OnResponseCompleted(request); | 250 OnResponseCompleted(request); |
288 } else if (!request->status().is_success()) { | 251 } else if (!request->status().is_success()) { |
289 DCHECK(request->status().status() == net::URLRequestStatus::FAILED || | 252 DCHECK(request->status().status() == net::URLRequestStatus::FAILED || |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
335 else if (cancel_in_rd_pending_) | 298 else if (cancel_in_rd_pending_) |
336 request->Cancel(); | 299 request->Cancel(); |
337 } | 300 } |
338 | 301 |
339 void TestDelegate::OnResponseCompleted(net::URLRequest* request) { | 302 void TestDelegate::OnResponseCompleted(net::URLRequest* request) { |
340 if (quit_on_complete_) | 303 if (quit_on_complete_) |
341 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 304 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
342 } | 305 } |
343 | 306 |
344 TestNetworkDelegate::TestNetworkDelegate() | 307 TestNetworkDelegate::TestNetworkDelegate() |
345 : last_error_(0), | 308 : last_error_(0), |
346 error_count_(0), | 309 error_count_(0), |
347 created_requests_(0), | 310 created_requests_(0), |
348 destroyed_requests_(0), | 311 destroyed_requests_(0), |
349 completed_requests_(0) { | 312 completed_requests_(0), |
| 313 cookie_options_bit_mask_(0), |
| 314 blocked_get_cookies_count_(0), |
| 315 blocked_set_cookie_count_(0), |
| 316 set_cookie_count_(0) { |
350 } | 317 } |
351 | 318 |
352 TestNetworkDelegate::~TestNetworkDelegate() { | 319 TestNetworkDelegate::~TestNetworkDelegate() { |
353 for (std::map<int, int>::iterator i = next_states_.begin(); | 320 for (std::map<int, int>::iterator i = next_states_.begin(); |
354 i != next_states_.end(); ++i) { | 321 i != next_states_.end(); ++i) { |
355 event_order_[i->first] += "~TestNetworkDelegate\n"; | 322 event_order_[i->first] += "~TestNetworkDelegate\n"; |
356 EXPECT_TRUE(i->second & kStageDestruction) << event_order_[i->first]; | 323 EXPECT_TRUE(i->second & kStageDestruction) << event_order_[i->first]; |
357 } | 324 } |
358 } | 325 } |
359 | 326 |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
517 EXPECT_TRUE(next_states_[req_id] & kStageAuthRequired) << | 484 EXPECT_TRUE(next_states_[req_id] & kStageAuthRequired) << |
518 event_order_[req_id]; | 485 event_order_[req_id]; |
519 next_states_[req_id] = kStageBeforeSendHeaders | | 486 next_states_[req_id] = kStageBeforeSendHeaders | |
520 kStageHeadersReceived | // Request canceled by delegate simulates empty | 487 kStageHeadersReceived | // Request canceled by delegate simulates empty |
521 // response. | 488 // response. |
522 kStageResponseStarted | // data: URLs do not trigger sending headers | 489 kStageResponseStarted | // data: URLs do not trigger sending headers |
523 kStageBeforeRedirect; // a delegate can trigger a redirection | 490 kStageBeforeRedirect; // a delegate can trigger a redirection |
524 return net::NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION; | 491 return net::NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION; |
525 } | 492 } |
526 | 493 |
| 494 bool TestNetworkDelegate::CanGetCookies(const net::URLRequest* request, |
| 495 const net::CookieList& cookie_list) { |
| 496 bool allow = true; |
| 497 if (cookie_options_bit_mask_ & NO_GET_COOKIES) |
| 498 allow = false; |
| 499 |
| 500 if (!allow) { |
| 501 blocked_get_cookies_count_++; |
| 502 } |
| 503 |
| 504 return allow; |
| 505 } |
| 506 |
| 507 bool TestNetworkDelegate::CanSetCookie(const net::URLRequest* request, |
| 508 const std::string& cookie_line, |
| 509 net::CookieOptions* options) { |
| 510 bool allow = true; |
| 511 if (cookie_options_bit_mask_ & NO_SET_COOKIE) |
| 512 allow = false; |
| 513 |
| 514 if (cookie_options_bit_mask_ & FORCE_SESSION) |
| 515 options->set_force_session(); |
| 516 |
| 517 if (!allow) { |
| 518 blocked_set_cookie_count_++; |
| 519 } else { |
| 520 set_cookie_count_++; |
| 521 } |
| 522 |
| 523 return allow; |
| 524 } |
| 525 |
527 // static | 526 // static |
528 std::string ScopedCustomUrlRequestTestHttpHost::value_("127.0.0.1"); | 527 std::string ScopedCustomUrlRequestTestHttpHost::value_("127.0.0.1"); |
529 | 528 |
530 ScopedCustomUrlRequestTestHttpHost::ScopedCustomUrlRequestTestHttpHost( | 529 ScopedCustomUrlRequestTestHttpHost::ScopedCustomUrlRequestTestHttpHost( |
531 const std::string& new_value) | 530 const std::string& new_value) |
532 : old_value_(value_), | 531 : old_value_(value_), |
533 new_value_(new_value) { | 532 new_value_(new_value) { |
534 value_ = new_value_; | 533 value_ = new_value_; |
535 } | 534 } |
536 | 535 |
537 ScopedCustomUrlRequestTestHttpHost::~ScopedCustomUrlRequestTestHttpHost() { | 536 ScopedCustomUrlRequestTestHttpHost::~ScopedCustomUrlRequestTestHttpHost() { |
538 DCHECK_EQ(value_, new_value_); | 537 DCHECK_EQ(value_, new_value_); |
539 value_ = old_value_; | 538 value_ = old_value_; |
540 } | 539 } |
541 | 540 |
542 // static | 541 // static |
543 const std::string& ScopedCustomUrlRequestTestHttpHost::value() { | 542 const std::string& ScopedCustomUrlRequestTestHttpHost::value() { |
544 return value_; | 543 return value_; |
545 } | 544 } |
OLD | NEW |