Add a function to create a bookmark app from a WebApplicationInfo.
[chromium-blink-merge.git] / net / url_request / url_request_test_util.h
blob0b2f48cdbee7095068a14f0fa91bf120b5b9fdde
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 // Will redirect once to the given URL when the next set of headers are
238 // received.
239 void set_redirect_on_headers_received_url(
240 GURL redirect_on_headers_received_url) {
241 redirect_on_headers_received_url_ = redirect_on_headers_received_url;
244 void set_allowed_unsafe_redirect_url(GURL allowed_unsafe_redirect_url) {
245 allowed_unsafe_redirect_url_ = allowed_unsafe_redirect_url;
248 void set_cookie_options(int o) {cookie_options_bit_mask_ = o; }
250 int last_error() const { return last_error_; }
251 int error_count() const { return error_count_; }
252 int created_requests() const { return created_requests_; }
253 int destroyed_requests() const { return destroyed_requests_; }
254 int completed_requests() const { return completed_requests_; }
255 int canceled_requests() const { return canceled_requests_; }
256 int blocked_get_cookies_count() const { return blocked_get_cookies_count_; }
257 int blocked_set_cookie_count() const { return blocked_set_cookie_count_; }
258 int set_cookie_count() const { return set_cookie_count_; }
260 void set_can_access_files(bool val) { can_access_files_ = val; }
261 bool can_access_files() const { return can_access_files_; }
263 void set_can_throttle_requests(bool val) { can_throttle_requests_ = val; }
264 bool can_throttle_requests() const { return can_throttle_requests_; }
266 protected:
267 // NetworkDelegate:
268 virtual int OnBeforeURLRequest(URLRequest* request,
269 const CompletionCallback& callback,
270 GURL* new_url) OVERRIDE;
271 virtual int OnBeforeSendHeaders(URLRequest* request,
272 const CompletionCallback& callback,
273 HttpRequestHeaders* headers) OVERRIDE;
274 virtual void OnSendHeaders(URLRequest* request,
275 const HttpRequestHeaders& headers) OVERRIDE;
276 virtual int OnHeadersReceived(
277 URLRequest* request,
278 const CompletionCallback& callback,
279 const HttpResponseHeaders* original_response_headers,
280 scoped_refptr<HttpResponseHeaders>* override_response_headers,
281 GURL* allowed_unsafe_redirect_url) OVERRIDE;
282 virtual void OnBeforeRedirect(URLRequest* request,
283 const GURL& new_location) OVERRIDE;
284 virtual void OnResponseStarted(URLRequest* request) OVERRIDE;
285 virtual void OnRawBytesRead(const URLRequest& request,
286 int bytes_read) OVERRIDE;
287 virtual void OnCompleted(URLRequest* request, bool started) OVERRIDE;
288 virtual void OnURLRequestDestroyed(URLRequest* request) OVERRIDE;
289 virtual void OnPACScriptError(int line_number,
290 const base::string16& error) OVERRIDE;
291 virtual NetworkDelegate::AuthRequiredResponse OnAuthRequired(
292 URLRequest* request,
293 const AuthChallengeInfo& auth_info,
294 const AuthCallback& callback,
295 AuthCredentials* credentials) OVERRIDE;
296 virtual bool OnCanGetCookies(const URLRequest& request,
297 const CookieList& cookie_list) OVERRIDE;
298 virtual bool OnCanSetCookie(const URLRequest& request,
299 const std::string& cookie_line,
300 CookieOptions* options) OVERRIDE;
301 virtual bool OnCanAccessFile(const URLRequest& request,
302 const base::FilePath& path) const OVERRIDE;
303 virtual bool OnCanThrottleRequest(
304 const URLRequest& request) const OVERRIDE;
305 virtual int OnBeforeSocketStreamConnect(
306 SocketStream* stream,
307 const CompletionCallback& callback) OVERRIDE;
309 void InitRequestStatesIfNew(int request_id);
311 GURL redirect_on_headers_received_url_;
312 // URL marked as safe for redirection at the onHeadersReceived stage.
313 GURL allowed_unsafe_redirect_url_;
315 int last_error_;
316 int error_count_;
317 int created_requests_;
318 int destroyed_requests_;
319 int completed_requests_;
320 int canceled_requests_;
321 int cookie_options_bit_mask_;
322 int blocked_get_cookies_count_;
323 int blocked_set_cookie_count_;
324 int set_cookie_count_;
326 // NetworkDelegate callbacks happen in a particular order (e.g.
327 // OnBeforeURLRequest is always called before OnBeforeSendHeaders).
328 // This bit-set indicates for each request id (key) what events may be sent
329 // next.
330 std::map<int, int> next_states_;
332 // A log that records for each request id (key) the order in which On...
333 // functions were called.
334 std::map<int, std::string> event_order_;
336 LoadTimingInfo load_timing_info_before_redirect_;
337 bool has_load_timing_info_before_redirect_;
339 LoadTimingInfo load_timing_info_before_auth_;
340 bool has_load_timing_info_before_auth_;
342 bool can_access_files_; // true by default
343 bool can_throttle_requests_; // true by default
346 // Overrides the host used by the LocalHttpTestServer in
347 // url_request_unittest.cc . This is used by the chrome_frame_net_tests due to
348 // a mysterious bug when tests execute over the loopback adapter. See
349 // http://crbug.com/114369 .
350 class ScopedCustomUrlRequestTestHttpHost {
351 public:
352 // Sets the host name to be used. The previous hostname will be stored and
353 // restored upon destruction. Note that if the lifetimes of two or more
354 // instances of this class overlap, they must be strictly nested.
355 explicit ScopedCustomUrlRequestTestHttpHost(const std::string& new_value);
357 ~ScopedCustomUrlRequestTestHttpHost();
359 // Returns the current value to be used by HTTP tests in
360 // url_request_unittest.cc .
361 static const std::string& value();
363 private:
364 static std::string value_;
365 const std::string old_value_;
366 const std::string new_value_;
368 DISALLOW_COPY_AND_ASSIGN(ScopedCustomUrlRequestTestHttpHost);
371 //-----------------------------------------------------------------------------
373 // A simple ProtocolHandler that returns a pre-built URLRequestJob only once.
374 class TestJobInterceptor : public URLRequestJobFactory::ProtocolHandler {
375 public:
376 TestJobInterceptor();
378 virtual URLRequestJob* MaybeCreateJob(
379 URLRequest* request,
380 NetworkDelegate* network_delegate) const OVERRIDE;
381 void set_main_intercept_job(URLRequestJob* job);
383 private:
384 mutable URLRequestJob* main_intercept_job_;
387 } // namespace net
389 #endif // NET_URL_REQUEST_URL_REQUEST_TEST_UTIL_H_