Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / net / url_request / url_request_test_util.h
blob924635ee1ac35547a68c453c965a34ea85e33877
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_
8 #include <stdint.h>
9 #include <stdlib.h>
11 #include <map>
12 #include <string>
14 #include "base/basictypes.h"
15 #include "base/compiler_specific.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/path_service.h"
19 #include "base/single_thread_task_runner.h"
20 #include "base/strings/string16.h"
21 #include "base/strings/string_util.h"
22 #include "base/strings/utf_string_conversions.h"
23 #include "base/time/time.h"
24 #include "net/base/io_buffer.h"
25 #include "net/base/load_timing_info.h"
26 #include "net/base/net_errors.h"
27 #include "net/base/network_delegate_impl.h"
28 #include "net/base/request_priority.h"
29 #include "net/base/sdch_manager.h"
30 #include "net/cert/cert_verifier.h"
31 #include "net/cookies/cookie_monster.h"
32 #include "net/disk_cache/disk_cache.h"
33 #include "net/ftp/ftp_network_layer.h"
34 #include "net/http/http_auth_handler_factory.h"
35 #include "net/http/http_cache.h"
36 #include "net/http/http_network_layer.h"
37 #include "net/http/http_network_session.h"
38 #include "net/http/http_request_headers.h"
39 #include "net/proxy/proxy_service.h"
40 #include "net/ssl/ssl_config_service_defaults.h"
41 #include "net/url_request/url_request.h"
42 #include "net/url_request/url_request_context.h"
43 #include "net/url_request/url_request_context_getter.h"
44 #include "net/url_request/url_request_context_storage.h"
45 #include "net/url_request/url_request_job_factory.h"
46 #include "url/url_util.h"
48 using base::TimeDelta;
50 namespace net {
52 //-----------------------------------------------------------------------------
54 class TestURLRequestContext : public URLRequestContext {
55 public:
56 TestURLRequestContext();
57 // Default constructor like TestURLRequestContext() but does not call
58 // Init() in case |delay_initialization| is true. This allows modifying the
59 // URLRequestContext before it is constructed completely. If
60 // |delay_initialization| is true, Init() needs be be called manually.
61 explicit TestURLRequestContext(bool delay_initialization);
62 ~TestURLRequestContext() override;
64 void Init();
66 ClientSocketFactory* client_socket_factory() {
67 return client_socket_factory_;
69 void set_client_socket_factory(ClientSocketFactory* factory) {
70 client_socket_factory_ = factory;
73 void set_http_network_session_params(
74 scoped_ptr<HttpNetworkSession::Params> params) {
75 http_network_session_params_ = params.Pass();
78 void SetSdchManager(scoped_ptr<SdchManager> sdch_manager) {
79 context_storage_.set_sdch_manager(sdch_manager.Pass());
82 private:
83 bool initialized_;
85 // Optional parameters to override default values. Note that values that
86 // point to other objects the TestURLRequestContext creates will be
87 // overwritten.
88 scoped_ptr<HttpNetworkSession::Params> http_network_session_params_;
90 // Not owned:
91 ClientSocketFactory* client_socket_factory_;
93 protected:
94 URLRequestContextStorage context_storage_;
97 //-----------------------------------------------------------------------------
99 // Used to return a dummy context, which lives on the message loop
100 // given in the constructor.
101 class TestURLRequestContextGetter : public URLRequestContextGetter {
102 public:
103 // |network_task_runner| must not be NULL.
104 explicit TestURLRequestContextGetter(
105 const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner);
107 // Use to pass a pre-initialized |context|.
108 TestURLRequestContextGetter(
109 const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner,
110 scoped_ptr<TestURLRequestContext> context);
112 // URLRequestContextGetter implementation.
113 TestURLRequestContext* GetURLRequestContext() override;
114 scoped_refptr<base::SingleThreadTaskRunner> GetNetworkTaskRunner()
115 const override;
117 protected:
118 ~TestURLRequestContextGetter() override;
120 private:
121 const scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_;
122 scoped_ptr<TestURLRequestContext> context_;
125 //-----------------------------------------------------------------------------
127 class TestDelegate : public URLRequest::Delegate {
128 public:
129 TestDelegate();
130 ~TestDelegate() override;
132 void set_cancel_in_received_redirect(bool val) { cancel_in_rr_ = val; }
133 void set_cancel_in_response_started(bool val) { cancel_in_rs_ = val; }
134 void set_cancel_in_received_data(bool val) { cancel_in_rd_ = val; }
135 void set_cancel_in_received_data_pending(bool val) {
136 cancel_in_rd_pending_ = val;
139 void set_quit_on_complete(bool val) { quit_on_complete_ = val; }
140 void set_quit_on_redirect(bool val) { quit_on_redirect_ = val; }
141 // Enables quitting the message loop in response to auth requests, as opposed
142 // to returning credentials or cancelling the request.
143 void set_quit_on_auth_required(bool val) { quit_on_auth_required_ = val; }
144 void set_quit_on_network_start(bool val) {
145 quit_on_before_network_start_ = val;
148 void set_allow_certificate_errors(bool val) {
149 allow_certificate_errors_ = val;
151 void set_credentials(const AuthCredentials& credentials) {
152 credentials_ = credentials;
155 // query state
156 const std::string& data_received() const { return data_received_; }
157 int bytes_received() const { return static_cast<int>(data_received_.size()); }
158 int response_started_count() const { return response_started_count_; }
159 int received_redirect_count() const { return received_redirect_count_; }
160 int received_before_network_start_count() const {
161 return received_before_network_start_count_;
163 bool received_data_before_response() const {
164 return received_data_before_response_;
166 bool request_failed() const { return request_failed_; }
167 bool have_certificate_errors() const { return have_certificate_errors_; }
168 bool certificate_errors_are_fatal() const {
169 return certificate_errors_are_fatal_;
171 bool auth_required_called() const { return auth_required_; }
172 bool have_full_request_headers() const { return have_full_request_headers_; }
173 const HttpRequestHeaders& full_request_headers() const {
174 return full_request_headers_;
176 void ClearFullRequestHeaders();
178 // URLRequest::Delegate:
179 void OnReceivedRedirect(URLRequest* request,
180 const RedirectInfo& redirect_info,
181 bool* defer_redirect) override;
182 void OnBeforeNetworkStart(URLRequest* request, bool* defer) override;
183 void OnAuthRequired(URLRequest* request,
184 AuthChallengeInfo* auth_info) override;
185 // NOTE: |fatal| causes |certificate_errors_are_fatal_| to be set to true.
186 // (Unit tests use this as a post-condition.) But for policy, this method
187 // consults |allow_certificate_errors_|.
188 void OnSSLCertificateError(URLRequest* request,
189 const SSLInfo& ssl_info,
190 bool fatal) override;
191 void OnResponseStarted(URLRequest* request) override;
192 void OnReadCompleted(URLRequest* request, int bytes_read) override;
194 private:
195 static const int kBufferSize = 4096;
197 virtual void OnResponseCompleted(URLRequest* request);
199 // options for controlling behavior
200 bool cancel_in_rr_;
201 bool cancel_in_rs_;
202 bool cancel_in_rd_;
203 bool cancel_in_rd_pending_;
204 bool quit_on_complete_;
205 bool quit_on_redirect_;
206 bool quit_on_auth_required_;
207 bool quit_on_before_network_start_;
208 bool allow_certificate_errors_;
209 AuthCredentials credentials_;
211 // tracks status of callbacks
212 int response_started_count_;
213 int received_bytes_count_;
214 int received_redirect_count_;
215 int received_before_network_start_count_;
216 bool received_data_before_response_;
217 bool request_failed_;
218 bool have_certificate_errors_;
219 bool certificate_errors_are_fatal_;
220 bool auth_required_;
221 std::string data_received_;
222 bool have_full_request_headers_;
223 HttpRequestHeaders full_request_headers_;
225 // our read buffer
226 scoped_refptr<IOBuffer> buf_;
229 //-----------------------------------------------------------------------------
231 class TestNetworkDelegate : public NetworkDelegateImpl {
232 public:
233 enum Options {
234 NO_GET_COOKIES = 1 << 0,
235 NO_SET_COOKIE = 1 << 1,
238 TestNetworkDelegate();
239 ~TestNetworkDelegate() override;
241 // Writes the LoadTimingInfo during the most recent call to OnBeforeRedirect.
242 bool GetLoadTimingInfoBeforeRedirect(
243 LoadTimingInfo* load_timing_info_before_redirect) const;
245 // Same as GetLoadTimingInfoBeforeRedirect, except for calls to
246 // AuthRequiredResponse.
247 bool GetLoadTimingInfoBeforeAuth(
248 LoadTimingInfo* load_timing_info_before_auth) const;
250 // Will redirect once to the given URL when the next set of headers are
251 // received.
252 void set_redirect_on_headers_received_url(
253 GURL redirect_on_headers_received_url) {
254 redirect_on_headers_received_url_ = redirect_on_headers_received_url;
257 void set_allowed_unsafe_redirect_url(GURL allowed_unsafe_redirect_url) {
258 allowed_unsafe_redirect_url_ = allowed_unsafe_redirect_url;
261 void set_cookie_options(int o) {cookie_options_bit_mask_ = o; }
263 int last_error() const { return last_error_; }
264 int error_count() const { return error_count_; }
265 int created_requests() const { return created_requests_; }
266 int destroyed_requests() const { return destroyed_requests_; }
267 int completed_requests() const { return completed_requests_; }
268 int canceled_requests() const { return canceled_requests_; }
269 int blocked_get_cookies_count() const { return blocked_get_cookies_count_; }
270 int blocked_set_cookie_count() const { return blocked_set_cookie_count_; }
271 int set_cookie_count() const { return set_cookie_count_; }
273 void set_can_access_files(bool val) { can_access_files_ = val; }
274 bool can_access_files() const { return can_access_files_; }
276 void set_first_party_only_cookies_enabled(bool val) {
277 first_party_only_cookies_enabled_ = val;
280 void set_cancel_request_with_policy_violating_referrer(bool val) {
281 cancel_request_with_policy_violating_referrer_ = val;
284 int observed_before_proxy_headers_sent_callbacks() const {
285 return observed_before_proxy_headers_sent_callbacks_;
287 int before_send_headers_count() const { return before_send_headers_count_; }
288 int headers_received_count() const { return headers_received_count_; }
289 int64_t total_network_bytes_received() const {
290 return total_network_bytes_received_;
293 // Last observed proxy in proxy header sent callback.
294 HostPortPair last_observed_proxy() {
295 return last_observed_proxy_;
298 void set_can_be_intercepted_on_error(bool can_be_intercepted_on_error) {
299 will_be_intercepted_on_next_error_ = can_be_intercepted_on_error;
302 protected:
303 // NetworkDelegate:
304 int OnBeforeURLRequest(URLRequest* request,
305 const CompletionCallback& callback,
306 GURL* new_url) override;
307 int OnBeforeSendHeaders(URLRequest* request,
308 const CompletionCallback& callback,
309 HttpRequestHeaders* headers) override;
310 void OnBeforeSendProxyHeaders(URLRequest* request,
311 const ProxyInfo& proxy_info,
312 HttpRequestHeaders* headers) override;
313 void OnSendHeaders(URLRequest* request,
314 const HttpRequestHeaders& headers) override;
315 int OnHeadersReceived(
316 URLRequest* request,
317 const CompletionCallback& callback,
318 const HttpResponseHeaders* original_response_headers,
319 scoped_refptr<HttpResponseHeaders>* override_response_headers,
320 GURL* allowed_unsafe_redirect_url) override;
321 void OnBeforeRedirect(URLRequest* request, const GURL& new_location) override;
322 void OnResponseStarted(URLRequest* request) override;
323 void OnNetworkBytesReceived(const URLRequest& request,
324 int64_t bytes_received) override;
325 void OnCompleted(URLRequest* request, bool started) override;
326 void OnURLRequestDestroyed(URLRequest* request) override;
327 void OnPACScriptError(int line_number, const base::string16& error) override;
328 NetworkDelegate::AuthRequiredResponse OnAuthRequired(
329 URLRequest* request,
330 const AuthChallengeInfo& auth_info,
331 const AuthCallback& callback,
332 AuthCredentials* credentials) override;
333 bool OnCanGetCookies(const URLRequest& request,
334 const CookieList& cookie_list) override;
335 bool OnCanSetCookie(const URLRequest& request,
336 const std::string& cookie_line,
337 CookieOptions* options) override;
338 bool OnCanAccessFile(const URLRequest& request,
339 const base::FilePath& path) const override;
340 bool OnFirstPartyOnlyCookieExperimentEnabled() const override;
341 bool OnCancelURLRequestWithPolicyViolatingReferrerHeader(
342 const URLRequest& request,
343 const GURL& target_url,
344 const GURL& referrer_url) const override;
346 void InitRequestStatesIfNew(int request_id);
348 GURL redirect_on_headers_received_url_;
349 // URL marked as safe for redirection at the onHeadersReceived stage.
350 GURL allowed_unsafe_redirect_url_;
352 int last_error_;
353 int error_count_;
354 int created_requests_;
355 int destroyed_requests_;
356 int completed_requests_;
357 int canceled_requests_;
358 int cookie_options_bit_mask_;
359 int blocked_get_cookies_count_;
360 int blocked_set_cookie_count_;
361 int set_cookie_count_;
362 int observed_before_proxy_headers_sent_callbacks_;
363 int before_send_headers_count_;
364 int headers_received_count_;
365 int64_t total_network_bytes_received_;
366 // Last observed proxy in before proxy header sent callback.
367 HostPortPair last_observed_proxy_;
369 // NetworkDelegate callbacks happen in a particular order (e.g.
370 // OnBeforeURLRequest is always called before OnBeforeSendHeaders).
371 // This bit-set indicates for each request id (key) what events may be sent
372 // next.
373 std::map<int, int> next_states_;
375 // A log that records for each request id (key) the order in which On...
376 // functions were called.
377 std::map<int, std::string> event_order_;
379 LoadTimingInfo load_timing_info_before_redirect_;
380 bool has_load_timing_info_before_redirect_;
382 LoadTimingInfo load_timing_info_before_auth_;
383 bool has_load_timing_info_before_auth_;
385 bool can_access_files_; // true by default
386 bool first_party_only_cookies_enabled_; // false by default
387 bool cancel_request_with_policy_violating_referrer_; // false by default
388 bool will_be_intercepted_on_next_error_;
391 //-----------------------------------------------------------------------------
393 // A simple ProtocolHandler that returns a pre-built URLRequestJob only once.
394 class TestJobInterceptor : public URLRequestJobFactory::ProtocolHandler {
395 public:
396 TestJobInterceptor();
398 URLRequestJob* MaybeCreateJob(
399 URLRequest* request,
400 NetworkDelegate* network_delegate) const override;
401 void set_main_intercept_job(URLRequestJob* job);
403 private:
404 mutable URLRequestJob* main_intercept_job_;
407 } // namespace net
409 #endif // NET_URL_REQUEST_URL_REQUEST_TEST_UTIL_H_