authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-02-05 05:23:33 -08:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-02-05 05:23:33 -08:00
logc2eed6c101cd9d0dbffee42fdb91638c012ac07a
tree1a74b1334accfbbb7aa77339b1fc37e2349974a0
parent42ac83cb327249a36550eae6e204da69e7164cd6

start httpbinCompresssed


2 files changed, 26 insertions(+), 0 deletions(-)

http.zig+9
......@@ -317,4 +317,13 @@ pub const HeadersMap = struct {
317317 pub fn value(map: *const HeadersMap, idx: usize) []const u8 {
318318 return map.data.items(idx * 4 + 2);
319319 }
320
321 pub fn find(map: *const HeadersMap, needle: []const u8) ?[]const u8 {
322 for (0..map.count()) |i| {
323 if (std.mem.eql(u8, map.name(i), needle)) {
324 return map.value(i);
325 }
326 }
327 return null;
328 }
320329};
test.zig+17
......@@ -4,6 +4,7 @@ const json = @import("json");
44const expect = @import("expect").expect;
55const extras = @import("extras");
66const Scheme = enum { http };
7const Compression = enum { deflate, gzip, brotli };
78
89test {
910 const allocator = std.testing.allocator;
......@@ -20,6 +21,9 @@ test { try httpbinMethod(.http, .PUT); }
2021test { try httpbinMethod(.http, .PATCH); }
2122test { try httpbinMethod(.http, .DELETE); }
2223test { try httpbinUserAgent(.http); }
24test { try httpbinCompresssed(.http, .deflate); }
25test { try httpbinCompresssed(.http, .gzip); }
26test { try httpbinCompresssed(.http, .brotli); }
2327// zig fmt: on
2428
2529fn httpbinMethod(comptime scheme: Scheme, comptime method: http.Method) !void {
......@@ -50,3 +54,16 @@ fn httpbinUserAgent(comptime scheme: Scheme) !void {
5054 defer doc.release();
5155 try expect(doc.root.object().getS("user-agent")).toEqualString("WIP https://github.com/nektro/zig-net-http");
5256}
57fn httpbinCompresssed(comptime scheme: Scheme, comptime compression: Compression) !void {
58 const allocator = std.testing.allocator;
59 const url = @tagName(scheme) ++ "://httpbin.org/" ++ @tagName(compression);
60 var req = try http.open(allocator, .GET, url);
61 defer req.close();
62 try req.writeUA();
63 try req.send();
64 try expect(req.status).toEqual(.ok);
65 try expect(req.headers.find("content-encoding")).toEqualString(switch (compression) {
66 else => |tag| @tagName(tag),
67 .brotli => "br",
68 });
69}