-
-
Notifications
You must be signed in to change notification settings - Fork 191
Expand file tree
/
Copy pathcreate_test_request_test.cpp
More file actions
538 lines (487 loc) · 23.5 KB
/
Copy pathcreate_test_request_test.cpp
File metadata and controls
538 lines (487 loc) · 23.5 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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
/*
This file is part of libhttpserver
Copyright (C) 2011-2019 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.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
USA
*/
#include <cstdint>
#include <memory>
#include <string>
#include "./httpserver.hpp"
#include "httpserver/create_test_request.hpp"
#include "httpserver/detail/response_body.hpp"
#include "./littletest.hpp"
using httpserver::create_test_request;
using httpserver::http_request;
using httpserver::http_resource;
using httpserver::http_response;
// Test-only accessor for http_response internals (same pattern as
// http_response_sbo_test.cpp and http_response_factories_test.cpp).
namespace httpserver {
struct http_response_sbo_test_access {
static bool body_inline(http_response& r) noexcept {
return r.body_inline_;
}
static httpserver::detail::response_body* body_ptr(http_response& r) noexcept {
return r.body_;
}
static body_kind kind(http_response& r) noexcept { return r.kind_; }
};
} // namespace httpserver
namespace {
using SBO = httpserver::http_response_sbo_test_access;
} // namespace
LT_BEGIN_SUITE(create_test_request_suite)
void set_up() {
}
void tear_down() {
}
LT_END_SUITE(create_test_request_suite)
// Test default values
LT_BEGIN_AUTO_TEST(create_test_request_suite, build_default)
auto req = create_test_request().build();
LT_CHECK_EQ(std::string(req.get_method()), std::string("GET"));
LT_CHECK_EQ(std::string(req.get_path()), std::string("/"));
LT_CHECK_EQ(std::string(req.get_version()), std::string("HTTP/1.1"));
LT_CHECK_EQ(std::string(req.get_content()), std::string(""));
LT_END_AUTO_TEST(build_default)
// Test custom method and path
LT_BEGIN_AUTO_TEST(create_test_request_suite, build_method_path)
auto req = create_test_request()
.method("POST")
.path("/api/users")
.build();
LT_CHECK_EQ(std::string(req.get_method()), std::string("POST"));
LT_CHECK_EQ(std::string(req.get_path()), std::string("/api/users"));
LT_END_AUTO_TEST(build_method_path)
// Test headers
LT_BEGIN_AUTO_TEST(create_test_request_suite, build_headers)
auto req = create_test_request()
.header("Content-Type", "application/json")
.header("Accept", "text/plain")
.build();
LT_CHECK_EQ(std::string(req.get_header("Content-Type")), std::string("application/json"));
LT_CHECK_EQ(std::string(req.get_header("Accept")), std::string("text/plain"));
LT_CHECK_EQ(std::string(req.get_header("NonExistent")), std::string(""));
// get_headers() returns const&; bind by reference.
const auto& headers = req.get_headers();
LT_CHECK_EQ(headers.size(), static_cast<size_t>(2));
LT_END_AUTO_TEST(build_headers)
// Test footers and cookies
LT_BEGIN_AUTO_TEST(create_test_request_suite, build_footers_cookies)
auto req = create_test_request()
.footer("X-Checksum", "abc123")
.cookie("session_id", "xyz789")
.build();
LT_CHECK_EQ(std::string(req.get_footer("X-Checksum")), std::string("abc123"));
LT_CHECK_EQ(std::string(req.get_cookie("session_id")), std::string("xyz789"));
// get_footers/get_cookies() return const&; bind by reference.
const auto& footers = req.get_footers();
LT_CHECK_EQ(footers.size(), static_cast<size_t>(1));
const auto& cookies = req.get_cookies();
LT_CHECK_EQ(cookies.size(), static_cast<size_t>(1));
LT_END_AUTO_TEST(build_footers_cookies)
// Test args
LT_BEGIN_AUTO_TEST(create_test_request_suite, build_args)
auto req = create_test_request()
.arg("name", "World")
.arg("lang", "en")
.build();
LT_CHECK_EQ(std::string(req.get_arg_flat("name")), std::string("World"));
LT_CHECK_EQ(std::string(req.get_arg_flat("lang")), std::string("en"));
LT_END_AUTO_TEST(build_args)
// Test multiple values per arg key
LT_BEGIN_AUTO_TEST(create_test_request_suite, build_multi_args)
auto req = create_test_request()
.arg("color", "red")
.arg("color", "blue")
.build();
auto arg = req.get_arg("color");
LT_CHECK_EQ(arg.values.size(), static_cast<size_t>(2));
LT_CHECK_EQ(std::string(arg.values[0]), std::string("red"));
LT_CHECK_EQ(std::string(arg.values[1]), std::string("blue"));
LT_END_AUTO_TEST(build_multi_args)
// Test querystring
LT_BEGIN_AUTO_TEST(create_test_request_suite, build_querystring)
auto req = create_test_request()
.querystring("?foo=bar&baz=qux")
.build();
LT_CHECK_EQ(std::string(req.get_querystring()), std::string("?foo=bar&baz=qux"));
LT_END_AUTO_TEST(build_querystring)
// Test content
LT_BEGIN_AUTO_TEST(create_test_request_suite, build_content)
auto req = create_test_request()
.content("{\"key\":\"value\"}")
.build();
LT_CHECK_EQ(std::string(req.get_content()), std::string("{\"key\":\"value\"}"));
LT_END_AUTO_TEST(build_content)
// Auth setters are unconditional. On a HAVE_BAUTH-on build the
// values land in the http_request impl and get_user / get_pass echo
// them; on a HAVE_BAUTH-off build the same builder chain compiles and
// runs, but get_user / get_pass return the sentinel empty view.
LT_BEGIN_AUTO_TEST(create_test_request_suite, build_basic_auth)
auto req = create_test_request()
.user("admin")
.pass("secret")
.build();
#ifdef HAVE_BAUTH
LT_CHECK_EQ(std::string(req.get_user()), std::string("admin"));
LT_CHECK_EQ(std::string(req.get_pass()), std::string("secret"));
#else
LT_CHECK(req.get_user().empty());
LT_CHECK(req.get_pass().empty());
#endif
LT_END_AUTO_TEST(build_basic_auth)
// Same unconditional-setter shape for digested_user.
LT_BEGIN_AUTO_TEST(create_test_request_suite, build_digested_user)
auto req = create_test_request()
.digested_user("admin")
.build();
#ifdef HAVE_DAUTH
LT_CHECK_EQ(std::string(req.get_digested_user()), std::string("admin"));
#else
LT_CHECK(req.get_digested_user().empty());
#endif
LT_END_AUTO_TEST(build_digested_user)
// Test requestor
LT_BEGIN_AUTO_TEST(create_test_request_suite, build_requestor)
auto req = create_test_request()
.requestor("192.168.1.1")
.requestor_port(8080)
.build();
LT_CHECK_EQ(std::string(req.get_requestor()), std::string("192.168.1.1"));
LT_CHECK_EQ(req.get_requestor_port(), static_cast<uint16_t>(8080));
LT_END_AUTO_TEST(build_requestor)
// Test default requestor
LT_BEGIN_AUTO_TEST(create_test_request_suite, build_default_requestor)
auto req = create_test_request().build();
LT_CHECK_EQ(std::string(req.get_requestor()), std::string("127.0.0.1"));
LT_CHECK_EQ(req.get_requestor_port(), static_cast<uint16_t>(0));
LT_END_AUTO_TEST(build_default_requestor)
// tls_enabled() setter is unconditional. On HAVE_GNUTLS-on
// builds has_tls_session() echoes the recorded flag; on HAVE_GNUTLS-off
// builds the flag is silently dropped (has_tls_session() always returns
// false). The contract being pinned here
// is that the *setter* is callable in both modes.
LT_BEGIN_AUTO_TEST(create_test_request_suite, build_tls_enabled)
auto req = create_test_request()
.tls_enabled()
.build();
#ifdef HAVE_GNUTLS
LT_CHECK_EQ(req.has_tls_session(), true);
#else
LT_CHECK_EQ(req.has_tls_session(), false);
#endif
LT_END_AUTO_TEST(build_tls_enabled)
// Test TLS not enabled by default
LT_BEGIN_AUTO_TEST(create_test_request_suite, build_no_tls)
auto req = create_test_request().build();
LT_CHECK_EQ(req.has_tls_session(), false);
LT_END_AUTO_TEST(build_no_tls)
// The high-level cert accessors are declared unconditionally,
// so they must compile and behave (return sentinels) in both build
// modes -- HAVE_GNUTLS on or off. This test is NOT #ifdef-gated; the
// runtime contract is the same either way: no live cert means
// empty / false / -1.
//
// Also covers the tls_enabled() builder variant: even with tls_enabled()
// set, no peer certificate is attached (the test-request path has no live
// MHD connection). All cert getters must still return the documented
// sentinels; only has_tls_session() reflects the recorded flag (on a
// HAVE_GNUTLS build). This subsumes the old build_tls_enabled_no_peer_cert
// test.
LT_BEGIN_AUTO_TEST(create_test_request_suite, build_no_client_cert_returns_sentinels)
// Default builder: no TLS, no cert.
auto req = create_test_request().build();
LT_CHECK_EQ(req.has_tls_session(), false);
LT_CHECK_EQ(req.has_client_certificate(), false);
LT_CHECK(req.get_client_cert_dn().empty());
LT_CHECK(req.get_client_cert_issuer_dn().empty());
LT_CHECK(req.get_client_cert_cn().empty());
LT_CHECK(req.get_client_cert_fingerprint_sha256().empty());
LT_CHECK_EQ(req.is_client_cert_verified(), false);
LT_CHECK_EQ(req.get_client_cert_not_before(), static_cast<std::int64_t>(-1));
LT_CHECK_EQ(req.get_client_cert_not_after(), static_cast<std::int64_t>(-1));
// tls_enabled() variant: session flag set, but still no live peer cert.
auto req2 = create_test_request().tls_enabled().build();
#ifdef HAVE_GNUTLS
LT_CHECK_EQ(req2.has_tls_session(), true);
#else
LT_CHECK_EQ(req2.has_tls_session(), false);
#endif
LT_CHECK_EQ(req2.has_client_certificate(), false);
LT_CHECK(req2.get_client_cert_dn().empty());
LT_CHECK(req2.get_client_cert_issuer_dn().empty());
LT_CHECK(req2.get_client_cert_cn().empty());
LT_CHECK(req2.get_client_cert_fingerprint_sha256().empty());
LT_CHECK_EQ(req2.is_client_cert_verified(), false);
LT_CHECK_EQ(req2.get_client_cert_not_before(), static_cast<std::int64_t>(-1));
LT_CHECK_EQ(req2.get_client_cert_not_after(), static_cast<std::int64_t>(-1));
LT_END_AUTO_TEST(build_no_client_cert_returns_sentinels)
// Smoke-check that all getters on a minimal request return empty/default without
// crashing. Each path (header, footer, cookie, arg, container) is exercised
// once; any crash here pinpoints the specific getter.
LT_BEGIN_AUTO_TEST(create_test_request_suite, empty_getters_smoke_check)
auto req = create_test_request().build();
// These should all return empty/default without crashing
LT_CHECK_EQ(std::string(req.get_header("Anything")), std::string(""));
LT_CHECK_EQ(std::string(req.get_footer("Anything")), std::string(""));
LT_CHECK_EQ(std::string(req.get_cookie("Anything")), std::string(""));
LT_CHECK_EQ(std::string(req.get_arg_flat("Anything")), std::string(""));
LT_CHECK_EQ(std::string(req.get_querystring()), std::string(""));
LT_CHECK_EQ(std::string(req.get_content()), std::string(""));
LT_CHECK_EQ(req.get_headers().size(), static_cast<size_t>(0));
LT_CHECK_EQ(req.get_footers().size(), static_cast<size_t>(0));
LT_CHECK_EQ(req.get_cookies().size(), static_cast<size_t>(0));
LT_CHECK_EQ(req.get_args().size(), static_cast<size_t>(0));
LT_CHECK_EQ(req.get_args_flat().size(), static_cast<size_t>(0));
LT_CHECK_EQ(req.get_path_pieces().size(), static_cast<size_t>(0));
LT_END_AUTO_TEST(empty_getters_smoke_check)
// End-to-end: build request, call render, inspect response
class greeting_resource : public http_resource {
public:
http_response render_get(const http_request& req) override {
std::string name(req.get_arg_flat("name"));
if (name.empty()) name = "World";
return http_response::string("Hello, " + name);
}
};
LT_BEGIN_AUTO_TEST(create_test_request_suite, render_with_test_request)
greeting_resource resource;
auto req = create_test_request()
.path("/greet")
.arg("name", "Alice")
.build();
// render_get returns http_response by value.
auto resp = resource.render_get(req);
// Verify the response body kind is string.
LT_CHECK_EQ(static_cast<int>(resp.kind()),
static_cast<int>(httpserver::body_kind::string));
// Verify the response body content reflects the arg.
auto* sb = dynamic_cast<httpserver::detail::string_response_body*>(SBO::body_ptr(resp));
LT_ASSERT(sb != nullptr);
LT_CHECK_EQ(sb->get_data(), std::string("Hello, Alice"));
LT_END_AUTO_TEST(render_with_test_request)
// Test full chain of all builder methods
LT_BEGIN_AUTO_TEST(create_test_request_suite, full_chain)
auto req = create_test_request()
.method("PUT")
.path("/api/resource/42")
.version("HTTP/1.0")
.content("request body")
.header("Content-Type", "text/plain")
.header("Authorization", "Bearer token123")
.footer("Trailer", "checksum")
.cookie("session", "abc")
.arg("key1", "val1")
.arg("key2", "val2")
.querystring("?key1=val1&key2=val2")
.user("testuser")
.pass("testpass")
.requestor("10.0.0.1")
.requestor_port(9090)
.build();
LT_CHECK_EQ(std::string(req.get_method()), std::string("PUT"));
LT_CHECK_EQ(std::string(req.get_path()), std::string("/api/resource/42"));
LT_CHECK_EQ(std::string(req.get_version()), std::string("HTTP/1.0"));
LT_CHECK_EQ(std::string(req.get_content()), std::string("request body"));
LT_CHECK_EQ(std::string(req.get_header("Content-Type")), std::string("text/plain"));
LT_CHECK_EQ(std::string(req.get_header("Authorization")), std::string("Bearer token123"));
LT_CHECK_EQ(std::string(req.get_footer("Trailer")), std::string("checksum"));
LT_CHECK_EQ(std::string(req.get_cookie("session")), std::string("abc"));
LT_CHECK_EQ(std::string(req.get_arg_flat("key1")), std::string("val1"));
LT_CHECK_EQ(std::string(req.get_arg_flat("key2")), std::string("val2"));
LT_CHECK_EQ(std::string(req.get_querystring()), std::string("?key1=val1&key2=val2"));
#ifdef HAVE_BAUTH
LT_CHECK_EQ(std::string(req.get_user()), std::string("testuser"));
LT_CHECK_EQ(std::string(req.get_pass()), std::string("testpass"));
#else
// On HAVE_BAUTH-off builds the accessors return empty.
LT_CHECK(req.get_user().empty());
LT_CHECK(req.get_pass().empty());
#endif
LT_CHECK_EQ(std::string(req.get_requestor()), std::string("10.0.0.1"));
LT_CHECK_EQ(req.get_requestor_port(), static_cast<uint16_t>(9090));
LT_END_AUTO_TEST(full_chain)
// Test path pieces work with test request
LT_BEGIN_AUTO_TEST(create_test_request_suite, build_path_pieces)
auto req = create_test_request()
.path("/api/users/42")
.build();
// get_path_pieces() returns const&; bind by reference.
const auto& pieces = req.get_path_pieces();
LT_CHECK_EQ(pieces.size(), static_cast<size_t>(3));
LT_CHECK_EQ(pieces[0], std::string("api"));
LT_CHECK_EQ(pieces[1], std::string("users"));
LT_CHECK_EQ(pieces[2], std::string("42"));
LT_END_AUTO_TEST(build_path_pieces)
// Test method is uppercased
LT_BEGIN_AUTO_TEST(create_test_request_suite, method_uppercase)
auto req = create_test_request()
.method("post")
.build();
LT_CHECK_EQ(std::string(req.get_method()), std::string("POST"));
LT_END_AUTO_TEST(method_uppercase)
// Container getters return `const ContainerType&` aliasing
// impl-owned storage. Repeated calls on the same const http_request must
// return the same address (the cached container in the impl), proving:
// (a) the return type is a reference (you can take its address),
// (b) the cache is built once and reused on subsequent calls.
LT_BEGIN_AUTO_TEST(create_test_request_suite, getters_return_const_ref_stable)
auto req = create_test_request()
.header("X-Foo", "1")
.footer("X-Bar", "2")
.cookie("session", "3")
.arg("a", "b")
.path("/p/q/r")
.build();
const httpserver::http_request& cref = req;
// Call each getter twice and verify the address is stable.
LT_CHECK_EQ(&cref.get_headers(), &cref.get_headers());
LT_CHECK_EQ(&cref.get_footers(), &cref.get_footers());
LT_CHECK_EQ(&cref.get_cookies(), &cref.get_cookies());
LT_CHECK_EQ(&cref.get_args(), &cref.get_args());
LT_CHECK_EQ(&cref.get_path_pieces(), &cref.get_path_pieces());
LT_CHECK_EQ(&cref.get_files(), &cref.get_files());
// Sanity: the cached values are also non-empty / correct.
LT_CHECK_EQ(cref.get_headers().size(), static_cast<size_t>(1));
LT_CHECK_EQ(cref.get_footers().size(), static_cast<size_t>(1));
LT_CHECK_EQ(cref.get_cookies().size(), static_cast<size_t>(1));
LT_CHECK_EQ(cref.get_args().size(), static_cast<size_t>(1));
LT_CHECK_EQ(cref.get_path_pieces().size(), static_cast<size_t>(3));
// get_files() returns a direct reference to impl_->files_ (no cache flag),
// so it cannot misbehave in the same way as the other five; include it here
// for symmetry and completeness of the sanity block.
LT_CHECK_EQ(cref.get_files().size(), static_cast<size_t>(0));
LT_END_AUTO_TEST(getters_return_const_ref_stable)
// Per-key getters must be empty-on-miss and must NOT insert
// the missing key into the underlying maps. We assert this externally by
// observing the public container-getter sizes before and after a series
// of misses. The container caches are built on the first call (so we
// snapshot the baseline AFTER the first call), then we hammer the per-key
// getters with missing keys and confirm the container sizes haven't grown.
LT_BEGIN_AUTO_TEST(create_test_request_suite, missing_key_does_not_insert)
auto req = create_test_request()
.header("Present", "yes")
.footer("AlsoPresent", "yes")
.cookie("CookiePresent", "yes")
.arg("argKey", "v")
.build();
const httpserver::http_request& r = req;
// Build the container caches once so the size snapshot is stable.
const auto headers_before = r.get_headers().size();
const auto footers_before = r.get_footers().size();
const auto cookies_before = r.get_cookies().size();
const auto args_before = r.get_args().size();
// One miss on each kind is sufficient to prove the invariant; the
// container-size check below catches any accidental insertion.
LT_CHECK(r.get_header("Missing-Header").empty());
LT_CHECK(r.get_footer("Missing-Footer").empty());
LT_CHECK(r.get_cookie("Missing-Cookie").empty());
LT_CHECK(r.get_arg_flat("Missing-Arg").empty());
LT_CHECK(r.get_arg("Missing-Arg").values.empty());
// The container caches expose the underlying map sizes. If any of
// the per-key misses had inserted, these would have grown.
LT_CHECK_EQ(r.get_headers().size(), headers_before);
LT_CHECK_EQ(r.get_footers().size(), footers_before);
LT_CHECK_EQ(r.get_cookies().size(), cookies_before);
LT_CHECK_EQ(r.get_args().size(), args_before);
LT_END_AUTO_TEST(missing_key_does_not_insert)
// get_arg_flat must return the first value
// when a key has multiple values. This directly covers the documented
// first-value disambiguation behaviour.
LT_BEGIN_AUTO_TEST(create_test_request_suite, get_arg_flat_multi_value_returns_first)
auto req = create_test_request()
.arg("k", "first")
.arg("k", "second")
.build();
LT_CHECK_EQ(std::string(req.get_arg_flat("k")), std::string("first"));
// Verify both values exist via get_arg (multi-value path)
auto arg = req.get_arg("k");
LT_CHECK_EQ(arg.values.size(), static_cast<size_t>(2));
LT_CHECK_EQ(std::string(arg.values[0]), std::string("first"));
LT_CHECK_EQ(std::string(arg.values[1]), std::string("second"));
LT_END_AUTO_TEST(get_arg_flat_multi_value_returns_first)
// Note: the fallback path in
// get_arg_flat() that previously called get_connection_value(key,
// MHD_GET_ARGUMENT_KIND) on a miss has been removed.
// The MHD live-connection path is exercised by integration tests; on the
// test-request path (connection_ == nullptr) a miss correctly returns EMPTY.
// get_arg_flat on a miss must return a
// string_view whose data() points to the canonical empty sentinel, not a
// nullptr. This is a direct check on the return value, complementing the
// container-size invariant check in missing_key_does_not_insert.
LT_BEGIN_AUTO_TEST(create_test_request_suite, get_arg_flat_miss_returns_empty_sentinel)
auto req = create_test_request().build();
std::string_view sv = req.get_arg_flat("no-such-key");
LT_CHECK(sv.empty());
// The view must point to the canonical empty sentinel, not a nullptr.
LT_CHECK(sv.data() != nullptr);
LT_END_AUTO_TEST(get_arg_flat_miss_returns_empty_sentinel)
// Per-key getters return string_view aliasing the request's
// owned storage and surface the correct value on a hit.
LT_BEGIN_AUTO_TEST(create_test_request_suite, getters_return_string_view_correct_value)
auto req = create_test_request()
.header("X-Foo", "foo-value")
.footer("X-Bar", "bar-value")
.cookie("session", "sess-value")
.arg("q", "q-value")
.build();
const httpserver::http_request& r = req;
LT_CHECK_EQ(std::string(r.get_header("X-Foo")), std::string("foo-value"));
LT_CHECK_EQ(std::string(r.get_footer("X-Bar")), std::string("bar-value"));
LT_CHECK_EQ(std::string(r.get_cookie("session")), std::string("sess-value"));
LT_CHECK_EQ(std::string(r.get_arg_flat("q")), std::string("q-value"));
LT_END_AUTO_TEST(getters_return_string_view_correct_value)
// CWE-476 guard: check_digest_auth and
// check_digest_auth_digest must not dereference a null connection_ when called
// on a test request (connection_ == nullptr). The documented contract is to
// return WRONG_HEADER — the same sentinel returned on HAVE_DAUTH-off builds
// and by the existing HAVE_DAUTH-off else branch. This test is guarded on
// HAVE_DAUTH because the off-path already returns WRONG_HEADER unconditionally,
// so the null-pointer path being guarded is the HAVE_DAUTH-on branch.
#ifdef HAVE_DAUTH
LT_BEGIN_AUTO_TEST(create_test_request_suite,
check_digest_auth_on_test_request_returns_wrong_header)
auto req = create_test_request()
.digested_user("admin")
.build();
// connection_ is null on the test-request path. Without the null guard
// this call passes nullptr to MHD_digest_auth_check3 — UB / crash.
// With the guard it must return WRONG_HEADER instead.
using DAR = httpserver::http::http_utils::digest_auth_result;
using DA = httpserver::http::http_utils::digest_algorithm;
DAR result = req.check_digest_auth(
"realm", "password", /*nonce_timeout=*/300, /*max_nc=*/1000,
DA::MD5);
LT_CHECK_EQ(static_cast<int>(result),
static_cast<int>(DAR::WRONG_HEADER));
LT_END_AUTO_TEST(check_digest_auth_on_test_request_returns_wrong_header)
LT_BEGIN_AUTO_TEST(create_test_request_suite,
check_digest_auth_digest_on_test_request_returns_wrong_header)
auto req = create_test_request()
.digested_user("admin")
.build();
const char dummy_digest[32] = {};
using DAR = httpserver::http::http_utils::digest_auth_result;
using DA = httpserver::http::http_utils::digest_algorithm;
DAR result = req.check_digest_auth_digest(
"realm", dummy_digest, sizeof(dummy_digest),
/*nonce_timeout=*/300, /*max_nc=*/1000, DA::MD5);
LT_CHECK_EQ(static_cast<int>(result),
static_cast<int>(DAR::WRONG_HEADER));
LT_END_AUTO_TEST(check_digest_auth_digest_on_test_request_returns_wrong_header)
#endif // HAVE_DAUTH
LT_BEGIN_AUTO_TEST_ENV()
AUTORUN_TESTS()
LT_END_AUTO_TEST_ENV()