From 7aade76c99613027d6c4dc2b78efb6373410cbd4 Mon Sep 17 00:00:00 2001 From: Karl Seguin Date: Mon, 16 Sep 2024 11:38:45 +0800 Subject: [PATCH] empty URL shouldn't be possible, move check to an assert --- src/router.zig | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/router.zig b/src/router.zig index d60f10a..0b53e34 100644 --- a/src/router.zig +++ b/src/router.zig @@ -410,7 +410,8 @@ pub fn Part(comptime A: type) type { } fn getRoute(comptime A: type, root: *const Part(A), url: []const u8, params: *Params) ?A { - if (url.len == 0 or (url.len == 1 and url[0] == '/')) { + std.debug.assert(url.len != 0); + if (url.len == 1 and url[0] == '/') { return root.action; } @@ -478,17 +479,12 @@ test "route: root" { router.get("/", testRoute1, .{}); router.put("/", testRoute2, .{}); - router.post("", testRoute3, .{}); router.all("/all", testRoute4, .{}); const urls = .{ "/", "/other", "/all" }; - try t.expectEqual(&testRoute1, router.route(httpz.Method.GET, "", ¶ms).?.action); - try t.expectEqual(&testRoute2, router.route(httpz.Method.PUT, "", ¶ms).?.action); - try t.expectEqual(&testRoute3, router.route(httpz.Method.POST, "", ¶ms).?.action); try t.expectEqual(&testRoute1, router.route(httpz.Method.GET, urls[0], ¶ms).?.action); try t.expectEqual(&testRoute2, router.route(httpz.Method.PUT, urls[0], ¶ms).?.action); - try t.expectEqual(&testRoute3, router.route(httpz.Method.POST, urls[0], ¶ms).?.action); try t.expectEqual(null, router.route(httpz.Method.GET, urls[1], ¶ms)); try t.expectEqual(null, router.route(httpz.Method.DELETE, urls[0], ¶ms));