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.
5 #ifndef NET_URL_REQUEST_URL_REQUEST_TEST_UTIL_H_
6 #define NET_URL_REQUEST_URL_REQUEST_TEST_UTIL_H_
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.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"
47 using base::TimeDelta
;
51 //-----------------------------------------------------------------------------
53 class TestURLRequestContext
: public URLRequestContext
{
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();
65 ClientSocketFactory
* client_socket_factory() {
66 return client_socket_factory_
;
68 void set_client_socket_factory(ClientSocketFactory
* factory
) {
69 client_socket_factory_
= factory
;
72 void set_http_network_session_params(
73 const HttpNetworkSession::Params
& params
) {
76 void SetSdchManager(scoped_ptr
<SdchManager
> sdch_manager
) {
77 context_storage_
.set_sdch_manager(sdch_manager
.Pass());
83 // Optional parameters to override default values. Note that values that
84 // point to other objects the TestURLRequestContext creates will be
86 scoped_ptr
<HttpNetworkSession::Params
> http_network_session_params_
;
89 ClientSocketFactory
* client_socket_factory_
;
92 URLRequestContextStorage context_storage_
;
95 //-----------------------------------------------------------------------------
97 // Used to return a dummy context, which lives on the message loop
98 // given in the constructor.
99 class TestURLRequestContextGetter
: public URLRequestContextGetter
{
101 // |network_task_runner| must not be NULL.
102 explicit TestURLRequestContextGetter(
103 const scoped_refptr
<base::SingleThreadTaskRunner
>& network_task_runner
);
105 // Use to pass a pre-initialized |context|.
106 TestURLRequestContextGetter(
107 const scoped_refptr
<base::SingleThreadTaskRunner
>& network_task_runner
,
108 scoped_ptr
<TestURLRequestContext
> context
);
110 // URLRequestContextGetter implementation.
111 virtual TestURLRequestContext
* GetURLRequestContext() OVERRIDE
;
112 virtual scoped_refptr
<base::SingleThreadTaskRunner
>
113 GetNetworkTaskRunner() const OVERRIDE
;
116 virtual ~TestURLRequestContextGetter();
119 const scoped_refptr
<base::SingleThreadTaskRunner
> network_task_runner_
;
120 scoped_ptr
<TestURLRequestContext
> context_
;
123 //-----------------------------------------------------------------------------
125 class TestURLRequest
: public URLRequest
{
127 TestURLRequest(const GURL
& url
,
128 RequestPriority priority
,
130 TestURLRequestContext
* context
);
131 virtual ~TestURLRequest();
134 //-----------------------------------------------------------------------------
136 class TestDelegate
: public URLRequest::Delegate
{
139 virtual ~TestDelegate();
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
;
147 void set_quit_on_complete(bool val
) { quit_on_complete_
= val
; }
148 void set_quit_on_redirect(bool val
) { quit_on_redirect_
= val
; }
149 void set_quit_on_network_start(bool val
) {
150 quit_on_before_network_start_
= val
;
152 void set_allow_certificate_errors(bool val
) {
153 allow_certificate_errors_
= val
;
155 void set_credentials(const AuthCredentials
& credentials
) {
156 credentials_
= credentials
;
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_
; }
164 int received_before_network_start_count() const {
165 return received_before_network_start_count_
;
167 bool received_data_before_response() const {
168 return received_data_before_response_
;
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_
;
175 bool auth_required_called() const { return auth_required_
; }
176 bool have_full_request_headers() const { return have_full_request_headers_
; }
177 const HttpRequestHeaders
& full_request_headers() const {
178 return full_request_headers_
;
180 void ClearFullRequestHeaders();
182 // URLRequest::Delegate:
183 virtual void OnReceivedRedirect(URLRequest
* request
,
184 const RedirectInfo
& redirect_info
,
185 bool* defer_redirect
) OVERRIDE
;
186 virtual void OnBeforeNetworkStart(URLRequest
* request
, bool* defer
) OVERRIDE
;
187 virtual void OnAuthRequired(URLRequest
* request
,
188 AuthChallengeInfo
* auth_info
) OVERRIDE
;
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_|.
192 virtual void OnSSLCertificateError(URLRequest
* request
,
193 const SSLInfo
& ssl_info
,
194 bool fatal
) OVERRIDE
;
195 virtual void OnResponseStarted(URLRequest
* request
) OVERRIDE
;
196 virtual void OnReadCompleted(URLRequest
* request
,
197 int bytes_read
) OVERRIDE
;
200 static const int kBufferSize
= 4096;
202 virtual void OnResponseCompleted(URLRequest
* request
);
204 // options for controlling behavior
208 bool cancel_in_rd_pending_
;
209 bool quit_on_complete_
;
210 bool quit_on_redirect_
;
211 bool quit_on_before_network_start_
;
212 bool allow_certificate_errors_
;
213 AuthCredentials credentials_
;
215 // tracks status of callbacks
216 int response_started_count_
;
217 int received_bytes_count_
;
218 int received_redirect_count_
;
219 int received_before_network_start_count_
;
220 bool received_data_before_response_
;
221 bool request_failed_
;
222 bool have_certificate_errors_
;
223 bool certificate_errors_are_fatal_
;
225 std::string data_received_
;
226 bool have_full_request_headers_
;
227 HttpRequestHeaders full_request_headers_
;
230 scoped_refptr
<IOBuffer
> buf_
;
233 //-----------------------------------------------------------------------------
235 class TestNetworkDelegate
: public NetworkDelegate
{
238 NO_GET_COOKIES
= 1 << 0,
239 NO_SET_COOKIE
= 1 << 1,
242 TestNetworkDelegate();
243 virtual ~TestNetworkDelegate();
245 // Writes the LoadTimingInfo during the most recent call to OnBeforeRedirect.
246 bool GetLoadTimingInfoBeforeRedirect(
247 LoadTimingInfo
* load_timing_info_before_redirect
) const;
249 // Same as GetLoadTimingInfoBeforeRedirect, except for calls to
250 // AuthRequiredResponse.
251 bool GetLoadTimingInfoBeforeAuth(
252 LoadTimingInfo
* load_timing_info_before_auth
) const;
254 // Will redirect once to the given URL when the next set of headers are
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
;
261 void set_allowed_unsafe_redirect_url(GURL allowed_unsafe_redirect_url
) {
262 allowed_unsafe_redirect_url_
= allowed_unsafe_redirect_url
;
265 void set_cookie_options(int o
) {cookie_options_bit_mask_
= o
; }
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_
; }
272 int canceled_requests() const { return canceled_requests_
; }
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_
; }
277 void set_can_access_files(bool val
) { can_access_files_
= val
; }
278 bool can_access_files() const { return can_access_files_
; }
280 void set_can_throttle_requests(bool val
) { can_throttle_requests_
= val
; }
281 bool can_throttle_requests() const { return can_throttle_requests_
; }
283 int observed_before_proxy_headers_sent_callbacks() const {
284 return observed_before_proxy_headers_sent_callbacks_
;
287 // Last observed proxy in proxy header sent callback.
288 HostPortPair
last_observed_proxy() {
289 return last_observed_proxy_
;
294 virtual int OnBeforeURLRequest(URLRequest
* request
,
295 const CompletionCallback
& callback
,
296 GURL
* new_url
) OVERRIDE
;
297 virtual int OnBeforeSendHeaders(URLRequest
* request
,
298 const CompletionCallback
& callback
,
299 HttpRequestHeaders
* headers
) OVERRIDE
;
300 virtual void OnBeforeSendProxyHeaders(
301 net::URLRequest
* request
,
302 const net::ProxyInfo
& proxy_info
,
303 net::HttpRequestHeaders
* headers
) OVERRIDE
;
304 virtual void OnSendHeaders(URLRequest
* request
,
305 const HttpRequestHeaders
& headers
) OVERRIDE
;
306 virtual int OnHeadersReceived(
308 const CompletionCallback
& callback
,
309 const HttpResponseHeaders
* original_response_headers
,
310 scoped_refptr
<HttpResponseHeaders
>* override_response_headers
,
311 GURL
* allowed_unsafe_redirect_url
) OVERRIDE
;
312 virtual void OnBeforeRedirect(URLRequest
* request
,
313 const GURL
& new_location
) OVERRIDE
;
314 virtual void OnResponseStarted(URLRequest
* request
) OVERRIDE
;
315 virtual void OnRawBytesRead(const URLRequest
& request
,
316 int bytes_read
) OVERRIDE
;
317 virtual void OnCompleted(URLRequest
* request
, bool started
) OVERRIDE
;
318 virtual void OnURLRequestDestroyed(URLRequest
* request
) OVERRIDE
;
319 virtual void OnPACScriptError(int line_number
,
320 const base::string16
& error
) OVERRIDE
;
321 virtual NetworkDelegate::AuthRequiredResponse
OnAuthRequired(
323 const AuthChallengeInfo
& auth_info
,
324 const AuthCallback
& callback
,
325 AuthCredentials
* credentials
) OVERRIDE
;
326 virtual bool OnCanGetCookies(const URLRequest
& request
,
327 const CookieList
& cookie_list
) OVERRIDE
;
328 virtual bool OnCanSetCookie(const URLRequest
& request
,
329 const std::string
& cookie_line
,
330 CookieOptions
* options
) OVERRIDE
;
331 virtual bool OnCanAccessFile(const URLRequest
& request
,
332 const base::FilePath
& path
) const OVERRIDE
;
333 virtual bool OnCanThrottleRequest(
334 const URLRequest
& request
) const OVERRIDE
;
335 virtual int OnBeforeSocketStreamConnect(
336 SocketStream
* stream
,
337 const CompletionCallback
& callback
) OVERRIDE
;
339 void InitRequestStatesIfNew(int request_id
);
341 GURL redirect_on_headers_received_url_
;
342 // URL marked as safe for redirection at the onHeadersReceived stage.
343 GURL allowed_unsafe_redirect_url_
;
347 int created_requests_
;
348 int destroyed_requests_
;
349 int completed_requests_
;
350 int canceled_requests_
;
351 int cookie_options_bit_mask_
;
352 int blocked_get_cookies_count_
;
353 int blocked_set_cookie_count_
;
354 int set_cookie_count_
;
355 int observed_before_proxy_headers_sent_callbacks_
;
356 // Last observed proxy in before proxy header sent callback.
357 HostPortPair last_observed_proxy_
;
359 // NetworkDelegate callbacks happen in a particular order (e.g.
360 // OnBeforeURLRequest is always called before OnBeforeSendHeaders).
361 // This bit-set indicates for each request id (key) what events may be sent
363 std::map
<int, int> next_states_
;
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_
;
369 LoadTimingInfo load_timing_info_before_redirect_
;
370 bool has_load_timing_info_before_redirect_
;
372 LoadTimingInfo load_timing_info_before_auth_
;
373 bool has_load_timing_info_before_auth_
;
375 bool can_access_files_
; // true by default
376 bool can_throttle_requests_
; // true by default
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
{
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
);
390 ~ScopedCustomUrlRequestTestHttpHost();
392 // Returns the current value to be used by HTTP tests in
393 // url_request_unittest.cc .
394 static const std::string
& value();
397 static std::string value_
;
398 const std::string old_value_
;
399 const std::string new_value_
;
401 DISALLOW_COPY_AND_ASSIGN(ScopedCustomUrlRequestTestHttpHost
);
404 //-----------------------------------------------------------------------------
406 // A simple ProtocolHandler that returns a pre-built URLRequestJob only once.
407 class TestJobInterceptor
: public URLRequestJobFactory::ProtocolHandler
{
409 TestJobInterceptor();
411 virtual URLRequestJob
* MaybeCreateJob(
413 NetworkDelegate
* network_delegate
) const OVERRIDE
;
414 void set_main_intercept_job(URLRequestJob
* job
);
417 mutable URLRequestJob
* main_intercept_job_
;
422 #endif // NET_URL_REQUEST_URL_REQUEST_TEST_UTIL_H_