@@ -22,7 +22,6 @@ pub fn check(request: *HttpParser.HttpRequest, response: Client.HttpResponse) !v
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 ];
25-
2625 const actual_value = response .headers .get (header_name );
2726 if (actual_value == null or ! std .ascii .eqlIgnoreCase (actual_value .? , assertion .value )) {
2827 stderr .print ("[Fail] Expected header \" {s}\" to be \" {s}\" , got \" {s}\" \n " , .{ header_name , assertion .value , actual_value orelse "null" }) catch return error .HeaderMismatch ;
@@ -33,6 +32,32 @@ pub fn check(request: *HttpParser.HttpRequest, response: Client.HttpResponse) !v
3332 return error .InvalidAssertionKey ;
3433 }
3534 },
35+ .not_equal = > {
36+ if (std .ascii .eqlIgnoreCase (assertion .key , "status" )) {
37+ const assert_status_code = try std .fmt .parseInt (u16 , assertion .value , 10 );
38+ 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 ;
40+ return error .StatusCodesMatchButShouldnt ;
41+ }
42+ } else if (std .ascii .eqlIgnoreCase (assertion .key , "body" )) {
43+ 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 ;
45+ return error .BodyContentMatchesButShouldnt ;
46+ }
47+ } else if (std .mem .startsWith (u8 , assertion .key , "header[\" " )) {
48+ // Extract the header name from the assertion key
49+ const header_name = assertion .key [8 .. assertion .key .len - 2 ];
50+ const actual_value = response .headers .get (header_name );
51+ 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 ;
53+ return error .HeaderMatchesButShouldnt ;
54+ }
55+ } else {
56+ stderr .print ("[Fail] Invalid assertion key: {s}\n " , .{assertion .key }) catch return error .InvalidAssertionKey ;
57+ return error .InvalidAssertionKey ;
58+ }
59+ },
60+
3661 // .header => {
3762 // // assertion.key is header[""] so we need to
3863 // // parse it out of the quotes
@@ -101,7 +126,56 @@ test "HttpParser parses assertions" {
101126 .assertion_type = .equal ,
102127 });
103128
104- const request = HttpParser.HttpRequest {
129+ var request = HttpParser.HttpRequest {
130+ .method = .GET ,
131+ .url = "https://api.example.com" ,
132+ .headers = std .ArrayList (http .Header ).init (allocator ),
133+ .assertions = assertions ,
134+ .body = null ,
135+ };
136+
137+ var response_headers = std .StringHashMap ([]const u8 ).init (allocator );
138+ try response_headers .put ("content-type" , "application/json" );
139+ defer response_headers .deinit ();
140+
141+ const body = try allocator .dupe (u8 , "Response body content" );
142+ defer allocator .free (body );
143+ const response = Client.HttpResponse {
144+ .status = http .Status .ok ,
145+ .headers = response_headers ,
146+ .body = body ,
147+ .allocator = allocator ,
148+ };
149+
150+ try check (& request , response );
151+ }
152+
153+ test "HttpParser handles NotEquals" {
154+ const allocator = std .testing .allocator ;
155+
156+ var assertions = std .ArrayList (HttpParser .Assertion ).init (allocator );
157+ defer assertions .deinit ();
158+
159+ try assertions .append (HttpParser.Assertion {
160+ .key = "status" ,
161+ .value = "400" ,
162+ .assertion_type = .not_equal ,
163+ });
164+
165+ try assertions .append (HttpParser.Assertion {
166+ .key = "body" ,
167+ .value = "Response body content!!!" ,
168+ .assertion_type = .not_equal ,
169+ });
170+
171+ // TODO: This should also work with header[\"Content-Type\"] as the key
172+ try assertions .append (HttpParser.Assertion {
173+ .key = "header[\" content-type\" ]" ,
174+ .value = "application/xml" ,
175+ .assertion_type = .not_equal ,
176+ });
177+
178+ var request = HttpParser.HttpRequest {
105179 .method = .GET ,
106180 .url = "https://api.example.com" ,
107181 .headers = std .ArrayList (http .Header ).init (allocator ),
@@ -122,5 +196,5 @@ test "HttpParser parses assertions" {
122196 .allocator = allocator ,
123197 };
124198
125- try check (request , response );
199+ try check (& request , response );
126200}
0 commit comments