Skip to content

Commit c5c5b37

Browse files
committed
add support for not_contains assertions
1 parent b439a42 commit c5c5b37

1 file changed

Lines changed: 30 additions & 11 deletions

File tree

‎src/httpfile/assertion_checker.zig‎

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,49 +11,49 @@ pub fn check(request: *HttpParser.HttpRequest, response: Client.HttpResponse) !v
1111
if (std.ascii.eqlIgnoreCase(assertion.key, "status")) {
1212
const assert_status_code = try std.fmt.parseInt(u16, assertion.value, 10);
1313
if (response.status != try std.meta.intToEnum(http.Status, assert_status_code)) {
14-
stderr.print("[Fail] Expected status code {d}, got {d}\n", .{ assert_status_code, @intFromEnum(response.status.?) }) catch return error.StatusCodeMismatch;
14+
stderr.print("[Fail] Expected status code {d}, got {d}\n", .{ assert_status_code, @intFromEnum(response.status.?) }) catch {};
1515
return error.StatusCodeMismatch;
1616
}
1717
} else if (std.ascii.eqlIgnoreCase(assertion.key, "body")) {
1818
if (!std.mem.eql(u8, response.body, assertion.value)) {
19-
stderr.print("[Fail] Expected body content \"{s}\", got \"{s}\"\n", .{ assertion.value, response.body }) catch return error.BodyContentMismatch;
19+
stderr.print("[Fail] Expected body content \"{s}\", got \"{s}\"\n", .{ assertion.value, response.body }) catch {};
2020
return error.BodyContentMismatch;
2121
}
2222
} else if (std.mem.startsWith(u8, assertion.key, "header[\"")) {
2323
// Extract the header name from the assertion key
2424
const header_name = assertion.key[8 .. assertion.key.len - 2];
2525
const actual_value = response.headers.get(header_name);
2626
if (actual_value == null or !std.ascii.eqlIgnoreCase(actual_value.?, assertion.value)) {
27-
stderr.print("[Fail] Expected header \"{s}\" to be \"{s}\", got \"{s}\"\n", .{ header_name, assertion.value, actual_value orelse "null" }) catch return error.HeaderMismatch;
27+
stderr.print("[Fail] Expected header \"{s}\" to be \"{s}\", got \"{s}\"\n", .{ header_name, assertion.value, actual_value orelse "null" }) catch {};
2828
return error.HeaderMismatch;
2929
}
3030
} else {
31-
stderr.print("[Fail] Invalid assertion key: {s}\n", .{assertion.key}) catch return error.InvalidAssertionKey;
31+
stderr.print("[Fail] Invalid assertion key: {s}\n", .{assertion.key}) catch {};
3232
return error.InvalidAssertionKey;
3333
}
3434
},
3535
.not_equal => {
3636
if (std.ascii.eqlIgnoreCase(assertion.key, "status")) {
3737
const assert_status_code = try std.fmt.parseInt(u16, assertion.value, 10);
3838
if (response.status == try std.meta.intToEnum(http.Status, assert_status_code)) {
39-
stderr.print("[Fail] Expected status code to NOT equal {d}, got {d}\n", .{ assert_status_code, @intFromEnum(response.status.?) }) catch return error.StatusCodesMatchButShouldnt;
39+
stderr.print("[Fail] Expected status code to NOT equal {d}, got {d}\n", .{ assert_status_code, @intFromEnum(response.status.?) }) catch {};
4040
return error.StatusCodesMatchButShouldnt;
4141
}
4242
} else if (std.ascii.eqlIgnoreCase(assertion.key, "body")) {
4343
if (std.mem.eql(u8, response.body, assertion.value)) {
44-
stderr.print("[Fail] Expected body content to NOT equal \"{s}\", got \"{s}\"\n", .{ assertion.value, response.body }) catch return error.BodyContentMatchesButShouldnt;
44+
stderr.print("[Fail] Expected body content to NOT equal \"{s}\", got \"{s}\"\n", .{ assertion.value, response.body }) catch {};
4545
return error.BodyContentMatchesButShouldnt;
4646
}
4747
} else if (std.mem.startsWith(u8, assertion.key, "header[\"")) {
4848
// Extract the header name from the assertion key
4949
const header_name = assertion.key[8 .. assertion.key.len - 2];
5050
const actual_value = response.headers.get(header_name);
5151
if (actual_value != null and std.ascii.eqlIgnoreCase(actual_value.?, assertion.value)) {
52-
stderr.print("[Fail] Expected header \"{s}\" to NOT equal \"{s}\", got \"{s}\"\n", .{ header_name, assertion.value, actual_value orelse "null" }) catch return error.HeaderMatchesButShouldnt;
52+
stderr.print("[Fail] Expected header \"{s}\" to NOT equal \"{s}\", got \"{s}\"\n", .{ header_name, assertion.value, actual_value orelse "null" }) catch {};
5353
return error.HeaderMatchesButShouldnt;
5454
}
5555
} else {
56-
stderr.print("[Fail] Invalid assertion key: {s}\n", .{assertion.key}) catch return error.InvalidAssertionKey;
56+
stderr.print("[Fail] Invalid assertion key: {s}\n", .{assertion.key}) catch {};
5757
return error.InvalidAssertionKey;
5858
}
5959
},
@@ -77,16 +77,35 @@ pub fn check(request: *HttpParser.HttpRequest, response: Client.HttpResponse) !v
7777
const status_code = @intFromEnum(response.status.?); // .? if status is optional
7878
const status_str = std.fmt.bufPrint(&status_buf, "{}", .{status_code}) catch return error.StatusCodeFormat;
7979
if (std.mem.indexOf(u8, status_str, assertion.value) == null) {
80-
stderr.print("[Fail] Expected status code to contain \"{s}\", got \"{s}\"\n", .{ assertion.value, status_str }) catch return error.StatusCodeNotContains;
80+
stderr.print("[Fail] Expected status code to contain \"{s}\", got \"{s}\"\n", .{ assertion.value, status_str }) catch {};
8181
return error.StatusCodeNotContains;
8282
}
8383
} else if (std.ascii.eqlIgnoreCase(assertion.key, "body")) {
8484
if (std.mem.indexOf(u8, response.body, assertion.value) == null) {
85-
stderr.print("[Fail] Expected body content to NOT contain \"{s}\", got \"{s}\"\n", .{ assertion.value, response.body }) catch return error.BodyContentNotContains;
85+
stderr.print("[Fail] Expected body content to NOT contain \"{s}\", got \"{s}\"\n", .{ assertion.value, response.body }) catch {};
8686
return error.BodyContentNotContains;
8787
}
8888
} else {
89-
stderr.print("[Fail] Invalid assertion key for contains: {s}\n", .{assertion.key}) catch return error.InvalidAssertionKey;
89+
stderr.print("[Fail] Invalid assertion key for contains: {s}\n", .{assertion.key}) catch {};
90+
return error.InvalidAssertionKey;
91+
}
92+
},
93+
.not_contains => {
94+
if (std.ascii.eqlIgnoreCase(assertion.key, "status")) {
95+
var status_buf: [3]u8 = undefined;
96+
const status_code = @intFromEnum(response.status.?); // .? if status is optional
97+
const status_str = std.fmt.bufPrint(&status_buf, "{}", .{status_code}) catch return error.StatusCodeFormat;
98+
if (std.mem.indexOf(u8, status_str, assertion.value) != null) {
99+
stderr.print("[Fail] Expected status code to NOT contain \"{s}\", got \"{s}\"\n", .{ assertion.value, status_str }) catch {};
100+
return error.StatusCodeContainsButShouldnt;
101+
}
102+
} else if (std.ascii.eqlIgnoreCase(assertion.key, "body")) {
103+
if (std.mem.indexOf(u8, response.body, assertion.value) == null) {
104+
stderr.print("[Fail] Expected body content to NOT contain \"{s}\", got \"{s}\"\n", .{ assertion.value, response.body }) catch {};
105+
return error.BodyContentContainsButShouldnt;
106+
}
107+
} else {
108+
stderr.print("[Fail] Invalid assertion key for contains: {s}\n", .{assertion.key}) catch {};
90109
return error.InvalidAssertionKey;
91110
}
92111
},

0 commit comments

Comments
 (0)