Add env config and git test

This commit is contained in:
Alex Selimov 2026-05-28 06:19:07 -04:00
parent dac5504754
commit f5c55ef8c8
3 changed files with 59 additions and 2 deletions

28
src/env.zig Normal file
View file

@ -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"));
}

View file

@ -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 = &.{},
};

29
src/test_git.zig Normal file
View file

@ -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"));
}