I use oneshot()
for testing all the time. Now I want to test an endpoint that accepts multipart form data. I cannot find an idiomatic way to create a test Request with a multipart form body. The best I have come up with is below. It works, but is clunky.
reqwest
has a Form
type, which is great for creating real requests on the client side, but Form
does not impl the http_body::Body
trait, so it can't be used to create the body of an axum::http::Request
(unless I'm missing something). Form
also (understandably) doesn't provide easy access to its contents, so I can't impl http_body::Body
on it myself.
let request = axum::http::Request::builder()
.method("POST")
.uri("/my_endpoint")
.header(
axum::http::header::CONTENT_TYPE,
"multipart/form-data; boundary=boundary",
)
.body(
r#"--boundary
Content-Disposition:form-data;name="file1";filename="file1.jpg"
Content-Type:image/jpeg
<TEST IMAGE FILE CONTENTS>
--boundary
Content-Disposition:form-data;name="file2";filename="file2.jpg"
Content-Type:image/jpeg
<MORE TEST IMAGE FILE CONTENTS>
--boundary--
"#
.replace('\n', "\r\n")
.replace(' ', ""),
)
.unwrap();
let res = my_routes().oneshot(request).await.unwrap();