[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/url_request/url_request_test_util.h" |
| 6 | |
[email protected] | 529623e | 2011-02-23 17:37:16 | [diff] [blame] | 7 | #include "base/compiler_specific.h" |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 8 | #include "base/logging.h" |
| 9 | #include "base/message_loop.h" |
| 10 | #include "base/threading/thread.h" |
[email protected] | 82b4230 | 2011-04-20 16:28:16 | [diff] [blame] | 11 | #include "net/base/host_port_pair.h" |
[email protected] | 57cb0f7 | 2011-01-28 06:33:58 | [diff] [blame] | 12 | #include "net/http/http_network_session.h" |
[email protected] | a8c1e745 | 2011-05-14 06:17:07 | [diff] [blame] | 13 | #include "net/url_request/url_request_job_factory.h" |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 14 | |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 15 | namespace { |
| 16 | |
| 17 | // These constants put the net::NetworkDelegate events of TestNetworkDelegate |
| 18 | // into an order. They are used in conjunction with |
| 19 | // |TestNetworkDelegate::next_states_| to check that we do not send |
| 20 | // events in the wrong order. |
| 21 | const int kStageBeforeURLRequest = 1 << 0; |
| 22 | const int kStageBeforeSendHeaders = 1 << 1; |
[email protected] | 5796dc94 | 2011-07-14 19:26:10 | [diff] [blame^] | 23 | const int kStageSendHeaders = 1 << 2; |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 24 | const int kStageBeforeRedirect = 1 << 3; |
| 25 | const int kStageResponseStarted = 1 << 4; |
| 26 | const int kStageCompletedSuccess = 1 << 5; |
| 27 | const int kStageCompletedError = 1 << 6; |
| 28 | const int kStageURLRequestDestroyed = 1 << 7; |
| 29 | const int kStageDestruction = 1 << 8; |
| 30 | |
| 31 | } // namespace |
| 32 | |
[email protected] | 529623e | 2011-02-23 17:37:16 | [diff] [blame] | 33 | TestURLRequestContext::TestURLRequestContext() |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 34 | : initialized_(false), |
| 35 | ALLOW_THIS_IN_INITIALIZER_LIST(context_storage_(this)) { |
[email protected] | 529623e | 2011-02-23 17:37:16 | [diff] [blame] | 36 | context_storage_.set_host_resolver( |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 37 | net::CreateSystemHostResolver(net::HostResolver::kDefaultParallelism, |
[email protected] | 06ef6d9 | 2011-05-19 04:24:58 | [diff] [blame] | 38 | net::HostResolver::kDefaultRetryAttempts, |
[email protected] | 6e7fd16 | 2011-04-27 18:08:20 | [diff] [blame] | 39 | NULL)); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 40 | SetProxyDirect(); |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 41 | Init(); |
| 42 | } |
| 43 | |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 44 | TestURLRequestContext::TestURLRequestContext(bool delay_initialization) |
| 45 | : initialized_(false), |
| 46 | ALLOW_THIS_IN_INITIALIZER_LIST(context_storage_(this)) { |
[email protected] | 529623e | 2011-02-23 17:37:16 | [diff] [blame] | 47 | context_storage_.set_host_resolver( |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 48 | net::CreateSystemHostResolver(net::HostResolver::kDefaultParallelism, |
[email protected] | 06ef6d9 | 2011-05-19 04:24:58 | [diff] [blame] | 49 | net::HostResolver::kDefaultRetryAttempts, |
[email protected] | 6e7fd16 | 2011-04-27 18:08:20 | [diff] [blame] | 50 | NULL)); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 51 | SetProxyDirect(); |
| 52 | if (!delay_initialization) |
| 53 | Init(); |
| 54 | } |
| 55 | |
| 56 | TestURLRequestContext::TestURLRequestContext(const std::string& proxy) |
| 57 | : initialized_(false), |
| 58 | ALLOW_THIS_IN_INITIALIZER_LIST(context_storage_(this)) { |
| 59 | context_storage_.set_host_resolver( |
| 60 | net::CreateSystemHostResolver(net::HostResolver::kDefaultParallelism, |
| 61 | net::HostResolver::kDefaultRetryAttempts, |
| 62 | NULL)); |
| 63 | SetProxyFromString(proxy); |
| 64 | Init(); |
| 65 | } |
| 66 | |
| 67 | TestURLRequestContext::TestURLRequestContext(const char* proxy) |
| 68 | : initialized_(false), |
| 69 | ALLOW_THIS_IN_INITIALIZER_LIST(context_storage_(this)) { |
| 70 | context_storage_.set_host_resolver( |
| 71 | net::CreateSystemHostResolver(net::HostResolver::kDefaultParallelism, |
| 72 | net::HostResolver::kDefaultRetryAttempts, |
| 73 | NULL)); |
| 74 | SetProxyFromString(proxy); |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 75 | Init(); |
| 76 | } |
| 77 | |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 78 | TestURLRequestContext::TestURLRequestContext(const std::string& proxy, |
[email protected] | 529623e | 2011-02-23 17:37:16 | [diff] [blame] | 79 | net::HostResolver* host_resolver) |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 80 | : initialized_(false), |
| 81 | ALLOW_THIS_IN_INITIALIZER_LIST(context_storage_(this)) { |
[email protected] | 529623e | 2011-02-23 17:37:16 | [diff] [blame] | 82 | context_storage_.set_host_resolver(host_resolver); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 83 | SetProxyFromString(proxy); |
| 84 | Init(); |
| 85 | } |
| 86 | |
| 87 | void TestURLRequestContext::SetProxyFromString(const std::string& proxy) { |
| 88 | DCHECK(!initialized_); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 89 | net::ProxyConfig proxy_config; |
| 90 | proxy_config.proxy_rules().ParseFromString(proxy); |
[email protected] | 529623e | 2011-02-23 17:37:16 | [diff] [blame] | 91 | context_storage_.set_proxy_service( |
| 92 | net::ProxyService::CreateFixed(proxy_config)); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 93 | } |
| 94 | |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 95 | void TestURLRequestContext::SetProxyDirect() { |
| 96 | DCHECK(!initialized_); |
| 97 | context_storage_.set_proxy_service(net::ProxyService::CreateDirect()); |
| 98 | } |
| 99 | |
| 100 | TestURLRequestContext::~TestURLRequestContext() { |
| 101 | DCHECK(initialized_); |
| 102 | } |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 103 | |
| 104 | void TestURLRequestContext::Init() { |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 105 | DCHECK(!initialized_); |
| 106 | initialized_ = true; |
| 107 | if (!cert_verifier()) |
| 108 | context_storage_.set_cert_verifier(new net::CertVerifier); |
| 109 | if (!ftp_transaction_factory()) { |
| 110 | context_storage_.set_ftp_transaction_factory( |
| 111 | new net::FtpNetworkLayer(host_resolver())); |
| 112 | } |
| 113 | if (!ssl_config_service()) |
| 114 | context_storage_.set_ssl_config_service(new net::SSLConfigServiceDefaults); |
| 115 | if (!http_auth_handler_factory()) { |
| 116 | context_storage_.set_http_auth_handler_factory( |
| 117 | net::HttpAuthHandlerFactory::CreateDefault(host_resolver())); |
| 118 | } |
[email protected] | 9e1bdd3 | 2011-02-03 21:48:34 | [diff] [blame] | 119 | net::HttpNetworkSession::Params params; |
[email protected] | f6c21cb | 2011-02-16 19:45:41 | [diff] [blame] | 120 | params.host_resolver = host_resolver(); |
| 121 | params.cert_verifier = cert_verifier(); |
| 122 | params.proxy_service = proxy_service(); |
| 123 | params.ssl_config_service = ssl_config_service(); |
| 124 | params.http_auth_handler_factory = http_auth_handler_factory(); |
| 125 | params.network_delegate = network_delegate(); |
[email protected] | 9e1bdd3 | 2011-02-03 21:48:34 | [diff] [blame] | 126 | |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 127 | if (!http_transaction_factory()) { |
| 128 | context_storage_.set_http_transaction_factory(new net::HttpCache( |
| 129 | new net::HttpNetworkSession(params), |
| 130 | net::HttpCache::DefaultBackend::InMemory(0))); |
| 131 | } |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 132 | // In-memory cookie store. |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 133 | if (!cookie_store()) |
| 134 | context_storage_.set_cookie_store(new net::CookieMonster(NULL, NULL)); |
| 135 | if (accept_language().empty()) |
| 136 | set_accept_language("en-us,fr"); |
| 137 | if (accept_charset().empty()) |
| 138 | set_accept_charset("iso-8859-1,*,utf-8"); |
| 139 | if (!job_factory()) |
| 140 | context_storage_.set_job_factory(new net::URLRequestJobFactory); |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | |
| 144 | TestURLRequest::TestURLRequest(const GURL& url, Delegate* delegate) |
| 145 | : net::URLRequest(url, delegate) { |
| 146 | set_context(new TestURLRequestContext()); |
| 147 | } |
| 148 | |
| 149 | TestURLRequest::~TestURLRequest() {} |
| 150 | |
| 151 | TestDelegate::TestDelegate() |
| 152 | : cancel_in_rr_(false), |
| 153 | cancel_in_rs_(false), |
| 154 | cancel_in_rd_(false), |
| 155 | cancel_in_rd_pending_(false), |
| 156 | cancel_in_getcookiesblocked_(false), |
| 157 | cancel_in_setcookieblocked_(false), |
| 158 | quit_on_complete_(true), |
| 159 | quit_on_redirect_(false), |
| 160 | allow_certificate_errors_(false), |
[email protected] | ed24fad | 2011-05-10 22:44:01 | [diff] [blame] | 161 | cookie_options_bit_mask_(0), |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 162 | response_started_count_(0), |
| 163 | received_bytes_count_(0), |
| 164 | received_redirect_count_(0), |
| 165 | blocked_get_cookies_count_(0), |
| 166 | blocked_set_cookie_count_(0), |
| 167 | set_cookie_count_(0), |
| 168 | received_data_before_response_(false), |
| 169 | request_failed_(false), |
| 170 | have_certificate_errors_(false), |
| 171 | buf_(new net::IOBuffer(kBufferSize)) { |
| 172 | } |
| 173 | |
| 174 | TestDelegate::~TestDelegate() {} |
| 175 | |
| 176 | void TestDelegate::OnReceivedRedirect(net::URLRequest* request, |
| 177 | const GURL& new_url, |
| 178 | bool* defer_redirect) { |
| 179 | received_redirect_count_++; |
| 180 | if (quit_on_redirect_) { |
| 181 | *defer_redirect = true; |
| 182 | MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
| 183 | } else if (cancel_in_rr_) { |
| 184 | request->Cancel(); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | void TestDelegate::OnAuthRequired(net::URLRequest* request, |
| 189 | net::AuthChallengeInfo* auth_info) { |
| 190 | if (!username_.empty() || !password_.empty()) { |
| 191 | request->SetAuth(username_, password_); |
| 192 | } else { |
| 193 | request->CancelAuth(); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | void TestDelegate::OnSSLCertificateError(net::URLRequest* request, |
| 198 | int cert_error, |
| 199 | net::X509Certificate* cert) { |
| 200 | // The caller can control whether it needs all SSL requests to go through, |
| 201 | // independent of any possible errors, or whether it wants SSL errors to |
| 202 | // cancel the request. |
| 203 | have_certificate_errors_ = true; |
| 204 | if (allow_certificate_errors_) |
| 205 | request->ContinueDespiteLastError(); |
| 206 | else |
| 207 | request->Cancel(); |
| 208 | } |
| 209 | |
[email protected] | ed24fad | 2011-05-10 22:44:01 | [diff] [blame] | 210 | bool TestDelegate::CanGetCookies(net::URLRequest* request) { |
| 211 | bool allow = true; |
| 212 | if (cookie_options_bit_mask_ & NO_GET_COOKIES) |
| 213 | allow = false; |
| 214 | |
| 215 | if (!allow) { |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 216 | blocked_get_cookies_count_++; |
| 217 | if (cancel_in_getcookiesblocked_) |
| 218 | request->Cancel(); |
| 219 | } |
[email protected] | ed24fad | 2011-05-10 22:44:01 | [diff] [blame] | 220 | |
| 221 | return allow; |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 222 | } |
| 223 | |
[email protected] | ed24fad | 2011-05-10 22:44:01 | [diff] [blame] | 224 | bool TestDelegate::CanSetCookie(net::URLRequest* request, |
| 225 | const std::string& cookie_line, |
| 226 | net::CookieOptions* options) { |
| 227 | bool allow = true; |
| 228 | if (cookie_options_bit_mask_ & NO_SET_COOKIE) |
| 229 | allow = false; |
| 230 | |
| 231 | if (cookie_options_bit_mask_ & FORCE_SESSION) |
| 232 | options->set_force_session(); |
| 233 | |
| 234 | |
| 235 | if (!allow) { |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 236 | blocked_set_cookie_count_++; |
| 237 | if (cancel_in_setcookieblocked_) |
| 238 | request->Cancel(); |
| 239 | } else { |
| 240 | set_cookie_count_++; |
| 241 | } |
[email protected] | ed24fad | 2011-05-10 22:44:01 | [diff] [blame] | 242 | |
| 243 | return allow; |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | void TestDelegate::OnResponseStarted(net::URLRequest* request) { |
| 247 | // It doesn't make sense for the request to have IO pending at this point. |
| 248 | DCHECK(!request->status().is_io_pending()); |
| 249 | |
| 250 | response_started_count_++; |
| 251 | if (cancel_in_rs_) { |
| 252 | request->Cancel(); |
| 253 | OnResponseCompleted(request); |
| 254 | } else if (!request->status().is_success()) { |
| 255 | DCHECK(request->status().status() == net::URLRequestStatus::FAILED || |
| 256 | request->status().status() == net::URLRequestStatus::CANCELED); |
| 257 | request_failed_ = true; |
| 258 | OnResponseCompleted(request); |
| 259 | } else { |
| 260 | // Initiate the first read. |
| 261 | int bytes_read = 0; |
| 262 | if (request->Read(buf_, kBufferSize, &bytes_read)) |
| 263 | OnReadCompleted(request, bytes_read); |
| 264 | else if (!request->status().is_io_pending()) |
| 265 | OnResponseCompleted(request); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | void TestDelegate::OnReadCompleted(net::URLRequest* request, int bytes_read) { |
| 270 | // It doesn't make sense for the request to have IO pending at this point. |
| 271 | DCHECK(!request->status().is_io_pending()); |
| 272 | |
| 273 | if (response_started_count_ == 0) |
| 274 | received_data_before_response_ = true; |
| 275 | |
| 276 | if (cancel_in_rd_) |
| 277 | request->Cancel(); |
| 278 | |
| 279 | if (bytes_read >= 0) { |
| 280 | // There is data to read. |
| 281 | received_bytes_count_ += bytes_read; |
| 282 | |
| 283 | // consume the data |
| 284 | data_received_.append(buf_->data(), bytes_read); |
| 285 | } |
| 286 | |
| 287 | // If it was not end of stream, request to read more. |
| 288 | if (request->status().is_success() && bytes_read > 0) { |
| 289 | bytes_read = 0; |
| 290 | while (request->Read(buf_, kBufferSize, &bytes_read)) { |
| 291 | if (bytes_read > 0) { |
| 292 | data_received_.append(buf_->data(), bytes_read); |
| 293 | received_bytes_count_ += bytes_read; |
| 294 | } else { |
| 295 | break; |
| 296 | } |
| 297 | } |
| 298 | } |
| 299 | if (!request->status().is_io_pending()) |
| 300 | OnResponseCompleted(request); |
| 301 | else if (cancel_in_rd_pending_) |
| 302 | request->Cancel(); |
| 303 | } |
| 304 | |
| 305 | void TestDelegate::OnResponseCompleted(net::URLRequest* request) { |
| 306 | if (quit_on_complete_) |
| 307 | MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
| 308 | } |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 309 | |
[email protected] | 0651b81 | 2011-02-24 00:22:50 | [diff] [blame] | 310 | TestNetworkDelegate::TestNetworkDelegate() |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 311 | : last_os_error_(0), |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 312 | error_count_(0), |
| 313 | created_requests_(0), |
[email protected] | a83dd33 | 2011-07-13 10:41:01 | [diff] [blame] | 314 | destroyed_requests_(0), |
| 315 | completed_requests_(0) { |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 316 | } |
| 317 | |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 318 | TestNetworkDelegate::~TestNetworkDelegate() { |
| 319 | for (std::map<int, int>::iterator i = next_states_.begin(); |
| 320 | i != next_states_.end(); ++i) { |
| 321 | event_order_[i->first] += "~TestNetworkDelegate\n"; |
| 322 | EXPECT_TRUE(i->second & kStageDestruction) << event_order_[i->first]; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | void TestNetworkDelegate::InitRequestStatesIfNew(int request_id) { |
| 327 | if (next_states_.find(request_id) == next_states_.end()) { |
| 328 | next_states_[request_id] = kStageBeforeURLRequest; |
| 329 | event_order_[request_id] = ""; |
| 330 | } |
| 331 | } |
| 332 | |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 333 | |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 334 | int TestNetworkDelegate::OnBeforeURLRequest( |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 335 | net::URLRequest* request, |
| 336 | net::CompletionCallback* callback, |
[email protected] | 8523ba5 | 2011-05-22 19:00:58 | [diff] [blame] | 337 | GURL* new_url ) { |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 338 | int req_id = request->identifier(); |
| 339 | event_order_[req_id] += "OnBeforeURLRequest\n"; |
| 340 | InitRequestStatesIfNew(req_id); |
| 341 | EXPECT_TRUE(next_states_[req_id] & kStageBeforeURLRequest) << |
| 342 | event_order_[req_id]; |
| 343 | next_states_[req_id] = |
| 344 | kStageBeforeSendHeaders | |
| 345 | kStageResponseStarted | // data: URLs do not trigger sending headers |
| 346 | kStageBeforeRedirect | // a delegate can trigger a redirection |
| 347 | kStageCompletedError; // request canceled by delegate |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 348 | created_requests_++; |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 349 | return net::OK; |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 350 | } |
| 351 | |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 352 | int TestNetworkDelegate::OnBeforeSendHeaders( |
[email protected] | 636eccd | 2011-06-28 12:28:01 | [diff] [blame] | 353 | net::URLRequest* request, |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 354 | net::CompletionCallback* callback, |
| 355 | net::HttpRequestHeaders* headers) { |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 356 | int req_id = request->identifier(); |
| 357 | event_order_[req_id] += "OnBeforeSendHeaders\n"; |
| 358 | InitRequestStatesIfNew(req_id); |
| 359 | EXPECT_TRUE(next_states_[req_id] & kStageBeforeSendHeaders) << |
| 360 | event_order_[req_id]; |
| 361 | next_states_[req_id] = |
[email protected] | 5796dc94 | 2011-07-14 19:26:10 | [diff] [blame^] | 362 | kStageSendHeaders | |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 363 | kStageCompletedError; // request canceled by delegate |
| 364 | |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 365 | return net::OK; |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 366 | } |
| 367 | |
[email protected] | 5796dc94 | 2011-07-14 19:26:10 | [diff] [blame^] | 368 | void TestNetworkDelegate::OnSendHeaders( |
| 369 | net::URLRequest* request, |
[email protected] | 783573b | 2011-05-13 11:05:15 | [diff] [blame] | 370 | const net::HttpRequestHeaders& headers) { |
[email protected] | 5796dc94 | 2011-07-14 19:26:10 | [diff] [blame^] | 371 | int req_id = request->identifier(); |
| 372 | event_order_[req_id] += "OnSendHeaders\n"; |
| 373 | InitRequestStatesIfNew(req_id); |
| 374 | EXPECT_TRUE(next_states_[req_id] & kStageSendHeaders) << |
| 375 | event_order_[req_id]; |
| 376 | next_states_[req_id] = |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 377 | kStageBeforeRedirect | |
| 378 | kStageResponseStarted | |
| 379 | kStageCompletedError; // e.g. proxy resolution problem |
| 380 | |
| 381 | // Basic authentication sends a second request from the URLRequestHttpJob |
| 382 | // layer before the URLRequest reports that a response has started. |
[email protected] | 5796dc94 | 2011-07-14 19:26:10 | [diff] [blame^] | 383 | next_states_[req_id] |= kStageBeforeSendHeaders; |
[email protected] | 82b4230 | 2011-04-20 16:28:16 | [diff] [blame] | 384 | } |
| 385 | |
[email protected] | 31b2e5f | 2011-04-20 16:58:32 | [diff] [blame] | 386 | void TestNetworkDelegate::OnBeforeRedirect(net::URLRequest* request, |
| 387 | const GURL& new_location) { |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 388 | int req_id = request->identifier(); |
| 389 | event_order_[req_id] += "OnBeforeRedirect\n"; |
| 390 | InitRequestStatesIfNew(req_id); |
| 391 | EXPECT_TRUE(next_states_[req_id] & kStageBeforeRedirect) << |
| 392 | event_order_[req_id]; |
| 393 | next_states_[req_id] = |
| 394 | kStageBeforeURLRequest | // HTTP redirects trigger this. |
| 395 | kStageBeforeSendHeaders | // Redirects from the network delegate do not |
| 396 | // trigger onBeforeURLRequest. |
| 397 | kStageCompletedError; |
| 398 | |
| 399 | // A redirect can lead to a file or a data URL. In this case, we do not send |
| 400 | // headers. |
| 401 | next_states_[req_id] |= kStageResponseStarted; |
[email protected] | 31b2e5f | 2011-04-20 16:58:32 | [diff] [blame] | 402 | } |
| 403 | |
[email protected] | 0651b81 | 2011-02-24 00:22:50 | [diff] [blame] | 404 | void TestNetworkDelegate::OnResponseStarted(net::URLRequest* request) { |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 405 | int req_id = request->identifier(); |
| 406 | event_order_[req_id] += "OnResponseStarted\n"; |
| 407 | InitRequestStatesIfNew(req_id); |
| 408 | EXPECT_TRUE(next_states_[req_id] & kStageResponseStarted) << |
| 409 | event_order_[req_id]; |
| 410 | next_states_[req_id] = kStageCompletedSuccess | kStageCompletedError; |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 411 | if (request->status().status() == net::URLRequestStatus::FAILED) { |
| 412 | error_count_++; |
| 413 | last_os_error_ = request->status().os_error(); |
| 414 | } |
| 415 | } |
[email protected] | 0651b81 | 2011-02-24 00:22:50 | [diff] [blame] | 416 | |
[email protected] | 8523ba5 | 2011-05-22 19:00:58 | [diff] [blame] | 417 | void TestNetworkDelegate::OnRawBytesRead(const net::URLRequest& request, |
| 418 | int bytes_read) { |
| 419 | } |
| 420 | |
[email protected] | 4894438 | 2011-04-23 13:28:16 | [diff] [blame] | 421 | void TestNetworkDelegate::OnCompleted(net::URLRequest* request) { |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 422 | int req_id = request->identifier(); |
| 423 | event_order_[req_id] += "OnCompleted\n"; |
| 424 | InitRequestStatesIfNew(req_id); |
| 425 | // Expect "Success -> (next_states_ & kStageCompletedSuccess)" |
| 426 | // is logically identical to |
| 427 | // Expect "!(Success) || (next_states_ & kStageCompletedSuccess)" |
| 428 | EXPECT_TRUE(!request->status().is_success() || |
| 429 | (next_states_[req_id] & kStageCompletedSuccess)) << |
| 430 | event_order_[req_id]; |
| 431 | EXPECT_TRUE(request->status().is_success() || |
| 432 | (next_states_[req_id] & kStageCompletedError)) << |
| 433 | event_order_[req_id]; |
| 434 | next_states_[req_id] = kStageURLRequestDestroyed; |
[email protected] | a83dd33 | 2011-07-13 10:41:01 | [diff] [blame] | 435 | completed_requests_++; |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 436 | if (request->status().status() == net::URLRequestStatus::FAILED) { |
| 437 | error_count_++; |
| 438 | last_os_error_ = request->status().os_error(); |
| 439 | } |
| 440 | } |
[email protected] | 4b50cb5 | 2011-03-10 00:29:37 | [diff] [blame] | 441 | |
[email protected] | 8523ba5 | 2011-05-22 19:00:58 | [diff] [blame] | 442 | void TestNetworkDelegate::OnURLRequestDestroyed( |
| 443 | net::URLRequest* request) { |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 444 | int req_id = request->identifier(); |
| 445 | event_order_[req_id] += "OnURLRequestDestroyed\n"; |
| 446 | InitRequestStatesIfNew(req_id); |
| 447 | EXPECT_TRUE(next_states_[req_id] & kStageURLRequestDestroyed) << |
| 448 | event_order_[req_id]; |
| 449 | next_states_[req_id] = kStageDestruction; |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 450 | destroyed_requests_++; |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 451 | } |
| 452 | |
[email protected] | 5aa2013 | 2011-04-27 23:11:34 | [diff] [blame] | 453 | void TestNetworkDelegate::OnHttpTransactionDestroyed(uint64 request_id) { |
| 454 | } |
| 455 | |
[email protected] | 4b50cb5 | 2011-03-10 00:29:37 | [diff] [blame] | 456 | net::URLRequestJob* TestNetworkDelegate::OnMaybeCreateURLRequestJob( |
| 457 | net::URLRequest* request) { |
| 458 | return NULL; |
| 459 | } |
[email protected] | 82a3767 | 2011-05-03 12:02:41 | [diff] [blame] | 460 | |
| 461 | void TestNetworkDelegate::OnPACScriptError(int line_number, |
| 462 | const string16& error) { |
| 463 | } |