|
| 1 | +/* |
| 2 | + This file is part of libhttpserver |
| 3 | + Copyright (C) 2011-2026 Sebastiano Merlino |
| 4 | +
|
| 5 | + This library is free software; you can redistribute it and/or |
| 6 | + modify it under the terms of the GNU Lesser General Public |
| 7 | + License as published by the Free Software Foundation; either |
| 8 | + version 2.1 of the License, or (at your option) any later version. |
| 9 | +*/ |
| 10 | + |
| 11 | +// TASK-018: compile-time guarantees of http_request's per-key and |
| 12 | +// always-present getters. |
| 13 | +// |
| 14 | +// We assert the structural invariants TASK-018 owns: |
| 15 | +// 1. Every per-key getter (get_header / get_cookie / get_footer / |
| 16 | +// get_arg / get_arg_flat) is callable on `const http_request&` with |
| 17 | +// a `std::string_view` key. |
| 18 | +// 2. Every always-present getter (get_path / get_method / get_version |
| 19 | +// / get_content / get_querystring) is callable on `const |
| 20 | +// http_request&` and returns `std::string_view`. |
| 21 | +// 3. The five always-present getters above are `noexcept`. The |
| 22 | +// string_view-of-std::string conversion is itself noexcept since |
| 23 | +// C++17, so every getter that just hands back a member's string can |
| 24 | +// lock that contract in. |
| 25 | +// 4. Return-type lockdowns: every per-key getter returns |
| 26 | +// `std::string_view`, except `get_arg` which deliberately returns |
| 27 | +// `httpserver::http_arg_value` to preserve multi-value semantics |
| 28 | +// (see TASK-018 plan section 4 for the rationale). |
| 29 | +// |
| 30 | +// The intent of these static_asserts is to catch silent regressions: |
| 31 | +// e.g. if a future refactor narrows `get_arg`'s signature without |
| 32 | +// adjusting the multi-value tests, or if someone strips `noexcept` off |
| 33 | +// `get_path`, the build breaks at compile time rather than at the next |
| 34 | +// integration-test failure. |
| 35 | + |
| 36 | +// HTTPSERVER_COMPILATION is supplied by test/Makefile.am AM_CPPFLAGS. |
| 37 | +#include "httpserver/http_request.hpp" |
| 38 | +#include "httpserver/http_arg_value.hpp" |
| 39 | + |
| 40 | +#include <string_view> |
| 41 | +#include <type_traits> |
| 42 | +#include <utility> |
| 43 | + |
| 44 | +namespace { |
| 45 | + |
| 46 | +using h = httpserver::http_request; |
| 47 | +using cref = const h&; |
| 48 | + |
| 49 | +// (1) Const-callable invocability for every per-key getter. |
| 50 | +static_assert(std::is_invocable_v<decltype(&h::get_header), cref, std::string_view>, |
| 51 | + "get_header must be invocable on const http_request& with string_view"); |
| 52 | +static_assert(std::is_invocable_v<decltype(&h::get_cookie), cref, std::string_view>, |
| 53 | + "get_cookie must be invocable on const http_request& with string_view"); |
| 54 | +static_assert(std::is_invocable_v<decltype(&h::get_footer), cref, std::string_view>, |
| 55 | + "get_footer must be invocable on const http_request& with string_view"); |
| 56 | +static_assert(std::is_invocable_v<decltype(&h::get_arg), cref, std::string_view>, |
| 57 | + "get_arg must be invocable on const http_request& with string_view"); |
| 58 | +static_assert(std::is_invocable_v<decltype(&h::get_arg_flat), cref, std::string_view>, |
| 59 | + "get_arg_flat must be invocable on const http_request& with string_view"); |
| 60 | + |
| 61 | +// (2) Const-callable invocability for every always-present getter. |
| 62 | +static_assert(std::is_invocable_v<decltype(&h::get_path), cref>, |
| 63 | + "get_path must be invocable on const http_request&"); |
| 64 | +static_assert(std::is_invocable_v<decltype(&h::get_method), cref>, |
| 65 | + "get_method must be invocable on const http_request&"); |
| 66 | +static_assert(std::is_invocable_v<decltype(&h::get_version), cref>, |
| 67 | + "get_version must be invocable on const http_request&"); |
| 68 | +static_assert(std::is_invocable_v<decltype(&h::get_content), cref>, |
| 69 | + "get_content must be invocable on const http_request&"); |
| 70 | +static_assert(std::is_invocable_v<decltype(&h::get_querystring), cref>, |
| 71 | + "get_querystring must be invocable on const http_request&"); |
| 72 | + |
| 73 | +// (3) `noexcept` lockdowns for the five always-present getters. |
| 74 | +static_assert(noexcept(std::declval<cref>().get_path()), |
| 75 | + "get_path must be noexcept"); |
| 76 | +static_assert(noexcept(std::declval<cref>().get_method()), |
| 77 | + "get_method must be noexcept"); |
| 78 | +static_assert(noexcept(std::declval<cref>().get_version()), |
| 79 | + "get_version must be noexcept"); |
| 80 | +static_assert(noexcept(std::declval<cref>().get_content()), |
| 81 | + "get_content must be noexcept"); |
| 82 | +static_assert(noexcept(std::declval<cref>().get_querystring()), |
| 83 | + "get_querystring must be noexcept"); |
| 84 | + |
| 85 | +// (4) Return-type lockdowns: per-key getters narrow to string_view, |
| 86 | +// except get_arg which preserves multi-value semantics via http_arg_value. |
| 87 | +static_assert(std::is_same_v< |
| 88 | + decltype(std::declval<cref>().get_header(std::string_view{})), |
| 89 | + std::string_view>, |
| 90 | + "get_header must return std::string_view"); |
| 91 | +static_assert(std::is_same_v< |
| 92 | + decltype(std::declval<cref>().get_cookie(std::string_view{})), |
| 93 | + std::string_view>, |
| 94 | + "get_cookie must return std::string_view"); |
| 95 | +static_assert(std::is_same_v< |
| 96 | + decltype(std::declval<cref>().get_footer(std::string_view{})), |
| 97 | + std::string_view>, |
| 98 | + "get_footer must return std::string_view"); |
| 99 | +static_assert(std::is_same_v< |
| 100 | + decltype(std::declval<cref>().get_arg_flat(std::string_view{})), |
| 101 | + std::string_view>, |
| 102 | + "get_arg_flat must return std::string_view"); |
| 103 | +static_assert(std::is_same_v< |
| 104 | + decltype(std::declval<cref>().get_arg(std::string_view{})), |
| 105 | + httpserver::http_arg_value>, |
| 106 | + "get_arg must return http_arg_value (multi-value semantics)"); |
| 107 | + |
| 108 | +// Always-present getters all return string_view. |
| 109 | +static_assert(std::is_same_v< |
| 110 | + decltype(std::declval<cref>().get_path()), |
| 111 | + std::string_view>, |
| 112 | + "get_path must return std::string_view"); |
| 113 | +static_assert(std::is_same_v< |
| 114 | + decltype(std::declval<cref>().get_method()), |
| 115 | + std::string_view>, |
| 116 | + "get_method must return std::string_view"); |
| 117 | +static_assert(std::is_same_v< |
| 118 | + decltype(std::declval<cref>().get_version()), |
| 119 | + std::string_view>, |
| 120 | + "get_version must return std::string_view"); |
| 121 | +static_assert(std::is_same_v< |
| 122 | + decltype(std::declval<cref>().get_content()), |
| 123 | + std::string_view>, |
| 124 | + "get_content must return std::string_view"); |
| 125 | +static_assert(std::is_same_v< |
| 126 | + decltype(std::declval<cref>().get_querystring()), |
| 127 | + std::string_view>, |
| 128 | + "get_querystring must return std::string_view"); |
| 129 | + |
| 130 | +} // namespace |
| 131 | + |
| 132 | +int main() { return 0; } |
0 commit comments