Make path comparison case-insensitive

This commit is contained in:
jaina heartles 2022-05-19 21:13:04 -07:00
parent 64004cbd82
commit c2be0e9833

View file

@ -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();
}
};