xref: /aosp_15_r20/external/cronet/net/base/test_proxy_delegate.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 #ifndef NET_BASE_TEST_PROXY_DELEGATE_H_
6 #define NET_BASE_TEST_PROXY_DELEGATE_H_
7 
8 #include <optional>
9 #include <string>
10 #include <string_view>
11 #include <vector>
12 
13 #include "base/memory/scoped_refptr.h"
14 #include "net/base/proxy_chain.h"
15 #include "net/base/proxy_delegate.h"
16 #include "net/base/proxy_server.h"
17 
18 class GURL;
19 
20 namespace net {
21 
22 class ProxyInfo;
23 class ProxyResolutionService;
24 
25 class TestProxyDelegate : public ProxyDelegate {
26  public:
27   TestProxyDelegate();
28   ~TestProxyDelegate() override;
29 
30   // Setter and getter for the proxy chain to use for a given URL when
31   // `OnResolveProxy()` is called. Attempting to get the proxy chain when one
32   // hasn't been set will result in a crash.
33   void set_proxy_chain(const ProxyChain& proxy_chain);
34   ProxyChain proxy_chain() const;
35 
36   // Setter for the name of a header to add to the tunnel request. The value of
37   // the header will be based on the `OnBeforeTunnelRequest()` `proxy_chain` and
38   // `chain_index` parameters. If no extra header name is provided, no extra
39   // header will be added to the tunnel request.
set_extra_header_name(std::string_view extra_header_name)40   void set_extra_header_name(std::string_view extra_header_name) {
41     extra_header_name_ = extra_header_name;
42   }
43 
44   // Returns the header value that may be added to a tunnel request for a given
45   // proxy server. For more info, see `set_extra_header_name()`.
46   static std::string GetExtraHeaderValue(const ProxyServer& proxy_server);
47 
48   // Returns the number of times `OnBeforeTunnelRequest()` was called.
on_before_tunnel_request_call_count()49   size_t on_before_tunnel_request_call_count() const {
50     return on_before_tunnel_request_call_count_;
51   }
52 
53   // Returns the number of times `OnTunnelHeadersReceived()` was called.
on_tunnel_headers_received_call_count()54   size_t on_tunnel_headers_received_call_count() {
55     return on_tunnel_headers_received_headers_.size();
56   }
57 
58   // Make subsequent calls to `OnTunnelHeadersReceived()` fail with the given
59   // value.
60   void MakeOnTunnelHeadersReceivedFail(Error result);
61 
62   // Checks whether the provided proxy chain, chain index, response header name,
63   // and response header value were passed to a given
64   // `OnTunnelHeadersReceived()` call.
65   void VerifyOnTunnelHeadersReceived(const ProxyChain& proxy_chain,
66                                      size_t chain_index,
67                                      const std::string& response_header_name,
68                                      const std::string& response_header_value,
69                                      size_t call_index = 0) const;
70 
71   // ProxyDelegate implementation:
72   void OnResolveProxy(const GURL& url,
73                       const NetworkAnonymizationKey& network_anonymization_key,
74                       const std::string& method,
75                       const ProxyRetryInfoMap& proxy_retry_info,
76                       ProxyInfo* result) override;
77   void OnSuccessfulRequestAfterFailures(
78       const ProxyRetryInfoMap& proxy_retry_info) override;
79   void OnFallback(const ProxyChain& bad_chain, int net_error) override;
80   void OnBeforeTunnelRequest(const ProxyChain& proxy_chain,
81                              size_t chain_index,
82                              HttpRequestHeaders* extra_headers) override;
83   Error OnTunnelHeadersReceived(
84       const ProxyChain& proxy_chain,
85       size_t chain_index,
86       const HttpResponseHeaders& response_headers) override;
87   void SetProxyResolutionService(
88       ProxyResolutionService* proxy_resolution_service) override;
89 
90  private:
91   std::optional<ProxyChain> proxy_chain_;
92   std::optional<std::string> extra_header_name_;
93 
94   size_t on_before_tunnel_request_call_count_ = 0;
95 
96   Error on_tunnel_headers_received_result_ = OK;
97   std::vector<ProxyChain> on_tunnel_headers_received_proxy_chains_;
98   std::vector<size_t> on_tunnel_headers_received_chain_indices_;
99   std::vector<scoped_refptr<HttpResponseHeaders>>
100       on_tunnel_headers_received_headers_;
101 };
102 
103 }  // namespace net
104 
105 #endif  // NET_BASE_TEST_PROXY_DELEGATE_H_
106