Split out Route into separate fn

This commit is contained in:
jaina heartles 2022-05-19 21:25:03 -07:00
parent c2be0e9833
commit 626a7d33b0

View file

@ -2,19 +2,29 @@ const std = @import("std");
const http = @import("./http.zig");
const ciutf8 = @import("./util.zig").ciutf8;
fn RouteWithContext(comptime Context: type) type {
return struct {
const Self = @This();
pub const Handler = fn (Context) void;
path: []const u8,
method: http.Method,
handler: Handler,
pub fn bind(method: http.Method, comptime path: []const u8, handler: Handler) Self {
return .{ .method = method, .path = path, .handler = handler };
}
};
}
pub fn Router(comptime Context: type) type {
return struct {
const Self = @This();
const H = fn (Context) void;
const Route = struct {
path: []const u8,
method: http.Method,
handler: H,
};
pub const Route = RouteWithContext(Context);
routes: []const Route,
route_404: H,
route_404: Route.Handler,
pub fn dispatch(self: *const Self, method: http.Method, path: []const u8, ctx: Context) void {
for (self.routes) |r| {
@ -70,8 +80,8 @@ const _tests = struct {
const R = Router(Context).Route;
const routes = [_]R{
.{ .handler = mock_a.func, .method = .GET, .path = "/a" },
.{ .handler = mock_b.func, .method = .GET, .path = "/b" },
R.bind(.GET, "/a", mock_a.func),
R.bind(.GET, "/b", mock_b.func),
};
const router = Router(Context){ .routes = &routes, .route_404 = mock_404.func };
@ -94,8 +104,8 @@ const _tests = struct {
const R = Router(Context).Route;
const routes = [_]R{
.{ .handler = mock_a.func, .method = .GET, .path = "/a" },
.{ .handler = mock_b.func, .method = .GET, .path = "/b" },
R.bind(.GET, "/a", mock_a.func),
R.bind(.GET, "/b", mock_b.func),
};
const router = Router(Context){ .routes = &routes, .route_404 = mock_404.func };
@ -118,8 +128,8 @@ const _tests = struct {
const R = Router(Context).Route;
const routes = [_]R{
.{ .handler = mock_get.func, .method = .GET, .path = "/a" },
.{ .handler = mock_post.func, .method = .POST, .path = "/a" },
R.bind(.GET, "/a", mock_get.func),
R.bind(.POST, "/a", mock_post.func),
};
const router = Router(Context){ .routes = &routes, .route_404 = mock_404.func };
@ -142,8 +152,8 @@ const _tests = struct {
const R = Router(Context).Route;
const routes = [_]R{
.{ .handler = mock_a.func, .method = .GET, .path = "/a" },
.{ .handler = mock_b.func, .method = .GET, .path = "/a/b" },
R.bind(.GET, "/a", mock_a.func),
R.bind(.GET, "/a/b", mock_b.func),
};
const router = Router(Context){ .routes = &routes, .route_404 = mock_404.func };
@ -165,7 +175,7 @@ const _tests = struct {
const R = Router(Context).Route;
const routes = [_]R{
.{ .handler = mock_a.func, .method = .GET, .path = "/test/a" },
R.bind(.GET, "/test/a", mock_a.func),
};
const router = Router(Context){ .routes = &routes, .route_404 = mock_404.func };
@ -178,4 +188,29 @@ const _tests = struct {
try mock_a.expectCalledOnceWith(11);
try mock_404.expectNotCalled();
}
test "Router(T).dispatch redundant /" {
const mock_a = CallTracker(.{}, dummyHandler);
const mock_404 = CallTracker(.{}, dummyHandler);
const R = Router(Context).Route;
const routes = [_]R{
R.bind(.GET, "/test/a", mock_a.func),
};
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();
mock_a.reset();
router.dispatch(.GET, "test/a/", 12);
try mock_a.expectCalledOnceWith(12);
try mock_404.expectNotCalled();
}
};