Skip to content

Commit 0f87d78

Browse files
committed
add assertion parsing
1 parent 5349a3a commit 0f87d78

1 file changed

Lines changed: 88 additions & 2 deletions

File tree

‎src/httpfile/parser.zig‎

Lines changed: 88 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,61 @@ const ArrayList = std.ArrayList;
66

77
const ParserState = enum { headers, body };
88

9+
const AssertionType = enum {
10+
equal,
11+
not_equal,
12+
contains,
13+
not_contains,
14+
starts_with,
15+
ends_with,
16+
// matches_regex, TODO: Soon.
17+
// not_matches_regex,
18+
};
19+
20+
const Assertion = struct {
21+
key: []const u8,
22+
value: []const u8,
23+
// TODO: This shouldn't be nullable.
24+
// We can change the way this value is parsed so that
25+
// null is not a valid option.
26+
assertion_type: ?AssertionType,
27+
};
28+
929
pub const HttpRequest = struct {
1030
method: ?http.Method,
1131
url: []const u8,
1232
headers: ArrayList(http.Header),
1333
body: ?[]const u8,
34+
assertions: ArrayList(Assertion),
1435

1536
pub fn init(allocator: Allocator) HttpRequest {
1637
return .{
1738
.method = null,
1839
.url = "",
1940
.headers = ArrayList(http.Header).init(allocator),
2041
.body = null,
42+
.assertions = ArrayList(Assertion).init(allocator),
2143
};
2244
}
2345

2446
pub fn deinit(self: *HttpRequest, allocator: std.mem.Allocator) void {
2547
allocator.free(self.url);
2648

49+
for (self.assertions.items) |assertion| {
50+
allocator.free(assertion.key);
51+
allocator.free(assertion.value);
52+
}
53+
self.assertions.deinit();
54+
2755
for (self.headers.items) |header| {
2856
allocator.free(header.name);
2957
allocator.free(header.value);
3058
}
59+
self.headers.deinit();
3160

3261
if (self.body) |body| {
3362
allocator.free(body);
3463
}
35-
36-
self.headers.deinit();
3764
}
3865
};
3966

@@ -92,6 +119,39 @@ pub fn parseContent(allocator: std.mem.Allocator, content: []const u8) !ArrayLis
92119
continue;
93120
}
94121

122+
if (std.mem.startsWith(u8, trimmed_line, "//#")) {
123+
// These are assertions.
124+
var assertion_tokens = std.mem.tokenizeScalar(u8, trimmed_line[3..], ' ');
125+
var assertion = Assertion{
126+
.key = "",
127+
.value = "",
128+
.assertion_type = null,
129+
};
130+
if (assertion_tokens.next()) |first_token| {
131+
// The first token is the assertion type.
132+
assertion.key = first_token;
133+
} else {
134+
return error.InvalidAssertionFormat;
135+
}
136+
if (assertion_tokens.next()) |second_token| {
137+
// The first token is the assertion type.
138+
assertion.assertion_type = std.meta.stringToEnum(AssertionType, second_token) orelse null;
139+
} else {
140+
return error.InvalidAssertionFormat;
141+
}
142+
if (assertion_tokens.next()) |third_token| {
143+
// The first token is the assertion type.
144+
assertion.value = third_token;
145+
} else {
146+
return error.InvalidAssertionFormat;
147+
}
148+
if (assertion.key.len == 0 or assertion.value.len == 0 or assertion.assertion_type == null) {
149+
return error.InvalidAssertionFormat;
150+
}
151+
try current_request.assertions.append(assertion);
152+
continue;
153+
}
154+
95155
if (std.mem.startsWith(u8, trimmed_line, "#") or std.mem.startsWith(u8, trimmed_line, "//")) {
96156
continue;
97157
}
@@ -175,3 +235,29 @@ test "HttpParser from String Contents" {
175235
try std.testing.expectEqual(0, (requests.items[0].body orelse "").len);
176236
try std.testing.expect(0 != (requests.items[1].body orelse "").len);
177237
}
238+
239+
test "HttpParser parses assertions" {
240+
const test_http_contents =
241+
\\GET https://api.example.com
242+
\\Accept: */*
243+
\\Authorization: Bearer ABC123
244+
\\
245+
\\//# status_code equal 200
246+
;
247+
248+
var requests = try parseContent(std.testing.allocator, test_http_contents);
249+
defer {
250+
for (requests.items) |*request| {
251+
request.deinit(std.testing.allocator);
252+
}
253+
requests.deinit();
254+
}
255+
256+
try std.testing.expectEqual(http.Method.GET, requests.items[0].method);
257+
try std.testing.expectEqualStrings("https://api.example.com", requests.items[0].url);
258+
try std.testing.expectEqualStrings("status", requests.items[0].assertions.items[0].assertions[0].key);
259+
try std.testing.expectEqual(AssertionType.equal, requests.items[0].assertions.items[0].assertions[0].assertion_type);
260+
try std.testing.expectEqualStrings("200", requests.items[0].assertions.items[0].assertions[0].value);
261+
try std.testing.expectEqual(0, (requests.items[0].body orelse "").len);
262+
try std.testing.expect(0 != (requests.items[1].body orelse "").len);
263+
}

0 commit comments

Comments
 (0)