Home | History | Annotate | Download | only in url_request
      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.
      4 
      5 #ifndef NET_URL_REQUEST_URL_REQUEST_TEST_UTIL_H_
      6 #define NET_URL_REQUEST_URL_REQUEST_TEST_UTIL_H_
      7 
      8 #include <stdlib.h>
      9 
     10 #include <map>
     11 #include <string>
     12 
     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"
     46 
     47 using base::TimeDelta;
     48 
     49 namespace net {
     50 
     51 //-----------------------------------------------------------------------------
     52 
     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();
     62 
     63   void Init();
     64 
     65   ClientSocketFactory* client_socket_factory() {
     66     return client_socket_factory_;
     67   }
     68   void set_client_socket_factory(ClientSocketFactory* factory) {
     69     client_socket_factory_ = factory;
     70   }
     71 
     72   void set_http_network_session_params(
     73       const HttpNetworkSession::Params& params) {
     74   }
     75 
     76   void SetSdchManager(scoped_ptr<SdchManager> sdch_manager) {
     77     context_storage_.set_sdch_manager(sdch_manager.Pass());
     78   }
     79 
     80  private:
     81   bool initialized_;
     82 
     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_;
     87 
     88   // Not owned:
     89   ClientSocketFactory* client_socket_factory_;
     90 
     91  protected:
     92   URLRequestContextStorage context_storage_;
     93 };
     94 
     95 //-----------------------------------------------------------------------------
     96 
     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);
    104 
    105   // Use to pass a pre-initialized |context|.
    106   TestURLRequestContextGetter(
    107       const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner,
    108       scoped_ptr<TestURLRequestContext> context);
    109 
    110   // URLRequestContextGetter implementation.
    111   virtual TestURLRequestContext* GetURLRequestContext() OVERRIDE;
    112   virtual scoped_refptr<base::SingleThreadTaskRunner>
    113       GetNetworkTaskRunner() const OVERRIDE;
    114 
    115  protected:
    116   virtual ~TestURLRequestContextGetter();
    117 
    118  private:
    119   const scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_;
    120   scoped_ptr<TestURLRequestContext> context_;
    121 };
    122 
    123 //-----------------------------------------------------------------------------
    124 
    125 class TestDelegate : public URLRequest::Delegate {
    126  public:
    127   TestDelegate();
    128   virtual ~TestDelegate();
    129 
    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;
    135   }
    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;
    140   }
    141   void set_allow_certificate_errors(bool val) {
    142     allow_certificate_errors_ = val;
    143   }
    144   void set_credentials(const AuthCredentials& credentials) {
    145     credentials_ = credentials;
    146   }
    147 
    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_;
    155   }
    156   bool received_data_before_response() const {
    157     return received_data_before_response_;
    158   }
    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_;
    163   }
    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_;
    168   }
    169   void ClearFullRequestHeaders();
    170 
    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;
    187 
    188  private:
    189   static const int kBufferSize = 4096;
    190 
    191   virtual void OnResponseCompleted(URLRequest* request);
    192 
    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_;
    203 
    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_;
    217 
    218   // our read buffer
    219   scoped_refptr<IOBuffer> buf_;
    220 };
    221 
    222 //-----------------------------------------------------------------------------
    223 
    224 class TestNetworkDelegate : public NetworkDelegate {
    225  public:
    226   enum Options {
    227     NO_GET_COOKIES = 1 << 0,
    228     NO_SET_COOKIE  = 1 << 1,
    229   };
    230 
    231   TestNetworkDelegate();
    232   virtual ~TestNetworkDelegate();
    233 
    234   // Writes the LoadTimingInfo during the most recent call to OnBeforeRedirect.
    235   bool GetLoadTimingInfoBeforeRedirect(
    236       LoadTimingInfo* load_timing_info_before_redirect) const;
    237 
    238   // Same as GetLoadTimingInfoBeforeRedirect, except for calls to
    239   // AuthRequiredResponse.
    240   bool GetLoadTimingInfoBeforeAuth(
    241       LoadTimingInfo* load_timing_info_before_auth) const;
    242 
    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;
    248   }
    249 
    250   void set_allowed_unsafe_redirect_url(GURL allowed_unsafe_redirect_url) {
    251     allowed_unsafe_redirect_url_ = allowed_unsafe_redirect_url;
    252   }
    253 
    254   void set_cookie_options(int o) {cookie_options_bit_mask_ = o; }
    255 
    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_; }
    265 
    266   void set_can_access_files(bool val) { can_access_files_ = val; }
    267   bool can_access_files() const { return can_access_files_; }
    268 
    269   void set_can_throttle_requests(bool val) { can_throttle_requests_ = val; }
    270   bool can_throttle_requests() const { return can_throttle_requests_; }
    271 
    272   void set_cancel_request_with_policy_violating_referrer(bool val) {
    273     cancel_request_with_policy_violating_referrer_ = val;
    274   }
    275 
    276   int observed_before_proxy_headers_sent_callbacks() const {
    277     return observed_before_proxy_headers_sent_callbacks_;
    278   }
    279 
    280   // Last observed proxy in proxy header sent callback.
    281   HostPortPair last_observed_proxy() {
    282     return last_observed_proxy_;
    283   }
    284 
    285  protected:
    286   // NetworkDelegate:
    287   virtual int OnBeforeURLRequest(URLRequest* request,
    288                                  const CompletionCallback& callback,
    289                                  GURL* new_url) OVERRIDE;
    290   virtual int OnBeforeSendHeaders(URLRequest* request,
    291                                   const CompletionCallback& callback,
    292                                   HttpRequestHeaders* headers) OVERRIDE;
    293   virtual void OnBeforeSendProxyHeaders(
    294       net::URLRequest* request,
    295       const net::ProxyInfo& proxy_info,
    296       net::HttpRequestHeaders* headers) OVERRIDE;
    297   virtual void OnSendHeaders(URLRequest* request,
    298                              const HttpRequestHeaders& headers) OVERRIDE;
    299   virtual int OnHeadersReceived(
    300       URLRequest* request,
    301       const CompletionCallback& callback,
    302       const HttpResponseHeaders* original_response_headers,
    303       scoped_refptr<HttpResponseHeaders>* override_response_headers,
    304       GURL* allowed_unsafe_redirect_url) OVERRIDE;
    305   virtual void OnBeforeRedirect(URLRequest* request,
    306                                 const GURL& new_location) OVERRIDE;
    307   virtual void OnResponseStarted(URLRequest* request) OVERRIDE;
    308   virtual void OnRawBytesRead(const URLRequest& request,
    309                               int bytes_read) OVERRIDE;
    310   virtual void OnCompleted(URLRequest* request, bool started) OVERRIDE;
    311   virtual void OnURLRequestDestroyed(URLRequest* request) OVERRIDE;
    312   virtual void OnPACScriptError(int line_number,
    313                                 const base::string16& error) OVERRIDE;
    314   virtual NetworkDelegate::AuthRequiredResponse OnAuthRequired(
    315       URLRequest* request,
    316       const AuthChallengeInfo& auth_info,
    317       const AuthCallback& callback,
    318       AuthCredentials* credentials) OVERRIDE;
    319   virtual bool OnCanGetCookies(const URLRequest& request,
    320                                const CookieList& cookie_list) OVERRIDE;
    321   virtual bool OnCanSetCookie(const URLRequest& request,
    322                               const std::string& cookie_line,
    323                               CookieOptions* options) OVERRIDE;
    324   virtual bool OnCanAccessFile(const URLRequest& request,
    325                                const base::FilePath& path) const OVERRIDE;
    326   virtual bool OnCanThrottleRequest(
    327       const URLRequest& request) const OVERRIDE;
    328   virtual int OnBeforeSocketStreamConnect(
    329       SocketStream* stream,
    330       const CompletionCallback& callback) OVERRIDE;
    331   virtual bool OnCancelURLRequestWithPolicyViolatingReferrerHeader(
    332       const URLRequest& request,
    333       const GURL& target_url,
    334       const GURL& referrer_url) const OVERRIDE;
    335 
    336   void InitRequestStatesIfNew(int request_id);
    337 
    338   GURL redirect_on_headers_received_url_;
    339   // URL marked as safe for redirection at the onHeadersReceived stage.
    340   GURL allowed_unsafe_redirect_url_;
    341 
    342   int last_error_;
    343   int error_count_;
    344   int created_requests_;
    345   int destroyed_requests_;
    346   int completed_requests_;
    347   int canceled_requests_;
    348   int cookie_options_bit_mask_;
    349   int blocked_get_cookies_count_;
    350   int blocked_set_cookie_count_;
    351   int set_cookie_count_;
    352   int observed_before_proxy_headers_sent_callbacks_;
    353   // Last observed proxy in before proxy header sent callback.
    354   HostPortPair last_observed_proxy_;
    355 
    356   // NetworkDelegate callbacks happen in a particular order (e.g.
    357   // OnBeforeURLRequest is always called before OnBeforeSendHeaders).
    358   // This bit-set indicates for each request id (key) what events may be sent
    359   // next.
    360   std::map<int, int> next_states_;
    361 
    362   // A log that records for each request id (key) the order in which On...
    363   // functions were called.
    364   std::map<int, std::string> event_order_;
    365 
    366   LoadTimingInfo load_timing_info_before_redirect_;
    367   bool has_load_timing_info_before_redirect_;
    368 
    369   LoadTimingInfo load_timing_info_before_auth_;
    370   bool has_load_timing_info_before_auth_;
    371 
    372   bool can_access_files_;  // true by default
    373   bool can_throttle_requests_;  // true by default
    374   bool cancel_request_with_policy_violating_referrer_;  // false by default
    375 };
    376 
    377 // Overrides the host used by the LocalHttpTestServer in
    378 // url_request_unittest.cc . This is used by the chrome_frame_net_tests due to
    379 // a mysterious bug when tests execute over the loopback adapter. See
    380 // http://crbug.com/114369 .
    381 class ScopedCustomUrlRequestTestHttpHost {
    382  public:
    383   // Sets the host name to be used. The previous hostname will be stored and
    384   // restored upon destruction. Note that if the lifetimes of two or more
    385   // instances of this class overlap, they must be strictly nested.
    386   explicit ScopedCustomUrlRequestTestHttpHost(const std::string& new_value);
    387 
    388   ~ScopedCustomUrlRequestTestHttpHost();
    389 
    390   // Returns the current value to be used by HTTP tests in
    391   // url_request_unittest.cc .
    392   static const std::string& value();
    393 
    394  private:
    395   static std::string value_;
    396   const std::string old_value_;
    397   const std::string new_value_;
    398 
    399   DISALLOW_COPY_AND_ASSIGN(ScopedCustomUrlRequestTestHttpHost);
    400 };
    401 
    402 //-----------------------------------------------------------------------------
    403 
    404 // A simple ProtocolHandler that returns a pre-built URLRequestJob only once.
    405 class TestJobInterceptor : public URLRequestJobFactory::ProtocolHandler {
    406  public:
    407   TestJobInterceptor();
    408 
    409   virtual URLRequestJob* MaybeCreateJob(
    410       URLRequest* request,
    411       NetworkDelegate* network_delegate) const OVERRIDE;
    412   void set_main_intercept_job(URLRequestJob* job);
    413 
    414  private:
    415   mutable URLRequestJob* main_intercept_job_;
    416 };
    417 
    418 }  // namespace net
    419 
    420 #endif  // NET_URL_REQUEST_URL_REQUEST_TEST_UTIL_H_
    421