forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
websocket_integration_test.cc
449 lines (373 loc) · 19 KB
/
websocket_integration_test.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
#include "test/integration/websocket_integration_test.h"
#include <string>
#include "envoy/config/bootstrap/v3/bootstrap.pb.h"
#include "envoy/extensions/filters/network/http_connection_manager/v3/http_connection_manager.pb.h"
#include "common/http/header_map_impl.h"
#include "common/protobuf/utility.h"
#include "test/integration/utility.h"
#include "test/test_common/network_utility.h"
#include "test/test_common/printers.h"
#include "test/test_common/utility.h"
#include "absl/strings/str_cat.h"
#include "gtest/gtest.h"
namespace Envoy {
namespace {
Http::TestRequestHeaderMapImpl upgradeRequestHeaders(const char* upgrade_type = "websocket",
uint32_t content_length = 0) {
return Http::TestRequestHeaderMapImpl{{":authority", "host"},
{"content-length", fmt::format("{}", content_length)},
{":path", "/websocket/test"},
{":method", "GET"},
{":scheme", "http"},
{"upgrade", upgrade_type},
{"connection", "keep-alive, upgrade"}};
}
Http::TestResponseHeaderMapImpl upgradeResponseHeaders(const char* upgrade_type = "websocket") {
return Http::TestResponseHeaderMapImpl{
{":status", "101"}, {"connection", "upgrade"}, {"upgrade", upgrade_type}};
}
template <class ProxiedHeaders, class OriginalHeaders>
void commonValidate(ProxiedHeaders& proxied_headers, const OriginalHeaders& original_headers) {
// If no content length is specified, the HTTP1 codec will add a chunked encoding header.
if (original_headers.ContentLength() == nullptr &&
proxied_headers.TransferEncoding() != nullptr) {
ASSERT_EQ(proxied_headers.TransferEncoding()->value().getStringView(), "chunked");
proxied_headers.removeTransferEncoding();
}
if (proxied_headers.Connection() != nullptr &&
proxied_headers.Connection()->value() == "upgrade" &&
original_headers.Connection() != nullptr &&
original_headers.Connection()->value() == "keep-alive, upgrade") {
// The keep-alive is implicit for HTTP/1.1, so Envoy only sets the upgrade
// header when converting from HTTP/1.1 to H2
proxied_headers.setConnection("keep-alive, upgrade");
}
}
} // namespace
void WebsocketIntegrationTest::validateUpgradeRequestHeaders(
const Http::RequestHeaderMap& original_proxied_request_headers,
const Http::RequestHeaderMap& original_request_headers) {
Http::TestRequestHeaderMapImpl proxied_request_headers(original_proxied_request_headers);
if (proxied_request_headers.ForwardedProto()) {
ASSERT_EQ(proxied_request_headers.ForwardedProto()->value().getStringView(), "http");
proxied_request_headers.removeForwardedProto();
}
// Check for and remove headers added by default for HTTP requests.
ASSERT_TRUE(proxied_request_headers.RequestId() != nullptr);
ASSERT_TRUE(proxied_request_headers.EnvoyExpectedRequestTimeoutMs() != nullptr);
proxied_request_headers.removeEnvoyExpectedRequestTimeoutMs();
if (proxied_request_headers.Scheme()) {
ASSERT_EQ(proxied_request_headers.Scheme()->value().getStringView(), "http");
} else {
proxied_request_headers.setScheme("http");
}
// 0 byte content lengths may be stripped on the H2 path - ignore that as a difference by adding
// it back to the proxied headers.
if (original_request_headers.ContentLength() &&
proxied_request_headers.ContentLength() == nullptr) {
proxied_request_headers.setContentLength(size_t(0));
}
commonValidate(proxied_request_headers, original_request_headers);
proxied_request_headers.removeRequestId();
EXPECT_THAT(&proxied_request_headers, HeaderMapEqualIgnoreOrder(&original_request_headers));
}
void WebsocketIntegrationTest::validateUpgradeResponseHeaders(
const Http::ResponseHeaderMap& original_proxied_response_headers,
const Http::ResponseHeaderMap& original_response_headers) {
Http::TestResponseHeaderMapImpl proxied_response_headers(original_proxied_response_headers);
// Check for and remove headers added by default for HTTP responses.
ASSERT_TRUE(proxied_response_headers.Date() != nullptr);
ASSERT_TRUE(proxied_response_headers.Server() != nullptr);
ASSERT_EQ(proxied_response_headers.Server()->value().getStringView(), "envoy");
proxied_response_headers.removeDate();
proxied_response_headers.removeServer();
commonValidate(proxied_response_headers, original_response_headers);
EXPECT_THAT(&proxied_response_headers, HeaderMapEqualIgnoreOrder(&original_response_headers));
}
INSTANTIATE_TEST_SUITE_P(Protocols, WebsocketIntegrationTest,
testing::ValuesIn(HttpProtocolIntegrationTest::getProtocolTestParams()),
HttpProtocolIntegrationTest::protocolTestParamsToString);
ConfigHelper::HttpModifierFunction setRouteUsingWebsocket() {
return [](envoy::extensions::filters::network::http_connection_manager::v3::HttpConnectionManager&
hcm) { hcm.add_upgrade_configs()->set_upgrade_type("websocket"); };
}
void WebsocketIntegrationTest::initialize() {
if (upstreamProtocol() != FakeHttpConnection::Type::HTTP1) {
config_helper_.addConfigModifier(
[&](envoy::config::bootstrap::v3::Bootstrap& bootstrap) -> void {
auto* cluster = bootstrap.mutable_static_resources()->mutable_clusters(0);
cluster->mutable_http2_protocol_options()->set_allow_connect(true);
});
}
if (downstreamProtocol() != Http::CodecClient::Type::HTTP1) {
config_helper_.addConfigModifier(
[&](envoy::extensions::filters::network::http_connection_manager::v3::HttpConnectionManager&
hcm) -> void { hcm.mutable_http2_protocol_options()->set_allow_connect(true); });
}
HttpProtocolIntegrationTest::initialize();
}
void WebsocketIntegrationTest::performUpgrade(
const Http::TestRequestHeaderMapImpl& upgrade_request_headers,
const Http::TestResponseHeaderMapImpl& upgrade_response_headers) {
// Establish the initial connection.
codec_client_ = makeHttpConnection(lookupPort("http"));
// Send websocket upgrade request
auto encoder_decoder = codec_client_->startRequest(upgrade_request_headers);
request_encoder_ = &encoder_decoder.first;
response_ = std::move(encoder_decoder.second);
test_server_->waitForCounterGe("http.config_test.downstream_cx_upgrades_total", 1);
test_server_->waitForGaugeGe("http.config_test.downstream_cx_upgrades_active", 1);
// Verify the upgrade was received upstream.
ASSERT_TRUE(fake_upstreams_[0]->waitForHttpConnection(*dispatcher_, fake_upstream_connection_));
ASSERT_TRUE(fake_upstream_connection_->waitForNewStream(*dispatcher_, upstream_request_));
ASSERT_TRUE(upstream_request_->waitForHeadersComplete());
validateUpgradeRequestHeaders(upstream_request_->headers(), upgrade_request_headers);
// Send the upgrade response
upstream_request_->encodeHeaders(upgrade_response_headers, false);
// Verify the upgrade response was received downstream.
response_->waitForHeaders();
validateUpgradeResponseHeaders(response_->headers(), upgrade_response_headers);
}
void WebsocketIntegrationTest::sendBidirectionalData() {
// Verify that the client can still send data upstream, and that upstream
// receives it.
codec_client_->sendData(*request_encoder_, "hello", false);
ASSERT_TRUE(upstream_request_->waitForData(*dispatcher_, "hello"));
// Verify the upstream can send data to the client and that the client
// receives it.
upstream_request_->encodeData("world", false);
response_->waitForBodyData(5);
EXPECT_EQ("world", response_->body());
}
TEST_P(WebsocketIntegrationTest, WebSocketConnectionDownstreamDisconnect) {
config_helper_.addConfigModifier(setRouteUsingWebsocket());
initialize();
performUpgrade(upgradeRequestHeaders(), upgradeResponseHeaders());
sendBidirectionalData();
// Send some final data from the client, and disconnect.
codec_client_->sendData(*request_encoder_, "bye!", false);
codec_client_->close();
// Verify the final data was received and that the connection is torn down.
ASSERT_TRUE(upstream_request_->waitForData(*dispatcher_, "hellobye!"));
ASSERT_TRUE(waitForUpstreamDisconnectOrReset());
test_server_->waitForGaugeEq("http.config_test.downstream_cx_upgrades_active", 0);
}
TEST_P(WebsocketIntegrationTest, WebSocketConnectionUpstreamDisconnect) {
config_helper_.addConfigModifier(setRouteUsingWebsocket());
initialize();
performUpgrade(upgradeRequestHeaders(), upgradeResponseHeaders());
// Standard TCP proxy semantics post upgrade
codec_client_->sendData(*request_encoder_, "hello", false);
ASSERT_TRUE(upstream_request_->waitForData(*dispatcher_, "hello"));
// Send data downstream and disconnect immediately.
upstream_request_->encodeData("world", false);
ASSERT_TRUE(fake_upstream_connection_->close());
ASSERT_TRUE(fake_upstream_connection_->waitForDisconnect());
// Verify both the data and the disconnect went through.
response_->waitForBodyData(5);
EXPECT_EQ("world", response_->body());
waitForClientDisconnectOrReset();
}
TEST_P(WebsocketIntegrationTest, EarlyData) {
if (downstreamProtocol() == Http::CodecClient::Type::HTTP2 ||
upstreamProtocol() == FakeHttpConnection::Type::HTTP2) {
return;
}
config_helper_.addConfigModifier(setRouteUsingWebsocket());
initialize();
// Establish the initial connection.
codec_client_ = makeHttpConnection(lookupPort("http"));
const std::string early_data_req_str = "hello";
const std::string early_data_resp_str = "world";
// Send websocket upgrade request with early data.
auto encoder_decoder =
codec_client_->startRequest(upgradeRequestHeaders("websocket", early_data_req_str.size()));
request_encoder_ = &encoder_decoder.first;
response_ = std::move(encoder_decoder.second);
codec_client_->sendData(*request_encoder_, early_data_req_str, false);
// Wait for both the upgrade, and the early data.
ASSERT_TRUE(fake_upstreams_[0]->waitForHttpConnection(*dispatcher_, fake_upstream_connection_));
ASSERT_TRUE(fake_upstream_connection_->waitForNewStream(*dispatcher_, upstream_request_));
ASSERT_TRUE(upstream_request_->waitForHeadersComplete());
ASSERT_TRUE(upstream_request_->waitForData(*dispatcher_, "hello"));
// Accept websocket upgrade request
upstream_request_->encodeHeaders(upgradeResponseHeaders(), false);
// Reply also with early data
upstream_request_->encodeData(early_data_resp_str, false);
// upstream disconnect
ASSERT_TRUE(fake_upstream_connection_->close());
ASSERT_TRUE(fake_upstream_connection_->waitForDisconnect());
response_->waitForHeaders();
auto upgrade_response_headers(upgradeResponseHeaders());
validateUpgradeResponseHeaders(response_->headers(), upgrade_response_headers);
if (downstreamProtocol() == Http::CodecClient::Type::HTTP1) {
// For H2, the disconnect may result in the terminal data not being proxied.
response_->waitForBodyData(5);
}
waitForClientDisconnectOrReset();
EXPECT_EQ("world", response_->body());
}
TEST_P(WebsocketIntegrationTest, WebSocketConnectionIdleTimeout) {
config_helper_.addConfigModifier(setRouteUsingWebsocket());
config_helper_.addConfigModifier(
[&](envoy::extensions::filters::network::http_connection_manager::v3::HttpConnectionManager&
hcm) -> void {
auto* route_config = hcm.mutable_route_config();
auto* virtual_host = route_config->mutable_virtual_hosts(0);
auto* route = virtual_host->mutable_routes(0)->mutable_route();
route->mutable_idle_timeout()->set_seconds(0);
route->mutable_idle_timeout()->set_nanos(200 * 1000 * 1000);
});
initialize();
// WebSocket upgrade, send some data and disconnect downstream
performUpgrade(upgradeRequestHeaders(), upgradeResponseHeaders());
sendBidirectionalData();
test_server_->waitForCounterGe("http.config_test.downstream_rq_idle_timeout", 1);
waitForClientDisconnectOrReset();
ASSERT_TRUE(waitForUpstreamDisconnectOrReset());
}
// Technically not a websocket tests, but verifies normal upgrades have parity
// with websocket upgrades
TEST_P(WebsocketIntegrationTest, NonWebsocketUpgrade) {
config_helper_.addConfigModifier(
[&](envoy::extensions::filters::network::http_connection_manager::v3::HttpConnectionManager&
hcm) -> void {
auto* foo_upgrade = hcm.add_upgrade_configs();
foo_upgrade->set_upgrade_type("foo");
});
config_helper_.addConfigModifier(setRouteUsingWebsocket());
initialize();
performUpgrade(upgradeRequestHeaders("foo", 0), upgradeResponseHeaders("foo"));
sendBidirectionalData();
codec_client_->sendData(*request_encoder_, "bye!", false);
if (downstreamProtocol() == Http::CodecClient::Type::HTTP1) {
codec_client_->close();
} else {
codec_client_->sendReset(*request_encoder_);
}
ASSERT_TRUE(upstream_request_->waitForData(*dispatcher_, "hellobye!"));
ASSERT_TRUE(waitForUpstreamDisconnectOrReset());
auto upgrade_response_headers(upgradeResponseHeaders("foo"));
validateUpgradeResponseHeaders(response_->headers(), upgrade_response_headers);
codec_client_->close();
}
TEST_P(WebsocketIntegrationTest, RouteSpecificUpgrade) {
config_helper_.addConfigModifier(
[&](envoy::extensions::filters::network::http_connection_manager::v3::HttpConnectionManager&
hcm) -> void {
auto* foo_upgrade = hcm.add_upgrade_configs();
foo_upgrade->set_upgrade_type("foo");
foo_upgrade->mutable_enabled()->set_value(false);
});
auto host = config_helper_.createVirtualHost("host", "/websocket/test");
host.mutable_routes(0)->mutable_route()->add_upgrade_configs()->set_upgrade_type("foo");
config_helper_.addVirtualHost(host);
initialize();
performUpgrade(upgradeRequestHeaders("foo", 0), upgradeResponseHeaders("foo"));
sendBidirectionalData();
codec_client_->sendData(*request_encoder_, "bye!", false);
if (downstreamProtocol() == Http::CodecClient::Type::HTTP1) {
codec_client_->close();
} else {
codec_client_->sendReset(*request_encoder_);
}
ASSERT_TRUE(upstream_request_->waitForData(*dispatcher_, "hellobye!"));
ASSERT_TRUE(waitForUpstreamDisconnectOrReset());
auto upgrade_response_headers(upgradeResponseHeaders("foo"));
validateUpgradeResponseHeaders(response_->headers(), upgrade_response_headers);
codec_client_->close();
}
TEST_P(WebsocketIntegrationTest, WebsocketCustomFilterChain) {
config_helper_.addConfigModifier(setRouteUsingWebsocket());
// Add a small buffer filter to the standard HTTP filter chain. Websocket
// upgrades will use the HTTP filter chain so will also have small buffers.
config_helper_.addFilter(ConfigHelper::smallBufferFilter());
// Add a second upgrade type which goes directly to the router filter.
config_helper_.addConfigModifier(
[&](envoy::extensions::filters::network::http_connection_manager::v3::HttpConnectionManager&
hcm) -> void {
auto* foo_upgrade = hcm.add_upgrade_configs();
foo_upgrade->set_upgrade_type("foo");
auto* filter_list_back = foo_upgrade->add_filters();
TestUtility::loadFromYaml("name: envoy.filters.http.router", *filter_list_back);
});
initialize();
// Websocket upgrades are configured to disallow large payload.
const std::string large_req_str(2048, 'a');
{
codec_client_ = makeHttpConnection(lookupPort("http"));
auto encoder_decoder = codec_client_->startRequest(upgradeRequestHeaders("websocket"));
response_ = std::move(encoder_decoder.second);
codec_client_->sendData(encoder_decoder.first, large_req_str, false);
response_->waitForEndStream();
EXPECT_EQ("413", response_->headers().Status()->value().getStringView());
waitForClientDisconnectOrReset();
codec_client_->close();
}
// HTTP requests are configured to disallow large bodies.
{
Http::TestRequestHeaderMapImpl request_headers{{":method", "GET"},
{":path", "/"},
{"content-length", "2048"},
{":authority", "host"},
{":scheme", "https"}};
codec_client_ = makeHttpConnection(lookupPort("http"));
auto encoder_decoder = codec_client_->startRequest(request_headers);
response_ = std::move(encoder_decoder.second);
codec_client_->sendData(encoder_decoder.first, large_req_str, false);
response_->waitForEndStream();
EXPECT_EQ("413", response_->headers().Status()->value().getStringView());
waitForClientDisconnectOrReset();
codec_client_->close();
}
// Foo upgrades are configured without the buffer filter, so should explicitly
// allow large payload.
if (downstreamProtocol() != Http::CodecClient::Type::HTTP2) {
performUpgrade(upgradeRequestHeaders("foo"), upgradeResponseHeaders("foo"));
codec_client_->sendData(*request_encoder_, large_req_str, false);
ASSERT_TRUE(upstream_request_->waitForData(*dispatcher_, large_req_str));
// Tear down all the connections cleanly.
codec_client_->close();
ASSERT_TRUE(waitForUpstreamDisconnectOrReset());
}
}
TEST_P(WebsocketIntegrationTest, BidirectionalChunkedData) {
if (downstreamProtocol() == Http::CodecClient::Type::HTTP2 ||
upstreamProtocol() == FakeHttpConnection::Type::HTTP2) {
return;
}
config_helper_.addConfigModifier(setRouteUsingWebsocket());
initialize();
auto request_headers = upgradeRequestHeaders();
request_headers.removeContentLength();
auto response_headers = upgradeResponseHeaders();
response_headers.removeContentLength();
performUpgrade(request_headers, response_headers);
// With content-length not present, the HTTP codec will send the request with
// transfer-encoding: chunked.
if (upstreamProtocol() == FakeHttpConnection::Type::HTTP1) {
ASSERT_TRUE(upstream_request_->headers().TransferEncoding() != nullptr);
}
if (downstreamProtocol() == Http::CodecClient::Type::HTTP1) {
ASSERT_TRUE(response_->headers().TransferEncoding() != nullptr);
}
// Send both a chunked request body and "websocket" payload.
std::string request_payload = "3\r\n123\r\n0\r\n\r\nSomeWebsocketRequestPayload";
codec_client_->sendData(*request_encoder_, request_payload, false);
ASSERT_TRUE(upstream_request_->waitForData(*dispatcher_, request_payload));
// Send both a chunked response body and "websocket" payload.
std::string response_payload = "4\r\nabcd\r\n0\r\n\r\nSomeWebsocketResponsePayload";
upstream_request_->encodeData(response_payload, false);
response_->waitForBodyData(response_payload.size());
EXPECT_EQ(response_payload, response_->body());
// Verify follow-up bidirectional data still works.
codec_client_->sendData(*request_encoder_, "FinalClientPayload", false);
ASSERT_TRUE(upstream_request_->waitForData(*dispatcher_, request_payload + "FinalClientPayload"));
upstream_request_->encodeData("FinalServerPayload", false);
response_->waitForBodyData(response_->body().size() + 5);
EXPECT_EQ(response_payload + "FinalServerPayload", response_->body());
// Clean up.
codec_client_->close();
ASSERT_TRUE(waitForUpstreamDisconnectOrReset());
}
} // namespace Envoy