diff --git a/radicle-httpd/src/api.rs b/radicle-httpd/src/api.rs index d0b2dd6d..98ec964b 100644 --- a/radicle-httpd/src/api.rs +++ b/radicle-httpd/src/api.rs @@ -1,3 +1,6 @@ +#[cfg(test)] +pub mod test; + use std::collections::HashMap; use std::sync::Arc; use std::time::Duration; @@ -22,8 +25,6 @@ mod auth; mod axum_extra; mod error; mod json; -#[cfg(test)] -mod test; mod v1; pub const VERSION: &str = env!("CARGO_PKG_VERSION"); @@ -64,6 +65,11 @@ impl Context { id, }) } + + #[cfg(test)] + pub fn profile(&self) -> &Arc { + &self.profile + } } pub fn router(ctx: Context) -> Router { diff --git a/radicle-httpd/src/error.rs b/radicle-httpd/src/error.rs index 01f11f6a..1bee5d9e 100644 --- a/radicle-httpd/src/error.rs +++ b/radicle-httpd/src/error.rs @@ -38,6 +38,7 @@ impl Error { match self { Error::ServiceUnavailable(_) => http::StatusCode::SERVICE_UNAVAILABLE, Error::InvalidId => http::StatusCode::NOT_FOUND, + Error::Id(_) => http::StatusCode::NOT_FOUND, _ => http::StatusCode::INTERNAL_SERVER_ERROR, } } diff --git a/radicle-httpd/src/git.rs b/radicle-httpd/src/git.rs index cf4c3784..ddc49dd2 100644 --- a/radicle-httpd/src/git.rs +++ b/radicle-httpd/src/git.rs @@ -192,3 +192,35 @@ async fn git_http_backend( } } } + +#[cfg(test)] +mod routes { + use std::net::SocketAddr; + + use axum::body::Body; + use axum::extract::ConnectInfo; + use axum::http::Request; + use axum::http::{Method, StatusCode}; + use tower::ServiceExt; + + use crate::api::test; + + #[tokio::test] + async fn test_invalid_route_returns_404() { + let tmp = tempfile::tempdir().unwrap(); + let ctx = test::seed(tmp.path()); + let app = super::router(ctx.profile().to_owned()); + + let request = Request::builder() + .method(Method::GET) + .uri("/aa/a".to_string()); + + let mut request = request.body(Body::empty()).unwrap(); + let socket_addr: SocketAddr = "127.0.0.1:8080".parse().unwrap(); + request.extensions_mut().insert(ConnectInfo(socket_addr)); + + let response = app.oneshot(request).await.unwrap(); + + assert_eq!(response.status(), StatusCode::NOT_FOUND); + } +}