1
#pragma once
2

            
3
#include <gtest/gtest.h>
4

            
5
#include <chrono>
6
#include <string>
7
#include <utility>
8
#include <vector>
9

            
10
#include "envoy/common/pure.h"
11
#include "envoy/http/header_map.h"
12
#include "envoy/network/address.h"
13

            
14
#include "source/common/protobuf/protobuf.h" // IWYU pragma: keep
15

            
16
#include "test/integration/http_integration.h"
17
#include "test/test_common/utility.h"
18

            
19
#include "absl/types/optional.h"
20
#include "cilium/api/accesslog.pb.h"
21
#include "tests/accesslog_server.h"
22

            
23
namespace Envoy {
24

            
25
class CiliumHttpIntegrationTest : public HttpIntegrationTest,
26
                                  public testing::TestWithParam<Network::Address::IpVersion> {
27
public:
28
  CiliumHttpIntegrationTest(const std::string& config);
29
  ~CiliumHttpIntegrationTest() override;
30

            
31
  void createEnvoy() override;
32

            
33
10
  void initialize() override {
34
10
    accessLogServer_.clear();
35
10
    HttpIntegrationTest::initialize();
36
10
  }
37

            
38
  virtual std::string testPolicyFmt() PURE;
39

            
40
2
  virtual std::vector<std::pair<std::string, std::string>> testSecrets() {
41
2
    return std::vector<std::pair<std::string, std::string>>{};
42
2
  }
43

            
44
  absl::optional<::cilium::LogEntry>
45
  waitForAccessLogMessage(::cilium::EntryType entry_type,
46
                          std::chrono::milliseconds timeout = TestUtility::DefaultTimeout) {
47
    return accessLogServer_.waitForMessage(entry_type, timeout);
48
  }
49

            
50
64
  template <typename P> bool expectAccessLogRequestTo(P&& pred) {
51
64
    return accessLogServer_.expectRequestTo(pred);
52
64
  }
53

            
54
66
  template <typename P> bool expectAccessLogResponseTo(P&& pred) {
55
66
    return accessLogServer_.expectResponseTo(pred);
56
66
  }
57

            
58
48
  template <typename P> bool expectAccessLogDeniedTo(P&& pred) {
59
48
    return accessLogServer_.expectDeniedTo(pred);
60
48
  }
61

            
62
  static absl::optional<std::string>
63
  getHeader(const Protobuf::RepeatedPtrField<::cilium::KeyValue>& headers,
64
132
            const std::string& name) {
65
284
    for (const auto& entry : headers) {
66
284
      if (Http::LowerCaseString(entry.key()) == Http::LowerCaseString(name)) {
67
132
        return entry.value();
68
132
      }
69
284
    }
70
    return absl::nullopt;
71
132
  }
72

            
73
  static bool hasHeader(const Protobuf::RepeatedPtrField<::cilium::KeyValue>& headers,
74
78
                        const std::string& name, const std::string& value = "") {
75
227
    for (const auto& entry : headers) {
76
227
      if (Http::LowerCaseString(entry.key()) == Http::LowerCaseString(name) &&
77
227
          (value.empty() || entry.value() == value)) {
78
60
        return true;
79
60
      }
80
227
    }
81
18
    return false;
82
78
  }
83

            
84
  AccessLogServer accessLogServer_;
85
};
86

            
87
} // namespace Envoy