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

Replace public use of mime crate with &str #642

Merged
merged 1 commit into from
Dec 21, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion axum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ tokio-tungstenite = { optional = true, version = "0.16" }

[dev-dependencies]
futures = "0.3"
reqwest = { version = "0.11", default-features = false, features = ["json", "stream"] }
reqwest = { version = "0.11", default-features = false, features = ["json", "stream", "multipart"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
tokio = { version = "1.6.1", features = ["macros", "rt", "rt-multi-thread", "net"] }
Expand Down
48 changes: 42 additions & 6 deletions axum/src/extract/multipart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ use crate::BoxError;
use async_trait::async_trait;
use futures_util::stream::Stream;
use http::header::{HeaderMap, CONTENT_TYPE};
use mime::Mime;
use std::{
fmt,
pin::Pin,
task::{Context, Poll},
};

/// Extractor that parses `multipart/form-data` requests commonly used with file uploads.
/// Extractor that parses `multipart/form-data` requests (commonly used with file uploads).
///
/// # Example
///
Expand All @@ -42,7 +41,7 @@ use std::{
/// # };
/// ```
///
/// For security reasons its recommended to combine this with
/// For security reasons it's recommended to combine this with
/// [`ContentLengthLimit`](super::ContentLengthLimit) to limit the size of the request payload.
#[derive(Debug)]
pub struct Multipart {
Expand Down Expand Up @@ -120,9 +119,9 @@ impl<'a> Field<'a> {
self.inner.file_name()
}

/// Get the content type of the field.
pub fn content_type(&self) -> Option<&Mime> {
self.inner.content_type()
/// Get the [content type](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type) of the field.
pub fn content_type(&self) -> Option<&str> {
self.inner.content_type().map(|m| m.as_ref())
}

/// Get a map of headers as [`HeaderMap`].
Expand Down Expand Up @@ -191,3 +190,40 @@ define_rejection! {
/// missing or invalid.
pub struct InvalidBoundary;
}

#[cfg(test)]
mod tests {
use super::*;
use crate::{response::IntoResponse, routing::post, test_helpers::*, Router};

#[tokio::test]
async fn content_type_with_encoding() {
const BYTES: &[u8] = "<!doctype html><title>🦀</title>".as_bytes();
const FILE_NAME: &str = "index.html";
const CONTENT_TYPE: &str = "text/html; charset=utf-8";

async fn handle(mut multipart: Multipart) -> impl IntoResponse {
let field = multipart.next_field().await.unwrap().unwrap();

assert_eq!(field.file_name().unwrap(), FILE_NAME);
assert_eq!(field.content_type().unwrap(), CONTENT_TYPE);
assert_eq!(field.bytes().await.unwrap(), BYTES);

assert!(multipart.next_field().await.unwrap().is_none());
}

let app = Router::new().route("/", post(handle));

let client = TestClient::new(app);

let form = reqwest::multipart::Form::new().part(
"file",
reqwest::multipart::Part::bytes(BYTES)
.file_name(FILE_NAME)
.mime_str(CONTENT_TYPE)
.unwrap(),
);

client.post("/").multipart(form).send().await;
}
}
6 changes: 6 additions & 0 deletions axum/src/test_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ impl RequestBuilder {
self.builder = self.builder.json(json);
self
}

pub(crate) fn header<K, V>(mut self, key: K, value: V) -> Self
where
HeaderName: TryFrom<K>,
Expand All @@ -110,6 +111,11 @@ impl RequestBuilder {
self.builder = self.builder.header(key, value);
self
}

pub(crate) fn multipart(mut self, form: reqwest::multipart::Form) -> Self {
self.builder = self.builder.multipart(form);
self
}
}

pub(crate) struct TestResponse {
Expand Down
10 changes: 9 additions & 1 deletion examples/multipart-form/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,16 @@ async fn accept_form(
) {
while let Some(field) = multipart.next_field().await.unwrap() {
let name = field.name().unwrap().to_string();
let file_name = field.file_name().unwrap().to_string();
let content_type = field.content_type().unwrap().to_string();
let data = field.bytes().await.unwrap();

println!("Length of `{}` is {} bytes", name, data.len());
println!(
"Length of `{}` (`{}`: `{}`) is {} bytes",
name,
file_name,
content_type,
data.len()
);
}
}