diff --git a/README.md b/README.md index 05a13cc7b011030984ef8c9914e798529b327024..d1a634f3d3f8e163ba5ea1e0435c67643c5b05ab 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ ![loc](https://sloc.xyz/github/nektro/zig-oauth2) [![license](https://img.shields.io/github/license/nektro/zig-oauth2.svg)](https://github.com/nektro/zig-oauth2/blob/master/LICENSE) [![nektro @ github sponsors](https://img.shields.io/badge/sponsors-nektro-purple?logo=github)](https://github.com/sponsors/nektro) -[![Zig](https://img.shields.io/badge/Zig-0.15-f7a41d)](https://ziglang.org/) +[![Zig](https://img.shields.io/badge/Zig-0.16-f7a41d)](https://ziglang.org/) [![Zigmod](https://img.shields.io/badge/Zigmod-latest-f7a41d)](https://github.com/nektro/zigmod) HTTP handler functions to allow you to easily add OAuth2 login support to your Zig application. diff --git a/build.zig b/build.zig index c8b2f465b5dea81068c5ee06448be09756f48b84..691ea5f05fe8afee54f787f133ee48b396977f8f 100644 --- a/build.zig +++ b/build.zig @@ -14,7 +14,7 @@ pub fn build(b: *std.Build) void { }), }); deps.addAllTo(tests); - tests.linkLibC(); + tests.root_module.link_libc = true; tests.use_llvm = !disable_llvm; tests.use_lld = !disable_llvm; b.getInstallStep().dependOn(&tests.step); diff --git a/oauth2.zig b/oauth2.zig index d53e23bb76877e7aca413e78ffe687b0b25fb94e..258697eb47386b7d0a1348b89dc3914acf3d4f42 100644 --- a/oauth2.zig +++ b/oauth2.zig @@ -9,6 +9,8 @@ const url = @import("url"); const http = @import("http"); const nio = @import("nio"); const json = @import("json"); +const builtin = @import("builtin"); +const root = @import("root"); const Base = @This(); pub const Provider = struct { @@ -266,11 +268,6 @@ pub fn clientByProviderId(clients: []const Client, name: string) ?Client { } pub fn Handlers(comptime T: type) type { - comptime std.debug.assert(@hasDecl(T, "isLoggedIn")); - comptime std.debug.assert(@hasDecl(T, "doneUrl")); - comptime std.debug.assert(@hasDecl(T, "saveInfo")); - comptime std.debug.assert(@hasDecl(T, "callbackPath")); - return struct { const Self = @This(); pub var clients: []Client = &.{}; @@ -298,8 +295,9 @@ pub fn Handlers(comptime T: type) type { const client = clientByProviderId(Self.clients, state) orelse return try fail(response_status, body_writer, "error: No handler found for provider: {s}\n", .{state}); const code = query.get("code") orelse return try fail(response_status, body_writer, "", .{}); + const io = if (!builtin.is_test) root.io else std.Options.debug_io; var buf: [4096]u8 = @splat(0); - var http_client: std.http.Client = .{ .allocator = alloc }; + var http_client: std.http.Client = .{ .allocator = alloc, .io = io }; defer http_client.deinit(); var params = url.SearchParams.init(alloc); diff --git a/test.zig b/test.zig index 1fa8d9433083b22b6f6589f8d0b9926262558e8e..4d20bdde09fb73536274f25408e109a0edb805d4 100644 --- a/test.zig +++ b/test.zig @@ -1,7 +1,32 @@ const std = @import("std"); const oauth2 = @import("oauth2"); +const nio = @import("nio"); +const http = @import("http"); +const json = @import("json"); -// TODO: how do we make this better? test { - std.testing.refAllDeclsRecursive(oauth2); + _ = &oauth2.Provider; + _ = &oauth2.Client; + std.testing.refAllDecls(oauth2.providers); + std.testing.refAllDecls(oauth2.dynamic_providers); + _ = &oauth2.providerById; + _ = &oauth2.clientByProviderId; + _ = &oauth2.pek_domain; +} +test { + const S = struct { + pub const callbackPath = "/-/callback"; + pub const doneUrl = "/"; + + pub fn isLoggedIn(_: *http.ServerRequest, _: std.mem.Allocator) !bool { + return false; + } + pub fn saveInfo(_: *http.HeadersMap, _: std.mem.Allocator, _: oauth2.Provider, _: []const u8, _: []const u8, _: json.Document, _: json.Document) !void { + // + } + }; + const H = oauth2.Handlers(S); + std.testing.refAllDecls(oauth2.Handlers(S)); + _ = @TypeOf(H.login(undefined, nio.NullWriter{}, undefined, undefined, undefined, undefined, undefined)); + _ = @TypeOf(H.callback(undefined, nio.NullWriter{}, undefined, undefined, undefined, undefined, undefined)); } diff --git a/zig.mod b/zig.mod index cc1473b87db0e338c02b190ab10b1d27fdcfb2db..089e315eef3c21979fd7c1c640c9b1c859401e51 100644 --- a/zig.mod +++ b/zig.mod @@ -12,3 +12,7 @@ dependencies: - src: git https://github.com/nektro/zig-net-http - src: git https://github.com/nektro/zig-nio - src: git https://github.com/nektro/zig-json +root_dependencies: + - src: git https://github.com/nektro/zig-nio + - src: git https://github.com/nektro/zig-net-http + - src: git https://github.com/nektro/zig-json