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 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 TestURLRequest::~TestURLRequest() {} | 165 TestURLRequest::~TestURLRequest() {} |
166 | 166 |
167 TestDelegate::TestDelegate() | 167 TestDelegate::TestDelegate() |
168 : cancel_in_rr_(false), | 168 : cancel_in_rr_(false), |
169 cancel_in_rs_(false), | 169 cancel_in_rs_(false), |
170 cancel_in_rd_(false), | 170 cancel_in_rd_(false), |
171 cancel_in_rd_pending_(false), | 171 cancel_in_rd_pending_(false), |
172 quit_on_complete_(true), | 172 quit_on_complete_(true), |
173 quit_on_redirect_(false), | 173 quit_on_redirect_(false), |
174 allow_certificate_errors_(false), | 174 allow_certificate_errors_(false), |
175 cookie_options_bit_mask_(0), | |
176 response_started_count_(0), | 175 response_started_count_(0), |
177 received_bytes_count_(0), | 176 received_bytes_count_(0), |
178 received_redirect_count_(0), | 177 received_redirect_count_(0), |
179 blocked_get_cookies_count_(0), | |
180 blocked_set_cookie_count_(0), | |
181 set_cookie_count_(0), | |
182 received_data_before_response_(false), | 178 received_data_before_response_(false), |
183 request_failed_(false), | 179 request_failed_(false), |
184 have_certificate_errors_(false), | 180 have_certificate_errors_(false), |
185 certificate_errors_are_fatal_(false), | 181 certificate_errors_are_fatal_(false), |
186 auth_required_(false), | 182 auth_required_(false), |
187 buf_(new net::IOBuffer(kBufferSize)) { | 183 buf_(new net::IOBuffer(kBufferSize)) { |
188 } | 184 } |
189 | 185 |
190 TestDelegate::~TestDelegate() {} | 186 TestDelegate::~TestDelegate() {} |
191 | 187 |
(...skipping 26 matching lines...) Expand all Loading... |
218 // independent of any possible errors, or whether it wants SSL errors to | 214 // independent of any possible errors, or whether it wants SSL errors to |
219 // cancel the request. | 215 // cancel the request. |
220 have_certificate_errors_ = true; | 216 have_certificate_errors_ = true; |
221 certificate_errors_are_fatal_ = fatal; | 217 certificate_errors_are_fatal_ = fatal; |
222 if (allow_certificate_errors_) | 218 if (allow_certificate_errors_) |
223 request->ContinueDespiteLastError(); | 219 request->ContinueDespiteLastError(); |
224 else | 220 else |
225 request->Cancel(); | 221 request->Cancel(); |
226 } | 222 } |
227 | 223 |
228 bool TestDelegate::CanGetCookies(const net::URLRequest* request, | |
229 const net::CookieList& cookie_list) const { | |
230 bool allow = true; | |
231 if (cookie_options_bit_mask_ & NO_GET_COOKIES) | |
232 allow = false; | |
233 | |
234 if (!allow) { | |
235 blocked_get_cookies_count_++; | |
236 } | |
237 | |
238 return allow; | |
239 } | |
240 | |
241 bool TestDelegate::CanSetCookie(const net::URLRequest* request, | |
242 const std::string& cookie_line, | |
243 net::CookieOptions* options) const { | |
244 bool allow = true; | |
245 if (cookie_options_bit_mask_ & NO_SET_COOKIE) | |
246 allow = false; | |
247 | |
248 if (cookie_options_bit_mask_ & FORCE_SESSION) | |
249 options->set_force_session(); | |
250 | |
251 | |
252 if (!allow) { | |
253 blocked_set_cookie_count_++; | |
254 } else { | |
255 set_cookie_count_++; | |
256 } | |
257 | |
258 return allow; | |
259 } | |
260 | |
261 void TestDelegate::OnResponseStarted(net::URLRequest* request) { | 224 void TestDelegate::OnResponseStarted(net::URLRequest* request) { |
262 // It doesn't make sense for the request to have IO pending at this point. | 225 // It doesn't make sense for the request to have IO pending at this point. |
263 DCHECK(!request->status().is_io_pending()); | 226 DCHECK(!request->status().is_io_pending()); |
264 | 227 |
265 response_started_count_++; | 228 response_started_count_++; |
266 if (cancel_in_rs_) { | 229 if (cancel_in_rs_) { |
267 request->Cancel(); | 230 request->Cancel(); |
268 OnResponseCompleted(request); | 231 OnResponseCompleted(request); |
269 } else if (!request->status().is_success()) { | 232 } else if (!request->status().is_success()) { |
270 DCHECK(request->status().status() == net::URLRequestStatus::FAILED || | 233 DCHECK(request->status().status() == net::URLRequestStatus::FAILED || |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 else if (cancel_in_rd_pending_) | 279 else if (cancel_in_rd_pending_) |
317 request->Cancel(); | 280 request->Cancel(); |
318 } | 281 } |
319 | 282 |
320 void TestDelegate::OnResponseCompleted(net::URLRequest* request) { | 283 void TestDelegate::OnResponseCompleted(net::URLRequest* request) { |
321 if (quit_on_complete_) | 284 if (quit_on_complete_) |
322 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 285 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
323 } | 286 } |
324 | 287 |
325 TestNetworkDelegate::TestNetworkDelegate() | 288 TestNetworkDelegate::TestNetworkDelegate() |
326 : last_error_(0), | 289 : last_error_(0), |
327 error_count_(0), | 290 error_count_(0), |
328 created_requests_(0), | 291 created_requests_(0), |
329 destroyed_requests_(0), | 292 destroyed_requests_(0), |
330 completed_requests_(0) { | 293 completed_requests_(0), |
| 294 cookie_options_bit_mask_(0), |
| 295 blocked_get_cookies_count_(0), |
| 296 blocked_set_cookie_count_(0), |
| 297 set_cookie_count_(0) { |
| 298 } |
| 299 |
| 300 void TestNetworkDelegate::ResetCookieCounters() { |
| 301 blocked_get_cookies_count_ = 0; |
| 302 blocked_set_cookie_count_ = 0; |
| 303 set_cookie_count_ = 0; |
331 } | 304 } |
332 | 305 |
333 TestNetworkDelegate::~TestNetworkDelegate() { | 306 TestNetworkDelegate::~TestNetworkDelegate() { |
334 for (std::map<int, int>::iterator i = next_states_.begin(); | 307 for (std::map<int, int>::iterator i = next_states_.begin(); |
335 i != next_states_.end(); ++i) { | 308 i != next_states_.end(); ++i) { |
336 event_order_[i->first] += "~TestNetworkDelegate\n"; | 309 event_order_[i->first] += "~TestNetworkDelegate\n"; |
337 EXPECT_TRUE(i->second & kStageDestruction) << event_order_[i->first]; | 310 EXPECT_TRUE(i->second & kStageDestruction) << event_order_[i->first]; |
338 } | 311 } |
339 } | 312 } |
340 | 313 |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
498 EXPECT_TRUE(next_states_[req_id] & kStageAuthRequired) << | 471 EXPECT_TRUE(next_states_[req_id] & kStageAuthRequired) << |
499 event_order_[req_id]; | 472 event_order_[req_id]; |
500 next_states_[req_id] = kStageBeforeSendHeaders | | 473 next_states_[req_id] = kStageBeforeSendHeaders | |
501 kStageHeadersReceived | // Request canceled by delegate simulates empty | 474 kStageHeadersReceived | // Request canceled by delegate simulates empty |
502 // response. | 475 // response. |
503 kStageResponseStarted | // data: URLs do not trigger sending headers | 476 kStageResponseStarted | // data: URLs do not trigger sending headers |
504 kStageBeforeRedirect; // a delegate can trigger a redirection | 477 kStageBeforeRedirect; // a delegate can trigger a redirection |
505 return net::NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION; | 478 return net::NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION; |
506 } | 479 } |
507 | 480 |
| 481 bool TestNetworkDelegate::OnReadingCookies(const net::URLRequest* request, |
| 482 const net::CookieList& cookie_list) { |
| 483 bool allow = true; |
| 484 if (cookie_options_bit_mask_ & NO_GET_COOKIES) |
| 485 allow = false; |
| 486 |
| 487 if (!allow) { |
| 488 blocked_get_cookies_count_++; |
| 489 } |
| 490 |
| 491 return allow; |
| 492 } |
| 493 |
| 494 bool TestNetworkDelegate::OnSettingCookie(const net::URLRequest* request, |
| 495 const std::string& cookie_line, |
| 496 net::CookieOptions* options) { |
| 497 bool allow = true; |
| 498 if (cookie_options_bit_mask_ & NO_SET_COOKIE) |
| 499 allow = false; |
| 500 |
| 501 if (cookie_options_bit_mask_ & FORCE_SESSION) |
| 502 options->set_force_session(); |
| 503 |
| 504 |
| 505 if (!allow) { |
| 506 blocked_set_cookie_count_++; |
| 507 } else { |
| 508 set_cookie_count_++; |
| 509 } |
| 510 |
| 511 return allow; |
| 512 } |
| 513 |
508 // static | 514 // static |
509 std::string ScopedCustomUrlRequestTestHttpHost::value_("127.0.0.1"); | 515 std::string ScopedCustomUrlRequestTestHttpHost::value_("127.0.0.1"); |
510 | 516 |
511 ScopedCustomUrlRequestTestHttpHost::ScopedCustomUrlRequestTestHttpHost( | 517 ScopedCustomUrlRequestTestHttpHost::ScopedCustomUrlRequestTestHttpHost( |
512 const std::string& new_value) | 518 const std::string& new_value) |
513 : old_value_(value_), | 519 : old_value_(value_), |
514 new_value_(new_value) { | 520 new_value_(new_value) { |
515 value_ = new_value_; | 521 value_ = new_value_; |
516 } | 522 } |
517 | 523 |
518 ScopedCustomUrlRequestTestHttpHost::~ScopedCustomUrlRequestTestHttpHost() { | 524 ScopedCustomUrlRequestTestHttpHost::~ScopedCustomUrlRequestTestHttpHost() { |
519 DCHECK_EQ(value_, new_value_); | 525 DCHECK_EQ(value_, new_value_); |
520 value_ = old_value_; | 526 value_ = old_value_; |
521 } | 527 } |
522 | 528 |
523 // static | 529 // static |
524 const std::string& ScopedCustomUrlRequestTestHttpHost::value() { | 530 const std::string& ScopedCustomUrlRequestTestHttpHost::value() { |
525 return value_; | 531 return value_; |
526 } | 532 } |
OLD | NEW |