From c2be0e98338f3178738afd240e2463c6ebaaa7a3 Mon Sep 17 00:00:00 2001 From: jaina heartles Date: Thu, 19 May 2022 21:13:04 -0700 Subject: [PATCH] Make path comparison case-insensitive --- src/router.zig | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/router.zig b/src/router.zig index 0db0af0..82acc13 100644 --- a/src/router.zig +++ b/src/router.zig @@ -1,5 +1,6 @@ const std = @import("std"); const http = @import("./http.zig"); +const ciutf8 = @import("./util.zig").ciutf8; pub fn Router(comptime Context: type) type { return struct { @@ -17,7 +18,7 @@ pub fn Router(comptime Context: type) type { pub fn dispatch(self: *const Self, method: http.Method, path: []const u8, ctx: Context) void { for (self.routes) |r| { - if (method == r.method and std.mem.eql(u8, path, r.path)) { + if (method == r.method and ciutf8.eql(path, r.path)) { return r.handler(ctx); } } @@ -157,4 +158,24 @@ const _tests = struct { try mock_b.expectCalledOnceWith(11); try mock_404.expectNotCalled(); } + + test "Router(T).dispatch case-insensitive route" { + const mock_a = CallTracker(.{}, dummyHandler); + const mock_404 = CallTracker(.{}, dummyHandler); + + const R = Router(Context).Route; + const routes = [_]R{ + .{ .handler = mock_a.func, .method = .GET, .path = "/test/a" }, + }; + + const router = Router(Context){ .routes = &routes, .route_404 = mock_404.func }; + router.dispatch(.GET, "/TEST/A", 10); + try mock_a.expectCalledOnceWith(10); + try mock_404.expectNotCalled(); + mock_a.reset(); + + router.dispatch(.GET, "/TesT/a", 11); + try mock_a.expectCalledOnceWith(11); + try mock_404.expectNotCalled(); + } };