Disable signin-to-Chrome when using Guest profile.
[chromium-blink-merge.git] / net / url_request / url_request_test_util.h
blob0d35876157e64bb545fa6fef23f67deb72bc8e81
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/message_loop/message_loop_proxy.h"
17 #include "base/path_service.h"
18 #include "base/strings/string16.h"
19 #include "base/strings/string_util.h"
20 #include "base/strings/utf_string_conversions.h"
21 #include "base/time/time.h"
22 #include "net/base/io_buffer.h"
23 #include "net/base/load_timing_info.h"
24 #include "net/base/net_errors.h"
25 #include "net/base/network_delegate.h"
26 #include "net/base/request_priority.h"
27 #include "net/cert/cert_verifier.h"
28 #include "net/cookies/cookie_monster.h"
29 #include "net/disk_cache/disk_cache.h"
30 #include "net/ftp/ftp_network_layer.h"
31 #include "net/http/http_auth_handler_factory.h"
32 #include "net/http/http_cache.h"
33 #include "net/http/http_network_layer.h"
34 #include "net/http/http_request_headers.h"
35 #include "net/proxy/proxy_service.h"
36 #include "net/ssl/ssl_config_service_defaults.h"
37 #include "net/url_request/url_request.h"
38 #include "net/url_request/url_request_context.h"
39 #include "net/url_request/url_request_context_getter.h"
40 #include "net/url_request/url_request_context_storage.h"
41 #include "net/url_request/url_request_job_factory.h"
42 #include "url/url_util.h"
44 using base::TimeDelta;
46 namespace net {
48 //-----------------------------------------------------------------------------
50 class TestURLRequestContext : public URLRequestContext {
51 public:
52 TestURLRequestContext();
53 // Default constructor like TestURLRequestContext() but does not call
54 // Init() in case |delay_initialization| is true. This allows modifying the
55 // URLRequestContext before it is constructed completely. If
56 // |delay_initialization| is true, Init() needs be be called manually.
57 explicit TestURLRequestContext(bool delay_initialization);
58 virtual ~TestURLRequestContext();
60 void Init();
62 ClientSocketFactory* client_socket_factory() {
63 return client_socket_factory_;
65 void set_client_socket_factory(ClientSocketFactory* factory) {
66 client_socket_factory_ = factory;
69 private:
70 bool initialized_;
72 // Not owned:
73 ClientSocketFactory* client_socket_factory_;
75 protected:
76 URLRequestContextStorage context_storage_;
79 //-----------------------------------------------------------------------------
81 // Used to return a dummy context, which lives on the message loop
82 // given in the constructor.
83 class TestURLRequestContextGetter : public URLRequestContextGetter {
84 public:
85 // |network_task_runner| must not be NULL.
86 explicit TestURLRequestContextGetter(
87 const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner);
89 // Use to pass a pre-initialized |context|.
90 TestURLRequestContextGetter(
91 const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner,
92 scoped_ptr<TestURLRequestContext> context);
94 // URLRequestContextGetter implementation.
95 virtual TestURLRequestContext* GetURLRequestContext() OVERRIDE;
96 virtual scoped_refptr<base::SingleThreadTaskRunner>
97 GetNetworkTaskRunner() const OVERRIDE;
99 protected:
100 virtual ~TestURLRequestContextGetter();
102 private:
103 const scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_;
104 scoped_ptr<TestURLRequestContext> context_;
107 //-----------------------------------------------------------------------------
109 class TestURLRequest : public URLRequest {
110 public:
111 TestURLRequest(const GURL& url,
112 RequestPriority priority,
113 Delegate* delegate,
114 TestURLRequestContext* context);
115 virtual ~TestURLRequest();
118 //-----------------------------------------------------------------------------
120 class TestDelegate : public URLRequest::Delegate {
121 public:
122 TestDelegate();
123 virtual ~TestDelegate();
125 void set_cancel_in_received_redirect(bool val) { cancel_in_rr_ = val; }
126 void set_cancel_in_response_started(bool val) { cancel_in_rs_ = val; }
127 void set_cancel_in_received_data(bool val) { cancel_in_rd_ = val; }
128 void set_cancel_in_received_data_pending(bool val) {
129 cancel_in_rd_pending_ = val;
131 void set_quit_on_complete(bool val) { quit_on_complete_ = val; }
132 void set_quit_on_redirect(bool val) { quit_on_redirect_ = val; }
133 void set_quit_on_network_start(bool val) {
134 quit_on_before_network_start_ = val;
136 void set_allow_certificate_errors(bool val) {
137 allow_certificate_errors_ = val;
139 void set_credentials(const AuthCredentials& credentials) {
140 credentials_ = credentials;
143 // query state
144 const std::string& data_received() const { return data_received_; }
145 int bytes_received() const { return static_cast<int>(data_received_.size()); }
146 int response_started_count() const { return response_started_count_; }
147 int received_redirect_count() const { return received_redirect_count_; }
148 int received_before_network_start_count() const {
149 return received_before_network_start_count_;
151 bool received_data_before_response() const {
152 return received_data_before_response_;
154 bool request_failed() const { return request_failed_; }
155 bool have_certificate_errors() const { return have_certificate_errors_; }
156 bool certificate_errors_are_fatal() const {
157 return certificate_errors_are_fatal_;
159 bool auth_required_called() const { return auth_required_; }
160 bool have_full_request_headers() const { return have_full_request_headers_; }
161 const HttpRequestHeaders& full_request_headers() const {
162 return full_request_headers_;
164 void ClearFullRequestHeaders();
166 // URLRequest::Delegate:
167 virtual void OnReceivedRedirect(URLRequest* request, const GURL& new_url,
168 bool* defer_redirect) OVERRIDE;
169 virtual void OnBeforeNetworkStart(URLRequest* request, bool* defer) OVERRIDE;
170 virtual void OnAuthRequired(URLRequest* request,
171 AuthChallengeInfo* auth_info) OVERRIDE;
172 // NOTE: |fatal| causes |certificate_errors_are_fatal_| to be set to true.
173 // (Unit tests use this as a post-condition.) But for policy, this method
174 // consults |allow_certificate_errors_|.
175 virtual void OnSSLCertificateError(URLRequest* request,
176 const SSLInfo& ssl_info,
177 bool fatal) OVERRIDE;
178 virtual void OnResponseStarted(URLRequest* request) OVERRIDE;
179 virtual void OnReadCompleted(URLRequest* request,
180 int bytes_read) OVERRIDE;
182 private:
183 static const int kBufferSize = 4096;
185 virtual void OnResponseCompleted(URLRequest* request);
187 // options for controlling behavior
188 bool cancel_in_rr_;
189 bool cancel_in_rs_;
190 bool cancel_in_rd_;
191 bool cancel_in_rd_pending_;
192 bool quit_on_complete_;
193 bool quit_on_redirect_;
194 bool quit_on_before_network_start_;
195 bool allow_certificate_errors_;
196 AuthCredentials credentials_;
198 // tracks status of callbacks
199 int response_started_count_;
200 int received_bytes_count_;
201 int received_redirect_count_;
202 int received_before_network_start_count_;
203 bool received_data_before_response_;
204 bool request_failed_;
205 bool have_certificate_errors_;
206 bool certificate_errors_are_fatal_;
207 bool auth_required_;
208 std::string data_received_;
209 bool have_full_request_headers_;
210 HttpRequestHeaders full_request_headers_;
212 // our read buffer
213 scoped_refptr<IOBuffer> buf_;
216 //-----------------------------------------------------------------------------
218 class TestNetworkDelegate : public NetworkDelegate {
219 public:
220 enum Options {
221 NO_GET_COOKIES = 1 << 0,
222 NO_SET_COOKIE = 1 << 1,
225 TestNetworkDelegate();
226 virtual ~TestNetworkDelegate();
228 // Writes the LoadTimingInfo during the most recent call to OnBeforeRedirect.
229 bool GetLoadTimingInfoBeforeRedirect(
230 LoadTimingInfo* load_timing_info_before_redirect) const;
232 // Same as GetLoadTimingInfoBeforeRedirect, except for calls to
233 // AuthRequiredResponse.
234 bool GetLoadTimingInfoBeforeAuth(
235 LoadTimingInfo* load_timing_info_before_auth) const;
237 void set_cookie_options(int o) {cookie_options_bit_mask_ = o; }
239 int last_error() const { return last_error_; }
240 int error_count() const { return error_count_; }
241 int created_requests() const { return created_requests_; }
242 int destroyed_requests() const { return destroyed_requests_; }
243 int completed_requests() const { return completed_requests_; }
244 int canceled_requests() const { return canceled_requests_; }
245 int blocked_get_cookies_count() const { return blocked_get_cookies_count_; }
246 int blocked_set_cookie_count() const { return blocked_set_cookie_count_; }
247 int set_cookie_count() const { return set_cookie_count_; }
249 void set_can_access_files(bool val) { can_access_files_ = val; }
250 bool can_access_files() const { return can_access_files_; }
252 void set_can_throttle_requests(bool val) { can_throttle_requests_ = val; }
253 bool can_throttle_requests() const { return can_throttle_requests_; }
255 protected:
256 // NetworkDelegate:
257 virtual int OnBeforeURLRequest(URLRequest* request,
258 const CompletionCallback& callback,
259 GURL* new_url) OVERRIDE;
260 virtual int OnBeforeSendHeaders(URLRequest* request,
261 const CompletionCallback& callback,
262 HttpRequestHeaders* headers) OVERRIDE;
263 virtual void OnSendHeaders(URLRequest* request,
264 const HttpRequestHeaders& headers) OVERRIDE;
265 virtual int OnHeadersReceived(
266 URLRequest* request,
267 const CompletionCallback& callback,
268 const HttpResponseHeaders* original_response_headers,
269 scoped_refptr<HttpResponseHeaders>* override_response_headers) OVERRIDE;
270 virtual void OnBeforeRedirect(URLRequest* request,
271 const GURL& new_location) OVERRIDE;
272 virtual void OnResponseStarted(URLRequest* request) OVERRIDE;
273 virtual void OnRawBytesRead(const URLRequest& request,
274 int bytes_read) OVERRIDE;
275 virtual void OnCompleted(URLRequest* request, bool started) OVERRIDE;
276 virtual void OnURLRequestDestroyed(URLRequest* request) OVERRIDE;
277 virtual void OnPACScriptError(int line_number,
278 const base::string16& error) OVERRIDE;
279 virtual NetworkDelegate::AuthRequiredResponse OnAuthRequired(
280 URLRequest* request,
281 const AuthChallengeInfo& auth_info,
282 const AuthCallback& callback,
283 AuthCredentials* credentials) OVERRIDE;
284 virtual bool OnCanGetCookies(const URLRequest& request,
285 const CookieList& cookie_list) OVERRIDE;
286 virtual bool OnCanSetCookie(const URLRequest& request,
287 const std::string& cookie_line,
288 CookieOptions* options) OVERRIDE;
289 virtual bool OnCanAccessFile(const URLRequest& request,
290 const base::FilePath& path) const OVERRIDE;
291 virtual bool OnCanThrottleRequest(
292 const URLRequest& request) const OVERRIDE;
293 virtual int OnBeforeSocketStreamConnect(
294 SocketStream* stream,
295 const CompletionCallback& callback) OVERRIDE;
297 void InitRequestStatesIfNew(int request_id);
299 int last_error_;
300 int error_count_;
301 int created_requests_;
302 int destroyed_requests_;
303 int completed_requests_;
304 int canceled_requests_;
305 int cookie_options_bit_mask_;
306 int blocked_get_cookies_count_;
307 int blocked_set_cookie_count_;
308 int set_cookie_count_;
310 // NetworkDelegate callbacks happen in a particular order (e.g.
311 // OnBeforeURLRequest is always called before OnBeforeSendHeaders).
312 // This bit-set indicates for each request id (key) what events may be sent
313 // next.
314 std::map<int, int> next_states_;
316 // A log that records for each request id (key) the order in which On...
317 // functions were called.
318 std::map<int, std::string> event_order_;
320 LoadTimingInfo load_timing_info_before_redirect_;
321 bool has_load_timing_info_before_redirect_;
323 LoadTimingInfo load_timing_info_before_auth_;
324 bool has_load_timing_info_before_auth_;
326 bool can_access_files_; // true by default
327 bool can_throttle_requests_; // true by default
330 // Overrides the host used by the LocalHttpTestServer in
331 // url_request_unittest.cc . This is used by the chrome_frame_net_tests due to
332 // a mysterious bug when tests execute over the loopback adapter. See
333 // http://crbug.com/114369 .
334 class ScopedCustomUrlRequestTestHttpHost {
335 public:
336 // Sets the host name to be used. The previous hostname will be stored and
337 // restored upon destruction. Note that if the lifetimes of two or more
338 // instances of this class overlap, they must be strictly nested.
339 explicit ScopedCustomUrlRequestTestHttpHost(const std::string& new_value);
341 ~ScopedCustomUrlRequestTestHttpHost();
343 // Returns the current value to be used by HTTP tests in
344 // url_request_unittest.cc .
345 static const std::string& value();
347 private:
348 static std::string value_;
349 const std::string old_value_;
350 const std::string new_value_;
352 DISALLOW_COPY_AND_ASSIGN(ScopedCustomUrlRequestTestHttpHost);
355 //-----------------------------------------------------------------------------
357 // A simple ProtocolHandler that returns a pre-built URLRequestJob only once.
358 class TestJobInterceptor : public URLRequestJobFactory::ProtocolHandler {
359 public:
360 TestJobInterceptor();
362 virtual URLRequestJob* MaybeCreateJob(
363 URLRequest* request,
364 NetworkDelegate* network_delegate) const OVERRIDE;
365 void set_main_intercept_job(URLRequestJob* job);
367 private:
368 mutable URLRequestJob* main_intercept_job_;
371 } // namespace net
373 #endif // NET_URL_REQUEST_URL_REQUEST_TEST_UTIL_H_