Fixed part headers formatting and added Content-Length

This commit is contained in:
Bruce Weirdan 2024-08-04 14:37:51 +02:00
parent fc964074d3
commit efd784353e
No known key found for this signature in database
GPG Key ID: CFC3AAB181751B0D
2 changed files with 17 additions and 12 deletions

View File

@ -281,17 +281,21 @@ pub fn send_request(
for (col, val) in val.into_owned() {
if let Value::Binary { val, .. } = val {
let headers = format!(
r#"Content-Type: application/octet-stream
Content-Disposition: form-data; name="{}"; filename="{}"
Content-Transfer-Encoding: binary
"#,
col, col,
);
builder.add(&mut Cursor::new(val), &headers).map_err(err)?;
let headers = [
"Content-Type: application/octet-stream".to_string(),
"Content-Transfer-Encoding: binary".to_string(),
format!(
"Content-Disposition: form-data; name=\"{}\"; filename=\"{}\"",
col, col
),
format!("Content-Length: {}", val.len()),
];
builder
.add(&mut Cursor::new(val), &headers.join("\n"))
.map_err(err)?;
} else {
let headers =
format!(r#"Content-Disposition: form-data; name="{}""#, col,);
format!(r#"Content-Disposition: form-data; name="{}""#, col);
builder
.add(val.coerce_into_string()?.as_bytes(), &headers)
.map_err(err)?;

View File

@ -210,9 +210,10 @@ fn http_post_multipart_is_success() {
Matcher::Regex("multipart/form-data; boundary=.*".to_string()),
)
.match_body(Matcher::AllOf(vec![
Matcher::Regex(r#"Content-Disposition: form-data; name="foo""#.to_string()),
Matcher::Regex(r#"Content-Type: application/octet-stream"#.to_string()),
Matcher::Regex(r#"bar"#.to_string()),
Matcher::Regex(r#"(?m)^Content-Disposition: form-data; name="foo""#.to_string()),
Matcher::Regex(r#"(?m)^Content-Type: application/octet-stream"#.to_string()),
Matcher::Regex(r#"(?m)^Content-Length: 3"#.to_string()),
Matcher::Regex(r#"(?m)^bar"#.to_string()),
]))
.with_status(200)
.create();