Radish alpha
r
Radicle web interface
Radicle
Git (anonymous pull)
Log in to clone via SSH
httpd: Change `500` into `404` when project doesn't exist
xphoniex committed 3 years ago
commit 1185a1fc6bf36cf5417949fc2e61ec38836cb459
parent 1a2ee8da58a4d1684461cba060bc53543cf33a48
3 files changed +41 -2
modified 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<Profile> {
+
        &self.profile
+
    }
}

pub fn router(ctx: Context) -> Router {
modified 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,
        }
    }
modified 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);
+
    }
+
}