From cd769d98f264ba57f4799885881c0118899cfdde Mon Sep 17 00:00:00 2001 From: Marshall Ku Date: Mon, 13 May 2024 22:36:28 +0900 Subject: [PATCH] Verify http utils --- Cargo.lock | 2 ++ Cargo.toml | 2 ++ cspell.json | 9 +++++++- src/http.rs | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 280ac8b..e47d780 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -927,10 +927,12 @@ version = "1.1.3" dependencies = [ "axum", "env_logger", + "http-body-util", "image", "log", "mime_guess", "reqwest", + "tempfile", "tokio", "tokio-util", "tower-http", diff --git a/Cargo.toml b/Cargo.toml index 414d919..896aa3d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,10 +8,12 @@ edition = "2021" [dependencies] axum = { version = "0.7.5", features = ["multipart"] } env_logger = "0.11.3" +http-body-util = "0.1.1" image = { version = "0.25.1", features = ["webp"] } log = "0.4.21" mime_guess = "2.0.4" reqwest = { version = "0.12.3", features = ["json"] } +tempfile = "3.10.1" tokio = { version = "1.37.0", features = ["macros", "rt-multi-thread", "fs"] } tokio-util = { version = "0.7.10", features = ["io"] } tower-http = { version = "0.5.2", features = ["trace"] } diff --git a/cspell.json b/cspell.json index 154f823..9aed74f 100644 --- a/cspell.json +++ b/cspell.json @@ -25,6 +25,8 @@ "referer", "reqwest", "rfind", + "tempdir", + "tempfile", "treeshake", "tsup", "Turborepo", @@ -33,5 +35,10 @@ "WEBP", "xlink" ], - "ignorePaths": ["Cargo.lock", "Cargo.toml", "target", "config/nginx.conf"] + "ignorePaths": [ + "Cargo.lock", + "Cargo.toml", + "target", + "config/nginx.conf" + ] } diff --git a/src/http.rs b/src/http.rs index 42891e1..e1e43da 100644 --- a/src/http.rs +++ b/src/http.rs @@ -37,3 +37,66 @@ pub async fn response_file(file_path: &PathBuf) -> Response { (get_cache_header(YEAR_TO_SECONDS), body).into_response() } + +#[cfg(test)] +mod tests { + use super::*; + use http_body_util::BodyExt; + use std::fs::File; + use std::io::prelude::*; + use tempfile::tempdir; + + #[tokio::test] + async fn test_response_file() { + let dir = tempdir().unwrap(); + let file_path = dir.path().join("test.txt"); + let mut file = File::create(file_path.clone()) + .and_then(|file| Ok(file)) + .unwrap(); + + file.write_all(b"test").unwrap(); + + let response = response_file(&file_path).await; + let body = response.collect().await.unwrap().to_bytes(); + + assert_eq!(body, "test".as_bytes()); + } + + #[tokio::test] + async fn test_response_file_with_error() { + let file_path = PathBuf::from("non-existing-file.txt"); + let response = response_file(&file_path).await; + + assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR); + } + + #[test] + fn test_get_cache_header() { + let headers = get_cache_header(0); + let cache_control = headers.get("Cache-Control").unwrap().to_str().unwrap(); + + assert_eq!(cache_control, "no-cache"); + + let headers = get_cache_header(100); + let cache_control = headers.get("Cache-Control").unwrap().to_str().unwrap(); + + assert_eq!(cache_control, "public, max-age=100"); + } + + #[test] + fn test_response_error() { + let response = response_error(StatusCode::NOT_FOUND); + let status = response.status(); + + assert_eq!(status, StatusCode::NOT_FOUND); + } + + #[test] + fn test_response_error_with_cache() { + let response = response_error(StatusCode::NOT_FOUND); + let headers = response.headers(); + let cache_control = headers.get("Cache-Control").unwrap().to_str().unwrap(); + + assert_eq!(cache_control, "no-cache"); + } +}