Chromium Code Reviews| Index: net/base/layered_network_delegate_unittest.cc |
| diff --git a/net/base/layered_network_delegate_unittest.cc b/net/base/layered_network_delegate_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..935f9cebc6a839bafde9d24eb45e87abdc864142 |
| --- /dev/null |
| +++ b/net/base/layered_network_delegate_unittest.cc |
| @@ -0,0 +1,379 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "net/base/layered_network_delegate.h" |
| + |
| +#include <map> |
| + |
| +#include "base/bind.h" |
| +#include "base/files/file_path.h" |
| +#include "base/memory/ref_counted.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "net/base/auth.h" |
| +#include "net/base/net_errors.h" |
| +#include "net/base/network_delegate_impl.h" |
| +#include "net/base/request_priority.h" |
| +#include "net/base/test_completion_callback.h" |
| +#include "net/http/http_response_headers.h" |
| +#include "net/proxy/proxy_config_service.h" |
| +#include "net/proxy/proxy_info.h" |
| +#include "net/proxy/proxy_service.h" |
| +#include "net/url_request/url_request.h" |
| +#include "net/url_request/url_request_test_util.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "url/gurl.h" |
| + |
| +namespace net { |
| + |
| +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.
|
| + |
| +class TestNetworkDelegateImpl : public NetworkDelegateImpl { |
| + public: |
| + TestNetworkDelegateImpl(CountersMap* layered_network_delegate_counters) |
| + : 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.
|
| + } |
| + |
| + ~TestNetworkDelegateImpl() override { |
| + } |
| + |
| + private: |
| + void IncrementAndCompareCounter(const char* counter_name) { |
| + ++counters_[counter_name]; |
| + EXPECT_EQ((*layered_network_delegate_counters_)[counter_name], |
| + counters_[counter_name]); |
| + } |
| + |
| + 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.
|
| + const CompletionCallback& callback, |
| + GURL* new_url) override { |
| + IncrementAndCompareCounter("on_before_url_request_count"); |
| + return OK; |
| + } |
| + |
| + void OnResolveProxy(const GURL& url, |
| + int load_flags, |
| + const ProxyService& proxy_service, |
| + ProxyInfo* result) override { |
| + IncrementAndCompareCounter("on_resolve_proxy_count"); |
| + } |
| + |
| + void OnProxyFallback(const ProxyServer& bad_proxy, int net_error) override { |
| + IncrementAndCompareCounter("on_proxy_fallback_count"); |
| + } |
| + |
| + int OnBeforeSendHeaders(URLRequest* request, |
| + const CompletionCallback& callback, |
| + HttpRequestHeaders* headers) override { |
| + IncrementAndCompareCounter("on_before_send_headers_count"); |
| + return OK; |
| + } |
| + |
| + void OnBeforeSendProxyHeaders(URLRequest* request, |
| + const ProxyInfo& proxy_info, |
| + HttpRequestHeaders* headers) override { |
| + IncrementAndCompareCounter("on_before_send_proxy_headers_count"); |
| + } |
| + |
| + void OnSendHeaders(URLRequest* request, |
| + const HttpRequestHeaders& headers) override { |
| + IncrementAndCompareCounter("on_send_headers_count"); |
| + } |
| + |
| + int OnHeadersReceived( |
| + URLRequest* request, |
| + const CompletionCallback& callback, |
| + const HttpResponseHeaders* original_response_headers, |
| + scoped_refptr<HttpResponseHeaders>* override_response_headers, |
| + GURL* allowed_unsafe_redirect_url) override { |
| + IncrementAndCompareCounter("on_headers_received_count"); |
| + return OK; |
| + } |
| + |
| + void OnBeforeRedirect(URLRequest* request, |
| + const GURL& new_location) override { |
| + IncrementAndCompareCounter("on_before_redirect_count"); |
| + } |
| + |
| + void OnResponseStarted(URLRequest* request) override { |
| + IncrementAndCompareCounter("on_response_started_count"); |
| + } |
| + |
| + void OnRawBytesRead(const URLRequest& request, int bytes_read) override { |
| + IncrementAndCompareCounter("on_raw_bytes_read_count"); |
| + } |
| + |
| + void OnCompleted(URLRequest* request, bool started) override { |
| + IncrementAndCompareCounter("on_completed_count"); |
| + } |
| + |
| + void OnURLRequestDestroyed(URLRequest* request) override { |
| + IncrementAndCompareCounter("on_url_request_destroyed_count"); |
| + } |
| + |
| + void OnPACScriptError(int line_number, const base::string16& error) override { |
| + IncrementAndCompareCounter("on_pac_script_error_count"); |
| + } |
| + |
| + AuthRequiredResponse OnAuthRequired(URLRequest* request, |
| + const AuthChallengeInfo& auth_info, |
| + const AuthCallback& callback, |
| + AuthCredentials* credentials) override { |
| + IncrementAndCompareCounter("on_auth_required_count"); |
| + return NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION; |
| + } |
| + |
| + bool OnCanGetCookies(const URLRequest& request, |
| + const CookieList& cookie_list) override { |
| + IncrementAndCompareCounter("on_can_get_cookies_count"); |
| + return false; |
| + } |
| + |
| + bool OnCanSetCookie(const URLRequest& request, |
| + const std::string& cookie_line, |
| + CookieOptions* options) override { |
| + IncrementAndCompareCounter("on_can_set_cookie_count"); |
| + return false; |
| + } |
| + |
| + bool OnCanAccessFile(const URLRequest& request, const base::FilePath& path) { |
| + IncrementAndCompareCounter("on_can_access_file_count"); |
| + return false; |
| + } |
| + |
| + bool OnCanThrottleRequest(const URLRequest& request) { |
| + IncrementAndCompareCounter("on_can_throttle_request_count"); |
| + return false; |
| + } |
| + |
| + bool OnCanEnablePrivacyMode(const GURL& url, |
| + const GURL& first_party_for_cookies) { |
| + IncrementAndCompareCounter("on_can_enable_privacy_mode_count"); |
| + return false; |
| + } |
| + |
| + bool OnCancelURLRequestWithPolicyViolatingReferrerHeader( |
| + const URLRequest& request, |
| + const GURL& target_url, |
| + const GURL& referrer_url) { |
| + IncrementAndCompareCounter( |
| + "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.
|
| + return false; |
| + } |
| + |
| + mutable CountersMap counters_; |
| + mutable CountersMap* layered_network_delegate_counters_; |
| + |
| + 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.
|
| +}; |
| + |
| +class TestLayeredNetworkDelegate : public LayeredNetworkDelegate { |
| + public: |
| + TestLayeredNetworkDelegate(scoped_ptr<NetworkDelegate> network_delegate, |
| + CountersMap* counters) |
| + : LayeredNetworkDelegate(network_delegate.Pass()), context_(true), |
| + counters_(counters) { |
| + context_.Init(); |
| + } |
| + |
| + ~TestLayeredNetworkDelegate() override { |
| + } |
| + |
| + void CallAndVerify() { |
| + scoped_refptr<AuthChallengeInfo> auth_challenge(new AuthChallengeInfo()); |
| + scoped_ptr<URLRequest> request = |
| + context_.CreateRequest(GURL(), IDLE, &delegate_, NULL); |
| + scoped_ptr<HttpRequestHeaders> request_headers(new HttpRequestHeaders()); |
| + scoped_refptr<HttpResponseHeaders> response_headers( |
| + new HttpResponseHeaders("")); |
| + TestCompletionCallback completion_callback; |
| + scoped_ptr<ProxyService> proxy_service(ProxyService::CreateDirect()); |
| + |
| + EXPECT_EQ(OK, OnBeforeURLRequest(request.get(), |
| + completion_callback.callback(), NULL)); |
| + OnResolveProxy(GURL(), 0, *proxy_service, new ProxyInfo()); |
| + OnProxyFallback(ProxyServer(), 0); |
| + EXPECT_EQ(OK, OnBeforeSendHeaders(NULL, completion_callback.callback(), |
| + request_headers.get())); |
| + OnBeforeSendProxyHeaders(NULL, ProxyInfo(), request_headers.get()); |
| + OnSendHeaders(NULL, *request_headers); |
| + EXPECT_EQ(OK, OnHeadersReceived(NULL, completion_callback.callback(), |
| + response_headers.get(), NULL, NULL)); |
| + OnResponseStarted(request.get()); |
| + OnRawBytesRead(*request, 0); |
| + OnCompleted(request.get(), false); |
| + OnURLRequestDestroyed(request.get()); |
| + OnPACScriptError(0, base::string16()); |
| + EXPECT_EQ(NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION, |
| + OnAuthRequired(request.get(), *auth_challenge, |
| + AuthCallback(), NULL)); |
| + EXPECT_FALSE(OnCanGetCookies(*request, CookieList())); |
| + EXPECT_FALSE(OnCanSetCookie(*request, std::string(), NULL)); |
| + EXPECT_FALSE(OnCanAccessFile(*request, base::FilePath())); |
| + EXPECT_FALSE(OnCanThrottleRequest(*request)); |
| + EXPECT_FALSE(OnCanEnablePrivacyMode(GURL(), GURL())); |
| + EXPECT_FALSE(OnCancelURLRequestWithPolicyViolatingReferrerHeader( |
| + *request, GURL(), GURL())); |
| + } |
| + |
| + private: |
| + void OnBeforeURLRequestInternal(URLRequest* request, |
| + const CompletionCallback& callback, |
| + GURL* new_url) override { |
| + ++(*counters_)["on_before_url_request_count"]; |
| + EXPECT_EQ(1, (*counters_)["on_before_url_request_count"]); |
| + } |
| + |
| + void OnResolveProxyInternal(const GURL& url, |
| + int load_flags, |
| + const ProxyService& proxy_service, |
| + ProxyInfo* result) override { |
| + ++(*counters_)["on_resolve_proxy_count"]; |
| + EXPECT_EQ(1, (*counters_)["on_resolve_proxy_count"]); |
| + } |
| + |
| + void OnProxyFallbackInternal(const ProxyServer& bad_proxy, |
| + int net_error) override { |
| + ++(*counters_)["on_proxy_fallback_count"]; |
| + EXPECT_EQ(1, (*counters_)["on_proxy_fallback_count"]); |
| + } |
| + |
| + void OnBeforeSendHeadersInternal(URLRequest* request, |
| + const CompletionCallback& callback, |
| + HttpRequestHeaders* headers) override { |
| + ++(*counters_)["on_before_send_headers_count"]; |
| + EXPECT_EQ(1, (*counters_)["on_before_send_headers_count"]); |
| + } |
| + |
| + void OnBeforeSendProxyHeadersInternal(URLRequest* request, |
| + const ProxyInfo& proxy_info, |
| + HttpRequestHeaders* headers) override { |
| + ++(*counters_)["on_before_send_proxy_headers_count"]; |
| + EXPECT_EQ(1, (*counters_)["on_before_send_proxy_headers_count"]); |
| + } |
| + |
| + void OnSendHeadersInternal(URLRequest* request, |
| + const HttpRequestHeaders& headers) override { |
| + ++(*counters_)["on_send_headers_count"]; |
| + EXPECT_EQ(1, (*counters_)["on_send_headers_count"]); |
| + } |
| + |
| + void OnHeadersReceivedInternal( |
| + URLRequest* request, |
| + const CompletionCallback& callback, |
| + const HttpResponseHeaders* original_response_headers, |
| + scoped_refptr<HttpResponseHeaders>* override_response_headers, |
| + GURL* allowed_unsafe_redirect_url) override { |
| + ++(*counters_)["on_headers_received_count"]; |
| + EXPECT_EQ(1, (*counters_)["on_headers_received_count"]); |
| + } |
| + |
| + void OnBeforeRedirectInternal(URLRequest* request, |
| + const GURL& new_location) override { |
| + ++(*counters_)["on_before_redirect_count"]; |
| + EXPECT_EQ(1, (*counters_)["on_before_redirect_count"]); |
| + } |
| + |
| + void OnResponseStartedInternal(URLRequest* request) override { |
| + ++(*counters_)["on_response_started_count"]; |
| + EXPECT_EQ(1, (*counters_)["on_response_started_count"]); |
| + } |
| + |
| + void OnRawBytesReadInternal(const URLRequest& request, |
| + int bytes_read) override { |
| + ++(*counters_)["on_raw_bytes_read_count"]; |
| + EXPECT_EQ(1, (*counters_)["on_raw_bytes_read_count"]); |
| + } |
| + |
| + void OnCompletedInternal(URLRequest* request, bool started) override { |
| + ++(*counters_)["on_completed_count"]; |
| + EXPECT_EQ(1, (*counters_)["on_completed_count"]); |
| + } |
| + |
| + void OnURLRequestDestroyedInternal(URLRequest* request) override { |
| + ++(*counters_)["on_url_request_destroyed_count"]; |
| + EXPECT_EQ(1, (*counters_)["on_url_request_destroyed_count"]); |
| + } |
| + |
| + void OnPACScriptErrorInternal(int line_number, |
| + const base::string16& error) override { |
| + ++(*counters_)["on_pac_script_error_count"]; |
| + EXPECT_EQ(1, (*counters_)["on_pac_script_error_count"]); |
| + } |
| + |
| + void OnAuthRequiredInternal(URLRequest* request, |
| + const AuthChallengeInfo& auth_info, |
| + const AuthCallback& callback, |
| + AuthCredentials* credentials) override { |
| + ++(*counters_)["on_auth_required_count"]; |
| + EXPECT_EQ(1, (*counters_)["on_auth_required_count"]); |
| + } |
| + |
| + void OnCanGetCookiesInternal(const URLRequest& request, |
| + const CookieList& cookie_list) override { |
| + ++(*counters_)["on_can_get_cookies_count"]; |
| + EXPECT_EQ(1, (*counters_)["on_can_get_cookies_count"]); |
| + } |
| + |
| + void OnCanSetCookieInternal(const URLRequest& request, |
| + const std::string& cookie_line, |
| + CookieOptions* options) override { |
| + ++(*counters_)["on_can_set_cookie_count"]; |
| + EXPECT_EQ(1, (*counters_)["on_can_set_cookie_count"]); |
| + } |
| + |
| + void OnCanAccessFileInternal(const URLRequest& request, |
| + const base::FilePath& path) const override { |
| + ++(*counters_)["on_can_access_file_count"]; |
| + EXPECT_EQ(1, (*counters_)["on_can_access_file_count"]); |
| + } |
| + |
| + void OnCanThrottleRequestInternal(const URLRequest& request) const override { |
| + ++(*counters_)["on_can_throttle_request_count"]; |
| + EXPECT_EQ(1, (*counters_)["on_can_throttle_request_count"]); |
| + } |
| + |
| + void OnCanEnablePrivacyModeInternal( |
| + const GURL& url, const GURL& first_party_for_cookies) const override { |
| + ++(*counters_)["on_can_enable_privacy_mode_count"]; |
| + EXPECT_EQ(1, (*counters_)["on_can_enable_privacy_mode_count"]); |
| + } |
| + |
| + void OnCancelURLRequestWithPolicyViolatingReferrerHeaderInternal( |
| + const URLRequest& request, |
| + const GURL& target_url, |
| + const GURL& referrer_url) const override { |
| + ++(*counters_)["on_cancel_url_request_with_policy_" |
| + "violating_referrer_header_count"]; |
| + EXPECT_EQ(1, (*counters_)["on_cancel_url_request_with_policy_" |
| + "violating_referrer_header_count"]); |
| + } |
| + |
| + TestURLRequestContext context_; |
| + TestDelegate delegate_; |
| + mutable CountersMap* counters_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(TestLayeredNetworkDelegate); |
| +}; |
| + |
| +class LayeredNetworkDelegateTest : public testing::Test { |
| + public: |
| + LayeredNetworkDelegateTest() { |
| + scoped_ptr<TestNetworkDelegateImpl> test_network_delegate( |
| + new TestNetworkDelegateImpl(&layered_network_delegate_counters)); |
| + test_network_delegate_ = test_network_delegate.get(); |
| + layered_network_delegate_ = scoped_ptr<TestLayeredNetworkDelegate>( |
| + new TestLayeredNetworkDelegate(test_network_delegate.Pass(), |
| + &layered_network_delegate_counters)); |
| + } |
| + |
| + CountersMap layered_network_delegate_counters; |
| + TestNetworkDelegateImpl* test_network_delegate_; |
| + scoped_ptr<TestLayeredNetworkDelegate> layered_network_delegate_; |
| +}; |
| + |
| +TEST_F(LayeredNetworkDelegateTest, VerifyLayeredNetworkDelegateInternal) { |
| + layered_network_delegate_->CallAndVerify(); |
| +} |
| + |
| +} // namespace net |