-
-
Notifications
You must be signed in to change notification settings - Fork 191
Expand file tree
/
Copy pathrouting_regression_test.cpp
More file actions
725 lines (639 loc) · 32.6 KB
/
Copy pathrouting_regression_test.cpp
File metadata and controls
725 lines (639 loc) · 32.6 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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
/*
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.
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
*/
// v2.0 routing-semantics regression gate. Walks every routing
// pattern in the v1 test corpus (taxonomy enumerated in
// test/REGRESSION.md) through the public webserver registration surface
// and asserts the v2 3-tier table (probed via webserver_impl::lookup_v2)
// resolves each pattern to the right tier, the right captures, and the
// right method_set. Today the v2 table is shadow-populated alongside the
// v1 maps and is not yet on the dispatch path (the dispatch cutover
// lands separately). This TU is the only thing pinning v2-lookup semantics ahead of
// that cutover, so a failure here is a release-blocker per AR-003.
//
// Each LT_BEGIN_AUTO_TEST mirrors one row of the taxonomy table in
// REGRESSION.md. When you add a new routing pattern, add a test here
// AND a row to the table.
#include <memory>
#include <string>
#include <utility>
#include "./httpserver.hpp"
#include "./httpserver/detail/webserver_impl.hpp"
#include "./littletest.hpp"
namespace ht = httpserver;
namespace {
// Stateless resource — every test that needs a class-derived handler
// uses this. The render_get body is irrelevant: lookup_v2 returns the
// route_entry without invoking the handler.
class noop_resource : public ht::http_resource {
public:
ht::http_response render_get(const ht::http_request&)
override {
return
ht::http_response::string("ok");
}
};
// Convenience: pull the impl handle off a non-started webserver.
ht::detail::webserver_impl& impl_of(ht::webserver& ws) {
return *ht::webserver_test_access::impl(ws);
}
// Sentinel used by on_get_only_method_yields_get_only_method_set — we
// just need a lambda that compiles; the body is never called.
ht::http_response noop_handler(const ht::http_request&) {
return ht::http_response(ht::http_response::string("ok"));
}
} // namespace
LT_BEGIN_SUITE(routing_regression_suite)
void set_up() {}
void tear_down() {}
LT_END_SUITE(routing_regression_suite)
// ---------------------------------------------------------------------
// Exact paths (taxonomy row: exact).
// ---------------------------------------------------------------------
LT_BEGIN_AUTO_TEST(routing_regression_suite,
exact_path_hits_exact_tier_with_full_methods)
ht::webserver ws{ht::create_webserver(8080)
.start_method(ht::http::http_utils::INTERNAL_SELECT)};
ws.register_path("/exact", std::make_shared<noop_resource>());
auto r = impl_of(ws).lookup_v2(ht::http_method::get,
std::string("/exact"));
LT_CHECK(r.found);
LT_CHECK(r.tier == ht::detail::webserver_impl::tier_hit::exact);
// register_path → method_set is "all methods set" (matches v1
// semantics: class-derived resources serve every method the class
// is configured to render).
LT_CHECK(r.entry.methods == ht::method_set{}.set_all());
LT_CHECK(!r.entry.is_prefix);
LT_END_AUTO_TEST(exact_path_hits_exact_tier_with_full_methods)
LT_BEGIN_AUTO_TEST(routing_regression_suite, exact_path_root_only)
// Mirrors http_endpoint_root_only — root-only registration should be
// exact-tier and only match the root, not arbitrary paths.
ht::webserver ws{ht::create_webserver(8080)
.start_method(ht::http::http_utils::INTERNAL_SELECT)};
ws.register_path("/", std::make_shared<noop_resource>());
auto root = impl_of(ws).lookup_v2(ht::http_method::get,
std::string("/"));
LT_CHECK(root.found);
LT_CHECK(root.tier == ht::detail::webserver_impl::tier_hit::exact);
auto sub = impl_of(ws).lookup_v2(ht::http_method::get,
std::string("/foo"));
LT_CHECK(!sub.found);
LT_CHECK(sub.tier == ht::detail::webserver_impl::tier_hit::none);
LT_END_AUTO_TEST(exact_path_root_only)
LT_BEGIN_AUTO_TEST(routing_regression_suite,
exact_path_normalization_aliases)
// Mirrors basic_suite::duplicate_endpoints — v1 normalizes "OK",
// "/OK", "/OK/", "OK/" to the same canonical key. The v2 lookup
// path must agree.
ht::webserver ws{ht::create_webserver(8080)
.start_method(ht::http::http_utils::INTERNAL_SELECT)};
ws.register_path("/ok", std::make_shared<noop_resource>());
// The segment trie's exact tier stores the canonical form. Both
// /ok and /ok/ should resolve.
auto a = impl_of(ws).lookup_v2(ht::http_method::get,
std::string("/ok"));
LT_CHECK(a.found);
auto b = impl_of(ws).lookup_v2(ht::http_method::get,
std::string("/ok/"));
LT_CHECK(b.found);
LT_END_AUTO_TEST(exact_path_normalization_aliases)
// ---------------------------------------------------------------------
// Parameterized paths (taxonomy rows: single-param, multi-param,
// custom-regex param).
// ---------------------------------------------------------------------
LT_BEGIN_AUTO_TEST(routing_regression_suite,
parameterized_single_segment_captures)
// Mirrors basic_suite::regex_matching_arg via the on_get lambda
// surface. The route is GET-only, so methods should
// contain GET and NOT contain POST. The radix tier carries the
// capture for {id}.
ht::webserver ws{ht::create_webserver(8080)
.start_method(ht::http::http_utils::INTERNAL_SELECT)};
ws.on_get("/users/{id}", noop_handler);
auto r = impl_of(ws).lookup_v2(ht::http_method::get,
std::string("/users/42"));
LT_CHECK(r.found);
LT_CHECK(r.tier == ht::detail::webserver_impl::tier_hit::radix);
LT_CHECK(r.entry.methods.contains(ht::http_method::get));
LT_CHECK(!r.entry.methods.contains(ht::http_method::post));
LT_CHECK_EQ(r.captured_params.size(),
static_cast<std::size_t>(1));
LT_CHECK_EQ(r.captured_params[0].first, std::string("id"));
LT_CHECK_EQ(r.captured_params[0].second, std::string("42"));
LT_END_AUTO_TEST(parameterized_single_segment_captures)
LT_BEGIN_AUTO_TEST(routing_regression_suite,
parameterized_multiple_segments_capture_in_order)
// Mirrors http_endpoint_suite::http_endpoint_multiple_params via
// the v2 lookup. The two {name} segments come out in the order
// they appear in the registered pattern.
ht::webserver ws{ht::create_webserver(8080)
.start_method(ht::http::http_utils::INTERNAL_SELECT)};
ws.register_path("/a/{x}/b/{y}/c",
std::make_shared<noop_resource>());
auto r = impl_of(ws).lookup_v2(ht::http_method::get,
std::string("/a/1/b/2/c"));
LT_CHECK(r.found);
LT_CHECK(r.tier == ht::detail::webserver_impl::tier_hit::radix);
LT_CHECK_EQ(r.captured_params.size(),
static_cast<std::size_t>(2));
LT_CHECK_EQ(r.captured_params[0].first, std::string("x"));
LT_CHECK_EQ(r.captured_params[0].second, std::string("1"));
LT_CHECK_EQ(r.captured_params[1].first, std::string("y"));
LT_CHECK_EQ(r.captured_params[1].second, std::string("2"));
LT_END_AUTO_TEST(parameterized_multiple_segments_capture_in_order)
LT_BEGIN_AUTO_TEST(routing_regression_suite,
parameterized_with_custom_regex_enforced_in_radix_tier)
// Mirrors basic_suite::regex_matching_arg_custom. v1 used the
// http_endpoint compiled-regex map to enforce per-segment
// constraints; the v2 radix tier now does the same in-line via
// segment_trie_node::wildcard_constraint_, restoring v1 parity. A
// matching segment resolves in the radix tier (and captures
// under the bare name); a non-matching segment misses the
// wildcard slot and falls through to 404.
ht::webserver ws{ht::create_webserver(8080)
.start_method(ht::http::http_utils::INTERNAL_SELECT)};
ws.register_path("/items/{id|([0-9]+)}",
std::make_shared<noop_resource>());
auto hit = impl_of(ws).lookup_v2(ht::http_method::get,
std::string("/items/42"));
LT_CHECK(hit.found);
LT_CHECK(hit.tier == ht::detail::webserver_impl::tier_hit::radix);
auto non_numeric = impl_of(ws).lookup_v2(ht::http_method::get,
std::string("/items/abc"));
LT_CHECK(!non_numeric.found);
LT_END_AUTO_TEST(parameterized_with_custom_regex_enforced_in_radix_tier)
// ---------------------------------------------------------------------
// Prefix paths (taxonomy row: prefix).
// ---------------------------------------------------------------------
LT_BEGIN_AUTO_TEST(routing_regression_suite,
prefix_serves_bare_and_subpaths)
// Mirrors basic_suite::family_endpoints. A prefix registration
// serves both the bare path and arbitrary subpaths under it.
ht::webserver ws{ht::create_webserver(8080)
.start_method(ht::http::http_utils::INTERNAL_SELECT)};
ws.register_prefix("/static", std::make_shared<noop_resource>());
auto bare = impl_of(ws).lookup_v2(ht::http_method::get,
std::string("/static"));
LT_CHECK(bare.found);
LT_CHECK(bare.tier == ht::detail::webserver_impl::tier_hit::radix);
LT_CHECK(bare.entry.is_prefix);
auto sub = impl_of(ws).lookup_v2(ht::http_method::get,
std::string("/static/foo/bar"));
LT_CHECK(sub.found);
LT_CHECK(sub.tier == ht::detail::webserver_impl::tier_hit::radix);
LT_CHECK(sub.entry.is_prefix);
LT_END_AUTO_TEST(prefix_serves_bare_and_subpaths)
LT_BEGIN_AUTO_TEST(routing_regression_suite,
prefix_then_more_specific_exact_shadows)
// Mirrors the "exact shadows prefix on the exact path, prefix
// serves the rest" precedence relied on by family-style routes
// with carve-outs.
ht::webserver ws{ht::create_webserver(8080)
.start_method(ht::http::http_utils::INTERNAL_SELECT)};
auto prefix_res = std::make_shared<noop_resource>();
auto exact_res = std::make_shared<noop_resource>();
ws.register_prefix("/static", prefix_res);
ws.register_path("/static/index", exact_res);
// /static/index — exact wins.
auto idx = impl_of(ws).lookup_v2(ht::http_method::get,
std::string("/static/index"));
LT_CHECK(idx.found);
LT_CHECK(idx.tier == ht::detail::webserver_impl::tier_hit::exact);
LT_CHECK(!idx.entry.is_prefix);
// /static/foo — radix tier serves it as a prefix subpath.
auto foo = impl_of(ws).lookup_v2(ht::http_method::get,
std::string("/static/foo"));
LT_CHECK(foo.found);
LT_CHECK(foo.tier == ht::detail::webserver_impl::tier_hit::radix);
LT_CHECK(foo.entry.is_prefix);
LT_END_AUTO_TEST(prefix_then_more_specific_exact_shadows)
// ---------------------------------------------------------------------
// Pure regex paths (taxonomy row: regex).
// ---------------------------------------------------------------------
LT_BEGIN_AUTO_TEST(routing_regression_suite,
regex_route_with_metachars_hits_regex_tier)
// Mirrors basic_suite::regex_matching. A path with regex
// metacharacters and no {name} params goes through the regex
// tier under the default regex_checking=true.
ht::webserver ws{ht::create_webserver(8080)
.start_method(ht::http::http_utils::INTERNAL_SELECT)};
ws.register_path("/api/v[0-9]+",
std::make_shared<noop_resource>());
auto r = impl_of(ws).lookup_v2(ht::http_method::get,
std::string("/api/v1"));
LT_CHECK(r.found);
LT_CHECK(r.tier == ht::detail::webserver_impl::tier_hit::regex);
LT_END_AUTO_TEST(regex_route_with_metachars_hits_regex_tier)
// ---------------------------------------------------------------------
// Register/unregister cycles (taxonomy row: register/unregister).
// ---------------------------------------------------------------------
LT_BEGIN_AUTO_TEST(routing_regression_suite,
register_then_unregister_then_lookup_misses)
// Mirrors basic_suite::register_unregister via the v2 lookup.
// After unregister_path, lookup_v2 must miss (tier_hit::none) so
// future dispatch will 404.
ht::webserver ws{ht::create_webserver(8080)
.start_method(ht::http::http_utils::INTERNAL_SELECT)};
ws.register_path("/ephemeral", std::make_shared<noop_resource>());
auto before = impl_of(ws).lookup_v2(ht::http_method::get,
std::string("/ephemeral"));
LT_CHECK(before.found);
ws.unregister_path("/ephemeral");
auto after = impl_of(ws).lookup_v2(ht::http_method::get,
std::string("/ephemeral"));
LT_CHECK(!after.found);
LT_CHECK(after.tier ==
ht::detail::webserver_impl::tier_hit::none);
LT_END_AUTO_TEST(register_then_unregister_then_lookup_misses)
LT_BEGIN_AUTO_TEST(routing_regression_suite,
prefix_register_then_unregister_then_lookup_misses)
// Symmetric pin for the prefix half of register/unregister — the
// v2 radix-tier prefix node must come out cleanly.
ht::webserver ws{ht::create_webserver(8080)
.start_method(ht::http::http_utils::INTERNAL_SELECT)};
ws.register_prefix("/scratch", std::make_shared<noop_resource>());
auto before = impl_of(ws).lookup_v2(
ht::http_method::get, std::string("/scratch/anything"));
LT_CHECK(before.found);
ws.unregister_prefix("/scratch");
auto after = impl_of(ws).lookup_v2(
ht::http_method::get, std::string("/scratch/anything"));
LT_CHECK(!after.found);
LT_END_AUTO_TEST(prefix_register_then_unregister_then_lookup_misses)
// ---------------------------------------------------------------------
// Prefix-vs-exact terminus collision.
//
// Two registrations on the SAME canonical path that disagree on the
// prefix/exact dimension cannot coexist in the v2 table: the
// route-cache key (method, path) cannot distinguish the two, and
// lookup_v2 would silently shadow one of them in tier-priority order.
// The collision-detection contract is: the SECOND registration
// throws std::invalid_argument at registration time and the original
// entry is left intact (atomicity).
//
// Six shapes are pinned — every combination of {register_path,
// register_prefix, on_get} where the two calls land on the same path
// but with opposite prefix/exact polarity.
// ---------------------------------------------------------------------
LT_BEGIN_AUTO_TEST(routing_regression_suite,
register_exact_after_prefix_throws_collision)
ht::webserver ws{ht::create_webserver(8080)
.start_method(ht::http::http_utils::INTERNAL_SELECT)};
ws.register_prefix("/admin", std::make_shared<noop_resource>());
bool threw = false;
try {
ws.on_get("/admin", noop_handler);
} catch (const std::invalid_argument&) {
threw = true;
}
LT_CHECK(threw);
// Original prefix entry must remain intact (atomicity contract).
// Verify via observable behaviour: /admin/anything resolves (prefix
// semantics) and /admin also resolves (the prefix terminates here).
auto r = impl_of(ws).lookup_v2(
ht::http_method::get, std::string("/admin/anything"));
LT_CHECK(r.found);
// Bare path also resolves via prefix.
auto r2 = impl_of(ws).lookup_v2(
ht::http_method::get, std::string("/admin"));
LT_CHECK(r2.found);
LT_END_AUTO_TEST(register_exact_after_prefix_throws_collision)
LT_BEGIN_AUTO_TEST(routing_regression_suite,
register_prefix_after_exact_throws_collision)
// Symmetric inverse: on_get first, then register_prefix on the
// same path. The prefix call is the one that must throw.
ht::webserver ws{ht::create_webserver(8080)
.start_method(ht::http::http_utils::INTERNAL_SELECT)};
ws.on_get("/admin", noop_handler);
bool threw = false;
try {
ws.register_prefix("/admin", std::make_shared<noop_resource>());
} catch (const std::invalid_argument&) {
threw = true;
}
LT_CHECK(threw);
// Original exact entry must remain intact: the bare path still
// resolves (observable behaviour — exact match exists).
auto r = impl_of(ws).lookup_v2(
ht::http_method::get, std::string("/admin"));
LT_CHECK(r.found);
// And the failed prefix registration must NOT have planted a
// prefix terminus — /admin/sub must miss (exact semantics preserved).
auto sub = impl_of(ws).lookup_v2(
ht::http_method::get, std::string("/admin/sub"));
LT_CHECK(!sub.found);
LT_END_AUTO_TEST(register_prefix_after_exact_throws_collision)
LT_BEGIN_AUTO_TEST(routing_regression_suite,
register_path_after_prefix_throws_collision)
// Class-based exact after class-based prefix on the same path.
ht::webserver ws{ht::create_webserver(8080)
.start_method(ht::http::http_utils::INTERNAL_SELECT)};
ws.register_prefix("/static", std::make_shared<noop_resource>());
bool threw = false;
try {
ws.register_path("/static", std::make_shared<noop_resource>());
} catch (const std::invalid_argument&) {
threw = true;
}
LT_CHECK(threw);
// Original prefix entry must remain intact: /static/x resolves
// (prefix semantics — deeper path matches), and /static itself also
// resolves (the prefix terminus is anchored here).
auto r = impl_of(ws).lookup_v2(
ht::http_method::get, std::string("/static/x"));
LT_CHECK(r.found);
auto r2 = impl_of(ws).lookup_v2(
ht::http_method::get, std::string("/static"));
LT_CHECK(r2.found);
LT_END_AUTO_TEST(register_path_after_prefix_throws_collision)
LT_BEGIN_AUTO_TEST(routing_regression_suite,
register_prefix_after_path_throws_collision)
// Class-based prefix after class-based exact on the same path.
ht::webserver ws{ht::create_webserver(8080)
.start_method(ht::http::http_utils::INTERNAL_SELECT)};
ws.register_path("/static", std::make_shared<noop_resource>());
bool threw = false;
try {
ws.register_prefix("/static", std::make_shared<noop_resource>());
} catch (const std::invalid_argument&) {
threw = true;
}
LT_CHECK(threw);
// Original exact entry must remain intact: /static resolves, but
// /static/sub does NOT (exact semantics — no deeper path matches).
auto r = impl_of(ws).lookup_v2(
ht::http_method::get, std::string("/static"));
LT_CHECK(r.found);
auto sub = impl_of(ws).lookup_v2(
ht::http_method::get, std::string("/static/sub"));
LT_CHECK(!sub.found);
LT_END_AUTO_TEST(register_prefix_after_path_throws_collision)
LT_BEGIN_AUTO_TEST(routing_regression_suite,
parameterized_exact_after_parameterized_prefix_throws_collision)
// Parameterized path lands in the radix tier on BOTH calls. The
// collision is between an exact_terminus_ and a prefix_terminus_
// on the exact same radix node.
ht::webserver ws{ht::create_webserver(8080)
.start_method(ht::http::http_utils::INTERNAL_SELECT)};
ws.register_prefix("/users/{id}",
std::make_shared<noop_resource>());
bool threw = false;
try {
ws.on_get("/users/{id}", noop_handler);
} catch (const std::invalid_argument&) {
threw = true;
}
LT_CHECK(threw);
// The original prefix entry must still match a deeper path
// (observable behaviour: /users/42/profile resolves via prefix).
auto r = impl_of(ws).lookup_v2(
ht::http_method::get, std::string("/users/42/profile"));
LT_CHECK(r.found);
// The exact node itself also resolves (prefix captures /users/42 and
// any extension).
auto r2 = impl_of(ws).lookup_v2(
ht::http_method::get, std::string("/users/42"));
LT_CHECK(r2.found);
LT_END_AUTO_TEST(parameterized_exact_after_parameterized_prefix_throws_collision)
LT_BEGIN_AUTO_TEST(routing_regression_suite,
parameterized_prefix_after_parameterized_exact_throws_collision)
// Inverse of the above.
ht::webserver ws{ht::create_webserver(8080)
.start_method(ht::http::http_utils::INTERNAL_SELECT)};
ws.on_get("/users/{id}", noop_handler);
bool threw = false;
try {
ws.register_prefix("/users/{id}",
std::make_shared<noop_resource>());
} catch (const std::invalid_argument&) {
threw = true;
}
LT_CHECK(threw);
// Original exact entry must remain intact: /users/42 resolves.
auto r = impl_of(ws).lookup_v2(
ht::http_method::get, std::string("/users/42"));
LT_CHECK(r.found);
// The failed prefix registration must NOT have planted a prefix
// terminus — /users/42/profile must miss (exact semantics).
auto sub = impl_of(ws).lookup_v2(
ht::http_method::get, std::string("/users/42/profile"));
LT_CHECK(!sub.found);
LT_END_AUTO_TEST(parameterized_prefix_after_parameterized_exact_throws_collision)
// ---------------------------------------------------------------------
// Method-mismatched semantics (taxonomy row: method-mismatched).
// The method_set bitmask is what dispatch uses for the 405 + Allow
// decision once dispatch cuts over to the v2 table.
// ---------------------------------------------------------------------
LT_BEGIN_AUTO_TEST(routing_regression_suite,
on_get_only_method_yields_get_only_method_set)
ht::webserver ws{ht::create_webserver(8080)
.start_method(ht::http::http_utils::INTERNAL_SELECT)};
ws.on_get("/g", noop_handler);
// Looking up under GET: found, GET-only mask.
auto r_get = impl_of(ws).lookup_v2(ht::http_method::get,
std::string("/g"));
LT_CHECK(r_get.found);
LT_CHECK(r_get.entry.methods.contains(ht::http_method::get));
LT_CHECK(!r_get.entry.methods.contains(ht::http_method::post));
LT_CHECK(!r_get.entry.methods.contains(ht::http_method::put));
// Looking up under POST: lookup_v2 doesn't filter on method (the
// 405 decision lives at the dispatch site per the comment on
// webserver_impl::lookup_v2). The entry still surfaces with its
// GET-only mask so dispatch can compose Allow.
auto r_post = impl_of(ws).lookup_v2(ht::http_method::post,
std::string("/g"));
LT_CHECK(r_post.found);
LT_CHECK(r_post.entry.methods.contains(ht::http_method::get));
LT_CHECK(!r_post.entry.methods.contains(ht::http_method::post));
LT_END_AUTO_TEST(on_get_only_method_yields_get_only_method_set)
LT_BEGIN_AUTO_TEST(routing_regression_suite,
on_get_then_on_post_same_path_merges_methods)
// Mirrors webserver_on_methods_test merge-on-distinct-method
// semantics — two on_* registrations on the same path compose
// into one entry whose method_set carries both bits.
ht::webserver ws{ht::create_webserver(8080)
.start_method(ht::http::http_utils::INTERNAL_SELECT)};
ws.on_get("/m", noop_handler);
ws.on_post("/m", noop_handler);
auto r = impl_of(ws).lookup_v2(ht::http_method::get,
std::string("/m"));
LT_CHECK(r.found);
LT_CHECK(r.entry.methods.contains(ht::http_method::get));
LT_CHECK(r.entry.methods.contains(ht::http_method::post));
LT_CHECK(!r.entry.methods.contains(ht::http_method::put));
LT_END_AUTO_TEST(on_get_then_on_post_same_path_merges_methods)
// ---------------------------------------------------------------------
// Overlapping endpoints (taxonomy row: overlap).
//
// v1's basic_suite::overlapping_endpoints documents "regex wins, not
// sure why" — an iteration-order accident over std::map. The v2 table
// gives a deterministic structural precedence (first-registered wins
// when two patterns of the same tier match). This test pins THAT v2
// behavior; the divergence-from-v1 rationale is documented in
// REGRESSION.md.
// ---------------------------------------------------------------------
LT_BEGIN_AUTO_TEST(routing_regression_suite,
overlapping_two_regex_routes_deterministic_first_wins)
ht::webserver ws{ht::create_webserver(8080)
.start_method(ht::http::http_utils::INTERNAL_SELECT)};
auto first = std::make_shared<noop_resource>();
auto second = std::make_shared<noop_resource>();
ws.register_path("/foo/{var|([a-z]+)}/", first);
ws.register_path("/{var|([a-z]+)}/bar/", second);
auto r = impl_of(ws).lookup_v2(ht::http_method::get,
std::string("/foo/bar/"));
LT_CHECK(r.found);
// The v2 segment trie prefers exact children over wildcard children
// at each node. /foo/{var|([a-z]+)}/ anchors segment 0 on the
// literal "foo" (exact child), so the tree descends there first and
// never tries the wildcard root branch required by the second
// pattern. Structural precedence → first-registered wins here.
// route_entry::handler is now a bare shared_ptr<http_resource>
// (the variant arm was collapsed); read it directly.
LT_CHECK(r.entry.handler != nullptr);
LT_CHECK(r.entry.handler == first);
LT_END_AUTO_TEST(overlapping_two_regex_routes_deterministic_first_wins)
LT_BEGIN_AUTO_TEST(routing_regression_suite,
later_exact_registration_shadows_earlier_regex)
// Mirrors the second half of basic_suite::overlapping_endpoints —
// an exact-tier registration must beat a regex-tier registration
// that also matches the path, because the lookup pipeline checks
// exact before regex.
ht::webserver ws{ht::create_webserver(8080)
.start_method(ht::http::http_utils::INTERNAL_SELECT)};
auto rx = std::make_shared<noop_resource>();
auto exact = std::make_shared<noop_resource>();
ws.register_path("/foo/{var|([a-z]+)}/", rx);
ws.register_path("/foo/bar/", exact);
auto r = impl_of(ws).lookup_v2(ht::http_method::get,
std::string("/foo/bar/"));
LT_CHECK(r.found);
LT_CHECK(r.tier == ht::detail::webserver_impl::tier_hit::exact);
LT_CHECK(r.entry.handler != nullptr);
LT_CHECK(r.entry.handler == exact);
LT_END_AUTO_TEST(later_exact_registration_shadows_earlier_regex)
// ---------------------------------------------------------------------
// Single-resource mode (taxonomy row: single-resource).
// ---------------------------------------------------------------------
LT_BEGIN_AUTO_TEST(routing_regression_suite,
single_resource_mode_serves_any_subpath)
// Mirrors basic_suite::single_resource_mode — under single_resource
// mode, a single register_prefix("/") catches everything.
ht::webserver ws{ht::create_webserver(8080)
.single_resource()
.start_method(ht::http::http_utils::INTERNAL_SELECT)};
auto only = std::make_shared<noop_resource>();
ws.register_prefix("/", only);
auto root = impl_of(ws).lookup_v2(ht::http_method::get,
std::string("/"));
LT_CHECK(root.found);
auto deep = impl_of(ws).lookup_v2(
ht::http_method::get, std::string("/anything/at/all"));
LT_CHECK(deep.found);
LT_CHECK(deep.entry.handler != nullptr);
LT_CHECK(deep.entry.handler == only);
LT_END_AUTO_TEST(single_resource_mode_serves_any_subpath)
// ---------------------------------------------------------------------
// Regex-checking off (taxonomy row: regex-disabled).
// ---------------------------------------------------------------------
LT_BEGIN_AUTO_TEST(routing_regression_suite,
no_regex_checking_treats_metachars_as_literal)
// With regex_checking(false), a path containing regex metacharacters
// is registered as a literal exact path. The classifier must NOT
// route it to the regex tier.
ht::webserver ws{ht::create_webserver(8080)
.regex_checking(false)
.start_method(ht::http::http_utils::INTERNAL_SELECT)};
ws.register_path("/api/v[0-9]+",
std::make_shared<noop_resource>());
// Literal match: hits exact tier.
auto lit = impl_of(ws).lookup_v2(ht::http_method::get,
std::string("/api/v[0-9]+"));
LT_CHECK(lit.found);
LT_CHECK(lit.tier == ht::detail::webserver_impl::tier_hit::exact);
// Regex-style probe: misses, because the route is no longer
// interpreted as a regex.
auto miss = impl_of(ws).lookup_v2(ht::http_method::get,
std::string("/api/v1"));
LT_CHECK(!miss.found);
LT_END_AUTO_TEST(no_regex_checking_treats_metachars_as_literal)
// ---------------------------------------------------------------------
// Baseline miss (taxonomy row: unregistered).
//
// Verifies that lookup_v2 returns found=false and tier_hit::none for a
// path that was never registered through any surface. Guards against a
// hypothetical bug where a default-constructed entry is treated as
// found.
// ---------------------------------------------------------------------
LT_BEGIN_AUTO_TEST(routing_regression_suite, unregistered_path_yields_miss)
ht::webserver ws{ht::create_webserver(8080)
.start_method(ht::http::http_utils::INTERNAL_SELECT)};
// Register an unrelated path so the table is not completely empty,
// reducing the chance this is a vacuous pass.
ws.register_path("/other", std::make_shared<noop_resource>());
auto r = impl_of(ws).lookup_v2(ht::http_method::get,
std::string("/never-registered"));
LT_CHECK(!r.found);
LT_CHECK(r.tier == ht::detail::webserver_impl::tier_hit::none);
LT_END_AUTO_TEST(unregistered_path_yields_miss)
// ---------------------------------------------------------------------
// Cache tier (taxonomy row: cache).
//
// (1) warm-cache: second lookup on the same path hits the LRU cache tier.
// (2) cache-invalidation: after unregister_path the stale cache entry
// must not survive — lookup must miss.
// ---------------------------------------------------------------------
LT_BEGIN_AUTO_TEST(routing_regression_suite,
second_lookup_same_path_hits_cache_tier)
ht::webserver ws{ht::create_webserver(8080)
.start_method(ht::http::http_utils::INTERNAL_SELECT)};
// Use a parameterized (radix) route: the exact tier bypasses the
// cache (it is a concurrent shared_lock probe), so only the
// genuinely-expensive parameter/regex tiers are memoised.
ws.register_path("/cached/{id}", std::make_shared<noop_resource>());
// First lookup: resolves via the radix tier and populates the cache.
auto cold = impl_of(ws).lookup_v2(ht::http_method::get,
std::string("/cached/42"));
LT_CHECK(cold.found);
// First lookup resolves via a tier other than cache.
LT_CHECK(cold.tier != ht::detail::webserver_impl::tier_hit::cache);
// Second lookup for the same path: must come from the LRU cache.
auto warm = impl_of(ws).lookup_v2(ht::http_method::get,
std::string("/cached/42"));
LT_CHECK(warm.found);
LT_CHECK(warm.tier == ht::detail::webserver_impl::tier_hit::cache);
LT_END_AUTO_TEST(second_lookup_same_path_hits_cache_tier)
LT_BEGIN_AUTO_TEST(routing_regression_suite,
unregister_invalidates_cache_entry)
ht::webserver ws{ht::create_webserver(8080)
.start_method(ht::http::http_utils::INTERNAL_SELECT)};
ws.register_path("/volatile", std::make_shared<noop_resource>());
// Warm the cache.
auto before = impl_of(ws).lookup_v2(ht::http_method::get,
std::string("/volatile"));
LT_CHECK(before.found);
// Remove the registration — must also purge the cached entry.
ws.unregister_path("/volatile");
// A stale cache entry would return found=true with tier_hit::cache;
// correct behavior is a miss.
auto after = impl_of(ws).lookup_v2(ht::http_method::get,
std::string("/volatile"));
LT_CHECK(!after.found);
LT_CHECK(after.tier == ht::detail::webserver_impl::tier_hit::none);
LT_END_AUTO_TEST(unregister_invalidates_cache_entry)
LT_BEGIN_AUTO_TEST_ENV()
AUTORUN_TESTS()
LT_END_AUTO_TEST_ENV()