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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
// 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.
#ifndef NET_HTTP_HTTP_STREAM_FACTORY_TEST_UTIL_H_
#define NET_HTTP_HTTP_STREAM_FACTORY_TEST_UTIL_H_
#include <memory>
#include <vector>
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "net/http/http_stream.h"
#include "net/http/http_stream_factory.h"
#include "net/http/http_stream_factory_job.h"
#include "net/http/http_stream_factory_job_controller.h"
#include "net/http/http_stream_request.h"
#include "net/proxy_resolution/proxy_info.h"
#include "net/socket/next_proto.h"
#include "net/ssl/ssl_config.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "url/scheme_host_port.h"
using testing::_;
using testing::Invoke;
namespace net {
class HttpStreamFactoryPeer {
public:
static void AddJobController(
HttpStreamFactory* factory,
std::unique_ptr<HttpStreamFactory::JobController> job_controller) {
factory->job_controller_set_.insert(std::move(job_controller));
}
static bool IsJobControllerDeleted(HttpStreamFactory* factory) {
return factory->job_controller_set_.empty();
}
static HttpStreamFactory::JobFactory* GetDefaultJobFactory(
HttpStreamFactory* factory) {
return factory->job_factory_.get();
}
};
// This delegate does nothing when called.
class MockHttpStreamRequestDelegate : public HttpStreamRequest::Delegate {
public:
MockHttpStreamRequestDelegate();
MockHttpStreamRequestDelegate(const MockHttpStreamRequestDelegate&) = delete;
MockHttpStreamRequestDelegate& operator=(
const MockHttpStreamRequestDelegate&) = delete;
~MockHttpStreamRequestDelegate() override;
// std::unique_ptr is not copyable and therefore cannot be mocked.
MOCK_METHOD2(OnStreamReadyImpl,
void(const ProxyInfo& used_proxy_info, HttpStream* stream));
void OnStreamReady(const ProxyInfo& used_proxy_info,
std::unique_ptr<HttpStream> stream) override {
OnStreamReadyImpl(used_proxy_info, stream.get());
}
// std::unique_ptr is not copyable and therefore cannot be mocked.
void OnBidirectionalStreamImplReady(
const ProxyInfo& used_proxy_info,
std::unique_ptr<BidirectionalStreamImpl> stream) override {}
// std::unique_ptr is not copyable and therefore cannot be mocked.
void OnWebSocketHandshakeStreamReady(
const ProxyInfo& used_proxy_info,
std::unique_ptr<WebSocketHandshakeStreamBase> stream) override {}
MOCK_METHOD4(OnStreamFailed,
void(int status,
const NetErrorDetails& net_error_details,
const ProxyInfo& used_proxy_info,
ResolveErrorInfo resolve_error_info));
MOCK_METHOD2(OnCertificateError, void(int status, const SSLInfo& ssl_info));
MOCK_METHOD3(OnNeedsProxyAuth,
void(const HttpResponseInfo& proxy_response,
const ProxyInfo& used_proxy_info,
HttpAuthController* auth_controller));
MOCK_METHOD1(OnNeedsClientAuth, void(SSLCertRequestInfo* cert_info));
MOCK_METHOD0(OnQuicBroken, void());
};
class MockHttpStreamFactoryJob : public HttpStreamFactory::Job {
public:
MockHttpStreamFactoryJob(
HttpStreamFactory::Job::Delegate* delegate,
HttpStreamFactory::JobType job_type,
HttpNetworkSession* session,
const HttpStreamFactory::StreamRequestInfo& request_info,
RequestPriority priority,
ProxyInfo proxy_info,
const std::vector<SSLConfig::CertAndStatus>& allowed_bad_certs,
url::SchemeHostPort destination,
GURL origin_url,
NextProto alternative_protocol,
quic::ParsedQuicVersion quic_version,
bool is_websocket,
bool enable_ip_based_pooling,
std::optional<ConnectionManagementConfig> management_config,
NetLog* net_log);
~MockHttpStreamFactoryJob() override;
MOCK_METHOD0(Resume, void());
MOCK_METHOD0(Orphan, void());
void DoResume();
};
// JobFactory for creating MockHttpStreamFactoryJobs.
class TestJobFactory : public HttpStreamFactory::JobFactory {
public:
TestJobFactory();
~TestJobFactory() override;
std::unique_ptr<HttpStreamFactory::Job> CreateJob(
HttpStreamFactory::Job::Delegate* delegate,
HttpStreamFactory::JobType job_type,
HttpNetworkSession* session,
const HttpStreamFactory::StreamRequestInfo& request_info,
RequestPriority priority,
const ProxyInfo& proxy_info,
const std::vector<SSLConfig::CertAndStatus>& allowed_bad_certs,
url::SchemeHostPort destination,
GURL origin_url,
bool is_websocket,
bool enable_ip_based_pooling,
NetLog* net_log,
NextProto alternative_protocol,
quic::ParsedQuicVersion quic_version,
std::optional<ConnectionManagementConfig> management_config) override;
MockHttpStreamFactoryJob* main_job() const { return main_job_; }
MockHttpStreamFactoryJob* alternative_job() const { return alternative_job_; }
MockHttpStreamFactoryJob* dns_alpn_h3_job() const { return dns_alpn_h3_job_; }
private:
raw_ptr<MockHttpStreamFactoryJob, AcrossTasksDanglingUntriaged> main_job_ =
nullptr;
raw_ptr<MockHttpStreamFactoryJob, AcrossTasksDanglingUntriaged>
alternative_job_ = nullptr;
raw_ptr<MockHttpStreamFactoryJob, AcrossTasksDanglingUntriaged>
dns_alpn_h3_job_ = nullptr;
};
} // namespace net
#endif // NET_HTTP_HTTP_STREAM_FACTORY_TEST_UTIL_H_
|