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