#include "lnm/common.h" #include "lnm/http/route.h" #include "test.h" #include "lnm/http/loop.h" void test_routing_simple() { 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, "/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); 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, "/test2/t/e") == 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); 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); const lnm_http_route_match_segment *segment; TEST_CHECK((segment = lnm_http_route_match_get(&match, "second")) != NULL); TEST_CHECK(segment->start == 15); TEST_CHECK(segment->len == 9); TEST_CHECK((segment = lnm_http_route_match_get(&match, "yuhh")) == NULL); TEST_CHECK((segment = lnm_http_route_match_get(&match, "hello")) != NULL); TEST_CHECK(segment->start == 6); TEST_CHECK(segment->len == 8); lnm_http_router_free(router); } 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); lnm_http_router_free(router); } void test_routing_merge() { lnm_http_router *rtr1, *rtr2; lnm_http_route *rt1, *rt2; lnm_http_route_match match; TEST_CHECK(lnm_http_router_init(&rtr1) == lnm_err_ok); TEST_CHECK(lnm_http_router_init(&rtr2) == lnm_err_ok); TEST_CHECK(lnm_http_router_add(&rt1, rtr1, lnm_http_method_get, "/*key") == lnm_err_ok); TEST_CHECK(lnm_http_router_add(NULL, rtr1, lnm_http_method_get, "/:key/hello") == lnm_err_ok); TEST_CHECK(lnm_http_router_add(&rt2, rtr2, lnm_http_method_get, "/test2") == lnm_err_ok); TEST_CHECK(lnm_http_router_add(NULL, rtr2, lnm_http_method_get, "/:key/hello2") == lnm_err_ok); TEST_CHECK(lnm_http_router_merge(rtr1, rtr2) == lnm_err_ok); TEST_CHECK(lnm_http_router_route(&match, rtr1, lnm_http_method_get, "/some/thing") == lnm_http_route_err_match); TEST_CHECK(match.route == rt1); TEST_CHECK(lnm_http_router_route(&match, rtr1, lnm_http_method_get, "/test2") == lnm_http_route_err_match); TEST_CHECK(match.route == rt2); lnm_http_router_free(rtr1); } void test_routing_nest() { lnm_http_router *r1, *r2; lnm_http_route_match match; TEST_CHECK(lnm_http_router_init(&r1) == lnm_err_ok); TEST_CHECK(lnm_http_router_init(&r2) == lnm_err_ok); TEST_CHECK(lnm_http_router_add(NULL, r1, lnm_http_method_get, "/*key") == lnm_err_ok); TEST_CHECK(lnm_http_router_add(NULL, r2, lnm_http_method_get, "/test/test2") == lnm_err_ok); TEST_CHECK(lnm_http_router_nest(r2, r1, "/test") == lnm_err_ok); TEST_CHECK(lnm_http_router_route(&match, r2, 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 == 18); TEST_CHECK(lnm_http_router_route(&match, r2, lnm_http_method_get, "/test/test2") == lnm_http_route_err_match); lnm_http_router_free(r2); } TEST_LIST = { { "routing simple", test_routing_simple }, { "routing star", test_routing_star }, { "routing merge", test_routing_merge }, { "routing nest", test_routing_nest }, { NULL, NULL } };