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 | 7 |
8 #include <stdlib.h> | 8 #include <stdlib.h> |
9 | 9 |
10 #include <map> | 10 #include <map> |
(...skipping 23 matching lines...) Expand all Loading... | |
34 #include "net/http/http_network_layer.h" | 34 #include "net/http/http_network_layer.h" |
35 #include "net/proxy/proxy_service.h" | 35 #include "net/proxy/proxy_service.h" |
36 #include "net/url_request/url_request.h" | 36 #include "net/url_request/url_request.h" |
37 #include "net/url_request/url_request_context.h" | 37 #include "net/url_request/url_request_context.h" |
38 #include "net/url_request/url_request_context_getter.h" | 38 #include "net/url_request/url_request_context_getter.h" |
39 #include "net/url_request/url_request_context_storage.h" | 39 #include "net/url_request/url_request_context_storage.h" |
40 #include "net/url_request/url_request_job_factory.h" | 40 #include "net/url_request/url_request_job_factory.h" |
41 | 41 |
42 using base::TimeDelta; | 42 using base::TimeDelta; |
43 | 43 |
44 namespace net { | |
45 | |
44 //----------------------------------------------------------------------------- | 46 //----------------------------------------------------------------------------- |
45 | 47 |
46 class TestURLRequestContext : public net::URLRequestContext { | 48 class TestURLRequestContext : public URLRequestContext { |
47 public: | 49 public: |
48 TestURLRequestContext(); | 50 TestURLRequestContext(); |
49 // Default constructor like TestURLRequestContext() but does not call | 51 // Default constructor like TestURLRequestContext() but does not call |
50 // Init() in case |delay_initialization| is true. This allows modifying the | 52 // Init() in case |delay_initialization| is true. This allows modifying the |
51 // URLRequestContext before it is constructed completely. If | 53 // URLRequestContext before it is constructed completely. If |
52 // |delay_initialization| is true, Init() needs be be called manually. | 54 // |delay_initialization| is true, Init() needs be be called manually. |
53 explicit TestURLRequestContext(bool delay_initialization); | 55 explicit TestURLRequestContext(bool delay_initialization); |
54 virtual ~TestURLRequestContext(); | 56 virtual ~TestURLRequestContext(); |
55 | 57 |
56 void Init(); | 58 void Init(); |
57 | 59 |
58 private: | 60 private: |
59 bool initialized_; | 61 bool initialized_; |
60 | 62 |
61 protected: | 63 protected: |
62 net::URLRequestContextStorage context_storage_; | 64 URLRequestContextStorage context_storage_; |
63 }; | 65 }; |
64 | 66 |
65 //----------------------------------------------------------------------------- | 67 //----------------------------------------------------------------------------- |
66 | 68 |
67 // Used to return a dummy context, which lives on the message loop | 69 // Used to return a dummy context, which lives on the message loop |
68 // given in the constructor. | 70 // given in the constructor. |
69 class TestURLRequestContextGetter : public net::URLRequestContextGetter { | 71 class TestURLRequestContextGetter : public URLRequestContextGetter { |
70 public: | 72 public: |
71 // |network_task_runner| must not be NULL. | 73 // |network_task_runner| must not be NULL. |
72 explicit TestURLRequestContextGetter( | 74 explicit TestURLRequestContextGetter( |
73 const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner); | 75 const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner); |
74 | 76 |
75 // Use to pass a pre-initialized |context|. | 77 // Use to pass a pre-initialized |context|. |
76 TestURLRequestContextGetter( | 78 TestURLRequestContextGetter( |
77 const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner, | 79 const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner, |
78 scoped_ptr<TestURLRequestContext> context); | 80 scoped_ptr<TestURLRequestContext> context); |
79 | 81 |
80 // net::URLRequestContextGetter implementation. | 82 // URLRequestContextGetter implementation. |
81 virtual TestURLRequestContext* GetURLRequestContext() OVERRIDE; | 83 virtual TestURLRequestContext* GetURLRequestContext() OVERRIDE; |
82 virtual scoped_refptr<base::SingleThreadTaskRunner> | 84 virtual scoped_refptr<base::SingleThreadTaskRunner> |
83 GetNetworkTaskRunner() const OVERRIDE; | 85 GetNetworkTaskRunner() const OVERRIDE; |
84 | 86 |
85 protected: | 87 protected: |
86 virtual ~TestURLRequestContextGetter(); | 88 virtual ~TestURLRequestContextGetter(); |
87 | 89 |
88 private: | 90 private: |
89 const scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_; | 91 const scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_; |
90 scoped_ptr<TestURLRequestContext> context_; | 92 scoped_ptr<TestURLRequestContext> context_; |
91 }; | 93 }; |
92 | 94 |
93 //----------------------------------------------------------------------------- | 95 //----------------------------------------------------------------------------- |
94 | 96 |
95 class TestURLRequest : public net::URLRequest { | 97 class TestURLRequest : public URLRequest { |
96 public: | 98 public: |
97 TestURLRequest( | 99 TestURLRequest( |
98 const GURL& url, Delegate* delegate, TestURLRequestContext* context); | 100 const GURL& url, Delegate* delegate, TestURLRequestContext* context); |
99 virtual ~TestURLRequest(); | 101 virtual ~TestURLRequest(); |
100 }; | 102 }; |
101 | 103 |
102 //----------------------------------------------------------------------------- | 104 //----------------------------------------------------------------------------- |
103 | 105 |
104 class TestDelegate : public net::URLRequest::Delegate { | 106 class TestDelegate : public URLRequest::Delegate { |
105 public: | 107 public: |
106 TestDelegate(); | 108 TestDelegate(); |
107 virtual ~TestDelegate(); | 109 virtual ~TestDelegate(); |
108 | 110 |
109 void set_cancel_in_received_redirect(bool val) { cancel_in_rr_ = val; } | 111 void set_cancel_in_received_redirect(bool val) { cancel_in_rr_ = val; } |
110 void set_cancel_in_response_started(bool val) { cancel_in_rs_ = val; } | 112 void set_cancel_in_response_started(bool val) { cancel_in_rs_ = val; } |
111 void set_cancel_in_received_data(bool val) { cancel_in_rd_ = val; } | 113 void set_cancel_in_received_data(bool val) { cancel_in_rd_ = val; } |
112 void set_cancel_in_received_data_pending(bool val) { | 114 void set_cancel_in_received_data_pending(bool val) { |
113 cancel_in_rd_pending_ = val; | 115 cancel_in_rd_pending_ = val; |
114 } | 116 } |
115 void set_quit_on_complete(bool val) { quit_on_complete_ = val; } | 117 void set_quit_on_complete(bool val) { quit_on_complete_ = val; } |
116 void set_quit_on_redirect(bool val) { quit_on_redirect_ = val; } | 118 void set_quit_on_redirect(bool val) { quit_on_redirect_ = val; } |
117 void set_allow_certificate_errors(bool val) { | 119 void set_allow_certificate_errors(bool val) { |
118 allow_certificate_errors_ = val; | 120 allow_certificate_errors_ = val; |
119 } | 121 } |
120 void set_credentials(const net::AuthCredentials& credentials) { | 122 void set_credentials(const AuthCredentials& credentials) { |
121 credentials_ = credentials; | 123 credentials_ = credentials; |
122 } | 124 } |
123 | 125 |
124 // query state | 126 // query state |
125 const std::string& data_received() const { return data_received_; } | 127 const std::string& data_received() const { return data_received_; } |
126 int bytes_received() const { return static_cast<int>(data_received_.size()); } | 128 int bytes_received() const { return static_cast<int>(data_received_.size()); } |
127 int response_started_count() const { return response_started_count_; } | 129 int response_started_count() const { return response_started_count_; } |
128 int received_redirect_count() const { return received_redirect_count_; } | 130 int received_redirect_count() const { return received_redirect_count_; } |
129 bool received_data_before_response() const { | 131 bool received_data_before_response() const { |
130 return received_data_before_response_; | 132 return received_data_before_response_; |
131 } | 133 } |
132 bool request_failed() const { return request_failed_; } | 134 bool request_failed() const { return request_failed_; } |
133 bool have_certificate_errors() const { return have_certificate_errors_; } | 135 bool have_certificate_errors() const { return have_certificate_errors_; } |
134 bool certificate_errors_are_fatal() const { | 136 bool certificate_errors_are_fatal() const { |
135 return certificate_errors_are_fatal_; | 137 return certificate_errors_are_fatal_; |
136 } | 138 } |
137 bool auth_required_called() const { return auth_required_; } | 139 bool auth_required_called() const { return auth_required_; } |
138 | 140 |
139 // net::URLRequest::Delegate: | 141 // URLRequest::Delegate: |
140 virtual void OnReceivedRedirect(net::URLRequest* request, const GURL& new_url, | 142 virtual void OnReceivedRedirect(URLRequest* request, const GURL& new_url, |
141 bool* defer_redirect) OVERRIDE; | 143 bool* defer_redirect) OVERRIDE; |
142 virtual void OnAuthRequired(net::URLRequest* request, | 144 virtual void OnAuthRequired(URLRequest* request, |
143 net::AuthChallengeInfo* auth_info) OVERRIDE; | 145 AuthChallengeInfo* auth_info) OVERRIDE; |
144 // NOTE: |fatal| causes |certificate_errors_are_fatal_| to be set to true. | 146 // NOTE: |fatal| causes |certificate_errors_are_fatal_| to be set to true. |
145 // (Unit tests use this as a post-condition.) But for policy, this method | 147 // (Unit tests use this as a post-condition.) But for policy, this method |
146 // consults |allow_certificate_errors_|. | 148 // consults |allow_certificate_errors_|. |
147 virtual void OnSSLCertificateError(net::URLRequest* request, | 149 virtual void OnSSLCertificateError(URLRequest* request, |
148 const net::SSLInfo& ssl_info, | 150 const SSLInfo& ssl_info, |
149 bool fatal) OVERRIDE; | 151 bool fatal) OVERRIDE; |
150 virtual void OnResponseStarted(net::URLRequest* request) OVERRIDE; | 152 virtual void OnResponseStarted(URLRequest* request) OVERRIDE; |
151 virtual void OnReadCompleted(net::URLRequest* request, | 153 virtual void OnReadCompleted(URLRequest* request, |
152 int bytes_read) OVERRIDE; | 154 int bytes_read) OVERRIDE; |
153 | 155 |
154 private: | 156 private: |
155 static const int kBufferSize = 4096; | 157 static const int kBufferSize = 4096; |
156 | 158 |
157 virtual void OnResponseCompleted(net::URLRequest* request); | 159 virtual void OnResponseCompleted(URLRequest* request); |
158 | 160 |
159 // options for controlling behavior | 161 // options for controlling behavior |
160 bool cancel_in_rr_; | 162 bool cancel_in_rr_; |
161 bool cancel_in_rs_; | 163 bool cancel_in_rs_; |
162 bool cancel_in_rd_; | 164 bool cancel_in_rd_; |
163 bool cancel_in_rd_pending_; | 165 bool cancel_in_rd_pending_; |
164 bool quit_on_complete_; | 166 bool quit_on_complete_; |
165 bool quit_on_redirect_; | 167 bool quit_on_redirect_; |
166 bool allow_certificate_errors_; | 168 bool allow_certificate_errors_; |
167 net::AuthCredentials credentials_; | 169 AuthCredentials credentials_; |
168 | 170 |
169 // tracks status of callbacks | 171 // tracks status of callbacks |
170 int response_started_count_; | 172 int response_started_count_; |
171 int received_bytes_count_; | 173 int received_bytes_count_; |
172 int received_redirect_count_; | 174 int received_redirect_count_; |
173 bool received_data_before_response_; | 175 bool received_data_before_response_; |
174 bool request_failed_; | 176 bool request_failed_; |
175 bool have_certificate_errors_; | 177 bool have_certificate_errors_; |
176 bool certificate_errors_are_fatal_; | 178 bool certificate_errors_are_fatal_; |
177 bool auth_required_; | 179 bool auth_required_; |
178 std::string data_received_; | 180 std::string data_received_; |
179 | 181 |
180 // our read buffer | 182 // our read buffer |
181 scoped_refptr<net::IOBuffer> buf_; | 183 scoped_refptr<IOBuffer> buf_; |
182 }; | 184 }; |
183 | 185 |
184 //----------------------------------------------------------------------------- | 186 //----------------------------------------------------------------------------- |
185 | 187 |
186 class TestNetworkDelegate : public net::NetworkDelegate { | 188 class TestNetworkDelegate : public NetworkDelegate { |
187 public: | 189 public: |
188 enum Options { | 190 enum Options { |
189 NO_GET_COOKIES = 1 << 0, | 191 NO_GET_COOKIES = 1 << 0, |
190 NO_SET_COOKIE = 1 << 1, | 192 NO_SET_COOKIE = 1 << 1, |
191 }; | 193 }; |
192 | 194 |
193 TestNetworkDelegate(); | 195 TestNetworkDelegate(); |
194 virtual ~TestNetworkDelegate(); | 196 virtual ~TestNetworkDelegate(); |
195 | 197 |
196 void set_cookie_options(int o) {cookie_options_bit_mask_ = o; } | 198 void set_cookie_options(int o) {cookie_options_bit_mask_ = o; } |
197 | 199 |
198 int last_error() const { return last_error_; } | 200 int last_error() const { return last_error_; } |
199 int error_count() const { return error_count_; } | 201 int error_count() const { return error_count_; } |
200 int created_requests() const { return created_requests_; } | 202 int created_requests() const { return created_requests_; } |
201 int destroyed_requests() const { return destroyed_requests_; } | 203 int destroyed_requests() const { return destroyed_requests_; } |
202 int completed_requests() const { return completed_requests_; } | 204 int completed_requests() const { return completed_requests_; } |
203 int blocked_get_cookies_count() const { return blocked_get_cookies_count_; } | 205 int blocked_get_cookies_count() const { return blocked_get_cookies_count_; } |
204 int blocked_set_cookie_count() const { return blocked_set_cookie_count_; } | 206 int blocked_set_cookie_count() const { return blocked_set_cookie_count_; } |
205 int set_cookie_count() const { return set_cookie_count_; } | 207 int set_cookie_count() const { return set_cookie_count_; } |
206 | 208 |
207 protected: | 209 protected: |
208 // net::NetworkDelegate: | 210 // NetworkDelegate: |
209 virtual int OnBeforeURLRequest(net::URLRequest* request, | 211 virtual int OnBeforeURLRequest(URLRequest* request, |
210 const net::CompletionCallback& callback, | 212 const CompletionCallback& callback, |
211 GURL* new_url) OVERRIDE; | 213 GURL* new_url) OVERRIDE; |
212 virtual int OnBeforeSendHeaders(net::URLRequest* request, | 214 virtual int OnBeforeSendHeaders(URLRequest* request, |
213 const net::CompletionCallback& callback, | 215 const CompletionCallback& callback, |
214 net::HttpRequestHeaders* headers) OVERRIDE; | 216 HttpRequestHeaders* headers) OVERRIDE; |
215 virtual void OnSendHeaders(net::URLRequest* request, | 217 virtual void OnSendHeaders(URLRequest* request, |
216 const net::HttpRequestHeaders& headers) OVERRIDE; | 218 const HttpRequestHeaders& headers) OVERRIDE; |
217 virtual int OnHeadersReceived( | 219 virtual int OnHeadersReceived( |
218 net::URLRequest* request, | 220 URLRequest* request, |
219 const net::CompletionCallback& callback, | 221 const CompletionCallback& callback, |
220 const net::HttpResponseHeaders* original_response_headers, | 222 const HttpResponseHeaders* original_response_headers, |
221 scoped_refptr<net::HttpResponseHeaders>* override_response_headers) | 223 scoped_refptr<HttpResponseHeaders>* override_response_headers) |
222 OVERRIDE; | 224 OVERRIDE; |
wtc
2012/11/12 21:50:49
This may fit on the previous line now.
jochen (gone - plz use gerrit)
2012/11/13 09:17:57
Done.
| |
223 virtual void OnBeforeRedirect(net::URLRequest* request, | 225 virtual void OnBeforeRedirect(URLRequest* request, |
224 const GURL& new_location) OVERRIDE; | 226 const GURL& new_location) OVERRIDE; |
225 virtual void OnResponseStarted(net::URLRequest* request) OVERRIDE; | 227 virtual void OnResponseStarted(URLRequest* request) OVERRIDE; |
226 virtual void OnRawBytesRead(const net::URLRequest& request, | 228 virtual void OnRawBytesRead(const URLRequest& request, |
227 int bytes_read) OVERRIDE; | 229 int bytes_read) OVERRIDE; |
228 virtual void OnCompleted(net::URLRequest* request, bool started) OVERRIDE; | 230 virtual void OnCompleted(URLRequest* request, bool started) OVERRIDE; |
229 virtual void OnURLRequestDestroyed(net::URLRequest* request) OVERRIDE; | 231 virtual void OnURLRequestDestroyed(URLRequest* request) OVERRIDE; |
230 virtual void OnPACScriptError(int line_number, | 232 virtual void OnPACScriptError(int line_number, |
231 const string16& error) OVERRIDE; | 233 const string16& error) OVERRIDE; |
232 virtual net::NetworkDelegate::AuthRequiredResponse OnAuthRequired( | 234 virtual NetworkDelegate::AuthRequiredResponse OnAuthRequired( |
233 net::URLRequest* request, | 235 URLRequest* request, |
234 const net::AuthChallengeInfo& auth_info, | 236 const AuthChallengeInfo& auth_info, |
235 const AuthCallback& callback, | 237 const AuthCallback& callback, |
236 net::AuthCredentials* credentials) OVERRIDE; | 238 AuthCredentials* credentials) OVERRIDE; |
237 virtual bool OnCanGetCookies(const net::URLRequest& request, | 239 virtual bool OnCanGetCookies(const URLRequest& request, |
238 const net::CookieList& cookie_list) OVERRIDE; | 240 const CookieList& cookie_list) OVERRIDE; |
239 virtual bool OnCanSetCookie(const net::URLRequest& request, | 241 virtual bool OnCanSetCookie(const URLRequest& request, |
240 const std::string& cookie_line, | 242 const std::string& cookie_line, |
241 net::CookieOptions* options) OVERRIDE; | 243 CookieOptions* options) OVERRIDE; |
242 virtual bool OnCanAccessFile(const net::URLRequest& request, | 244 virtual bool OnCanAccessFile(const URLRequest& request, |
243 const FilePath& path) const OVERRIDE; | 245 const FilePath& path) const OVERRIDE; |
244 virtual bool OnCanThrottleRequest( | 246 virtual bool OnCanThrottleRequest( |
245 const net::URLRequest& request) const OVERRIDE; | 247 const URLRequest& request) const OVERRIDE; |
246 virtual int OnBeforeSocketStreamConnect( | 248 virtual int OnBeforeSocketStreamConnect( |
247 net::SocketStream* stream, | 249 SocketStream* stream, |
248 const net::CompletionCallback& callback) OVERRIDE; | 250 const CompletionCallback& callback) OVERRIDE; |
249 virtual void OnRequestWaitStateChange(const net::URLRequest& request, | 251 virtual void OnRequestWaitStateChange(const URLRequest& request, |
250 RequestWaitState state) OVERRIDE; | 252 RequestWaitState state) OVERRIDE; |
251 | 253 |
252 void InitRequestStatesIfNew(int request_id); | 254 void InitRequestStatesIfNew(int request_id); |
253 | 255 |
254 int last_error_; | 256 int last_error_; |
255 int error_count_; | 257 int error_count_; |
256 int created_requests_; | 258 int created_requests_; |
257 int destroyed_requests_; | 259 int destroyed_requests_; |
258 int completed_requests_; | 260 int completed_requests_; |
259 int cookie_options_bit_mask_; | 261 int cookie_options_bit_mask_; |
260 int blocked_get_cookies_count_; | 262 int blocked_get_cookies_count_; |
261 int blocked_set_cookie_count_; | 263 int blocked_set_cookie_count_; |
262 int set_cookie_count_; | 264 int set_cookie_count_; |
263 | 265 |
264 // net::NetworkDelegate callbacks happen in a particular order (e.g. | 266 // NetworkDelegate callbacks happen in a particular order (e.g. |
265 // OnBeforeURLRequest is always called before OnBeforeSendHeaders). | 267 // OnBeforeURLRequest is always called before OnBeforeSendHeaders). |
266 // This bit-set indicates for each request id (key) what events may be sent | 268 // This bit-set indicates for each request id (key) what events may be sent |
267 // next. | 269 // next. |
268 std::map<int, int> next_states_; | 270 std::map<int, int> next_states_; |
269 | 271 |
270 // A log that records for each request id (key) the order in which On... | 272 // A log that records for each request id (key) the order in which On... |
271 // functions were called. | 273 // functions were called. |
272 std::map<int, std::string> event_order_; | 274 std::map<int, std::string> event_order_; |
273 }; | 275 }; |
274 | 276 |
(...skipping 18 matching lines...) Expand all Loading... | |
293 static std::string value_; | 295 static std::string value_; |
294 const std::string old_value_; | 296 const std::string old_value_; |
295 const std::string new_value_; | 297 const std::string new_value_; |
296 | 298 |
297 DISALLOW_COPY_AND_ASSIGN(ScopedCustomUrlRequestTestHttpHost); | 299 DISALLOW_COPY_AND_ASSIGN(ScopedCustomUrlRequestTestHttpHost); |
298 }; | 300 }; |
299 | 301 |
300 //----------------------------------------------------------------------------- | 302 //----------------------------------------------------------------------------- |
301 | 303 |
302 // A simple Interceptor that returns a pre-built URLRequestJob only once. | 304 // A simple Interceptor that returns a pre-built URLRequestJob only once. |
303 class TestJobInterceptor : public net::URLRequestJobFactory::Interceptor { | 305 class TestJobInterceptor : public URLRequestJobFactory::Interceptor { |
304 public: | 306 public: |
305 TestJobInterceptor(); | 307 TestJobInterceptor(); |
306 | 308 |
307 virtual net::URLRequestJob* MaybeIntercept( | 309 virtual URLRequestJob* MaybeIntercept( |
308 net::URLRequest* request, | 310 URLRequest* request, |
309 net::NetworkDelegate* network_delegate) const OVERRIDE; | 311 NetworkDelegate* network_delegate) const OVERRIDE; |
310 virtual net::URLRequestJob* MaybeInterceptRedirect( | 312 virtual URLRequestJob* MaybeInterceptRedirect( |
311 const GURL& location, | 313 const GURL& location, |
312 net::URLRequest* request, | 314 URLRequest* request, |
313 net::NetworkDelegate* network_delegate) const OVERRIDE; | 315 NetworkDelegate* network_delegate) const OVERRIDE; |
314 virtual net::URLRequestJob* MaybeInterceptResponse( | 316 virtual URLRequestJob* MaybeInterceptResponse( |
315 net::URLRequest* request, | 317 URLRequest* request, |
316 net::NetworkDelegate* network_delegate) const OVERRIDE; | 318 NetworkDelegate* network_delegate) const OVERRIDE; |
317 void set_main_intercept_job(net::URLRequestJob* job); | 319 void set_main_intercept_job(URLRequestJob* job); |
318 | 320 |
319 private: | 321 private: |
320 mutable net::URLRequestJob* main_intercept_job_; | 322 mutable URLRequestJob* main_intercept_job_; |
321 }; | 323 }; |
322 | 324 |
325 } // namespace net | |
326 | |
323 #endif // NET_URL_REQUEST_URL_REQUEST_TEST_UTIL_H_ | 327 #endif // NET_URL_REQUEST_URL_REQUEST_TEST_UTIL_H_ |
OLD | NEW |