///|
test "a text part and a file part" {
let request = @multipart.body(
@mttp_core.Request::post("https://example.com/upload"),
[
@multipart.Part::text("title", "notes"),
@multipart.Part::file("file", "a.txt", "text/plain", b"hi"),
],
boundary="XyZ",
)
assert_eq(
request.headers.get("content-type"),
Some("multipart/form-data; boundary=XyZ"),
)
assert_eq(
request.body.as_bytes(),
Some(
b"--XyZ\r\nContent-Disposition: form-data; name=\"title\"\r\n\r\nnotes\r\n--XyZ\r\nContent-Disposition: form-data; name=\"file\"; filename=\"a.txt\"\r\nContent-Type: text/plain\r\n\r\nhi\r\n--XyZ--\r\n",
),
)
}
///|
test "a fresh boundary from injected randomness" {
let boundary = @multipart.make_boundary(() => 0.5)
assert_eq(boundary, "mttp-" + "i".repeat(32))
}pub(all) suberror MultipartError {
InvalidBoundary(String)
BoundaryCollision
InvalidHeaderValue(String)
StreamPart(String)
} derive(Debug)test {
let part = @multipart.Part::file(
"upload", "data.bin", "application/octet-stream", b"\x00\xFF",
)
assert_eq(part.filename, Some("data.bin"))
assert_eq(part.body, Bytes(b"\x00\xFF"))
}fn Part::file_stream(name : String, filename : String, content_type : String, stream : &BodyStream, length? : Int64) -> Parttest {
let part = @multipart.Part::json("metadata", { "ok": true })
assert_eq(part.content_type, Some("application/json"))
assert_eq(part.body, Bytes(b"{\"ok\":true}"))
}test {
assert_eq(@multipart.Part::text("message", "hello").body, Bytes(b"hello"))
}test {
let request = @mttp_core.Request::post("/upload").header("x-id", "42")
let result = @multipart.body(
request,
[@multipart.Part::text("message", "hello")],
boundary="example",
)
assert_eq(
result.headers.get("content-type"),
Some("multipart/form-data; boundary=example"),
)
assert_eq(request.headers.get("content-type"), None)
}test {
let (content_type, body) = @multipart.encode(
[@multipart.Part::text("message", "hello")],
"example",
)
assert_eq(content_type, "multipart/form-data; boundary=example")
assert_true(body.has_suffix(b"--example--\r\n"[:]))
}test {
let (_, body) = @multipart.encode_body(
[@multipart.Part::text("message", "hello")],
"B",
)
assert_eq(body.length(), Some(69L))
}fn encode_segments(parts : Array[Part], boundary : String) -> (String, Array[Bytes]) raise MultipartErrortest {
let file = b"\x00\x01"
let (_, segments) = @multipart.encode_segments(
[@multipart.Part::file("upload", "a.bin", "application/octet-stream", file)],
"example",
)
assert_eq(segments.length(), 3)
assert_true(physical_equal(segments[1], file))
}fn encode_segments_fresh(parts : Array[Part], random~ : () -> Double, attempts? : Int) -> (String, Array[Bytes]) raise MultipartErrorfn make_boundary(random : () -> Double) -> Stringtest {
assert_eq(
@multipart.make_boundary(() => 0.0),
"mttp-00000000000000000000000000000000",
)
}Install
Download zipHTTP for MoonBit without I/O, for libraries such as SDKs: the types, an HTTP/1.1 codec and engines, a client with retries under a budget, deadlines, rate limits, redirects and cookies, SSE, multipart, and typed endpoints with a router and OpenAPI. Apps add gaato/mttp.