Enables compositing support for webview.
[chromium-blink-merge.git] / net / url_request / url_request_test_util.h
blob488d5f2868f4d8a5c1dccd1fbda10bd0be80b8dd
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_proxy.h"
17 #include "base/path_service.h"
18 #include "base/process_util.h"
19 #include "base/string16.h"
20 #include "base/string_util.h"
21 #include "base/time.h"
22 #include "base/utf_string_conversions.h"
23 #include "googleurl/src/url_util.h"
24 #include "net/base/cert_verifier.h"
25 #include "net/base/io_buffer.h"
26 #include "net/base/net_errors.h"
27 #include "net/base/network_delegate.h"
28 #include "net/base/ssl_config_service_defaults.h"
29 #include "net/cookies/cookie_monster.h"
30 #include "net/disk_cache/disk_cache.h"
31 #include "net/ftp/ftp_network_layer.h"
32 #include "net/http/http_auth_handler_factory.h"
33 #include "net/http/http_cache.h"
34 #include "net/http/http_network_layer.h"
35 #include "net/proxy/proxy_service.h"
36 #include "net/url_request/url_request.h"
37 #include "net/url_request/url_request_context.h"
38 #include "net/url_request/url_request_context_getter.h"
39 #include "net/url_request/url_request_context_storage.h"
40 #include "net/url_request/url_request_job_factory.h"
42 using base::TimeDelta;
44 namespace net {
46 //-----------------------------------------------------------------------------
48 class TestURLRequestContext : public URLRequestContext {
49 public:
50 TestURLRequestContext();
51 // Default constructor like TestURLRequestContext() but does not call
52 // Init() in case |delay_initialization| is true. This allows modifying the
53 // URLRequestContext before it is constructed completely. If
54 // |delay_initialization| is true, Init() needs be be called manually.
55 explicit TestURLRequestContext(bool delay_initialization);
56 virtual ~TestURLRequestContext();
58 void Init();
60 private:
61 bool initialized_;
63 protected:
64 URLRequestContextStorage context_storage_;
67 //-----------------------------------------------------------------------------
69 // Used to return a dummy context, which lives on the message loop
70 // given in the constructor.
71 class TestURLRequestContextGetter : public URLRequestContextGetter {
72 public:
73 // |network_task_runner| must not be NULL.
74 explicit TestURLRequestContextGetter(
75 const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner);
77 // Use to pass a pre-initialized |context|.
78 TestURLRequestContextGetter(
79 const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner,
80 scoped_ptr<TestURLRequestContext> context);
82 // URLRequestContextGetter implementation.
83 virtual TestURLRequestContext* GetURLRequestContext() OVERRIDE;
84 virtual scoped_refptr<base::SingleThreadTaskRunner>
85 GetNetworkTaskRunner() const OVERRIDE;
87 protected:
88 virtual ~TestURLRequestContextGetter();
90 private:
91 const scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_;
92 scoped_ptr<TestURLRequestContext> context_;
95 //-----------------------------------------------------------------------------
97 class TestURLRequest : public URLRequest {
98 public:
99 TestURLRequest(
100 const GURL& url, Delegate* delegate, TestURLRequestContext* context);
101 virtual ~TestURLRequest();
104 //-----------------------------------------------------------------------------
106 class TestDelegate : public URLRequest::Delegate {
107 public:
108 TestDelegate();
109 virtual ~TestDelegate();
111 void set_cancel_in_received_redirect(bool val) { cancel_in_rr_ = val; }
112 void set_cancel_in_response_started(bool val) { cancel_in_rs_ = val; }
113 void set_cancel_in_received_data(bool val) { cancel_in_rd_ = val; }
114 void set_cancel_in_received_data_pending(bool val) {
115 cancel_in_rd_pending_ = val;
117 void set_quit_on_complete(bool val) { quit_on_complete_ = val; }
118 void set_quit_on_redirect(bool val) { quit_on_redirect_ = val; }
119 void set_allow_certificate_errors(bool val) {
120 allow_certificate_errors_ = val;
122 void set_credentials(const AuthCredentials& credentials) {
123 credentials_ = credentials;
126 // query state
127 const std::string& data_received() const { return data_received_; }
128 int bytes_received() const { return static_cast<int>(data_received_.size()); }
129 int response_started_count() const { return response_started_count_; }
130 int received_redirect_count() const { return received_redirect_count_; }
131 bool received_data_before_response() const {
132 return received_data_before_response_;
134 bool request_failed() const { return request_failed_; }
135 bool have_certificate_errors() const { return have_certificate_errors_; }
136 bool certificate_errors_are_fatal() const {
137 return certificate_errors_are_fatal_;
139 bool auth_required_called() const { return auth_required_; }
141 // URLRequest::Delegate:
142 virtual void OnReceivedRedirect(URLRequest* request, const GURL& new_url,
143 bool* defer_redirect) OVERRIDE;
144 virtual void OnAuthRequired(URLRequest* request,
145 AuthChallengeInfo* auth_info) OVERRIDE;
146 // NOTE: |fatal| causes |certificate_errors_are_fatal_| to be set to true.
147 // (Unit tests use this as a post-condition.) But for policy, this method
148 // consults |allow_certificate_errors_|.
149 virtual void OnSSLCertificateError(URLRequest* request,
150 const SSLInfo& ssl_info,
151 bool fatal) OVERRIDE;
152 virtual void OnResponseStarted(URLRequest* request) OVERRIDE;
153 virtual void OnReadCompleted(URLRequest* request,
154 int bytes_read) OVERRIDE;
156 private:
157 static const int kBufferSize = 4096;
159 virtual void OnResponseCompleted(URLRequest* request);
161 // options for controlling behavior
162 bool cancel_in_rr_;
163 bool cancel_in_rs_;
164 bool cancel_in_rd_;
165 bool cancel_in_rd_pending_;
166 bool quit_on_complete_;
167 bool quit_on_redirect_;
168 bool allow_certificate_errors_;
169 AuthCredentials credentials_;
171 // tracks status of callbacks
172 int response_started_count_;
173 int received_bytes_count_;
174 int received_redirect_count_;
175 bool received_data_before_response_;
176 bool request_failed_;
177 bool have_certificate_errors_;
178 bool certificate_errors_are_fatal_;
179 bool auth_required_;
180 std::string data_received_;
182 // our read buffer
183 scoped_refptr<IOBuffer> buf_;
186 //-----------------------------------------------------------------------------
188 class TestNetworkDelegate : public NetworkDelegate {
189 public:
190 enum Options {
191 NO_GET_COOKIES = 1 << 0,
192 NO_SET_COOKIE = 1 << 1,
195 TestNetworkDelegate();
196 virtual ~TestNetworkDelegate();
198 void set_cookie_options(int o) {cookie_options_bit_mask_ = o; }
200 int last_error() const { return last_error_; }
201 int error_count() const { return error_count_; }
202 int created_requests() const { return created_requests_; }
203 int destroyed_requests() const { return destroyed_requests_; }
204 int completed_requests() const { return completed_requests_; }
205 int blocked_get_cookies_count() const { return blocked_get_cookies_count_; }
206 int blocked_set_cookie_count() const { return blocked_set_cookie_count_; }
207 int set_cookie_count() const { return set_cookie_count_; }
209 protected:
210 // NetworkDelegate:
211 virtual int OnBeforeURLRequest(URLRequest* request,
212 const CompletionCallback& callback,
213 GURL* new_url) OVERRIDE;
214 virtual int OnBeforeSendHeaders(URLRequest* request,
215 const CompletionCallback& callback,
216 HttpRequestHeaders* headers) OVERRIDE;
217 virtual void OnSendHeaders(URLRequest* request,
218 const HttpRequestHeaders& headers) OVERRIDE;
219 virtual int OnHeadersReceived(
220 URLRequest* request,
221 const CompletionCallback& callback,
222 const HttpResponseHeaders* original_response_headers,
223 scoped_refptr<HttpResponseHeaders>* override_response_headers) OVERRIDE;
224 virtual void OnBeforeRedirect(URLRequest* request,
225 const GURL& new_location) OVERRIDE;
226 virtual void OnResponseStarted(URLRequest* request) OVERRIDE;
227 virtual void OnRawBytesRead(const URLRequest& request,
228 int bytes_read) OVERRIDE;
229 virtual void OnCompleted(URLRequest* request, bool started) OVERRIDE;
230 virtual void OnURLRequestDestroyed(URLRequest* request) OVERRIDE;
231 virtual void OnPACScriptError(int line_number,
232 const string16& error) OVERRIDE;
233 virtual NetworkDelegate::AuthRequiredResponse OnAuthRequired(
234 URLRequest* request,
235 const AuthChallengeInfo& auth_info,
236 const AuthCallback& callback,
237 AuthCredentials* credentials) OVERRIDE;
238 virtual bool OnCanGetCookies(const URLRequest& request,
239 const CookieList& cookie_list) OVERRIDE;
240 virtual bool OnCanSetCookie(const URLRequest& request,
241 const std::string& cookie_line,
242 CookieOptions* options) OVERRIDE;
243 virtual bool OnCanAccessFile(const URLRequest& request,
244 const FilePath& path) const OVERRIDE;
245 virtual bool OnCanThrottleRequest(
246 const URLRequest& request) const OVERRIDE;
247 virtual int OnBeforeSocketStreamConnect(
248 SocketStream* stream,
249 const CompletionCallback& callback) OVERRIDE;
250 virtual void OnRequestWaitStateChange(const URLRequest& request,
251 RequestWaitState state) OVERRIDE;
253 void InitRequestStatesIfNew(int request_id);
255 int last_error_;
256 int error_count_;
257 int created_requests_;
258 int destroyed_requests_;
259 int completed_requests_;
260 int cookie_options_bit_mask_;
261 int blocked_get_cookies_count_;
262 int blocked_set_cookie_count_;
263 int set_cookie_count_;
265 // NetworkDelegate callbacks happen in a particular order (e.g.
266 // OnBeforeURLRequest is always called before OnBeforeSendHeaders).
267 // This bit-set indicates for each request id (key) what events may be sent
268 // next.
269 std::map<int, int> next_states_;
271 // A log that records for each request id (key) the order in which On...
272 // functions were called.
273 std::map<int, std::string> event_order_;
276 // Overrides the host used by the LocalHttpTestServer in
277 // url_request_unittest.cc . This is used by the chrome_frame_net_tests due to
278 // a mysterious bug when tests execute over the loopback adapter. See
279 // http://crbug.com/114369 .
280 class ScopedCustomUrlRequestTestHttpHost {
281 public:
282 // Sets the host name to be used. The previous hostname will be stored and
283 // restored upon destruction. Note that if the lifetimes of two or more
284 // instances of this class overlap, they must be strictly nested.
285 explicit ScopedCustomUrlRequestTestHttpHost(const std::string& new_value);
287 ~ScopedCustomUrlRequestTestHttpHost();
289 // Returns the current value to be used by HTTP tests in
290 // url_request_unittest.cc .
291 static const std::string& value();
293 private:
294 static std::string value_;
295 const std::string old_value_;
296 const std::string new_value_;
298 DISALLOW_COPY_AND_ASSIGN(ScopedCustomUrlRequestTestHttpHost);
301 //-----------------------------------------------------------------------------
303 // A simple ProtocolHandler that returns a pre-built URLRequestJob only once.
304 class TestJobInterceptor : public URLRequestJobFactory::ProtocolHandler {
305 public:
306 TestJobInterceptor();
308 virtual URLRequestJob* MaybeCreateJob(
309 URLRequest* request,
310 NetworkDelegate* network_delegate) const OVERRIDE;
311 void set_main_intercept_job(URLRequestJob* job);
313 private:
314 mutable URLRequestJob* main_intercept_job_;
317 } // namespace net
319 #endif // NET_URL_REQUEST_URL_REQUEST_TEST_UTIL_H_