From 3770e7b05712a9ca0ad9e180a55231f4dc9d3602 Mon Sep 17 00:00:00 2001 From: Altair-Bueno <67512202+Altair-Bueno@users.noreply.github.com> Date: Mon, 15 Aug 2022 18:27:06 +0200 Subject: [PATCH] tests(axum-extra): Added test for `WithRejection` --- axum-extra/src/extract/with_rejection.rs | 43 ++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/axum-extra/src/extract/with_rejection.rs b/axum-extra/src/extract/with_rejection.rs index 586afa5f..0f40fa00 100644 --- a/axum-extra/src/extract/with_rejection.rs +++ b/axum-extra/src/extract/with_rejection.rs @@ -41,3 +41,46 @@ where Ok(WithRejection(extractor, PhantomData)) } } + +#[cfg(test)] +mod tests { + use axum::http::Request; + use axum::response::Response; + + use super::*; + + #[tokio::test] + async fn extractor_rejection_is_transformed() { + struct TestExtractor; + struct TestRejection; + + #[async_trait] + impl FromRequest for TestExtractor { + type Rejection = (); + + async fn from_request(_: &mut RequestParts) -> Result { + Err(()) + } + } + + impl IntoResponse for TestRejection { + fn into_response(self) -> Response { + ().into_response() + } + } + + impl From<()> for TestRejection { + fn from(_: ()) -> Self { + TestRejection + } + } + + let mut req = RequestParts::new(Request::new(())); + + let result = req + .extract::>() + .await; + + assert!(matches!(result, Err(TestRejection))) + } +}