add test for multipart parser

This commit is contained in:
Luna 2021-04-12 23:57:59 -03:00
parent a7f9d581b9
commit 24ccd60afc
1 changed files with 40 additions and 0 deletions

View File

@ -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)).?;
}