| 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 #ifndef NET_URL_REQUEST_URL_REQUEST_TEST_UTIL_H_ | 5 #ifndef NET_URL_REQUEST_URL_REQUEST_TEST_UTIL_H_ |
| 6 #define NET_URL_REQUEST_URL_REQUEST_TEST_UTIL_H_ | 6 #define NET_URL_REQUEST_URL_REQUEST_TEST_UTIL_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <stdlib.h> | 9 #include <stdlib.h> |
| 10 | 10 |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 class TestURLRequest : public net::URLRequest { | 76 class TestURLRequest : public net::URLRequest { |
| 77 public: | 77 public: |
| 78 TestURLRequest(const GURL& url, Delegate* delegate); | 78 TestURLRequest(const GURL& url, Delegate* delegate); |
| 79 virtual ~TestURLRequest(); | 79 virtual ~TestURLRequest(); |
| 80 }; | 80 }; |
| 81 | 81 |
| 82 //----------------------------------------------------------------------------- | 82 //----------------------------------------------------------------------------- |
| 83 | 83 |
| 84 class TestDelegate : public net::URLRequest::Delegate { | 84 class TestDelegate : public net::URLRequest::Delegate { |
| 85 public: | 85 public: |
| 86 enum Options { | |
| 87 NO_GET_COOKIES = 1 << 0, | |
| 88 NO_SET_COOKIE = 1 << 1, | |
| 89 FORCE_SESSION = 1 << 2, | |
| 90 }; | |
| 91 | |
| 92 TestDelegate(); | 86 TestDelegate(); |
| 93 virtual ~TestDelegate(); | 87 virtual ~TestDelegate(); |
| 94 | 88 |
| 95 void set_cancel_in_received_redirect(bool val) { cancel_in_rr_ = val; } | 89 void set_cancel_in_received_redirect(bool val) { cancel_in_rr_ = val; } |
| 96 void set_cancel_in_response_started(bool val) { cancel_in_rs_ = val; } | 90 void set_cancel_in_response_started(bool val) { cancel_in_rs_ = val; } |
| 97 void set_cancel_in_received_data(bool val) { cancel_in_rd_ = val; } | 91 void set_cancel_in_received_data(bool val) { cancel_in_rd_ = val; } |
| 98 void set_cancel_in_received_data_pending(bool val) { | 92 void set_cancel_in_received_data_pending(bool val) { |
| 99 cancel_in_rd_pending_ = val; | 93 cancel_in_rd_pending_ = val; |
| 100 } | 94 } |
| 101 void set_quit_on_complete(bool val) { quit_on_complete_ = val; } | 95 void set_quit_on_complete(bool val) { quit_on_complete_ = val; } |
| 102 void set_quit_on_redirect(bool val) { quit_on_redirect_ = val; } | 96 void set_quit_on_redirect(bool val) { quit_on_redirect_ = val; } |
| 103 void set_allow_certificate_errors(bool val) { | 97 void set_allow_certificate_errors(bool val) { |
| 104 allow_certificate_errors_ = val; | 98 allow_certificate_errors_ = val; |
| 105 } | 99 } |
| 106 void set_cookie_options(int o) {cookie_options_bit_mask_ = o; } | |
| 107 void set_credentials(const net::AuthCredentials& credentials) { | 100 void set_credentials(const net::AuthCredentials& credentials) { |
| 108 credentials_ = credentials; | 101 credentials_ = credentials; |
| 109 } | 102 } |
| 110 | 103 |
| 111 // query state | 104 // query state |
| 112 const std::string& data_received() const { return data_received_; } | 105 const std::string& data_received() const { return data_received_; } |
| 113 int bytes_received() const { return static_cast<int>(data_received_.size()); } | 106 int bytes_received() const { return static_cast<int>(data_received_.size()); } |
| 114 int response_started_count() const { return response_started_count_; } | 107 int response_started_count() const { return response_started_count_; } |
| 115 int received_redirect_count() const { return received_redirect_count_; } | 108 int received_redirect_count() const { return received_redirect_count_; } |
| 116 int blocked_get_cookies_count() const { return blocked_get_cookies_count_; } | |
| 117 int blocked_set_cookie_count() const { return blocked_set_cookie_count_; } | |
| 118 int set_cookie_count() const { return set_cookie_count_; } | |
| 119 bool received_data_before_response() const { | 109 bool received_data_before_response() const { |
| 120 return received_data_before_response_; | 110 return received_data_before_response_; |
| 121 } | 111 } |
| 122 bool request_failed() const { return request_failed_; } | 112 bool request_failed() const { return request_failed_; } |
| 123 bool have_certificate_errors() const { return have_certificate_errors_; } | 113 bool have_certificate_errors() const { return have_certificate_errors_; } |
| 124 bool certificate_errors_are_fatal() const { | 114 bool certificate_errors_are_fatal() const { |
| 125 return certificate_errors_are_fatal_; | 115 return certificate_errors_are_fatal_; |
| 126 } | 116 } |
| 127 bool auth_required_called() const { return auth_required_; } | 117 bool auth_required_called() const { return auth_required_; } |
| 128 | 118 |
| 129 // net::URLRequest::Delegate: | 119 // net::URLRequest::Delegate: |
| 130 virtual void OnReceivedRedirect(net::URLRequest* request, const GURL& new_url, | 120 virtual void OnReceivedRedirect(net::URLRequest* request, const GURL& new_url, |
| 131 bool* defer_redirect) OVERRIDE; | 121 bool* defer_redirect) OVERRIDE; |
| 132 virtual void OnAuthRequired(net::URLRequest* request, | 122 virtual void OnAuthRequired(net::URLRequest* request, |
| 133 net::AuthChallengeInfo* auth_info) OVERRIDE; | 123 net::AuthChallengeInfo* auth_info) OVERRIDE; |
| 134 // NOTE: |fatal| causes |certificate_errors_are_fatal_| to be set to true. | 124 // NOTE: |fatal| causes |certificate_errors_are_fatal_| to be set to true. |
| 135 // (Unit tests use this as a post-condition.) But for policy, this method | 125 // (Unit tests use this as a post-condition.) But for policy, this method |
| 136 // consults |allow_certificate_errors_|. | 126 // consults |allow_certificate_errors_|. |
| 137 virtual void OnSSLCertificateError(net::URLRequest* request, | 127 virtual void OnSSLCertificateError(net::URLRequest* request, |
| 138 const net::SSLInfo& ssl_info, | 128 const net::SSLInfo& ssl_info, |
| 139 bool fatal) OVERRIDE; | 129 bool fatal) OVERRIDE; |
| 140 virtual bool CanGetCookies(const net::URLRequest* request, | |
| 141 const net::CookieList& cookie_list) const OVERRIDE; | |
| 142 virtual bool CanSetCookie(const net::URLRequest* request, | |
| 143 const std::string& cookie_line, | |
| 144 net::CookieOptions* options) const OVERRIDE; | |
| 145 virtual void OnResponseStarted(net::URLRequest* request) OVERRIDE; | 130 virtual void OnResponseStarted(net::URLRequest* request) OVERRIDE; |
| 146 virtual void OnReadCompleted(net::URLRequest* request, | 131 virtual void OnReadCompleted(net::URLRequest* request, |
| 147 int bytes_read) OVERRIDE; | 132 int bytes_read) OVERRIDE; |
| 148 | 133 |
| 149 private: | 134 private: |
| 150 static const int kBufferSize = 4096; | 135 static const int kBufferSize = 4096; |
| 151 | 136 |
| 152 virtual void OnResponseCompleted(net::URLRequest* request); | 137 virtual void OnResponseCompleted(net::URLRequest* request); |
| 153 | 138 |
| 154 // options for controlling behavior | 139 // options for controlling behavior |
| 155 bool cancel_in_rr_; | 140 bool cancel_in_rr_; |
| 156 bool cancel_in_rs_; | 141 bool cancel_in_rs_; |
| 157 bool cancel_in_rd_; | 142 bool cancel_in_rd_; |
| 158 bool cancel_in_rd_pending_; | 143 bool cancel_in_rd_pending_; |
| 159 bool quit_on_complete_; | 144 bool quit_on_complete_; |
| 160 bool quit_on_redirect_; | 145 bool quit_on_redirect_; |
| 161 bool allow_certificate_errors_; | 146 bool allow_certificate_errors_; |
| 162 int cookie_options_bit_mask_; | |
| 163 net::AuthCredentials credentials_; | 147 net::AuthCredentials credentials_; |
| 164 | 148 |
| 165 // tracks status of callbacks | 149 // tracks status of callbacks |
| 166 int response_started_count_; | 150 int response_started_count_; |
| 167 int received_bytes_count_; | 151 int received_bytes_count_; |
| 168 int received_redirect_count_; | 152 int received_redirect_count_; |
| 169 mutable int blocked_get_cookies_count_; | |
| 170 mutable int blocked_set_cookie_count_; | |
| 171 mutable int set_cookie_count_; | |
| 172 bool received_data_before_response_; | 153 bool received_data_before_response_; |
| 173 bool request_failed_; | 154 bool request_failed_; |
| 174 bool have_certificate_errors_; | 155 bool have_certificate_errors_; |
| 175 bool certificate_errors_are_fatal_; | 156 bool certificate_errors_are_fatal_; |
| 176 bool auth_required_; | 157 bool auth_required_; |
| 177 std::string data_received_; | 158 std::string data_received_; |
| 178 | 159 |
| 179 // our read buffer | 160 // our read buffer |
| 180 scoped_refptr<net::IOBuffer> buf_; | 161 scoped_refptr<net::IOBuffer> buf_; |
| 181 }; | 162 }; |
| 182 | 163 |
| 183 //----------------------------------------------------------------------------- | 164 //----------------------------------------------------------------------------- |
| 184 | 165 |
| 185 class TestNetworkDelegate : public net::NetworkDelegate { | 166 class TestNetworkDelegate : public net::NetworkDelegate { |
| 186 public: | 167 public: |
| 168 enum Options { |
| 169 NO_GET_COOKIES = 1 << 0, |
| 170 NO_SET_COOKIE = 1 << 1, |
| 171 FORCE_SESSION = 1 << 2, |
| 172 }; |
| 173 |
| 187 TestNetworkDelegate(); | 174 TestNetworkDelegate(); |
| 188 virtual ~TestNetworkDelegate(); | 175 virtual ~TestNetworkDelegate(); |
| 189 | 176 |
| 177 void set_cookie_options(int o) {cookie_options_bit_mask_ = o; } |
| 178 void ResetCookieCounters(); |
| 179 |
| 190 int last_error() const { return last_error_; } | 180 int last_error() const { return last_error_; } |
| 191 int error_count() const { return error_count_; } | 181 int error_count() const { return error_count_; } |
| 192 int created_requests() const { return created_requests_; } | 182 int created_requests() const { return created_requests_; } |
| 193 int destroyed_requests() const { return destroyed_requests_; } | 183 int destroyed_requests() const { return destroyed_requests_; } |
| 194 int completed_requests() const { return completed_requests_; } | 184 int completed_requests() const { return completed_requests_; } |
| 185 int blocked_get_cookies_count() const { return blocked_get_cookies_count_; } |
| 186 int blocked_set_cookie_count() const { return blocked_set_cookie_count_; } |
| 187 int set_cookie_count() const { return set_cookie_count_; } |
| 195 | 188 |
| 196 protected: | 189 protected: |
| 197 // net::NetworkDelegate: | 190 // net::NetworkDelegate: |
| 198 virtual int OnBeforeURLRequest(net::URLRequest* request, | 191 virtual int OnBeforeURLRequest(net::URLRequest* request, |
| 199 const net::CompletionCallback& callback, | 192 const net::CompletionCallback& callback, |
| 200 GURL* new_url) OVERRIDE; | 193 GURL* new_url) OVERRIDE; |
| 201 virtual int OnBeforeSendHeaders(net::URLRequest* request, | 194 virtual int OnBeforeSendHeaders(net::URLRequest* request, |
| 202 const net::CompletionCallback& callback, | 195 const net::CompletionCallback& callback, |
| 203 net::HttpRequestHeaders* headers) OVERRIDE; | 196 net::HttpRequestHeaders* headers) OVERRIDE; |
| 204 virtual void OnSendHeaders(net::URLRequest* request, | 197 virtual void OnSendHeaders(net::URLRequest* request, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 216 int bytes_read) OVERRIDE; | 209 int bytes_read) OVERRIDE; |
| 217 virtual void OnCompleted(net::URLRequest* request, bool started) OVERRIDE; | 210 virtual void OnCompleted(net::URLRequest* request, bool started) OVERRIDE; |
| 218 virtual void OnURLRequestDestroyed(net::URLRequest* request) OVERRIDE; | 211 virtual void OnURLRequestDestroyed(net::URLRequest* request) OVERRIDE; |
| 219 virtual void OnPACScriptError(int line_number, | 212 virtual void OnPACScriptError(int line_number, |
| 220 const string16& error) OVERRIDE; | 213 const string16& error) OVERRIDE; |
| 221 virtual net::NetworkDelegate::AuthRequiredResponse OnAuthRequired( | 214 virtual net::NetworkDelegate::AuthRequiredResponse OnAuthRequired( |
| 222 net::URLRequest* request, | 215 net::URLRequest* request, |
| 223 const net::AuthChallengeInfo& auth_info, | 216 const net::AuthChallengeInfo& auth_info, |
| 224 const AuthCallback& callback, | 217 const AuthCallback& callback, |
| 225 net::AuthCredentials* credentials) OVERRIDE; | 218 net::AuthCredentials* credentials) OVERRIDE; |
| 219 virtual bool OnReadingCookies(const net::URLRequest* request, |
| 220 const net::CookieList& cookie_list) OVERRIDE; |
| 221 virtual bool OnSettingCookie(const net::URLRequest* request, |
| 222 const std::string& cookie_line, |
| 223 net::CookieOptions* options) OVERRIDE; |
| 226 | 224 |
| 227 void InitRequestStatesIfNew(int request_id); | 225 void InitRequestStatesIfNew(int request_id); |
| 228 | 226 |
| 229 int last_error_; | 227 int last_error_; |
| 230 int error_count_; | 228 int error_count_; |
| 231 int created_requests_; | 229 int created_requests_; |
| 232 int destroyed_requests_; | 230 int destroyed_requests_; |
| 233 int completed_requests_; | 231 int completed_requests_; |
| 232 int cookie_options_bit_mask_; |
| 233 int blocked_get_cookies_count_; |
| 234 int blocked_set_cookie_count_; |
| 235 int set_cookie_count_; |
| 234 | 236 |
| 235 // net::NetworkDelegate callbacks happen in a particular order (e.g. | 237 // net::NetworkDelegate callbacks happen in a particular order (e.g. |
| 236 // OnBeforeURLRequest is always called before OnBeforeSendHeaders). | 238 // OnBeforeURLRequest is always called before OnBeforeSendHeaders). |
| 237 // This bit-set indicates for each request id (key) what events may be sent | 239 // This bit-set indicates for each request id (key) what events may be sent |
| 238 // next. | 240 // next. |
| 239 std::map<int, int> next_states_; | 241 std::map<int, int> next_states_; |
| 240 | 242 |
| 241 // A log that records for each request id (key) the order in which On... | 243 // A log that records for each request id (key) the order in which On... |
| 242 // functions were called. | 244 // functions were called. |
| 243 std::map<int, std::string> event_order_; | 245 std::map<int, std::string> event_order_; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 262 | 264 |
| 263 private: | 265 private: |
| 264 static std::string value_; | 266 static std::string value_; |
| 265 const std::string old_value_; | 267 const std::string old_value_; |
| 266 const std::string new_value_; | 268 const std::string new_value_; |
| 267 | 269 |
| 268 DISALLOW_COPY_AND_ASSIGN(ScopedCustomUrlRequestTestHttpHost); | 270 DISALLOW_COPY_AND_ASSIGN(ScopedCustomUrlRequestTestHttpHost); |
| 269 }; | 271 }; |
| 270 | 272 |
| 271 #endif // NET_URL_REQUEST_URL_REQUEST_TEST_UTIL_H_ | 273 #endif // NET_URL_REQUEST_URL_REQUEST_TEST_UTIL_H_ |
| OLD | NEW |