Skip to content

Commit

Permalink
Verify logics for caching files
Browse files Browse the repository at this point in the history
  • Loading branch information
marshallku committed Sep 5, 2024
1 parent 999eef6 commit 8300b82
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/controllers/__tests__/files.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#[cfg(test)]
mod tests {

use std::path::PathBuf;

use axum::{
body::Body,
http::{Request, StatusCode},
};
use tower::ServiceExt;

use crate::{constants::CDN_ROOT, controllers::app::app, env::state::AppState};

const URI: &str = "/files";

#[tokio::test]
async fn test_response_file() {
let app = app();
let state = AppState::from_env();
let file_path = "/images/hpp/ic_wahlberg_product_core_48.png8.png";
let response = app
.with_state(state)
.oneshot(
Request::builder()
.uri(format!("{}{}", URI, file_path))
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
let local_file_path = PathBuf::from(format!("{}{}{}", CDN_ROOT, URI, file_path));

assert_eq!(response.status(), StatusCode::OK);
assert!(local_file_path.exists());
}

#[tokio::test]
async fn test_response_error() {
let app = app();
let state = AppState::from_env();
let file_path = "/images/you-must-not-exist.png";
let response = app
.with_state(state)
.oneshot(
Request::builder()
.uri(format!("{}{}", URI, file_path))
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
let local_file_path = PathBuf::from(format!("{}{}{}", CDN_ROOT, URI, file_path));

assert_eq!(response.status(), StatusCode::NOT_FOUND);
assert!(!local_file_path.exists());
}
}
1 change: 1 addition & 0 deletions src/controllers/__tests__/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
mod files;
mod images;

0 comments on commit 8300b82

Please sign in to comment.