Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / net / url_request / url_request_test_util.h
blob0208b4392fcf3fb0789828333d636f9e3fb85ef4
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/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;
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 const HttpNetworkSession::Params& params) {
76 void SetSdchManager(scoped_ptr<SdchManager> sdch_manager) {
77 context_storage_.set_sdch_manager(sdch_manager.Pass());
80 private:
81 bool initialized_;
83 // Optional parameters to override default values. Note that values that
84 // point to other objects the TestURLRequestContext creates will be
85 // overwritten.
86 scoped_ptr<HttpNetworkSession::Params> http_network_session_params_;
88 // Not owned:
89 ClientSocketFactory* client_socket_factory_;
91 protected:
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 {
100 public:
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 TestURLRequestContext* GetURLRequestContext() override;
112 scoped_refptr<base::SingleThreadTaskRunner> GetNetworkTaskRunner()
113 const override;
115 protected:
116 ~TestURLRequestContextGetter() override;
118 private:
119 const scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_;
120 scoped_ptr<TestURLRequestContext> context_;
123 //-----------------------------------------------------------------------------
125 class TestDelegate : public URLRequest::Delegate {
126 public:
127 TestDelegate();
128 ~TestDelegate() override;
130 void set_cancel_in_received_redirect(bool val) { cancel_in_rr_ = val; }
131 void set_cancel_in_response_started(bool val) { cancel_in_rs_ = val; }
132 void set_cancel_in_received_data(bool val) { cancel_in_rd_ = val; }
133 void set_cancel_in_received_data_pending(bool val) {
134 cancel_in_rd_pending_ = val;
136 void set_quit_on_complete(bool val) { quit_on_complete_ = val; }
137 void set_quit_on_redirect(bool val) { quit_on_redirect_ = val; }
138 void set_quit_on_network_start(bool val) {
139 quit_on_before_network_start_ = val;
141 void set_allow_certificate_errors(bool val) {
142 allow_certificate_errors_ = val;
144 void set_credentials(const AuthCredentials& credentials) {
145 credentials_ = credentials;
148 // query state
149 const std::string& data_received() const { return data_received_; }
150 int bytes_received() const { return static_cast<int>(data_received_.size()); }
151 int response_started_count() const { return response_started_count_; }
152 int received_redirect_count() const { return received_redirect_count_; }
153 int received_before_network_start_count() const {
154 return received_before_network_start_count_;
156 bool received_data_before_response() const {
157 return received_data_before_response_;
159 bool request_failed() const { return request_failed_; }
160 bool have_certificate_errors() const { return have_certificate_errors_; }
161 bool certificate_errors_are_fatal() const {
162 return certificate_errors_are_fatal_;
164 bool auth_required_called() const { return auth_required_; }
165 bool have_full_request_headers() const { return have_full_request_headers_; }
166 const HttpRequestHeaders& full_request_headers() const {
167 return full_request_headers_;
169 void ClearFullRequestHeaders();
171 // URLRequest::Delegate:
172 void OnReceivedRedirect(URLRequest* request,
173 const RedirectInfo& redirect_info,
174 bool* defer_redirect) override;
175 void OnBeforeNetworkStart(URLRequest* request, bool* defer) override;
176 void OnAuthRequired(URLRequest* request,
177 AuthChallengeInfo* auth_info) override;
178 // NOTE: |fatal| causes |certificate_errors_are_fatal_| to be set to true.
179 // (Unit tests use this as a post-condition.) But for policy, this method
180 // consults |allow_certificate_errors_|.
181 void OnSSLCertificateError(URLRequest* request,
182 const SSLInfo& ssl_info,
183 bool fatal) override;
184 void OnResponseStarted(URLRequest* request) override;
185 void OnReadCompleted(URLRequest* request, int bytes_read) override;
187 private:
188 static const int kBufferSize = 4096;
190 virtual void OnResponseCompleted(URLRequest* request);
192 // options for controlling behavior
193 bool cancel_in_rr_;
194 bool cancel_in_rs_;
195 bool cancel_in_rd_;
196 bool cancel_in_rd_pending_;
197 bool quit_on_complete_;
198 bool quit_on_redirect_;
199 bool quit_on_before_network_start_;
200 bool allow_certificate_errors_;
201 AuthCredentials credentials_;
203 // tracks status of callbacks
204 int response_started_count_;
205 int received_bytes_count_;
206 int received_redirect_count_;
207 int received_before_network_start_count_;
208 bool received_data_before_response_;
209 bool request_failed_;
210 bool have_certificate_errors_;
211 bool certificate_errors_are_fatal_;
212 bool auth_required_;
213 std::string data_received_;
214 bool have_full_request_headers_;
215 HttpRequestHeaders full_request_headers_;
217 // our read buffer
218 scoped_refptr<IOBuffer> buf_;
221 //-----------------------------------------------------------------------------
223 class TestNetworkDelegate : public NetworkDelegateImpl {
224 public:
225 enum Options {
226 NO_GET_COOKIES = 1 << 0,
227 NO_SET_COOKIE = 1 << 1,
230 TestNetworkDelegate();
231 ~TestNetworkDelegate() override;
233 // Writes the LoadTimingInfo during the most recent call to OnBeforeRedirect.
234 bool GetLoadTimingInfoBeforeRedirect(
235 LoadTimingInfo* load_timing_info_before_redirect) const;
237 // Same as GetLoadTimingInfoBeforeRedirect, except for calls to
238 // AuthRequiredResponse.
239 bool GetLoadTimingInfoBeforeAuth(
240 LoadTimingInfo* load_timing_info_before_auth) const;
242 // Will redirect once to the given URL when the next set of headers are
243 // received.
244 void set_redirect_on_headers_received_url(
245 GURL redirect_on_headers_received_url) {
246 redirect_on_headers_received_url_ = redirect_on_headers_received_url;
249 void set_allowed_unsafe_redirect_url(GURL allowed_unsafe_redirect_url) {
250 allowed_unsafe_redirect_url_ = allowed_unsafe_redirect_url;
253 void set_cookie_options(int o) {cookie_options_bit_mask_ = o; }
255 int last_error() const { return last_error_; }
256 int error_count() const { return error_count_; }
257 int created_requests() const { return created_requests_; }
258 int destroyed_requests() const { return destroyed_requests_; }
259 int completed_requests() const { return completed_requests_; }
260 int canceled_requests() const { return canceled_requests_; }
261 int blocked_get_cookies_count() const { return blocked_get_cookies_count_; }
262 int blocked_set_cookie_count() const { return blocked_set_cookie_count_; }
263 int set_cookie_count() const { return set_cookie_count_; }
265 void set_can_access_files(bool val) { can_access_files_ = val; }
266 bool can_access_files() const { return can_access_files_; }
268 void set_first_party_only_cookies_enabled(bool val) {
269 first_party_only_cookies_enabled_ = val;
272 void set_can_throttle_requests(bool val) { can_throttle_requests_ = val; }
273 bool can_throttle_requests() const { return can_throttle_requests_; }
275 void set_cancel_request_with_policy_violating_referrer(bool val) {
276 cancel_request_with_policy_violating_referrer_ = val;
279 int observed_before_proxy_headers_sent_callbacks() const {
280 return observed_before_proxy_headers_sent_callbacks_;
282 int before_send_headers_count() const { return before_send_headers_count_; }
283 int headers_received_count() const { return headers_received_count_; }
285 // Last observed proxy in proxy header sent callback.
286 HostPortPair last_observed_proxy() {
287 return last_observed_proxy_;
290 void set_can_be_intercepted_on_error(bool can_be_intercepted_on_error) {
291 will_be_intercepted_on_next_error_ = can_be_intercepted_on_error;
294 protected:
295 // NetworkDelegate:
296 int OnBeforeURLRequest(URLRequest* request,
297 const CompletionCallback& callback,
298 GURL* new_url) override;
299 int OnBeforeSendHeaders(URLRequest* request,
300 const CompletionCallback& callback,
301 HttpRequestHeaders* headers) override;
302 void OnBeforeSendProxyHeaders(net::URLRequest* request,
303 const net::ProxyInfo& proxy_info,
304 net::HttpRequestHeaders* headers) override;
305 void OnSendHeaders(URLRequest* request,
306 const HttpRequestHeaders& headers) override;
307 int OnHeadersReceived(
308 URLRequest* request,
309 const CompletionCallback& callback,
310 const HttpResponseHeaders* original_response_headers,
311 scoped_refptr<HttpResponseHeaders>* override_response_headers,
312 GURL* allowed_unsafe_redirect_url) override;
313 void OnBeforeRedirect(URLRequest* request, const GURL& new_location) override;
314 void OnResponseStarted(URLRequest* request) override;
315 void OnRawBytesRead(const URLRequest& request, int bytes_read) override;
316 void OnCompleted(URLRequest* request, bool started) override;
317 void OnURLRequestDestroyed(URLRequest* request) override;
318 void OnPACScriptError(int line_number, const base::string16& error) override;
319 NetworkDelegate::AuthRequiredResponse OnAuthRequired(
320 URLRequest* request,
321 const AuthChallengeInfo& auth_info,
322 const AuthCallback& callback,
323 AuthCredentials* credentials) override;
324 bool OnCanGetCookies(const URLRequest& request,
325 const CookieList& cookie_list) override;
326 bool OnCanSetCookie(const URLRequest& request,
327 const std::string& cookie_line,
328 CookieOptions* options) override;
329 bool OnCanAccessFile(const URLRequest& request,
330 const base::FilePath& path) const override;
331 bool OnCanThrottleRequest(const URLRequest& request) const override;
332 bool OnFirstPartyOnlyCookieExperimentEnabled() const override;
333 bool OnCancelURLRequestWithPolicyViolatingReferrerHeader(
334 const URLRequest& request,
335 const GURL& target_url,
336 const GURL& referrer_url) const override;
338 void InitRequestStatesIfNew(int request_id);
340 GURL redirect_on_headers_received_url_;
341 // URL marked as safe for redirection at the onHeadersReceived stage.
342 GURL allowed_unsafe_redirect_url_;
344 int last_error_;
345 int error_count_;
346 int created_requests_;
347 int destroyed_requests_;
348 int completed_requests_;
349 int canceled_requests_;
350 int cookie_options_bit_mask_;
351 int blocked_get_cookies_count_;
352 int blocked_set_cookie_count_;
353 int set_cookie_count_;
354 int observed_before_proxy_headers_sent_callbacks_;
355 int before_send_headers_count_;
356 int headers_received_count_;
357 // Last observed proxy in before proxy header sent callback.
358 HostPortPair last_observed_proxy_;
360 // NetworkDelegate callbacks happen in a particular order (e.g.
361 // OnBeforeURLRequest is always called before OnBeforeSendHeaders).
362 // This bit-set indicates for each request id (key) what events may be sent
363 // next.
364 std::map<int, int> next_states_;
366 // A log that records for each request id (key) the order in which On...
367 // functions were called.
368 std::map<int, std::string> event_order_;
370 LoadTimingInfo load_timing_info_before_redirect_;
371 bool has_load_timing_info_before_redirect_;
373 LoadTimingInfo load_timing_info_before_auth_;
374 bool has_load_timing_info_before_auth_;
376 bool can_access_files_; // true by default
377 bool can_throttle_requests_; // true by default
378 bool first_party_only_cookies_enabled_; // false by default
379 bool cancel_request_with_policy_violating_referrer_; // false by default
380 bool will_be_intercepted_on_next_error_;
383 // Overrides the host used by the LocalHttpTestServer in
384 // url_request_unittest.cc . This is used by the chrome_frame_net_tests due to
385 // a mysterious bug when tests execute over the loopback adapter. See
386 // http://crbug.com/114369 .
387 class ScopedCustomUrlRequestTestHttpHost {
388 public:
389 // Sets the host name to be used. The previous hostname will be stored and
390 // restored upon destruction. Note that if the lifetimes of two or more
391 // instances of this class overlap, they must be strictly nested.
392 explicit ScopedCustomUrlRequestTestHttpHost(const std::string& new_value);
394 ~ScopedCustomUrlRequestTestHttpHost();
396 // Returns the current value to be used by HTTP tests in
397 // url_request_unittest.cc .
398 static const std::string& value();
400 private:
401 static std::string value_;
402 const std::string old_value_;
403 const std::string new_value_;
405 DISALLOW_COPY_AND_ASSIGN(ScopedCustomUrlRequestTestHttpHost);
408 //-----------------------------------------------------------------------------
410 // A simple ProtocolHandler that returns a pre-built URLRequestJob only once.
411 class TestJobInterceptor : public URLRequestJobFactory::ProtocolHandler {
412 public:
413 TestJobInterceptor();
415 URLRequestJob* MaybeCreateJob(
416 URLRequest* request,
417 NetworkDelegate* network_delegate) const override;
418 void set_main_intercept_job(URLRequestJob* job);
420 private:
421 mutable URLRequestJob* main_intercept_job_;
424 } // namespace net
426 #endif // NET_URL_REQUEST_URL_REQUEST_TEST_UTIL_H_