Added file not found responce.

This commit is contained in:
Jeff Baskin 2025-04-16 10:16:41 -04:00
parent a38bfc200d
commit 9e6d407b69

View File

@ -1,4 +1,4 @@
use axum::{extract::State, handler::Handler, response::IntoResponse}; use axum::{extract::State, response::IntoResponse, routing::get, Router};
use axum_extra::extract::cookie::{Cookie, CookieJar}; use axum_extra::extract::cookie::{Cookie, CookieJar};
use clap::Parser; use clap::Parser;
use morethantext::MoreThanText; use morethantext::MoreThanText;
@ -26,13 +26,17 @@ async fn main() {
let args = Args::parse(); let args = Args::parse();
let addr = format!("{}:{}", args.address, args.port); let addr = format!("{}:{}", args.address, args.port);
let state = MoreThanText::new(); let state = MoreThanText::new();
let app = mtt_conn.with_state(state); let app = create_app(state).await;
let listener = tokio::net::TcpListener::bind(&addr).await.unwrap(); let listener = tokio::net::TcpListener::bind(&addr).await.unwrap();
axum::serve(listener, app.into_make_service()) axum::serve(listener, app.into_make_service())
.await .await
.unwrap(); .unwrap();
} }
async fn create_app(state: MoreThanText) -> Router {
Router::new().route("/", get(mtt_conn)).with_state(state)
}
async fn mtt_conn(jar: CookieJar, state: State<MoreThanText>) -> impl IntoResponse { async fn mtt_conn(jar: CookieJar, state: State<MoreThanText>) -> impl IntoResponse {
let sid = match jar.get(SESSION_KEY) { let sid = match jar.get(SESSION_KEY) {
Some(cookie) => Some(cookie.value().to_string()), Some(cookie) => Some(cookie.value().to_string()),
@ -60,7 +64,7 @@ mod servers {
#[tokio::test] #[tokio::test]
async fn get_home_page() { async fn get_home_page() {
let app = mtt_conn.with_state(MoreThanText::new()); let app = create_app(MoreThanText::new()).await;
let response = app let response = app
.oneshot(Request::builder().uri("/").body(Body::empty()).unwrap()) .oneshot(Request::builder().uri("/").body(Body::empty()).unwrap())
.await .await
@ -72,7 +76,7 @@ mod servers {
#[tokio::test] #[tokio::test]
async fn session_ids_are_unique() { async fn session_ids_are_unique() {
let app = mtt_conn.with_state(MoreThanText::new()); let app = create_app(MoreThanText::new()).await;
let mut holder: Vec<String> = Vec::new(); let mut holder: Vec<String> = Vec::new();
for _ in 0..5 { for _ in 0..5 {
let response = app let response = app
@ -89,4 +93,19 @@ mod servers {
holder.push(sessid); holder.push(sessid);
} }
} }
#[tokio::test]
async fn receive_file_not_found() {
let app = create_app(MoreThanText::new()).await;
let response = app
.oneshot(
Request::builder()
.uri("/isomething")
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(response.status(), StatusCode::NOT_FOUND);
}
} }