| 1 | const std = @import("std"); |
| 2 | const http = @import("http"); |
| 3 | const json = @import("json"); |
| 4 | const expect = @import("expect").expect; |
| 5 | const extras = @import("extras"); |
| 6 | const Scheme = enum { http }; |
| 7 | const Compression = enum { deflate, gzip, brotli }; |
| 8 | |
| 9 | test { |
| 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 | } |
| 16 | test { |
| 17 | _ = &http.Status.phrase; |
| 18 | _ = &http.Status.digits; |
| 19 | } |
| 20 | |
| 21 | // zig fmt: off |
| 22 | test { try httpbinMethod(.http, .GET); } |
| 23 | test { try httpbinMethod(.http, .POST); } |
| 24 | test { try httpbinMethod(.http, .PUT); } |
| 25 | test { try httpbinMethod(.http, .PATCH); } |
| 26 | test { try httpbinMethod(.http, .DELETE); } |
| 27 | test { try httpbinUserAgent(.http); } |
| 28 | test { try httpbinCompresssed(.http, .deflate); } |
| 29 | test { try httpbinCompresssed(.http, .gzip); } |
| 30 | test { try httpbinCompresssed(.http, .brotli); } |
| 31 | // zig fmt: on |
| 32 | |
| 33 | fn 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 | } |
| 47 | fn 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 | } |
| 61 | fn 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 | } |