Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 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 #include "net/base/layered_network_delegate.h" | |
| 6 | |
| 7 #include <map> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/files/file_path.h" | |
| 11 #include "base/memory/ref_counted.h" | |
| 12 #include "base/memory/scoped_ptr.h" | |
| 13 #include "net/base/auth.h" | |
| 14 #include "net/base/net_errors.h" | |
| 15 #include "net/base/network_delegate_impl.h" | |
| 16 #include "net/base/request_priority.h" | |
| 17 #include "net/base/test_completion_callback.h" | |
| 18 #include "net/http/http_response_headers.h" | |
| 19 #include "net/proxy/proxy_config_service.h" | |
| 20 #include "net/proxy/proxy_info.h" | |
| 21 #include "net/proxy/proxy_service.h" | |
| 22 #include "net/url_request/url_request.h" | |
| 23 #include "net/url_request/url_request_test_util.h" | |
| 24 #include "testing/gtest/include/gtest/gtest.h" | |
| 25 #include "url/gurl.h" | |
| 26 | |
| 27 namespace net { | |
| 28 | |
| 29 typedef std::map<const char*, int> CountersMap; | |
|
mmenke
2014/12/04 20:50:56
Typedef and all other classes besides the test fix
megjablon
2014/12/05 22:24:21
Done.
| |
| 30 | |
| 31 class TestNetworkDelegateImpl : public NetworkDelegateImpl { | |
| 32 public: | |
| 33 TestNetworkDelegateImpl(CountersMap* layered_network_delegate_counters) | |
| 34 : layered_network_delegate_counters_(layered_network_delegate_counters) { | |
|
mmenke
2014/12/04 20:50:57
nit: +1 indent
megjablon
2014/12/05 22:24:21
Done.
| |
| 35 } | |
| 36 | |
| 37 ~TestNetworkDelegateImpl() override { | |
| 38 } | |
| 39 | |
| 40 private: | |
| 41 void IncrementAndCompareCounter(const char* counter_name) { | |
| 42 ++counters_[counter_name]; | |
| 43 EXPECT_EQ((*layered_network_delegate_counters_)[counter_name], | |
| 44 counters_[counter_name]); | |
| 45 } | |
| 46 | |
| 47 int OnBeforeURLRequest(URLRequest* request, | |
|
mmenke
2014/12/04 20:50:56
nit: Suggest making all the overrides public (But
mmenke
2014/12/04 20:50:57
nit: "// NetworkDelegateImpl implementation:"
megjablon
2014/12/05 22:24:21
Done.
| |
| 48 const CompletionCallback& callback, | |
| 49 GURL* new_url) override { | |
| 50 IncrementAndCompareCounter("on_before_url_request_count"); | |
| 51 return OK; | |
| 52 } | |
| 53 | |
| 54 void OnResolveProxy(const GURL& url, | |
| 55 int load_flags, | |
| 56 const ProxyService& proxy_service, | |
| 57 ProxyInfo* result) override { | |
| 58 IncrementAndCompareCounter("on_resolve_proxy_count"); | |
| 59 } | |
| 60 | |
| 61 void OnProxyFallback(const ProxyServer& bad_proxy, int net_error) override { | |
| 62 IncrementAndCompareCounter("on_proxy_fallback_count"); | |
| 63 } | |
| 64 | |
| 65 int OnBeforeSendHeaders(URLRequest* request, | |
| 66 const CompletionCallback& callback, | |
| 67 HttpRequestHeaders* headers) override { | |
| 68 IncrementAndCompareCounter("on_before_send_headers_count"); | |
| 69 return OK; | |
| 70 } | |
| 71 | |
| 72 void OnBeforeSendProxyHeaders(URLRequest* request, | |
| 73 const ProxyInfo& proxy_info, | |
| 74 HttpRequestHeaders* headers) override { | |
| 75 IncrementAndCompareCounter("on_before_send_proxy_headers_count"); | |
| 76 } | |
| 77 | |
| 78 void OnSendHeaders(URLRequest* request, | |
| 79 const HttpRequestHeaders& headers) override { | |
| 80 IncrementAndCompareCounter("on_send_headers_count"); | |
| 81 } | |
| 82 | |
| 83 int OnHeadersReceived( | |
| 84 URLRequest* request, | |
| 85 const CompletionCallback& callback, | |
| 86 const HttpResponseHeaders* original_response_headers, | |
| 87 scoped_refptr<HttpResponseHeaders>* override_response_headers, | |
| 88 GURL* allowed_unsafe_redirect_url) override { | |
| 89 IncrementAndCompareCounter("on_headers_received_count"); | |
| 90 return OK; | |
| 91 } | |
| 92 | |
| 93 void OnBeforeRedirect(URLRequest* request, | |
| 94 const GURL& new_location) override { | |
| 95 IncrementAndCompareCounter("on_before_redirect_count"); | |
| 96 } | |
| 97 | |
| 98 void OnResponseStarted(URLRequest* request) override { | |
| 99 IncrementAndCompareCounter("on_response_started_count"); | |
| 100 } | |
| 101 | |
| 102 void OnRawBytesRead(const URLRequest& request, int bytes_read) override { | |
| 103 IncrementAndCompareCounter("on_raw_bytes_read_count"); | |
| 104 } | |
| 105 | |
| 106 void OnCompleted(URLRequest* request, bool started) override { | |
| 107 IncrementAndCompareCounter("on_completed_count"); | |
| 108 } | |
| 109 | |
| 110 void OnURLRequestDestroyed(URLRequest* request) override { | |
| 111 IncrementAndCompareCounter("on_url_request_destroyed_count"); | |
| 112 } | |
| 113 | |
| 114 void OnPACScriptError(int line_number, const base::string16& error) override { | |
| 115 IncrementAndCompareCounter("on_pac_script_error_count"); | |
| 116 } | |
| 117 | |
| 118 AuthRequiredResponse OnAuthRequired(URLRequest* request, | |
| 119 const AuthChallengeInfo& auth_info, | |
| 120 const AuthCallback& callback, | |
| 121 AuthCredentials* credentials) override { | |
| 122 IncrementAndCompareCounter("on_auth_required_count"); | |
| 123 return NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION; | |
| 124 } | |
| 125 | |
| 126 bool OnCanGetCookies(const URLRequest& request, | |
| 127 const CookieList& cookie_list) override { | |
| 128 IncrementAndCompareCounter("on_can_get_cookies_count"); | |
| 129 return false; | |
| 130 } | |
| 131 | |
| 132 bool OnCanSetCookie(const URLRequest& request, | |
| 133 const std::string& cookie_line, | |
| 134 CookieOptions* options) override { | |
| 135 IncrementAndCompareCounter("on_can_set_cookie_count"); | |
| 136 return false; | |
| 137 } | |
| 138 | |
| 139 bool OnCanAccessFile(const URLRequest& request, const base::FilePath& path) { | |
| 140 IncrementAndCompareCounter("on_can_access_file_count"); | |
| 141 return false; | |
| 142 } | |
| 143 | |
| 144 bool OnCanThrottleRequest(const URLRequest& request) { | |
| 145 IncrementAndCompareCounter("on_can_throttle_request_count"); | |
| 146 return false; | |
| 147 } | |
| 148 | |
| 149 bool OnCanEnablePrivacyMode(const GURL& url, | |
| 150 const GURL& first_party_for_cookies) { | |
| 151 IncrementAndCompareCounter("on_can_enable_privacy_mode_count"); | |
| 152 return false; | |
| 153 } | |
| 154 | |
| 155 bool OnCancelURLRequestWithPolicyViolatingReferrerHeader( | |
| 156 const URLRequest& request, | |
| 157 const GURL& target_url, | |
| 158 const GURL& referrer_url) { | |
| 159 IncrementAndCompareCounter( | |
| 160 "on_cancel_url_request_with_policy_violating_referrer_header_count"); | |
|
mmenke
2014/12/04 20:50:57
+1 indent
megjablon
2014/12/05 22:24:21
Done.
| |
| 161 return false; | |
| 162 } | |
| 163 | |
| 164 mutable CountersMap counters_; | |
| 165 mutable CountersMap* layered_network_delegate_counters_; | |
| 166 | |
| 167 DISALLOW_COPY_AND_ASSIGN(TestNetworkDelegateImpl); | |
|
mmenke
2014/12/04 20:50:56
nit: Should include base/macros.h for DISALLOW_CO
megjablon
2014/12/05 22:24:21
Done.
| |
| 168 }; | |
| 169 | |
| 170 class TestLayeredNetworkDelegate : public LayeredNetworkDelegate { | |
| 171 public: | |
| 172 TestLayeredNetworkDelegate(scoped_ptr<NetworkDelegate> network_delegate, | |
| 173 CountersMap* counters) | |
| 174 : LayeredNetworkDelegate(network_delegate.Pass()), context_(true), | |
| 175 counters_(counters) { | |
| 176 context_.Init(); | |
| 177 } | |
| 178 | |
| 179 ~TestLayeredNetworkDelegate() override { | |
| 180 } | |
| 181 | |
| 182 void CallAndVerify() { | |
| 183 scoped_refptr<AuthChallengeInfo> auth_challenge(new AuthChallengeInfo()); | |
| 184 scoped_ptr<URLRequest> request = | |
| 185 context_.CreateRequest(GURL(), IDLE, &delegate_, NULL); | |
| 186 scoped_ptr<HttpRequestHeaders> request_headers(new HttpRequestHeaders()); | |
| 187 scoped_refptr<HttpResponseHeaders> response_headers( | |
| 188 new HttpResponseHeaders("")); | |
| 189 TestCompletionCallback completion_callback; | |
| 190 scoped_ptr<ProxyService> proxy_service(ProxyService::CreateDirect()); | |
| 191 | |
| 192 EXPECT_EQ(OK, OnBeforeURLRequest(request.get(), | |
| 193 completion_callback.callback(), NULL)); | |
| 194 OnResolveProxy(GURL(), 0, *proxy_service, new ProxyInfo()); | |
| 195 OnProxyFallback(ProxyServer(), 0); | |
| 196 EXPECT_EQ(OK, OnBeforeSendHeaders(NULL, completion_callback.callback(), | |
| 197 request_headers.get())); | |
| 198 OnBeforeSendProxyHeaders(NULL, ProxyInfo(), request_headers.get()); | |
| 199 OnSendHeaders(NULL, *request_headers); | |
| 200 EXPECT_EQ(OK, OnHeadersReceived(NULL, completion_callback.callback(), | |
| 201 response_headers.get(), NULL, NULL)); | |
| 202 OnResponseStarted(request.get()); | |
| 203 OnRawBytesRead(*request, 0); | |
| 204 OnCompleted(request.get(), false); | |
| 205 OnURLRequestDestroyed(request.get()); | |
| 206 OnPACScriptError(0, base::string16()); | |
| 207 EXPECT_EQ(NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION, | |
| 208 OnAuthRequired(request.get(), *auth_challenge, | |
| 209 AuthCallback(), NULL)); | |
| 210 EXPECT_FALSE(OnCanGetCookies(*request, CookieList())); | |
| 211 EXPECT_FALSE(OnCanSetCookie(*request, std::string(), NULL)); | |
| 212 EXPECT_FALSE(OnCanAccessFile(*request, base::FilePath())); | |
| 213 EXPECT_FALSE(OnCanThrottleRequest(*request)); | |
| 214 EXPECT_FALSE(OnCanEnablePrivacyMode(GURL(), GURL())); | |
| 215 EXPECT_FALSE(OnCancelURLRequestWithPolicyViolatingReferrerHeader( | |
| 216 *request, GURL(), GURL())); | |
| 217 } | |
| 218 | |
| 219 private: | |
| 220 void OnBeforeURLRequestInternal(URLRequest* request, | |
| 221 const CompletionCallback& callback, | |
| 222 GURL* new_url) override { | |
| 223 ++(*counters_)["on_before_url_request_count"]; | |
| 224 EXPECT_EQ(1, (*counters_)["on_before_url_request_count"]); | |
| 225 } | |
| 226 | |
| 227 void OnResolveProxyInternal(const GURL& url, | |
| 228 int load_flags, | |
| 229 const ProxyService& proxy_service, | |
| 230 ProxyInfo* result) override { | |
| 231 ++(*counters_)["on_resolve_proxy_count"]; | |
| 232 EXPECT_EQ(1, (*counters_)["on_resolve_proxy_count"]); | |
| 233 } | |
| 234 | |
| 235 void OnProxyFallbackInternal(const ProxyServer& bad_proxy, | |
| 236 int net_error) override { | |
| 237 ++(*counters_)["on_proxy_fallback_count"]; | |
| 238 EXPECT_EQ(1, (*counters_)["on_proxy_fallback_count"]); | |
| 239 } | |
| 240 | |
| 241 void OnBeforeSendHeadersInternal(URLRequest* request, | |
| 242 const CompletionCallback& callback, | |
| 243 HttpRequestHeaders* headers) override { | |
| 244 ++(*counters_)["on_before_send_headers_count"]; | |
| 245 EXPECT_EQ(1, (*counters_)["on_before_send_headers_count"]); | |
| 246 } | |
| 247 | |
| 248 void OnBeforeSendProxyHeadersInternal(URLRequest* request, | |
| 249 const ProxyInfo& proxy_info, | |
| 250 HttpRequestHeaders* headers) override { | |
| 251 ++(*counters_)["on_before_send_proxy_headers_count"]; | |
| 252 EXPECT_EQ(1, (*counters_)["on_before_send_proxy_headers_count"]); | |
| 253 } | |
| 254 | |
| 255 void OnSendHeadersInternal(URLRequest* request, | |
| 256 const HttpRequestHeaders& headers) override { | |
| 257 ++(*counters_)["on_send_headers_count"]; | |
| 258 EXPECT_EQ(1, (*counters_)["on_send_headers_count"]); | |
| 259 } | |
| 260 | |
| 261 void OnHeadersReceivedInternal( | |
| 262 URLRequest* request, | |
| 263 const CompletionCallback& callback, | |
| 264 const HttpResponseHeaders* original_response_headers, | |
| 265 scoped_refptr<HttpResponseHeaders>* override_response_headers, | |
| 266 GURL* allowed_unsafe_redirect_url) override { | |
| 267 ++(*counters_)["on_headers_received_count"]; | |
| 268 EXPECT_EQ(1, (*counters_)["on_headers_received_count"]); | |
| 269 } | |
| 270 | |
| 271 void OnBeforeRedirectInternal(URLRequest* request, | |
| 272 const GURL& new_location) override { | |
| 273 ++(*counters_)["on_before_redirect_count"]; | |
| 274 EXPECT_EQ(1, (*counters_)["on_before_redirect_count"]); | |
| 275 } | |
| 276 | |
| 277 void OnResponseStartedInternal(URLRequest* request) override { | |
| 278 ++(*counters_)["on_response_started_count"]; | |
| 279 EXPECT_EQ(1, (*counters_)["on_response_started_count"]); | |
| 280 } | |
| 281 | |
| 282 void OnRawBytesReadInternal(const URLRequest& request, | |
| 283 int bytes_read) override { | |
| 284 ++(*counters_)["on_raw_bytes_read_count"]; | |
| 285 EXPECT_EQ(1, (*counters_)["on_raw_bytes_read_count"]); | |
| 286 } | |
| 287 | |
| 288 void OnCompletedInternal(URLRequest* request, bool started) override { | |
| 289 ++(*counters_)["on_completed_count"]; | |
| 290 EXPECT_EQ(1, (*counters_)["on_completed_count"]); | |
| 291 } | |
| 292 | |
| 293 void OnURLRequestDestroyedInternal(URLRequest* request) override { | |
| 294 ++(*counters_)["on_url_request_destroyed_count"]; | |
| 295 EXPECT_EQ(1, (*counters_)["on_url_request_destroyed_count"]); | |
| 296 } | |
| 297 | |
| 298 void OnPACScriptErrorInternal(int line_number, | |
| 299 const base::string16& error) override { | |
| 300 ++(*counters_)["on_pac_script_error_count"]; | |
| 301 EXPECT_EQ(1, (*counters_)["on_pac_script_error_count"]); | |
| 302 } | |
| 303 | |
| 304 void OnAuthRequiredInternal(URLRequest* request, | |
| 305 const AuthChallengeInfo& auth_info, | |
| 306 const AuthCallback& callback, | |
| 307 AuthCredentials* credentials) override { | |
| 308 ++(*counters_)["on_auth_required_count"]; | |
| 309 EXPECT_EQ(1, (*counters_)["on_auth_required_count"]); | |
| 310 } | |
| 311 | |
| 312 void OnCanGetCookiesInternal(const URLRequest& request, | |
| 313 const CookieList& cookie_list) override { | |
| 314 ++(*counters_)["on_can_get_cookies_count"]; | |
| 315 EXPECT_EQ(1, (*counters_)["on_can_get_cookies_count"]); | |
| 316 } | |
| 317 | |
| 318 void OnCanSetCookieInternal(const URLRequest& request, | |
| 319 const std::string& cookie_line, | |
| 320 CookieOptions* options) override { | |
| 321 ++(*counters_)["on_can_set_cookie_count"]; | |
| 322 EXPECT_EQ(1, (*counters_)["on_can_set_cookie_count"]); | |
| 323 } | |
| 324 | |
| 325 void OnCanAccessFileInternal(const URLRequest& request, | |
| 326 const base::FilePath& path) const override { | |
| 327 ++(*counters_)["on_can_access_file_count"]; | |
| 328 EXPECT_EQ(1, (*counters_)["on_can_access_file_count"]); | |
| 329 } | |
| 330 | |
| 331 void OnCanThrottleRequestInternal(const URLRequest& request) const override { | |
| 332 ++(*counters_)["on_can_throttle_request_count"]; | |
| 333 EXPECT_EQ(1, (*counters_)["on_can_throttle_request_count"]); | |
| 334 } | |
| 335 | |
| 336 void OnCanEnablePrivacyModeInternal( | |
| 337 const GURL& url, const GURL& first_party_for_cookies) const override { | |
| 338 ++(*counters_)["on_can_enable_privacy_mode_count"]; | |
| 339 EXPECT_EQ(1, (*counters_)["on_can_enable_privacy_mode_count"]); | |
| 340 } | |
| 341 | |
| 342 void OnCancelURLRequestWithPolicyViolatingReferrerHeaderInternal( | |
| 343 const URLRequest& request, | |
| 344 const GURL& target_url, | |
| 345 const GURL& referrer_url) const override { | |
| 346 ++(*counters_)["on_cancel_url_request_with_policy_" | |
| 347 "violating_referrer_header_count"]; | |
| 348 EXPECT_EQ(1, (*counters_)["on_cancel_url_request_with_policy_" | |
| 349 "violating_referrer_header_count"]); | |
| 350 } | |
| 351 | |
| 352 TestURLRequestContext context_; | |
| 353 TestDelegate delegate_; | |
| 354 mutable CountersMap* counters_; | |
| 355 | |
| 356 DISALLOW_COPY_AND_ASSIGN(TestLayeredNetworkDelegate); | |
| 357 }; | |
| 358 | |
| 359 class LayeredNetworkDelegateTest : public testing::Test { | |
| 360 public: | |
| 361 LayeredNetworkDelegateTest() { | |
| 362 scoped_ptr<TestNetworkDelegateImpl> test_network_delegate( | |
| 363 new TestNetworkDelegateImpl(&layered_network_delegate_counters)); | |
| 364 test_network_delegate_ = test_network_delegate.get(); | |
| 365 layered_network_delegate_ = scoped_ptr<TestLayeredNetworkDelegate>( | |
| 366 new TestLayeredNetworkDelegate(test_network_delegate.Pass(), | |
| 367 &layered_network_delegate_counters)); | |
| 368 } | |
| 369 | |
| 370 CountersMap layered_network_delegate_counters; | |
| 371 TestNetworkDelegateImpl* test_network_delegate_; | |
| 372 scoped_ptr<TestLayeredNetworkDelegate> layered_network_delegate_; | |
| 373 }; | |
| 374 | |
| 375 TEST_F(LayeredNetworkDelegateTest, VerifyLayeredNetworkDelegateInternal) { | |
| 376 layered_network_delegate_->CallAndVerify(); | |
| 377 } | |
| 378 | |
| 379 } // namespace net | |
| OLD | NEW |