-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathcodec_impl_test.cc
2426 lines (2091 loc) · 109 KB
/
codec_impl_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 <cstdint>
#include <string>
#include "envoy/http/codec.h"
#include "envoy/stats/scope.h"
#include "common/http/exception.h"
#include "common/http/header_map_impl.h"
#include "common/http/http2/codec_impl.h"
#include "common/runtime/runtime_features.h"
#include "test/common/http/common.h"
#include "test/common/http/http2/http2_frame.h"
#include "test/common/stats/stat_test_utility.h"
#include "test/mocks/http/mocks.h"
#include "test/mocks/init/mocks.h"
#include "test/mocks/local_info/mocks.h"
#include "test/mocks/network/mocks.h"
#include "test/mocks/protobuf/mocks.h"
#include "test/mocks/thread_local/mocks.h"
#include "test/test_common/logging.h"
#include "test/test_common/printers.h"
#include "test/test_common/registry.h"
#include "test/test_common/test_runtime.h"
#include "test/test_common/utility.h"
#include "codec_impl_test_util.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
using testing::_;
using testing::AnyNumber;
using testing::AtLeast;
using testing::InSequence;
using testing::Invoke;
using testing::InvokeWithoutArgs;
using testing::NiceMock;
using testing::Return;
namespace Envoy {
namespace Http {
namespace Http2 {
using Http2SettingsTuple = ::testing::tuple<uint32_t, uint32_t, uint32_t, uint32_t>;
using Http2SettingsTestParam = ::testing::tuple<Http2SettingsTuple, Http2SettingsTuple>;
namespace CommonUtility = ::Envoy::Http2::Utility;
class Http2CodecImplTestFixture {
public:
// The Http::Connection::dispatch method does not throw (any more). However unit tests in this
// file use codecs for sending test data through mock network connections to the codec under test.
// It is infeasible to plumb error codes returned by the dispatch() method of the codecs under
// test, through mock connections and sending codec. As a result error returned by the dispatch
// method of the codec under test invoked by the ConnectionWrapper is thrown as an exception. Note
// that exception goes only through the mock network connection and sending codec, i.e. it is
// thrown only through the test harness code. Specific exception types are to distinguish error
// codes returned when processing requests or responses.
// TODO(yanavlasov): modify the code to verify test expectations at the point of calling codec
// under test through the ON_CALL expectations in the
// setupDefaultConnectionMocks() method. This will make the exceptions below
// unnecessary.
struct ClientCodecError : public std::runtime_error {
ClientCodecError(Http::Status&& status)
: std::runtime_error(std::string(status.message())), status_(std::move(status)) {}
const char* what() const noexcept override { return status_.message().data(); }
const Http::Status status_;
};
struct ServerCodecError : public std::runtime_error {
ServerCodecError(Http::Status&& status)
: std::runtime_error(std::string(status.message())), status_(std::move(status)) {}
const char* what() const noexcept override { return status_.message().data(); }
const Http::Status status_;
};
struct ConnectionWrapper {
Http::Status dispatch(const Buffer::Instance& data, Connection& connection) {
Http::Status status = Http::okStatus();
buffer_.add(data);
if (!dispatching_) {
while (buffer_.length() > 0) {
dispatching_ = true;
status = connection.dispatch(buffer_);
if (!status.ok()) {
// Exit early if we hit an error status.
return status;
}
dispatching_ = false;
}
}
return status;
}
bool dispatching_{};
Buffer::OwnedImpl buffer_;
};
enum SettingsTupleIndex {
HpackTableSize = 0,
MaxConcurrentStreams,
InitialStreamWindowSize,
InitialConnectionWindowSize
};
Http2CodecImplTestFixture() = default;
Http2CodecImplTestFixture(Http2SettingsTuple client_settings, Http2SettingsTuple server_settings)
: client_settings_(client_settings), server_settings_(server_settings) {
// Make sure we explicitly test for stream flush timer creation.
EXPECT_CALL(client_connection_.dispatcher_, createTimer_(_)).Times(0);
EXPECT_CALL(server_connection_.dispatcher_, createTimer_(_)).Times(0);
}
virtual ~Http2CodecImplTestFixture() {
client_connection_.dispatcher_.clearDeferredDeleteList();
if (client_ != nullptr) {
client_.reset();
EXPECT_EQ(0, TestUtility::findGauge(client_stats_store_, "http2.streams_active")->value());
EXPECT_EQ(0,
TestUtility::findGauge(client_stats_store_, "http2.pending_send_bytes")->value());
}
server_connection_.dispatcher_.clearDeferredDeleteList();
if (server_ != nullptr) {
server_.reset();
EXPECT_EQ(0, TestUtility::findGauge(server_stats_store_, "http2.streams_active")->value());
EXPECT_EQ(0,
TestUtility::findGauge(server_stats_store_, "http2.pending_send_bytes")->value());
}
}
virtual void initialize() {
http2OptionsFromTuple(client_http2_options_, client_settings_);
http2OptionsFromTuple(server_http2_options_, server_settings_);
if (Runtime::runtimeFeatureEnabled("envoy.reloadable_features.new_codec_behavior")) {
client_ = std::make_unique<TestClientConnectionImplNew>(
client_connection_, client_callbacks_, client_stats_store_, client_http2_options_,
max_request_headers_kb_, max_response_headers_count_,
ProdNghttp2SessionFactoryNew::get());
server_ = std::make_unique<TestServerConnectionImplNew>(
server_connection_, server_callbacks_, server_stats_store_, server_http2_options_,
max_request_headers_kb_, max_request_headers_count_, headers_with_underscores_action_);
} else {
client_ = std::make_unique<TestClientConnectionImplLegacy>(
client_connection_, client_callbacks_, client_stats_store_, client_http2_options_,
max_request_headers_kb_, max_response_headers_count_,
ProdNghttp2SessionFactoryLegacy::get());
server_ = std::make_unique<TestServerConnectionImplLegacy>(
server_connection_, server_callbacks_, server_stats_store_, server_http2_options_,
max_request_headers_kb_, max_request_headers_count_, headers_with_underscores_action_);
}
request_encoder_ = &client_->newStream(response_decoder_);
setupDefaultConnectionMocks();
EXPECT_CALL(server_callbacks_, newStream(_, _))
.WillRepeatedly(Invoke([&](ResponseEncoder& encoder, bool) -> RequestDecoder& {
response_encoder_ = &encoder;
encoder.getStream().addCallbacks(server_stream_callbacks_);
encoder.getStream().setFlushTimeout(std::chrono::milliseconds(30000));
return request_decoder_;
}));
}
void setupDefaultConnectionMocks() {
ON_CALL(client_connection_, write(_, _))
.WillByDefault(Invoke([&](Buffer::Instance& data, bool) -> void {
if (corrupt_metadata_frame_) {
corruptMetadataFramePayload(data);
}
auto status = server_wrapper_.dispatch(data, *server_);
if (!status.ok()) {
throw ServerCodecError(std::move(status));
}
}));
ON_CALL(server_connection_, write(_, _))
.WillByDefault(Invoke([&](Buffer::Instance& data, bool) -> void {
auto status = client_wrapper_.dispatch(data, *client_);
if (!status.ok()) {
throw ClientCodecError(std::move(status));
}
}));
}
void http2OptionsFromTuple(envoy::config::core::v3::Http2ProtocolOptions& options,
const absl::optional<const Http2SettingsTuple>& tp) {
options.mutable_hpack_table_size()->set_value(
(tp.has_value()) ? ::testing::get<SettingsTupleIndex::HpackTableSize>(*tp)
: CommonUtility::OptionsLimits::DEFAULT_HPACK_TABLE_SIZE);
options.mutable_max_concurrent_streams()->set_value(
(tp.has_value()) ? ::testing::get<SettingsTupleIndex::MaxConcurrentStreams>(*tp)
: CommonUtility::OptionsLimits::DEFAULT_MAX_CONCURRENT_STREAMS);
options.mutable_initial_stream_window_size()->set_value(
(tp.has_value()) ? ::testing::get<SettingsTupleIndex::InitialStreamWindowSize>(*tp)
: CommonUtility::OptionsLimits::DEFAULT_INITIAL_STREAM_WINDOW_SIZE);
options.mutable_initial_connection_window_size()->set_value(
(tp.has_value()) ? ::testing::get<SettingsTupleIndex::InitialConnectionWindowSize>(*tp)
: CommonUtility::OptionsLimits::DEFAULT_INITIAL_CONNECTION_WINDOW_SIZE);
options.set_allow_metadata(allow_metadata_);
options.mutable_override_stream_error_on_invalid_http_message()->set_value(
stream_error_on_invalid_http_messaging_);
options.mutable_max_outbound_frames()->set_value(max_outbound_frames_);
options.mutable_max_outbound_control_frames()->set_value(max_outbound_control_frames_);
options.mutable_max_consecutive_inbound_frames_with_empty_payload()->set_value(
max_consecutive_inbound_frames_with_empty_payload_);
options.mutable_max_inbound_priority_frames_per_stream()->set_value(
max_inbound_priority_frames_per_stream_);
options.mutable_max_inbound_window_update_frames_per_data_frame_sent()->set_value(
max_inbound_window_update_frames_per_data_frame_sent_);
}
// corruptMetadataFramePayload assumes data contains at least 10 bytes of the beginning of a
// frame.
void corruptMetadataFramePayload(Buffer::Instance& data) {
const size_t length = data.length();
const size_t corrupt_start = 10;
if (length < corrupt_start || length > METADATA_MAX_PAYLOAD_SIZE) {
ENVOY_LOG_MISC(error, "data size too big or too small");
return;
}
corruptAtOffset(data, corrupt_start, 0xff);
}
void corruptAtOffset(Buffer::Instance& data, size_t index, char new_value) {
if (data.length() == 0) {
return;
}
reinterpret_cast<uint8_t*>(data.linearize(data.length()))[index % data.length()] = new_value;
}
void expectDetailsRequest(const absl::string_view details) {
EXPECT_EQ(details, request_encoder_->getStream().responseDetails());
}
void expectDetailsResponse(const absl::string_view details) {
EXPECT_EQ(details, response_encoder_->getStream().responseDetails());
}
absl::optional<const Http2SettingsTuple> client_settings_;
absl::optional<const Http2SettingsTuple> server_settings_;
bool allow_metadata_ = false;
bool stream_error_on_invalid_http_messaging_ = false;
Stats::TestUtil::TestStore client_stats_store_;
envoy::config::core::v3::Http2ProtocolOptions client_http2_options_;
NiceMock<Network::MockConnection> client_connection_;
MockConnectionCallbacks client_callbacks_;
std::unique_ptr<TestClientConnection> client_;
ConnectionWrapper client_wrapper_;
Stats::TestUtil::TestStore server_stats_store_;
envoy::config::core::v3::Http2ProtocolOptions server_http2_options_;
NiceMock<Network::MockConnection> server_connection_;
MockServerConnectionCallbacks server_callbacks_;
std::unique_ptr<TestServerConnection> server_;
ConnectionWrapper server_wrapper_;
MockResponseDecoder response_decoder_;
RequestEncoder* request_encoder_;
MockRequestDecoder request_decoder_;
ResponseEncoder* response_encoder_{};
MockStreamCallbacks server_stream_callbacks_;
// Corrupt a metadata frame payload.
bool corrupt_metadata_frame_ = false;
uint32_t max_request_headers_kb_ = Http::DEFAULT_MAX_REQUEST_HEADERS_KB;
uint32_t max_request_headers_count_ = Http::DEFAULT_MAX_HEADERS_COUNT;
uint32_t max_response_headers_count_ = Http::DEFAULT_MAX_HEADERS_COUNT;
uint32_t max_outbound_frames_ = CommonUtility::OptionsLimits::DEFAULT_MAX_OUTBOUND_FRAMES;
uint32_t max_outbound_control_frames_ =
CommonUtility::OptionsLimits::DEFAULT_MAX_OUTBOUND_CONTROL_FRAMES;
uint32_t max_consecutive_inbound_frames_with_empty_payload_ =
CommonUtility::OptionsLimits::DEFAULT_MAX_CONSECUTIVE_INBOUND_FRAMES_WITH_EMPTY_PAYLOAD;
uint32_t max_inbound_priority_frames_per_stream_ =
CommonUtility::OptionsLimits::DEFAULT_MAX_INBOUND_PRIORITY_FRAMES_PER_STREAM;
uint32_t max_inbound_window_update_frames_per_data_frame_sent_ =
CommonUtility::OptionsLimits::DEFAULT_MAX_INBOUND_WINDOW_UPDATE_FRAMES_PER_DATA_FRAME_SENT;
envoy::config::core::v3::HttpProtocolOptions::HeadersWithUnderscoresAction
headers_with_underscores_action_{envoy::config::core::v3::HttpProtocolOptions::ALLOW};
};
class Http2CodecImplTest : public ::testing::TestWithParam<Http2SettingsTestParam>,
protected Http2CodecImplTestFixture {
public:
Http2CodecImplTest()
: Http2CodecImplTestFixture(::testing::get<0>(GetParam()), ::testing::get<1>(GetParam())) {}
protected:
void priorityFlood() {
initialize();
TestRequestHeaderMapImpl request_headers;
HttpTestUtility::addDefaultHeaders(request_headers, "POST");
EXPECT_CALL(request_decoder_, decodeHeaders_(_, false));
request_encoder_->encodeHeaders(request_headers, false);
nghttp2_priority_spec spec = {0, 10, 0};
// HTTP/2 codec adds 1 to the number of active streams when computing PRIORITY frames limit
constexpr uint32_t max_allowed =
2 * CommonUtility::OptionsLimits::DEFAULT_MAX_INBOUND_PRIORITY_FRAMES_PER_STREAM;
for (uint32_t i = 0; i < max_allowed + 1; ++i) {
EXPECT_EQ(0, nghttp2_submit_priority(client_->session(), NGHTTP2_FLAG_NONE, 1, &spec));
}
}
void windowUpdateFlood() {
initialize();
TestRequestHeaderMapImpl request_headers;
HttpTestUtility::addDefaultHeaders(request_headers);
EXPECT_CALL(request_decoder_, decodeHeaders_(_, true));
request_encoder_->encodeHeaders(request_headers, true);
// Send one DATA frame back
EXPECT_CALL(response_decoder_, decodeHeaders_(_, false));
EXPECT_CALL(response_decoder_, decodeData(_, false));
TestResponseHeaderMapImpl response_headers{{":status", "200"}};
response_encoder_->encodeHeaders(response_headers, false);
Buffer::OwnedImpl data("0");
EXPECT_NO_THROW(response_encoder_->encodeData(data, false));
// See the limit formula in the
// `Envoy::Http::Http2::ServerConnectionImpl::checkInboundFrameLimits()' method.
constexpr uint32_t max_allowed =
1 + 2 * (CommonUtility::OptionsLimits::
DEFAULT_MAX_INBOUND_WINDOW_UPDATE_FRAMES_PER_DATA_FRAME_SENT +
1);
for (uint32_t i = 0; i < max_allowed + 1; ++i) {
EXPECT_EQ(0, nghttp2_submit_window_update(client_->session(), NGHTTP2_FLAG_NONE, 1, 1));
}
}
void emptyDataFlood(Buffer::OwnedImpl& data) {
initialize();
TestRequestHeaderMapImpl request_headers;
HttpTestUtility::addDefaultHeaders(request_headers, "POST");
EXPECT_CALL(request_decoder_, decodeHeaders_(_, false));
request_encoder_->encodeHeaders(request_headers, false);
// HTTP/2 codec does not send empty DATA frames with no END_STREAM flag.
// To make this work, send raw bytes representing empty DATA frames bypassing client codec.
Http2Frame emptyDataFrame = Http2Frame::makeEmptyDataFrame(0);
constexpr uint32_t max_allowed =
CommonUtility::OptionsLimits::DEFAULT_MAX_CONSECUTIVE_INBOUND_FRAMES_WITH_EMPTY_PAYLOAD;
for (uint32_t i = 0; i < max_allowed + 1; ++i) {
data.add(emptyDataFrame.data(), emptyDataFrame.size());
}
}
};
TEST_P(Http2CodecImplTest, ShutdownNotice) {
initialize();
EXPECT_EQ(absl::nullopt, request_encoder_->http1StreamEncoderOptions());
TestRequestHeaderMapImpl request_headers;
HttpTestUtility::addDefaultHeaders(request_headers);
EXPECT_CALL(request_decoder_, decodeHeaders_(_, true));
request_encoder_->encodeHeaders(request_headers, true);
EXPECT_CALL(client_callbacks_, onGoAway(_));
server_->shutdownNotice();
server_->goAway();
TestResponseHeaderMapImpl response_headers{{":status", "200"}};
EXPECT_CALL(response_decoder_, decodeHeaders_(_, true));
response_encoder_->encodeHeaders(response_headers, true);
}
// 100 response followed by 200 results in a [decode100ContinueHeaders, decodeHeaders] sequence.
TEST_P(Http2CodecImplTest, ContinueHeaders) {
initialize();
TestRequestHeaderMapImpl request_headers;
HttpTestUtility::addDefaultHeaders(request_headers);
EXPECT_CALL(request_decoder_, decodeHeaders_(_, true));
request_encoder_->encodeHeaders(request_headers, true);
TestResponseHeaderMapImpl continue_headers{{":status", "100"}};
EXPECT_CALL(response_decoder_, decode100ContinueHeaders_(_));
response_encoder_->encode100ContinueHeaders(continue_headers);
TestResponseHeaderMapImpl response_headers{{":status", "200"}};
EXPECT_CALL(response_decoder_, decodeHeaders_(_, true));
response_encoder_->encodeHeaders(response_headers, true);
};
// nghttp2 rejects trailers with :status.
TEST_P(Http2CodecImplTest, TrailerStatus) {
initialize();
TestRequestHeaderMapImpl request_headers;
HttpTestUtility::addDefaultHeaders(request_headers);
EXPECT_CALL(request_decoder_, decodeHeaders_(_, true));
request_encoder_->encodeHeaders(request_headers, true);
TestResponseHeaderMapImpl continue_headers{{":status", "100"}};
EXPECT_CALL(response_decoder_, decode100ContinueHeaders_(_));
response_encoder_->encode100ContinueHeaders(continue_headers);
TestResponseHeaderMapImpl response_headers{{":status", "200"}};
EXPECT_CALL(response_decoder_, decodeHeaders_(_, false));
response_encoder_->encodeHeaders(response_headers, false);
// nghttp2 doesn't allow :status in trailers
EXPECT_THROW(response_encoder_->encode100ContinueHeaders(continue_headers), ClientCodecError);
EXPECT_EQ(1, client_stats_store_.counter("http2.rx_messaging_error").value());
};
// Multiple 100 responses are passed to the response encoder (who is responsible for coalescing).
TEST_P(Http2CodecImplTest, MultipleContinueHeaders) {
initialize();
TestRequestHeaderMapImpl request_headers;
HttpTestUtility::addDefaultHeaders(request_headers);
EXPECT_CALL(request_decoder_, decodeHeaders_(_, true));
request_encoder_->encodeHeaders(request_headers, true);
TestResponseHeaderMapImpl continue_headers{{":status", "100"}};
EXPECT_CALL(response_decoder_, decode100ContinueHeaders_(_));
response_encoder_->encode100ContinueHeaders(continue_headers);
EXPECT_CALL(response_decoder_, decode100ContinueHeaders_(_));
response_encoder_->encode100ContinueHeaders(continue_headers);
TestResponseHeaderMapImpl response_headers{{":status", "200"}};
EXPECT_CALL(response_decoder_, decodeHeaders_(_, true));
response_encoder_->encodeHeaders(response_headers, true);
};
// 101/102 headers etc. are passed to the response encoder (who is responsibly for deciding to
// upgrade, ignore, etc.).
TEST_P(Http2CodecImplTest, 1xxNonContinueHeaders) {
initialize();
TestRequestHeaderMapImpl request_headers;
HttpTestUtility::addDefaultHeaders(request_headers);
EXPECT_CALL(request_decoder_, decodeHeaders_(_, true));
request_encoder_->encodeHeaders(request_headers, true);
TestResponseHeaderMapImpl other_headers{{":status", "102"}};
EXPECT_CALL(response_decoder_, decodeHeaders_(_, false));
response_encoder_->encodeHeaders(other_headers, false);
};
// nghttp2 treats 101 inside an HTTP/2 stream as an invalid HTTP header field.
TEST_P(Http2CodecImplTest, Invalid101SwitchingProtocols) {
initialize();
TestRequestHeaderMapImpl request_headers;
HttpTestUtility::addDefaultHeaders(request_headers);
EXPECT_CALL(request_decoder_, decodeHeaders_(_, true));
request_encoder_->encodeHeaders(request_headers, true);
TestResponseHeaderMapImpl upgrade_headers{{":status", "101"}};
EXPECT_CALL(response_decoder_, decodeHeaders_(_, _)).Times(0);
EXPECT_THROW(response_encoder_->encodeHeaders(upgrade_headers, false), ClientCodecError);
EXPECT_EQ(1, client_stats_store_.counter("http2.rx_messaging_error").value());
}
TEST_P(Http2CodecImplTest, InvalidContinueWithFin) {
initialize();
TestRequestHeaderMapImpl request_headers;
HttpTestUtility::addDefaultHeaders(request_headers);
EXPECT_CALL(request_decoder_, decodeHeaders_(_, true));
request_encoder_->encodeHeaders(request_headers, true);
TestResponseHeaderMapImpl continue_headers{{":status", "100"}};
EXPECT_THROW(response_encoder_->encodeHeaders(continue_headers, true), ClientCodecError);
EXPECT_EQ(1, client_stats_store_.counter("http2.rx_messaging_error").value());
}
TEST_P(Http2CodecImplTest, InvalidContinueWithFinAllowed) {
stream_error_on_invalid_http_messaging_ = true;
initialize();
MockStreamCallbacks request_callbacks;
request_encoder_->getStream().addCallbacks(request_callbacks);
TestRequestHeaderMapImpl request_headers;
HttpTestUtility::addDefaultHeaders(request_headers);
EXPECT_CALL(request_decoder_, decodeHeaders_(_, true));
request_encoder_->encodeHeaders(request_headers, true);
// Buffer client data to avoid mock recursion causing lifetime issues.
ON_CALL(server_connection_, write(_, _))
.WillByDefault(
Invoke([&](Buffer::Instance& data, bool) -> void { client_wrapper_.buffer_.add(data); }));
TestResponseHeaderMapImpl continue_headers{{":status", "100"}};
response_encoder_->encodeHeaders(continue_headers, true);
// Flush pending data.
EXPECT_CALL(request_callbacks, onResetStream(StreamResetReason::LocalReset, _));
setupDefaultConnectionMocks();
auto status = client_wrapper_.dispatch(Buffer::OwnedImpl(), *client_);
EXPECT_TRUE(status.ok());
EXPECT_EQ(1, client_stats_store_.counter("http2.rx_messaging_error").value());
expectDetailsRequest("http2.violation.of.messaging.rule");
}
TEST_P(Http2CodecImplTest, CodecHasCorrectStreamErrorIfFalse) {
initialize();
TestRequestHeaderMapImpl request_headers;
HttpTestUtility::addDefaultHeaders(request_headers);
EXPECT_CALL(request_decoder_, decodeHeaders_(_, true));
request_encoder_->encodeHeaders(request_headers, true);
EXPECT_FALSE(response_encoder_->streamErrorOnInvalidHttpMessage());
}
TEST_P(Http2CodecImplTest, CodecHasCorrectStreamErrorIfTrue) {
stream_error_on_invalid_http_messaging_ = true;
initialize();
TestRequestHeaderMapImpl request_headers;
HttpTestUtility::addDefaultHeaders(request_headers);
EXPECT_CALL(request_decoder_, decodeHeaders_(_, true));
request_encoder_->encodeHeaders(request_headers, true);
EXPECT_TRUE(response_encoder_->streamErrorOnInvalidHttpMessage());
}
TEST_P(Http2CodecImplTest, InvalidRepeatContinue) {
initialize();
TestRequestHeaderMapImpl request_headers;
HttpTestUtility::addDefaultHeaders(request_headers);
EXPECT_CALL(request_decoder_, decodeHeaders_(_, true));
request_encoder_->encodeHeaders(request_headers, true);
TestResponseHeaderMapImpl continue_headers{{":status", "100"}};
EXPECT_CALL(response_decoder_, decode100ContinueHeaders_(_));
response_encoder_->encode100ContinueHeaders(continue_headers);
EXPECT_THROW(response_encoder_->encodeHeaders(continue_headers, true), ClientCodecError);
EXPECT_EQ(1, client_stats_store_.counter("http2.rx_messaging_error").value());
};
TEST_P(Http2CodecImplTest, InvalidRepeatContinueAllowed) {
stream_error_on_invalid_http_messaging_ = true;
initialize();
MockStreamCallbacks request_callbacks;
request_encoder_->getStream().addCallbacks(request_callbacks);
TestRequestHeaderMapImpl request_headers;
HttpTestUtility::addDefaultHeaders(request_headers);
EXPECT_CALL(request_decoder_, decodeHeaders_(_, true));
request_encoder_->encodeHeaders(request_headers, true);
TestResponseHeaderMapImpl continue_headers{{":status", "100"}};
EXPECT_CALL(response_decoder_, decode100ContinueHeaders_(_));
response_encoder_->encode100ContinueHeaders(continue_headers);
// Buffer client data to avoid mock recursion causing lifetime issues.
ON_CALL(server_connection_, write(_, _))
.WillByDefault(
Invoke([&](Buffer::Instance& data, bool) -> void { client_wrapper_.buffer_.add(data); }));
response_encoder_->encodeHeaders(continue_headers, true);
// Flush pending data.
EXPECT_CALL(request_callbacks, onResetStream(StreamResetReason::LocalReset, _));
setupDefaultConnectionMocks();
auto status = client_wrapper_.dispatch(Buffer::OwnedImpl(), *client_);
EXPECT_TRUE(status.ok());
EXPECT_EQ(1, client_stats_store_.counter("http2.rx_messaging_error").value());
expectDetailsRequest("http2.violation.of.messaging.rule");
};
TEST_P(Http2CodecImplTest, Invalid204WithContentLength) {
initialize();
TestRequestHeaderMapImpl request_headers;
HttpTestUtility::addDefaultHeaders(request_headers);
EXPECT_CALL(request_decoder_, decodeHeaders_(_, true));
request_encoder_->encodeHeaders(request_headers, true);
TestResponseHeaderMapImpl response_headers{{":status", "204"}, {"content-length", "3"}};
// What follows is a hack to get headers that should span into continuation frames. The default
// maximum frame size is 16K. We will add 3,000 headers that will take us above this size and
// not easily compress with HPACK. (I confirmed this generates 26,468 bytes of header data
// which should contain a continuation.)
for (unsigned i = 1; i < 3000; i++) {
response_headers.addCopy(std::to_string(i), std::to_string(i));
}
EXPECT_LOG_CONTAINS(
"debug",
"Invalid HTTP header field was received: frame type: 1, stream: 1, name: [content-length], "
"value: [3]",
EXPECT_THROW(response_encoder_->encodeHeaders(response_headers, false), ClientCodecError));
EXPECT_EQ(1, client_stats_store_.counter("http2.rx_messaging_error").value());
};
TEST_P(Http2CodecImplTest, Invalid204WithContentLengthAllowed) {
stream_error_on_invalid_http_messaging_ = true;
initialize();
MockStreamCallbacks request_callbacks;
request_encoder_->getStream().addCallbacks(request_callbacks);
TestRequestHeaderMapImpl request_headers;
HttpTestUtility::addDefaultHeaders(request_headers);
EXPECT_CALL(request_decoder_, decodeHeaders_(_, true));
request_encoder_->encodeHeaders(request_headers, true);
// Buffer client data to avoid mock recursion causing lifetime issues.
ON_CALL(server_connection_, write(_, _))
.WillByDefault(
Invoke([&](Buffer::Instance& data, bool) -> void { client_wrapper_.buffer_.add(data); }));
TestResponseHeaderMapImpl response_headers{{":status", "204"}, {"content-length", "3"}};
// What follows is a hack to get headers that should span into continuation frames. The default
// maximum frame size is 16K. We will add 3,000 headers that will take us above this size and
// not easily compress with HPACK. (I confirmed this generates 26,468 bytes of header data
// which should contain a continuation.)
for (int i = 1; i < 3000; i++) {
response_headers.addCopy(std::to_string(i), std::to_string(i));
}
response_encoder_->encodeHeaders(response_headers, false);
// Flush pending data.
EXPECT_CALL(request_callbacks, onResetStream(StreamResetReason::LocalReset, _));
EXPECT_CALL(server_stream_callbacks_, onResetStream(StreamResetReason::RemoteReset, _));
setupDefaultConnectionMocks();
auto status = client_wrapper_.dispatch(Buffer::OwnedImpl(), *client_);
EXPECT_TRUE(status.ok());
EXPECT_EQ(1, client_stats_store_.counter("http2.rx_messaging_error").value());
expectDetailsRequest("http2.invalid.header.field");
};
TEST_P(Http2CodecImplTest, RefusedStreamReset) {
initialize();
TestRequestHeaderMapImpl request_headers;
HttpTestUtility::addDefaultHeaders(request_headers);
EXPECT_CALL(request_decoder_, decodeHeaders_(_, false));
request_encoder_->encodeHeaders(request_headers, false);
MockStreamCallbacks callbacks;
request_encoder_->getStream().addCallbacks(callbacks);
EXPECT_CALL(server_stream_callbacks_,
onResetStream(StreamResetReason::LocalRefusedStreamReset, _));
EXPECT_CALL(callbacks, onResetStream(StreamResetReason::RemoteRefusedStreamReset, _));
response_encoder_->getStream().resetStream(StreamResetReason::LocalRefusedStreamReset);
}
TEST_P(Http2CodecImplTest, InvalidHeadersFrame) {
initialize();
EXPECT_THROW(request_encoder_->encodeHeaders(TestRequestHeaderMapImpl{}, true), ServerCodecError);
EXPECT_EQ(1, server_stats_store_.counter("http2.rx_messaging_error").value());
}
TEST_P(Http2CodecImplTest, InvalidHeadersFrameAllowed) {
stream_error_on_invalid_http_messaging_ = true;
initialize();
MockStreamCallbacks request_callbacks;
request_encoder_->getStream().addCallbacks(request_callbacks);
ON_CALL(client_connection_, write(_, _))
.WillByDefault(
Invoke([&](Buffer::Instance& data, bool) -> void { server_wrapper_.buffer_.add(data); }));
request_encoder_->encodeHeaders(TestRequestHeaderMapImpl{}, true);
EXPECT_CALL(server_stream_callbacks_, onResetStream(StreamResetReason::LocalReset, _));
EXPECT_CALL(request_callbacks, onResetStream(StreamResetReason::RemoteReset, _));
auto status = server_wrapper_.dispatch(Buffer::OwnedImpl(), *server_);
EXPECT_TRUE(status.ok());
expectDetailsResponse("http2.violation.of.messaging.rule");
}
TEST_P(Http2CodecImplTest, TrailingHeaders) {
initialize();
TestRequestHeaderMapImpl request_headers;
HttpTestUtility::addDefaultHeaders(request_headers);
EXPECT_CALL(request_decoder_, decodeHeaders_(_, false));
request_encoder_->encodeHeaders(request_headers, false);
EXPECT_CALL(request_decoder_, decodeData(_, false));
Buffer::OwnedImpl hello("hello");
request_encoder_->encodeData(hello, false);
EXPECT_CALL(request_decoder_, decodeTrailers_(_));
request_encoder_->encodeTrailers(TestRequestTrailerMapImpl{{"trailing", "header"}});
TestResponseHeaderMapImpl response_headers{{":status", "200"}};
EXPECT_CALL(response_decoder_, decodeHeaders_(_, false));
response_encoder_->encodeHeaders(response_headers, false);
EXPECT_CALL(response_decoder_, decodeData(_, false));
Buffer::OwnedImpl world("world");
response_encoder_->encodeData(world, false);
EXPECT_CALL(response_decoder_, decodeTrailers_(_));
response_encoder_->encodeTrailers(TestResponseTrailerMapImpl{{"trailing", "header"}});
}
// When having empty trailers, codec submits empty buffer and end_stream instead.
TEST_P(Http2CodecImplTest, IgnoreTrailingEmptyHeaders) {
TestScopedRuntime scoped_runtime;
Runtime::LoaderSingleton::getExisting()->mergeValues(
{{"envoy.reloadable_features.http2_skip_encoding_empty_trailers", "true"}});
initialize();
Buffer::OwnedImpl empty_buffer;
TestRequestHeaderMapImpl request_headers;
HttpTestUtility::addDefaultHeaders(request_headers);
EXPECT_CALL(request_decoder_, decodeHeaders_(_, false));
request_encoder_->encodeHeaders(request_headers, false);
EXPECT_CALL(request_decoder_, decodeData(_, false));
Buffer::OwnedImpl hello("hello");
request_encoder_->encodeData(hello, false);
EXPECT_CALL(request_decoder_, decodeData(BufferEqual(&empty_buffer), true));
request_encoder_->encodeTrailers(TestRequestTrailerMapImpl{});
TestResponseHeaderMapImpl response_headers{{":status", "200"}};
EXPECT_CALL(response_decoder_, decodeHeaders_(_, false));
response_encoder_->encodeHeaders(response_headers, false);
EXPECT_CALL(response_decoder_, decodeData(_, false));
Buffer::OwnedImpl world("world");
response_encoder_->encodeData(world, false);
EXPECT_CALL(response_decoder_, decodeData(BufferEqual(&empty_buffer), true));
response_encoder_->encodeTrailers(TestResponseTrailerMapImpl{});
}
// When having empty trailers and "envoy.reloadable_features.http2_skip_encoding_empty_trailers" is
// turned off, codec submits empty trailers.
TEST_P(Http2CodecImplTest, SubmitTrailingEmptyHeaders) {
TestScopedRuntime scoped_runtime;
Runtime::LoaderSingleton::getExisting()->mergeValues(
{{"envoy.reloadable_features.http2_skip_encoding_empty_trailers", "false"}});
initialize();
TestRequestHeaderMapImpl request_headers;
HttpTestUtility::addDefaultHeaders(request_headers);
EXPECT_CALL(request_decoder_, decodeHeaders_(_, false));
request_encoder_->encodeHeaders(request_headers, false);
EXPECT_CALL(request_decoder_, decodeData(_, false));
Buffer::OwnedImpl hello("hello");
request_encoder_->encodeData(hello, false);
EXPECT_CALL(request_decoder_, decodeTrailers_(_));
request_encoder_->encodeTrailers(TestRequestTrailerMapImpl{});
TestResponseHeaderMapImpl response_headers{{":status", "200"}};
EXPECT_CALL(response_decoder_, decodeHeaders_(_, false));
response_encoder_->encodeHeaders(response_headers, false);
EXPECT_CALL(response_decoder_, decodeData(_, false));
Buffer::OwnedImpl world("world");
response_encoder_->encodeData(world, false);
EXPECT_CALL(response_decoder_, decodeTrailers_(_));
response_encoder_->encodeTrailers(TestResponseTrailerMapImpl{});
}
TEST_P(Http2CodecImplTest, TrailingHeadersLargeClientBody) {
initialize();
// Buffer server data so we can make sure we don't get any window updates.
ON_CALL(client_connection_, write(_, _))
.WillByDefault(
Invoke([&](Buffer::Instance& data, bool) -> void { server_wrapper_.buffer_.add(data); }));
TestRequestHeaderMapImpl request_headers;
HttpTestUtility::addDefaultHeaders(request_headers);
EXPECT_CALL(request_decoder_, decodeHeaders_(_, false));
request_encoder_->encodeHeaders(request_headers, false);
EXPECT_CALL(request_decoder_, decodeData(_, false)).Times(AtLeast(1));
Buffer::OwnedImpl body(std::string(1024 * 1024, 'a'));
request_encoder_->encodeData(body, false);
request_encoder_->encodeTrailers(TestRequestTrailerMapImpl{{"trailing", "header"}});
// Flush pending data.
setupDefaultConnectionMocks();
EXPECT_CALL(request_decoder_, decodeTrailers_(_));
auto status = server_wrapper_.dispatch(Buffer::OwnedImpl(), *server_);
EXPECT_TRUE(status.ok());
TestResponseHeaderMapImpl response_headers{{":status", "200"}};
EXPECT_CALL(response_decoder_, decodeHeaders_(_, false));
response_encoder_->encodeHeaders(response_headers, false);
EXPECT_CALL(response_decoder_, decodeData(_, false));
Buffer::OwnedImpl world("world");
response_encoder_->encodeData(world, false);
EXPECT_CALL(response_decoder_, decodeTrailers_(_));
response_encoder_->encodeTrailers(TestResponseTrailerMapImpl{{"trailing", "header"}});
}
TEST_P(Http2CodecImplTest, SmallMetadataVecTest) {
allow_metadata_ = true;
initialize();
// Generates a valid stream_id by sending a request header.
TestRequestHeaderMapImpl request_headers;
HttpTestUtility::addDefaultHeaders(request_headers);
EXPECT_CALL(request_decoder_, decodeHeaders_(_, true));
request_encoder_->encodeHeaders(request_headers, true);
MetadataMapVector metadata_map_vector;
const int size = 10;
for (int i = 0; i < size; i++) {
MetadataMap metadata_map = {
{"header_key1", "header_value1"},
{"header_key2", "header_value2"},
{"header_key3", "header_value3"},
{"header_key4", "header_value4"},
};
MetadataMapPtr metadata_map_ptr = std::make_unique<MetadataMap>(metadata_map);
metadata_map_vector.push_back(std::move(metadata_map_ptr));
}
EXPECT_CALL(request_decoder_, decodeMetadata_(_)).Times(size);
request_encoder_->encodeMetadata(metadata_map_vector);
EXPECT_CALL(response_decoder_, decodeMetadata_(_)).Times(size);
response_encoder_->encodeMetadata(metadata_map_vector);
}
TEST_P(Http2CodecImplTest, LargeMetadataVecTest) {
allow_metadata_ = true;
initialize();
// Generates a valid stream_id by sending a request header.
TestRequestHeaderMapImpl request_headers;
HttpTestUtility::addDefaultHeaders(request_headers);
EXPECT_CALL(request_decoder_, decodeHeaders_(_, true));
request_encoder_->encodeHeaders(request_headers, true);
MetadataMapVector metadata_map_vector;
const int size = 10;
for (int i = 0; i < size; i++) {
MetadataMap metadata_map = {
{"header_key1", std::string(50 * 1024, 'a')},
};
MetadataMapPtr metadata_map_ptr = std::make_unique<MetadataMap>(metadata_map);
metadata_map_vector.push_back(std::move(metadata_map_ptr));
}
EXPECT_CALL(request_decoder_, decodeMetadata_(_)).Times(size);
request_encoder_->encodeMetadata(metadata_map_vector);
EXPECT_CALL(response_decoder_, decodeMetadata_(_)).Times(size);
response_encoder_->encodeMetadata(metadata_map_vector);
}
TEST_P(Http2CodecImplTest, BadMetadataVecReceivedTest) {
allow_metadata_ = true;
initialize();
// Generates a valid stream_id by sending a request header.
TestRequestHeaderMapImpl request_headers;
HttpTestUtility::addDefaultHeaders(request_headers);
EXPECT_CALL(request_decoder_, decodeHeaders_(_, true));
request_encoder_->encodeHeaders(request_headers, true);
MetadataMap metadata_map = {
{"header_key1", "header_value1"},
{"header_key2", "header_value2"},
{"header_key3", "header_value3"},
{"header_key4", "header_value4"},
};
MetadataMapPtr metadata_map_ptr = std::make_unique<MetadataMap>(metadata_map);
MetadataMapVector metadata_map_vector;
metadata_map_vector.push_back(std::move(metadata_map_ptr));
corrupt_metadata_frame_ = true;
EXPECT_THROW_WITH_MESSAGE(request_encoder_->encodeMetadata(metadata_map_vector), ServerCodecError,
"The user callback function failed");
}
// Encode response metadata while dispatching request data from the client, so
// that nghttp2 can't fill the metadata frames' payloads until dispatching
// is finished.
TEST_P(Http2CodecImplTest, EncodeMetadataWhileDispatchingTest) {
allow_metadata_ = true;
initialize();
MetadataMapVector metadata_map_vector;
const int size = 10;
for (int i = 0; i < size; i++) {
MetadataMap metadata_map = {
{"header_key1", "header_value1"},
{"header_key2", "header_value2"},
{"header_key3", "header_value3"},
{"header_key4", "header_value4"},
};
MetadataMapPtr metadata_map_ptr = std::make_unique<MetadataMap>(metadata_map);
metadata_map_vector.push_back(std::move(metadata_map_ptr));
}
TestRequestHeaderMapImpl request_headers;
HttpTestUtility::addDefaultHeaders(request_headers);
EXPECT_CALL(request_decoder_, decodeHeaders_(_, true)).WillOnce(InvokeWithoutArgs([&]() -> void {
response_encoder_->encodeMetadata(metadata_map_vector);
}));
EXPECT_CALL(response_decoder_, decodeMetadata_(_)).Times(size);
request_encoder_->encodeHeaders(request_headers, true);
}
class Http2CodecImplDeferredResetTest : public Http2CodecImplTest {};
TEST_P(Http2CodecImplDeferredResetTest, DeferredResetClient) {
initialize();
InSequence s;
MockStreamCallbacks client_stream_callbacks;
request_encoder_->getStream().addCallbacks(client_stream_callbacks);
// Do a request, but pause server dispatch so we don't send window updates. This will result in a
// deferred reset, followed by a pending frames flush which will cause the stream to actually
// be reset immediately since we are outside of dispatch context.
ON_CALL(client_connection_, write(_, _))
.WillByDefault(
Invoke([&](Buffer::Instance& data, bool) -> void { server_wrapper_.buffer_.add(data); }));
TestRequestHeaderMapImpl request_headers;
HttpTestUtility::addDefaultHeaders(request_headers);
request_encoder_->encodeHeaders(request_headers, false);
Buffer::OwnedImpl body(std::string(1024 * 1024, 'a'));
EXPECT_CALL(client_stream_callbacks, onAboveWriteBufferHighWatermark()).Times(AnyNumber());
request_encoder_->encodeData(body, true);
EXPECT_CALL(client_stream_callbacks, onResetStream(StreamResetReason::LocalReset, _));
request_encoder_->getStream().resetStream(StreamResetReason::LocalReset);
// Dispatch server. We expect to see some data.
EXPECT_CALL(response_decoder_, decodeHeaders_(_, _)).Times(0);
EXPECT_CALL(request_decoder_, decodeHeaders_(_, false)).WillOnce(InvokeWithoutArgs([&]() -> void {
// Start a response inside the headers callback. This should not result in the client
// seeing any headers as the stream should already be reset on the other side, even though
// we don't know about it yet.
TestResponseHeaderMapImpl response_headers{{":status", "200"}};
response_encoder_->encodeHeaders(response_headers, false);
}));
EXPECT_CALL(request_decoder_, decodeData(_, false)).Times(AtLeast(1));
EXPECT_CALL(server_stream_callbacks_, onResetStream(StreamResetReason::RemoteReset, _));
setupDefaultConnectionMocks();
auto status = server_wrapper_.dispatch(Buffer::OwnedImpl(), *server_);
EXPECT_TRUE(status.ok());
}
TEST_P(Http2CodecImplDeferredResetTest, DeferredResetServer) {
initialize();
InSequence s;
TestRequestHeaderMapImpl request_headers;
HttpTestUtility::addDefaultHeaders(request_headers);
EXPECT_CALL(request_decoder_, decodeHeaders_(_, false));
request_encoder_->encodeHeaders(request_headers, false);
// In this case we do the same thing as DeferredResetClient but on the server side.
ON_CALL(server_connection_, write(_, _))
.WillByDefault(
Invoke([&](Buffer::Instance& data, bool) -> void { client_wrapper_.buffer_.add(data); }));
TestResponseHeaderMapImpl response_headers{{":status", "200"}};
response_encoder_->encodeHeaders(response_headers, false);
Buffer::OwnedImpl body(std::string(1024 * 1024, 'a'));
EXPECT_CALL(server_stream_callbacks_, onAboveWriteBufferHighWatermark()).Times(AnyNumber());
auto flush_timer = new Event::MockTimer(&server_connection_.dispatcher_);
EXPECT_CALL(*flush_timer, enableTimer(std::chrono::milliseconds(30000), _));
response_encoder_->encodeData(body, true);
EXPECT_CALL(server_stream_callbacks_, onResetStream(StreamResetReason::LocalReset, _));
EXPECT_CALL(*flush_timer, disableTimer());
response_encoder_->getStream().resetStream(StreamResetReason::LocalReset);
MockStreamCallbacks client_stream_callbacks;
request_encoder_->getStream().addCallbacks(client_stream_callbacks);
EXPECT_CALL(response_decoder_, decodeHeaders_(_, false));
EXPECT_CALL(response_decoder_, decodeData(_, false)).Times(AtLeast(1));
EXPECT_CALL(client_stream_callbacks, onResetStream(StreamResetReason::RemoteReset, _));
setupDefaultConnectionMocks();
auto status = client_wrapper_.dispatch(Buffer::OwnedImpl(), *client_);
EXPECT_TRUE(status.ok());
}
class Http2CodecImplFlowControlTest : public Http2CodecImplTest {};
// Back up the pending_sent_data_ buffer in the client connection and make sure the watermarks fire
// as expected.
//
// This also tests the readDisable logic in StreamImpl, verifying that h2 bytes are consumed
// when the stream has readDisable(true) called.
TEST_P(Http2CodecImplFlowControlTest, TestFlowControlInPendingSendData) {
initialize();
MockStreamCallbacks callbacks;
request_encoder_->getStream().addCallbacks(callbacks);
TestRequestHeaderMapImpl request_headers;
HttpTestUtility::addDefaultHeaders(request_headers);
TestRequestHeaderMapImpl expected_headers;
HttpTestUtility::addDefaultHeaders(expected_headers);
EXPECT_CALL(request_decoder_, decodeHeaders_(HeaderMapEqual(&expected_headers), false));
request_encoder_->encodeHeaders(request_headers, false);
// Force the server stream to be read disabled. This will cause it to stop sending window
// updates to the client.
server_->getStream(1)->readDisable(true);
EXPECT_EQ(1, TestUtility::findGauge(client_stats_store_, "http2.streams_active")->value());
EXPECT_EQ(1, TestUtility::findGauge(server_stats_store_, "http2.streams_active")->value());