forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
integration_test.cc
2687 lines (2315 loc) · 116 KB
/
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "test/integration/integration_test.h"
#include <string>
#include "envoy/config/bootstrap/v3/bootstrap.pb.h"
#include "envoy/config/listener/v3/listener.pb.h"
#include "envoy/config/route/v3/route_components.pb.h"
#include "envoy/extensions/filters/network/http_connection_manager/v3/http_connection_manager.pb.h"
#include "envoy/registry/registry.h"
#include "source/common/http/header_map_impl.h"
#include "source/common/http/headers.h"
#include "source/common/network/socket_option_factory.h"
#include "source/common/network/socket_option_impl.h"
#include "source/common/network/utility.h"
#include "source/common/protobuf/utility.h"
#include "test/integration/autonomous_upstream.h"
#include "test/integration/filters/process_context_filter.h"
#include "test/integration/filters/stop_and_continue_filter_config.pb.h"
#include "test/integration/utility.h"
#include "test/mocks/http/mocks.h"
#include "test/test_common/network_utility.h"
#include "test/test_common/printers.h"
#include "test/test_common/registry.h"
#include "test/test_common/utility.h"
#include "gtest/gtest.h"
using Envoy::Http::Headers;
using Envoy::Http::HeaderValueOf;
using Envoy::Http::HttpStatusIs;
using testing::Combine;
using testing::ContainsRegex;
using testing::EndsWith;
using testing::HasSubstr;
using testing::Not;
using testing::StartsWith;
using testing::Values;
using testing::ValuesIn;
namespace Envoy {
namespace {
std::string normalizeDate(const std::string& s) {
const std::regex date_regex("date:[^\r]+");
return std::regex_replace(s, date_regex, "date: Mon, 01 Jan 2017 00:00:00 GMT");
}
void setDisallowAbsoluteUrl(
envoy::extensions::filters::network::http_connection_manager::v3::HttpConnectionManager& hcm) {
hcm.mutable_http_protocol_options()->mutable_allow_absolute_url()->set_value(false);
};
void setAllowHttp10WithDefaultHost(
envoy::extensions::filters::network::http_connection_manager::v3::HttpConnectionManager& hcm) {
hcm.mutable_http_protocol_options()->set_accept_http_10(true);
hcm.mutable_http_protocol_options()->set_default_host_for_http_10("default.com");
}
std::string testParamToString(
const testing::TestParamInfo<std::tuple<Network::Address::IpVersion, Http1ParserImpl>>&
params) {
return absl::StrCat(TestUtility::ipVersionToString(std::get<0>(params.param)),
TestUtility::http1ParserImplToString(std::get<1>(params.param)));
}
} // namespace
INSTANTIATE_TEST_SUITE_P(IpVersionsAndHttp1Parser, IntegrationTest,
Combine(ValuesIn(TestEnvironment::getIpVersionsForTest()),
Values(Http1ParserImpl::HttpParser, Http1ParserImpl::BalsaParser)),
testParamToString);
// Verify that we gracefully handle an invalid pre-bind socket option when using reuse_port.
TEST_P(IntegrationTest, BadPrebindSocketOptionWithReusePort) {
// Reserve a port that we can then use on the integration listener with reuse_port.
auto addr_socket =
Network::Test::bindFreeLoopbackPort(version_, Network::Socket::Type::Stream, true);
// Do not wait for listeners to start as the listener will fail.
defer_listener_finalization_ = true;
config_helper_.addConfigModifier([&](envoy::config::bootstrap::v3::Bootstrap& bootstrap) -> void {
auto* listener = bootstrap.mutable_static_resources()->mutable_listeners(0);
listener->mutable_address()->mutable_socket_address()->set_port_value(
addr_socket.second->connectionInfoProvider().localAddress()->ip()->port());
auto socket_option = listener->add_socket_options();
socket_option->set_state(envoy::config::core::v3::SocketOption::STATE_PREBIND);
socket_option->set_level(10000); // Invalid level.
socket_option->set_int_value(10000); // Invalid value.
});
initialize();
test_server_->waitForCounterGe("listener_manager.listener_create_failure", 1);
}
// Verify that we gracefully handle an invalid post-bind socket option when using reuse_port.
TEST_P(IntegrationTest, BadPostbindSocketOptionWithReusePort) {
// Reserve a port that we can then use on the integration listener with reuse_port.
auto addr_socket =
Network::Test::bindFreeLoopbackPort(version_, Network::Socket::Type::Stream, true);
// Do not wait for listeners to start as the listener will fail.
defer_listener_finalization_ = true;
config_helper_.addConfigModifier([&](envoy::config::bootstrap::v3::Bootstrap& bootstrap) -> void {
auto* listener = bootstrap.mutable_static_resources()->mutable_listeners(0);
listener->mutable_address()->mutable_socket_address()->set_port_value(
addr_socket.second->connectionInfoProvider().localAddress()->ip()->port());
auto socket_option = listener->add_socket_options();
socket_option->set_state(envoy::config::core::v3::SocketOption::STATE_BOUND);
socket_option->set_level(10000); // Invalid level.
socket_option->set_int_value(10000); // Invalid value.
});
initialize();
test_server_->waitForCounterGe("listener_manager.listener_create_failure", 1);
}
// Verify that we gracefully handle an invalid post-listen socket option.
TEST_P(IntegrationTest, BadPostListenSocketOption) {
// Do not wait for listeners to start as the listener will fail.
defer_listener_finalization_ = true;
config_helper_.addConfigModifier([&](envoy::config::bootstrap::v3::Bootstrap& bootstrap) -> void {
auto* listener = bootstrap.mutable_static_resources()->mutable_listeners(0);
auto socket_option = listener->add_socket_options();
socket_option->set_state(envoy::config::core::v3::SocketOption::STATE_LISTENING);
socket_option->set_level(10000); // Invalid level.
socket_option->set_int_value(10000); // Invalid value.
});
initialize();
test_server_->waitForCounterGe("listener_manager.listener_create_failure", 1);
}
// Make sure we have correctly specified per-worker performance stats.
TEST_P(IntegrationTest, PerWorkerStatsAndBalancing) {
DISABLE_IF_ADMIN_DISABLED; // Uses admin stats
concurrency_ = 2;
config_helper_.addConfigModifier([&](envoy::config::bootstrap::v3::Bootstrap& bootstrap) -> void {
auto* listener = bootstrap.mutable_static_resources()->mutable_listeners(0);
listener->mutable_connection_balance_config()->mutable_exact_balance();
});
initialize();
// Per-worker listener stats.
auto check_listener_stats = [this](uint64_t cx_active, uint64_t cx_total) {
if (version_ == Network::Address::IpVersion::v4) {
test_server_->waitForGaugeEq("listener.127.0.0.1_0.worker_0.downstream_cx_active", cx_active);
test_server_->waitForGaugeEq("listener.127.0.0.1_0.worker_1.downstream_cx_active", cx_active);
test_server_->waitForCounterEq("listener.127.0.0.1_0.worker_0.downstream_cx_total", cx_total);
test_server_->waitForCounterEq("listener.127.0.0.1_0.worker_1.downstream_cx_total", cx_total);
} else {
test_server_->waitForGaugeEq("listener.[__1]_0.worker_0.downstream_cx_active", cx_active);
test_server_->waitForGaugeEq("listener.[__1]_0.worker_1.downstream_cx_active", cx_active);
test_server_->waitForCounterEq("listener.[__1]_0.worker_0.downstream_cx_total", cx_total);
test_server_->waitForCounterEq("listener.[__1]_0.worker_1.downstream_cx_total", cx_total);
}
};
check_listener_stats(0, 0);
// Main thread admin listener stats.
test_server_->waitForCounterExists("listener.admin.main_thread.downstream_cx_total");
// Per-thread watchdog stats.
test_server_->waitForCounterExists("server.main_thread.watchdog_miss");
test_server_->waitForCounterExists("server.worker_0.watchdog_miss");
test_server_->waitForCounterExists("server.worker_1.watchdog_miss");
codec_client_ = makeHttpConnection(lookupPort("http"));
IntegrationCodecClientPtr codec_client2 = makeHttpConnection(lookupPort("http"));
check_listener_stats(1, 1);
codec_client_->close();
codec_client2->close();
check_listener_stats(0, 1);
}
class TestConnectionBalanceFactory : public Network::ConnectionBalanceFactory {
public:
ProtobufTypes::MessagePtr createEmptyConfigProto() override {
// Using Struct instead of a custom empty config proto. This is only allowed in tests.
return ProtobufTypes::MessagePtr{new Envoy::ProtobufWkt::Struct()};
}
Network::ConnectionBalancerSharedPtr
createConnectionBalancerFromProto(const Protobuf::Message&,
Server::Configuration::FactoryContext&) override {
return std::make_shared<Network::ExactConnectionBalancerImpl>();
}
std::string name() const override { return "envoy.network.connection_balance.test"; }
};
// Test extend balance.
TEST_P(IntegrationTest, ConnectionBalanceFactory) {
DISABLE_IF_ADMIN_DISABLED; // Uses admin stats
concurrency_ = 2;
TestConnectionBalanceFactory factory;
Registry::InjectFactory<Envoy::Network::ConnectionBalanceFactory> registered(factory);
config_helper_.addConfigModifier([&](envoy::config::bootstrap::v3::Bootstrap& bootstrap) -> void {
TestConnectionBalanceFactory test_connection_balancer;
Registry::InjectFactory<Envoy::Network::ConnectionBalanceFactory> inject_factory(
test_connection_balancer);
auto* listener = bootstrap.mutable_static_resources()->mutable_listeners(0);
auto* connection_balance_config = listener->mutable_connection_balance_config();
auto* extend_balance_config = connection_balance_config->mutable_extend_balance();
extend_balance_config->set_name("envoy.network.connection_balance.test");
extend_balance_config->mutable_typed_config()->set_type_url(
"type.googleapis.com/google.protobuf.Struct");
});
initialize();
auto check_listener_stats = [this](uint64_t cx_active, uint64_t cx_total) {
if (version_ == Network::Address::IpVersion::v4) {
test_server_->waitForGaugeEq("listener.127.0.0.1_0.worker_0.downstream_cx_active", cx_active);
test_server_->waitForGaugeEq("listener.127.0.0.1_0.worker_1.downstream_cx_active", cx_active);
test_server_->waitForCounterEq("listener.127.0.0.1_0.worker_0.downstream_cx_total", cx_total);
test_server_->waitForCounterEq("listener.127.0.0.1_0.worker_1.downstream_cx_total", cx_total);
} else {
test_server_->waitForGaugeEq("listener.[__1]_0.worker_0.downstream_cx_active", cx_active);
test_server_->waitForGaugeEq("listener.[__1]_0.worker_1.downstream_cx_active", cx_active);
test_server_->waitForCounterEq("listener.[__1]_0.worker_0.downstream_cx_total", cx_total);
test_server_->waitForCounterEq("listener.[__1]_0.worker_1.downstream_cx_total", cx_total);
}
};
check_listener_stats(0, 0);
// Main thread admin listener stats.
test_server_->waitForCounterExists("listener.admin.main_thread.downstream_cx_total");
// Per-thread watchdog stats.
test_server_->waitForCounterExists("server.main_thread.watchdog_miss");
test_server_->waitForCounterExists("server.worker_0.watchdog_miss");
test_server_->waitForCounterExists("server.worker_1.watchdog_miss");
codec_client_ = makeHttpConnection(lookupPort("http"));
IntegrationCodecClientPtr codec_client2 = makeHttpConnection(lookupPort("http"));
check_listener_stats(1, 1);
codec_client_->close();
codec_client2->close();
check_listener_stats(0, 1);
}
// On OSX this is flaky as we can end up with connection imbalance.
#if !defined(__APPLE__)
// Make sure all workers pick up connections
TEST_P(IntegrationTest, AllWorkersAreHandlingLoad) {
concurrency_ = 2;
initialize();
std::string worker0_stat_name, worker1_stat_name;
if (version_ == Network::Address::IpVersion::v4) {
worker0_stat_name = "listener.127.0.0.1_0.worker_0.downstream_cx_total";
worker1_stat_name = "listener.127.0.0.1_0.worker_1.downstream_cx_total";
} else {
worker0_stat_name = "listener.[__1]_0.worker_0.downstream_cx_total";
worker1_stat_name = "listener.[__1]_0.worker_1.downstream_cx_total";
}
test_server_->waitForCounterEq(worker0_stat_name, 0);
test_server_->waitForCounterEq(worker1_stat_name, 0);
// We set the counters for the two workers to see how many connections each handles.
uint64_t w0_ctr = 0;
uint64_t w1_ctr = 0;
constexpr int loops = 5;
for (int i = 0; i < loops; i++) {
constexpr int requests_per_loop = 4;
std::array<IntegrationCodecClientPtr, requests_per_loop> connections;
for (int j = 0; j < requests_per_loop; j++) {
connections[j] = makeHttpConnection(lookupPort("http"));
}
auto worker0_ctr = test_server_->counter(worker0_stat_name);
auto worker1_ctr = test_server_->counter(worker1_stat_name);
auto target = w0_ctr + w1_ctr + requests_per_loop;
while (test_server_->counter(worker0_stat_name)->value() +
test_server_->counter(worker1_stat_name)->value() <
target) {
timeSystem().advanceTimeWait(std::chrono::milliseconds(10));
}
w0_ctr = test_server_->counter(worker0_stat_name)->value();
w1_ctr = test_server_->counter(worker1_stat_name)->value();
for (int j = 0; j < requests_per_loop; j++) {
connections[j]->close();
}
}
EXPECT_TRUE(w0_ctr > 1);
EXPECT_TRUE(w1_ctr > 1);
}
#endif
TEST_P(IntegrationTest, RouterDirectResponseWithBody) {
const std::string body = "Response body";
const std::string file_path = TestEnvironment::writeStringToFileForTest("test_envoy", body);
static const std::string domain("direct.example.com");
static const std::string prefix("/");
static const Http::Code status(Http::Code::OK);
config_helper_.addConfigModifier(
[&](envoy::extensions::filters::network::http_connection_manager::v3::HttpConnectionManager&
hcm) -> void {
auto* route_config = hcm.mutable_route_config();
auto* header_value_option = route_config->mutable_response_headers_to_add()->Add();
header_value_option->mutable_header()->set_key("x-additional-header");
header_value_option->mutable_header()->set_value("example-value");
header_value_option->set_append_action(
envoy::config::core::v3::HeaderValueOption::OVERWRITE_IF_EXISTS_OR_ADD);
header_value_option = route_config->mutable_response_headers_to_add()->Add();
header_value_option->mutable_header()->set_key("content-type");
header_value_option->mutable_header()->set_value("text/html");
header_value_option->set_append_action(
envoy::config::core::v3::HeaderValueOption::OVERWRITE_IF_EXISTS_OR_ADD);
// Add a wrong content-length.
header_value_option = route_config->mutable_response_headers_to_add()->Add();
header_value_option->mutable_header()->set_key("content-length");
header_value_option->mutable_header()->set_value("2000");
header_value_option->set_append_action(
envoy::config::core::v3::HeaderValueOption::OVERWRITE_IF_EXISTS_OR_ADD);
auto* virtual_host = route_config->add_virtual_hosts();
virtual_host->set_name(domain);
virtual_host->add_domains(domain);
virtual_host->add_routes()->mutable_match()->set_prefix(prefix);
virtual_host->mutable_routes(0)->mutable_direct_response()->set_status(
static_cast<uint32_t>(status));
virtual_host->mutable_routes(0)->mutable_direct_response()->mutable_body()->set_filename(
file_path);
});
initialize();
BufferingStreamDecoderPtr response = IntegrationUtil::makeSingleRequest(
lookupPort("http"), "GET", "/", "", downstream_protocol_, version_, "direct.example.com");
ASSERT_TRUE(response->complete());
EXPECT_EQ("200", response->headers().getStatusValue());
EXPECT_EQ("example-value", response->headers()
.get(Envoy::Http::LowerCaseString("x-additional-header"))[0]
->value()
.getStringView());
EXPECT_EQ("text/html", response->headers().getContentTypeValue());
// Verify content-length is correct.
EXPECT_EQ(fmt::format("{}", body.size()), response->headers().getContentLengthValue());
EXPECT_EQ(body, response->body());
}
TEST_P(IntegrationTest, RouterDirectResponseEmptyBody) {
useAccessLog("%ROUTE_NAME%");
static const std::string domain("direct.example.com");
static const std::string prefix("/");
static const Http::Code status(Http::Code::OK);
static const std::string route_name("direct_response_route");
config_helper_.addConfigModifier(
[&](envoy::extensions::filters::network::http_connection_manager::v3::HttpConnectionManager&
hcm) -> void {
auto* route_config = hcm.mutable_route_config();
auto* header_value_option = route_config->mutable_response_headers_to_add()->Add();
header_value_option->mutable_header()->set_key("x-additional-header");
header_value_option->mutable_header()->set_value("example-value");
header_value_option->set_append_action(
envoy::config::core::v3::HeaderValueOption::OVERWRITE_IF_EXISTS_OR_ADD);
header_value_option = route_config->mutable_response_headers_to_add()->Add();
header_value_option->mutable_header()->set_key("content-type");
header_value_option->mutable_header()->set_value("text/html");
header_value_option->set_append_action(
envoy::config::core::v3::HeaderValueOption::OVERWRITE_IF_EXISTS_OR_ADD);
// Add a wrong content-length.
header_value_option = route_config->mutable_response_headers_to_add()->Add();
header_value_option->mutable_header()->set_key("content-length");
header_value_option->mutable_header()->set_value("2000");
header_value_option->set_append_action(
envoy::config::core::v3::HeaderValueOption::OVERWRITE_IF_EXISTS_OR_ADD);
auto* virtual_host = route_config->add_virtual_hosts();
virtual_host->set_name(domain);
virtual_host->add_domains(domain);
virtual_host->add_routes()->mutable_match()->set_prefix(prefix);
virtual_host->mutable_routes(0)->mutable_direct_response()->set_status(
static_cast<uint32_t>(status));
virtual_host->mutable_routes(0)->set_name(route_name);
});
initialize();
BufferingStreamDecoderPtr response = IntegrationUtil::makeSingleRequest(
lookupPort("http"), "GET", "/", "", downstream_protocol_, version_, "direct.example.com");
ASSERT_TRUE(response->complete());
EXPECT_EQ("200", response->headers().getStatusValue());
EXPECT_EQ("example-value", response->headers()
.get(Envoy::Http::LowerCaseString("x-additional-header"))[0]
->value()
.getStringView());
// Content-type header is removed.
EXPECT_EQ(nullptr, response->headers().ContentType());
// Content-length header is correct.
EXPECT_EQ("0", response->headers().getContentLengthValue());
std::string log = waitForAccessLog(access_log_name_);
EXPECT_THAT(log, HasSubstr(route_name));
}
TEST_P(IntegrationTest, ConnectionClose) {
autonomous_upstream_ = true;
initialize();
codec_client_ = makeHttpConnection(lookupPort("http"));
auto response = codec_client_->makeHeaderOnlyRequest(Http::TestRequestHeaderMapImpl{
{":method", "GET"}, {":path", "/"}, {":authority", "host"}, {"connection", "close"}});
ASSERT_TRUE(response->waitForEndStream());
ASSERT_TRUE(codec_client_->waitForDisconnect());
EXPECT_TRUE(response->complete());
EXPECT_THAT(response->headers(), HttpStatusIs("200"));
}
TEST_P(IntegrationTest, RouterRequestAndResponseWithBodyNoBuffer) {
testRouterRequestAndResponseWithBody(1024, 512, false, false);
}
TEST_P(IntegrationTest, RouterRequestAndResponseWithGiantBodyNoBuffer) {
testRouterRequestAndResponseWithBody(10 * 1024 * 1024, 10 * 1024 * 1024, false, false);
}
TEST_P(IntegrationTest, FlowControlOnAndGiantBody) {
config_helper_.setBufferLimits(1024, 1024);
testRouterRequestAndResponseWithBody(10 * 1024 * 1024, 10 * 1024 * 1024, false, false);
}
TEST_P(IntegrationTest, LargeFlowControlOnAndGiantBody) {
config_helper_.setBufferLimits(128 * 1024, 128 * 1024);
testRouterRequestAndResponseWithBody(10 * 1024 * 1024, 10 * 1024 * 1024, false, false);
}
TEST_P(IntegrationTest, RouterRequestAndResponseWithBodyAndContentLengthNoBuffer) {
testRouterRequestAndResponseWithBody(1024, 512, false, true);
}
TEST_P(IntegrationTest, RouterRequestAndResponseWithGiantBodyAndContentLengthNoBuffer) {
testRouterRequestAndResponseWithBody(10 * 1024 * 1024, 10 * 1024 * 1024, false, true);
}
TEST_P(IntegrationTest, FlowControlOnAndGiantBodyWithContentLength) {
config_helper_.setBufferLimits(1024, 1024);
testRouterRequestAndResponseWithBody(10 * 1024 * 1024, 10 * 1024 * 1024, false, true);
}
TEST_P(IntegrationTest, LargeFlowControlOnAndGiantBodyWithContentLength) {
config_helper_.setBufferLimits(128 * 1024, 128 * 1024);
testRouterRequestAndResponseWithBody(10 * 1024 * 1024, 10 * 1024 * 1024, false, true);
}
TEST_P(IntegrationTest, RouterRequestAndResponseLargeHeaderNoBuffer) {
testRouterRequestAndResponseWithBody(1024, 512, true);
}
TEST_P(IntegrationTest, RouterHeaderOnlyRequestAndResponseNoBuffer) {
testRouterHeaderOnlyRequestAndResponse();
}
TEST_P(IntegrationTest, RouterUpstreamDisconnectBeforeRequestcomplete) {
testRouterUpstreamDisconnectBeforeRequestComplete();
}
TEST_P(IntegrationTest, RouterUpstreamDisconnectBeforeResponseComplete) {
testRouterUpstreamDisconnectBeforeResponseComplete();
}
// Regression test for https://github.com/envoyproxy/envoy/issues/9508
TEST_P(IntegrationTest, ResponseFramedByConnectionCloseWithReadLimits) {
// Set a small buffer limit on the downstream in order to trigger a call to trigger readDisable on
// the upstream when proxying the response. Upstream limit needs to be larger so that
// RawBufferSocket::doRead reads the response body and detects the upstream close in the same call
// stack.
config_helper_.setBufferLimits(100000, 1);
initialize();
codec_client_ = makeHttpConnection(makeClientConnection((lookupPort("http"))));
auto response = codec_client_->makeHeaderOnlyRequest(default_request_headers_);
waitForNextUpstreamRequest();
// Disable chunk encoding to trigger framing by connection close.
upstream_request_->http1StreamEncoderOptions().value().get().disableChunkEncoding();
upstream_request_->encodeHeaders(Http::TestResponseHeaderMapImpl{{":status", "200"}}, false);
upstream_request_->encodeData(512, true);
ASSERT_TRUE(fake_upstream_connection_->close());
ASSERT_TRUE(response->waitForEndStream());
EXPECT_TRUE(response->complete());
EXPECT_THAT(response->headers(), HttpStatusIs("200"));
EXPECT_EQ(512, response->body().size());
}
TEST_P(IntegrationTest, RouterDownstreamDisconnectBeforeRequestComplete) {
testRouterDownstreamDisconnectBeforeRequestComplete();
}
TEST_P(IntegrationTest, RouterDownstreamDisconnectBeforeResponseComplete) {
testRouterDownstreamDisconnectBeforeResponseComplete();
}
TEST_P(IntegrationTest, RouterUpstreamResponseBeforeRequestComplete) {
testRouterUpstreamResponseBeforeRequestComplete();
}
TEST_P(IntegrationTest, EnvoyProxyingEarly1xxWithEncoderFilter) {
testEnvoyProxying1xx(true, true);
}
TEST_P(IntegrationTest, EnvoyProxyingLate1xxWithEncoderFilter) {
testEnvoyProxying1xx(false, true);
}
// Regression test for https://github.com/envoyproxy/envoy/issues/10923.
TEST_P(IntegrationTest, EnvoyProxying1xxWithDecodeDataPause) {
config_helper_.prependFilter(R"EOF(
name: stop-iteration-and-continue-filter
typed_config:
"@type": type.googleapis.com/test.integration.filters.StopAndContinueConfig
)EOF");
testEnvoyProxying1xx(true);
}
// Test the x-envoy-is-timeout-retry header is set to false for retries that are not
// initiated by timeouts.
TEST_P(IntegrationTest, RouterIsTimeoutRetryHeader) {
auto host = config_helper_.createVirtualHost("example.com", "/test_retry");
host.set_include_is_timeout_retry_header(true);
config_helper_.addVirtualHost(host);
initialize();
codec_client_ = makeHttpConnection(lookupPort("http"));
auto response = codec_client_->makeRequestWithBody(
Http::TestRequestHeaderMapImpl{{":method", "POST"},
{":path", "/test_retry"},
{":scheme", "http"},
{":authority", "example.com"},
{"x-forwarded-for", "10.0.0.1"},
{"x-envoy-retry-on", "5xx"}},
1024);
waitForNextUpstreamRequest();
// Send a non-timeout failure response.
upstream_request_->encodeHeaders(Http::TestResponseHeaderMapImpl{{":status", "503"}}, false);
if (fake_upstreams_[0]->httpType() == Http::CodecType::HTTP1) {
ASSERT_TRUE(fake_upstream_connection_->waitForDisconnect());
ASSERT_TRUE(fake_upstreams_[0]->waitForHttpConnection(*dispatcher_, fake_upstream_connection_));
} else {
ASSERT_TRUE(upstream_request_->waitForReset());
}
// 5XX responses are retried.
waitForNextUpstreamRequest();
// The request did not fail due to a timeout, therefore we expect the x-envoy-is-timeout-retry
// header to be false.
EXPECT_EQ(upstream_request_->headers().getEnvoyIsTimeoutRetryValue(), "false");
// Return 200 to the retry.
upstream_request_->encodeHeaders(default_response_headers_, false);
upstream_request_->encodeData(512, true);
ASSERT_TRUE(response->waitForEndStream());
EXPECT_TRUE(upstream_request_->complete());
EXPECT_EQ(1024U, upstream_request_->bodyLength());
EXPECT_TRUE(response->complete());
EXPECT_EQ("200", response->headers().getStatusValue());
EXPECT_EQ(512U, response->body().size());
}
// Verifies that we can construct a match tree with a filter, and that we are able to skip
// filter invocation through the match tree.
TEST_P(IntegrationTest, MatchingHttpFilterConstruction) {
concurrency_ = 2;
config_helper_.prependFilter(R"EOF(
name: matcher
typed_config:
"@type": type.googleapis.com/envoy.extensions.common.matching.v3.ExtensionWithMatcher
extension_config:
name: set-response-code
typed_config:
"@type": type.googleapis.com/test.integration.filters.SetResponseCodeFilterConfig
code: 403
xds_matcher:
matcher_tree:
input:
name: request-headers
typed_config:
"@type": type.googleapis.com/envoy.type.matcher.v3.HttpRequestHeaderMatchInput
header_name: match-header
exact_match_map:
map:
match:
action:
name: skip
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.common.matcher.action.v3.SkipFilter
)EOF");
initialize();
{
codec_client_ = makeHttpConnection(lookupPort("http"));
auto response = codec_client_->makeRequestWithBody(default_request_headers_, 1024);
ASSERT_TRUE(response->waitForEndStream());
EXPECT_THAT(response->headers(), HttpStatusIs("403"));
codec_client_->close();
}
{
codec_client_ = makeHttpConnection(lookupPort("http"));
Http::TestRequestHeaderMapImpl request_headers{
{":method", "POST"}, {":path", "/test/long/url"}, {":scheme", "http"},
{":authority", "host"}, {"match-header", "match"}, {"content-type", "application/grpc"}};
auto response = codec_client_->makeRequestWithBody(request_headers, 1024);
waitForNextUpstreamRequest();
upstream_request_->encodeHeaders(default_response_headers_, true);
ASSERT_TRUE(response->waitForEndStream());
EXPECT_THAT(response->headers(), HttpStatusIs("200"));
}
auto second_codec = makeHttpConnection(lookupPort("http"));
Http::TestRequestHeaderMapImpl request_headers{
{":method", "POST"}, {":path", "/test/long/url"}, {":scheme", "http"},
{":authority", "host"}, {"match-header", "not-match"}, {"content-type", "application/grpc"}};
auto response = second_codec->makeRequestWithBody(request_headers, 1024);
ASSERT_TRUE(response->waitForEndStream());
EXPECT_THAT(response->headers(), HttpStatusIs("200"));
codec_client_->close();
second_codec->close();
}
// Verifies that we can construct a match tree with a filter using the new matcher tree proto, and
// that we are able to skip filter invocation through the match tree.
TEST_P(IntegrationTest, MatchingHttpFilterConstructionNewProto) {
concurrency_ = 2;
config_helper_.prependFilter(R"EOF(
name: matcher
typed_config:
"@type": type.googleapis.com/envoy.extensions.common.matching.v3.ExtensionWithMatcher
extension_config:
name: set-response-code
typed_config:
"@type": type.googleapis.com/test.integration.filters.SetResponseCodeFilterConfig
code: 403
xds_matcher:
matcher_tree:
input:
name: request-headers
typed_config:
"@type": type.googleapis.com/envoy.type.matcher.v3.HttpRequestHeaderMatchInput
header_name: match-header
exact_match_map:
map:
match:
action:
name: skip
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.common.matcher.action.v3.SkipFilter
)EOF");
initialize();
{
codec_client_ = makeHttpConnection(lookupPort("http"));
auto response = codec_client_->makeRequestWithBody(default_request_headers_, 1024);
ASSERT_TRUE(response->waitForEndStream());
EXPECT_THAT(response->headers(), HttpStatusIs("403"));
codec_client_->close();
}
{
codec_client_ = makeHttpConnection(lookupPort("http"));
Http::TestRequestHeaderMapImpl request_headers{
{":method", "POST"}, {":path", "/test/long/url"}, {":scheme", "http"},
{":authority", "host"}, {"match-header", "match"}, {"content-type", "application/grpc"}};
auto response = codec_client_->makeRequestWithBody(request_headers, 1024);
waitForNextUpstreamRequest();
upstream_request_->encodeHeaders(default_response_headers_, true);
ASSERT_TRUE(response->waitForEndStream());
EXPECT_THAT(response->headers(), HttpStatusIs("200"));
}
auto second_codec = makeHttpConnection(lookupPort("http"));
Http::TestRequestHeaderMapImpl request_headers{
{":method", "POST"}, {":path", "/test/long/url"}, {":scheme", "http"},
{":authority", "host"}, {"match-header", "not-match"}, {"content-type", "application/grpc"}};
auto response = second_codec->makeRequestWithBody(request_headers, 1024);
ASSERT_TRUE(response->waitForEndStream());
EXPECT_THAT(response->headers(), HttpStatusIs("200"));
codec_client_->close();
second_codec->close();
}
// Verifies routing via the match tree API.
TEST_P(IntegrationTest, MatchTreeRouting) {
const std::string vhost_yaml = R"EOF(
name: vhost
domains: ["matcher.com"]
matcher:
matcher_tree:
input:
name: request-headers
typed_config:
"@type": type.googleapis.com/envoy.type.matcher.v3.HttpRequestHeaderMatchInput
header_name: match-header
exact_match_map:
map:
"route":
action:
name: route
typed_config:
"@type": type.googleapis.com/envoy.config.route.v3.Route
match:
prefix: /
route:
cluster: cluster_0
)EOF";
envoy::config::route::v3::VirtualHost virtual_host;
TestUtility::loadFromYaml(vhost_yaml, virtual_host);
config_helper_.addVirtualHost(virtual_host);
autonomous_upstream_ = true;
initialize();
codec_client_ = makeHttpConnection(lookupPort("http"));
Http::TestRequestHeaderMapImpl headers{{":method", "GET"},
{":path", "/whatever"},
{":scheme", "http"},
{"match-header", "route"},
{":authority", "matcher.com"}};
auto response = codec_client_->makeHeaderOnlyRequest(headers);
ASSERT_TRUE(response->waitForEndStream());
EXPECT_THAT(response->headers(), HttpStatusIs("200"));
codec_client_->close();
}
// Verifies routing via the match tree API with prefix matching.
TEST_P(IntegrationTest, PrefixMatchTreeRouting) {
const std::string vhost_yaml = R"EOF(
name: vhost
domains: ["matcher.com"]
matcher:
matcher_tree:
input:
name: request-headers
typed_config:
"@type": type.googleapis.com/envoy.type.matcher.v3.HttpRequestHeaderMatchInput
header_name: match-header
prefix_match_map:
map:
"r":
action:
name: route
typed_config:
"@type": type.googleapis.com/envoy.config.route.v3.Route
match:
prefix: /
route:
cluster: cluster_0
)EOF";
envoy::config::route::v3::VirtualHost virtual_host;
TestUtility::loadFromYaml(vhost_yaml, virtual_host);
config_helper_.addVirtualHost(virtual_host);
autonomous_upstream_ = true;
initialize();
codec_client_ = makeHttpConnection(lookupPort("http"));
Http::TestRequestHeaderMapImpl headers{{":method", "GET"},
{":path", "/whatever"},
{":scheme", "http"},
{"match-header", "route"},
{":authority", "matcher.com"}};
auto response = codec_client_->makeHeaderOnlyRequest(headers);
ASSERT_TRUE(response->waitForEndStream());
EXPECT_THAT(response->headers(), HttpStatusIs("200"));
codec_client_->close();
}
// This is a regression for https://github.com/envoyproxy/envoy/issues/2715 and validates that a
// pending request is not sent on a connection that has been half-closed.
TEST_P(IntegrationTest, UpstreamDisconnectWithTwoRequests) {
config_helper_.addConfigModifier([](envoy::config::bootstrap::v3::Bootstrap& bootstrap) {
auto* static_resources = bootstrap.mutable_static_resources();
auto* cluster = static_resources->mutable_clusters(0);
// Ensure we only have one connection upstream, one request active at a time.
ConfigHelper::HttpProtocolOptions protocol_options;
protocol_options.mutable_common_http_protocol_options()
->mutable_max_requests_per_connection()
->set_value(1);
protocol_options.mutable_use_downstream_protocol_config();
auto* circuit_breakers = cluster->mutable_circuit_breakers();
circuit_breakers->add_thresholds()->mutable_max_connections()->set_value(1);
ConfigHelper::setProtocolOptions(*bootstrap.mutable_static_resources()->mutable_clusters(0),
protocol_options);
});
initialize();
codec_client_ = makeHttpConnection(lookupPort("http"));
// Request 1.
auto response = codec_client_->makeRequestWithBody(default_request_headers_, 1024);
waitForNextUpstreamRequest();
// Request 2.
IntegrationCodecClientPtr codec_client2 = makeHttpConnection(lookupPort("http"));
auto response2 = codec_client2->makeRequestWithBody(default_request_headers_, 512);
// Validate one request active, the other pending.
test_server_->waitForGaugeEq("cluster.cluster_0.upstream_rq_active", 1);
test_server_->waitForGaugeEq("cluster.cluster_0.upstream_rq_pending_active", 1);
// Response 1.
upstream_request_->encodeHeaders(default_response_headers_, false);
upstream_request_->encodeData(512, true);
ASSERT_TRUE(fake_upstream_connection_->close());
ASSERT_TRUE(response->waitForEndStream());
EXPECT_TRUE(upstream_request_->complete());
EXPECT_TRUE(response->complete());
EXPECT_EQ("200", response->headers().getStatusValue());
test_server_->waitForCounterGe("cluster.cluster_0.upstream_cx_total", 1);
test_server_->waitForCounterGe("cluster.cluster_0.upstream_rq_200", 1);
// Response 2.
ASSERT_TRUE(fake_upstream_connection_->waitForDisconnect());
fake_upstream_connection_.reset();
waitForNextUpstreamRequest();
upstream_request_->encodeHeaders(default_response_headers_, false);
upstream_request_->encodeData(1024, true);
ASSERT_TRUE(response2->waitForEndStream());
codec_client2->close();
EXPECT_TRUE(upstream_request_->complete());
EXPECT_TRUE(response2->complete());
EXPECT_EQ("200", response2->headers().getStatusValue());
test_server_->waitForCounterGe("cluster.cluster_0.upstream_cx_total", 2);
test_server_->waitForCounterGe("cluster.cluster_0.upstream_rq_200", 2);
}
TEST_P(IntegrationTest, TestSmuggling) {
#ifdef ENVOY_ENABLE_UHV
// TODO(#23289) - uniform handling of Transfer-Encoding validation between codec and UHV
return;
#endif
config_helper_.disableDelayClose();
initialize();
// Make sure the http parser rejects having content-length and transfer-encoding: chunked
// on the same request, regardless of order and spacing.
{
std::string response;
const std::string full_request = "GET / HTTP/1.1\r\n"
"Host: host\r\ncontent-length: 0\r\n"
"transfer-encoding: chunked\r\n\r\n";
sendRawHttpAndWaitForResponse(lookupPort("http"), full_request.c_str(), &response, false);
EXPECT_THAT(response, StartsWith("HTTP/1.1 400 Bad Request\r\n"));
}
// Check with a non-zero content length as well.
{
std::string response;
const std::string full_request = "GET / HTTP/1.1\r\n"
"Host: host\r\ncontent-length: 36\r\n"
"transfer-encoding: chunked\r\n\r\n";
sendRawHttpAndWaitForResponse(lookupPort("http"), full_request.c_str(), &response, false);
EXPECT_THAT(response, StartsWith("HTTP/1.1 400 Bad Request\r\n"));
}
// Make sure transfer encoding is still treated as such with leading whitespace.
{
std::string response;
const std::string full_request = "GET / HTTP/1.1\r\n"
"Host: host\r\ncontent-length: 0\r\n"
"\ttransfer-encoding: chunked\r\n\r\n";
sendRawHttpAndWaitForResponse(lookupPort("http"), full_request.c_str(), &response, false);
EXPECT_THAT(response, HasSubstr("HTTP/1.1 400 Bad Request\r\n"));
}
{
std::string response;
const std::string request = "GET / HTTP/1.1\r\nHost: host\r\ntransfer-encoding: chunked "
"\r\ncontent-length: 36\r\n\r\n";
sendRawHttpAndWaitForResponse(lookupPort("http"), request.c_str(), &response, false);
EXPECT_THAT(response, StartsWith("HTTP/1.1 400 Bad Request\r\n"));
}
{
std::string response;
const std::string request = "GET / HTTP/1.1\r\nHost: host\r\ntransfer-encoding: "
"identity,chunked \r\ncontent-length: 36\r\n\r\n";
sendRawHttpAndWaitForResponse(lookupPort("http"), request.c_str(), &response, false);
EXPECT_THAT(response, StartsWith("HTTP/1.1 400 Bad Request\r\n"));
}
{
// Verify that sending `Transfer-Encoding: chunked` as a second header is detected and triggers
// the "no Transfer-Encoding + Content-Length" check.
std::string response;
const std::string request =
"GET / HTTP/1.1\r\nHost: host\r\ntransfer-encoding: "
"identity\r\ncontent-length: 36\r\ntransfer-encoding: chunked \r\n\r\n";
sendRawHttpAndWaitForResponse(lookupPort("http"), request.c_str(), &response, false);
EXPECT_THAT(response, StartsWith("HTTP/1.1 400 Bad Request\r\n"));
}
}
TEST_P(IntegrationTest, TestPipelinedResponses) {
initialize();
auto tcp_client = makeTcpConnection(lookupPort("http"));
ASSERT_TRUE(tcp_client->write(
"POST /test/long/url HTTP/1.1\r\nHost: host\r\ntransfer-encoding: chunked\r\n\r\n"));
FakeRawConnectionPtr fake_upstream_connection;
ASSERT_TRUE(fake_upstreams_[0]->waitForRawConnection(fake_upstream_connection));
std::string data;
ASSERT_TRUE(fake_upstream_connection->waitForData(
FakeRawConnection::waitForInexactMatch("\r\n\r\n"), &data));
ASSERT_THAT(data, StartsWith("POST"));
ASSERT_TRUE(fake_upstream_connection->write(
"HTTP/1.1 200 OK\r\ntransfer-encoding: chunked\r\n\r\n0\r\n\r\n"
"HTTP/1.1 200 OK\r\ntransfer-encoding: chunked\r\n\r\n0\r\n\r\n"
"HTTP/1.1 200 OK\r\ntransfer-encoding: chunked\r\n\r\n0\r\n\r\n"));
tcp_client->waitForData("0\r\n\r\n", false);
std::string response = tcp_client->data();
EXPECT_THAT(response, StartsWith("HTTP/1.1 200 OK\r\n"));
EXPECT_THAT(response, HasSubstr("transfer-encoding: chunked\r\n"));
EXPECT_THAT(response, EndsWith("0\r\n\r\n"));
ASSERT_TRUE(fake_upstream_connection->close());
ASSERT_TRUE(fake_upstream_connection->waitForDisconnect());
tcp_client->close();
EXPECT_EQ(test_server_->counter("cluster.cluster_0.upstream_cx_protocol_error")->value(), 1);
}
TEST_P(IntegrationTest, TestServerAllowChunkedLength) {
config_helper_.addConfigModifier(
[&](envoy::extensions::filters::network::http_connection_manager::v3::HttpConnectionManager&
hcm) -> void {
hcm.mutable_http_protocol_options()->set_allow_chunked_length(true);
});
initialize();
auto tcp_client = makeTcpConnection(lookupPort("http"));
ASSERT_TRUE(tcp_client->write("POST / HTTP/1.1\r\n"
"Host: host\r\n"
"Content-length: 100\r\n"
"Transfer-Encoding: chunked\r\n\r\n"
"4\r\nbody\r\n"
"0\r\n\r\n"));
FakeRawConnectionPtr fake_upstream_connection;
ASSERT_TRUE(fake_upstreams_[0]->waitForRawConnection(fake_upstream_connection));
std::string data;
ASSERT_TRUE(fake_upstream_connection->waitForData(
FakeRawConnection::waitForInexactMatch("\r\n\r\n"), &data));
ASSERT_THAT(data, StartsWith("POST / HTTP/1.1"));
ASSERT_THAT(data, HasSubstr("transfer-encoding: chunked"));
// verify no 'content-length' header
ASSERT_THAT(data, Not(HasSubstr("ontent-length")));
ASSERT_TRUE(
fake_upstream_connection->write("HTTP/1.1 200 OK\r\nTransfer-encoding: chunked\r\n\r\n"));
ASSERT_TRUE(fake_upstream_connection->close());
ASSERT_TRUE(fake_upstream_connection->waitForDisconnect());
tcp_client->close();
}
TEST_P(IntegrationTest, TestClientAllowChunkedLength) {
#ifdef ENVOY_ENABLE_UHV
// TODO(#23289) - uniform handling of Transfer-Encoding validation between codec and UHV
return;
#endif
config_helper_.addConfigModifier([&](envoy::config::bootstrap::v3::Bootstrap& bootstrap) -> void {
RELEASE_ASSERT(bootstrap.mutable_static_resources()->clusters_size() == 1, "");
if (fake_upstreams_[0]->httpType() == Http::CodecType::HTTP1) {
ConfigHelper::HttpProtocolOptions protocol_options;
protocol_options.mutable_explicit_http_config()
->mutable_http_protocol_options()
->set_allow_chunked_length(true);