From 3856fceb9fce68e0b63b9256863f7fac04b76cdf Mon Sep 17 00:00:00 2001 From: renerocksai Date: Sun, 16 Mar 2025 15:52:26 +0100 Subject: [PATCH] fix auth tests b/c of new zap.Endpoint --- src/endpoint.zig | 9 ++- src/tests/test_auth.zig | 128 +++++++++++++++++++--------------------- 2 files changed, 67 insertions(+), 70 deletions(-) diff --git a/src/endpoint.zig b/src/endpoint.zig index 047f073..236e33a 100644 --- a/src/endpoint.zig +++ b/src/endpoint.zig @@ -73,9 +73,10 @@ const EndpointWrapper = struct { .DELETE => return self.wrapped.*.delete(r), .PATCH => return self.wrapped.*.patch(r), .OPTIONS => return self.wrapped.*.options(r), - else => {}, + else => { + // TODO: log that method is ignored + }, } - // TODO: log that req fn is not implemented on this EP } }; } @@ -197,6 +198,10 @@ pub const Listener = struct { /// callback in the provided ListenerSettings, this request callback will be /// called every time a request arrives that no endpoint matches. pub fn init(a: std.mem.Allocator, l: ListenerSettings) Self { + // reset the global in case init is called multiple times, as is the + // case in the authentication tests + endpoints = .empty; + // take copy of listener settings before modifying the callback field var ls = l; diff --git a/src/tests/test_auth.zig b/src/tests/test_auth.zig index 2ac4905..b33487d 100644 --- a/src/tests/test_auth.zig +++ b/src/tests/test_auth.zig @@ -1,7 +1,6 @@ const std = @import("std"); const zap = @import("zap"); const Authenticators = zap.Auth; -const Endpoint = zap.Endpoint; const fio = zap; const util = zap; @@ -102,23 +101,6 @@ const HTTP_RESPONSE: []const u8 = ; var received_response: []const u8 = "null"; -fn endpoint_http_get(e: *Endpoint, r: zap.Request) void { - _ = e; - r.sendBody(HTTP_RESPONSE) catch return; - received_response = HTTP_RESPONSE; - std.time.sleep(1 * std.time.ns_per_s); - zap.stop(); -} - -fn endpoint_http_unauthorized(e: *Endpoint, r: zap.Request) void { - _ = e; - r.setStatus(.unauthorized); - r.sendBody("UNAUTHORIZED ACCESS") catch return; - received_response = "UNAUTHORIZED"; - std.time.sleep(1 * std.time.ns_per_s); - zap.stop(); -} - // // http client code for in-process sending of http request // @@ -165,6 +147,31 @@ fn makeRequestThread(a: std.mem.Allocator, url: []const u8, auth: ?ClientAuthReq return try std.Thread.spawn(.{}, makeRequest, .{ a, url, auth }); } +pub const Endpoint = struct { + path: []const u8, + + pub fn get(e: *Endpoint, r: zap.Request) void { + _ = e; + r.sendBody(HTTP_RESPONSE) catch return; + received_response = HTTP_RESPONSE; + std.time.sleep(1 * std.time.ns_per_s); + zap.stop(); + } + + pub fn unauthorized(e: *Endpoint, r: zap.Request) void { + _ = e; + r.setStatus(.unauthorized); + r.sendBody("UNAUTHORIZED ACCESS") catch return; + received_response = "UNAUTHORIZED"; + std.time.sleep(1 * std.time.ns_per_s); + zap.stop(); + } + pub fn post(_: *Endpoint, _: zap.Request) void {} + pub fn put(_: *Endpoint, _: zap.Request) void {} + pub fn delete(_: *Endpoint, _: zap.Request) void {} + pub fn patch(_: *Endpoint, _: zap.Request) void {} + pub fn options(_: *Endpoint, _: zap.Request) void {} +}; // // end of http client code // @@ -187,11 +194,9 @@ test "BearerAuthSingle authenticateRequest OK" { defer listener.deinit(); // create mini endpoint - var ep = Endpoint.init(.{ + var ep: Endpoint = .{ .path = "/test", - .get = endpoint_http_get, - .unauthorized = endpoint_http_unauthorized, - }); + }; // create authenticator const Authenticator = Authenticators.BearerSingle; @@ -199,10 +204,10 @@ test "BearerAuthSingle authenticateRequest OK" { defer authenticator.deinit(); // create authenticating endpoint - const BearerAuthEndpoint = Endpoint.Authenticating(Authenticator); + const BearerAuthEndpoint = zap.Endpoint.Authenticating(Endpoint, Authenticator); var auth_ep = BearerAuthEndpoint.init(&ep, &authenticator); - try listener.register(auth_ep.endpoint()); + try listener.register(&auth_ep); listener.listen() catch {}; // std.debug.print("\n\n*******************************************\n", .{}); @@ -240,11 +245,9 @@ test "BearerAuthSingle authenticateRequest test-unauthorized" { defer listener.deinit(); // create mini endpoint - var ep = Endpoint.init(.{ + var ep: Endpoint = .{ .path = "/test", - .get = endpoint_http_get, - .unauthorized = endpoint_http_unauthorized, - }); + }; const Set = std.StringHashMap(void); var set = Set.init(a); // set @@ -258,12 +261,12 @@ test "BearerAuthSingle authenticateRequest test-unauthorized" { defer authenticator.deinit(); // create authenticating endpoint - const BearerAuthEndpoint = Endpoint.Authenticating(Authenticator); + const BearerAuthEndpoint = zap.Endpoint.Authenticating(Endpoint, Authenticator); var auth_ep = BearerAuthEndpoint.init(&ep, &authenticator); - try listener.register(auth_ep.endpoint()); + try listener.register(&auth_ep); - listener.listen() catch {}; + try listener.listen(); // std.debug.print("Waiting for the following:\n", .{}); // std.debug.print("./zig-out/bin/http_client http://127.0.0.1:3000/test Bearer invalid\r", .{}); @@ -277,6 +280,7 @@ test "BearerAuthSingle authenticateRequest test-unauthorized" { }); try std.testing.expectEqualStrings("UNAUTHORIZED", received_response); + std.debug.print("\nI made it\n", .{}); } test "BearerAuthMulti authenticateRequest OK" { @@ -297,11 +301,9 @@ test "BearerAuthMulti authenticateRequest OK" { defer listener.deinit(); // create mini endpoint - var ep = Endpoint.init(.{ + var ep: Endpoint = .{ .path = "/test", - .get = endpoint_http_get, - .unauthorized = endpoint_http_unauthorized, - }); + }; // create authenticator const Authenticator = Authenticators.BearerSingle; @@ -309,12 +311,12 @@ test "BearerAuthMulti authenticateRequest OK" { defer authenticator.deinit(); // create authenticating endpoint - const BearerAuthEndpoint = Endpoint.Authenticating(Authenticator); + const BearerAuthEndpoint = zap.Endpoint.Authenticating(Endpoint, Authenticator); var auth_ep = BearerAuthEndpoint.init(&ep, &authenticator); - try listener.register(auth_ep.endpoint()); + try listener.register(&auth_ep); - listener.listen() catch {}; + try listener.listen(); // std.debug.print("Waiting for the following:\n", .{}); // std.debug.print("./zig-out/bin/http_client_runner http://127.0.0.1:3000/test Bearer " ++ token ++ "\r", .{}); @@ -348,11 +350,9 @@ test "BearerAuthMulti authenticateRequest test-unauthorized" { defer listener.deinit(); // create mini endpoint - var ep = Endpoint.init(.{ + var ep: Endpoint = .{ .path = "/test", - .get = endpoint_http_get, - .unauthorized = endpoint_http_unauthorized, - }); + }; // create authenticator const Authenticator = Authenticators.BearerSingle; @@ -360,10 +360,10 @@ test "BearerAuthMulti authenticateRequest test-unauthorized" { defer authenticator.deinit(); // create authenticating endpoint - const BearerAuthEndpoint = Endpoint.Authenticating(Authenticator); + const BearerAuthEndpoint = zap.Endpoint.Authenticating(Endpoint, Authenticator); var auth_ep = BearerAuthEndpoint.init(&ep, &authenticator); - try listener.register(auth_ep.endpoint()); + try listener.register(&auth_ep); listener.listen() catch {}; // std.debug.print("Waiting for the following:\n", .{}); @@ -399,11 +399,9 @@ test "BasicAuth Token68 authenticateRequest" { defer listener.deinit(); // create mini endpoint - var ep = Endpoint.init(.{ + var ep: Endpoint = .{ .path = "/test", - .get = endpoint_http_get, - .unauthorized = endpoint_http_unauthorized, - }); + }; // create a set of Token68 entries const Set = std.StringHashMap(void); var set = Set.init(a); // set @@ -416,10 +414,10 @@ test "BasicAuth Token68 authenticateRequest" { defer authenticator.deinit(); // create authenticating endpoint - const BearerAuthEndpoint = Endpoint.Authenticating(Authenticator); + const BearerAuthEndpoint = zap.Endpoint.Authenticating(Endpoint, Authenticator); var auth_ep = BearerAuthEndpoint.init(&ep, &authenticator); - try listener.register(auth_ep.endpoint()); + try listener.register(&auth_ep); listener.listen() catch {}; // std.debug.print("Waiting for the following:\n", .{}); @@ -455,11 +453,9 @@ test "BasicAuth Token68 authenticateRequest test-unauthorized" { defer listener.deinit(); // create mini endpoint - var ep = Endpoint.init(.{ + var ep: Endpoint = .{ .path = "/test", - .get = endpoint_http_get, - .unauthorized = endpoint_http_unauthorized, - }); + }; // create a set of Token68 entries const Set = std.StringHashMap(void); var set = Set.init(a); // set @@ -472,10 +468,10 @@ test "BasicAuth Token68 authenticateRequest test-unauthorized" { defer authenticator.deinit(); // create authenticating endpoint - const BearerAuthEndpoint = Endpoint.Authenticating(Authenticator); + const BearerAuthEndpoint = zap.Endpoint.Authenticating(Endpoint, Authenticator); var auth_ep = BearerAuthEndpoint.init(&ep, &authenticator); - try listener.register(auth_ep.endpoint()); + try listener.register(&auth_ep); listener.listen() catch {}; // std.debug.print("Waiting for the following:\n", .{}); @@ -510,11 +506,9 @@ test "BasicAuth UserPass authenticateRequest" { defer listener.deinit(); // create mini endpoint - var ep = Endpoint.init(.{ + var ep: Endpoint = .{ .path = "/test", - .get = endpoint_http_get, - .unauthorized = endpoint_http_unauthorized, - }); + }; // create a set of User -> Pass entries const Map = std.StringHashMap([]const u8); @@ -538,10 +532,10 @@ test "BasicAuth UserPass authenticateRequest" { defer authenticator.deinit(); // create authenticating endpoint - const BearerAuthEndpoint = Endpoint.Authenticating(Authenticator); + const BearerAuthEndpoint = zap.Endpoint.Authenticating(Endpoint, Authenticator); var auth_ep = BearerAuthEndpoint.init(&ep, &authenticator); - try listener.register(auth_ep.endpoint()); + try listener.register(&auth_ep); listener.listen() catch {}; // std.debug.print("Waiting for the following:\n", .{}); @@ -576,11 +570,9 @@ test "BasicAuth UserPass authenticateRequest test-unauthorized" { defer listener.deinit(); // create mini endpoint - var ep = Endpoint.init(.{ + var ep: Endpoint = .{ .path = "/test", - .get = endpoint_http_get, - .unauthorized = endpoint_http_unauthorized, - }); + }; // create a set of User -> Pass entries const Map = std.StringHashMap([]const u8); @@ -605,10 +597,10 @@ test "BasicAuth UserPass authenticateRequest test-unauthorized" { defer authenticator.deinit(); // create authenticating endpoint - const BearerAuthEndpoint = Endpoint.Authenticating(Authenticator); + const BearerAuthEndpoint = zap.Endpoint.Authenticating(Endpoint, Authenticator); var auth_ep = BearerAuthEndpoint.init(&ep, &authenticator); - try listener.register(auth_ep.endpoint()); + try listener.register(&auth_ep); listener.listen() catch {}; // std.debug.print("Waiting for the following:\n", .{});