| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #ifndef NET_URL_REQUEST_URL_REQUEST_UNITTEST_H_ | 5 #ifndef NET_URL_REQUEST_URL_REQUEST_UNITTEST_H_ |
| 6 #define NET_URL_REQUEST_URL_REQUEST_UNITTEST_H_ | 6 #define NET_URL_REQUEST_URL_REQUEST_UNITTEST_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <stdlib.h> | 9 #include <stdlib.h> |
| 10 | 10 |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 net::HttpCache::DefaultBackend::InMemory(0)); | 172 net::HttpCache::DefaultBackend::InMemory(0)); |
| 173 // In-memory cookie store. | 173 // In-memory cookie store. |
| 174 cookie_store_ = new net::CookieMonster(NULL, NULL); | 174 cookie_store_ = new net::CookieMonster(NULL, NULL); |
| 175 accept_language_ = "en-us,fr"; | 175 accept_language_ = "en-us,fr"; |
| 176 accept_charset_ = "iso-8859-1,*,utf-8"; | 176 accept_charset_ = "iso-8859-1,*,utf-8"; |
| 177 } | 177 } |
| 178 }; | 178 }; |
| 179 | 179 |
| 180 //----------------------------------------------------------------------------- | 180 //----------------------------------------------------------------------------- |
| 181 | 181 |
| 182 class TestURLRequest : public URLRequest { | 182 class TestURLRequest : public net::URLRequest { |
| 183 public: | 183 public: |
| 184 TestURLRequest(const GURL& url, Delegate* delegate) | 184 TestURLRequest(const GURL& url, Delegate* delegate) |
| 185 : URLRequest(url, delegate) { | 185 : net::URLRequest(url, delegate) { |
| 186 set_context(new TestURLRequestContext()); | 186 set_context(new TestURLRequestContext()); |
| 187 } | 187 } |
| 188 }; | 188 }; |
| 189 | 189 |
| 190 //----------------------------------------------------------------------------- | 190 //----------------------------------------------------------------------------- |
| 191 | 191 |
| 192 class TestDelegate : public URLRequest::Delegate { | 192 class TestDelegate : public net::URLRequest::Delegate { |
| 193 public: | 193 public: |
| 194 TestDelegate() | 194 TestDelegate() |
| 195 : cancel_in_rr_(false), | 195 : cancel_in_rr_(false), |
| 196 cancel_in_rs_(false), | 196 cancel_in_rs_(false), |
| 197 cancel_in_rd_(false), | 197 cancel_in_rd_(false), |
| 198 cancel_in_rd_pending_(false), | 198 cancel_in_rd_pending_(false), |
| 199 cancel_in_getcookiesblocked_(false), | 199 cancel_in_getcookiesblocked_(false), |
| 200 cancel_in_setcookieblocked_(false), | 200 cancel_in_setcookieblocked_(false), |
| 201 quit_on_complete_(true), | 201 quit_on_complete_(true), |
| 202 quit_on_redirect_(false), | 202 quit_on_redirect_(false), |
| 203 allow_certificate_errors_(false), | 203 allow_certificate_errors_(false), |
| 204 response_started_count_(0), | 204 response_started_count_(0), |
| 205 received_bytes_count_(0), | 205 received_bytes_count_(0), |
| 206 received_redirect_count_(0), | 206 received_redirect_count_(0), |
| 207 blocked_get_cookies_count_(0), | 207 blocked_get_cookies_count_(0), |
| 208 blocked_set_cookie_count_(0), | 208 blocked_set_cookie_count_(0), |
| 209 set_cookie_count_(0), | 209 set_cookie_count_(0), |
| 210 received_data_before_response_(false), | 210 received_data_before_response_(false), |
| 211 request_failed_(false), | 211 request_failed_(false), |
| 212 have_certificate_errors_(false), | 212 have_certificate_errors_(false), |
| 213 buf_(new net::IOBuffer(kBufferSize)) { | 213 buf_(new net::IOBuffer(kBufferSize)) { |
| 214 } | 214 } |
| 215 | 215 |
| 216 virtual void OnReceivedRedirect(URLRequest* request, const GURL& new_url, | 216 virtual void OnReceivedRedirect(net::URLRequest* request, const GURL& new_url, |
| 217 bool* defer_redirect) { | 217 bool* defer_redirect) { |
| 218 received_redirect_count_++; | 218 received_redirect_count_++; |
| 219 if (quit_on_redirect_) { | 219 if (quit_on_redirect_) { |
| 220 *defer_redirect = true; | 220 *defer_redirect = true; |
| 221 MessageLoop::current()->Quit(); | 221 MessageLoop::current()->Quit(); |
| 222 } else if (cancel_in_rr_) { | 222 } else if (cancel_in_rr_) { |
| 223 request->Cancel(); | 223 request->Cancel(); |
| 224 } | 224 } |
| 225 } | 225 } |
| 226 | 226 |
| 227 virtual void OnResponseStarted(URLRequest* request) { | 227 virtual void OnResponseStarted(net::URLRequest* request) { |
| 228 // It doesn't make sense for the request to have IO pending at this point. | 228 // It doesn't make sense for the request to have IO pending at this point. |
| 229 DCHECK(!request->status().is_io_pending()); | 229 DCHECK(!request->status().is_io_pending()); |
| 230 | 230 |
| 231 response_started_count_++; | 231 response_started_count_++; |
| 232 if (cancel_in_rs_) { | 232 if (cancel_in_rs_) { |
| 233 request->Cancel(); | 233 request->Cancel(); |
| 234 OnResponseCompleted(request); | 234 OnResponseCompleted(request); |
| 235 } else if (!request->status().is_success()) { | 235 } else if (!request->status().is_success()) { |
| 236 DCHECK(request->status().status() == URLRequestStatus::FAILED || | 236 DCHECK(request->status().status() == URLRequestStatus::FAILED || |
| 237 request->status().status() == URLRequestStatus::CANCELED); | 237 request->status().status() == URLRequestStatus::CANCELED); |
| 238 request_failed_ = true; | 238 request_failed_ = true; |
| 239 OnResponseCompleted(request); | 239 OnResponseCompleted(request); |
| 240 } else { | 240 } else { |
| 241 // Initiate the first read. | 241 // Initiate the first read. |
| 242 int bytes_read = 0; | 242 int bytes_read = 0; |
| 243 if (request->Read(buf_, kBufferSize, &bytes_read)) | 243 if (request->Read(buf_, kBufferSize, &bytes_read)) |
| 244 OnReadCompleted(request, bytes_read); | 244 OnReadCompleted(request, bytes_read); |
| 245 else if (!request->status().is_io_pending()) | 245 else if (!request->status().is_io_pending()) |
| 246 OnResponseCompleted(request); | 246 OnResponseCompleted(request); |
| 247 } | 247 } |
| 248 } | 248 } |
| 249 | 249 |
| 250 virtual void OnReadCompleted(URLRequest* request, int bytes_read) { | 250 virtual void OnReadCompleted(net::URLRequest* request, int bytes_read) { |
| 251 // It doesn't make sense for the request to have IO pending at this point. | 251 // It doesn't make sense for the request to have IO pending at this point. |
| 252 DCHECK(!request->status().is_io_pending()); | 252 DCHECK(!request->status().is_io_pending()); |
| 253 | 253 |
| 254 if (response_started_count_ == 0) | 254 if (response_started_count_ == 0) |
| 255 received_data_before_response_ = true; | 255 received_data_before_response_ = true; |
| 256 | 256 |
| 257 if (cancel_in_rd_) | 257 if (cancel_in_rd_) |
| 258 request->Cancel(); | 258 request->Cancel(); |
| 259 | 259 |
| 260 if (bytes_read >= 0) { | 260 if (bytes_read >= 0) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 276 break; | 276 break; |
| 277 } | 277 } |
| 278 } | 278 } |
| 279 } | 279 } |
| 280 if (!request->status().is_io_pending()) | 280 if (!request->status().is_io_pending()) |
| 281 OnResponseCompleted(request); | 281 OnResponseCompleted(request); |
| 282 else if (cancel_in_rd_pending_) | 282 else if (cancel_in_rd_pending_) |
| 283 request->Cancel(); | 283 request->Cancel(); |
| 284 } | 284 } |
| 285 | 285 |
| 286 virtual void OnResponseCompleted(URLRequest* request) { | 286 virtual void OnResponseCompleted(net::URLRequest* request) { |
| 287 if (quit_on_complete_) | 287 if (quit_on_complete_) |
| 288 MessageLoop::current()->Quit(); | 288 MessageLoop::current()->Quit(); |
| 289 } | 289 } |
| 290 | 290 |
| 291 void OnAuthRequired(URLRequest* request, net::AuthChallengeInfo* auth_info) { | 291 void OnAuthRequired(net::URLRequest* request, |
| 292 net::AuthChallengeInfo* auth_info) { |
| 292 if (!username_.empty() || !password_.empty()) { | 293 if (!username_.empty() || !password_.empty()) { |
| 293 request->SetAuth(username_, password_); | 294 request->SetAuth(username_, password_); |
| 294 } else { | 295 } else { |
| 295 request->CancelAuth(); | 296 request->CancelAuth(); |
| 296 } | 297 } |
| 297 } | 298 } |
| 298 | 299 |
| 299 virtual void OnSSLCertificateError(URLRequest* request, | 300 virtual void OnSSLCertificateError(net::URLRequest* request, |
| 300 int cert_error, | 301 int cert_error, |
| 301 net::X509Certificate* cert) { | 302 net::X509Certificate* cert) { |
| 302 // The caller can control whether it needs all SSL requests to go through, | 303 // The caller can control whether it needs all SSL requests to go through, |
| 303 // independent of any possible errors, or whether it wants SSL errors to | 304 // independent of any possible errors, or whether it wants SSL errors to |
| 304 // cancel the request. | 305 // cancel the request. |
| 305 have_certificate_errors_ = true; | 306 have_certificate_errors_ = true; |
| 306 if (allow_certificate_errors_) | 307 if (allow_certificate_errors_) |
| 307 request->ContinueDespiteLastError(); | 308 request->ContinueDespiteLastError(); |
| 308 else | 309 else |
| 309 request->Cancel(); | 310 request->Cancel(); |
| 310 } | 311 } |
| 311 | 312 |
| 312 virtual void OnGetCookies(URLRequest* request, bool blocked_by_policy) { | 313 virtual void OnGetCookies(net::URLRequest* request, bool blocked_by_policy) { |
| 313 if (blocked_by_policy) { | 314 if (blocked_by_policy) { |
| 314 blocked_get_cookies_count_++; | 315 blocked_get_cookies_count_++; |
| 315 if (cancel_in_getcookiesblocked_) | 316 if (cancel_in_getcookiesblocked_) |
| 316 request->Cancel(); | 317 request->Cancel(); |
| 317 } | 318 } |
| 318 } | 319 } |
| 319 | 320 |
| 320 virtual void OnSetCookie(URLRequest* request, | 321 virtual void OnSetCookie(net::URLRequest* request, |
| 321 const std::string& cookie_line, | 322 const std::string& cookie_line, |
| 322 const net::CookieOptions& options, | 323 const net::CookieOptions& options, |
| 323 bool blocked_by_policy) { | 324 bool blocked_by_policy) { |
| 324 if (blocked_by_policy) { | 325 if (blocked_by_policy) { |
| 325 blocked_set_cookie_count_++; | 326 blocked_set_cookie_count_++; |
| 326 if (cancel_in_setcookieblocked_) | 327 if (cancel_in_setcookieblocked_) |
| 327 request->Cancel(); | 328 request->Cancel(); |
| 328 } else { | 329 } else { |
| 329 set_cookie_count_++; | 330 set_cookie_count_++; |
| 330 } | 331 } |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 390 bool received_data_before_response_; | 391 bool received_data_before_response_; |
| 391 bool request_failed_; | 392 bool request_failed_; |
| 392 bool have_certificate_errors_; | 393 bool have_certificate_errors_; |
| 393 std::string data_received_; | 394 std::string data_received_; |
| 394 | 395 |
| 395 // our read buffer | 396 // our read buffer |
| 396 scoped_refptr<net::IOBuffer> buf_; | 397 scoped_refptr<net::IOBuffer> buf_; |
| 397 }; | 398 }; |
| 398 | 399 |
| 399 #endif // NET_URL_REQUEST_URL_REQUEST_UNITTEST_H_ | 400 #endif // NET_URL_REQUEST_URL_REQUEST_UNITTEST_H_ |
| OLD | NEW |