Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add public interface to Multipart's Part headers #1687

Merged
merged 6 commits into from
Jan 3, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/async_impl/multipart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,14 @@ impl Part {
self.with_inner(move |inner| inner.file_name(filename))
}

/// Sets custom headers for the part.
pub fn headers<T>(self, headers: T) -> Part
where
T: Into<HeaderMap>,
beeb marked this conversation as resolved.
Show resolved Hide resolved
{
self.with_inner(move |inner| inner.headers(headers))
}

fn with_inner<F>(self, func: F) -> Self
where
F: FnOnce(PartMetadata) -> PartMetadata,
Expand Down Expand Up @@ -394,6 +402,14 @@ impl PartMetadata {
self.file_name = Some(filename.into());
self
}

pub(crate) fn headers<T>(mut self, headers: T) -> Self
where
T: Into<HeaderMap>,
{
self.headers = headers.into();
self
}
}

impl PartMetadata {
Expand Down Expand Up @@ -591,7 +607,9 @@ mod tests {
#[test]
fn stream_to_end_with_header() {
let mut part = Part::text("value2").mime(mime::IMAGE_BMP);
part.meta.headers.insert("Hdr3", "/a/b/c".parse().unwrap());
let mut headers = HeaderMap::new();
headers.insert("Hdr3", "/a/b/c".parse().unwrap());
part = part.headers(headers);
let mut form = Form::new().part("key2", part);
form.inner.boundary = "boundary".to_string();
let expected = "--boundary\r\n\
Expand Down
13 changes: 12 additions & 1 deletion src/blocking/multipart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ use std::fs::File;
use std::io::{self, Cursor, Read};
use std::path::Path;

use http::HeaderMap;
use mime_guess::{self, Mime};

use super::Body;
Expand Down Expand Up @@ -256,6 +257,14 @@ impl Part {
self.with_inner(move |inner| inner.file_name(filename))
}

/// Sets custom headers for the part.
pub fn headers<T>(self, headers: T) -> Part
where
T: Into<HeaderMap>,
{
self.with_inner(move |inner| inner.headers(headers))
}

fn with_inner<F>(self, func: F) -> Self
where
F: FnOnce(PartMetadata) -> PartMetadata,
Expand Down Expand Up @@ -453,7 +462,9 @@ mod tests {
fn read_to_end_with_header() {
let mut output = Vec::new();
let mut part = Part::text("value2").mime(mime::IMAGE_BMP);
part.meta.headers.insert("Hdr3", "/a/b/c".parse().unwrap());
let mut headers = HeaderMap::new();
headers.insert("Hdr3", "/a/b/c".parse().unwrap());
part = part.headers(headers);
let mut form = Form::new().part("key2", part);
form.inner.boundary = "boundary".to_string();
let expected = "--boundary\r\n\
Expand Down