-
-
Notifications
You must be signed in to change notification settings - Fork 191
Expand file tree
/
Copy pathhooks_per_route_invalid_phase_throws.cpp
More file actions
257 lines (234 loc) · 8.81 KB
/
Copy pathhooks_per_route_invalid_phase_throws.cpp
File metadata and controls
257 lines (234 loc) · 8.81 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
This file is part of libhttpserver
Copyright (C) 2011-2026 Sebastiano Merlino
*/
// Contract under test:
//
// "r.add_hook(hook_phase::request_received, ...) throws
// std::invalid_argument."
//
// Validates that http_resource::add_hook performs runtime phase
// validation: each overload binds to a single permitted phase and
// throws std::invalid_argument when the runtime phase tag does not
// match. The five permitted phases are the post-route-resolution ones
// (before_handler, handler_exception, after_handler, response_sent,
// request_completed); the other six phases are rejected as a category.
//
// Compile-time gate: the absence of add_hook overloads for the
// pre-route-resolution phase signatures is enforced naturally by C++
// overload resolution -- a `std::function<hook_action(
// request_received_ctx&)>` argument simply does not match any of the
// five overloads at all. This test exercises the runtime guard for
// callers who construct a matching-signature callable but pass the
// wrong phase tag (e.g. `add_hook(response_sent, before_handler_ctx
// hook)` -- valid signature, mismatched phase).
//
// No webserver, no MHD daemon, no curl -- exercises the validation
// path directly on a stand-alone http_resource subclass.
#include <functional>
#include <stdexcept>
#include "./httpserver.hpp"
#include "./littletest.hpp"
using httpserver::hook_action;
using httpserver::hook_phase;
using httpserver::http_resource;
namespace {
class trivial_resource : public http_resource {
public:
httpserver::http_response render(const httpserver::http_request&) override {
return httpserver::http_response::string("ok");
}
};
} // namespace
LT_BEGIN_SUITE(hooks_per_route_invalid_phase_throws_suite)
void set_up() {}
void tear_down() {}
LT_END_SUITE(hooks_per_route_invalid_phase_throws_suite)
// Each of the six pre-route-resolution phases passed as a runtime tag
// to ANY of the five overloads must throw. We instantiate one of the
// five (before_handler) and feed it every wrong-phase tag in turn.
LT_BEGIN_AUTO_TEST(hooks_per_route_invalid_phase_throws_suite,
request_received_tag_throws)
trivial_resource r;
bool threw = false;
try {
r.add_hook(hook_phase::request_received,
std::function<hook_action(httpserver::before_handler_ctx&)>(
[](httpserver::before_handler_ctx&) {
return hook_action{};
}));
} catch (const std::invalid_argument&) {
threw = true;
}
LT_CHECK_EQ(threw, true);
LT_END_AUTO_TEST(request_received_tag_throws)
LT_BEGIN_AUTO_TEST(hooks_per_route_invalid_phase_throws_suite,
connection_opened_tag_throws)
trivial_resource r;
bool threw = false;
try {
r.add_hook(hook_phase::connection_opened,
std::function<hook_action(httpserver::before_handler_ctx&)>(
[](httpserver::before_handler_ctx&) {
return hook_action{};
}));
} catch (const std::invalid_argument&) {
threw = true;
}
LT_CHECK_EQ(threw, true);
LT_END_AUTO_TEST(connection_opened_tag_throws)
LT_BEGIN_AUTO_TEST(hooks_per_route_invalid_phase_throws_suite,
accept_decision_tag_throws)
trivial_resource r;
bool threw = false;
try {
r.add_hook(hook_phase::accept_decision,
std::function<void(const httpserver::response_sent_ctx&)>(
[](const httpserver::response_sent_ctx&) {}));
} catch (const std::invalid_argument&) {
threw = true;
}
LT_CHECK_EQ(threw, true);
LT_END_AUTO_TEST(accept_decision_tag_throws)
LT_BEGIN_AUTO_TEST(hooks_per_route_invalid_phase_throws_suite,
body_chunk_tag_throws)
trivial_resource r;
bool threw = false;
try {
r.add_hook(hook_phase::body_chunk,
std::function<hook_action(httpserver::after_handler_ctx&)>(
[](httpserver::after_handler_ctx&) {
return hook_action{};
}));
} catch (const std::invalid_argument&) {
threw = true;
}
LT_CHECK_EQ(threw, true);
LT_END_AUTO_TEST(body_chunk_tag_throws)
LT_BEGIN_AUTO_TEST(hooks_per_route_invalid_phase_throws_suite,
route_resolved_tag_throws)
trivial_resource r;
bool threw = false;
try {
r.add_hook(hook_phase::route_resolved,
std::function<void(const httpserver::request_completed_ctx&)>(
[](const httpserver::request_completed_ctx&) {}));
} catch (const std::invalid_argument&) {
threw = true;
}
LT_CHECK_EQ(threw, true);
LT_END_AUTO_TEST(route_resolved_tag_throws)
LT_BEGIN_AUTO_TEST(hooks_per_route_invalid_phase_throws_suite,
connection_closed_tag_throws)
trivial_resource r;
bool threw = false;
try {
r.add_hook(hook_phase::connection_closed,
std::function<hook_action(const httpserver::handler_exception_ctx&)>(
[](const httpserver::handler_exception_ctx&) {
return hook_action{};
}));
} catch (const std::invalid_argument&) {
threw = true;
}
LT_CHECK_EQ(threw, true);
LT_END_AUTO_TEST(connection_closed_tag_throws)
// Cross-mismatch within the five permitted phases is also a throw:
// passing `response_sent` to the before_handler overload should fail
// because each overload binds to exactly one phase.
LT_BEGIN_AUTO_TEST(hooks_per_route_invalid_phase_throws_suite,
cross_mismatch_within_valid_phases_throws)
trivial_resource r;
bool threw = false;
try {
// before_handler overload, response_sent tag -- mismatch.
r.add_hook(hook_phase::response_sent,
std::function<hook_action(httpserver::before_handler_ctx&)>(
[](httpserver::before_handler_ctx&) {
return hook_action{};
}));
} catch (const std::invalid_argument&) {
threw = true;
}
LT_CHECK_EQ(threw, true);
LT_END_AUTO_TEST(cross_mismatch_within_valid_phases_throws)
// The five permitted phases must NOT throw on their matching overload.
LT_BEGIN_AUTO_TEST(hooks_per_route_invalid_phase_throws_suite,
before_handler_does_not_throw)
trivial_resource r;
bool threw = false;
try {
auto h = r.add_hook(hook_phase::before_handler,
std::function<hook_action(httpserver::before_handler_ctx&)>(
[](httpserver::before_handler_ctx&) {
return hook_action{};
}));
(void)h;
} catch (...) {
threw = true;
}
LT_CHECK_EQ(threw, false);
LT_END_AUTO_TEST(before_handler_does_not_throw)
LT_BEGIN_AUTO_TEST(hooks_per_route_invalid_phase_throws_suite,
handler_exception_does_not_throw)
trivial_resource r;
bool threw = false;
try {
auto h = r.add_hook(hook_phase::handler_exception,
std::function<hook_action(const httpserver::handler_exception_ctx&)>(
[](const httpserver::handler_exception_ctx&) {
return hook_action{};
}));
(void)h;
} catch (...) {
threw = true;
}
LT_CHECK_EQ(threw, false);
LT_END_AUTO_TEST(handler_exception_does_not_throw)
LT_BEGIN_AUTO_TEST(hooks_per_route_invalid_phase_throws_suite,
after_handler_does_not_throw)
trivial_resource r;
bool threw = false;
try {
auto h = r.add_hook(hook_phase::after_handler,
std::function<hook_action(httpserver::after_handler_ctx&)>(
[](httpserver::after_handler_ctx&) {
return hook_action{};
}));
(void)h;
} catch (...) {
threw = true;
}
LT_CHECK_EQ(threw, false);
LT_END_AUTO_TEST(after_handler_does_not_throw)
LT_BEGIN_AUTO_TEST(hooks_per_route_invalid_phase_throws_suite,
response_sent_does_not_throw)
trivial_resource r;
bool threw = false;
try {
auto h = r.add_hook(hook_phase::response_sent,
std::function<void(const httpserver::response_sent_ctx&)>(
[](const httpserver::response_sent_ctx&) {}));
(void)h;
} catch (...) {
threw = true;
}
LT_CHECK_EQ(threw, false);
LT_END_AUTO_TEST(response_sent_does_not_throw)
LT_BEGIN_AUTO_TEST(hooks_per_route_invalid_phase_throws_suite,
request_completed_does_not_throw)
trivial_resource r;
bool threw = false;
try {
auto h = r.add_hook(hook_phase::request_completed,
std::function<void(const httpserver::request_completed_ctx&)>(
[](const httpserver::request_completed_ctx&) {}));
(void)h;
} catch (...) {
threw = true;
}
LT_CHECK_EQ(threw, false);
LT_END_AUTO_TEST(request_completed_does_not_throw)
LT_BEGIN_AUTO_TEST_ENV()
AUTORUN_TESTS()
LT_END_AUTO_TEST_ENV()