Coverage Report

Created: 2024-09-19 09:45

/proc/self/cwd/test/common/http/common.h
Line
Count
Source (jump to first uncovered line)
1
#pragma once
2
3
#include <functional>
4
5
#include "envoy/http/conn_pool.h"
6
7
#include "source/common/http/codec_client.h"
8
9
#include "test/mocks/common.h"
10
#include "test/mocks/event/mocks.h"
11
12
namespace Envoy {
13
/**
14
 * A fake CodecClient that 1) allows a mock codec to be passed in and 2) Allows for a destroy
15
 * callback.
16
 */
17
class CodecClientForTest : public Http::CodecClient {
18
public:
19
  using DestroyCb = std::function<void(CodecClient*)>;
20
  CodecClientForTest(Http::CodecType type, Network::ClientConnectionPtr&& connection,
21
                     Http::ClientConnection* codec, DestroyCb destroy_cb,
22
                     Upstream::HostDescriptionConstSharedPtr host, Event::Dispatcher& dispatcher)
23
20.4k
      : CodecClient(type, std::move(connection), host, dispatcher), destroy_cb_(destroy_cb) {
24
20.4k
    codec_.reset(codec);
25
20.4k
    connect();
26
20.4k
  }
27
20.4k
  ~CodecClientForTest() override {
28
20.4k
    if (destroy_cb_) {
29
0
      destroy_cb_(this);
30
0
    }
31
20.4k
  }
32
12.4k
  void raiseGoAway(Http::GoAwayErrorCode error_code) { onGoAway(error_code); }
33
0
  Event::Timer* idleTimer() { return idle_timer_.get(); }
34
  using Http::CodecClient::onSettings;
35
36
  DestroyCb destroy_cb_;
37
};
38
39
/**
40
 * Mock callbacks used for conn pool testing.
41
 */
42
struct ConnPoolCallbacks : public Http::ConnectionPool::Callbacks {
43
  void onPoolReady(Http::RequestEncoder& encoder, Upstream::HostDescriptionConstSharedPtr host,
44
0
                   StreamInfo::StreamInfo&, absl::optional<Http::Protocol>) override {
45
0
    outer_encoder_ = &encoder;
46
0
    host_ = host;
47
0
    pool_ready_.ready();
48
0
  }
49
50
  void onPoolFailure(ConnectionPool::PoolFailureReason reason,
51
                     absl::string_view transport_failure_reason,
52
0
                     Upstream::HostDescriptionConstSharedPtr host) override {
53
0
    host_ = host;
54
0
    reason_ = reason;
55
0
    transport_failure_reason_ = transport_failure_reason;
56
0
    pool_failure_.ready();
57
0
  }
58
59
  ConnectionPool::PoolFailureReason reason_;
60
  std::string transport_failure_reason_;
61
  testing::NiceMock<ReadyWatcher> pool_failure_;
62
  testing::NiceMock<ReadyWatcher> pool_ready_;
63
  Http::RequestEncoder* outer_encoder_{};
64
  Upstream::HostDescriptionConstSharedPtr host_;
65
};
66
67
/**
68
 * Common utility functions for HTTP tests.
69
 */
70
class HttpTestUtility {
71
public:
72
  static void addDefaultHeaders(Http::RequestHeaderMap& headers, bool overwrite = true);
73
};
74
} // namespace Envoy