2024-02-22 17:43:30 +01:00
|
|
|
#include "test.h"
|
|
|
|
|
2024-02-23 15:21:23 +01:00
|
|
|
#include "lnm/http/loop.h"
|
2024-02-22 18:17:26 +01:00
|
|
|
|
|
|
|
void test_routing_simple() {
|
|
|
|
lnm_http_router *router;
|
2024-02-24 13:25:06 +01:00
|
|
|
TEST_CHECK(lnm_http_router_init(&router) == lnm_err_ok);
|
2024-02-22 18:17:26 +01:00
|
|
|
|
2024-02-22 22:15:19 +01:00
|
|
|
TEST_CHECK(lnm_http_router_add(NULL, router, lnm_http_method_get, "/test") == lnm_err_ok);
|
|
|
|
TEST_CHECK(lnm_http_router_add(NULL, router, lnm_http_method_get, "/test/test2") == lnm_err_ok);
|
|
|
|
TEST_CHECK(lnm_http_router_add(NULL, router, lnm_http_method_get, "/test/:hello") == lnm_err_ok);
|
|
|
|
TEST_CHECK(lnm_http_router_add(NULL, router, lnm_http_method_get, "/test/:hello/:second") == lnm_err_ok);
|
2024-02-22 18:17:26 +01:00
|
|
|
|
|
|
|
TEST_CHECK(lnm_http_router_route(NULL, router, lnm_http_method_get, "/test") == lnm_http_route_err_match);
|
2024-02-22 22:15:19 +01:00
|
|
|
TEST_CHECK(lnm_http_router_route(NULL, router, lnm_http_method_get, "/test2/t/e") == lnm_http_route_err_unknown_route);
|
2024-02-22 18:17:26 +01:00
|
|
|
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);
|
2024-02-22 22:15:19 +01:00
|
|
|
|
|
|
|
lnm_http_route_match match;
|
|
|
|
TEST_CHECK(lnm_http_router_route(&match, router, lnm_http_method_get, "/test/test_var") == lnm_http_route_err_match);
|
|
|
|
TEST_CHECK(match.key_segments[0].start == 6);
|
|
|
|
TEST_CHECK(match.key_segments[0].len == 8);
|
|
|
|
|
|
|
|
TEST_CHECK(lnm_http_router_route(NULL, router, lnm_http_method_get, "/test/") == lnm_http_route_err_unknown_route);
|
|
|
|
|
|
|
|
TEST_CHECK(lnm_http_router_route(&match, router, lnm_http_method_get, "/test/test_var/secondvar") == lnm_http_route_err_match);
|
|
|
|
TEST_CHECK(match.key_segments[0].start == 6);
|
|
|
|
TEST_CHECK(match.key_segments[0].len == 8);
|
|
|
|
TEST_CHECK(match.key_segments[1].start == 15);
|
|
|
|
TEST_CHECK(match.key_segments[1].len == 9);
|
2024-02-22 22:31:00 +01:00
|
|
|
|
2024-02-23 11:38:52 +01:00
|
|
|
const lnm_http_route_match_segment *segment;
|
|
|
|
TEST_CHECK((segment = lnm_http_route_match_get(&match, "second")) != NULL);
|
2024-02-22 22:31:00 +01:00
|
|
|
TEST_CHECK(segment->start == 15);
|
|
|
|
TEST_CHECK(segment->len == 9);
|
2024-02-23 11:38:52 +01:00
|
|
|
TEST_CHECK((segment = lnm_http_route_match_get(&match, "yuhh")) == NULL);
|
|
|
|
TEST_CHECK((segment = lnm_http_route_match_get(&match, "hello")) != NULL);
|
2024-02-22 22:31:00 +01:00
|
|
|
TEST_CHECK(segment->start == 6);
|
|
|
|
TEST_CHECK(segment->len == 8);
|
2024-02-24 13:25:06 +01:00
|
|
|
|
|
|
|
lnm_http_router_free(router);
|
2024-02-22 18:17:26 +01:00
|
|
|
}
|
|
|
|
|
2024-02-26 22:32:50 +01:00
|
|
|
void test_routing_star() {
|
|
|
|
lnm_http_router *router;
|
|
|
|
TEST_CHECK(lnm_http_router_init(&router) == lnm_err_ok);
|
|
|
|
|
|
|
|
TEST_CHECK(lnm_http_router_add(NULL, router, lnm_http_method_get, "/*key") == lnm_err_ok);
|
|
|
|
|
|
|
|
lnm_http_route_match match;
|
|
|
|
TEST_CHECK(lnm_http_router_route(&match, router, lnm_http_method_get, "/hello/world") == lnm_http_route_err_match);
|
|
|
|
TEST_CHECK(match.key_segments[0].start == 1);
|
|
|
|
TEST_CHECK(match.key_segments[0].len == 11);
|
|
|
|
}
|
|
|
|
|
2024-02-22 17:43:30 +01:00
|
|
|
TEST_LIST = {
|
2024-02-22 18:17:26 +01:00
|
|
|
{ "routing simple", test_routing_simple },
|
2024-02-26 22:32:50 +01:00
|
|
|
{ "routing star", test_routing_star },
|
2024-02-22 17:43:30 +01:00
|
|
|
{ NULL, NULL }
|
|
|
|
};
|