Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ insta-cmd = "0.7"
is_terminal_polyfill = "1.70"
lettre = { version = "0.11.22", default-features = false }
libtest-mimic = "0.8"
matchit = "0.9.2"
mime = "0.3"
mime_guess = { version = "2", default-features = false }
mockall = "0.15"
Expand Down
1 change: 1 addition & 0 deletions cot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ idna = { workspace = true, optional = true }
indexmap.workspace = true
is_terminal_polyfill.workspace = true
lettre = { workspace = true, features = ["builder", "sendmail-transport", "smtp-transport", "tokio1", "tokio1-rustls", "ring", "rustls-platform-verifier"], optional = true }
matchit.workspace = true
mime.workspace = true
mime_guess.workspace = true
multer.workspace = true
Expand Down
87 changes: 78 additions & 9 deletions cot/src/error_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use tracing::{Level, error, warn};
use crate::config::ProjectConfig;
use crate::error::NotFound;
use crate::router::Router;
use crate::router::path::AbsolutePath;
use crate::{Error, Result, StatusCode, Template};

#[derive(Debug)]
Expand Down Expand Up @@ -123,7 +124,12 @@ impl ErrorPageTemplateBuilder {
fn diagnostics(&mut self, diagnostics: &Diagnostics) -> &mut Self {
self.project_config = format!("{:#?}", diagnostics.project_config);
self.route_data.clear();
Self::build_route_data(&mut self.route_data, &diagnostics.router, "", "");
Self::build_route_data(
&mut self.route_data,
&diagnostics.router,
&AbsolutePath::root(),
"",
);
self.request_data = diagnostics
.request_head
.as_ref()
Expand All @@ -133,14 +139,16 @@ impl ErrorPageTemplateBuilder {

fn build_route_data(
route_data: &mut Vec<RouteData>,
router: &Router,
url_prefix: &str,
router: &Arc<Router>,
url_prefix: &AbsolutePath,
index_prefix: &str,
) {
for (index, route) in router.routes().iter().enumerate() {
let full_path = url_prefix.join(&AbsolutePath::new(route.url()));

route_data.push(RouteData {
index: format!("{index_prefix}{index}"),
path: format!("{url_prefix}{}", route.url()),
path: full_path.to_string(),
kind: match route.kind() {
crate::router::RouteKind::Router => if route_data.is_empty() {
"Root Router"
Expand All @@ -156,8 +164,8 @@ impl ErrorPageTemplateBuilder {
if let Some(inner_router) = route.router() {
Self::build_route_data(
route_data,
inner_router,
&format!("{}{}", url_prefix, route.url()),
&inner_router,
&full_path,
&format!("{index_prefix}{index}."),
);
}
Expand Down Expand Up @@ -453,6 +461,10 @@ mod tests {
use std::panic;
use std::sync::Arc;

use cot_core::handler::RequestHandler;
use cot_core::html::Html;
use cot_core::request::Request;
use cot_core::response::{IntoResponse, Response};
use tracing_test::traced_test;

use super::*;
Expand All @@ -468,6 +480,15 @@ mod tests {
}
}

struct MockHandler;

impl RequestHandler for MockHandler {
#[expect(clippy::unused_async_trait_impl)]
async fn handle(&self, _request: Request) -> Result<Response> {
Html::new("OK").into_response()
}
}

#[test]
#[traced_test]
fn test_log_error() {
Expand Down Expand Up @@ -588,9 +609,16 @@ mod tests {
let mut route_data = Vec::new();
let sub_sub_router = Router::with_urls(vec![]);
let sub_router = Router::with_urls(vec![Route::with_router("/bar", sub_sub_router)]);
let router = Router::with_urls(vec![Route::with_router("/foo", sub_router)]);

ErrorPageTemplateBuilder::build_route_data(&mut route_data, &router, "", "");
let router = Arc::new(Router::with_urls(vec![Route::with_router(
"/foo", sub_router,
)]));

ErrorPageTemplateBuilder::build_route_data(
&mut route_data,
&router,
&AbsolutePath::root(),
"",
);

assert_eq!(
route_data,
Expand All @@ -611,6 +639,47 @@ mod tests {
);
}

#[test]
fn build_route_data_root_mount_no_double_slash() {
let mut route_data = Vec::new();
let sub_router = Router::with_urls(vec![Route::with_handler_and_name(
"/",
MockHandler,
"index",
)]);
let router = Arc::new(Router::with_urls(vec![Route::with_router("/", sub_router)]));

ErrorPageTemplateBuilder::build_route_data(
&mut route_data,
&router,
&AbsolutePath::root(),
"",
);

assert_eq!(route_data[0].path, "/");
assert_eq!(route_data[1].path, "/");
}

#[test]
fn build_route_data_root_mount_with_nested_static_route() {
let mut route_data = Vec::new();
let sub_router = Router::with_urls(vec![Route::with_handler_and_name(
"/nested",
MockHandler,
"nested",
)]);
let router = Arc::new(Router::with_urls(vec![Route::with_router("/", sub_router)]));

ErrorPageTemplateBuilder::build_route_data(
&mut route_data,
&router,
&AbsolutePath::root(),
"",
);

assert_eq!(route_data[1].path, "/nested");
}

#[test]
fn test_build_cot_failure_page() {
let response = build_cot_failure_page();
Expand Down
2 changes: 1 addition & 1 deletion cot/src/openapi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
//! # async fn main() -> cot::Result<()> {
//! # let mut client = cot::test::Client::new(ApiProject).await;
//! #
//! # let response = client.get("/swagger/").await?;
//! # let response = client.get("/swagger").await?;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, we should probably modify the URL routes to include the trailing slashes.

//! # assert_eq!(response.status(), StatusCode::OK);
//! #
//! # Ok(())
Expand Down
Loading
Loading