|
| 1 | +/* |
| 2 | + This file is part of libhttpserver |
| 3 | + Copyright (C) 2011-2026 Sebastiano Merlino |
| 4 | +*/ |
| 5 | + |
| 6 | +// TASK-071: end-to-end pin for the not_found_handler -> route_resolved |
| 7 | +// alias seat. |
| 8 | +// |
| 9 | +// Sub-item A from TASK-071 — replace the empty stub callback in |
| 10 | +// webserver::install_not_found_alias_ with the wired observation hook |
| 11 | +// documented per DR-012 §4.10. The on-wire 404 body still flows through |
| 12 | +// webserver_impl::not_found_page (route_resolved is observation-only; |
| 13 | +// the response slot is not exposed at this phase), but the alias seat |
| 14 | +// in the bus must be a real hook so: |
| 15 | +// |
| 16 | +// (1) PRD-HOOK-REQ-009: v1 setters expose themselves as hook-bus |
| 17 | +// subscriptions (verified by hooks_alias_count_test already; we |
| 18 | +// cross-pin the on-wire behaviour here). |
| 19 | +// (2) AC-1: a fresh webserver with no explicit not_found_handler |
| 20 | +// returns 404 with the canonical "Not Found" body. |
| 21 | +// (3) AC-1 (custom branch): a webserver with a user not_found_handler |
| 22 | +// returns 404 with the user-supplied body on a miss path. |
| 23 | +// (4) DR-012 §4.10 ordering pin: user-registered route_resolved hooks |
| 24 | +// still fire alongside the alias seat (observation phase is not |
| 25 | +// short-circuit-capable). A user observation hook must still see |
| 26 | +// the miss. |
| 27 | + |
| 28 | +#include <curl/curl.h> |
| 29 | + |
| 30 | +#include <atomic> |
| 31 | +#include <chrono> |
| 32 | +#include <functional> |
| 33 | +#include <memory> |
| 34 | +#include <string> |
| 35 | +#include <thread> |
| 36 | + |
| 37 | +#include "./httpserver.hpp" |
| 38 | +#include "./littletest.hpp" |
| 39 | + |
| 40 | +using httpserver::create_webserver; |
| 41 | +using httpserver::hook_phase; |
| 42 | +using httpserver::http_request; |
| 43 | +using httpserver::http_response; |
| 44 | +using httpserver::route_resolved_ctx; |
| 45 | +using httpserver::webserver; |
| 46 | + |
| 47 | +#define PORT_DEFAULT 8211 |
| 48 | +#define PORT_CUSTOM 8212 |
| 49 | +#define PORT_ORDER 8213 |
| 50 | + |
| 51 | +namespace { |
| 52 | + |
| 53 | +size_t writefunc(void* ptr, size_t size, size_t nmemb, std::string* s) { |
| 54 | + s->append(reinterpret_cast<char*>(ptr), size * nmemb); |
| 55 | + return size * nmemb; |
| 56 | +} |
| 57 | + |
| 58 | +CURLcode get_url(int port, const std::string& path, |
| 59 | + long* http_code, // NOLINT(runtime/int) |
| 60 | + std::string* body) { |
| 61 | + CURL* curl = curl_easy_init(); |
| 62 | + if (curl == nullptr) return CURLE_FAILED_INIT; |
| 63 | + std::string url = "http://127.0.0.1:" + std::to_string(port) + path; |
| 64 | + curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); |
| 65 | + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); |
| 66 | + curl_easy_setopt(curl, CURLOPT_WRITEDATA, body); |
| 67 | + CURLcode rc = curl_easy_perform(curl); |
| 68 | + if (http_code != nullptr) { |
| 69 | + curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, http_code); |
| 70 | + } |
| 71 | + curl_easy_cleanup(curl); |
| 72 | + return rc; |
| 73 | +} |
| 74 | + |
| 75 | +} // namespace |
| 76 | + |
| 77 | +LT_BEGIN_SUITE(hooks_not_found_alias_suite) |
| 78 | + void set_up() {} |
| 79 | + void tear_down() {} |
| 80 | +LT_END_SUITE(hooks_not_found_alias_suite) |
| 81 | + |
| 82 | +// AC-1, default branch. With no explicit not_found_handler, a request |
| 83 | +// for a missing route returns 404 with the canonical "Not Found" body. |
| 84 | +// Pinned end-to-end so any future structural refactor of |
| 85 | +// install_not_found_alias_ cannot silently regress the wire contract. |
| 86 | +LT_BEGIN_AUTO_TEST(hooks_not_found_alias_suite, |
| 87 | + not_found_alias_default_body_when_no_user_handler) |
| 88 | + webserver ws{create_webserver(PORT_DEFAULT)}; |
| 89 | + ws.start(false); |
| 90 | + std::this_thread::sleep_for(std::chrono::milliseconds(50)); |
| 91 | + |
| 92 | + long code = 0; // NOLINT(runtime/int) |
| 93 | + std::string body; |
| 94 | + LT_CHECK_EQ(get_url(PORT_DEFAULT, "/nonexistent", &code, &body), CURLE_OK); |
| 95 | + |
| 96 | + ws.stop(); |
| 97 | + |
| 98 | + LT_CHECK_EQ(code, 404L); |
| 99 | + // constants::NOT_FOUND_ERROR == "Not Found" (src/httpserver/constants.hpp:51). |
| 100 | + LT_CHECK_EQ(body, std::string("Not Found")); |
| 101 | +LT_END_AUTO_TEST(not_found_alias_default_body_when_no_user_handler) |
| 102 | + |
| 103 | +// AC-1, custom branch. With an explicit not_found_handler, a request for |
| 104 | +// a missing route returns 404 with the user-supplied body. The alias |
| 105 | +// seat is wired (verified separately by hooks_alias_count_test), and the |
| 106 | +// on-wire 404 body flows through webserver_impl::not_found_page calling |
| 107 | +// the same handler — the user handler is invoked exactly once per miss |
| 108 | +// (not double-counted by the alias hook body, which is observation-only). |
| 109 | +LT_BEGIN_AUTO_TEST(hooks_not_found_alias_suite, |
| 110 | + not_found_alias_invokes_user_handler_on_miss_with_v1_body) |
| 111 | + std::atomic<int> handler_calls{0}; |
| 112 | + auto user_not_found = [&handler_calls](const http_request&) { |
| 113 | + handler_calls.fetch_add(1, std::memory_order_relaxed); |
| 114 | + return http_response::string("CUSTOM_404").with_status(404); |
| 115 | + }; |
| 116 | + |
| 117 | + webserver ws{create_webserver(PORT_CUSTOM).not_found_handler(user_not_found)}; |
| 118 | + ws.start(false); |
| 119 | + std::this_thread::sleep_for(std::chrono::milliseconds(50)); |
| 120 | + |
| 121 | + long code = 0; // NOLINT(runtime/int) |
| 122 | + std::string body; |
| 123 | + LT_CHECK_EQ(get_url(PORT_CUSTOM, "/nonexistent", &code, &body), CURLE_OK); |
| 124 | + |
| 125 | + ws.stop(); |
| 126 | + |
| 127 | + LT_CHECK_EQ(code, 404L); |
| 128 | + LT_CHECK_EQ(body, std::string("CUSTOM_404")); |
| 129 | + // The user handler fires exactly once. The alias hook body is a |
| 130 | + // pure observation marker (DR-012 §4.10); it does NOT re-invoke |
| 131 | + // the user handler. The on-wire body comes from not_found_page. |
| 132 | + LT_CHECK_EQ(handler_calls.load(), 1); |
| 133 | +LT_END_AUTO_TEST(not_found_alias_invokes_user_handler_on_miss_with_v1_body) |
| 134 | + |
| 135 | +// DR-012 §4.10 ordering pin. The alias seat installs hook[0] at |
| 136 | +// route_resolved when not_found_handler is set; a user-added observation |
| 137 | +// hook becomes hook[1]. Both fire on a miss (observation phase is not |
| 138 | +// short-circuit-capable). Cross-pins that the alias upgrade did not |
| 139 | +// accidentally suppress co-registered user hooks. |
| 140 | +LT_BEGIN_AUTO_TEST(hooks_not_found_alias_suite, |
| 141 | + not_found_alias_does_not_suppress_user_route_resolved_hook) |
| 142 | + std::atomic<int> user_observer_calls{0}; |
| 143 | + std::atomic<int> user_observer_misses{0}; |
| 144 | + auto user_not_found = [](const http_request&) { |
| 145 | + return http_response::string("CUSTOM_404").with_status(404); |
| 146 | + }; |
| 147 | + |
| 148 | + webserver ws{create_webserver(PORT_ORDER).not_found_handler(user_not_found)}; |
| 149 | + |
| 150 | + auto h = ws.add_hook(hook_phase::route_resolved, |
| 151 | + std::function<void(const route_resolved_ctx&)>( |
| 152 | + [&user_observer_calls, &user_observer_misses] |
| 153 | + (const route_resolved_ctx& ctx) { |
| 154 | + user_observer_calls.fetch_add(1, std::memory_order_relaxed); |
| 155 | + if (!ctx.matched.has_value()) { |
| 156 | + user_observer_misses.fetch_add(1, |
| 157 | + std::memory_order_relaxed); |
| 158 | + } |
| 159 | + })); |
| 160 | + |
| 161 | + ws.start(false); |
| 162 | + std::this_thread::sleep_for(std::chrono::milliseconds(50)); |
| 163 | + |
| 164 | + long code = 0; // NOLINT(runtime/int) |
| 165 | + std::string body; |
| 166 | + LT_CHECK_EQ(get_url(PORT_ORDER, "/nonexistent", &code, &body), CURLE_OK); |
| 167 | + |
| 168 | + ws.stop(); |
| 169 | + |
| 170 | + LT_CHECK_EQ(code, 404L); |
| 171 | + LT_CHECK_EQ(body, std::string("CUSTOM_404")); |
| 172 | + // User observation hook fired once on the miss path. |
| 173 | + LT_CHECK_EQ(user_observer_calls.load(), 1); |
| 174 | + LT_CHECK_EQ(user_observer_misses.load(), 1); |
| 175 | +LT_END_AUTO_TEST(not_found_alias_does_not_suppress_user_route_resolved_hook) |
| 176 | + |
| 177 | +LT_BEGIN_AUTO_TEST_ENV() |
| 178 | + AUTORUN_TESTS() |
| 179 | +LT_END_AUTO_TEST_ENV() |
0 commit comments