From fcbc0fac7b522c965869b2350f62ffa717dbb50e Mon Sep 17 00:00:00 2001 From: FloRide1 Date: Fri, 20 Sep 2024 16:54:52 +0200 Subject: [PATCH] feat(tests): Add test related utils function --- tests/utils.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 tests/utils.rs diff --git a/tests/utils.rs b/tests/utils.rs new file mode 100644 index 0000000..75ae578 --- /dev/null +++ b/tests/utils.rs @@ -0,0 +1,48 @@ +use axum::response::IntoResponse; +use axum_oidc::{EmptyAdditionalClaims, OidcClaims}; + + +pub async fn authenticated(claims: OidcClaims) -> impl IntoResponse { + format!("Hello {}", claims.subject().as_str()) +} + +pub async fn maybe_authenticated( + claims: Option>, +) -> impl IntoResponse { + if let Some(claims) = claims { + format!( + "Hello {}! You are already logged in from another Handler.", + claims.subject().as_str() + ) + } else { + "Hello anon!".to_string() + } +} + +pub async fn handle_axum_oidc_middleware_error( + e: axum_oidc::error::MiddlewareError, +) -> axum::http::Response { + e.into_response() +} + +pub fn extract_location_header_testresponse(response: axum_test::TestResponse) -> Option { + Some( + response + .headers() + .get("Location")? + .to_str() + .ok()? + .to_string(), + ) +} + +pub fn extract_location_header_response(response: reqwest::Response) -> Option { + Some( + response + .headers() + .get("Location")? + .to_str() + .ok()? + .to_string(), + ) +}