Repository navigation
Expand file tree
/
Copy pathhooks_accept_ctx_shape_test.cpp
More file actions
64 lines (54 loc) · 2.47 KB
/
Copy pathhooks_accept_ctx_shape_test.cpp
File metadata and controls
64 lines (54 loc) · 2.47 KB
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
/*
This file is part of libhttpserver
Copyright (C) 2011-2026 Sebastiano Merlino
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
*/
// Compile-time pin for accept_ctx's extended shape.
//
// accept_ctx originally carried only a `peer_address peer` member; it
// was extended to carry the {accepted, reason} decision so the
// observation-only hook can render denied-IP log entries (issue #332).
//
// The fields are pinned by static_asserts so a future refactor that
// reshapes accept_ctx must update this gate explicitly.
#include <optional>
#include <string>
#include <string_view>
#include <type_traits>
#include "./httpserver.hpp"
#include "./littletest.hpp"
using httpserver::accept_ctx;
using httpserver::peer_address;
static_assert(std::is_same_v<decltype(accept_ctx{}.peer), peer_address>,
"accept_ctx::peer must remain peer_address");
static_assert(std::is_same_v<decltype(accept_ctx{}.accepted), bool>,
"accept_ctx::accepted must be bool");
static_assert(std::is_same_v<decltype(accept_ctx{}.reason),
std::optional<std::string_view>>,
"accept_ctx::reason must be std::optional<std::string_view>");
// Default-construction: accepted defaults to true and reason is empty.
static_assert(accept_ctx{}.accepted == true,
"accept_ctx default `accepted` must be true");
static_assert(!accept_ctx{}.reason.has_value(),
"accept_ctx default `reason` must be nullopt");
LT_BEGIN_SUITE(hooks_accept_ctx_shape_suite)
void set_up() {}
void tear_down() {}
LT_END_SUITE(hooks_accept_ctx_shape_suite)
LT_BEGIN_AUTO_TEST(hooks_accept_ctx_shape_suite,
default_accepted_true_reason_nullopt_and_aggregate_init_with_denied)
accept_ctx ctx{};
LT_CHECK_EQ(ctx.accepted, true);
LT_CHECK(!ctx.reason.has_value());
// Aggregate-initialize with reason set, mirroring the firing-site code.
accept_ctx ctx2{peer_address{}, false, std::string_view{"denied"}};
LT_CHECK_EQ(ctx2.accepted, false);
LT_CHECK(ctx2.reason.has_value());
LT_CHECK_EQ(std::string(*ctx2.reason), std::string("denied"));
LT_END_AUTO_TEST(default_accepted_true_reason_nullopt_and_aggregate_init_with_denied)
LT_BEGIN_AUTO_TEST_ENV()
AUTORUN_TESTS()
LT_END_AUTO_TEST_ENV()