Skip to content

Commit

Permalink
Add public interface to Multipart's Part headers (#1687)
Browse files Browse the repository at this point in the history
* feat(async_impl): set custom headers on multipart Part

* feat(blocking): set custom headers on multipart

* test(multipart): use new method to add custom headers

* fix: missing import

* refactor(multipart): use concrete type for headers setter arg
  • Loading branch information
beeb authored Jan 3, 2023
1 parent cdbf84f commit b16ea79
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
19 changes: 17 additions & 2 deletions src/async_impl/multipart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ use std::fmt;
use std::pin::Pin;

use bytes::Bytes;
use http::HeaderMap;
use mime_guess::Mime;
use percent_encoding::{self, AsciiSet, NON_ALPHANUMERIC};

use futures_core::Stream;
use futures_util::{future, stream, StreamExt};

use super::Body;
use crate::header::HeaderMap;

/// An async multipart/form-data request.
pub struct Form {
Expand Down Expand Up @@ -244,6 +244,11 @@ impl Part {
self.with_inner(move |inner| inner.file_name(filename))
}

/// Sets custom headers for the part.
pub fn headers(self, headers: HeaderMap) -> Part {
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 +399,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 +604,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
10 changes: 9 additions & 1 deletion src/blocking/multipart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ use mime_guess::{self, Mime};

use super::Body;
use crate::async_impl::multipart::{FormParts, PartMetadata, PartProps};
use crate::header::HeaderMap;

/// A multipart/form-data request.
pub struct Form {
Expand Down Expand Up @@ -256,6 +257,11 @@ impl Part {
self.with_inner(move |inner| inner.file_name(filename))
}

/// Sets custom headers for the part.
pub fn headers(self, headers: HeaderMap) -> Part {
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 +459,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

0 comments on commit b16ea79

Please sign in to comment.