1const std = @import("std");
2const http = @import("http");
3const json = @import("json");
4const expect = @import("expect").expect;
5const extras = @import("extras");
6const Scheme = enum { http };
7const Compression = enum { deflate, gzip, brotli };
8
9test {
10 const allocator = std.testing.allocator;
11 var req = try http.open(allocator, .GET, "http://he.net/");
12 defer req.close();
13 try req.writeUA();
14 try req.send();
15}
16test {
17 _ = &http.Status.phrase;
18 _ = &http.Status.digits;
19}
20
21// zig fmt: off
22test { try httpbinMethod(.http, .GET); }
23test { try httpbinMethod(.http, .POST); }
24test { try httpbinMethod(.http, .PUT); }
25test { try httpbinMethod(.http, .PATCH); }
26test { try httpbinMethod(.http, .DELETE); }
27test { try httpbinUserAgent(.http); }
28test { try httpbinCompresssed(.http, .deflate); }
29test { try httpbinCompresssed(.http, .gzip); }
30test { try httpbinCompresssed(.http, .brotli); }
31// zig fmt: on
32
33fn httpbinMethod(comptime scheme: Scheme, comptime method: http.Method) !void {
34 const allocator = std.testing.allocator;
35 const url = @tagName(scheme) ++ "://httpbin.org/" ++ comptime extras.asciiLowerComptime(@tagName(method));
36 var req = try http.open(allocator, method, url);
37 defer req.close();
38 try req.writeUA();
39 try req.send();
40 try expect(req.status).toEqual(.ok);
41 const doc = try json.parse(allocator, "httpbinMethod", &req, .{ .support_trailing_commas = false, .maximum_depth = 2 });
42 defer doc.deinit(allocator);
43 doc.acquire();
44 defer doc.release();
45 try expect(doc.root.object().getS("url")).toEqualString(url);
46}
47fn httpbinUserAgent(comptime scheme: Scheme) !void {
48 const allocator = std.testing.allocator;
49 const url = @tagName(scheme) ++ "://httpbin.org/user-agent";
50 var req = try http.open(allocator, .GET, url);
51 defer req.close();
52 try req.writeUA();
53 try req.send();
54 try expect(req.status).toEqual(.ok);
55 const doc = try json.parse(allocator, "httpbinMethod", &req, .{ .support_trailing_commas = false, .maximum_depth = 2 });
56 defer doc.deinit(allocator);
57 doc.acquire();
58 defer doc.release();
59 try expect(doc.root.object().getS("user-agent")).toEqualString("WIP https://github.com/nektro/zig-net-http");
60}
61fn httpbinCompresssed(comptime scheme: Scheme, comptime compression: Compression) !void {
62 const allocator = std.testing.allocator;
63 const url = @tagName(scheme) ++ "://httpbin.org/" ++ @tagName(compression);
64 var req = try http.open(allocator, .GET, url);
65 defer req.close();
66 try req.writeUA();
67 try req.send();
68 try expect(req.status).toEqual(.ok);
69 try expect(req.headers.find("content-encoding")).toEqualString(switch (compression) {
70 else => |tag| @tagName(tag),
71 .brotli => "br",
72 });
73}