| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 #include "net/base/layered_network_delegate.h" | 5 #include "net/base/layered_network_delegate.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "net/base/auth.h" | 15 #include "net/base/auth.h" |
| 16 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
| 17 #include "net/base/network_delegate_impl.h" | 17 #include "net/base/network_delegate_impl.h" |
| 18 #include "net/base/request_priority.h" | 18 #include "net/base/request_priority.h" |
| 19 #include "net/base/test_completion_callback.h" | 19 #include "net/base/test_completion_callback.h" |
| 20 #include "net/http/http_response_headers.h" | 20 #include "net/http/http_response_headers.h" |
| 21 #include "net/proxy/proxy_config_service.h" | 21 #include "net/proxy/proxy_config_service.h" |
| 22 #include "net/proxy/proxy_info.h" | 22 #include "net/proxy/proxy_info.h" |
| 23 #include "net/proxy/proxy_service.h" |
| 23 #include "net/url_request/url_request.h" | 24 #include "net/url_request/url_request.h" |
| 24 #include "net/url_request/url_request_test_util.h" | 25 #include "net/url_request/url_request_test_util.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
| 26 #include "url/gurl.h" | 27 #include "url/gurl.h" |
| 27 | 28 |
| 28 namespace net { | 29 namespace net { |
| 29 namespace { | 30 namespace { |
| 30 | 31 |
| 31 typedef std::map<const char*, int> CountersMap; | 32 typedef std::map<const char*, int> CountersMap; |
| 32 | 33 |
| 33 class TestNetworkDelegateImpl : public NetworkDelegateImpl { | 34 class TestNetworkDelegateImpl : public NetworkDelegateImpl { |
| 34 public: | 35 public: |
| 35 TestNetworkDelegateImpl(CountersMap* layered_network_delegate_counters) | 36 TestNetworkDelegateImpl(CountersMap* layered_network_delegate_counters) |
| 36 : layered_network_delegate_counters_(layered_network_delegate_counters) {} | 37 : layered_network_delegate_counters_(layered_network_delegate_counters) {} |
| 37 | 38 |
| 38 ~TestNetworkDelegateImpl() override {} | 39 ~TestNetworkDelegateImpl() override {} |
| 39 | 40 |
| 40 // NetworkDelegateImpl implementation: | 41 // NetworkDelegateImpl implementation: |
| 41 int OnBeforeURLRequest(URLRequest* request, | 42 int OnBeforeURLRequest(URLRequest* request, |
| 42 const CompletionCallback& callback, | 43 const CompletionCallback& callback, |
| 43 GURL* new_url) override { | 44 GURL* new_url) override { |
| 44 IncrementAndCompareCounter("on_before_url_request_count"); | 45 IncrementAndCompareCounter("on_before_url_request_count"); |
| 45 return OK; | 46 return OK; |
| 46 } | 47 } |
| 47 | 48 |
| 49 void OnResolveProxy(const GURL& url, |
| 50 int load_flags, |
| 51 const ProxyService& proxy_service, |
| 52 ProxyInfo* result) override { |
| 53 IncrementAndCompareCounter("on_resolve_proxy_count"); |
| 54 } |
| 55 |
| 56 void OnProxyFallback(const ProxyServer& bad_proxy, int net_error) override { |
| 57 IncrementAndCompareCounter("on_proxy_fallback_count"); |
| 58 } |
| 59 |
| 48 int OnBeforeSendHeaders(URLRequest* request, | 60 int OnBeforeSendHeaders(URLRequest* request, |
| 49 const CompletionCallback& callback, | 61 const CompletionCallback& callback, |
| 50 HttpRequestHeaders* headers) override { | 62 HttpRequestHeaders* headers) override { |
| 51 IncrementAndCompareCounter("on_before_send_headers_count"); | 63 IncrementAndCompareCounter("on_before_send_headers_count"); |
| 52 return OK; | 64 return OK; |
| 53 } | 65 } |
| 54 | 66 |
| 55 void OnBeforeSendProxyHeaders(URLRequest* request, | 67 void OnBeforeSendProxyHeaders(URLRequest* request, |
| 56 const ProxyInfo& proxy_info, | 68 const ProxyInfo& proxy_info, |
| 57 HttpRequestHeaders* headers) override { | 69 HttpRequestHeaders* headers) override { |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 ~TestLayeredNetworkDelegate() override {} | 184 ~TestLayeredNetworkDelegate() override {} |
| 173 | 185 |
| 174 void CallAndVerify() { | 186 void CallAndVerify() { |
| 175 scoped_refptr<AuthChallengeInfo> auth_challenge(new AuthChallengeInfo()); | 187 scoped_refptr<AuthChallengeInfo> auth_challenge(new AuthChallengeInfo()); |
| 176 scoped_ptr<URLRequest> request = | 188 scoped_ptr<URLRequest> request = |
| 177 context_.CreateRequest(GURL(), IDLE, &delegate_); | 189 context_.CreateRequest(GURL(), IDLE, &delegate_); |
| 178 scoped_ptr<HttpRequestHeaders> request_headers(new HttpRequestHeaders()); | 190 scoped_ptr<HttpRequestHeaders> request_headers(new HttpRequestHeaders()); |
| 179 scoped_refptr<HttpResponseHeaders> response_headers( | 191 scoped_refptr<HttpResponseHeaders> response_headers( |
| 180 new HttpResponseHeaders("")); | 192 new HttpResponseHeaders("")); |
| 181 TestCompletionCallback completion_callback; | 193 TestCompletionCallback completion_callback; |
| 194 scoped_ptr<ProxyService> proxy_service(ProxyService::CreateDirect()); |
| 195 scoped_ptr<ProxyInfo> proxy_info(new ProxyInfo()); |
| 182 | 196 |
| 183 EXPECT_EQ(OK, OnBeforeURLRequest(request.get(), | 197 EXPECT_EQ(OK, OnBeforeURLRequest(request.get(), |
| 184 completion_callback.callback(), NULL)); | 198 completion_callback.callback(), NULL)); |
| 199 OnResolveProxy(GURL(), 0, *proxy_service, proxy_info.get()); |
| 200 OnProxyFallback(ProxyServer(), 0); |
| 185 EXPECT_EQ(OK, OnBeforeSendHeaders(NULL, completion_callback.callback(), | 201 EXPECT_EQ(OK, OnBeforeSendHeaders(NULL, completion_callback.callback(), |
| 186 request_headers.get())); | 202 request_headers.get())); |
| 187 OnBeforeSendProxyHeaders(NULL, ProxyInfo(), request_headers.get()); | 203 OnBeforeSendProxyHeaders(NULL, ProxyInfo(), request_headers.get()); |
| 188 OnSendHeaders(NULL, *request_headers); | 204 OnSendHeaders(NULL, *request_headers); |
| 189 OnNetworkBytesSent(request.get(), 42); | 205 OnNetworkBytesSent(request.get(), 42); |
| 190 EXPECT_EQ(OK, OnHeadersReceived(NULL, completion_callback.callback(), | 206 EXPECT_EQ(OK, OnHeadersReceived(NULL, completion_callback.callback(), |
| 191 response_headers.get(), NULL, NULL)); | 207 response_headers.get(), NULL, NULL)); |
| 192 OnResponseStarted(request.get()); | 208 OnResponseStarted(request.get()); |
| 193 OnNetworkBytesReceived(request.get(), 42); | 209 OnNetworkBytesReceived(request.get(), 42); |
| 194 OnCompleted(request.get(), false); | 210 OnCompleted(request.get(), false); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 206 } | 222 } |
| 207 | 223 |
| 208 protected: | 224 protected: |
| 209 void OnBeforeURLRequestInternal(URLRequest* request, | 225 void OnBeforeURLRequestInternal(URLRequest* request, |
| 210 const CompletionCallback& callback, | 226 const CompletionCallback& callback, |
| 211 GURL* new_url) override { | 227 GURL* new_url) override { |
| 212 ++(*counters_)["on_before_url_request_count"]; | 228 ++(*counters_)["on_before_url_request_count"]; |
| 213 EXPECT_EQ(1, (*counters_)["on_before_url_request_count"]); | 229 EXPECT_EQ(1, (*counters_)["on_before_url_request_count"]); |
| 214 } | 230 } |
| 215 | 231 |
| 232 void OnResolveProxyInternal(const GURL& url, |
| 233 int load_flags, |
| 234 const ProxyService& proxy_service, |
| 235 ProxyInfo* result) override { |
| 236 ++(*counters_)["on_resolve_proxy_count"]; |
| 237 EXPECT_EQ(1, (*counters_)["on_resolve_proxy_count"]); |
| 238 } |
| 239 |
| 240 void OnProxyFallbackInternal(const ProxyServer& bad_proxy, |
| 241 int net_error) override { |
| 242 ++(*counters_)["on_proxy_fallback_count"]; |
| 243 EXPECT_EQ(1, (*counters_)["on_proxy_fallback_count"]); |
| 244 } |
| 245 |
| 216 void OnBeforeSendHeadersInternal(URLRequest* request, | 246 void OnBeforeSendHeadersInternal(URLRequest* request, |
| 217 const CompletionCallback& callback, | 247 const CompletionCallback& callback, |
| 218 HttpRequestHeaders* headers) override { | 248 HttpRequestHeaders* headers) override { |
| 219 ++(*counters_)["on_before_send_headers_count"]; | 249 ++(*counters_)["on_before_send_headers_count"]; |
| 220 EXPECT_EQ(1, (*counters_)["on_before_send_headers_count"]); | 250 EXPECT_EQ(1, (*counters_)["on_before_send_headers_count"]); |
| 221 } | 251 } |
| 222 | 252 |
| 223 void OnBeforeSendProxyHeadersInternal(URLRequest* request, | 253 void OnBeforeSendProxyHeadersInternal(URLRequest* request, |
| 224 const ProxyInfo& proxy_info, | 254 const ProxyInfo& proxy_info, |
| 225 HttpRequestHeaders* headers) override { | 255 HttpRequestHeaders* headers) override { |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 352 CountersMap layered_network_delegate_counters; | 382 CountersMap layered_network_delegate_counters; |
| 353 TestNetworkDelegateImpl* test_network_delegate_; | 383 TestNetworkDelegateImpl* test_network_delegate_; |
| 354 scoped_ptr<TestLayeredNetworkDelegate> layered_network_delegate_; | 384 scoped_ptr<TestLayeredNetworkDelegate> layered_network_delegate_; |
| 355 }; | 385 }; |
| 356 | 386 |
| 357 TEST_F(LayeredNetworkDelegateTest, VerifyLayeredNetworkDelegateInternal) { | 387 TEST_F(LayeredNetworkDelegateTest, VerifyLayeredNetworkDelegateInternal) { |
| 358 layered_network_delegate_->CallAndVerify(); | 388 layered_network_delegate_->CallAndVerify(); |
| 359 } | 389 } |
| 360 | 390 |
| 361 } // namespace net | 391 } // namespace net |
| OLD | NEW |