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