| 5 | #include "fl/stl/cstring.h" |
| 6 | |
| 7 | FL_TEST_FILE(FL_FILEPATH) { |
| 8 | |
| 9 | // Helper: wrap a C string as a span for feed() |
| 10 | static fl::span<const uint8_t> asSpan(const char* s) { |
| 11 | return fl::span<const uint8_t>( |
| 12 | reinterpret_cast<const uint8_t*>(s), fl::strlen(s)); // ok reinterpret cast |
| 13 | } |
| 14 | |
| 15 | using namespace fl; |
| 16 | |
| 17 | FL_TEST_CASE("HttpRequestParser - Simple GET request") { |
| 18 | HttpRequestParser parser; |
| 19 | |
| 20 | const char* request = |
| 21 | "GET /hello HTTP/1.1\r\n" |
| 22 | "Host: localhost\r\n" |
| 23 | "\r\n"; |
| 24 | |
| 25 | parser.feed(asSpan(request)); |
| 26 | |
| 27 | FL_CHECK(parser.isComplete()); |
| 28 | |
| 29 | auto req = parser.getRequest(); |
| 30 | FL_REQUIRE(req); |
| 31 | FL_CHECK(req->method == "GET"); |
| 32 | FL_CHECK(req->uri == "/hello"); |
| 33 | FL_CHECK(req->version == "HTTP/1.1"); |
| 34 | FL_CHECK(req->headers.size() == 1); |
| 35 | FL_CHECK(req->headers.at("Host") == "localhost"); |
| 36 | FL_CHECK(req->body.empty()); |
| 37 | } |
| 38 | |
| 39 | FL_TEST_CASE("HttpRequestParser - POST with Content-Length") { |
| 40 | HttpRequestParser parser; |
| 41 | |
| 42 | const char* request = |
| 43 | "POST /rpc HTTP/1.1\r\n" |
| 44 | "Host: localhost:8080\r\n" |
| 45 | "Content-Type: application/json\r\n" |
| 46 | "Content-Length: 13\r\n" |
| 47 | "\r\n" |
| 48 | "{\"test\": 123}"; // Fixed: Added space to match Content-Length: 13 |
| 49 | |
| 50 | parser.feed(asSpan(request)); |
| 51 | |
| 52 | FL_CHECK(parser.isComplete()); |
| 53 | |
| 54 | auto req = parser.getRequest(); |
| 55 | FL_REQUIRE(req); |
| 56 | FL_CHECK(req->method == "POST"); |
| 57 | FL_CHECK(req->uri == "/rpc"); |
| 58 | FL_CHECK(req->version == "HTTP/1.1"); |
| 59 | FL_CHECK(req->headers.size() == 3); |
| 60 | FL_CHECK(req->headers.at("Content-Type") == "application/json"); |
| 61 | FL_CHECK(req->headers.at("Content-Length") == "13"); |
| 62 | FL_CHECK(req->body.size() == 13); |
| 63 | |
| 64 | fl::string body(reinterpret_cast<const char*>(req->body.data()), req->body.size()); |
nothing calls this directly
no test coverage detected