Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

app_routes tests #761

Merged
merged 2 commits into from
Sep 19, 2024
Merged
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
107 changes: 107 additions & 0 deletions src/controller/app_routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,3 +521,110 @@ fn handle_panic(err: Box<dyn std::any::Any + Send + 'static>) -> axum::response:

errors::Error::InternalServerError.into_response()
}

#[cfg(test)]
mod tests {

use super::*;
use crate::prelude::*;
use crate::tests_cfg;
use insta::assert_debug_snapshot;
use rstest::rstest;
use tower::ServiceExt;

async fn action() -> Result<Response> {
format::json("loco")
}

#[test]
fn can_load_app_route_from_default() {
for route in AppRoutes::with_default_routes().collect() {
assert_debug_snapshot!(
format!("[{}]", route.uri.replace('/', "[slash]")),
format!("{:?} {}", route.actions, route.uri)
);
}
}

#[test]
fn can_load_empty_app_routes() {
assert_eq!(AppRoutes::empty().collect().len(), 0);
}

#[test]
fn can_load_routes() {
let router_without_prefix = Routes::new().add("/", get(action));
let normalizer = Routes::new()
.prefix("/normalizer")
.add("no-slash", get(action))
.add("/", post(action))
.add("//loco///rs//", delete(action))
.add("//////multiple-start", head(action))
.add("multiple-end/////", trace(action));

let app_router = AppRoutes::empty()
.add_route(router_without_prefix)
.add_route(normalizer)
.add_routes(vec![
Routes::new().add("multiple1", put(action)),
Routes::new().add("multiple2", options(action)),
Routes::new().add("multiple3", patch(action)),
]);

for route in app_router.collect() {
assert_debug_snapshot!(
format!("[{}]", route.uri.replace('/', "[slash]")),
format!("{:?} {}", route.actions, route.uri)
);
}
}

#[test]
fn can_load_routes_with_root_prefix() {
let router_without_prefix = Routes::new()
.add("/loco", get(action))
.add("loco-rs", get(action));

let app_router = AppRoutes::empty()
.prefix("api")
.add_route(router_without_prefix);

for route in app_router.collect() {
assert_debug_snapshot!(
format!("[{}]", route.uri.replace('/', "[slash]")),
format!("{:?} {}", route.actions, route.uri)
);
}
}
#[rstest]
#[case(axum::http::Method::GET, get(action))]
#[case(axum::http::Method::POST, post(action))]
#[case(axum::http::Method::DELETE, delete(action))]
#[case(axum::http::Method::HEAD, head(action))]
#[case(axum::http::Method::OPTIONS, options(action))]
#[case(axum::http::Method::PATCH, patch(action))]
#[case(axum::http::Method::POST, post(action))]
#[case(axum::http::Method::PUT, put(action))]
#[case(axum::http::Method::TRACE, trace(action))]
#[tokio::test]
async fn can_xx(
#[case] http_method: axum::http::Method,
#[case] method: axum::routing::MethodRouter<AppContext>,
) {
let router_without_prefix = Routes::new().add("/loco", method);

let app_router = AppRoutes::empty().add_route(router_without_prefix);

let ctx = tests_cfg::app::get_app_context().await;
let router = app_router.to_router(ctx, axum::Router::new()).unwrap();

let req = axum::http::Request::builder()
.uri("/loco")
.method(http_method)
.body(axum::body::Body::empty())
.unwrap();

let response = router.oneshot(req).await.unwrap();
assert!(response.status().is_success());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/controller/app_routes.rs
expression: "format!(\"{:?} {}\", route.actions, route.uri)"
---
"[GET] /"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/controller/app_routes.rs
expression: "format!(\"{:?} {}\", route.actions, route.uri)"
---
"[GET] /_health"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/controller/app_routes.rs
expression: "format!(\"{:?} {}\", route.actions, route.uri)"
---
"[GET] /_ping"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/controller/app_routes.rs
expression: "format!(\"{:?} {}\", route.actions, route.uri)"
---
"[GET] /api/bar"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/controller/app_routes.rs
expression: "format!(\"{:?} {}\", route.actions, route.uri)"
---
"[GET] /api/foo"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/controller/app_routes.rs
expression: "format!(\"{:?} {}\", route.actions, route.uri)"
---
"[GET] /api/loco-rs"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/controller/app_routes.rs
expression: "format!(\"{:?} {}\", route.actions, route.uri)"
---
"[GET] /api/loco"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/controller/app_routes.rs
expression: "format!(\"{:?} {}\", route.actions, route.uri)"
---
"[PUT] /multiple1"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/controller/app_routes.rs
expression: "format!(\"{:?} {}\", route.actions, route.uri)"
---
"[OPTIONS] /multiple2"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/controller/app_routes.rs
expression: "format!(\"{:?} {}\", route.actions, route.uri)"
---
"[PATCH] /multiple3"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/controller/app_routes.rs
expression: "format!(\"{:?} {}\", route.actions, route.uri)"
---
"[DELETE] /normalizer/foo/bar"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/controller/app_routes.rs
expression: "format!(\"{:?} {}\", route.actions, route.uri)"
---
"[DELETE] /normalizer/loco/rs"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/controller/app_routes.rs
expression: "format!(\"{:?} {}\", route.actions, route.uri)"
---
"[TRACE] /normalizer/multiple-end"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/controller/app_routes.rs
expression: "format!(\"{:?} {}\", route.actions, route.uri)"
---
"[HEAD] /normalizer/multiple-start"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/controller/app_routes.rs
expression: "format!(\"{:?} {}\", route.actions, route.uri)"
---
"[GET] /normalizer/no-slash"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/controller/app_routes.rs
expression: "format!(\"{:?} {}\", route.actions, route.uri)"
---
"[POST] /normalizer"
2 changes: 1 addition & 1 deletion src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pub use async_trait::async_trait;
pub use axum::{
extract::{Form, Path, State},
response::{IntoResponse, Response},
routing::{delete, get, post, put},
routing::{delete, get, head, options, patch, post, put, trace},
};
pub use axum_extra::extract::cookie;
pub use chrono::NaiveDateTime as DateTime;
Expand Down
Loading