1 // Copyright (c) 2011 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 #pragma once 8 9 #include <stdlib.h> 10 11 #include <sstream> 12 #include <string> 13 14 #include "base/path_service.h" 15 #include "base/process_util.h" 16 #include "base/string_util.h" 17 #include "base/string16.h" 18 #include "base/time.h" 19 #include "base/utf_string_conversions.h" 20 #include "net/base/cert_verifier.h" 21 #include "net/base/cookie_monster.h" 22 #include "net/base/cookie_policy.h" 23 #include "net/base/host_resolver.h" 24 #include "net/base/io_buffer.h" 25 #include "net/base/net_errors.h" 26 #include "net/base/network_delegate.h" 27 #include "net/base/ssl_config_service_defaults.h" 28 #include "net/disk_cache/disk_cache.h" 29 #include "net/ftp/ftp_network_layer.h" 30 #include "net/http/http_auth_handler_factory.h" 31 #include "net/http/http_cache.h" 32 #include "net/http/http_network_layer.h" 33 #include "net/test/test_server.h" 34 #include "net/url_request/url_request.h" 35 #include "net/url_request/url_request_context.h" 36 #include "net/url_request/url_request_context_storage.h" 37 #include "net/proxy/proxy_service.h" 38 #include "testing/gtest/include/gtest/gtest.h" 39 #include "googleurl/src/url_util.h" 40 41 using base::TimeDelta; 42 43 //----------------------------------------------------------------------------- 44 45 class TestCookiePolicy : public net::CookiePolicy { 46 public: 47 enum Options { 48 NO_GET_COOKIES = 1 << 0, 49 NO_SET_COOKIE = 1 << 1, 50 FORCE_SESSION = 1 << 2, 51 }; 52 53 explicit TestCookiePolicy(int options_bit_mask); 54 virtual ~TestCookiePolicy(); 55 56 // net::CookiePolicy: 57 virtual int CanGetCookies(const GURL& url, const GURL& first_party) const; 58 virtual int CanSetCookie(const GURL& url, 59 const GURL& first_party, 60 const std::string& cookie_line) const; 61 62 private: 63 int options_; 64 }; 65 66 //----------------------------------------------------------------------------- 67 68 class TestURLRequestContext : public net::URLRequestContext { 69 public: 70 TestURLRequestContext(); 71 explicit TestURLRequestContext(const std::string& proxy); 72 TestURLRequestContext(const std::string& proxy, 73 net::HostResolver* host_resolver); 74 75 protected: 76 virtual ~TestURLRequestContext(); 77 78 private: 79 void Init(); 80 81 net::URLRequestContextStorage context_storage_; 82 }; 83 84 //----------------------------------------------------------------------------- 85 86 class TestURLRequest : public net::URLRequest { 87 public: 88 TestURLRequest(const GURL& url, Delegate* delegate); 89 virtual ~TestURLRequest(); 90 }; 91 92 //----------------------------------------------------------------------------- 93 94 class TestDelegate : public net::URLRequest::Delegate { 95 public: 96 TestDelegate(); 97 virtual ~TestDelegate(); 98 99 void set_cancel_in_received_redirect(bool val) { cancel_in_rr_ = val; } 100 void set_cancel_in_response_started(bool val) { cancel_in_rs_ = val; } 101 void set_cancel_in_received_data(bool val) { cancel_in_rd_ = val; } 102 void set_cancel_in_received_data_pending(bool val) { 103 cancel_in_rd_pending_ = val; 104 } 105 void set_cancel_in_get_cookies_blocked(bool val) { 106 cancel_in_getcookiesblocked_ = val; 107 } 108 void set_cancel_in_set_cookie_blocked(bool val) { 109 cancel_in_setcookieblocked_ = val; 110 } 111 void set_quit_on_complete(bool val) { quit_on_complete_ = val; } 112 void set_quit_on_redirect(bool val) { quit_on_redirect_ = val; } 113 void set_allow_certificate_errors(bool val) { 114 allow_certificate_errors_ = val; 115 } 116 void set_username(const string16& u) { username_ = u; } 117 void set_password(const string16& p) { password_ = p; } 118 119 // query state 120 const std::string& data_received() const { return data_received_; } 121 int bytes_received() const { return static_cast<int>(data_received_.size()); } 122 int response_started_count() const { return response_started_count_; } 123 int received_redirect_count() const { return received_redirect_count_; } 124 int blocked_get_cookies_count() const { return blocked_get_cookies_count_; } 125 int blocked_set_cookie_count() const { return blocked_set_cookie_count_; } 126 int set_cookie_count() const { return set_cookie_count_; } 127 bool received_data_before_response() const { 128 return received_data_before_response_; 129 } 130 bool request_failed() const { return request_failed_; } 131 bool have_certificate_errors() const { return have_certificate_errors_; } 132 133 // net::URLRequest::Delegate: 134 virtual void OnReceivedRedirect(net::URLRequest* request, const GURL& new_url, 135 bool* defer_redirect); 136 virtual void OnAuthRequired(net::URLRequest* request, 137 net::AuthChallengeInfo* auth_info); 138 virtual void OnSSLCertificateError(net::URLRequest* request, 139 int cert_error, 140 net::X509Certificate* cert); 141 virtual void OnGetCookies(net::URLRequest* request, bool blocked_by_policy); 142 virtual void OnSetCookie(net::URLRequest* request, 143 const std::string& cookie_line, 144 const net::CookieOptions& options, 145 bool blocked_by_policy); 146 virtual void OnResponseStarted(net::URLRequest* request); 147 virtual void OnReadCompleted(net::URLRequest* request, int bytes_read); 148 149 private: 150 static const int kBufferSize = 4096; 151 152 virtual void OnResponseCompleted(net::URLRequest* request); 153 154 // options for controlling behavior 155 bool cancel_in_rr_; 156 bool cancel_in_rs_; 157 bool cancel_in_rd_; 158 bool cancel_in_rd_pending_; 159 bool cancel_in_getcookiesblocked_; 160 bool cancel_in_setcookieblocked_; 161 bool quit_on_complete_; 162 bool quit_on_redirect_; 163 bool allow_certificate_errors_; 164 165 string16 username_; 166 string16 password_; 167 168 // tracks status of callbacks 169 int response_started_count_; 170 int received_bytes_count_; 171 int received_redirect_count_; 172 int blocked_get_cookies_count_; 173 int blocked_set_cookie_count_; 174 int set_cookie_count_; 175 bool received_data_before_response_; 176 bool request_failed_; 177 bool have_certificate_errors_; 178 std::string data_received_; 179 180 // our read buffer 181 scoped_refptr<net::IOBuffer> buf_; 182 }; 183 184 //----------------------------------------------------------------------------- 185 186 class TestNetworkDelegate : public net::NetworkDelegate { 187 public: 188 TestNetworkDelegate(); 189 virtual ~TestNetworkDelegate(); 190 191 int last_os_error() const { return last_os_error_; } 192 int error_count() const { return error_count_; } 193 int created_requests() const { return created_requests_; } 194 int destroyed_requests() const { return destroyed_requests_; } 195 196 protected: 197 // net::NetworkDelegate: 198 virtual int OnBeforeURLRequest(net::URLRequest* request, 199 net::CompletionCallback* callback, 200 GURL* new_url); 201 virtual int OnBeforeSendHeaders(uint64 request_id, 202 net::CompletionCallback* callback, 203 net::HttpRequestHeaders* headers); 204 virtual void OnResponseStarted(net::URLRequest* request); 205 virtual void OnReadCompleted(net::URLRequest* request, int bytes_read); 206 virtual void OnURLRequestDestroyed(net::URLRequest* request); 207 virtual net::URLRequestJob* OnMaybeCreateURLRequestJob( 208 net::URLRequest* request); 209 210 int last_os_error_; 211 int error_count_; 212 int created_requests_; 213 int destroyed_requests_; 214 }; 215 216 #endif // NET_URL_REQUEST_URL_REQUEST_TEST_UTIL_H_ 217