Skip to content
This repository was archived by the owner on Apr 9, 2025. It is now read-only.

Commit 12f4702

Browse files
Merge pull request #9 from lightpanda-io/update-test
test: refacto test
2 parents ed7ae07 + 2366cb9 commit 12f4702

File tree

1 file changed

+29
-22
lines changed

1 file changed

+29
-22
lines changed

src/tests.zig

+29-22
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,39 @@ fn onRequestWait(ctx: *Ctx, res: anyerror!void) !void {
1212
std.debug.print("error: {any}\n", .{e});
1313
return e;
1414
};
15-
std.log.debug("REQUEST WAITED", .{});
15+
std.log.debug("request waited", .{});
1616
std.log.debug("Status code: {any}", .{ctx.req.response.status});
1717
const body = try ctx.req.reader().readAllAlloc(ctx.alloc(), 1024 * 1024);
1818
defer ctx.alloc().free(body);
19-
std.log.debug("Body: \n{s}", .{body});
19+
std.log.debug("body length: {d}", .{body.len});
2020
}
2121

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

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

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

4040
test "example.com" {
41-
// const url = "http://127.0.0.1:8080";
42-
const url = "https://www.example.com";
41+
var urls = [_][]const u8{
42+
"https://www.example.com",
43+
};
44+
try do(&urls);
45+
}
4346

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

57-
var req = Client.Request{
58-
.client = &client,
59-
};
60-
defer req.deinit();
61+
var server_header_buffer: [1024 * 1024]u8 = undefined;
6162

62-
var ctx = try Client.Ctx.init(&loop, &req);
63-
defer ctx.deinit();
63+
for (urls) |url| {
64+
var req = Client.Request{
65+
.client = &client,
66+
};
67+
defer req.deinit();
6468

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

67-
try client.async_open(
68-
.GET,
69-
try std.Uri.parse(url),
70-
.{ .server_header_buffer = &server_header_buffer },
71-
&ctx,
72-
onRequestConnect,
73-
);
72+
std.log.info("request {s}", .{url});
73+
try client.async_open(
74+
.GET,
75+
try std.Uri.parse(url),
76+
.{ .server_header_buffer = &server_header_buffer },
77+
&ctx,
78+
onRequestConnect,
79+
);
7480

75-
try std.testing.expect(ctx.err == null);
81+
try std.testing.expect(ctx.err == null);
82+
}
7683
}

0 commit comments

Comments
 (0)