From 24ccd60afca12a156dee42d5d9a28c4bcf2a9ab1 Mon Sep 17 00:00:00 2001 From: Luna Date: Mon, 12 Apr 2021 23:57:59 -0300 Subject: [PATCH] add test for multipart parser --- src/main.zig | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/main.zig b/src/main.zig index 1973d8c..bf19559 100644 --- a/src/main.zig +++ b/src/main.zig @@ -314,3 +314,43 @@ fn fetchFile(response: *http.Response, request: http.Request, filename: []const try response.writer().writeAll(&file_write_buffer); } } + +pub const log_level: std.log.Level = .debug; +test "multipart" { + const body = + "--1234\r\n" ++ + "Content-Type: text/plain\r\n" ++ + "Content-Disposition: form-data; name=file1; filename=ab.txt\r\n" ++ + "\r\n" ++ + "Hello!\n" ++ + "--1234\r\n" ++ + "Content-Type: application/json\r\n" ++ + // TODO: add 'content-type' support to content-disposition as well + "Content-Disposition: form-data; name=file2; filename=data.json\r\n" ++ + "\r\n" ++ + "{\"status\": \"OK\"}\n" ++ + "--1234--\r\n"; + + var buf: [512]u8 = undefined; + var multipart = try Multipart.init( + body, + "multipart/form-data; boundary=1234", + &buf, + ); + + var hzzp_buffer: [1024]u8 = undefined; + var part1 = (try multipart.next(&hzzp_buffer, std.testing.allocator)).?; + defer part1.deinit(); + + std.debug.warn( + "\npart={}\n", + .{part1}, + ); + + std.testing.expectEqualSlices(u8, "text/plain", part1.content_type); + std.testing.expectEqualSlices(u8, "file1", part1.disposition.name); + std.testing.expectEqualSlices(u8, "ab.txt", part1.disposition.filename); + std.testing.expectEqualSlices(u8, "Hello!", part1.body); + + // var part2 = (try multipart.next(&hzzp_buffer)).?; +}