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

Fixup tests

This commit is contained in:
JacobCrabill 2024-01-30 21:10:33 -08:00
parent 58352bc4b8
commit c74f699252
3 changed files with 27 additions and 20 deletions

View file

@ -146,22 +146,21 @@ fn makeRequest(a: std.mem.Allocator, url: []const u8, auth: ?ClientAuthReqHeader
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.request(.GET, uri, h, .{}); // Note: you could/should consider setting the buffer size here
var req = try http_client.fetch(a, .{
.location = .{ .uri = uri },
.method = .GET,
.headers = h,
});
defer req.deinit(); defer req.deinit();
try req.start();
try req.wait();
// req.deinit() panics!
// defer req.deinit();
// without this block, the tests sometimes get stuck which // without this block, the tests sometimes get stuck which
// might have to do with connection pooling and connections being in // might have to do with connection pooling and connections being in
// a different state when all data has been read?!? // a different state when all data has been read?!?
{ {
var buffer: [1024]u8 = undefined; if (req.body) |body| {
// we know we won't receive a lot std.debug.print("RESPONSE:\n{s}\n", .{body});
const len = try req.reader().readAll(&buffer); }
std.debug.print("RESPONSE:\n{s}\n", .{buffer[0..len]});
} }
zap.stop(); zap.stop();

View file

@ -10,11 +10,13 @@ fn makeRequest(a: std.mem.Allocator, url: []const u8) !void {
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.request(.GET, uri, h, .{}); var req = try http_client.fetch(a, .{
.location = .{ .uri = uri },
.method = .GET,
.headers = h,
});
defer req.deinit(); defer req.deinit();
try req.start();
try req.wait();
zap.stop(); zap.stop();
} }
@ -23,7 +25,7 @@ fn makeRequestThread(a: std.mem.Allocator, url: []const u8) !std.Thread {
} }
test "http parameters" { test "http parameters" {
var allocator = std.testing.allocator; const allocator = std.testing.allocator;
const Handler = struct { const Handler = struct {
var alloc: std.mem.Allocator = undefined; var alloc: std.mem.Allocator = undefined;

View file

@ -1,7 +1,7 @@
const std = @import("std"); const std = @import("std");
const zap = @import("zap"); const zap = @import("zap");
var buffer: [1024]u8 = undefined; var buffer: []u8 = undefined;
var read_len: ?usize = null; var read_len: ?usize = null;
const testfile = @embedFile("testfile.txt"); const testfile = @embedFile("testfile.txt");
@ -15,12 +15,17 @@ fn makeRequest(a: std.mem.Allocator, url: []const u8) !void {
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.request(.GET, uri, h, .{}); var req = try http_client.fetch(a, .{
.location = .{ .uri = uri },
.method = .GET,
.headers = h,
});
defer req.deinit(); defer req.deinit();
try req.start(); if (req.body) |body| {
try req.wait(); read_len = body.len;
read_len = try req.readAll(&buffer); buffer = try a.dupe(u8, body);
}
zap.stop(); zap.stop();
} }
@ -33,7 +38,7 @@ pub fn on_request(r: zap.Request) void {
} }
test "send file" { test "send file" {
var allocator = std.testing.allocator; const allocator = std.testing.allocator;
// setup listener // setup listener
var listener = zap.HttpListener.init( var listener = zap.HttpListener.init(
@ -56,6 +61,7 @@ test "send file" {
}); });
if (read_len) |rl| { if (read_len) |rl| {
defer allocator.free(buffer);
try std.testing.expectEqual(testfile.len, rl); try std.testing.expectEqual(testfile.len, rl);
try std.testing.expectEqualSlices(u8, testfile, buffer[0..rl]); try std.testing.expectEqualSlices(u8, testfile, buffer[0..rl]);
} else { } else {