| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef NET_URL_REQUEST_URL_REQUEST_TEST_UTIL_H_ | |
| 6 #define NET_URL_REQUEST_URL_REQUEST_TEST_UTIL_H_ | |
| 7 | |
| 8 #include <stdlib.h> | |
| 9 | |
| 10 #include <map> | |
| 11 #include <string> | |
| 12 | |
| 13 #include "base/basictypes.h" | |
| 14 #include "base/compiler_specific.h" | |
| 15 #include "base/memory/ref_counted.h" | |
| 16 #include "base/memory/scoped_ptr.h" | |
| 17 #include "base/message_loop/message_loop_proxy.h" | |
| 18 #include "base/path_service.h" | |
| 19 #include "base/strings/string16.h" | |
| 20 #include "base/strings/string_util.h" | |
| 21 #include "base/strings/utf_string_conversions.h" | |
| 22 #include "base/time/time.h" | |
| 23 #include "net/base/io_buffer.h" | |
| 24 #include "net/base/load_timing_info.h" | |
| 25 #include "net/base/net_errors.h" | |
| 26 #include "net/base/network_delegate_impl.h" | |
| 27 #include "net/base/request_priority.h" | |
| 28 #include "net/base/sdch_manager.h" | |
| 29 #include "net/cert/cert_verifier.h" | |
| 30 #include "net/cookies/cookie_monster.h" | |
| 31 #include "net/disk_cache/disk_cache.h" | |
| 32 #include "net/ftp/ftp_network_layer.h" | |
| 33 #include "net/http/http_auth_handler_factory.h" | |
| 34 #include "net/http/http_cache.h" | |
| 35 #include "net/http/http_network_layer.h" | |
| 36 #include "net/http/http_network_session.h" | |
| 37 #include "net/http/http_request_headers.h" | |
| 38 #include "net/proxy/proxy_service.h" | |
| 39 #include "net/ssl/ssl_config_service_defaults.h" | |
| 40 #include "net/url_request/url_request.h" | |
| 41 #include "net/url_request/url_request_context.h" | |
| 42 #include "net/url_request/url_request_context_getter.h" | |
| 43 #include "net/url_request/url_request_context_storage.h" | |
| 44 #include "net/url_request/url_request_job_factory.h" | |
| 45 #include "url/url_util.h" | |
| 46 | |
| 47 using base::TimeDelta; | |
| 48 | |
| 49 namespace net { | |
| 50 | |
| 51 //----------------------------------------------------------------------------- | |
| 52 | |
| 53 class TestURLRequestContext : public URLRequestContext { | |
| 54 public: | |
| 55 TestURLRequestContext(); | |
| 56 // Default constructor like TestURLRequestContext() but does not call | |
| 57 // Init() in case |delay_initialization| is true. This allows modifying the | |
| 58 // URLRequestContext before it is constructed completely. If | |
| 59 // |delay_initialization| is true, Init() needs be be called manually. | |
| 60 explicit TestURLRequestContext(bool delay_initialization); | |
| 61 ~TestURLRequestContext() override; | |
| 62 | |
| 63 void Init(); | |
| 64 | |
| 65 ClientSocketFactory* client_socket_factory() { | |
| 66 return client_socket_factory_; | |
| 67 } | |
| 68 void set_client_socket_factory(ClientSocketFactory* factory) { | |
| 69 client_socket_factory_ = factory; | |
| 70 } | |
| 71 | |
| 72 void set_http_network_session_params( | |
| 73 const HttpNetworkSession::Params& params) { | |
| 74 } | |
| 75 | |
| 76 void SetSdchManager(scoped_ptr<SdchManager> sdch_manager) { | |
| 77 context_storage_.set_sdch_manager(sdch_manager.Pass()); | |
| 78 } | |
| 79 | |
| 80 private: | |
| 81 bool initialized_; | |
| 82 | |
| 83 // Optional parameters to override default values. Note that values that | |
| 84 // point to other objects the TestURLRequestContext creates will be | |
| 85 // overwritten. | |
| 86 scoped_ptr<HttpNetworkSession::Params> http_network_session_params_; | |
| 87 | |
| 88 // Not owned: | |
| 89 ClientSocketFactory* client_socket_factory_; | |
| 90 | |
| 91 protected: | |
| 92 URLRequestContextStorage context_storage_; | |
| 93 }; | |
| 94 | |
| 95 //----------------------------------------------------------------------------- | |
| 96 | |
| 97 // Used to return a dummy context, which lives on the message loop | |
| 98 // given in the constructor. | |
| 99 class TestURLRequestContextGetter : public URLRequestContextGetter { | |
| 100 public: | |
| 101 // |network_task_runner| must not be NULL. | |
| 102 explicit TestURLRequestContextGetter( | |
| 103 const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner); | |
| 104 | |
| 105 // Use to pass a pre-initialized |context|. | |
| 106 TestURLRequestContextGetter( | |
| 107 const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner, | |
| 108 scoped_ptr<TestURLRequestContext> context); | |
| 109 | |
| 110 // URLRequestContextGetter implementation. | |
| 111 TestURLRequestContext* GetURLRequestContext() override; | |
| 112 scoped_refptr<base::SingleThreadTaskRunner> GetNetworkTaskRunner() | |
| 113 const override; | |
| 114 | |
| 115 protected: | |
| 116 ~TestURLRequestContextGetter() override; | |
| 117 | |
| 118 private: | |
| 119 const scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_; | |
| 120 scoped_ptr<TestURLRequestContext> context_; | |
| 121 }; | |
| 122 | |
| 123 //----------------------------------------------------------------------------- | |
| 124 | |
| 125 class TestDelegate : public URLRequest::Delegate { | |
| 126 public: | |
| 127 TestDelegate(); | |
| 128 ~TestDelegate() override; | |
| 129 | |
| 130 void set_cancel_in_received_redirect(bool val) { cancel_in_rr_ = val; } | |
| 131 void set_cancel_in_response_started(bool val) { cancel_in_rs_ = val; } | |
| 132 void set_cancel_in_received_data(bool val) { cancel_in_rd_ = val; } | |
| 133 void set_cancel_in_received_data_pending(bool val) { | |
| 134 cancel_in_rd_pending_ = val; | |
| 135 } | |
| 136 void set_quit_on_complete(bool val) { quit_on_complete_ = val; } | |
| 137 void set_quit_on_redirect(bool val) { quit_on_redirect_ = val; } | |
| 138 void set_quit_on_network_start(bool val) { | |
| 139 quit_on_before_network_start_ = val; | |
| 140 } | |
| 141 void set_allow_certificate_errors(bool val) { | |
| 142 allow_certificate_errors_ = val; | |
| 143 } | |
| 144 void set_credentials(const AuthCredentials& credentials) { | |
| 145 credentials_ = credentials; | |
| 146 } | |
| 147 | |
| 148 // query state | |
| 149 const std::string& data_received() const { return data_received_; } | |
| 150 int bytes_received() const { return static_cast<int>(data_received_.size()); } | |
| 151 int response_started_count() const { return response_started_count_; } | |
| 152 int received_redirect_count() const { return received_redirect_count_; } | |
| 153 int received_before_network_start_count() const { | |
| 154 return received_before_network_start_count_; | |
| 155 } | |
| 156 bool received_data_before_response() const { | |
| 157 return received_data_before_response_; | |
| 158 } | |
| 159 bool request_failed() const { return request_failed_; } | |
| 160 bool have_certificate_errors() const { return have_certificate_errors_; } | |
| 161 bool certificate_errors_are_fatal() const { | |
| 162 return certificate_errors_are_fatal_; | |
| 163 } | |
| 164 bool auth_required_called() const { return auth_required_; } | |
| 165 bool have_full_request_headers() const { return have_full_request_headers_; } | |
| 166 const HttpRequestHeaders& full_request_headers() const { | |
| 167 return full_request_headers_; | |
| 168 } | |
| 169 void ClearFullRequestHeaders(); | |
| 170 | |
| 171 // URLRequest::Delegate: | |
| 172 void OnReceivedRedirect(URLRequest* request, | |
| 173 const RedirectInfo& redirect_info, | |
| 174 bool* defer_redirect) override; | |
| 175 void OnBeforeNetworkStart(URLRequest* request, bool* defer) override; | |
| 176 void OnAuthRequired(URLRequest* request, | |
| 177 AuthChallengeInfo* auth_info) override; | |
| 178 // NOTE: |fatal| causes |certificate_errors_are_fatal_| to be set to true. | |
| 179 // (Unit tests use this as a post-condition.) But for policy, this method | |
| 180 // consults |allow_certificate_errors_|. | |
| 181 void OnSSLCertificateError(URLRequest* request, | |
| 182 const SSLInfo& ssl_info, | |
| 183 bool fatal) override; | |
| 184 void OnResponseStarted(URLRequest* request) override; | |
| 185 void OnReadCompleted(URLRequest* request, int bytes_read) override; | |
| 186 | |
| 187 private: | |
| 188 static const int kBufferSize = 4096; | |
| 189 | |
| 190 virtual void OnResponseCompleted(URLRequest* request); | |
| 191 | |
| 192 // options for controlling behavior | |
| 193 bool cancel_in_rr_; | |
| 194 bool cancel_in_rs_; | |
| 195 bool cancel_in_rd_; | |
| 196 bool cancel_in_rd_pending_; | |
| 197 bool quit_on_complete_; | |
| 198 bool quit_on_redirect_; | |
| 199 bool quit_on_before_network_start_; | |
| 200 bool allow_certificate_errors_; | |
| 201 AuthCredentials credentials_; | |
| 202 | |
| 203 // tracks status of callbacks | |
| 204 int response_started_count_; | |
| 205 int received_bytes_count_; | |
| 206 int received_redirect_count_; | |
| 207 int received_before_network_start_count_; | |
| 208 bool received_data_before_response_; | |
| 209 bool request_failed_; | |
| 210 bool have_certificate_errors_; | |
| 211 bool certificate_errors_are_fatal_; | |
| 212 bool auth_required_; | |
| 213 std::string data_received_; | |
| 214 bool have_full_request_headers_; | |
| 215 HttpRequestHeaders full_request_headers_; | |
| 216 | |
| 217 // our read buffer | |
| 218 scoped_refptr<IOBuffer> buf_; | |
| 219 }; | |
| 220 | |
| 221 //----------------------------------------------------------------------------- | |
| 222 | |
| 223 class TestNetworkDelegate : public NetworkDelegateImpl { | |
| 224 public: | |
| 225 enum Options { | |
| 226 NO_GET_COOKIES = 1 << 0, | |
| 227 NO_SET_COOKIE = 1 << 1, | |
| 228 }; | |
| 229 | |
| 230 TestNetworkDelegate(); | |
| 231 ~TestNetworkDelegate() override; | |
| 232 | |
| 233 // Writes the LoadTimingInfo during the most recent call to OnBeforeRedirect. | |
| 234 bool GetLoadTimingInfoBeforeRedirect( | |
| 235 LoadTimingInfo* load_timing_info_before_redirect) const; | |
| 236 | |
| 237 // Same as GetLoadTimingInfoBeforeRedirect, except for calls to | |
| 238 // AuthRequiredResponse. | |
| 239 bool GetLoadTimingInfoBeforeAuth( | |
| 240 LoadTimingInfo* load_timing_info_before_auth) const; | |
| 241 | |
| 242 // Will redirect once to the given URL when the next set of headers are | |
| 243 // received. | |
| 244 void set_redirect_on_headers_received_url( | |
| 245 GURL redirect_on_headers_received_url) { | |
| 246 redirect_on_headers_received_url_ = redirect_on_headers_received_url; | |
| 247 } | |
| 248 | |
| 249 void set_allowed_unsafe_redirect_url(GURL allowed_unsafe_redirect_url) { | |
| 250 allowed_unsafe_redirect_url_ = allowed_unsafe_redirect_url; | |
| 251 } | |
| 252 | |
| 253 void set_cookie_options(int o) {cookie_options_bit_mask_ = o; } | |
| 254 | |
| 255 int last_error() const { return last_error_; } | |
| 256 int error_count() const { return error_count_; } | |
| 257 int created_requests() const { return created_requests_; } | |
| 258 int destroyed_requests() const { return destroyed_requests_; } | |
| 259 int completed_requests() const { return completed_requests_; } | |
| 260 int canceled_requests() const { return canceled_requests_; } | |
| 261 int blocked_get_cookies_count() const { return blocked_get_cookies_count_; } | |
| 262 int blocked_set_cookie_count() const { return blocked_set_cookie_count_; } | |
| 263 int set_cookie_count() const { return set_cookie_count_; } | |
| 264 | |
| 265 void set_can_access_files(bool val) { can_access_files_ = val; } | |
| 266 bool can_access_files() const { return can_access_files_; } | |
| 267 | |
| 268 void set_can_throttle_requests(bool val) { can_throttle_requests_ = val; } | |
| 269 bool can_throttle_requests() const { return can_throttle_requests_; } | |
| 270 | |
| 271 void set_cancel_request_with_policy_violating_referrer(bool val) { | |
| 272 cancel_request_with_policy_violating_referrer_ = val; | |
| 273 } | |
| 274 | |
| 275 int observed_before_proxy_headers_sent_callbacks() const { | |
| 276 return observed_before_proxy_headers_sent_callbacks_; | |
| 277 } | |
| 278 int before_send_headers_count() const { return before_send_headers_count_; } | |
| 279 int headers_received_count() const { return headers_received_count_; } | |
| 280 | |
| 281 // Last observed proxy in proxy header sent callback. | |
| 282 HostPortPair last_observed_proxy() { | |
| 283 return last_observed_proxy_; | |
| 284 } | |
| 285 | |
| 286 void set_can_be_intercepted_on_error(bool can_be_intercepted_on_error) { | |
| 287 will_be_intercepted_on_next_error_ = can_be_intercepted_on_error; | |
| 288 } | |
| 289 | |
| 290 protected: | |
| 291 // NetworkDelegate: | |
| 292 int OnBeforeURLRequest(URLRequest* request, | |
| 293 const CompletionCallback& callback, | |
| 294 GURL* new_url) override; | |
| 295 int OnBeforeSendHeaders(URLRequest* request, | |
| 296 const CompletionCallback& callback, | |
| 297 HttpRequestHeaders* headers) override; | |
| 298 void OnBeforeSendProxyHeaders(net::URLRequest* request, | |
| 299 const net::ProxyInfo& proxy_info, | |
| 300 net::HttpRequestHeaders* headers) override; | |
| 301 void OnSendHeaders(URLRequest* request, | |
| 302 const HttpRequestHeaders& headers) override; | |
| 303 int OnHeadersReceived( | |
| 304 URLRequest* request, | |
| 305 const CompletionCallback& callback, | |
| 306 const HttpResponseHeaders* original_response_headers, | |
| 307 scoped_refptr<HttpResponseHeaders>* override_response_headers, | |
| 308 GURL* allowed_unsafe_redirect_url) override; | |
| 309 void OnBeforeRedirect(URLRequest* request, const GURL& new_location) override; | |
| 310 void OnResponseStarted(URLRequest* request) override; | |
| 311 void OnRawBytesRead(const URLRequest& request, int bytes_read) override; | |
| 312 void OnCompleted(URLRequest* request, bool started) override; | |
| 313 void OnURLRequestDestroyed(URLRequest* request) override; | |
| 314 void OnPACScriptError(int line_number, const base::string16& error) override; | |
| 315 NetworkDelegate::AuthRequiredResponse OnAuthRequired( | |
| 316 URLRequest* request, | |
| 317 const AuthChallengeInfo& auth_info, | |
| 318 const AuthCallback& callback, | |
| 319 AuthCredentials* credentials) override; | |
| 320 bool OnCanGetCookies(const URLRequest& request, | |
| 321 const CookieList& cookie_list) override; | |
| 322 bool OnCanSetCookie(const URLRequest& request, | |
| 323 const std::string& cookie_line, | |
| 324 CookieOptions* options) override; | |
| 325 bool OnCanAccessFile(const URLRequest& request, | |
| 326 const base::FilePath& path) const override; | |
| 327 bool OnCanThrottleRequest(const URLRequest& request) const override; | |
| 328 bool OnCancelURLRequestWithPolicyViolatingReferrerHeader( | |
| 329 const URLRequest& request, | |
| 330 const GURL& target_url, | |
| 331 const GURL& referrer_url) const override; | |
| 332 | |
| 333 void InitRequestStatesIfNew(int request_id); | |
| 334 | |
| 335 GURL redirect_on_headers_received_url_; | |
| 336 // URL marked as safe for redirection at the onHeadersReceived stage. | |
| 337 GURL allowed_unsafe_redirect_url_; | |
| 338 | |
| 339 int last_error_; | |
| 340 int error_count_; | |
| 341 int created_requests_; | |
| 342 int destroyed_requests_; | |
| 343 int completed_requests_; | |
| 344 int canceled_requests_; | |
| 345 int cookie_options_bit_mask_; | |
| 346 int blocked_get_cookies_count_; | |
| 347 int blocked_set_cookie_count_; | |
| 348 int set_cookie_count_; | |
| 349 int observed_before_proxy_headers_sent_callbacks_; | |
| 350 int before_send_headers_count_; | |
| 351 int headers_received_count_; | |
| 352 // Last observed proxy in before proxy header sent callback. | |
| 353 HostPortPair last_observed_proxy_; | |
| 354 | |
| 355 // NetworkDelegate callbacks happen in a particular order (e.g. | |
| 356 // OnBeforeURLRequest is always called before OnBeforeSendHeaders). | |
| 357 // This bit-set indicates for each request id (key) what events may be sent | |
| 358 // next. | |
| 359 std::map<int, int> next_states_; | |
| 360 | |
| 361 // A log that records for each request id (key) the order in which On... | |
| 362 // functions were called. | |
| 363 std::map<int, std::string> event_order_; | |
| 364 | |
| 365 LoadTimingInfo load_timing_info_before_redirect_; | |
| 366 bool has_load_timing_info_before_redirect_; | |
| 367 | |
| 368 LoadTimingInfo load_timing_info_before_auth_; | |
| 369 bool has_load_timing_info_before_auth_; | |
| 370 | |
| 371 bool can_access_files_; // true by default | |
| 372 bool can_throttle_requests_; // true by default | |
| 373 bool cancel_request_with_policy_violating_referrer_; // false by default | |
| 374 bool will_be_intercepted_on_next_error_; | |
| 375 }; | |
| 376 | |
| 377 // Overrides the host used by the LocalHttpTestServer in | |
| 378 // url_request_unittest.cc . This is used by the chrome_frame_net_tests due to | |
| 379 // a mysterious bug when tests execute over the loopback adapter. See | |
| 380 // http://crbug.com/114369 . | |
| 381 class ScopedCustomUrlRequestTestHttpHost { | |
| 382 public: | |
| 383 // Sets the host name to be used. The previous hostname will be stored and | |
| 384 // restored upon destruction. Note that if the lifetimes of two or more | |
| 385 // instances of this class overlap, they must be strictly nested. | |
| 386 explicit ScopedCustomUrlRequestTestHttpHost(const std::string& new_value); | |
| 387 | |
| 388 ~ScopedCustomUrlRequestTestHttpHost(); | |
| 389 | |
| 390 // Returns the current value to be used by HTTP tests in | |
| 391 // url_request_unittest.cc . | |
| 392 static const std::string& value(); | |
| 393 | |
| 394 private: | |
| 395 static std::string value_; | |
| 396 const std::string old_value_; | |
| 397 const std::string new_value_; | |
| 398 | |
| 399 DISALLOW_COPY_AND_ASSIGN(ScopedCustomUrlRequestTestHttpHost); | |
| 400 }; | |
| 401 | |
| 402 //----------------------------------------------------------------------------- | |
| 403 | |
| 404 // A simple ProtocolHandler that returns a pre-built URLRequestJob only once. | |
| 405 class TestJobInterceptor : public URLRequestJobFactory::ProtocolHandler { | |
| 406 public: | |
| 407 TestJobInterceptor(); | |
| 408 | |
| 409 URLRequestJob* MaybeCreateJob( | |
| 410 URLRequest* request, | |
| 411 NetworkDelegate* network_delegate) const override; | |
| 412 void set_main_intercept_job(URLRequestJob* job); | |
| 413 | |
| 414 private: | |
| 415 mutable URLRequestJob* main_intercept_job_; | |
| 416 }; | |
| 417 | |
| 418 } // namespace net | |
| 419 | |
| 420 #endif // NET_URL_REQUEST_URL_REQUEST_TEST_UTIL_H_ | |
| OLD | NEW |