pub fn create_form_data_payload_and_headers(
name: &str,
filename: Option<String>,
content_type: Option<Mime>,
file: Bytes,
) -> (Bytes, HeaderMap)
Expand description
Constructs a multipart/form-data
payload from bytes and metadata.
Returned header map can be extended or merged with existing headers.
Multipart boundary used is a random alphanumeric string.
ยงExamples
use actix_multipart::test::create_form_data_payload_and_headers;
use actix_web::{test::TestRequest, web::Bytes};
use memchr::memmem::find;
let (body, headers) = create_form_data_payload_and_headers(
"foo",
Some("lorem.txt".to_owned()),
Some(mime::TEXT_PLAIN_UTF_8),
Bytes::from_static(b"Lorem ipsum."),
);
assert!(find(&body, b"foo").is_some());
assert!(find(&body, b"lorem.txt").is_some());
assert!(find(&body, b"text/plain; charset=utf-8").is_some());
assert!(find(&body, b"Lorem ipsum.").is_some());
let req = TestRequest::default();
// merge header map into existing test request and set multipart body
let req = headers
.into_iter()
.fold(req, |req, hdr| req.insert_header(hdr))
.set_payload(body)
.to_http_request();
assert!(
req.headers()
.get("content-type")
.unwrap()
.to_str()
.unwrap()
.starts_with("multipart/form-data; boundary=\"")
);