Integration test for data reduction proxy version header
[chromium-blink-merge.git] / net / url_request / url_request_test_util.h
blob0c342f427e6647b46bf7aefd220ec5d34126dd2b
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.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 virtual ~TestURLRequestContext();
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 virtual TestURLRequestContext* GetURLRequestContext() OVERRIDE;
112 virtual scoped_refptr<base::SingleThreadTaskRunner>
113 GetNetworkTaskRunner() const OVERRIDE;
115 protected:
116 virtual ~TestURLRequestContextGetter();
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 virtual ~TestDelegate();
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 virtual void OnReceivedRedirect(URLRequest* request,
173 const RedirectInfo& redirect_info,
174 bool* defer_redirect) OVERRIDE;
175 virtual void OnBeforeNetworkStart(URLRequest* request, bool* defer) OVERRIDE;
176 virtual 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 virtual void OnSSLCertificateError(URLRequest* request,
182 const SSLInfo& ssl_info,
183 bool fatal) OVERRIDE;
184 virtual void OnResponseStarted(URLRequest* request) OVERRIDE;
185 virtual void OnReadCompleted(URLRequest* request,
186 int bytes_read) OVERRIDE;
188 private:
189 static const int kBufferSize = 4096;
191 virtual void OnResponseCompleted(URLRequest* request);
193 // options for controlling behavior
194 bool cancel_in_rr_;
195 bool cancel_in_rs_;
196 bool cancel_in_rd_;
197 bool cancel_in_rd_pending_;
198 bool quit_on_complete_;
199 bool quit_on_redirect_;
200 bool quit_on_before_network_start_;
201 bool allow_certificate_errors_;
202 AuthCredentials credentials_;
204 // tracks status of callbacks
205 int response_started_count_;
206 int received_bytes_count_;
207 int received_redirect_count_;
208 int received_before_network_start_count_;
209 bool received_data_before_response_;
210 bool request_failed_;
211 bool have_certificate_errors_;
212 bool certificate_errors_are_fatal_;
213 bool auth_required_;
214 std::string data_received_;
215 bool have_full_request_headers_;
216 HttpRequestHeaders full_request_headers_;
218 // our read buffer
219 scoped_refptr<IOBuffer> buf_;
222 //-----------------------------------------------------------------------------
224 class TestNetworkDelegate : public NetworkDelegate {
225 public:
226 enum Options {
227 NO_GET_COOKIES = 1 << 0,
228 NO_SET_COOKIE = 1 << 1,
231 TestNetworkDelegate();
232 virtual ~TestNetworkDelegate();
234 // Writes the LoadTimingInfo during the most recent call to OnBeforeRedirect.
235 bool GetLoadTimingInfoBeforeRedirect(
236 LoadTimingInfo* load_timing_info_before_redirect) const;
238 // Same as GetLoadTimingInfoBeforeRedirect, except for calls to
239 // AuthRequiredResponse.
240 bool GetLoadTimingInfoBeforeAuth(
241 LoadTimingInfo* load_timing_info_before_auth) const;
243 // Will redirect once to the given URL when the next set of headers are
244 // received.
245 void set_redirect_on_headers_received_url(
246 GURL redirect_on_headers_received_url) {
247 redirect_on_headers_received_url_ = redirect_on_headers_received_url;
250 void set_allowed_unsafe_redirect_url(GURL allowed_unsafe_redirect_url) {
251 allowed_unsafe_redirect_url_ = allowed_unsafe_redirect_url;
254 void set_cookie_options(int o) {cookie_options_bit_mask_ = o; }
256 int last_error() const { return last_error_; }
257 int error_count() const { return error_count_; }
258 int created_requests() const { return created_requests_; }
259 int destroyed_requests() const { return destroyed_requests_; }
260 int completed_requests() const { return completed_requests_; }
261 int canceled_requests() const { return canceled_requests_; }
262 int blocked_get_cookies_count() const { return blocked_get_cookies_count_; }
263 int blocked_set_cookie_count() const { return blocked_set_cookie_count_; }
264 int set_cookie_count() const { return set_cookie_count_; }
266 void set_can_access_files(bool val) { can_access_files_ = val; }
267 bool can_access_files() const { return can_access_files_; }
269 void set_can_throttle_requests(bool val) { can_throttle_requests_ = val; }
270 bool can_throttle_requests() const { return can_throttle_requests_; }
272 int observed_before_proxy_headers_sent_callbacks() const {
273 return observed_before_proxy_headers_sent_callbacks_;
276 // Last observed proxy in proxy header sent callback.
277 HostPortPair last_observed_proxy() {
278 return last_observed_proxy_;
281 protected:
282 // NetworkDelegate:
283 virtual int OnBeforeURLRequest(URLRequest* request,
284 const CompletionCallback& callback,
285 GURL* new_url) OVERRIDE;
286 virtual int OnBeforeSendHeaders(URLRequest* request,
287 const CompletionCallback& callback,
288 HttpRequestHeaders* headers) OVERRIDE;
289 virtual void OnBeforeSendProxyHeaders(
290 net::URLRequest* request,
291 const net::ProxyInfo& proxy_info,
292 net::HttpRequestHeaders* headers) OVERRIDE;
293 virtual void OnSendHeaders(URLRequest* request,
294 const HttpRequestHeaders& headers) OVERRIDE;
295 virtual int OnHeadersReceived(
296 URLRequest* request,
297 const CompletionCallback& callback,
298 const HttpResponseHeaders* original_response_headers,
299 scoped_refptr<HttpResponseHeaders>* override_response_headers,
300 GURL* allowed_unsafe_redirect_url) OVERRIDE;
301 virtual void OnBeforeRedirect(URLRequest* request,
302 const GURL& new_location) OVERRIDE;
303 virtual void OnResponseStarted(URLRequest* request) OVERRIDE;
304 virtual void OnRawBytesRead(const URLRequest& request,
305 int bytes_read) OVERRIDE;
306 virtual void OnCompleted(URLRequest* request, bool started) OVERRIDE;
307 virtual void OnURLRequestDestroyed(URLRequest* request) OVERRIDE;
308 virtual void OnPACScriptError(int line_number,
309 const base::string16& error) OVERRIDE;
310 virtual NetworkDelegate::AuthRequiredResponse OnAuthRequired(
311 URLRequest* request,
312 const AuthChallengeInfo& auth_info,
313 const AuthCallback& callback,
314 AuthCredentials* credentials) OVERRIDE;
315 virtual bool OnCanGetCookies(const URLRequest& request,
316 const CookieList& cookie_list) OVERRIDE;
317 virtual bool OnCanSetCookie(const URLRequest& request,
318 const std::string& cookie_line,
319 CookieOptions* options) OVERRIDE;
320 virtual bool OnCanAccessFile(const URLRequest& request,
321 const base::FilePath& path) const OVERRIDE;
322 virtual bool OnCanThrottleRequest(
323 const URLRequest& request) const OVERRIDE;
324 virtual int OnBeforeSocketStreamConnect(
325 SocketStream* stream,
326 const CompletionCallback& callback) OVERRIDE;
328 void InitRequestStatesIfNew(int request_id);
330 GURL redirect_on_headers_received_url_;
331 // URL marked as safe for redirection at the onHeadersReceived stage.
332 GURL allowed_unsafe_redirect_url_;
334 int last_error_;
335 int error_count_;
336 int created_requests_;
337 int destroyed_requests_;
338 int completed_requests_;
339 int canceled_requests_;
340 int cookie_options_bit_mask_;
341 int blocked_get_cookies_count_;
342 int blocked_set_cookie_count_;
343 int set_cookie_count_;
344 int observed_before_proxy_headers_sent_callbacks_;
345 // Last observed proxy in before proxy header sent callback.
346 HostPortPair last_observed_proxy_;
348 // NetworkDelegate callbacks happen in a particular order (e.g.
349 // OnBeforeURLRequest is always called before OnBeforeSendHeaders).
350 // This bit-set indicates for each request id (key) what events may be sent
351 // next.
352 std::map<int, int> next_states_;
354 // A log that records for each request id (key) the order in which On...
355 // functions were called.
356 std::map<int, std::string> event_order_;
358 LoadTimingInfo load_timing_info_before_redirect_;
359 bool has_load_timing_info_before_redirect_;
361 LoadTimingInfo load_timing_info_before_auth_;
362 bool has_load_timing_info_before_auth_;
364 bool can_access_files_; // true by default
365 bool can_throttle_requests_; // true by default
368 // Overrides the host used by the LocalHttpTestServer in
369 // url_request_unittest.cc . This is used by the chrome_frame_net_tests due to
370 // a mysterious bug when tests execute over the loopback adapter. See
371 // http://crbug.com/114369 .
372 class ScopedCustomUrlRequestTestHttpHost {
373 public:
374 // Sets the host name to be used. The previous hostname will be stored and
375 // restored upon destruction. Note that if the lifetimes of two or more
376 // instances of this class overlap, they must be strictly nested.
377 explicit ScopedCustomUrlRequestTestHttpHost(const std::string& new_value);
379 ~ScopedCustomUrlRequestTestHttpHost();
381 // Returns the current value to be used by HTTP tests in
382 // url_request_unittest.cc .
383 static const std::string& value();
385 private:
386 static std::string value_;
387 const std::string old_value_;
388 const std::string new_value_;
390 DISALLOW_COPY_AND_ASSIGN(ScopedCustomUrlRequestTestHttpHost);
393 //-----------------------------------------------------------------------------
395 // A simple ProtocolHandler that returns a pre-built URLRequestJob only once.
396 class TestJobInterceptor : public URLRequestJobFactory::ProtocolHandler {
397 public:
398 TestJobInterceptor();
400 virtual URLRequestJob* MaybeCreateJob(
401 URLRequest* request,
402 NetworkDelegate* network_delegate) const OVERRIDE;
403 void set_main_intercept_job(URLRequestJob* job);
405 private:
406 mutable URLRequestJob* main_intercept_job_;
409 } // namespace net
411 #endif // NET_URL_REQUEST_URL_REQUEST_TEST_UTIL_H_