Skip to content

Commit 9b3ffe4

Browse files
committed
Fix: Better handling of missing json keys from fast
1 parent 62bc686 commit 9b3ffe4

File tree

1 file changed

+41
-8
lines changed

1 file changed

+41
-8
lines changed

src/lib/fast.zig

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,19 @@ const FastError = error{
1515
ConnectionTimeout,
1616
};
1717

18-
const Location = struct {
19-
city: []const u8,
20-
country: []const u8,
21-
};
18+
const Location = struct { city: []const u8, country: []const u8 };
2219

2320
const Client = struct {
2421
ip: []const u8,
25-
asn: []const u8,
26-
isp: []const u8,
27-
location: Location,
22+
asn: ?[]const u8 = null,
23+
isp: ?[]const u8 = null,
24+
location: ?Location = null,
2825
};
2926

3027
const Target = struct {
3128
name: []const u8,
3229
url: []const u8,
33-
location: Location,
30+
location: ?Location = null,
3431
};
3532

3633
const FastResponse = struct {
@@ -271,3 +268,39 @@ test "extract_token" {
271268
defer allocator.free(token);
272269
try testing.expect(std.mem.eql(u8, token, "abcdef123456"));
273270
}
271+
272+
test "parse_response_without_isp" {
273+
const response =
274+
\\{"client":{"ip":"87.52.107.67","asn":"3292","location":{"city":"Test","country":"DK"}},"targets":[{"name":"https://example.com/0","url":"https://example.com/0","location":{"city":"Test","country":"DK"}}]}
275+
;
276+
const allocator = testing.allocator;
277+
278+
const urls = try Fast.parse_response_urls(response, allocator);
279+
defer {
280+
for (urls.items) |url| {
281+
allocator.free(url);
282+
}
283+
urls.deinit();
284+
}
285+
286+
try testing.expect(urls.items.len == 1);
287+
try testing.expect(std.mem.eql(u8, urls.items[0], "https://example.com/0"));
288+
}
289+
290+
test "parse_response_minimal_client" {
291+
const response =
292+
\\{"client":{"ip":"87.52.107.67"},"targets":[{"name":"https://example.com/0","url":"https://example.com/0"}]}
293+
;
294+
const allocator = testing.allocator;
295+
296+
const urls = try Fast.parse_response_urls(response, allocator);
297+
defer {
298+
for (urls.items) |url| {
299+
allocator.free(url);
300+
}
301+
urls.deinit();
302+
}
303+
304+
try testing.expect(urls.items.len == 1);
305+
try testing.expect(std.mem.eql(u8, urls.items[0], "https://example.com/0"));
306+
}

0 commit comments

Comments
 (0)