diff --git a/http.zig b/http.zig index a815aaefb78b52baca7eeb0da22bfa4c5205cc22..e387bb48d36e8cdd9568587e1311b2c2c13a5b0b 100644 --- a/http.zig +++ b/http.zig @@ -317,4 +317,13 @@ pub const HeadersMap = struct { pub fn value(map: *const HeadersMap, idx: usize) []const u8 { return map.data.items(idx * 4 + 2); } + + pub fn find(map: *const HeadersMap, needle: []const u8) ?[]const u8 { + for (0..map.count()) |i| { + if (std.mem.eql(u8, map.name(i), needle)) { + return map.value(i); + } + } + return null; + } }; diff --git a/test.zig b/test.zig index ad0d2c74d59df09c17c199d42d0e9677c125f830..a1aad74d9179eb8a00c683b799339bb1eb4fc4b6 100644 --- a/test.zig +++ b/test.zig @@ -4,6 +4,7 @@ const json = @import("json"); const expect = @import("expect").expect; const extras = @import("extras"); const Scheme = enum { http }; +const Compression = enum { deflate, gzip, brotli }; test { const allocator = std.testing.allocator; @@ -20,6 +21,9 @@ test { try httpbinMethod(.http, .PUT); } test { try httpbinMethod(.http, .PATCH); } test { try httpbinMethod(.http, .DELETE); } test { try httpbinUserAgent(.http); } +test { try httpbinCompresssed(.http, .deflate); } +test { try httpbinCompresssed(.http, .gzip); } +test { try httpbinCompresssed(.http, .brotli); } // zig fmt: on fn httpbinMethod(comptime scheme: Scheme, comptime method: http.Method) !void { @@ -50,3 +54,16 @@ fn httpbinUserAgent(comptime scheme: Scheme) !void { defer doc.release(); try expect(doc.root.object().getS("user-agent")).toEqualString("WIP https://github.com/nektro/zig-net-http"); } +fn httpbinCompresssed(comptime scheme: Scheme, comptime compression: Compression) !void { + const allocator = std.testing.allocator; + const url = @tagName(scheme) ++ "://httpbin.org/" ++ @tagName(compression); + var req = try http.open(allocator, .GET, url); + defer req.close(); + try req.writeUA(); + try req.send(); + try expect(req.status).toEqual(.ok); + try expect(req.headers.find("content-encoding")).toEqualString(switch (compression) { + else => |tag| @tagName(tag), + .brotli => "br", + }); +}