Skip to content

Commit

Permalink
feat(http): add multipart for server
Browse files Browse the repository at this point in the history
  • Loading branch information
StellarisW committed Oct 24, 2024
1 parent 11c3367 commit 865976b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
3 changes: 1 addition & 2 deletions volo-http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ tokio-util = { workspace = true, features = ["io"] }
tracing.workspace = true

# =====optional=====

multer = { workspace = true, optional = true }

# server optional
Expand All @@ -86,9 +85,9 @@ sonic-rs = { workspace = true, optional = true }
async-stream.workspace = true
libc.workspace = true
serde = { workspace = true, features = ["derive"] }
tokio-test.workspace = true
rand.workspace = true
reqwest = { workspace = true, features = ["multipart"] }
tokio-test.workspace = true
url.workspace = true

[features]
Expand Down
24 changes: 15 additions & 9 deletions volo-http/src/server/layer/body_limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub struct BodyLimitService<S> {
limit: usize,
}

impl<S,B> Service<ServerContext, ServerRequest<B>> for BodyLimitService<S>
impl<S, B> Service<ServerContext, ServerRequest<B>> for BodyLimitService<S>
where
S: Service<ServerContext, ServerRequest<B>> + Send + Sync + 'static,
S::Response: IntoResponse,
Expand Down Expand Up @@ -100,13 +100,17 @@ where
#[cfg(test)]
mod tests {
use http::{Method, StatusCode};
use motore::layer::Layer;
use motore::Service;
use motore::{layer::Layer, Service};
use rand::Rng;
use crate::server::layer::BodyLimitLayer;
use crate::server::route::{any, Route};
use crate::server::test_helpers::empty_cx;
use crate::utils::test_helpers::simple_req;

use crate::{
server::{
layer::BodyLimitLayer,
route::{any, Route},
test_helpers::empty_cx,
},
utils::test_helpers::simple_req,
};

#[tokio::test]
async fn test_body_limit() {
Expand All @@ -125,7 +129,9 @@ mod tests {
let min_part_size = 4096;
let mut body: Vec<u8> = vec![0; min_part_size];
rng.fill(&mut body[..]);
let req = simple_req(Method::GET, "/", unsafe { String::from_utf8_unchecked(body) });
let req = simple_req(Method::GET, "/", unsafe {
String::from_utf8_unchecked(body)
});
let res = service.call(&mut cx, req).await.unwrap();
assert_eq!(res.status(), StatusCode::PAYLOAD_TOO_LARGE);

Expand All @@ -134,4 +140,4 @@ mod tests {
let res = service.call(&mut cx, req).await.unwrap();
assert_eq!(res.status(), StatusCode::OK);
}
}
}
8 changes: 2 additions & 6 deletions volo-http/src/server/utils/multipart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
//! Router,
//! };
//!
//! async fn upload(
//! mut multipart: Multipart,
//! ) -> Result<StatusCode, MultipartRejectionError> {
//! async fn upload(mut multipart: Multipart) -> Result<StatusCode, MultipartRejectionError> {
//! while let Some(field) = multipart.next_field().await? {
//! let name = field.name().unwrap();
//! let value = field.bytes().await?;
Expand Down Expand Up @@ -64,9 +62,7 @@ use crate::{
/// server::utils::multipart::{Multipart, MultipartRejectionError},
/// };
///
/// async fn upload(
/// mut multipart: Multipart,
/// ) -> Result<StatusCode, MultipartRejectionError> {
/// async fn upload(mut multipart: Multipart) -> Result<StatusCode, MultipartRejectionError> {
/// while let Some(field) = multipart.next_field().await? {
/// todo!()
/// }
Expand Down

0 comments on commit 865976b

Please sign in to comment.