Coverage Report

Created: 2023-11-12 09:30

/proc/self/cwd/test/integration/integration_stream_decoder.h
Line
Count
Source (jump to first uncovered line)
1
#pragma once
2
3
#include <cstdint>
4
#include <memory>
5
#include <string>
6
7
#include "envoy/event/dispatcher.h"
8
#include "envoy/http/codec.h"
9
#include "envoy/http/header_map.h"
10
#include "envoy/http/metadata_interface.h"
11
12
#include "source/common/common/dump_state_utils.h"
13
14
#include "test/test_common/utility.h"
15
16
#include "absl/container/node_hash_map.h"
17
#include "absl/strings/string_view.h"
18
#include "gtest/gtest.h"
19
20
namespace Envoy {
21
/**
22
 * Stream decoder wrapper used during integration testing.
23
 */
24
class IntegrationStreamDecoder : public Http::ResponseDecoder, public Http::StreamCallbacks {
25
public:
26
  IntegrationStreamDecoder(Event::Dispatcher& dispatcher);
27
28
0
  const std::string& body() { return body_; }
29
0
  bool complete() { return saw_end_stream_; }
30
0
  bool reset() { return saw_reset_; }
31
0
  Http::StreamResetReason resetReason() { return reset_reason_; }
32
0
  const Http::ResponseHeaderMap* informationalHeaders() { return continue_headers_.get(); }
33
0
  const Http::ResponseHeaderMap& headers() { return *headers_; }
34
0
  const Http::ResponseTrailerMapPtr& trailers() { return trailers_; }
35
0
  const Http::MetadataMap& metadataMap() { return *metadata_map_; }
36
0
  uint64_t keyCount(std::string key) { return duplicated_metadata_key_count_[key]; }
37
0
  uint32_t metadataMapsDecodedCount() const { return metadata_maps_decoded_count_; }
38
  void waitFor1xxHeaders();
39
  void waitForHeaders();
40
  // This function waits until body_ has at least size bytes in it (it might have more). clearBody()
41
  // can be used if the previous body data is not relevant and the test wants to wait for a specific
42
  // amount of new data without considering the existing body size.
43
  void waitForBodyData(uint64_t size);
44
  ABSL_MUST_USE_RESULT testing::AssertionResult
45
  waitForEndStream(std::chrono::milliseconds timeout = TestUtility::DefaultTimeout);
46
  ABSL_MUST_USE_RESULT testing::AssertionResult
47
  waitForReset(std::chrono::milliseconds timeout = TestUtility::DefaultTimeout);
48
0
  void clearBody() { body_.clear(); }
49
50
  // Http::StreamDecoder
51
  void decodeData(Buffer::Instance& data, bool end_stream) override;
52
  void decodeMetadata(Http::MetadataMapPtr&& metadata_map) override;
53
54
  // Http::ResponseDecoder
55
  void decode1xxHeaders(Http::ResponseHeaderMapPtr&& headers) override;
56
  void decodeHeaders(Http::ResponseHeaderMapPtr&& headers, bool end_stream) override;
57
  void decodeTrailers(Http::ResponseTrailerMapPtr&& trailers) override;
58
0
  void dumpState(std::ostream& os, int indent_level) const override {
59
0
    DUMP_STATE_UNIMPLEMENTED(DecoderShim);
60
0
  }
61
62
  // Http::StreamCallbacks
63
  void onResetStream(Http::StreamResetReason reason,
64
                     absl::string_view transport_failure_reason) override;
65
0
  void onAboveWriteBufferHighWatermark() override {}
66
0
  void onBelowWriteBufferLowWatermark() override {}
67
68
private:
69
  Event::Dispatcher& dispatcher_;
70
  Http::ResponseHeaderMapPtr continue_headers_;
71
  Http::ResponseHeaderMapPtr headers_;
72
  Http::ResponseTrailerMapPtr trailers_;
73
  Http::MetadataMapPtr metadata_map_{new Http::MetadataMap()};
74
  absl::node_hash_map<std::string, uint64_t> duplicated_metadata_key_count_;
75
  bool waiting_for_end_stream_{};
76
  bool saw_end_stream_{};
77
  std::string body_;
78
  uint64_t body_data_waiting_length_{};
79
  bool waiting_for_reset_{};
80
  bool waiting_for_continue_headers_{};
81
  bool waiting_for_headers_{};
82
  bool saw_reset_{};
83
  Http::StreamResetReason reset_reason_{};
84
  uint32_t metadata_maps_decoded_count_{};
85
};
86
87
using IntegrationStreamDecoderPtr = std::unique_ptr<IntegrationStreamDecoder>;
88
89
} // namespace Envoy