1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
// Copyright 2017 rust-multipart-rfc7578 Developers
//
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.
//
use crate::{
boundary::{BoundaryGenerator, RandomAsciiGenerator},
error::Error,
};
use bytes::{BufMut, BytesMut};
use futures_core::Stream;
use futures_util::io::{AllowStdIo, AsyncRead, Cursor};
use http::{
self,
header::{self, HeaderName},
request::{Builder, Request},
};
use mime::{self, Mime};
use std::{
fmt::Display,
fs::File,
io::{self, Read},
iter::Peekable,
path::Path,
pin::Pin,
task::{Context, Poll},
vec::IntoIter,
};
static CONTENT_DISPOSITION: HeaderName = header::CONTENT_DISPOSITION;
static CONTENT_TYPE: HeaderName = header::CONTENT_TYPE;
/// Async streamable Multipart body.
///
pub struct Body<'a> {
/// The amount of data to write with each chunk.
///
buf: BytesMut,
/// The active reader.
///
current: Option<Box<dyn 'a + AsyncRead + Send + Sync + Unpin>>,
/// The parts as an iterator. When the iterator stops
/// yielding, the body is fully written.
///
parts: Peekable<IntoIter<Part<'a>>>,
/// The multipart boundary.
///
boundary: String,
}
impl<'a> Body<'a> {
/// Writes a CLRF.
///
fn write_crlf(&mut self) {
self.buf.put_slice(&[b'\r', b'\n']);
}
/// Implements section 4.1.
///
/// [See](https://tools.ietf.org/html/rfc7578#section-4.1).
///
fn write_boundary(&mut self) {
self.buf.put_slice(&[b'-', b'-']);
self.buf.put_slice(self.boundary.as_bytes());
}
/// Writes the last form boundary.
///
/// [See](https://tools.ietf.org/html/rfc2046#section-5.1).
///
fn write_final_boundary(&mut self) {
self.write_boundary();
self.buf.put_slice(&[b'-', b'-']);
}
/// Writes the Content-Disposition, and Content-Type headers.
///
fn write_headers(&mut self, part: &Part) {
self.write_crlf();
self.buf.put_slice(CONTENT_TYPE.as_ref());
self.buf.put_slice(b": ");
self.buf.put_slice(part.content_type.as_bytes());
self.write_crlf();
self.buf.put_slice(CONTENT_DISPOSITION.as_ref());
self.buf.put_slice(b": ");
self.buf.put_slice(part.content_disposition.as_bytes());
self.write_crlf();
self.write_crlf();
}
}
impl<'a> Stream for Body<'a> {
type Item = Result<BytesMut, Error>;
/// Iterate over each form part, and write it out.
///
fn poll_next(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
let body = self.get_mut();
match body.current {
None => {
if let Some(part) = body.parts.next() {
body.write_boundary();
body.write_headers(&part);
let read: Box<dyn AsyncRead + Send + Sync + Unpin> = match part.inner {
Inner::Read(read, _) => Box::new(AllowStdIo::new(read)),
Inner::AsyncRead(read) => read,
Inner::Text(s) => Box::new(Cursor::new(s)),
};
body.current = Some(read);
cx.waker().wake_by_ref();
Poll::Ready(Some(Ok(body.buf.split())))
} else {
// No current part, and no parts left means there is nothing
// left to write.
//
Poll::Ready(None)
}
}
Some(ref mut read) => {
// Reserve some space to read the next part
body.buf.reserve(256);
let len_before = body.buf.len();
// Init the remaining capacity to 0, and get a mut slice to it
body.buf.resize(body.buf.capacity(), 0);
let slice = &mut body.buf.as_mut()[len_before..];
match Pin::new(read).poll_read(cx, slice) {
Poll::Pending => {
body.buf.truncate(len_before);
Poll::Pending
}
// Read some data.
Poll::Ready(Ok(bytes_read)) => {
body.buf.truncate(len_before + bytes_read);
if bytes_read == 0 {
// EOF: No data left to read. Get ready to move onto write the next part.
body.current = None;
body.write_crlf();
if body.parts.peek().is_none() {
// If there is no next part, write the final boundary
body.write_final_boundary();
body.write_crlf();
}
}
Poll::Ready(Some(Ok(body.buf.split())))
}
// Error reading from underlying stream.
Poll::Ready(Err(e)) => {
body.buf.truncate(len_before);
Poll::Ready(Some(Err(Error::ContentRead(e))))
}
}
}
}
}
}
/// Implements the multipart/form-data media type as described by
/// RFC 7578.
///
/// [See](https://tools.ietf.org/html/rfc7578#section-1).
///
pub struct Form<'a> {
parts: Vec<Part<'a>>,
/// The auto-generated boundary as described by 4.1.
///
/// [See](https://tools.ietf.org/html/rfc7578#section-4.1).
///
boundary: String,
}
impl<'a> Default for Form<'a> {
/// Creates a new form with the default boundary generator.
///
#[inline]
fn default() -> Form<'a> {
Form::new::<RandomAsciiGenerator>()
}
}
impl<'a> Form<'a> {
/// Creates a new form with the specified boundary generator function.
///
/// # Examples
///
/// ```
/// # use common_multipart_rfc7578::client::multipart::{
/// # self,
/// # BoundaryGenerator
/// # };
/// #
/// struct TestGenerator;
///
/// impl BoundaryGenerator for TestGenerator {
/// fn generate_boundary() -> String {
/// "test".to_string()
/// }
/// }
///
/// let form = multipart::Form::new::<TestGenerator>();
/// ```
///
#[inline]
pub fn new<G>() -> Form<'a>
where
G: BoundaryGenerator,
{
Form {
parts: vec![],
boundary: G::generate_boundary(),
}
}
/// Adds a text part to the Form.
///
/// # Examples
///
/// ```
/// use common_multipart_rfc7578::client::multipart;
///
/// let mut form = multipart::Form::default();
///
/// form.add_text("text", "Hello World!");
/// form.add_text("more", String::from("Hello Universe!"));
/// ```
///
pub fn add_text<N, T>(&mut self, name: N, text: T)
where
N: Display,
T: Into<String>,
{
self.parts.push(Part::new::<_, String>(
Inner::Text(text.into()),
name,
None,
None,
))
}
/// Adds a readable part to the Form.
///
/// # Examples
///
/// ```
/// use common_multipart_rfc7578::client::multipart;
/// use std::io::Cursor;
///
/// let bytes = Cursor::new("Hello World!");
/// let mut form = multipart::Form::default();
///
/// form.add_reader("input", bytes);
/// ```
///
pub fn add_reader<F, R>(&mut self, name: F, read: R)
where
F: Display,
R: 'a + Read + Send + Sync + Unpin,
{
let read = Box::new(read);
self.parts.push(Part::new::<_, String>(
Inner::Read(read, None),
name,
None,
None,
));
}
/// Adds a readable part to the Form.
///
/// # Examples
///
/// ```
/// use common_multipart_rfc7578::client::multipart;
/// use futures_util::io::Cursor;
///
/// let bytes = Cursor::new("Hello World!");
/// let mut form = multipart::Form::default();
///
/// form.add_async_reader("input", bytes);
/// ```
///
pub fn add_async_reader<F, R>(&mut self, name: F, read: R)
where
F: Display,
R: 'a + AsyncRead + Send + Sync + Unpin,
{
let read = Box::new(read);
self.parts.push(Part::new::<_, String>(
Inner::AsyncRead(read),
name,
None,
None,
));
}
/// Adds a file, and attempts to derive the mime type.
///
/// # Examples
///
/// ```
/// use common_multipart_rfc7578::client::multipart;
///
/// let mut form = multipart::Form::default();
///
/// form.add_file("file", file!()).expect("file to exist");
/// ```
///
pub fn add_file<P, F>(&mut self, name: F, path: P) -> io::Result<()>
where
P: AsRef<Path>,
F: Display,
{
self._add_file(name, path, None)
}
/// Adds a file with the specified mime type to the form.
/// If the mime type isn't specified, a mime type will try to
/// be derived.
///
/// # Examples
///
/// ```
/// use common_multipart_rfc7578::client::multipart;
///
/// let mut form = multipart::Form::default();
///
/// form.add_file_with_mime("data", "test.csv", mime::TEXT_CSV);
/// ```
///
pub fn add_file_with_mime<P, F>(&mut self, name: F, path: P, mime: Mime) -> io::Result<()>
where
P: AsRef<Path>,
F: Display,
{
self._add_file(name, path, Some(mime))
}
/// Internal method for adding a file part to the form.
///
fn _add_file<P, F>(&mut self, name: F, path: P, mime: Option<Mime>) -> io::Result<()>
where
P: AsRef<Path>,
F: Display,
{
let f = File::open(&path)?;
let mime = mime.or_else(|| mime_guess::from_path(&path).first());
let len = match f.metadata() {
// If the path is not a file, it can't be uploaded because there
// is no content.
//
Ok(ref meta) if !meta.is_file() => Err(io::Error::new(
io::ErrorKind::InvalidInput,
"expected a file not directory",
)),
// If there is some metadata on the file, try to derive some
// header values.
//
Ok(ref meta) => Ok(Some(meta.len())),
// The file metadata could not be accessed. This MIGHT not be an
// error, if the file could be opened.
//
Err(e) => Err(e),
}?;
let read = Box::new(f);
self.parts.push(Part::new(
Inner::Read(read, len),
name,
mime,
Some(path.as_ref().as_os_str().to_string_lossy()),
));
Ok(())
}
/// Adds a readable part to the Form as a file.
///
/// # Examples
///
/// ```
/// use common_multipart_rfc7578::client::multipart;
/// use std::io::Cursor;
///
/// let bytes = Cursor::new("Hello World!");
/// let mut form = multipart::Form::default();
///
/// form.add_reader_file("input", bytes, "filename.txt");
/// ```
///
pub fn add_reader_file<F, G, R>(&mut self, name: F, read: R, filename: G)
where
F: Display,
G: Into<String>,
R: 'a + Read + Send + Sync + Unpin,
{
let read = Box::new(read);
self.parts.push(Part::new::<_, String>(
Inner::Read(read, None),
name,
None,
Some(filename.into()),
));
}
/// Adds a readable part to the Form as a file.
///
/// # Examples
///
/// ```
/// use common_multipart_rfc7578::client::multipart;
/// use futures_util::io::Cursor;
///
/// let bytes = Cursor::new("Hello World!");
/// let mut form = multipart::Form::default();
///
/// form.add_async_reader_file("input", bytes, "filename.txt");
/// ```
///
pub fn add_async_reader_file<F, G, R>(&mut self, name: F, read: R, filename: G)
where
F: Display,
G: Into<String>,
R: 'a + AsyncRead + Send + Sync + Unpin,
{
let read = Box::new(read);
self.parts.push(Part::new::<_, String>(
Inner::AsyncRead(read),
name,
None,
Some(filename.into()),
));
}
pub fn add_reader_2<F, R>(&mut self, name: F, read: R, filename: Option<String>, mime: Option<Mime>)
where
F: Display,
R: 'a + Read + Send + Sync + Unpin,
{
let read = Box::new(read);
self.parts.push(Part::new::<_, String>(
Inner::Read(read, None),
name,
mime,
filename,
));
}
/// Adds a readable part to the Form as a file with a specified mime.
///
/// # Examples
///
/// ```
/// use common_multipart_rfc7578::client::multipart;
/// use std::io::Cursor;
///
/// let bytes = Cursor::new("Hello World!");
/// let mut form = multipart::Form::default();
///
/// form.add_reader_file_with_mime("input", bytes, "filename.txt", mime::TEXT_PLAIN);
/// ```
///
pub fn add_reader_file_with_mime<F, G, R>(&mut self, name: F, read: R, filename: G, mime: Mime)
where
F: Display,
G: Into<String>,
R: 'a + Read + Send + Sync + Unpin,
{
let read = Box::new(read);
self.parts.push(Part::new::<_, String>(
Inner::Read(read, None),
name,
Some(mime),
Some(filename.into()),
));
}
/// Adds a readable part to the Form as a file with a specified mime.
///
/// # Examples
///
/// ```
/// use common_multipart_rfc7578::client::multipart;
/// use futures_util::io::Cursor;
///
/// let bytes = Cursor::new("Hello World!");
/// let mut form = multipart::Form::default();
///
/// form.add_async_reader_file_with_mime("input", bytes, "filename.txt", mime::TEXT_PLAIN);
/// ```
///
pub fn add_async_reader_file_with_mime<F, G, R>(
&mut self,
name: F,
read: R,
filename: G,
mime: Mime,
) where
F: Display,
G: Into<String>,
R: 'a + AsyncRead + Send + Sync + Unpin,
{
let read = Box::new(read);
self.parts.push(Part::new::<_, String>(
Inner::AsyncRead(read),
name,
Some(mime),
Some(filename.into()),
));
}
/// Updates a request instance with the multipart Content-Type header
/// and the payload data.
///
/// # Examples
///
/// ```
/// use hyper::{Method, Request};
/// use hyper_multipart_rfc7578::client::multipart;
///
/// let mut req_builder = Request::post("http://localhost:80/upload");
/// let mut form = multipart::Form::default();
///
/// form.add_text("text", "Hello World!");
/// let req = form.set_body::<multipart::Body>(req_builder).unwrap();
/// ```
///
pub fn set_body<B>(self, req: Builder) -> Result<Request<B>, http::Error>
where
B: From<Body<'a>>,
{
self.set_body_convert::<B, B>(req)
}
/// Updates a request instance with the multipart Content-Type header
/// and the payload data.
///
/// Allows converting body into an intermediate type.
///
/// # Examples
///
/// ```
/// use hyper::{Body, Method, Request};
/// use hyper_multipart_rfc7578::client::multipart;
///
/// let mut req_builder = Request::post("http://localhost:80/upload");
/// let mut form = multipart::Form::default();
///
/// form.add_text("text", "Hello World!");
/// let req = form.set_body_convert::<hyper::Body, multipart::Body>(req_builder).unwrap();
/// ```
///
pub fn set_body_convert<B, I>(self, req: Builder) -> Result<Request<B>, http::Error>
where
I: From<Body<'a>> + Into<B>,
{
req.header(&CONTENT_TYPE, self.content_type().as_str())
.body(I::from(Body::from(self)).into())
}
pub fn content_type(&self) -> String {
format!("multipart/form-data; boundary={}", &self.boundary)
}
}
impl<'a> From<Form<'a>> for Body<'a> {
/// Turns a `Form` into a multipart `Body`.
///
fn from(form: Form<'a>) -> Self {
Body {
buf: BytesMut::with_capacity(2048),
current: None,
parts: form.parts.into_iter().peekable(),
boundary: form.boundary,
}
}
}
/// One part of a body delimited by a boundary line.
///
/// [See RFC2046 5.1](https://tools.ietf.org/html/rfc2046#section-5.1).
///
pub struct Part<'a> {
inner: Inner<'a>,
/// Each part can include a Content-Type header field. If this
/// is not specified, it defaults to "text/plain", or
/// "application/octet-stream" for file data.
///
/// [See](https://tools.ietf.org/html/rfc7578#section-4.4)
///
content_type: String,
/// Each part must contain a Content-Disposition header field.
///
/// [See](https://tools.ietf.org/html/rfc7578#section-4.2).
///
content_disposition: String,
}
impl<'a> Part<'a> {
/// Internal method to build a new Part instance. Sets the disposition type,
/// content-type, and the disposition parameters for name, and optionally
/// for filename.
///
/// Per [4.3](https://tools.ietf.org/html/rfc7578#section-4.3), if multiple
/// files need to be specified for one form field, they can all be specified
/// with the same name parameter.
///
fn new<N, F>(inner: Inner<'a>, name: N, mime: Option<Mime>, filename: Option<F>) -> Part<'a>
where
N: Display,
F: Display,
{
// `name` disposition parameter is required. It should correspond to the
// name of a form field.
//
// [See 4.2](https://tools.ietf.org/html/rfc7578#section-4.2)
//
let mut disposition_params = vec![format!("name=\"{}\"", name)];
// `filename` can be supplied for files, but is totally optional.
//
// [See 4.2](https://tools.ietf.org/html/rfc7578#section-4.2)
//
if let Some(filename) = filename {
disposition_params.push(format!("filename=\"{}\"", filename));
}
let content_type = format!("{}", mime.unwrap_or_else(|| inner.default_content_type()));
Part {
inner,
content_type,
content_disposition: format!("form-data; {}", disposition_params.join("; ")),
}
}
}
enum Inner<'a> {
/// The `Read` and `AsyncRead` variants captures multiple cases.
///
/// * The first is it supports uploading a file, which is explicitly
/// described in RFC 7578.
///
/// * The second (which is not described by RFC 7578), is it can handle
/// arbitrary input streams (for example, a server response).
/// Any arbitrary input stream is automatically considered a file,
/// and assigned the corresponding content type if not explicitly
/// specified.
///
Read(Box<dyn 'a + Read + Send + Sync + Unpin>, Option<u64>),
AsyncRead(Box<dyn 'a + AsyncRead + Send + Sync + Unpin>),
/// The `String` variant handles "text/plain" form data payloads.
///
Text(String),
}
impl<'a> Inner<'a> {
/// Returns the default Content-Type header value as described in section 4.4.
///
/// [See](https://tools.ietf.org/html/rfc7578#section-4.4)
///
fn default_content_type(&self) -> Mime {
match *self {
Inner::Read(_, _) | Inner::AsyncRead(_) => mime::APPLICATION_OCTET_STREAM,
Inner::Text(_) => mime::TEXT_PLAIN,
}
}
}
#[cfg(test)]
mod tests {
use super::{Body, Form};
use crate::error::Error;
use bytes::BytesMut;
use futures_util::TryStreamExt;
use std::{
io::Cursor,
path::{Path, PathBuf},
};
async fn form_output(form: Form<'_>) -> String {
let result: Result<BytesMut, Error> = Body::from(form).try_concat().await;
assert!(result.is_ok());
let bytes = result.unwrap();
let data = std::str::from_utf8(bytes.as_ref()).unwrap();
data.into()
}
fn test_file_path() -> PathBuf {
// common/src/data/test.txt
Path::new(env!("CARGO_MANIFEST_DIR"))
.join("src")
.join("data")
.join("test.txt")
}
#[tokio::test]
async fn add_text_returns_expected_result() {
let mut form = Form::default();
form.add_text("test", "Hello World!");
let data = form_output(form).await;
assert!(data.contains("Hello World!"));
}
#[tokio::test]
async fn add_reader_returns_expected_result() {
let bytes = Cursor::new("Hello World!");
let mut form = Form::default();
form.add_reader("input", bytes);
let data = form_output(form).await;
assert!(data.contains("Hello World!"));
}
#[tokio::test]
async fn add_file_returns_expected_result() {
let mut form = Form::default();
assert!(form.add_file("test_file.txt", test_file_path()).is_ok());
let data = form_output(form).await;
assert!(data.contains("This is a test file!"));
assert!(data.contains("text/plain"));
}
#[tokio::test]
async fn add_file_with_mime_returns_expected_result() {
let mut form = Form::default();
assert!(form
.add_file_with_mime("test_file.txt", test_file_path(), mime::TEXT_CSV)
.is_ok());
let data = form_output(form).await;
assert!(data.contains("This is a test file!"));
assert!(data.contains("text/csv"));
}
struct FixedBoundary;
impl crate::boundary::BoundaryGenerator for FixedBoundary {
fn generate_boundary() -> String {
"boundary".to_owned()
}
}
#[tokio::test]
async fn test_form_body_stream() {
let mut form = Form::new::<FixedBoundary>();
// Text fields
form.add_text("name1", "value1");
form.add_text("name2", "value2");
// Reader field
form.add_reader("input", Cursor::new("Hello World!"));
let result: BytesMut = Body::from(form).try_concat().await.unwrap();
assert_eq!(
result.as_ref(),
[
b"--boundary\r\n".as_ref(),
b"content-type: text/plain\r\n".as_ref(),
b"content-disposition: form-data; name=\"name1\"\r\n".as_ref(),
b"\r\n".as_ref(),
b"value1\r\n".as_ref(),
b"--boundary\r\n".as_ref(),
b"content-type: text/plain\r\n".as_ref(),
b"content-disposition: form-data; name=\"name2\"\r\n".as_ref(),
b"\r\n".as_ref(),
b"value2\r\n".as_ref(),
b"--boundary\r\n".as_ref(),
b"content-type: application/octet-stream\r\n".as_ref(),
b"content-disposition: form-data; name=\"input\"\r\n".as_ref(),
b"\r\n".as_ref(),
b"Hello World!\r\n".as_ref(),
b"--boundary--\r\n".as_ref(),
]
.into_iter()
.flatten()
.copied()
.collect::<Vec<u8>>()
);
}
#[tokio::test]
async fn test_content_type_header_format() {
use http::Request;
let mut form = Form::new::<FixedBoundary>();
// Text fields
form.add_text("name1", "value1");
form.add_text("name2", "value2");
let builder = Request::builder();
let body = form.set_body::<Body>(builder).unwrap();
assert_eq!(
body.headers().get("Content-Type").unwrap().as_bytes(),
b"multipart/form-data; boundary=boundary",
)
}
}