forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utility.cc
159 lines (135 loc) · 5.79 KB
/
utility.cc
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
#include "utility.h"
#include <chrono>
#include <cstdint>
#include <memory>
#include <string>
#include "envoy/event/dispatcher.h"
#include "envoy/network/connection.h"
#include "common/api/api_impl.h"
#include "common/buffer/buffer_impl.h"
#include "common/common/assert.h"
#include "common/common/fmt.h"
#include "common/http/header_map_impl.h"
#include "common/http/headers.h"
#include "common/network/utility.h"
#include "common/upstream/upstream_impl.h"
#include "test/common/upstream/utility.h"
#include "test/mocks/stats/mocks.h"
#include "test/mocks/upstream/mocks.h"
#include "test/test_common/network_utility.h"
#include "test/test_common/printers.h"
#include "test/test_common/utility.h"
#include "absl/strings/match.h"
namespace Envoy {
void BufferingStreamDecoder::decodeHeaders(Http::ResponseHeaderMapPtr&& headers, bool end_stream) {
ASSERT(!complete_);
complete_ = end_stream;
headers_ = std::move(headers);
if (complete_) {
onComplete();
}
}
void BufferingStreamDecoder::decodeData(Buffer::Instance& data, bool end_stream) {
ASSERT(!complete_);
complete_ = end_stream;
body_.append(data.toString());
if (complete_) {
onComplete();
}
}
void BufferingStreamDecoder::decodeTrailers(Http::ResponseTrailerMapPtr&&) {
NOT_IMPLEMENTED_GCOVR_EXCL_LINE;
}
void BufferingStreamDecoder::onComplete() {
ASSERT(complete_);
on_complete_cb_();
}
void BufferingStreamDecoder::onResetStream(Http::StreamResetReason, absl::string_view) {
ADD_FAILURE();
}
BufferingStreamDecoderPtr
IntegrationUtil::makeSingleRequest(const Network::Address::InstanceConstSharedPtr& addr,
const std::string& method, const std::string& url,
const std::string& body, Http::CodecClient::Type type,
const std::string& host, const std::string& content_type) {
NiceMock<Stats::MockIsolatedStatsStore> mock_stats_store;
Event::GlobalTimeSystem time_system;
Api::Impl api(Thread::threadFactoryForTest(), mock_stats_store, time_system,
Filesystem::fileSystemForTest());
Event::DispatcherPtr dispatcher(api.allocateDispatcher());
std::shared_ptr<Upstream::MockClusterInfo> cluster{new NiceMock<Upstream::MockClusterInfo>()};
Upstream::HostDescriptionConstSharedPtr host_description{
Upstream::makeTestHostDescription(cluster, "tcp://127.0.0.1:80")};
Http::CodecClientProd client(
type,
dispatcher->createClientConnection(addr, Network::Address::InstanceConstSharedPtr(),
Network::Test::createRawBufferSocket(), nullptr),
host_description, *dispatcher);
BufferingStreamDecoderPtr response(new BufferingStreamDecoder([&]() -> void {
client.close();
dispatcher->exit();
}));
Http::RequestEncoder& encoder = client.newStream(*response);
encoder.getStream().addCallbacks(*response);
Http::RequestHeaderMapImpl headers;
headers.setMethod(method);
headers.setPath(url);
headers.setHost(host);
headers.setReferenceScheme(Http::Headers::get().SchemeValues.Http);
if (!content_type.empty()) {
headers.setContentType(content_type);
}
encoder.encodeHeaders(headers, body.empty());
if (!body.empty()) {
Buffer::OwnedImpl body_buffer(body);
encoder.encodeData(body_buffer, true);
}
dispatcher->run(Event::Dispatcher::RunType::Block);
return response;
}
BufferingStreamDecoderPtr
IntegrationUtil::makeSingleRequest(uint32_t port, const std::string& method, const std::string& url,
const std::string& body, Http::CodecClient::Type type,
Network::Address::IpVersion ip_version, const std::string& host,
const std::string& content_type) {
auto addr = Network::Utility::resolveUrl(
fmt::format("tcp://{}:{}", Network::Test::getLoopbackAddressUrlString(ip_version), port));
return makeSingleRequest(addr, method, url, body, type, host, content_type);
}
RawConnectionDriver::RawConnectionDriver(uint32_t port, Buffer::Instance& initial_data,
ReadCallback data_callback,
Network::Address::IpVersion version) {
api_ = Api::createApiForTest(stats_store_);
Event::GlobalTimeSystem time_system;
dispatcher_ = api_->allocateDispatcher();
callbacks_ = std::make_unique<ConnectionCallbacks>();
client_ = dispatcher_->createClientConnection(
Network::Utility::resolveUrl(
fmt::format("tcp://{}:{}", Network::Test::getLoopbackAddressUrlString(version), port)),
Network::Address::InstanceConstSharedPtr(), Network::Test::createRawBufferSocket(), nullptr);
client_->addConnectionCallbacks(*callbacks_);
client_->addReadFilter(Network::ReadFilterSharedPtr{new ForwardingFilter(*this, data_callback)});
client_->write(initial_data, false);
client_->connect();
}
RawConnectionDriver::~RawConnectionDriver() = default;
void RawConnectionDriver::run(Event::Dispatcher::RunType run_type) { dispatcher_->run(run_type); }
void RawConnectionDriver::close() { client_->close(Network::ConnectionCloseType::FlushWrite); }
WaitForPayloadReader::WaitForPayloadReader(Event::Dispatcher& dispatcher)
: dispatcher_(dispatcher) {}
Network::FilterStatus WaitForPayloadReader::onData(Buffer::Instance& data, bool end_stream) {
data_.append(data.toString());
data.drain(data.length());
read_end_stream_ = end_stream;
if ((!data_to_wait_for_.empty() && absl::StartsWith(data_, data_to_wait_for_)) ||
(exact_match_ == false && data_.find(data_to_wait_for_) != std::string::npos) || end_stream) {
data_to_wait_for_.clear();
dispatcher_.exit();
}
if (wait_for_length_ && data_.size() >= length_to_wait_for_) {
wait_for_length_ = false;
dispatcher_.exit();
}
return Network::FilterStatus::StopIteration;
}
} // namespace Envoy