Skip to content

Commit

Permalink
Merge pull request #9 from lightpanda-io/update-test
Browse files Browse the repository at this point in the history
test: refacto test
  • Loading branch information
krichprollsch authored Nov 29, 2024
2 parents ed7ae07 + 2366cb9 commit 12f4702
Showing 1 changed file with 29 additions and 22 deletions.
51 changes: 29 additions & 22 deletions src/tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,39 @@ fn onRequestWait(ctx: *Ctx, res: anyerror!void) !void {
std.debug.print("error: {any}\n", .{e});
return e;
};
std.log.debug("REQUEST WAITED", .{});
std.log.debug("request waited", .{});
std.log.debug("Status code: {any}", .{ctx.req.response.status});
const body = try ctx.req.reader().readAllAlloc(ctx.alloc(), 1024 * 1024);
defer ctx.alloc().free(body);
std.log.debug("Body: \n{s}", .{body});
std.log.debug("body length: {d}", .{body.len});
}

fn onRequestFinish(ctx: *Ctx, res: anyerror!void) !void {
res catch |err| return err;
std.log.debug("REQUEST FINISHED", .{});
std.log.debug("request finished", .{});
return ctx.req.async_wait(ctx, onRequestWait);
}

fn onRequestSend(ctx: *Ctx, res: anyerror!void) !void {
res catch |err| return err;
std.log.debug("REQUEST SENT", .{});
std.log.debug("request sent", .{});
return ctx.req.async_finish(ctx, onRequestFinish);
}

pub fn onRequestConnect(ctx: *Ctx, res: anyerror!void) anyerror!void {
res catch |err| return err;
std.log.debug("REQUEST CONNECTED", .{});
std.log.debug("request connected", .{});
return ctx.req.async_send(ctx, onRequestSend);
}

test "example.com" {
// const url = "http://127.0.0.1:8080";
const url = "https://www.example.com";
var urls = [_][]const u8{
"https://www.example.com",
};
try do(&urls);
}

fn do(urls: [][]const u8) !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer switch (gpa.deinit()) {
.ok => {},
Expand All @@ -54,23 +58,26 @@ test "example.com" {
var client = Client{ .allocator = alloc };
defer client.deinit();

var req = Client.Request{
.client = &client,
};
defer req.deinit();
var server_header_buffer: [1024 * 1024]u8 = undefined;

var ctx = try Client.Ctx.init(&loop, &req);
defer ctx.deinit();
for (urls) |url| {
var req = Client.Request{
.client = &client,
};
defer req.deinit();

var server_header_buffer: [1024 * 1024]u8 = undefined;
var ctx = try Client.Ctx.init(&loop, &req);
defer ctx.deinit();

try client.async_open(
.GET,
try std.Uri.parse(url),
.{ .server_header_buffer = &server_header_buffer },
&ctx,
onRequestConnect,
);
std.log.info("request {s}", .{url});
try client.async_open(
.GET,
try std.Uri.parse(url),
.{ .server_header_buffer = &server_header_buffer },
&ctx,
onRequestConnect,
);

try std.testing.expect(ctx.err == null);
try std.testing.expect(ctx.err == null);
}
}

0 comments on commit 12f4702

Please sign in to comment.