Add public interface to Multipart's Part headers (#1687)
* 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
This commit is contained in:
@@ -4,7 +4,6 @@ use std::fmt;
|
|||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
|
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use http::HeaderMap;
|
|
||||||
use mime_guess::Mime;
|
use mime_guess::Mime;
|
||||||
use percent_encoding::{self, AsciiSet, NON_ALPHANUMERIC};
|
use percent_encoding::{self, AsciiSet, NON_ALPHANUMERIC};
|
||||||
|
|
||||||
@@ -12,6 +11,7 @@ use futures_core::Stream;
|
|||||||
use futures_util::{future, stream, StreamExt};
|
use futures_util::{future, stream, StreamExt};
|
||||||
|
|
||||||
use super::Body;
|
use super::Body;
|
||||||
|
use crate::header::HeaderMap;
|
||||||
|
|
||||||
/// An async multipart/form-data request.
|
/// An async multipart/form-data request.
|
||||||
pub struct Form {
|
pub struct Form {
|
||||||
@@ -244,6 +244,11 @@ impl Part {
|
|||||||
self.with_inner(move |inner| inner.file_name(filename))
|
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
|
fn with_inner<F>(self, func: F) -> Self
|
||||||
where
|
where
|
||||||
F: FnOnce(PartMetadata) -> PartMetadata,
|
F: FnOnce(PartMetadata) -> PartMetadata,
|
||||||
@@ -394,6 +399,14 @@ impl PartMetadata {
|
|||||||
self.file_name = Some(filename.into());
|
self.file_name = Some(filename.into());
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn headers<T>(mut self, headers: T) -> Self
|
||||||
|
where
|
||||||
|
T: Into<HeaderMap>,
|
||||||
|
{
|
||||||
|
self.headers = headers.into();
|
||||||
|
self
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PartMetadata {
|
impl PartMetadata {
|
||||||
@@ -591,7 +604,9 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn stream_to_end_with_header() {
|
fn stream_to_end_with_header() {
|
||||||
let mut part = Part::text("value2").mime(mime::IMAGE_BMP);
|
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);
|
let mut form = Form::new().part("key2", part);
|
||||||
form.inner.boundary = "boundary".to_string();
|
form.inner.boundary = "boundary".to_string();
|
||||||
let expected = "--boundary\r\n\
|
let expected = "--boundary\r\n\
|
||||||
|
|||||||
@@ -46,6 +46,7 @@ use mime_guess::{self, Mime};
|
|||||||
|
|
||||||
use super::Body;
|
use super::Body;
|
||||||
use crate::async_impl::multipart::{FormParts, PartMetadata, PartProps};
|
use crate::async_impl::multipart::{FormParts, PartMetadata, PartProps};
|
||||||
|
use crate::header::HeaderMap;
|
||||||
|
|
||||||
/// A multipart/form-data request.
|
/// A multipart/form-data request.
|
||||||
pub struct Form {
|
pub struct Form {
|
||||||
@@ -256,6 +257,11 @@ impl Part {
|
|||||||
self.with_inner(move |inner| inner.file_name(filename))
|
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
|
fn with_inner<F>(self, func: F) -> Self
|
||||||
where
|
where
|
||||||
F: FnOnce(PartMetadata) -> PartMetadata,
|
F: FnOnce(PartMetadata) -> PartMetadata,
|
||||||
@@ -453,7 +459,9 @@ mod tests {
|
|||||||
fn read_to_end_with_header() {
|
fn read_to_end_with_header() {
|
||||||
let mut output = Vec::new();
|
let mut output = Vec::new();
|
||||||
let mut part = Part::text("value2").mime(mime::IMAGE_BMP);
|
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);
|
let mut form = Form::new().part("key2", part);
|
||||||
form.inner.boundary = "boundary".to_string();
|
form.inner.boundary = "boundary".to_string();
|
||||||
let expected = "--boundary\r\n\
|
let expected = "--boundary\r\n\
|
||||||
|
|||||||
Reference in New Issue
Block a user