Make path comparison case-insensitive
This commit is contained in:
parent
64004cbd82
commit
c2be0e9833
1 changed files with 22 additions and 1 deletions
|
@ -1,5 +1,6 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const http = @import("./http.zig");
|
const http = @import("./http.zig");
|
||||||
|
const ciutf8 = @import("./util.zig").ciutf8;
|
||||||
|
|
||||||
pub fn Router(comptime Context: type) type {
|
pub fn Router(comptime Context: type) type {
|
||||||
return struct {
|
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 {
|
pub fn dispatch(self: *const Self, method: http.Method, path: []const u8, ctx: Context) void {
|
||||||
for (self.routes) |r| {
|
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);
|
return r.handler(ctx);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -157,4 +158,24 @@ const _tests = struct {
|
||||||
try mock_b.expectCalledOnceWith(11);
|
try mock_b.expectCalledOnceWith(11);
|
||||||
try mock_404.expectNotCalled();
|
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();
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue