1
0
Fork 0
mirror of https://github.com/zigzap/zap.git synced 2025-10-20 15:14:08 +00:00

fix test-authentication (std.http use)

This commit is contained in:
Rene Schallner 2024-04-21 16:20:07 +02:00
parent 50017aa1f4
commit 90c544d69a

View file

@ -1,12 +1,9 @@
const std = @import("std"); const std = @import("std");
const zap = @import("zap"); const zap = @import("zap");
// const Authenticators = @import("http_auth.zig");
const Authenticators = zap.Auth; const Authenticators = zap.Auth;
const Endpoint = zap.Endpoint; const Endpoint = zap.Endpoint;
const fio = zap; const fio = zap;
// const fio = @import("fio.zig");
const util = zap; const util = zap;
// const util = @import("util.zig");
test "BearerAuthSingle authenticate" { test "BearerAuthSingle authenticate" {
const a = std.testing.allocator; const a = std.testing.allocator;
@ -132,37 +129,34 @@ const ClientAuthReqHeaderFields = struct {
}; };
fn makeRequest(a: std.mem.Allocator, url: []const u8, auth: ?ClientAuthReqHeaderFields) !void { fn makeRequest(a: std.mem.Allocator, url: []const u8, auth: ?ClientAuthReqHeaderFields) !void {
const uri = try std.Uri.parse(url);
var h = std.http.Headers{ .allocator = a };
defer h.deinit();
if (auth) |auth_fields| {
const authstring = try std.fmt.allocPrint(a, "{s}{s}", .{ auth_fields.auth.str(), auth_fields.token });
defer a.free(authstring);
try h.append(auth_fields.auth.headerFieldStrHeader(), authstring);
}
var http_client: std.http.Client = .{ .allocator = a }; var http_client: std.http.Client = .{ .allocator = a };
defer http_client.deinit(); defer http_client.deinit();
var req = try http_client.open(.GET, uri, h, .{}); var auth_buf: [256]u8 = undefined;
defer req.deinit(); const auth_string: []const u8 = blk: {
if (auth) |auth_fields| {
try req.send(.{}); const authstring = try std.fmt.bufPrint(&auth_buf, "{s}{s}", .{ auth_fields.auth.str(), auth_fields.token });
try req.wait(); break :blk authstring;
// req.deinit() panics! } else {
// defer req.deinit(); break :blk "";
}
// without this block, the tests sometimes get stuck which };
// might have to do with connection pooling and connections being in _ = try http_client.fetch(.{
// a different state when all data has been read?!? .location = .{ .url = url },
{ .headers = .{
var buffer: [1024]u8 = undefined; .authorization = .omit,
// we know we won't receive a lot },
const len = try req.reader().readAll(&buffer); .extra_headers = blk: {
std.debug.print("RESPONSE:\n{s}\n", .{buffer[0..len]}); if (auth) |auth_fields| {
} break :blk &.{.{
.name = auth_fields.auth.headerFieldStrHeader(),
.value = auth_string,
}};
} else {
break :blk &.{};
}
},
});
zap.stop(); zap.stop();
} }