Skip to content

Commit e74cf91

Browse files
committed
Upgrade to 0.15.1
1 parent 3ad05e5 commit e74cf91

6 files changed

Lines changed: 101 additions & 84 deletions

File tree

‎build.zig‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,26 +46,32 @@ pub fn build(b: *std.Build) void {
4646
const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
4747

4848
// Automatically find all Zig files in src/ and subdirectories and add test steps for each
49-
var zig_files = std.ArrayList([]const u8).init(b.allocator);
50-
defer zig_files.deinit();
49+
var zig_files: std.ArrayList([]const u8) = .empty;
50+
defer zig_files.deinit(b.allocator);
5151
findZigFiles(b, &zig_files, "src") catch unreachable;
5252

53-
var test_run_steps = std.ArrayList(*std.Build.Step).init(b.allocator);
54-
defer test_run_steps.deinit();
53+
var test_run_steps: std.ArrayList(*std.Build.Step) = .empty;
54+
defer test_run_steps.deinit(b.allocator);
5555

5656
for (zig_files.items) |zig_file| {
5757
// Skip main.zig since it's already covered by exe_unit_tests
5858
if (std.mem.endsWith(u8, zig_file, "/main.zig")) continue;
59-
const test_artifact = b.addTest(.{ .root_source_file = b.path(zig_file), .target = target, .optimize = optimize });
60-
59+
const test_artifact = b.addTest(.{
60+
.root_module = b.createModule(.{
61+
.root_source_file = b.path(zig_file),
62+
.target = target,
63+
.optimize = optimize,
64+
}),
65+
});
66+
6167
// Add dependencies to individual test artifacts
6268
for (dependencies) |dependency| {
6369
const dep = b.dependency(dependency, .{});
6470
test_artifact.root_module.addImport(dependency, dep.module(dependency));
6571
}
66-
72+
6773
const run_test = b.addRunArtifact(test_artifact);
68-
test_run_steps.append(&run_test.step) catch unreachable;
74+
test_run_steps.append(b.allocator, &run_test.step) catch unreachable;
6975
}
7076

7177
const test_step = b.step("test", "Run unit tests");
@@ -82,7 +88,7 @@ fn findZigFiles(b: *std.Build, files: *std.ArrayList([]const u8), dir_path: []co
8288
while (try it.next()) |entry| {
8389
if (entry.kind == .file and std.mem.endsWith(u8, entry.name, ".zig")) {
8490
const full_path = try std.fs.path.join(b.allocator, &[_][]const u8{ dir_path, entry.name });
85-
try files.append(full_path);
91+
try files.append(b.allocator, full_path);
8692
} else if (entry.kind == .directory and !std.mem.eql(u8, entry.name, ".") and !std.mem.eql(u8, entry.name, "..")) {
8793
const subdir = try std.fs.path.join(b.allocator, &[_][]const u8{ dir_path, entry.name });
8894
defer b.allocator.free(subdir);

‎build.zig.zon‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
// Tracks the earliest Zig version that the package considers to be a
3030
// supported use case.
31-
.minimum_zig_version = "0.14.0",
31+
.minimum_zig_version = "0.15.1",
3232

3333
// This field is optional.
3434
// Each dependency must either provide a `url` and `hash`, or a `path`.
@@ -37,12 +37,12 @@
3737
// internet connectivity.
3838
.dependencies = .{
3939
.clap = .{
40-
.url = "git+https://github.com/Hejsil/zig-clap#cc5c6a5d71a317ed4b0ad776842d1d0655f72d0a",
41-
.hash = "clap-0.10.0-oBajB7jkAQAZ4cKLlzkeV9mDu2yGZvtN2QuOyfAfjBij",
40+
.url = "git+https://github.com/Hejsil/zig-clap#5289e0753cd274d65344bef1c114284c633536ea",
41+
.hash = "clap-0.11.0-oBajB-HnAQDPCKYzwF7rO3qDFwRcD39Q0DALlTSz5H7e",
4242
},
4343
.regex = .{
44-
.url = "git+https://github.com/tiehuis/zig-regex/?ref=HEAD#88105ccfb6d679111b91d798d97cd4eb95617452",
45-
.hash = "regex-0.1.2-axC356XdAQCrReHJQmJLbCDBoYVvzj8y-wE1PXUjT4V6",
44+
.url = "git+https://github.com/bradcypert/zig-regex.git#05f641c08939f131bb82195855efa7afaf7566eb",
45+
.hash = "regex-0.1.2-axC350bnAQCyrPkH2IEfri8hebJO6IujGY1rhcgZBXpL",
4646
},
4747
},
4848
.paths = .{

‎src/httpfile/assertion_checker.zig‎

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub const AssertionDiagnostic = struct {
4545

4646
pub fn init(allocator: Allocator) AssertionDiagnostic {
4747
return .{
48-
.failures = ArrayList(AssertionFailure).init(allocator),
48+
.failures = .empty,
4949
.allocator = allocator,
5050
};
5151
}
@@ -54,7 +54,7 @@ pub const AssertionDiagnostic = struct {
5454
for (self.failures.items) |*failure| {
5555
failure.deinit(self.allocator);
5656
}
57-
self.failures.deinit();
57+
self.failures.deinit(self.allocator);
5858
}
5959

6060
pub fn addFailure(
@@ -76,7 +76,7 @@ pub const AssertionDiagnostic = struct {
7676
.assertion_index = assertion_index,
7777
.source_file = if (source_file) |file| try self.allocator.dupe(u8, file) else null,
7878
};
79-
try self.failures.append(failure);
79+
try self.failures.append(self.allocator, failure);
8080
}
8181
};
8282

@@ -86,16 +86,16 @@ pub fn hasFailures(diagnostic: *const AssertionDiagnostic) bool {
8686

8787
pub fn reportFailures(diagnostic: *const AssertionDiagnostic, writer: anytype) !void {
8888
for (diagnostic.failures.items) |failure| {
89-
const source_info = if (failure.source_file) |file|
89+
const source_info = if (failure.source_file) |file|
9090
try std.fmt.allocPrint(diagnostic.allocator, " in {s}:{d}", .{ file, failure.assertion_index + 1 })
91-
else
92-
try std.fmt.allocPrint(diagnostic.allocator, " (assertion #{d})", .{ failure.assertion_index + 1 });
91+
else
92+
try std.fmt.allocPrint(diagnostic.allocator, " (assertion #{d})", .{failure.assertion_index + 1});
9393
defer diagnostic.allocator.free(source_info);
9494

9595
switch (failure.reason) {
9696
.status_mismatch => try writer.print("[Fail]{s} Expected status {s}, got {s}\n", .{ source_info, failure.expected, failure.actual }),
97-
.header_mismatch => try writer.print("[Fail]{s} Expected header \"{s}\" to be \"{s}\", got \"{s}\"\n", .{ source_info, failure.assertion_key[8..failure.assertion_key.len-2], failure.expected, failure.actual }),
98-
.header_missing => try writer.print("[Fail]{s} Expected header \"{s}\" to be \"{s}\", but header was missing\n", .{ source_info, failure.assertion_key[8..failure.assertion_key.len-2], failure.expected }),
97+
.header_mismatch => try writer.print("[Fail]{s} Expected header \"{s}\" to be \"{s}\", got \"{s}\"\n", .{ source_info, failure.assertion_key[8 .. failure.assertion_key.len - 2], failure.expected, failure.actual }),
98+
.header_missing => try writer.print("[Fail]{s} Expected header \"{s}\" to be \"{s}\", but header was missing\n", .{ source_info, failure.assertion_key[8 .. failure.assertion_key.len - 2], failure.expected }),
9999
.body_mismatch => try writer.print("[Fail]{s} Expected body \"{s}\", got \"{s}\"\n", .{ source_info, failure.expected, failure.actual }),
100100
.contains_failed => try writer.print("[Fail]{s} Expected {s} to contain \"{s}\", got \"{s}\"\n", .{ source_info, failure.assertion_key, failure.expected, failure.actual }),
101101
.not_contains_failed => try writer.print("[Fail]{s} Expected {s} to NOT contain \"{s}\", got \"{s}\"\n", .{ source_info, failure.assertion_key, failure.expected, failure.actual }),
@@ -123,10 +123,9 @@ fn matchesRegex(text: []const u8, pattern: []const u8) bool {
123123
return compiled_regex.match(text) catch return false;
124124
}
125125

126-
127126
pub fn check(
128-
request: *HttpParser.HttpRequest,
129-
response: Client.HttpResponse,
127+
request: *HttpParser.HttpRequest,
128+
response: Client.HttpResponse,
130129
diagnostic: *AssertionDiagnostic,
131130
source_file: ?[]const u8,
132131
) void {
@@ -292,7 +291,6 @@ fn checkAssertion(
292291
}
293292
}
294293

295-
296294
test "Assertion checker with diagnostics - all pass" {
297295
const allocator = std.testing.allocator;
298296

@@ -346,9 +344,9 @@ test "Assertion checker with diagnostics - all pass" {
346344

347345
var diagnostic = AssertionDiagnostic.init(allocator);
348346
defer diagnostic.deinit();
349-
347+
350348
check(&request, response, &diagnostic, "test.httpspec");
351-
349+
352350
try std.testing.expect(!hasFailures(&diagnostic));
353351
try std.testing.expectEqual(@as(usize, 0), diagnostic.failures.items.len);
354352
}
@@ -400,9 +398,9 @@ test "Assertion checker with not_equal - all pass" {
400398

401399
var diagnostic = AssertionDiagnostic.init(allocator);
402400
defer diagnostic.deinit();
403-
401+
404402
check(&request, response, &diagnostic, "test.httpspec");
405-
403+
406404
try std.testing.expect(!hasFailures(&diagnostic));
407405
try std.testing.expectEqual(@as(usize, 0), diagnostic.failures.items.len);
408406
}
@@ -454,12 +452,12 @@ test "Assertion checker with failures - collects all failures" {
454452

455453
var diagnostic = AssertionDiagnostic.init(allocator);
456454
defer diagnostic.deinit();
457-
455+
458456
check(&request, response, &diagnostic, "test.httpspec");
459-
457+
460458
try std.testing.expect(hasFailures(&diagnostic));
461459
try std.testing.expectEqual(@as(usize, 3), diagnostic.failures.items.len);
462-
460+
463461
try std.testing.expectEqual(FailureReason.status_mismatch, diagnostic.failures.items[0].reason);
464462
try std.testing.expectEqual(FailureReason.body_mismatch, diagnostic.failures.items[1].reason);
465463
try std.testing.expectEqual(FailureReason.header_mismatch, diagnostic.failures.items[2].reason);
@@ -512,9 +510,9 @@ test "HttpParser supports starts_with for status, body, and header" {
512510

513511
var diagnostic = AssertionDiagnostic.init(allocator);
514512
defer diagnostic.deinit();
515-
513+
516514
check(&request, response, &diagnostic, "test.httpspec");
517-
515+
518516
try std.testing.expect(!hasFailures(&diagnostic));
519517
}
520518

@@ -575,9 +573,9 @@ test "HttpParser supports matches_regex and not_matches_regex for status, body,
575573

576574
var diagnostic = AssertionDiagnostic.init(allocator);
577575
defer diagnostic.deinit();
578-
576+
579577
check(&request, response, &diagnostic, "test.httpspec");
580-
578+
581579
try std.testing.expect(!hasFailures(&diagnostic));
582580
}
583581

@@ -624,8 +622,8 @@ test "HttpParser supports contains and not_contains for headers" {
624622

625623
var diagnostic = AssertionDiagnostic.init(allocator);
626624
defer diagnostic.deinit();
627-
625+
628626
check(&request, response, &diagnostic, "test.httpspec");
629-
627+
630628
try std.testing.expect(!hasFailures(&diagnostic));
631629
}

‎src/httpfile/http_client.zig‎

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,39 +57,41 @@ pub const HttpClient = struct {
5757
}
5858

5959
const uri = try Uri.parse(request.url);
60-
var server_header_buf: [4096]u8 = undefined;
60+
// var server_header_buf: [4096]u8 = undefined;
6161

62-
var req = try self.client.open(request.method.?, uri, .{
63-
.server_header_buffer = &server_header_buf,
62+
var req = try self.client.request(request.method.?, uri, .{
63+
// .server_header_buffer = &server_header_buf,
6464
.extra_headers = request.headers.items,
6565
});
66+
6667
defer req.deinit();
6768

6869
if (request.body) |body| {
6970
req.transfer_encoding = .{ .content_length = body.len };
7071
}
7172

72-
try req.send();
73-
7473
if (request.body) |body| {
75-
try req.writeAll(body);
74+
try req.sendBodyComplete(body);
75+
} else {
76+
try req.sendBodiless();
7677
}
7778

78-
try req.finish();
79-
try req.wait();
79+
var raw_response = try req.receiveHead(&.{});
8080

8181
var response = HttpResponse.init(self.allocator);
82-
response.status = req.response.status;
82+
response.status = raw_response.head.status;
8383

84-
var header_iterator = req.response.iterateHeaders();
84+
var header_iterator = raw_response.head.iterateHeaders();
8585
while (header_iterator.next()) |header| {
8686
const name = try self.allocator.dupe(u8, header.name);
8787
const value = try self.allocator.dupe(u8, header.value);
8888
try response.headers.put(name, value);
8989
}
9090

91-
const body_reader = req.reader();
92-
response.body = try body_reader.readAllAlloc(self.allocator, std.math.maxInt(usize));
91+
// Optimal size depends on how you will use the reader.
92+
var reader_buffer: [100]u8 = undefined;
93+
const body_reader = raw_response.reader(&reader_buffer);
94+
response.body = try body_reader.adaptToOldInterface().readAllAlloc(self.allocator, std.math.maxInt(usize));
9395

9496
return response;
9597
}

‎src/httpfile/parser.zig‎

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,17 @@ pub const HttpRequest = struct {
4040
method: ?http.Method,
4141
url: []const u8,
4242
headers: ArrayList(http.Header),
43-
body: ?[]const u8,
43+
body: ?[]u8,
4444
assertions: ArrayList(Assertion),
4545
// TODO: Add a name for the request if needed.
4646

47-
pub fn init(allocator: Allocator) HttpRequest {
47+
pub fn init() HttpRequest {
4848
return .{
4949
.method = null,
5050
.url = "",
51-
.headers = ArrayList(http.Header).init(allocator),
51+
.headers = .empty,
5252
.body = null,
53-
.assertions = ArrayList(Assertion).init(allocator),
53+
.assertions = .empty,
5454
};
5555
}
5656

@@ -60,12 +60,12 @@ pub const HttpRequest = struct {
6060
if (assertion.key.len > 0) allocator.free(assertion.key);
6161
if (assertion.value.len > 0) allocator.free(assertion.value);
6262
}
63-
self.assertions.deinit();
63+
self.assertions.deinit(allocator);
6464
for (self.headers.items) |header| {
6565
if (header.name.len > 0) allocator.free(header.name);
6666
if (header.value.len > 0) allocator.free(header.value);
6767
}
68-
self.headers.deinit();
68+
self.headers.deinit(allocator);
6969
if (self.body) |body| {
7070
if (body.len > 0) allocator.free(body);
7171
}
@@ -81,18 +81,18 @@ pub fn parseFile(allocator: Allocator, file_path: []const u8) !ArrayList(HttpReq
8181
}
8282

8383
pub fn parseContent(allocator: Allocator, content: []const u8) !ArrayList(HttpRequest) {
84-
var requests = ArrayList(HttpRequest).init(allocator);
84+
var requests: ArrayList(HttpRequest) = .empty;
8585
errdefer {
8686
for (requests.items) |*request| request.deinit(allocator);
87-
requests.deinit();
87+
requests.deinit(allocator);
8888
}
8989

90-
var current_request = HttpRequest.init(allocator);
90+
var current_request = HttpRequest.init();
9191
errdefer current_request.deinit(allocator);
9292
var state: ?ParserState = null;
9393
var lines = std.mem.splitScalar(u8, content, '\n');
94-
var body_buffer = ArrayList(u8).init(allocator);
95-
defer body_buffer.deinit();
94+
var body_buffer: ArrayList(u8) = .empty;
95+
defer body_buffer.deinit(allocator);
9696

9797
while (lines.next()) |line| {
9898
const trimmed_line = std.mem.trim(u8, line, &std.ascii.whitespace);
@@ -106,8 +106,8 @@ pub fn parseContent(allocator: Allocator, content: []const u8) !ArrayList(HttpRe
106106
current_request.body = try allocator.dupe(u8, body_buffer.items);
107107
body_buffer.clearRetainingCapacity();
108108
}
109-
try requests.append(current_request);
110-
current_request = HttpRequest.init(allocator);
109+
try requests.append(allocator, current_request);
110+
current_request = HttpRequest.init();
111111
state = null;
112112
}
113113
continue;
@@ -125,7 +125,7 @@ pub fn parseContent(allocator: Allocator, content: []const u8) !ArrayList(HttpRe
125125
.assertion_type = assertion_type,
126126
};
127127
if (assertion.key.len == 0 or assertion.value.len == 0) return error.InvalidAssertionFormat;
128-
try current_request.assertions.append(assertion);
128+
try current_request.assertions.append(allocator, assertion);
129129
continue;
130130
}
131131
if (std.mem.startsWith(u8, trimmed_line, "#") or std.mem.startsWith(u8, trimmed_line, "//")) {
@@ -144,7 +144,7 @@ pub fn parseContent(allocator: Allocator, content: []const u8) !ArrayList(HttpRe
144144
if (std.mem.indexOf(u8, trimmed_line, ":")) |colon_pos| {
145145
const header_name = std.mem.trim(u8, trimmed_line[0..colon_pos], &std.ascii.whitespace);
146146
const header_value = std.mem.trim(u8, trimmed_line[colon_pos + 1 ..], &std.ascii.whitespace);
147-
try current_request.headers.append(http.Header{
147+
try current_request.headers.append(allocator, http.Header{
148148
.name = try allocator.dupe(u8, header_name),
149149
.value = try allocator.dupe(u8, header_value),
150150
});
@@ -154,16 +154,16 @@ pub fn parseContent(allocator: Allocator, content: []const u8) !ArrayList(HttpRe
154154
continue;
155155
}
156156
if (state == .body) {
157-
try body_buffer.appendSlice(trimmed_line);
158-
try body_buffer.append('\n');
157+
try body_buffer.appendSlice(allocator, trimmed_line);
158+
try body_buffer.append(allocator, '\n');
159159
continue;
160160
}
161161
}
162162
if (current_request.method != null) {
163163
if (state == .body and body_buffer.items.len > 0) {
164164
current_request.body = try allocator.dupe(u8, body_buffer.items);
165165
}
166-
try requests.append(current_request);
166+
try requests.append(allocator, current_request);
167167
}
168168
return requests;
169169
}

0 commit comments

Comments
 (0)