Skip to content

Commit

Permalink
fix(tests): restructure our unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mookums committed Nov 26, 2024
1 parent 608ccfe commit d02060c
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 59 deletions.
5 changes: 3 additions & 2 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ pub fn build(b: *std.Build) void {
add_example(b, "valgrind", true, target, optimize, zzz, tardy);

const tests = b.addTest(.{
.name = "tests",
.root_source_file = b.path("./src/test.zig"),
.name = "unit-test",
.root_source_file = b.path("./src/unit_test.zig"),
});
tests.root_module.addImport("tardy", tardy);
tests.root_module.linkLibrary(bearssl);

const run_test = b.addRunArtifact(tests);
run_test.step.dependOn(&tests.step);
Expand Down
2 changes: 1 addition & 1 deletion examples/multithread/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fn hi_handler(ctx: *Context, _: void) !void {
}

fn redir_handler(ctx: *Context, _: void) !void {
try ctx.response.headers.put("Location", "/hi/redirect");
ctx.response.headers.putAssumeCapacity("Location", "/hi/redirect");

try ctx.respond(.{
.status = .@"Permanent Redirect",
Expand Down
11 changes: 6 additions & 5 deletions src/core/case_string_map.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const std = @import("std");
const assert = std.debug.assert;

pub fn CaseStringMap(comptime T: type) type {
return std.ArrayHashMap([]const u8, T, struct {
return std.ArrayHashMapUnmanaged([]const u8, T, struct {
pub fn hash(_: @This(), input: []const u8) u32 {
var h: u32 = 0;
for (input) |byte| {
Expand All @@ -24,11 +24,12 @@ pub fn CaseStringMap(comptime T: type) type {
const testing = std.testing;

test "CaseStringMap: Add Stuff" {
var csm = CaseStringMap([]const u8).init(testing.allocator);
defer csm.deinit();
var csm = CaseStringMap([]const u8){};
defer csm.deinit(testing.allocator);
try csm.ensureUnusedCapacity(testing.allocator, 2);

try csm.putNoClobber("Content-Length", "100");
try csm.putNoClobber("Host", "localhost:9999");
csm.putAssumeCapacity("Content-Length", "100");
csm.putAssumeCapacity("Host", "localhost:9999");

const content_length = csm.get("content-length");
try testing.expect(content_length != null);
Expand Down
6 changes: 3 additions & 3 deletions src/http/provision.zig
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ pub const Provision = struct {
provision.captures = ctx.allocator.alloc(Capture, config.capture_count_max) catch {
@panic("attempting to statically allocate more memory than available. (Captures)");
};
provision.queries = QueryMap.init(ctx.allocator);
provision.queries.ensureUnusedCapacity(config.query_count_max) catch {
provision.queries = QueryMap{};
provision.queries.ensureUnusedCapacity(ctx.allocator, config.query_count_max) catch {
@panic("attempting to statically allocate more memory than available. (QueryMap)");
};
provision.request = Request.init(ctx.allocator, config.header_count_max) catch {
Expand All @@ -69,7 +69,7 @@ pub const Provision = struct {
provision.arena.deinit();
provision.request.deinit();
provision.response.deinit();
provision.queries.deinit();
provision.queries.deinit(allocator);
allocator.free(provision.captures);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/http/request.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ pub const Request = struct {

/// This is for constructing a Request.
pub fn init(allocator: std.mem.Allocator, header_count_max: usize) !Request {
var headers = Headers.init(allocator);
try headers.ensureUnusedCapacity(header_count_max);
var headers = Headers{};
try headers.ensureUnusedCapacity(allocator, header_count_max);

return Request{
.allocator = allocator,
Expand All @@ -29,7 +29,7 @@ pub const Request = struct {
}

pub fn deinit(self: *Request) void {
self.headers.deinit();
self.headers.deinit(self.allocator);
}

const RequestParseOptions = struct {
Expand Down
6 changes: 3 additions & 3 deletions src/http/response.zig
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ pub const Response = struct {
headers: Headers,

pub fn init(allocator: std.mem.Allocator, header_count_max: usize) !Response {
var headers = Headers.init(allocator);
try headers.ensureUnusedCapacity(header_count_max);
var headers = Headers{};
try headers.ensureUnusedCapacity(allocator, header_count_max);

return Response{
.allocator = allocator,
Expand All @@ -24,7 +24,7 @@ pub const Response = struct {
}

pub fn deinit(self: *Response) void {
self.headers.deinit();
self.headers.deinit(self.allocator);
}

pub fn clear(self: *Response) void {
Expand Down
9 changes: 3 additions & 6 deletions src/http/router.zig
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ pub fn Router(comptime Server: type) type {
.{etag_hash},
);

try provision.response.headers.put("ETag", calc_etag);
provision.response.headers.putAssumeCapacity("ETag", calc_etag);

// If we have an ETag on the request...
if (provision.request.headers.get("If-None-Match")) |etag| {
Expand Down Expand Up @@ -308,17 +308,14 @@ pub fn Router(comptime Server: type) type {
.{std.time.s_per_day * 30},
);

try ctx.response.headers.put(
"Cache-Control",
cache_control,
);
ctx.response.headers.putAssumeCapacity("Cache-Control", cache_control);

// If our static item is greater than 1KB,
// it might be more beneficial to using caching.
if (comptime bytes.len > 1024) {
@setEvalBranchQuota(1_000_000);
const etag = comptime std.fmt.comptimePrint("\"{d}\"", .{std.hash.Wyhash.hash(0, bytes)});
try ctx.response.headers.put("ETag", etag[0..]);
ctx.response.headers.putAssumeCapacity("ETag", etag[0..]);

if (ctx.request.headers.get("If-None-Match")) |match| {
if (std.mem.eql(u8, etag, match)) {
Expand Down
11 changes: 1 addition & 10 deletions src/http/server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -828,16 +828,7 @@ pub fn Server(comptime security: Security) type {
break :route;
};

p.response.headers.put("Allow", allowed) catch {
p.response.set(.{
.status = .@"Internal Server Error",
.mime = Mime.HTML,
.body = "",
});

break :route;
};

p.response.headers.putAssumeCapacity("Allow", allowed);
break :route;
}
}
Expand Down
9 changes: 0 additions & 9 deletions src/test.zig

This file was deleted.

20 changes: 3 additions & 17 deletions src/tls/bear.zig
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ fn parse_pem(allocator: std.mem.Allocator, section: []const u8, buffer: []const
}

const TLSContextOptions = struct {
allocator: std.mem.Allocator,
cert: TLSFileOptions,
cert_name: []const u8,
key: TLSFileOptions,
Expand All @@ -110,10 +109,10 @@ pub const TLSContext = struct {
key: []const u8,

/// This only needs to be called once and it should create all of the stuff needed.
pub fn init(options: TLSContextOptions) !TLSContext {
pub fn init(allocator: std.mem.Allocator, options: TLSContextOptions) !TLSContext {
var self: TLSContext = undefined;
self.parent_allocator = options.allocator;
self.arena = std.heap.ArenaAllocator.init(options.allocator);
self.parent_allocator = allocator;
self.arena = std.heap.ArenaAllocator.init(allocator);
self.allocator = self.arena.allocator();

const cert_buf = blk: {
Expand Down Expand Up @@ -812,16 +811,3 @@ pub const TLS = struct {
return error.EncryptTimeout;
}
};

const testing = std.testing;

test "Parsing Certificates" {
const cert = "src/examples/tls/certs/server.cert";
const key = "src/examples/tls/certs/server.key";

const context = try TLSContext.init(testing.allocator, cert, key);
defer context.deinit();

const tls = try context.create(undefined);
defer tls.deinit();
}
31 changes: 31 additions & 0 deletions src/unit_test.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const std = @import("std");
const testing = std.testing;

pub const Core = @import("./core/lib.zig");
pub const HTTP = @import("./http/lib.zig");

test "zzz unit tests" {
// Core
testing.refAllDecls(@import("./core/case_string_map.zig"));
testing.refAllDecls(@import("./core/job.zig"));
testing.refAllDecls(@import("./core/pseudoslice.zig"));
testing.refAllDecls(@import("./core/zc_buffer.zig"));

// HTTP
testing.refAllDecls(@import("./http/context.zig"));
testing.refAllDecls(@import("./http/date.zig"));
testing.refAllDecls(@import("./http/method.zig"));
testing.refAllDecls(@import("./http/mime.zig"));
testing.refAllDecls(@import("./http/provision.zig"));
testing.refAllDecls(@import("./http/request.zig"));
testing.refAllDecls(@import("./http/response.zig"));
testing.refAllDecls(@import("./http/route.zig"));
testing.refAllDecls(@import("./http/router.zig"));
testing.refAllDecls(@import("./http/routing_trie.zig"));
testing.refAllDecls(@import("./http/server.zig"));
testing.refAllDecls(@import("./http/sse.zig"));
testing.refAllDecls(@import("./http/status.zig"));

// TLS
testing.refAllDecls(@import("./tls/bear.zig"));
}

0 comments on commit d02060c

Please sign in to comment.