srctree

Gregory Mullen parent 9641cbeb 27d44505
add smokeTest to scan routes

inlinesplit
src/endpoint.zig added: 85, removed: 10, total 75
@@ -44,6 +44,10 @@ pub fn Endpoints(endpoints: anytype) type {
var server = try Server.init(self.alloc, router, options);
try server.serve();
}
 
pub fn smokeTest(a: Allocator) !void {
try testing.smokeTest(a, &Self.routes, .default);
}
};
}
 
@@ -298,3 +302,4 @@ const Frame = @import("frame.zig");
const Auth = @import("auth.zig");
const Server = @import("server.zig");
const Router = @import("router.zig");
const testing = @import("testing.zig");
 
src/request.zig added: 85, removed: 10, total 75
@@ -139,6 +139,13 @@ pub const Methods = enum(u9) {
return error.UnknownMethod;
}
 
pub fn readOnly(m: Methods) bool {
return switch (m) {
.GET, .HEAD, .OPTIONS => true,
.POST, .PUT, .DELETE, .CONNECT, .TRACE, .WEBSOCKET => false,
};
}
 
pub fn format(m: Methods, comptime _: []const u8, _: std.fmt.FormatOptions, w: anytype) !void {
switch (m) {
inline else => |e| try w.writeAll(@tagName(e)),
 
src/router.zig added: 85, removed: 10, total 75
@@ -401,7 +401,7 @@ fn fallbackRouter(frame: *Frame, routefn: RouteFn) BuildFn {
}
 
const root_with_static = root ++ [_]Match{
ROUTE("static", StaticFile.file),
ROUTE("static", StaticFile.fileOnDisk),
};
 
fn defaultRouterHtml(frame: *Frame, routefn: RouteFn) Error!void {
@@ -414,6 +414,12 @@ fn defaultRouterHtml(frame: *Frame, routefn: RouteFn) Error!void {
 
pub const TestingRouter: Router = Routes(&root);
 
test "smoke" {
const a = std.testing.allocator;
try testing.smokeTest(a, &root, .default);
try testing.smokeTest(a, &root_with_static, .default);
}
 
test "uri" {
const uri_file = "/root/first/second/third";
const uri_dir = "/root/first/second/";
@@ -463,3 +469,4 @@ const eql = std.mem.eql;
const Frame = @import("frame.zig");
const Request = @import("request.zig");
const StaticFile = @import("static-file.zig");
const testing = @import("testing.zig");
 
src/testing.zig added: 85, removed: 10, total 75
@@ -1,3 +1,56 @@
pub const SmokeTestOptions = struct {
soft_errors: []const Router.Error,
recurse: bool,
 
pub const default: SmokeTestOptions = .{
.recurse = true,
.soft_errors = &[_]Router.Error{
// By default, the two soft errors are BadData, [because smokeTest
// is unable to generate the default expected data], and Unrouteable
// [for the same reason, smokeTest is unlikely to be able to
// generate the required routing information]
error.BadData,
error.Unrouteable,
},
};
};
 
pub fn smokeTest(
a: Allocator,
comptime routes: []const Router.Match,
comptime opts: SmokeTestOptions,
) !void {
inline for (routes) |route| {
inline for (@typeInfo(Request.Methods).@"enum".fields) |field| {
if (comptime !Request.Methods.readOnly(@enumFromInt(field.value))) continue;
if (comptime route.target(@enumFromInt(field.value))) |trgt| {
switch (trgt) {
.build => |func| {
var fc: FrameCtx = try .init(a);
defer fc.raze(a);
if (func(&fc.frame)) {} else |err| {
for (opts.soft_errors) |serr| {
if (err == serr) break;
} else {
std.debug.print(
"Smoke test error for endpoint Match {s} : {}\n",
.{ route.name, func },
);
return err;
}
}
},
.simple => |smp| {
if (!opts.recurse) continue;
try smokeTest(a, smp, opts);
},
else => {},
}
}
}
}
}
 
pub fn headers() Headers {
return .{
.known = .{},
@@ -8,7 +61,7 @@ pub fn headers() Headers {
const Buffer = std.io.FixedBufferStream([]u8);
const DEFAULT_SIZE = 0x1000000;
 
pub fn request(a: std.mem.Allocator, buf: []u8) *Request {
pub fn request(a: Allocator, buf: []u8) *Request {
const fba = a.create(Buffer) catch @panic("OOM");
fba.* = .{ .buffer = buf, .pos = 0 };
 
@@ -40,12 +93,12 @@ pub const FrameCtx = struct {
frame: Frame,
buffer: []u8,
 
pub fn init(alloc: std.mem.Allocator) !FrameCtx {
pub fn init(alloc: Allocator) !FrameCtx {
var arena = try alloc.create(std.heap.ArenaAllocator);
arena.* = std.heap.ArenaAllocator.init(alloc);
 
const a = arena.allocator();
const buffer = try a.alloc(u8, DEFAULT_SIZE / 0x1000);
const buffer = try a.alloc(u8, DEFAULT_SIZE);
return .{
.arena = arena,
.frame = .{
@@ -125,6 +178,7 @@ test {
}
 
const std = @import("std");
const Allocator = std.mem.Allocator;
const Headers = @import("headers.zig");
const Request = @import("request.zig");
const Frame = @import("frame.zig");
 
src/verse.zig added: 85, removed: 10, total 75
@@ -35,9 +35,11 @@ comptime {
_ = &@This();
}
 
pub const testing = @import("testing.zig");
 
test {
std.testing.refAllDecls(@This());
_ = @import("testing.zig");
_ = &testing;
}
 
const std = @import("std");