Make USB permissions work in the new permission message system
[chromium-blink-merge.git] / net / url_request / url_request_test_util.h
blobffa1b1b4f337c447eba46440b513b5bb093f0e82
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 <stdlib.h>
10 #include <map>
11 #include <string>
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/path_service.h"
18 #include "base/single_thread_task_runner.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;
49 namespace net {
51 //-----------------------------------------------------------------------------
53 class TestURLRequestContext : public URLRequestContext {
54 public:
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;
63 void Init();
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());
81 private:
82 bool initialized_;
84 // Optional parameters to override default values. Note that values that
85 // point to other objects the TestURLRequestContext creates will be
86 // overwritten.
87 scoped_ptr<HttpNetworkSession::Params> http_network_session_params_;
89 // Not owned:
90 ClientSocketFactory* client_socket_factory_;
92 protected:
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 {
101 public:
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()
114 const override;
116 protected:
117 ~TestURLRequestContextGetter() override;
119 private:
120 const scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_;
121 scoped_ptr<TestURLRequestContext> context_;
124 //-----------------------------------------------------------------------------
126 class TestDelegate : public URLRequest::Delegate {
127 public:
128 TestDelegate();
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;
138 void set_quit_on_complete(bool val) { quit_on_complete_ = val; }
139 void set_quit_on_redirect(bool val) { quit_on_redirect_ = val; }
140 // Enables quitting the message loop in response to auth requests, as opposed
141 // to returning credentials or cancelling the request.
142 void set_quit_on_auth_required(bool val) { quit_on_auth_required_ = val; }
143 void set_quit_on_network_start(bool val) {
144 quit_on_before_network_start_ = val;
147 void set_allow_certificate_errors(bool val) {
148 allow_certificate_errors_ = val;
150 void set_credentials(const AuthCredentials& credentials) {
151 credentials_ = credentials;
154 // query state
155 const std::string& data_received() const { return data_received_; }
156 int bytes_received() const { return static_cast<int>(data_received_.size()); }
157 int response_started_count() const { return response_started_count_; }
158 int received_redirect_count() const { return received_redirect_count_; }
159 int received_before_network_start_count() const {
160 return received_before_network_start_count_;
162 bool received_data_before_response() const {
163 return received_data_before_response_;
165 bool request_failed() const { return request_failed_; }
166 bool have_certificate_errors() const { return have_certificate_errors_; }
167 bool certificate_errors_are_fatal() const {
168 return certificate_errors_are_fatal_;
170 bool auth_required_called() const { return auth_required_; }
171 bool have_full_request_headers() const { return have_full_request_headers_; }
172 const HttpRequestHeaders& full_request_headers() const {
173 return full_request_headers_;
175 void ClearFullRequestHeaders();
177 // URLRequest::Delegate:
178 void OnReceivedRedirect(URLRequest* request,
179 const RedirectInfo& redirect_info,
180 bool* defer_redirect) override;
181 void OnBeforeNetworkStart(URLRequest* request, bool* defer) override;
182 void OnAuthRequired(URLRequest* request,
183 AuthChallengeInfo* auth_info) override;
184 // NOTE: |fatal| causes |certificate_errors_are_fatal_| to be set to true.
185 // (Unit tests use this as a post-condition.) But for policy, this method
186 // consults |allow_certificate_errors_|.
187 void OnSSLCertificateError(URLRequest* request,
188 const SSLInfo& ssl_info,
189 bool fatal) override;
190 void OnResponseStarted(URLRequest* request) override;
191 void OnReadCompleted(URLRequest* request, int bytes_read) override;
193 private:
194 static const int kBufferSize = 4096;
196 virtual void OnResponseCompleted(URLRequest* request);
198 // options for controlling behavior
199 bool cancel_in_rr_;
200 bool cancel_in_rs_;
201 bool cancel_in_rd_;
202 bool cancel_in_rd_pending_;
203 bool quit_on_complete_;
204 bool quit_on_redirect_;
205 bool quit_on_auth_required_;
206 bool quit_on_before_network_start_;
207 bool allow_certificate_errors_;
208 AuthCredentials credentials_;
210 // tracks status of callbacks
211 int response_started_count_;
212 int received_bytes_count_;
213 int received_redirect_count_;
214 int received_before_network_start_count_;
215 bool received_data_before_response_;
216 bool request_failed_;
217 bool have_certificate_errors_;
218 bool certificate_errors_are_fatal_;
219 bool auth_required_;
220 std::string data_received_;
221 bool have_full_request_headers_;
222 HttpRequestHeaders full_request_headers_;
224 // our read buffer
225 scoped_refptr<IOBuffer> buf_;
228 //-----------------------------------------------------------------------------
230 class TestNetworkDelegate : public NetworkDelegateImpl {
231 public:
232 enum Options {
233 NO_GET_COOKIES = 1 << 0,
234 NO_SET_COOKIE = 1 << 1,
237 TestNetworkDelegate();
238 ~TestNetworkDelegate() override;
240 // Writes the LoadTimingInfo during the most recent call to OnBeforeRedirect.
241 bool GetLoadTimingInfoBeforeRedirect(
242 LoadTimingInfo* load_timing_info_before_redirect) const;
244 // Same as GetLoadTimingInfoBeforeRedirect, except for calls to
245 // AuthRequiredResponse.
246 bool GetLoadTimingInfoBeforeAuth(
247 LoadTimingInfo* load_timing_info_before_auth) const;
249 // Will redirect once to the given URL when the next set of headers are
250 // received.
251 void set_redirect_on_headers_received_url(
252 GURL redirect_on_headers_received_url) {
253 redirect_on_headers_received_url_ = redirect_on_headers_received_url;
256 void set_allowed_unsafe_redirect_url(GURL allowed_unsafe_redirect_url) {
257 allowed_unsafe_redirect_url_ = allowed_unsafe_redirect_url;
260 void set_cookie_options(int o) {cookie_options_bit_mask_ = o; }
262 int last_error() const { return last_error_; }
263 int error_count() const { return error_count_; }
264 int created_requests() const { return created_requests_; }
265 int destroyed_requests() const { return destroyed_requests_; }
266 int completed_requests() const { return completed_requests_; }
267 int canceled_requests() const { return canceled_requests_; }
268 int blocked_get_cookies_count() const { return blocked_get_cookies_count_; }
269 int blocked_set_cookie_count() const { return blocked_set_cookie_count_; }
270 int set_cookie_count() const { return set_cookie_count_; }
272 void set_can_access_files(bool val) { can_access_files_ = val; }
273 bool can_access_files() const { return can_access_files_; }
275 void set_first_party_only_cookies_enabled(bool val) {
276 first_party_only_cookies_enabled_ = val;
279 void set_cancel_request_with_policy_violating_referrer(bool val) {
280 cancel_request_with_policy_violating_referrer_ = val;
283 int observed_before_proxy_headers_sent_callbacks() const {
284 return observed_before_proxy_headers_sent_callbacks_;
286 int before_send_headers_count() const { return before_send_headers_count_; }
287 int headers_received_count() const { return headers_received_count_; }
289 // Last observed proxy in proxy header sent callback.
290 HostPortPair last_observed_proxy() {
291 return last_observed_proxy_;
294 void set_can_be_intercepted_on_error(bool can_be_intercepted_on_error) {
295 will_be_intercepted_on_next_error_ = can_be_intercepted_on_error;
298 protected:
299 // NetworkDelegate:
300 int OnBeforeURLRequest(URLRequest* request,
301 const CompletionCallback& callback,
302 GURL* new_url) override;
303 int OnBeforeSendHeaders(URLRequest* request,
304 const CompletionCallback& callback,
305 HttpRequestHeaders* headers) override;
306 void OnBeforeSendProxyHeaders(URLRequest* request,
307 const ProxyInfo& proxy_info,
308 HttpRequestHeaders* headers) override;
309 void OnSendHeaders(URLRequest* request,
310 const HttpRequestHeaders& headers) override;
311 int OnHeadersReceived(
312 URLRequest* request,
313 const CompletionCallback& callback,
314 const HttpResponseHeaders* original_response_headers,
315 scoped_refptr<HttpResponseHeaders>* override_response_headers,
316 GURL* allowed_unsafe_redirect_url) override;
317 void OnBeforeRedirect(URLRequest* request, const GURL& new_location) override;
318 void OnResponseStarted(URLRequest* request) override;
319 void OnRawBytesRead(const URLRequest& request, int bytes_read) override;
320 void OnCompleted(URLRequest* request, bool started) override;
321 void OnURLRequestDestroyed(URLRequest* request) override;
322 void OnPACScriptError(int line_number, const base::string16& error) override;
323 NetworkDelegate::AuthRequiredResponse OnAuthRequired(
324 URLRequest* request,
325 const AuthChallengeInfo& auth_info,
326 const AuthCallback& callback,
327 AuthCredentials* credentials) override;
328 bool OnCanGetCookies(const URLRequest& request,
329 const CookieList& cookie_list) override;
330 bool OnCanSetCookie(const URLRequest& request,
331 const std::string& cookie_line,
332 CookieOptions* options) override;
333 bool OnCanAccessFile(const URLRequest& request,
334 const base::FilePath& path) const override;
335 bool OnFirstPartyOnlyCookieExperimentEnabled() const override;
336 bool OnCancelURLRequestWithPolicyViolatingReferrerHeader(
337 const URLRequest& request,
338 const GURL& target_url,
339 const GURL& referrer_url) const override;
341 void InitRequestStatesIfNew(int request_id);
343 GURL redirect_on_headers_received_url_;
344 // URL marked as safe for redirection at the onHeadersReceived stage.
345 GURL allowed_unsafe_redirect_url_;
347 int last_error_;
348 int error_count_;
349 int created_requests_;
350 int destroyed_requests_;
351 int completed_requests_;
352 int canceled_requests_;
353 int cookie_options_bit_mask_;
354 int blocked_get_cookies_count_;
355 int blocked_set_cookie_count_;
356 int set_cookie_count_;
357 int observed_before_proxy_headers_sent_callbacks_;
358 int before_send_headers_count_;
359 int headers_received_count_;
360 // Last observed proxy in before proxy header sent callback.
361 HostPortPair last_observed_proxy_;
363 // NetworkDelegate callbacks happen in a particular order (e.g.
364 // OnBeforeURLRequest is always called before OnBeforeSendHeaders).
365 // This bit-set indicates for each request id (key) what events may be sent
366 // next.
367 std::map<int, int> next_states_;
369 // A log that records for each request id (key) the order in which On...
370 // functions were called.
371 std::map<int, std::string> event_order_;
373 LoadTimingInfo load_timing_info_before_redirect_;
374 bool has_load_timing_info_before_redirect_;
376 LoadTimingInfo load_timing_info_before_auth_;
377 bool has_load_timing_info_before_auth_;
379 bool can_access_files_; // true by default
380 bool first_party_only_cookies_enabled_; // false by default
381 bool cancel_request_with_policy_violating_referrer_; // false by default
382 bool will_be_intercepted_on_next_error_;
385 //-----------------------------------------------------------------------------
387 // A simple ProtocolHandler that returns a pre-built URLRequestJob only once.
388 class TestJobInterceptor : public URLRequestJobFactory::ProtocolHandler {
389 public:
390 TestJobInterceptor();
392 URLRequestJob* MaybeCreateJob(
393 URLRequest* request,
394 NetworkDelegate* network_delegate) const override;
395 void set_main_intercept_job(URLRequestJob* job);
397 private:
398 mutable URLRequestJob* main_intercept_job_;
401 } // namespace net
403 #endif // NET_URL_REQUEST_URL_REQUEST_TEST_UTIL_H_