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_impl.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 ~TestURLRequestContext() override
;
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 scoped_ptr
<HttpNetworkSession::Params
> params
) {
74 http_network_session_params_
= params
.Pass();
77 void SetSdchManager(scoped_ptr
<SdchManager
> sdch_manager
) {
78 context_storage_
.set_sdch_manager(sdch_manager
.Pass());
84 // Optional parameters to override default values. Note that values that
85 // point to other objects the TestURLRequestContext creates will be
87 scoped_ptr
<HttpNetworkSession::Params
> http_network_session_params_
;
90 ClientSocketFactory
* client_socket_factory_
;
93 URLRequestContextStorage context_storage_
;
96 //-----------------------------------------------------------------------------
98 // Used to return a dummy context, which lives on the message loop
99 // given in the constructor.
100 class TestURLRequestContextGetter
: public URLRequestContextGetter
{
102 // |network_task_runner| must not be NULL.
103 explicit TestURLRequestContextGetter(
104 const scoped_refptr
<base::SingleThreadTaskRunner
>& network_task_runner
);
106 // Use to pass a pre-initialized |context|.
107 TestURLRequestContextGetter(
108 const scoped_refptr
<base::SingleThreadTaskRunner
>& network_task_runner
,
109 scoped_ptr
<TestURLRequestContext
> context
);
111 // URLRequestContextGetter implementation.
112 TestURLRequestContext
* GetURLRequestContext() override
;
113 scoped_refptr
<base::SingleThreadTaskRunner
> GetNetworkTaskRunner()
117 ~TestURLRequestContextGetter() override
;
120 const scoped_refptr
<base::SingleThreadTaskRunner
> network_task_runner_
;
121 scoped_ptr
<TestURLRequestContext
> context_
;
124 //-----------------------------------------------------------------------------
126 class TestDelegate
: public URLRequest::Delegate
{
129 ~TestDelegate() override
;
131 void set_cancel_in_received_redirect(bool val
) { cancel_in_rr_
= val
; }
132 void set_cancel_in_response_started(bool val
) { cancel_in_rs_
= val
; }
133 void set_cancel_in_received_data(bool val
) { cancel_in_rd_
= val
; }
134 void set_cancel_in_received_data_pending(bool val
) {
135 cancel_in_rd_pending_
= val
;
137 void set_quit_on_complete(bool val
) { quit_on_complete_
= val
; }
138 void set_quit_on_redirect(bool val
) { quit_on_redirect_
= val
; }
139 void set_quit_on_network_start(bool val
) {
140 quit_on_before_network_start_
= val
;
142 void set_allow_certificate_errors(bool val
) {
143 allow_certificate_errors_
= val
;
145 void set_credentials(const AuthCredentials
& credentials
) {
146 credentials_
= credentials
;
150 const std::string
& data_received() const { return data_received_
; }
151 int bytes_received() const { return static_cast<int>(data_received_
.size()); }
152 int response_started_count() const { return response_started_count_
; }
153 int received_redirect_count() const { return received_redirect_count_
; }
154 int received_before_network_start_count() const {
155 return received_before_network_start_count_
;
157 bool received_data_before_response() const {
158 return received_data_before_response_
;
160 bool request_failed() const { return request_failed_
; }
161 bool have_certificate_errors() const { return have_certificate_errors_
; }
162 bool certificate_errors_are_fatal() const {
163 return certificate_errors_are_fatal_
;
165 bool auth_required_called() const { return auth_required_
; }
166 bool have_full_request_headers() const { return have_full_request_headers_
; }
167 const HttpRequestHeaders
& full_request_headers() const {
168 return full_request_headers_
;
170 void ClearFullRequestHeaders();
172 // URLRequest::Delegate:
173 void OnReceivedRedirect(URLRequest
* request
,
174 const RedirectInfo
& redirect_info
,
175 bool* defer_redirect
) override
;
176 void OnBeforeNetworkStart(URLRequest
* request
, bool* defer
) override
;
177 void OnAuthRequired(URLRequest
* request
,
178 AuthChallengeInfo
* auth_info
) override
;
179 // NOTE: |fatal| causes |certificate_errors_are_fatal_| to be set to true.
180 // (Unit tests use this as a post-condition.) But for policy, this method
181 // consults |allow_certificate_errors_|.
182 void OnSSLCertificateError(URLRequest
* request
,
183 const SSLInfo
& ssl_info
,
184 bool fatal
) override
;
185 void OnResponseStarted(URLRequest
* request
) override
;
186 void OnReadCompleted(URLRequest
* request
, int bytes_read
) override
;
189 static const int kBufferSize
= 4096;
191 virtual void OnResponseCompleted(URLRequest
* request
);
193 // options for controlling behavior
197 bool cancel_in_rd_pending_
;
198 bool quit_on_complete_
;
199 bool quit_on_redirect_
;
200 bool quit_on_before_network_start_
;
201 bool allow_certificate_errors_
;
202 AuthCredentials credentials_
;
204 // tracks status of callbacks
205 int response_started_count_
;
206 int received_bytes_count_
;
207 int received_redirect_count_
;
208 int received_before_network_start_count_
;
209 bool received_data_before_response_
;
210 bool request_failed_
;
211 bool have_certificate_errors_
;
212 bool certificate_errors_are_fatal_
;
214 std::string data_received_
;
215 bool have_full_request_headers_
;
216 HttpRequestHeaders full_request_headers_
;
219 scoped_refptr
<IOBuffer
> buf_
;
222 //-----------------------------------------------------------------------------
224 class TestNetworkDelegate
: public NetworkDelegateImpl
{
227 NO_GET_COOKIES
= 1 << 0,
228 NO_SET_COOKIE
= 1 << 1,
231 TestNetworkDelegate();
232 ~TestNetworkDelegate() override
;
234 // Writes the LoadTimingInfo during the most recent call to OnBeforeRedirect.
235 bool GetLoadTimingInfoBeforeRedirect(
236 LoadTimingInfo
* load_timing_info_before_redirect
) const;
238 // Same as GetLoadTimingInfoBeforeRedirect, except for calls to
239 // AuthRequiredResponse.
240 bool GetLoadTimingInfoBeforeAuth(
241 LoadTimingInfo
* load_timing_info_before_auth
) const;
243 // Will redirect once to the given URL when the next set of headers are
245 void set_redirect_on_headers_received_url(
246 GURL redirect_on_headers_received_url
) {
247 redirect_on_headers_received_url_
= redirect_on_headers_received_url
;
250 void set_allowed_unsafe_redirect_url(GURL allowed_unsafe_redirect_url
) {
251 allowed_unsafe_redirect_url_
= allowed_unsafe_redirect_url
;
254 void set_cookie_options(int o
) {cookie_options_bit_mask_
= o
; }
256 int last_error() const { return last_error_
; }
257 int error_count() const { return error_count_
; }
258 int created_requests() const { return created_requests_
; }
259 int destroyed_requests() const { return destroyed_requests_
; }
260 int completed_requests() const { return completed_requests_
; }
261 int canceled_requests() const { return canceled_requests_
; }
262 int blocked_get_cookies_count() const { return blocked_get_cookies_count_
; }
263 int blocked_set_cookie_count() const { return blocked_set_cookie_count_
; }
264 int set_cookie_count() const { return set_cookie_count_
; }
266 void set_can_access_files(bool val
) { can_access_files_
= val
; }
267 bool can_access_files() const { return can_access_files_
; }
269 void set_first_party_only_cookies_enabled(bool val
) {
270 first_party_only_cookies_enabled_
= val
;
273 void set_can_throttle_requests(bool val
) { can_throttle_requests_
= val
; }
274 bool can_throttle_requests() const { return can_throttle_requests_
; }
276 void set_cancel_request_with_policy_violating_referrer(bool val
) {
277 cancel_request_with_policy_violating_referrer_
= val
;
280 int observed_before_proxy_headers_sent_callbacks() const {
281 return observed_before_proxy_headers_sent_callbacks_
;
283 int before_send_headers_count() const { return before_send_headers_count_
; }
284 int headers_received_count() const { return headers_received_count_
; }
286 // Last observed proxy in proxy header sent callback.
287 HostPortPair
last_observed_proxy() {
288 return last_observed_proxy_
;
291 void set_can_be_intercepted_on_error(bool can_be_intercepted_on_error
) {
292 will_be_intercepted_on_next_error_
= can_be_intercepted_on_error
;
297 int OnBeforeURLRequest(URLRequest
* request
,
298 const CompletionCallback
& callback
,
299 GURL
* new_url
) override
;
300 int OnBeforeSendHeaders(URLRequest
* request
,
301 const CompletionCallback
& callback
,
302 HttpRequestHeaders
* headers
) override
;
303 void OnBeforeSendProxyHeaders(URLRequest
* request
,
304 const ProxyInfo
& proxy_info
,
305 HttpRequestHeaders
* headers
) override
;
306 void OnSendHeaders(URLRequest
* request
,
307 const HttpRequestHeaders
& headers
) override
;
308 int OnHeadersReceived(
310 const CompletionCallback
& callback
,
311 const HttpResponseHeaders
* original_response_headers
,
312 scoped_refptr
<HttpResponseHeaders
>* override_response_headers
,
313 GURL
* allowed_unsafe_redirect_url
) override
;
314 void OnBeforeRedirect(URLRequest
* request
, const GURL
& new_location
) override
;
315 void OnResponseStarted(URLRequest
* request
) override
;
316 void OnRawBytesRead(const URLRequest
& request
, int bytes_read
) override
;
317 void OnCompleted(URLRequest
* request
, bool started
) override
;
318 void OnURLRequestDestroyed(URLRequest
* request
) override
;
319 void OnPACScriptError(int line_number
, const base::string16
& error
) override
;
320 NetworkDelegate::AuthRequiredResponse
OnAuthRequired(
322 const AuthChallengeInfo
& auth_info
,
323 const AuthCallback
& callback
,
324 AuthCredentials
* credentials
) override
;
325 bool OnCanGetCookies(const URLRequest
& request
,
326 const CookieList
& cookie_list
) override
;
327 bool OnCanSetCookie(const URLRequest
& request
,
328 const std::string
& cookie_line
,
329 CookieOptions
* options
) override
;
330 bool OnCanAccessFile(const URLRequest
& request
,
331 const base::FilePath
& path
) const override
;
332 bool OnCanThrottleRequest(const URLRequest
& request
) const override
;
333 bool OnFirstPartyOnlyCookieExperimentEnabled() const override
;
334 bool OnCancelURLRequestWithPolicyViolatingReferrerHeader(
335 const URLRequest
& request
,
336 const GURL
& target_url
,
337 const GURL
& referrer_url
) const 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 int before_send_headers_count_
;
357 int headers_received_count_
;
358 // Last observed proxy in before proxy header sent callback.
359 HostPortPair last_observed_proxy_
;
361 // NetworkDelegate callbacks happen in a particular order (e.g.
362 // OnBeforeURLRequest is always called before OnBeforeSendHeaders).
363 // This bit-set indicates for each request id (key) what events may be sent
365 std::map
<int, int> next_states_
;
367 // A log that records for each request id (key) the order in which On...
368 // functions were called.
369 std::map
<int, std::string
> event_order_
;
371 LoadTimingInfo load_timing_info_before_redirect_
;
372 bool has_load_timing_info_before_redirect_
;
374 LoadTimingInfo load_timing_info_before_auth_
;
375 bool has_load_timing_info_before_auth_
;
377 bool can_access_files_
; // true by default
378 bool can_throttle_requests_
; // true by default
379 bool first_party_only_cookies_enabled_
; // false by default
380 bool cancel_request_with_policy_violating_referrer_
; // false by default
381 bool will_be_intercepted_on_next_error_
;
384 //-----------------------------------------------------------------------------
386 // A simple ProtocolHandler that returns a pre-built URLRequestJob only once.
387 class TestJobInterceptor
: public URLRequestJobFactory::ProtocolHandler
{
389 TestJobInterceptor();
391 URLRequestJob
* MaybeCreateJob(
393 NetworkDelegate
* network_delegate
) const override
;
394 void set_main_intercept_job(URLRequestJob
* job
);
397 mutable URLRequestJob
* main_intercept_job_
;
402 #endif // NET_URL_REQUEST_URL_REQUEST_TEST_UTIL_H_