chore: started routing tests
parent
386d83ec93
commit
de4e509c9c
|
@ -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);
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#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 ':':
|
||||
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:
|
||||
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]));
|
||||
http_router->represents_route = true;
|
||||
|
||||
if (out != NULL) {
|
||||
*out = http_router->routes[method];
|
||||
}
|
||||
|
||||
return lnm_err_ok;
|
||||
}
|
||||
|
@ -88,9 +99,15 @@ 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
|
||||
if (out != NULL) {
|
||||
*out = router->routes[method];
|
||||
}
|
||||
|
||||
return router->routes[method] == NULL ? lnm_http_route_err_unknown_method
|
||||
: lnm_http_route_err_match;
|
||||
}
|
||||
|
||||
|
|
|
@ -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 }
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue