File: test_proxy_delegate.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (138 lines) | stat: -rw-r--r-- 4,537 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/base/test_proxy_delegate.h"

#include <optional>
#include <string>
#include <vector>

#include "net/base/net_errors.h"
#include "net/base/proxy_chain.h"
#include "net/base/proxy_server.h"
#include "net/base/proxy_string_util.h"
#include "net/http/http_request_headers.h"
#include "net/http/http_response_headers.h"
#include "net/proxy_resolution/proxy_info.h"
#include "net/proxy_resolution/proxy_resolution_service.h"
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace net {

TestProxyDelegate::TestProxyDelegate() = default;

TestProxyDelegate::~TestProxyDelegate() = default;

void TestProxyDelegate::set_proxy_chain(const ProxyChain& proxy_chain) {
  CHECK(proxy_chain.IsValid());
  proxy_chain_ = proxy_chain;
}

ProxyChain TestProxyDelegate::proxy_chain() const {
  CHECK(proxy_chain_) << "No proxy chain has been set via 'set_proxy_chain()'";
  return *proxy_chain_;
}
void TestProxyDelegate::set_proxy_list(const ProxyList& proxy_list) {
  proxy_list_ = proxy_list;
}

ProxyList TestProxyDelegate::proxy_list() const {
  CHECK(proxy_list_) << "No proxy list has been set via 'set_proxy_list()'";
  return *proxy_list_;
}

void TestProxyDelegate::MakeOnTunnelHeadersReceivedFail(Error result) {
  on_tunnel_headers_received_result_ = result;
}

void TestProxyDelegate::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) const {
  ASSERT_LT(call_index, on_tunnel_headers_received_proxy_chains_.size());
  ASSERT_EQ(on_tunnel_headers_received_proxy_chains_.size(),
            on_tunnel_headers_received_chain_indices_.size());
  ASSERT_EQ(on_tunnel_headers_received_proxy_chains_.size(),
            on_tunnel_headers_received_headers_.size());

  EXPECT_EQ(proxy_chain,
            on_tunnel_headers_received_proxy_chains_.at(call_index));
  EXPECT_EQ(chain_index,
            on_tunnel_headers_received_chain_indices_.at(call_index));

  scoped_refptr<HttpResponseHeaders> response_headers =
      on_tunnel_headers_received_headers_.at(call_index);
  ASSERT_NE(response_headers.get(), nullptr);
  EXPECT_TRUE(response_headers->HasHeaderValue(response_header_name,
                                               response_header_value));
}

void TestProxyDelegate::OnResolveProxy(
    const GURL& url,
    const NetworkAnonymizationKey& network_anonymization_key,
    const std::string& method,
    const ProxyRetryInfoMap& proxy_retry_info,
    ProxyInfo* result) {
  if (proxy_list_) {
    CHECK(!proxy_chain_) << "Cannot set both a proxy list and a proxy chain.";
    result->OverrideProxyList(*proxy_list_);
  } else if (proxy_chain_) {
    result->UseProxyChain(*proxy_chain_);
  }
}

void TestProxyDelegate::OnSuccessfulRequestAfterFailures(
    const ProxyRetryInfoMap& proxy_retry_info) {}

void TestProxyDelegate::OnFallback(const ProxyChain& bad_chain, int net_error) {
}

// static
std::string TestProxyDelegate::GetExtraHeaderValue(
    const ProxyServer& proxy_server) {
  return ProxyServerToProxyUri(proxy_server);
}

Error TestProxyDelegate::OnBeforeTunnelRequest(
    const ProxyChain& proxy_chain,
    size_t chain_index,
    HttpRequestHeaders* extra_headers) {
  on_before_tunnel_request_call_count_++;

  if (extra_header_name_) {
    extra_headers->SetHeader(
        *extra_header_name_,
        GetExtraHeaderValue(proxy_chain.GetProxyServer(chain_index)));
  }

  return OK;
}

Error TestProxyDelegate::OnTunnelHeadersReceived(
    const ProxyChain& proxy_chain,
    size_t chain_index,
    const HttpResponseHeaders& response_headers) {
  on_tunnel_headers_received_headers_.push_back(
      base::MakeRefCounted<HttpResponseHeaders>(
          response_headers.raw_headers()));

  on_tunnel_headers_received_proxy_chains_.push_back(proxy_chain);
  on_tunnel_headers_received_chain_indices_.push_back(chain_index);
  return on_tunnel_headers_received_result_;
}

void TestProxyDelegate::SetProxyResolutionService(
    ProxyResolutionService* proxy_resolution_service) {}

bool TestProxyDelegate::AliasRequiresProxyOverride(
    const std::string scheme,
    const std::vector<std::string>& dns_aliases,
    const net::NetworkAnonymizationKey& network_anonymization_key) {
  return false;
}

}  // namespace net