Repository navigation
Expand file tree
/
Copy pathhooks_alias_count_test.cpp
More file actions
133 lines (116 loc) · 5.17 KB
/
Copy pathhooks_alias_count_test.cpp
File metadata and controls
133 lines (116 loc) · 5.17 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
This file is part of libhttpserver
Copyright (C) 2011-2026 Sebastiano Merlino
*/
// Verifies the v1-style handler setters are observably aliases:
// registering auth_handler(fn) then querying the before_handler hook
// count reports +1.
//
// Implementation: each of the three v1 error/auth setters is, when
// non-null, an alias for an internally-registered hook at the matching
// phase. The internal registration is observable through the
// HTTPSERVER_COMPILATION-gated webserver_test_access friend bridge.
//
// - auth_handler(fn) -> +1 on hook_phase::before_handler
// - method_not_allowed_handler(h) -> +1 on hook_phase::before_handler
// - not_found_handler(h) -> +1 on hook_phase::route_resolved
//
// Aliases are only installed when the user-supplied callable is non-null.
// A webserver constructed without any of these aliases set has zero
// hooks at both phases.
#include <functional>
#include <memory>
#include <optional>
#include "./httpserver.hpp"
#include "httpserver/detail/webserver_impl.hpp"
#include "./littletest.hpp"
using httpserver::create_webserver;
using httpserver::hook_phase;
using httpserver::http_request;
using httpserver::http_response;
using httpserver::webserver;
using httpserver::detail::webserver_impl;
namespace {
std::size_t before_handler_count(webserver& ws) {
auto* impl = httpserver::webserver_test_access::impl(ws);
return impl->hooks_.hooks_before_handler_.size();
}
std::size_t route_resolved_count(webserver& ws) {
auto* impl = httpserver::webserver_test_access::impl(ws);
return impl->hooks_.hooks_route_resolved_.size();
}
} // namespace
LT_BEGIN_SUITE(hooks_alias_count_suite)
void set_up() {}
void tear_down() {}
LT_END_SUITE(hooks_alias_count_suite)
LT_BEGIN_AUTO_TEST(hooks_alias_count_suite, baseline_has_no_alias_hooks)
webserver ws{create_webserver(8205)};
LT_CHECK_EQ(before_handler_count(ws), static_cast<std::size_t>(0));
LT_CHECK_EQ(route_resolved_count(ws), static_cast<std::size_t>(0));
LT_END_AUTO_TEST(baseline_has_no_alias_hooks)
LT_BEGIN_AUTO_TEST(hooks_alias_count_suite, auth_handler_registers_one_before_handler)
// The auth lambda returns std::optional<http_response>;
// std::nullopt is the v2 "allow" sentinel (v1 used a null shared_ptr).
webserver ws{create_webserver(8205)
.auth_handler([](const http_request&)
-> std::optional<http_response> {
return std::nullopt;
})};
// Alias installed: +1 on before_handler.
LT_CHECK_EQ(before_handler_count(ws), static_cast<std::size_t>(1));
LT_CHECK_EQ(route_resolved_count(ws), static_cast<std::size_t>(0));
LT_END_AUTO_TEST(auth_handler_registers_one_before_handler)
LT_BEGIN_AUTO_TEST(hooks_alias_count_suite,
method_not_allowed_handler_registers_one_before_handler)
webserver ws{create_webserver(8205)
.method_not_allowed_handler([](const http_request&) {
return http_response::string("405");
})};
LT_CHECK_EQ(before_handler_count(ws), static_cast<std::size_t>(1));
LT_CHECK_EQ(route_resolved_count(ws), static_cast<std::size_t>(0));
LT_END_AUTO_TEST(method_not_allowed_handler_registers_one_before_handler)
LT_BEGIN_AUTO_TEST(hooks_alias_count_suite,
not_found_handler_registers_one_route_resolved)
webserver ws{create_webserver(8205)
.not_found_handler([](const http_request&) {
return http_response::string("404");
})};
LT_CHECK_EQ(before_handler_count(ws), static_cast<std::size_t>(0));
LT_CHECK_EQ(route_resolved_count(ws), static_cast<std::size_t>(1));
LT_END_AUTO_TEST(not_found_handler_registers_one_route_resolved)
LT_BEGIN_AUTO_TEST(hooks_alias_count_suite, all_three_aliases_stack)
webserver ws{create_webserver(8205)
.auth_handler([](const http_request&)
-> std::optional<http_response> {
return std::nullopt;
})
.method_not_allowed_handler([](const http_request&) {
return http_response::string("405");
})
.not_found_handler([](const http_request&) {
return http_response::string("404");
})};
LT_CHECK_EQ(before_handler_count(ws), static_cast<std::size_t>(2));
LT_CHECK_EQ(route_resolved_count(ws), static_cast<std::size_t>(1));
LT_END_AUTO_TEST(all_three_aliases_stack)
LT_BEGIN_AUTO_TEST(hooks_alias_count_suite, user_add_hook_stacks_on_top_of_alias)
webserver ws{create_webserver(8205)
.auth_handler([](const http_request&)
-> std::optional<http_response> {
return std::nullopt;
})};
// Alias installed: +1.
LT_CHECK_EQ(before_handler_count(ws), static_cast<std::size_t>(1));
auto h = ws.add_hook(hook_phase::before_handler,
std::function<httpserver::hook_action(
httpserver::before_handler_ctx&)>(
[](httpserver::before_handler_ctx&) {
return httpserver::hook_action{};
}));
// User hook stacks: count is now 2.
LT_CHECK_EQ(before_handler_count(ws), static_cast<std::size_t>(2));
LT_END_AUTO_TEST(user_add_hook_stacks_on_top_of_alias)
LT_BEGIN_AUTO_TEST_ENV()
AUTORUN_TESTS()
LT_END_AUTO_TEST_ENV()