| author | |
| committer | |
| log | 3d61890d2405dc861db1e9e5a334ea6f2415a8f1 |
| tree | 1f7f53ffa630c73197aa17eea9114605362add89 |
| parent | cf4a2c4d187f3aae8ace72d28fcb1e9b4b80e989 |
making it no longer dead code. it is currently failing.3 files changed, 77 insertions(+), 34 deletions(-)
lib/std/http.zig+1| ... | ... | @@ -315,4 +315,5 @@ test { |
| 315 | 315 | _ = Method; |
| 316 | 316 | _ = Server; |
| 317 | 317 | _ = Status; |
| 318 | _ = @import("http/test.zig"); | |
| 318 | 319 | } |
lib/std/http/test.zig created+76| ... | ... | @@ -0,0 +1,76 @@ |
| 1 | const std = @import("std"); | |
| 2 | ||
| 3 | test "trailers" { | |
| 4 | const gpa = std.testing.allocator; | |
| 5 | ||
| 6 | var http_server = std.http.Server.init(.{ | |
| 7 | .reuse_address = true, | |
| 8 | }); | |
| 9 | const address = try std.net.Address.parseIp("127.0.0.1", 0); | |
| 10 | try http_server.listen(address); | |
| 11 | ||
| 12 | const port = http_server.socket.listen_address.in.getPort(); | |
| 13 | ||
| 14 | const server_thread = try std.Thread.spawn(.{}, serverThread, .{&http_server}); | |
| 15 | defer server_thread.join(); | |
| 16 | ||
| 17 | var client: std.http.Client = .{ .allocator = gpa }; | |
| 18 | defer client.deinit(); | |
| 19 | ||
| 20 | const location = try std.fmt.allocPrint(gpa, "http://127.0.0.1:{d}/trailer", .{port}); | |
| 21 | defer gpa.free(location); | |
| 22 | const uri = try std.Uri.parse(location); | |
| 23 | ||
| 24 | var server_header_buffer: [1024]u8 = undefined; | |
| 25 | var req = try client.open(.GET, uri, .{ | |
| 26 | .server_header_buffer = &server_header_buffer, | |
| 27 | }); | |
| 28 | defer req.deinit(); | |
| 29 | ||
| 30 | try req.send(.{}); | |
| 31 | try req.wait(); | |
| 32 | ||
| 33 | const body = try req.reader().readAllAlloc(gpa, 8192); | |
| 34 | defer gpa.free(body); | |
| 35 | ||
| 36 | try std.testing.expectEqualStrings("Hello, World!\n", body); | |
| 37 | if (true) @panic("TODO implement inspecting custom headers in responses"); | |
| 38 | //try testing.expectEqualStrings("aaaa", req.response.headers.getFirstValue("x-checksum").?); | |
| 39 | ||
| 40 | // connection has been kept alive | |
| 41 | try std.testing.expect(client.connection_pool.free_len == 1); | |
| 42 | } | |
| 43 | ||
| 44 | fn serverThread(http_server: *std.http.Server) anyerror!void { | |
| 45 | const gpa = std.testing.allocator; | |
| 46 | ||
| 47 | var header_buffer: [1024]u8 = undefined; | |
| 48 | var remaining: usize = 1; | |
| 49 | accept: while (remaining != 0) : (remaining -= 1) { | |
| 50 | var res = try http_server.accept(.{ | |
| 51 | .allocator = gpa, | |
| 52 | .client_header_buffer = &header_buffer, | |
| 53 | }); | |
| 54 | defer res.deinit(); | |
| 55 | ||
| 56 | res.wait() catch |err| switch (err) { | |
| 57 | error.HttpHeadersInvalid => continue :accept, | |
| 58 | error.EndOfStream => continue, | |
| 59 | else => return err, | |
| 60 | }; | |
| 61 | try serve(&res); | |
| 62 | ||
| 63 | try std.testing.expectEqual(.reset, res.reset()); | |
| 64 | } | |
| 65 | } | |
| 66 | ||
| 67 | fn serve(res: *std.http.Server.Response) !void { | |
| 68 | try std.testing.expectEqualStrings(res.request.target, "/trailer"); | |
| 69 | res.transfer_encoding = .chunked; | |
| 70 | ||
| 71 | try res.send(); | |
| 72 | try res.writeAll("Hello, "); | |
| 73 | try res.writeAll("World!\n"); | |
| 74 | // try res.finish(); | |
| 75 | try res.connection.writeAll("0\r\nX-Checksum: aaaa\r\n\r\n"); | |
| 76 | } |
test/standalone/http.zig-34| ... | ... | @@ -98,14 +98,6 @@ fn handleRequest(res: *Server.Response) !void { |
| 98 | 98 | try res.writeAll("Hello, "); |
| 99 | 99 | try res.writeAll("World!\n"); |
| 100 | 100 | try res.finish(); |
| 101 | } else if (mem.eql(u8, res.request.target, "/trailer")) { | |
| 102 | res.transfer_encoding = .chunked; | |
| 103 | ||
| 104 | try res.send(); | |
| 105 | try res.writeAll("Hello, "); | |
| 106 | try res.writeAll("World!\n"); | |
| 107 | // try res.finish(); | |
| 108 | try res.connection.writeAll("0\r\nX-Checksum: aaaa\r\n\r\n"); | |
| 109 | 101 | } else if (mem.eql(u8, res.request.target, "/redirect/1")) { |
| 110 | 102 | res.transfer_encoding = .chunked; |
| 111 | 103 | |
| ... | ... | @@ -378,32 +370,6 @@ pub fn main() !void { |
| 378 | 370 | // connection has been kept alive |
| 379 | 371 | try testing.expect(client.http_proxy != null or client.connection_pool.free_len == 1); |
| 380 | 372 | |
| 381 | { // check trailing headers | |
| 382 | const location = try std.fmt.allocPrint(calloc, "http://127.0.0.1:{d}/trailer", .{port}); | |
| 383 | defer calloc.free(location); | |
| 384 | const uri = try std.Uri.parse(location); | |
| 385 | ||
| 386 | log.info("{s}", .{location}); | |
| 387 | var server_header_buffer: [1024]u8 = undefined; | |
| 388 | var req = try client.open(.GET, uri, .{ | |
| 389 | .server_header_buffer = &server_header_buffer, | |
| 390 | }); | |
| 391 | defer req.deinit(); | |
| 392 | ||
| 393 | try req.send(.{}); | |
| 394 | try req.wait(); | |
| 395 | ||
| 396 | const body = try req.reader().readAllAlloc(calloc, 8192); | |
| 397 | defer calloc.free(body); | |
| 398 | ||
| 399 | try testing.expectEqualStrings("Hello, World!\n", body); | |
| 400 | @panic("TODO implement inspecting custom headers in responses"); | |
| 401 | //try testing.expectEqualStrings("aaaa", req.response.headers.getFirstValue("x-checksum").?); | |
| 402 | } | |
| 403 | ||
| 404 | // connection has been kept alive | |
| 405 | try testing.expect(client.http_proxy != null or client.connection_pool.free_len == 1); | |
| 406 | ||
| 407 | 373 | { // send content-length request |
| 408 | 374 | const location = try std.fmt.allocPrint(calloc, "http://127.0.0.1:{d}/echo-content", .{port}); |
| 409 | 375 | defer calloc.free(location); |