diff --git a/src/env.zig b/src/env.zig new file mode 100644 index 0000000..7844555 --- /dev/null +++ b/src/env.zig @@ -0,0 +1,28 @@ +const std = @import("std"); + +pub const Env = struct { + github_token: ?[]const u8, + pub fn init(env_map: *const std.process.Environ.Map) !Env { + const github_token = env_map.get("GITHUB_TOKEN") orelse null; + return .{ .github_token = github_token }; + } +}; + +test "Validate that missing github token is null" { + const allocator = std.testing.allocator; + var env_map = std.process.Environ.Map.init(allocator); + defer env_map.deinit(); + + const env: Env = try .init(&env_map); + try std.testing.expect(env.github_token == null); +} + +test "Validate that present github token isn't null" { + const allocator = std.testing.allocator; + var env_map = std.process.Environ.Map.init(allocator); + try env_map.put("GITHUB_TOKEN", "some_token"); + defer env_map.deinit(); + + const env: Env = try .init(&env_map); + try std.testing.expect(std.mem.eql(u8, env.github_token.?, "some_token")); +} diff --git a/src/http_utils/http_client_wrapper.zig b/src/http_utils/http_client_wrapper.zig index cebbd03..6231a03 100644 --- a/src/http_utils/http_client_wrapper.zig +++ b/src/http_utils/http_client_wrapper.zig @@ -1,12 +1,12 @@ const std = @import("std"); const HttpError = error{UnexpectedStatus}; -const HttpClientWrapper = @This(); +pub const HttpClientWrapper = @This(); client: std.http.Client, const ResponseOptions = struct { - token: ?[]u8 = null, + token: ?[]const u8 = null, extra_headers: []const std.http.Header = &.{}, }; diff --git a/src/test_git.zig b/src/test_git.zig new file mode 100644 index 0000000..bc4603f --- /dev/null +++ b/src/test_git.zig @@ -0,0 +1,29 @@ +const env_config = @import("env.zig"); +const http_utils = @import("http_utils/http_client_wrapper.zig"); +const std = @import("std"); + +test "Test get github file contents passing authorization and content-type" { + const io = std.Io.Threaded.global_single_threaded.io(); + const allocator = std.testing.allocator; + + const http_client: std.http.Client = .{ .allocator = allocator, .io = io }; + var test_client: http_utils.HttpClientWrapper = .{ .client = http_client }; + defer test_client.deinit(); + + var env_map = try std.process.Environ.createMap(std.testing.environ, allocator); + defer env_map.deinit(); + const env: env_config.Env = try .init(&env_map); + + const extra_headers: []const std.http.Header = &.{ + .{ .name = "Accept", .value = "application/vnd.github.raw+json" }, + .{ .name = "X-GitHub-Api-Version", .value = "2026-03-10" }, + }; + const response = try test_client.getRawResponse( + allocator, + "https://api.github.com/repos/aselimov/upvoters/contents/README.md", + .{ .token = env.github_token, .extra_headers = extra_headers }, + ); + defer allocator.free(response); + + try std.testing.expect(std.mem.containsAtLeast(u8, response, 1, "upvoters")); +}