From b80fc5437c6e772039a0eb66963bdd547a345a73 Mon Sep 17 00:00:00 2001 From: xphoniex Date: Mon, 13 Mar 2023 12:55:46 +0000 Subject: [PATCH] httpd: Move `404` test from `git` to root router Signed-off-by: xphoniex --- radicle-httpd/src/git.rs | 12 ------- radicle-httpd/src/lib.rs | 73 +++++++++++++++++++++++++++++++--------- 2 files changed, 58 insertions(+), 27 deletions(-) diff --git a/radicle-httpd/src/git.rs b/radicle-httpd/src/git.rs index 14042838..4be5946a 100644 --- a/radicle-httpd/src/git.rs +++ b/radicle-httpd/src/git.rs @@ -216,18 +216,6 @@ mod routes { use crate::test::{self, get, RID}; - #[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(), HashMap::new()) - .layer(MockConnectInfo(SocketAddr::from(([0, 0, 0, 0], 8080)))); - - let response = get(&app, "/aa/a").await; - - assert_eq!(response.status(), StatusCode::NOT_FOUND); - } - #[tokio::test] async fn test_info_request() { let tmp = tempfile::tempdir().unwrap(); diff --git a/radicle-httpd/src/lib.rs b/radicle-httpd/src/lib.rs index 650bbf82..888ecdb1 100644 --- a/radicle-httpd/src/lib.rs +++ b/radicle-httpd/src/lib.rs @@ -9,14 +9,16 @@ use std::str; use std::sync::Arc; use std::time::Duration; -use ::tracing::Span; use anyhow::Context as _; use axum::body::{Body, BoxBody, HttpBody}; use axum::http::{Request, Response}; use axum::middleware; use axum::Router; -use radicle::identity::Id; use tower_http::trace::TraceLayer; +use tracing::Span; + +use radicle::identity::Id; +use radicle::Profile; use tracing_extra::{tracing_middleware, ColoredStatus, Paint, RequestId, TracingInfo}; @@ -44,22 +46,17 @@ pub async fn run(options: Options) -> anyhow::Result<()> { tracing::info!("{}", str::from_utf8(&git_version)?.trim()); - let profile = Arc::new(radicle::Profile::load()?); + let listen = options.listen; + + tracing::info!("listening on http://{}", listen); + + let profile = Profile::load()?; let request_id = RequestId::new(); tracing::info!("using radicle home at {}", profile.home().display()); - let ctx = api::Context::new(profile.clone()); - let api_router = api::router(ctx); - let git_router = git::router(profile.clone(), options.aliases); - let raw_router = raw::router(profile); - - tracing::info!("listening on http://{}", options.listen); - - let app = Router::new() - .merge(git_router) - .nest("/api", api_router) - .nest("/raw", raw_router) + let app = + router(options, profile)? .layer(middleware::from_fn(tracing_middleware)) .layer( TraceLayer::new_for_http() @@ -95,8 +92,54 @@ pub async fn run(options: Options) -> anyhow::Result<()> { ) .into_make_service_with_connect_info::(); - axum::Server::bind(&options.listen) + axum::Server::bind(&listen) .serve(app) .await .map_err(anyhow::Error::from) } + +/// Create a router consisting of other sub-routers. +fn router(options: Options, profile: Profile) -> anyhow::Result { + let profile = Arc::new(profile); + let ctx = api::Context::new(profile.clone()); + + let api_router = api::router(ctx); + let git_router = git::router(profile.clone(), options.aliases); + let raw_router = raw::router(profile); + + let app = Router::new() + .merge(git_router) + .nest("/api", api_router) + .nest("/raw", raw_router); + + Ok(app) +} + +#[cfg(test)] +mod routes { + use std::collections::HashMap; + use std::net::SocketAddr; + + use axum::extract::connect_info::MockConnectInfo; + use axum::http::StatusCode; + + use crate::test::{self, get}; + + #[tokio::test] + async fn test_invalid_route_returns_404() { + let tmp = tempfile::tempdir().unwrap(); + let app = super::router( + super::Options { + aliases: HashMap::new(), + listen: SocketAddr::from(([0, 0, 0, 0], 8080)), + }, + test::profile(tmp.path(), [0xff; 32]), + ) + .unwrap() + .layer(MockConnectInfo(SocketAddr::from(([0, 0, 0, 0], 8080)))); + + let response = get(&app, "/aa/a").await; + + assert_eq!(response.status(), StatusCode::NOT_FOUND); + } +}