From 9e96a94f1764216f53606115ac75b96e71b726ab Mon Sep 17 00:00:00 2001 From: gengteng Date: Sat, 21 Oct 2023 20:16:47 +0800 Subject: [PATCH] add tests for validify extractors --- README.md | 1 + src/extra.rs | 52 ++++++++++++++++++++++++++++++++++++- src/validify/test.rs | 61 +++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 110 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index b19d0ef..0022d97 100644 --- a/README.md +++ b/README.md @@ -464,6 +464,7 @@ This project is licensed under the MIT License. * [axum](https://crates.io/crates/axum) * [validator](https://crates.io/crates/validator) * [garde](https://crates.io/crates/garde) +* [validify](https://crates.io/crates/validify) * [serde](https://crates.io/crates/serde) * [axum-extra](https://crates.io/crates/axum-extra) * [axum-yaml](https://crates.io/crates/axum-yaml) diff --git a/src/extra.rs b/src/extra.rs index 2dd4498..3aa901d 100644 --- a/src/extra.rs +++ b/src/extra.rs @@ -504,7 +504,7 @@ mod tests { #[cfg(feature = "validator")] use crate::Valid; #[cfg(feature = "validify")] - use crate::Validated; + use crate::{Modified, Validated, ValidifiedByRef}; use axum::http::StatusCode; use axum_extra::extract::{Cached, WithRejection}; use reqwest::RequestBuilder; @@ -617,4 +617,54 @@ mod tests { T::set_invalid_request(builder) } } + + #[cfg(feature = "validify")] + impl ValidTest for WithRejection, R> { + // just use `418 I'm a teapot` to test + const ERROR_STATUS_CODE: StatusCode = StatusCode::OK; + // If `WithRejection` is the outermost extractor, + // the error code returned will always be the one provided by WithRejection. + const INVALID_STATUS_CODE: StatusCode = StatusCode::OK; + // If `WithRejection` is the outermost extractor, + // the returned body may not be in JSON format. + const JSON_SERIALIZABLE: bool = false; + + fn set_valid_request(builder: RequestBuilder) -> RequestBuilder { + T::set_valid_request(builder) + } + + fn set_error_request(builder: RequestBuilder) -> RequestBuilder { + // invalid requests will cause the Valid extractor to fail. + T::set_invalid_request(builder) + } + + fn set_invalid_request(builder: RequestBuilder) -> RequestBuilder { + T::set_invalid_request(builder) + } + } + + #[cfg(feature = "validify")] + impl ValidTest for WithRejection, R> { + // just use `418 I'm a teapot` to test + const ERROR_STATUS_CODE: StatusCode = StatusCode::IM_A_TEAPOT; + // If `WithRejection` is the outermost extractor, + // the error code returned will always be the one provided by WithRejection. + const INVALID_STATUS_CODE: StatusCode = StatusCode::IM_A_TEAPOT; + // If `WithRejection` is the outermost extractor, + // the returned body may not be in JSON format. + const JSON_SERIALIZABLE: bool = false; + + fn set_valid_request(builder: RequestBuilder) -> RequestBuilder { + T::set_valid_request(builder) + } + + fn set_error_request(builder: RequestBuilder) -> RequestBuilder { + // invalid requests will cause the Valid extractor to fail. + T::set_invalid_request(builder) + } + + fn set_invalid_request(builder: RequestBuilder) -> RequestBuilder { + T::set_invalid_request(builder) + } + } } diff --git a/src/validify/test.rs b/src/validify/test.rs index ca0f8ca..e4194dc 100644 --- a/src/validify/test.rs +++ b/src/validify/test.rs @@ -173,7 +173,15 @@ async fn test_main() -> anyhow::Result<()> { ) .route( extra::route::WITH_REJECTION_VALIDIFY, - post(extra::extract_with_rejection_valid), + post(extra::extract_with_rejection_validifiy), + ) + .route( + extra::route::WITH_REJECTION_VALIDIFY_MODIFIED, + post(extra::extract_with_rejection_validifiy_modified), + ) + .route( + extra::route::WITH_REJECTION_VALIDIFY_VALIDIFIED_BY_REF, + post(extra::extract_with_rejection_validifiy_validified_by_ref), ); #[cfg(feature = "extra_typed_path")] @@ -594,12 +602,30 @@ async fn test_main() -> anyhow::Result<()> { ) .await?; + // Validated test_executor .execute::, WithRejectionValidifyRejection, >>(Method::POST, extra::route::WITH_REJECTION_VALIDIFY) .await?; + // Modified + test_executor + .execute_modified::, + ValidifyWithRejectionRejection, + >>(Method::POST, extra::route::WITH_REJECTION_VALIDIFY_MODIFIED) + .await?; + // ValidifiedByRef + test_executor + .execute::, + WithRejectionValidifyRejection, + >>( + Method::POST, + extra::route::WITH_REJECTION_VALIDIFY_VALIDIFIED_BY_REF, + ) + .await?; } #[cfg(feature = "extra_typed_path")] @@ -1325,7 +1351,7 @@ mod typed_multipart { mod extra { use super::{check_modified, check_validated, check_validified, ParametersValidify}; use crate::tests::{Rejection, ValidTest, ValidTestParameter}; - use crate::{Modified, Validated, ValidifiedByRef, ValidifyRejection}; + use crate::{HasModify, Modified, Validated, ValidifiedByRef, ValidifyRejection}; use axum::extract::FromRequestParts; use axum::http::request::Parts; use axum::http::StatusCode; @@ -1341,6 +1367,9 @@ mod extra { pub const WITH_REJECTION_MODIFIED: &str = "/with_rejection_modified"; pub const WITH_REJECTION_VALIDIFIED_BY_REF: &str = "/with_rejection_validified_by_ref"; pub const WITH_REJECTION_VALIDIFY: &str = "/with_rejection_validify"; + pub const WITH_REJECTION_VALIDIFY_MODIFIED: &str = "/with_rejection_validify_modified"; + pub const WITH_REJECTION_VALIDIFY_VALIDIFIED_BY_REF: &str = + "/with_rejection_validify_validified_by_ref"; } pub const PARAMETERS_HEADER: &str = "parameters-header"; pub const CACHED_REJECTION_STATUS: StatusCode = StatusCode::FORBIDDEN; @@ -1411,6 +1440,14 @@ mod extra { } } + impl HasModify for ParametersValidify { + type Modify = Self; + + fn get_modify(&mut self) -> &mut Self::Modify { + self + } + } + pub struct ValidifyWithRejectionRejection { inner: ParametersRejection, } @@ -1495,7 +1532,7 @@ mod extra { } } - pub async fn extract_with_rejection_valid( + pub async fn extract_with_rejection_validifiy( WithRejection(Validated(parameters), _): WithRejection< Validated, WithRejectionValidifyRejection, @@ -1503,6 +1540,24 @@ mod extra { ) -> StatusCode { check_validated(¶meters) } + + pub async fn extract_with_rejection_validifiy_modified( + WithRejection(Modified(parameters), _): WithRejection< + Modified, + ValidifyWithRejectionRejection, + >, + ) -> StatusCode { + check_modified(¶meters) + } + + pub async fn extract_with_rejection_validifiy_validified_by_ref( + WithRejection(ValidifiedByRef(parameters), _): WithRejection< + ValidifiedByRef, + WithRejectionValidifyRejection, + >, + ) -> StatusCode { + check_validified(¶meters) + } } #[cfg(feature = "extra_typed_path")]