1
#include <memory>
2

            
3
#include "envoy/http/protocol.h"
4

            
5
#include "source/common/network/address_impl.h"
6

            
7
#include "test/mocks/network/connection.h"
8
#include "test/mocks/upstream/cluster_info.h"
9
#include "test/test_common/simulated_time_system.h"
10
#include "test/test_common/utility.h"
11

            
12
#include "cilium/accesslog.h"
13
#include "cilium/api/accesslog.pb.h"
14
#include "gtest/gtest.h"
15

            
16
namespace Envoy {
17
namespace Cilium {
18

            
19
class CiliumTest : public testing::Test {
20
protected:
21
  Event::SimulatedTimeSystem time_system_;
22
};
23

            
24
1
TEST_F(CiliumTest, AccessLog) {
25
1
  Http::TestRequestHeaderMapImpl headers{{":method", "GET"},
26
1
                                         {":path", "/"},
27
1
                                         {":authority", "host"},
28
1
                                         {"x-forwarded-proto", "http"},
29
1
                                         {"x-request-id", "ba41267c-cfc2-4a92-ad3e-cd084ab099b4"}};
30
1
  Network::MockConnection connection;
31
1
  auto source_address = std::make_shared<Network::Address::Ipv4Instance>("5.6.7.8", 45678);
32
1
  auto destination_address = std::make_shared<Network::Address::Ipv4Instance>("1.2.3.4", 80);
33
1
  connection.stream_info_.protocol_ = Http::Protocol::Http11;
34
1
  connection.stream_info_.start_time_ = time_system_.systemTime();
35
1
  connection.stream_info_.downstream_connection_info_provider_->setRemoteAddress(source_address);
36
1
  connection.stream_info_.downstream_connection_info_provider_->setLocalAddress(
37
1
      destination_address);
38

            
39
1
  AccessLog::Entry log;
40

            
41
1
  log.initFromRequest("1.2.3.4", 42, true, 1, source_address, 173, destination_address,
42
1
                      connection.stream_info_, headers);
43

            
44
1
  EXPECT_EQ(log.entry_.is_ingress(), true);
45
1
  EXPECT_EQ(log.entry_.proxy_id(), 42);
46
1
  EXPECT_EQ(log.entry_.entry_type(), ::cilium::EntryType::Request);
47
1
  EXPECT_NE(log.entry_.timestamp(), 0);
48
1
  EXPECT_STREQ(log.entry_.policy_name().c_str(), "1.2.3.4");
49
1
  EXPECT_STREQ("1.2.3.4:80", log.entry_.destination_address().c_str());
50
1
  EXPECT_STREQ("5.6.7.8:45678", log.entry_.source_address().c_str());
51
1
  EXPECT_EQ(1, log.entry_.source_security_id());
52
1
  EXPECT_EQ(173, log.entry_.destination_security_id());
53

            
54
1
  EXPECT_EQ(log.entry_.has_http(), true);
55
1
  EXPECT_EQ(::cilium::HttpProtocol::HTTP11, log.entry_.http().http_protocol());
56
1
  EXPECT_STREQ("/", log.entry_.http().path().c_str());
57
1
  EXPECT_STREQ("GET", log.entry_.http().method().c_str());
58
1
  EXPECT_STREQ("host", log.entry_.http().host().c_str());
59
1
  EXPECT_STREQ("http", log.entry_.http().scheme().c_str());
60

            
61
  // Request headers not captured above
62
1
  EXPECT_EQ(log.entry_.http().headers_size(), 1);
63
1
  EXPECT_STREQ(log.entry_.http().headers(0).key().c_str(), "x-request-id");
64
1
  EXPECT_STREQ(log.entry_.http().headers(0).value().c_str(),
65
1
               "ba41267c-cfc2-4a92-ad3e-cd084ab099b4");
66

            
67
1
  Http::TestResponseHeaderMapImpl response_headers{{"my-response-header", "response"}};
68

            
69
1
  NiceMock<Event::SimulatedTimeSystem> time_source;
70
1
  log.updateFromResponse(response_headers, time_source);
71

            
72
  // Unmodified
73
1
  EXPECT_EQ(log.entry_.has_http(), true);
74
1
  EXPECT_EQ(::cilium::HttpProtocol::HTTP11, log.entry_.http().http_protocol());
75
1
  EXPECT_STREQ("/", log.entry_.http().path().c_str());
76
1
  EXPECT_STREQ("GET", log.entry_.http().method().c_str());
77
1
  EXPECT_STREQ("host", log.entry_.http().host().c_str());
78
1
  EXPECT_STREQ("http", log.entry_.http().scheme().c_str());
79

            
80
  // x-request-id and response headers only
81
1
  EXPECT_EQ(log.entry_.http().headers_size(), 2);
82
1
  EXPECT_STREQ(log.entry_.http().headers(0).key().c_str(), "x-request-id");
83
1
  EXPECT_STREQ(log.entry_.http().headers(0).value().c_str(),
84
1
               "ba41267c-cfc2-4a92-ad3e-cd084ab099b4");
85
1
  EXPECT_STREQ(log.entry_.http().headers(1).key().c_str(), "my-response-header");
86
1
  EXPECT_STREQ(log.entry_.http().headers(1).value().c_str(), "response");
87
1
}
88

            
89
} // namespace Cilium
90
} // namespace Envoy