From 9b3ffe4422740dcf38cd79809be6864d08f1573f Mon Sep 17 00:00:00 2001 From: mikkelam Date: Tue, 24 Jun 2025 19:31:02 +0200 Subject: [PATCH] Fix: Better handling of missing json keys from fast --- src/lib/fast.zig | 49 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 8 deletions(-) diff --git a/src/lib/fast.zig b/src/lib/fast.zig index d2bf12e..d206c99 100644 --- a/src/lib/fast.zig +++ b/src/lib/fast.zig @@ -15,22 +15,19 @@ const FastError = error{ ConnectionTimeout, }; -const Location = struct { - city: []const u8, - country: []const u8, -}; +const Location = struct { city: []const u8, country: []const u8 }; const Client = struct { ip: []const u8, - asn: []const u8, - isp: []const u8, - location: Location, + asn: ?[]const u8 = null, + isp: ?[]const u8 = null, + location: ?Location = null, }; const Target = struct { name: []const u8, url: []const u8, - location: Location, + location: ?Location = null, }; const FastResponse = struct { @@ -271,3 +268,39 @@ test "extract_token" { defer allocator.free(token); try testing.expect(std.mem.eql(u8, token, "abcdef123456")); } + +test "parse_response_without_isp" { + const response = + \\{"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"}}]} + ; + const allocator = testing.allocator; + + const urls = try Fast.parse_response_urls(response, allocator); + defer { + for (urls.items) |url| { + allocator.free(url); + } + urls.deinit(); + } + + try testing.expect(urls.items.len == 1); + try testing.expect(std.mem.eql(u8, urls.items[0], "https://example.com/0")); +} + +test "parse_response_minimal_client" { + const response = + \\{"client":{"ip":"87.52.107.67"},"targets":[{"name":"https://example.com/0","url":"https://example.com/0"}]} + ; + const allocator = testing.allocator; + + const urls = try Fast.parse_response_urls(response, allocator); + defer { + for (urls.items) |url| { + allocator.free(url); + } + urls.deinit(); + } + + try testing.expect(urls.items.len == 1); + try testing.expect(std.mem.eql(u8, urls.items[0], "https://example.com/0")); +}