• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 The Chromium Authors
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/test_proxy_delegate.h"
6 
7 #include <optional>
8 #include <string>
9 #include <vector>
10 
11 #include "net/base/net_errors.h"
12 #include "net/base/proxy_chain.h"
13 #include "net/base/proxy_server.h"
14 #include "net/base/proxy_string_util.h"
15 #include "net/http/http_request_headers.h"
16 #include "net/http/http_response_headers.h"
17 #include "net/proxy_resolution/proxy_info.h"
18 #include "net/proxy_resolution/proxy_resolution_service.h"
19 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21 
22 namespace net {
23 
24 TestProxyDelegate::TestProxyDelegate() = default;
25 
26 TestProxyDelegate::~TestProxyDelegate() = default;
27 
set_proxy_chain(const ProxyChain & proxy_chain)28 void TestProxyDelegate::set_proxy_chain(const ProxyChain& proxy_chain) {
29   CHECK(proxy_chain.IsValid());
30   proxy_chain_ = proxy_chain;
31 }
32 
proxy_chain() const33 ProxyChain TestProxyDelegate::proxy_chain() const {
34   CHECK(proxy_chain_) << "No proxy chain has been set via 'set_proxy_chain()'";
35   return *proxy_chain_;
36 }
37 
MakeOnTunnelHeadersReceivedFail(Error result)38 void TestProxyDelegate::MakeOnTunnelHeadersReceivedFail(Error result) {
39   on_tunnel_headers_received_result_ = result;
40 }
41 
VerifyOnTunnelHeadersReceived(const ProxyChain & proxy_chain,size_t chain_index,const std::string & response_header_name,const std::string & response_header_value,size_t call_index) const42 void TestProxyDelegate::VerifyOnTunnelHeadersReceived(
43     const ProxyChain& proxy_chain,
44     size_t chain_index,
45     const std::string& response_header_name,
46     const std::string& response_header_value,
47     size_t call_index) const {
48   ASSERT_LT(call_index, on_tunnel_headers_received_proxy_chains_.size());
49   ASSERT_EQ(on_tunnel_headers_received_proxy_chains_.size(),
50             on_tunnel_headers_received_chain_indices_.size());
51   ASSERT_EQ(on_tunnel_headers_received_proxy_chains_.size(),
52             on_tunnel_headers_received_headers_.size());
53 
54   EXPECT_EQ(proxy_chain,
55             on_tunnel_headers_received_proxy_chains_.at(call_index));
56   EXPECT_EQ(chain_index,
57             on_tunnel_headers_received_chain_indices_.at(call_index));
58 
59   scoped_refptr<HttpResponseHeaders> response_headers =
60       on_tunnel_headers_received_headers_.at(call_index);
61   ASSERT_NE(response_headers.get(), nullptr);
62   EXPECT_TRUE(response_headers->HasHeaderValue(response_header_name,
63                                                response_header_value));
64 }
65 
OnResolveProxy(const GURL & url,const NetworkAnonymizationKey & network_anonymization_key,const std::string & method,const ProxyRetryInfoMap & proxy_retry_info,ProxyInfo * result)66 void TestProxyDelegate::OnResolveProxy(
67     const GURL& url,
68     const NetworkAnonymizationKey& network_anonymization_key,
69     const std::string& method,
70     const ProxyRetryInfoMap& proxy_retry_info,
71     ProxyInfo* result) {
72   if (proxy_chain_) {
73     result->UseProxyChain(*proxy_chain_);
74   }
75 }
76 
OnSuccessfulRequestAfterFailures(const ProxyRetryInfoMap & proxy_retry_info)77 void TestProxyDelegate::OnSuccessfulRequestAfterFailures(
78     const ProxyRetryInfoMap& proxy_retry_info) {}
79 
OnFallback(const ProxyChain & bad_chain,int net_error)80 void TestProxyDelegate::OnFallback(const ProxyChain& bad_chain, int net_error) {
81 }
82 
83 // static
GetExtraHeaderValue(const ProxyServer & proxy_server)84 std::string TestProxyDelegate::GetExtraHeaderValue(
85     const ProxyServer& proxy_server) {
86   return ProxyServerToProxyUri(proxy_server);
87 }
88 
OnBeforeTunnelRequest(const ProxyChain & proxy_chain,size_t chain_index,HttpRequestHeaders * extra_headers)89 Error TestProxyDelegate::OnBeforeTunnelRequest(
90     const ProxyChain& proxy_chain,
91     size_t chain_index,
92     HttpRequestHeaders* extra_headers) {
93   on_before_tunnel_request_call_count_++;
94 
95   if (extra_header_name_) {
96     extra_headers->SetHeader(
97         *extra_header_name_,
98         GetExtraHeaderValue(proxy_chain.GetProxyServer(chain_index)));
99   }
100 
101   return OK;
102 }
103 
OnTunnelHeadersReceived(const ProxyChain & proxy_chain,size_t chain_index,const HttpResponseHeaders & response_headers)104 Error TestProxyDelegate::OnTunnelHeadersReceived(
105     const ProxyChain& proxy_chain,
106     size_t chain_index,
107     const HttpResponseHeaders& response_headers) {
108   on_tunnel_headers_received_headers_.push_back(
109       base::MakeRefCounted<HttpResponseHeaders>(
110           response_headers.raw_headers()));
111 
112   on_tunnel_headers_received_proxy_chains_.push_back(proxy_chain);
113   on_tunnel_headers_received_chain_indices_.push_back(chain_index);
114   return on_tunnel_headers_received_result_;
115 }
116 
SetProxyResolutionService(ProxyResolutionService * proxy_resolution_service)117 void TestProxyDelegate::SetProxyResolutionService(
118     ProxyResolutionService* proxy_resolution_service) {}
119 
120 }  // namespace net
121