| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef NET_URL_REQUEST_URL_REQUEST_TEST_UTIL_H_ | 5 #ifndef NET_URL_REQUEST_URL_REQUEST_TEST_UTIL_H_ |
| 6 #define NET_URL_REQUEST_URL_REQUEST_TEST_UTIL_H_ | 6 #define NET_URL_REQUEST_URL_REQUEST_TEST_UTIL_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <stdlib.h> | 9 #include <stdlib.h> |
| 10 | 10 |
| 11 #include <sstream> | 11 #include <sstream> |
| 12 #include <string> | 12 #include <string> |
| 13 | 13 |
| 14 #include "base/path_service.h" | 14 #include "base/path_service.h" |
| 15 #include "base/process_util.h" | 15 #include "base/process_util.h" |
| 16 #include "base/string_util.h" | 16 #include "base/string_util.h" |
| 17 #include "base/string16.h" | 17 #include "base/string16.h" |
| 18 #include "base/time.h" | 18 #include "base/time.h" |
| 19 #include "base/utf_string_conversions.h" | 19 #include "base/utf_string_conversions.h" |
| 20 #include "net/base/cert_verifier.h" | 20 #include "net/base/cert_verifier.h" |
| 21 #include "net/base/cookie_monster.h" | 21 #include "net/base/cookie_monster.h" |
| 22 #include "net/base/cookie_policy.h" | |
| 23 #include "net/base/host_resolver.h" | 22 #include "net/base/host_resolver.h" |
| 24 #include "net/base/io_buffer.h" | 23 #include "net/base/io_buffer.h" |
| 25 #include "net/base/net_errors.h" | 24 #include "net/base/net_errors.h" |
| 26 #include "net/base/network_delegate.h" | 25 #include "net/base/network_delegate.h" |
| 27 #include "net/base/ssl_config_service_defaults.h" | 26 #include "net/base/ssl_config_service_defaults.h" |
| 28 #include "net/disk_cache/disk_cache.h" | 27 #include "net/disk_cache/disk_cache.h" |
| 29 #include "net/ftp/ftp_network_layer.h" | 28 #include "net/ftp/ftp_network_layer.h" |
| 30 #include "net/http/http_auth_handler_factory.h" | 29 #include "net/http/http_auth_handler_factory.h" |
| 31 #include "net/http/http_cache.h" | 30 #include "net/http/http_cache.h" |
| 32 #include "net/http/http_network_layer.h" | 31 #include "net/http/http_network_layer.h" |
| 33 #include "net/url_request/url_request.h" | 32 #include "net/url_request/url_request.h" |
| 34 #include "net/url_request/url_request_context.h" | 33 #include "net/url_request/url_request_context.h" |
| 35 #include "net/url_request/url_request_context_storage.h" | 34 #include "net/url_request/url_request_context_storage.h" |
| 36 #include "net/proxy/proxy_service.h" | 35 #include "net/proxy/proxy_service.h" |
| 37 #include "testing/gtest/include/gtest/gtest.h" | 36 #include "testing/gtest/include/gtest/gtest.h" |
| 38 #include "googleurl/src/url_util.h" | 37 #include "googleurl/src/url_util.h" |
| 39 | 38 |
| 40 using base::TimeDelta; | 39 using base::TimeDelta; |
| 41 | 40 |
| 42 namespace net { | 41 namespace net { |
| 43 class HostPortPair; | 42 class HostPortPair; |
| 44 } | 43 } |
| 45 | 44 |
| 46 //----------------------------------------------------------------------------- | 45 //----------------------------------------------------------------------------- |
| 47 | 46 |
| 48 class TestCookiePolicy : public net::CookiePolicy { | |
| 49 public: | |
| 50 enum Options { | |
| 51 NO_GET_COOKIES = 1 << 0, | |
| 52 NO_SET_COOKIE = 1 << 1, | |
| 53 FORCE_SESSION = 1 << 2, | |
| 54 }; | |
| 55 | |
| 56 explicit TestCookiePolicy(int options_bit_mask); | |
| 57 virtual ~TestCookiePolicy(); | |
| 58 | |
| 59 // net::CookiePolicy: | |
| 60 virtual int CanGetCookies(const GURL& url, const GURL& first_party) const; | |
| 61 virtual int CanSetCookie(const GURL& url, | |
| 62 const GURL& first_party, | |
| 63 const std::string& cookie_line) const; | |
| 64 | |
| 65 private: | |
| 66 int options_; | |
| 67 }; | |
| 68 | |
| 69 //----------------------------------------------------------------------------- | |
| 70 | |
| 71 class TestURLRequestContext : public net::URLRequestContext { | 47 class TestURLRequestContext : public net::URLRequestContext { |
| 72 public: | 48 public: |
| 73 TestURLRequestContext(); | 49 TestURLRequestContext(); |
| 74 explicit TestURLRequestContext(const std::string& proxy); | 50 explicit TestURLRequestContext(const std::string& proxy); |
| 75 TestURLRequestContext(const std::string& proxy, | 51 TestURLRequestContext(const std::string& proxy, |
| 76 net::HostResolver* host_resolver); | 52 net::HostResolver* host_resolver); |
| 77 | 53 |
| 78 protected: | 54 protected: |
| 79 virtual ~TestURLRequestContext(); | 55 virtual ~TestURLRequestContext(); |
| 80 | 56 |
| 81 private: | 57 private: |
| 82 void Init(); | 58 void Init(); |
| 83 | 59 |
| 84 net::URLRequestContextStorage context_storage_; | 60 net::URLRequestContextStorage context_storage_; |
| 85 }; | 61 }; |
| 86 | 62 |
| 87 //----------------------------------------------------------------------------- | 63 //----------------------------------------------------------------------------- |
| 88 | 64 |
| 89 class TestURLRequest : public net::URLRequest { | 65 class TestURLRequest : public net::URLRequest { |
| 90 public: | 66 public: |
| 91 TestURLRequest(const GURL& url, Delegate* delegate); | 67 TestURLRequest(const GURL& url, Delegate* delegate); |
| 92 virtual ~TestURLRequest(); | 68 virtual ~TestURLRequest(); |
| 93 }; | 69 }; |
| 94 | 70 |
| 95 //----------------------------------------------------------------------------- | 71 //----------------------------------------------------------------------------- |
| 96 | 72 |
| 97 class TestDelegate : public net::URLRequest::Delegate { | 73 class TestDelegate : public net::URLRequest::Delegate { |
| 98 public: | 74 public: |
| 75 enum Options { |
| 76 NO_GET_COOKIES = 1 << 0, |
| 77 NO_SET_COOKIE = 1 << 1, |
| 78 FORCE_SESSION = 1 << 2, |
| 79 }; |
| 80 |
| 99 TestDelegate(); | 81 TestDelegate(); |
| 100 virtual ~TestDelegate(); | 82 virtual ~TestDelegate(); |
| 101 | 83 |
| 102 void set_cancel_in_received_redirect(bool val) { cancel_in_rr_ = val; } | 84 void set_cancel_in_received_redirect(bool val) { cancel_in_rr_ = val; } |
| 103 void set_cancel_in_response_started(bool val) { cancel_in_rs_ = val; } | 85 void set_cancel_in_response_started(bool val) { cancel_in_rs_ = val; } |
| 104 void set_cancel_in_received_data(bool val) { cancel_in_rd_ = val; } | 86 void set_cancel_in_received_data(bool val) { cancel_in_rd_ = val; } |
| 105 void set_cancel_in_received_data_pending(bool val) { | 87 void set_cancel_in_received_data_pending(bool val) { |
| 106 cancel_in_rd_pending_ = val; | 88 cancel_in_rd_pending_ = val; |
| 107 } | 89 } |
| 108 void set_cancel_in_get_cookies_blocked(bool val) { | 90 void set_cancel_in_get_cookies_blocked(bool val) { |
| 109 cancel_in_getcookiesblocked_ = val; | 91 cancel_in_getcookiesblocked_ = val; |
| 110 } | 92 } |
| 111 void set_cancel_in_set_cookie_blocked(bool val) { | 93 void set_cancel_in_set_cookie_blocked(bool val) { |
| 112 cancel_in_setcookieblocked_ = val; | 94 cancel_in_setcookieblocked_ = val; |
| 113 } | 95 } |
| 114 void set_quit_on_complete(bool val) { quit_on_complete_ = val; } | 96 void set_quit_on_complete(bool val) { quit_on_complete_ = val; } |
| 115 void set_quit_on_redirect(bool val) { quit_on_redirect_ = val; } | 97 void set_quit_on_redirect(bool val) { quit_on_redirect_ = val; } |
| 116 void set_allow_certificate_errors(bool val) { | 98 void set_allow_certificate_errors(bool val) { |
| 117 allow_certificate_errors_ = val; | 99 allow_certificate_errors_ = val; |
| 118 } | 100 } |
| 101 void set_cookie_options(int o) {cookie_options_bit_mask_ = o; } |
| 119 void set_username(const string16& u) { username_ = u; } | 102 void set_username(const string16& u) { username_ = u; } |
| 120 void set_password(const string16& p) { password_ = p; } | 103 void set_password(const string16& p) { password_ = p; } |
| 121 | 104 |
| 122 // query state | 105 // query state |
| 123 const std::string& data_received() const { return data_received_; } | 106 const std::string& data_received() const { return data_received_; } |
| 124 int bytes_received() const { return static_cast<int>(data_received_.size()); } | 107 int bytes_received() const { return static_cast<int>(data_received_.size()); } |
| 125 int response_started_count() const { return response_started_count_; } | 108 int response_started_count() const { return response_started_count_; } |
| 126 int received_redirect_count() const { return received_redirect_count_; } | 109 int received_redirect_count() const { return received_redirect_count_; } |
| 127 int blocked_get_cookies_count() const { return blocked_get_cookies_count_; } | 110 int blocked_get_cookies_count() const { return blocked_get_cookies_count_; } |
| 128 int blocked_set_cookie_count() const { return blocked_set_cookie_count_; } | 111 int blocked_set_cookie_count() const { return blocked_set_cookie_count_; } |
| 129 int set_cookie_count() const { return set_cookie_count_; } | 112 int set_cookie_count() const { return set_cookie_count_; } |
| 130 bool received_data_before_response() const { | 113 bool received_data_before_response() const { |
| 131 return received_data_before_response_; | 114 return received_data_before_response_; |
| 132 } | 115 } |
| 133 bool request_failed() const { return request_failed_; } | 116 bool request_failed() const { return request_failed_; } |
| 134 bool have_certificate_errors() const { return have_certificate_errors_; } | 117 bool have_certificate_errors() const { return have_certificate_errors_; } |
| 135 | 118 |
| 136 // net::URLRequest::Delegate: | 119 // net::URLRequest::Delegate: |
| 137 virtual void OnReceivedRedirect(net::URLRequest* request, const GURL& new_url, | 120 virtual void OnReceivedRedirect(net::URLRequest* request, const GURL& new_url, |
| 138 bool* defer_redirect); | 121 bool* defer_redirect) OVERRIDE; |
| 139 virtual void OnAuthRequired(net::URLRequest* request, | 122 virtual void OnAuthRequired(net::URLRequest* request, |
| 140 net::AuthChallengeInfo* auth_info); | 123 net::AuthChallengeInfo* auth_info) OVERRIDE; |
| 141 virtual void OnSSLCertificateError(net::URLRequest* request, | 124 virtual void OnSSLCertificateError(net::URLRequest* request, |
| 142 int cert_error, | 125 int cert_error, |
| 143 net::X509Certificate* cert); | 126 net::X509Certificate* cert) OVERRIDE; |
| 144 virtual void OnGetCookies(net::URLRequest* request, bool blocked_by_policy); | 127 virtual bool CanGetCookies(net::URLRequest* request) OVERRIDE; |
| 145 virtual void OnSetCookie(net::URLRequest* request, | 128 virtual bool CanSetCookie(net::URLRequest* request, |
| 146 const std::string& cookie_line, | 129 const std::string& cookie_line, |
| 147 const net::CookieOptions& options, | 130 net::CookieOptions* options) OVERRIDE; |
| 148 bool blocked_by_policy); | 131 virtual void OnResponseStarted(net::URLRequest* request) OVERRIDE; |
| 149 virtual void OnResponseStarted(net::URLRequest* request); | 132 virtual void OnReadCompleted(net::URLRequest* request, |
| 150 virtual void OnReadCompleted(net::URLRequest* request, int bytes_read); | 133 int bytes_read) OVERRIDE; |
| 151 | 134 |
| 152 private: | 135 private: |
| 153 static const int kBufferSize = 4096; | 136 static const int kBufferSize = 4096; |
| 154 | 137 |
| 155 virtual void OnResponseCompleted(net::URLRequest* request); | 138 virtual void OnResponseCompleted(net::URLRequest* request); |
| 156 | 139 |
| 157 // options for controlling behavior | 140 // options for controlling behavior |
| 158 bool cancel_in_rr_; | 141 bool cancel_in_rr_; |
| 159 bool cancel_in_rs_; | 142 bool cancel_in_rs_; |
| 160 bool cancel_in_rd_; | 143 bool cancel_in_rd_; |
| 161 bool cancel_in_rd_pending_; | 144 bool cancel_in_rd_pending_; |
| 162 bool cancel_in_getcookiesblocked_; | 145 bool cancel_in_getcookiesblocked_; |
| 163 bool cancel_in_setcookieblocked_; | 146 bool cancel_in_setcookieblocked_; |
| 164 bool quit_on_complete_; | 147 bool quit_on_complete_; |
| 165 bool quit_on_redirect_; | 148 bool quit_on_redirect_; |
| 166 bool allow_certificate_errors_; | 149 bool allow_certificate_errors_; |
| 150 int cookie_options_bit_mask_; |
| 167 | 151 |
| 168 string16 username_; | 152 string16 username_; |
| 169 string16 password_; | 153 string16 password_; |
| 170 | 154 |
| 171 // tracks status of callbacks | 155 // tracks status of callbacks |
| 172 int response_started_count_; | 156 int response_started_count_; |
| 173 int received_bytes_count_; | 157 int received_bytes_count_; |
| 174 int received_redirect_count_; | 158 int received_redirect_count_; |
| 175 int blocked_get_cookies_count_; | 159 int blocked_get_cookies_count_; |
| 176 int blocked_set_cookie_count_; | 160 int blocked_set_cookie_count_; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 net::URLRequest* request); | 200 net::URLRequest* request); |
| 217 virtual void OnPACScriptError(int line_number, const string16& error); | 201 virtual void OnPACScriptError(int line_number, const string16& error); |
| 218 | 202 |
| 219 int last_os_error_; | 203 int last_os_error_; |
| 220 int error_count_; | 204 int error_count_; |
| 221 int created_requests_; | 205 int created_requests_; |
| 222 int destroyed_requests_; | 206 int destroyed_requests_; |
| 223 }; | 207 }; |
| 224 | 208 |
| 225 #endif // NET_URL_REQUEST_URL_REQUEST_TEST_UTIL_H_ | 209 #endif // NET_URL_REQUEST_URL_REQUEST_TEST_UTIL_H_ |
| OLD | NEW |