Skip to content

Commit

Permalink
Verify http utils
Browse files Browse the repository at this point in the history
  • Loading branch information
marshallku committed May 13, 2024
1 parent b1bab2b commit cd769d9
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
9 changes: 8 additions & 1 deletion cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
"referer",
"reqwest",
"rfind",
"tempdir",
"tempfile",
"treeshake",
"tsup",
"Turborepo",
Expand All @@ -33,5 +35,10 @@
"WEBP",
"xlink"
],
"ignorePaths": ["Cargo.lock", "Cargo.toml", "target", "config/nginx.conf"]
"ignorePaths": [
"Cargo.lock",
"Cargo.toml",
"target",
"config/nginx.conf"
]
}
63 changes: 63 additions & 0 deletions src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

0 comments on commit cd769d9

Please sign in to comment.