From de4e509c9ce82d2d1376f7b15fe0a08dfb736703 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Thu, 22 Feb 2024 18:17:26 +0100 Subject: [PATCH] chore: started routing tests --- src/_include/lnm/http/router_internal.h | 1 + src/http/lnm_http_router.c | 29 ++++++++++++++++++++----- test/routing.c | 16 ++++++++++++++ 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/src/_include/lnm/http/router_internal.h b/src/_include/lnm/http/router_internal.h index a2504fa..b98b80f 100644 --- a/src/_include/lnm/http/router_internal.h +++ b/src/_include/lnm/http/router_internal.h @@ -11,6 +11,7 @@ struct lnm_http_router { struct lnm_http_router *exact_children[128]; struct lnm_http_router *single_segment_child; lnm_http_route *routes[lnm_http_method_total]; + bool represents_route; }; lnm_err lnm_http_route_init(lnm_http_route **out); diff --git a/src/http/lnm_http_router.c b/src/http/lnm_http_router.c index 6dee3c8..0fb127f 100644 --- a/src/http/lnm_http_router.c +++ b/src/http/lnm_http_router.c @@ -1,3 +1,4 @@ +#include #include #include "lnm/common.h" @@ -51,7 +52,10 @@ lnm_err lnm_http_router_add(lnm_http_route **out, lnm_http_router *http_router, switch (c) { case ':': - LNM_RES(lnm_http_router_init(&http_router->single_segment_child)); + if (http_router->single_segment_child == NULL) { + LNM_RES(lnm_http_router_init(&http_router->single_segment_child)); + } + http_router = http_router->single_segment_child; // All other characters in the segment are ignored @@ -62,7 +66,10 @@ lnm_err lnm_http_router_add(lnm_http_route **out, lnm_http_router *http_router, // TODO multi-segment wildcard break; default: - LNM_RES(lnm_http_router_init(&http_router->exact_children[c])); + if (http_router->exact_children[c] == NULL) { + LNM_RES(lnm_http_router_init(&http_router->exact_children[c])); + } + http_router = http_router->exact_children[c]; path++; break; @@ -74,7 +81,11 @@ lnm_err lnm_http_router_add(lnm_http_route **out, lnm_http_router *http_router, } LNM_RES(lnm_http_route_init(&http_router->routes[method])); - *out = http_router->routes[method]; + http_router->represents_route = true; + + if (out != NULL) { + *out = http_router->routes[method]; + } return lnm_err_ok; } @@ -88,10 +99,16 @@ lnm_http_route_err lnm_http_router_route(const lnm_http_route **out, } if (*path == '\0') { - *out = router->routes[method]; + if (!router->represents_route) { + return lnm_http_route_err_unknown_route; + } - return *out == NULL ? lnm_http_route_err_unknown_method - : lnm_http_route_err_match; + if (out != NULL) { + *out = router->routes[method]; + } + + return router->routes[method] == NULL ? lnm_http_route_err_unknown_method + : lnm_http_route_err_match; } lnm_http_route_err res = lnm_http_route_err_unknown_route; diff --git a/test/routing.c b/test/routing.c index acab1cc..a42923e 100644 --- a/test/routing.c +++ b/test/routing.c @@ -1,5 +1,21 @@ #include "test.h" +#include "lnm/http/router.h" + +void test_routing_simple() { + lnm_http_router *router; + lnm_http_router_init(&router); + + lnm_http_router_add(NULL, router, lnm_http_method_get, "/test"); + lnm_http_router_add(NULL, router, lnm_http_method_get, "/test/test2"); + + TEST_CHECK(lnm_http_router_route(NULL, router, lnm_http_method_get, "/test") == lnm_http_route_err_match); + TEST_CHECK(lnm_http_router_route(NULL, router, lnm_http_method_get, "/test/te") == lnm_http_route_err_unknown_route); + TEST_CHECK(lnm_http_router_route(NULL, router, lnm_http_method_head, "/test/test2") == lnm_http_route_err_unknown_method); + TEST_CHECK(lnm_http_router_route(NULL, router, lnm_http_method_get, "/test/test2") == lnm_http_route_err_match); +} + TEST_LIST = { + { "routing simple", test_routing_simple }, { NULL, NULL } };