diff --git a/axum-extra/src/response/file_stream.rs b/axum-extra/src/response/file_stream.rs index a6b4686737..057a10cd8f 100644 --- a/axum-extra/src/response/file_stream.rs +++ b/axum-extra/src/response/file_stream.rs @@ -170,7 +170,128 @@ mod tests { use tower::ServiceExt; #[tokio::test] - async fn response_file_stream() -> Result<(), Box> { + async fn response() -> Result<(), Box> { + let app = Router::new().route( + "/file", + get(|| async { + // Simulating a file stream + let file_content = b"Hello, this is the simulated file content!".to_vec(); + let reader = Cursor::new(file_content); + + // Response file stream + // Content size and file name are not attached by default + let stream = ReaderStream::new(reader); + FileStream::new(stream).into_response() + }), + ); + + // Simulating a GET request + let response = app + .oneshot(Request::builder().uri("/file").body(Body::empty())?) + .await?; + + // Validate Response Status Code + assert_eq!(response.status(), StatusCode::OK); + + // Validate Response Headers + assert_eq!( + response.headers().get("content-type").unwrap(), + "application/octet-stream" + ); + + // Validate Response Body + let body: &[u8] = &response.into_body().collect().await?.to_bytes(); + assert_eq!( + std::str::from_utf8(body)?, + "Hello, this is the simulated file content!" + ); + Ok(()) + } + + #[tokio::test] + async fn response_not_set_filename() -> Result<(), Box> { + let app = Router::new().route( + "/file", + get(|| async { + // Simulating a file stream + let file_content = b"Hello, this is the simulated file content!".to_vec(); + let size = file_content.len() as u64; + let reader = Cursor::new(file_content); + + // Response file stream + let stream = ReaderStream::new(reader); + FileStream::new(stream).content_size(size).into_response() + }), + ); + + // Simulating a GET request + let response = app + .oneshot(Request::builder().uri("/file").body(Body::empty())?) + .await?; + + // Validate Response Status Code + assert_eq!(response.status(), StatusCode::OK); + + // Validate Response Headers + assert_eq!( + response.headers().get("content-type").unwrap(), + "application/octet-stream" + ); + assert_eq!(response.headers().get("content-length").unwrap(), "42"); + + // Validate Response Body + let body: &[u8] = &response.into_body().collect().await?.to_bytes(); + assert_eq!( + std::str::from_utf8(body)?, + "Hello, this is the simulated file content!" + ); + Ok(()) + } + + #[tokio::test] + async fn response_not_set_content_size() -> Result<(), Box> { + let app = Router::new().route( + "/file", + get(|| async { + // Simulating a file stream + let file_content = b"Hello, this is the simulated file content!".to_vec(); + let reader = Cursor::new(file_content); + + // Response file stream + let stream = ReaderStream::new(reader); + FileStream::new(stream).file_name("test").into_response() + }), + ); + + // Simulating a GET request + let response = app + .oneshot(Request::builder().uri("/file").body(Body::empty())?) + .await?; + + // Validate Response Status Code + assert_eq!(response.status(), StatusCode::OK); + + // Validate Response Headers + assert_eq!( + response.headers().get("content-type").unwrap(), + "application/octet-stream" + ); + assert_eq!( + response.headers().get("content-disposition").unwrap(), + "attachment; filename=\"test\"" + ); + + // Validate Response Body + let body: &[u8] = &response.into_body().collect().await?.to_bytes(); + assert_eq!( + std::str::from_utf8(body)?, + "Hello, this is the simulated file content!" + ); + Ok(()) + } + + #[tokio::test] + async fn response_with_content_size_and_filename() -> Result<(), Box> { let app = Router::new().route( "/file", get(|| async { @@ -179,7 +300,7 @@ mod tests { let size = file_content.len() as u64; let reader = Cursor::new(file_content); - // response file stream + // Response file stream let stream = ReaderStream::new(reader); FileStream::new(stream) .file_name("test")