Add http method routing
This commit is contained in:
parent
126ab9a57b
commit
d4d3226a39
1 changed files with 24 additions and 9 deletions
|
@ -1,4 +1,5 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
const http = @import("./http.zig");
|
||||||
|
|
||||||
pub fn Router(comptime Context: type) type {
|
pub fn Router(comptime Context: type) type {
|
||||||
return struct {
|
return struct {
|
||||||
|
@ -7,15 +8,16 @@ pub fn Router(comptime Context: type) type {
|
||||||
|
|
||||||
const Route = struct {
|
const Route = struct {
|
||||||
path: []const u8,
|
path: []const u8,
|
||||||
|
method: http.Method,
|
||||||
handler: H,
|
handler: H,
|
||||||
};
|
};
|
||||||
|
|
||||||
routes: []const Route,
|
routes: []const Route,
|
||||||
route_404: H,
|
route_404: H,
|
||||||
|
|
||||||
pub fn dispatch(self: *const Self, 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 (std.mem.eql(u8, path, r.path)) {
|
if (method == r.method and std.mem.eql(u8, path, r.path)) {
|
||||||
return r.handler(ctx);
|
return r.handler(ctx);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -53,38 +55,51 @@ const _tests = struct {
|
||||||
|
|
||||||
test "Router(T).dispatch" {
|
test "Router(T).dispatch" {
|
||||||
const mock_a = CallTracker(.{}, dummyHandler);
|
const mock_a = CallTracker(.{}, dummyHandler);
|
||||||
|
const mock_a_post = CallTracker(.{}, dummyHandler);
|
||||||
const mock_b = CallTracker(.{}, dummyHandler);
|
const mock_b = CallTracker(.{}, dummyHandler);
|
||||||
const mock_c = CallTracker(.{}, dummyHandler);
|
const mock_c = CallTracker(.{}, dummyHandler);
|
||||||
const mock_404 = CallTracker(.{}, dummyHandler);
|
const mock_404 = CallTracker(.{}, dummyHandler);
|
||||||
|
|
||||||
const R = Router(Context).Route;
|
const R = Router(Context).Route;
|
||||||
const routes = [_]R{
|
const routes = [_]R{
|
||||||
.{ .handler = mock_a.func, .path = "/a" },
|
.{ .handler = mock_a.func, .method = .GET, .path = "/a" },
|
||||||
.{ .handler = mock_b.func, .path = "/b" },
|
.{ .handler = mock_a_post.func, .method = .POST, .path = "/a" },
|
||||||
.{ .handler = mock_c.func, .path = "/c" },
|
.{ .handler = mock_b.func, .method = .GET, .path = "/b" },
|
||||||
|
.{ .handler = mock_c.func, .method = .GET, .path = "/c" },
|
||||||
};
|
};
|
||||||
|
|
||||||
const router = Router(Context){ .routes = &routes, .route_404 = mock_404.func };
|
const router = Router(Context){ .routes = &routes, .route_404 = mock_404.func };
|
||||||
router.dispatch("/a", .{});
|
router.dispatch(.GET, "/a", .{});
|
||||||
try mock_a.expectCalled(1);
|
try mock_a.expectCalled(1);
|
||||||
|
try mock_a_post.expectCalled(0);
|
||||||
try mock_b.expectCalled(0);
|
try mock_b.expectCalled(0);
|
||||||
try mock_c.expectCalled(0);
|
try mock_c.expectCalled(0);
|
||||||
try mock_404.expectCalled(0);
|
try mock_404.expectCalled(0);
|
||||||
|
|
||||||
router.dispatch("/b", .{});
|
router.dispatch(.GET, "/b", .{});
|
||||||
try mock_a.expectCalled(1);
|
try mock_a.expectCalled(1);
|
||||||
|
try mock_a_post.expectCalled(0);
|
||||||
try mock_b.expectCalled(1);
|
try mock_b.expectCalled(1);
|
||||||
try mock_c.expectCalled(0);
|
try mock_c.expectCalled(0);
|
||||||
try mock_404.expectCalled(0);
|
try mock_404.expectCalled(0);
|
||||||
|
|
||||||
router.dispatch("/a", .{});
|
router.dispatch(.GET, "/a", .{});
|
||||||
try mock_a.expectCalled(2);
|
try mock_a.expectCalled(2);
|
||||||
|
try mock_a_post.expectCalled(0);
|
||||||
try mock_b.expectCalled(1);
|
try mock_b.expectCalled(1);
|
||||||
try mock_c.expectCalled(0);
|
try mock_c.expectCalled(0);
|
||||||
try mock_404.expectCalled(0);
|
try mock_404.expectCalled(0);
|
||||||
|
|
||||||
router.dispatch("/abcd", .{});
|
router.dispatch(.GET, "/abcd", .{});
|
||||||
try mock_a.expectCalled(2);
|
try mock_a.expectCalled(2);
|
||||||
|
try mock_a_post.expectCalled(0);
|
||||||
|
try mock_b.expectCalled(1);
|
||||||
|
try mock_c.expectCalled(0);
|
||||||
|
try mock_404.expectCalled(1);
|
||||||
|
|
||||||
|
router.dispatch(.POST, "/a", .{});
|
||||||
|
try mock_a.expectCalled(2);
|
||||||
|
try mock_a_post.expectCalled(1);
|
||||||
try mock_b.expectCalled(1);
|
try mock_b.expectCalled(1);
|
||||||
try mock_c.expectCalled(0);
|
try mock_c.expectCalled(0);
|
||||||
try mock_404.expectCalled(1);
|
try mock_404.expectCalled(1);
|
||||||
|
|
Loading…
Reference in a new issue