OLD | NEW |
(Empty) | |
| 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/ssl_config_service_defaults.h" |
| 27 #include "net/disk_cache/disk_cache.h" |
| 28 #include "net/ftp/ftp_network_layer.h" |
| 29 #include "net/http/http_auth_handler_factory.h" |
| 30 #include "net/http/http_cache.h" |
| 31 #include "net/http/http_network_layer.h" |
| 32 #include "net/test/test_server.h" |
| 33 #include "net/url_request/url_request.h" |
| 34 #include "net/url_request/url_request_context.h" |
| 35 #include "net/proxy/proxy_service.h" |
| 36 #include "testing/gtest/include/gtest/gtest.h" |
| 37 #include "googleurl/src/url_util.h" |
| 38 |
| 39 using base::TimeDelta; |
| 40 |
| 41 //----------------------------------------------------------------------------- |
| 42 |
| 43 class TestCookiePolicy : public net::CookiePolicy { |
| 44 public: |
| 45 enum Options { |
| 46 NO_GET_COOKIES = 1 << 0, |
| 47 NO_SET_COOKIE = 1 << 1, |
| 48 ASYNC = 1 << 2, |
| 49 FORCE_SESSION = 1 << 3, |
| 50 }; |
| 51 |
| 52 explicit TestCookiePolicy(int options_bit_mask); |
| 53 virtual ~TestCookiePolicy(); |
| 54 |
| 55 // net::CookiePolicy: |
| 56 virtual int CanGetCookies(const GURL& url, const GURL& first_party, |
| 57 net::CompletionCallback* callback); |
| 58 virtual int CanSetCookie(const GURL& url, const GURL& first_party, |
| 59 const std::string& cookie_line, |
| 60 net::CompletionCallback* callback); |
| 61 |
| 62 private: |
| 63 void DoGetCookiesPolicy(const GURL& url, const GURL& first_party); |
| 64 void DoSetCookiePolicy(const GURL& url, const GURL& first_party, |
| 65 const std::string& cookie_line); |
| 66 |
| 67 ScopedRunnableMethodFactory<TestCookiePolicy> method_factory_; |
| 68 int options_; |
| 69 net::CompletionCallback* callback_; |
| 70 }; |
| 71 |
| 72 //----------------------------------------------------------------------------- |
| 73 |
| 74 class TestURLRequestContext : public net::URLRequestContext { |
| 75 public: |
| 76 TestURLRequestContext(); |
| 77 explicit TestURLRequestContext(const std::string& proxy); |
| 78 |
| 79 void set_cookie_policy(net::CookiePolicy* policy) { |
| 80 cookie_policy_ = policy; |
| 81 } |
| 82 |
| 83 protected: |
| 84 virtual ~TestURLRequestContext(); |
| 85 |
| 86 private: |
| 87 void Init(); |
| 88 }; |
| 89 |
| 90 //----------------------------------------------------------------------------- |
| 91 |
| 92 class TestURLRequest : public net::URLRequest { |
| 93 public: |
| 94 TestURLRequest(const GURL& url, Delegate* delegate); |
| 95 virtual ~TestURLRequest(); |
| 96 }; |
| 97 |
| 98 //----------------------------------------------------------------------------- |
| 99 |
| 100 class TestDelegate : public net::URLRequest::Delegate { |
| 101 public: |
| 102 TestDelegate(); |
| 103 virtual ~TestDelegate(); |
| 104 |
| 105 void set_cancel_in_received_redirect(bool val) { cancel_in_rr_ = val; } |
| 106 void set_cancel_in_response_started(bool val) { cancel_in_rs_ = val; } |
| 107 void set_cancel_in_received_data(bool val) { cancel_in_rd_ = val; } |
| 108 void set_cancel_in_received_data_pending(bool val) { |
| 109 cancel_in_rd_pending_ = val; |
| 110 } |
| 111 void set_cancel_in_get_cookies_blocked(bool val) { |
| 112 cancel_in_getcookiesblocked_ = val; |
| 113 } |
| 114 void set_cancel_in_set_cookie_blocked(bool val) { |
| 115 cancel_in_setcookieblocked_ = val; |
| 116 } |
| 117 void set_quit_on_complete(bool val) { quit_on_complete_ = val; } |
| 118 void set_quit_on_redirect(bool val) { quit_on_redirect_ = val; } |
| 119 void set_allow_certificate_errors(bool val) { |
| 120 allow_certificate_errors_ = val; |
| 121 } |
| 122 void set_username(const string16& u) { username_ = u; } |
| 123 void set_password(const string16& p) { password_ = p; } |
| 124 |
| 125 // query state |
| 126 const std::string& data_received() const { return data_received_; } |
| 127 int bytes_received() const { return static_cast<int>(data_received_.size()); } |
| 128 int response_started_count() const { return response_started_count_; } |
| 129 int received_redirect_count() const { return received_redirect_count_; } |
| 130 int blocked_get_cookies_count() const { return blocked_get_cookies_count_; } |
| 131 int blocked_set_cookie_count() const { return blocked_set_cookie_count_; } |
| 132 int set_cookie_count() const { return set_cookie_count_; } |
| 133 bool received_data_before_response() const { |
| 134 return received_data_before_response_; |
| 135 } |
| 136 bool request_failed() const { return request_failed_; } |
| 137 bool have_certificate_errors() const { return have_certificate_errors_; } |
| 138 |
| 139 // net::URLRequest::Delegate: |
| 140 virtual void OnReceivedRedirect(net::URLRequest* request, const GURL& new_url, |
| 141 bool* defer_redirect); |
| 142 virtual void OnAuthRequired(net::URLRequest* request, |
| 143 net::AuthChallengeInfo* auth_info); |
| 144 virtual void OnSSLCertificateError(net::URLRequest* request, |
| 145 int cert_error, |
| 146 net::X509Certificate* cert); |
| 147 virtual void OnGetCookies(net::URLRequest* request, bool blocked_by_policy); |
| 148 virtual void OnSetCookie(net::URLRequest* request, |
| 149 const std::string& cookie_line, |
| 150 const net::CookieOptions& options, |
| 151 bool blocked_by_policy); |
| 152 virtual void OnResponseStarted(net::URLRequest* request); |
| 153 virtual void OnReadCompleted(net::URLRequest* request, int bytes_read); |
| 154 |
| 155 private: |
| 156 static const int kBufferSize = 4096; |
| 157 |
| 158 virtual void OnResponseCompleted(net::URLRequest* request); |
| 159 |
| 160 // options for controlling behavior |
| 161 bool cancel_in_rr_; |
| 162 bool cancel_in_rs_; |
| 163 bool cancel_in_rd_; |
| 164 bool cancel_in_rd_pending_; |
| 165 bool cancel_in_getcookiesblocked_; |
| 166 bool cancel_in_setcookieblocked_; |
| 167 bool quit_on_complete_; |
| 168 bool quit_on_redirect_; |
| 169 bool allow_certificate_errors_; |
| 170 |
| 171 string16 username_; |
| 172 string16 password_; |
| 173 |
| 174 // tracks status of callbacks |
| 175 int response_started_count_; |
| 176 int received_bytes_count_; |
| 177 int received_redirect_count_; |
| 178 int blocked_get_cookies_count_; |
| 179 int blocked_set_cookie_count_; |
| 180 int set_cookie_count_; |
| 181 bool received_data_before_response_; |
| 182 bool request_failed_; |
| 183 bool have_certificate_errors_; |
| 184 std::string data_received_; |
| 185 |
| 186 // our read buffer |
| 187 scoped_refptr<net::IOBuffer> buf_; |
| 188 }; |
| 189 |
| 190 #endif // NET_URL_REQUEST_URL_REQUEST_TEST_UTIL_H_ |
OLD | NEW |