From 37c1f42888ff2067a45d896ee75b8e97b974bebe Mon Sep 17 00:00:00 2001 From: gengteng Date: Fri, 4 Aug 2023 21:08:52 +0800 Subject: [PATCH] add test for Form, Query from axum-extra --- README.md | 5 ++- src/extra/form.rs | 23 +++++++++++++ src/extra/query.rs | 24 ++++++++++++++ src/test.rs | 82 ++++++++++++++++++++++++++++++++++++++++------ 4 files changed, 121 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 6a88c72..ac82084 100644 --- a/README.md +++ b/README.md @@ -59,10 +59,9 @@ When validation errors occur, the extractor will automatically return 400 with v | typed_header | Enables support for `TypedHeader` | ❌ | ✅ | | msgpack | Enables support for `MsgPack` and `MsgPackRaw` from `axum-msgpack` | ❌ | ❌ | | yaml | Enables support for `Yaml` from `axum-yaml` | ❌ | ❌ | -| extra_protobuf | Enables support for `Protobuf` from `axum-extra` | ❌ | ❌ | | extra | Enables support for `Cached`, `WithRejection` from `axum-extra` | ❌ | ✅ | -| extra_query | Enables support for `Query` from `axum-extra` | ❌ | ❌ | -| extra_form | Enables support for `Form` from `axum-extra` | ❌ | ❌ | +| extra_query | Enables support for `Query` from `axum-extra` | ❌ | ✅ | +| extra_form | Enables support for `Form` from `axum-extra` | ❌ | ✅ | | extra_protobuf | Enables support for `Protobuf` from `axum-extra` | ❌ | ❌ | | extra_all | Enables support for all extractors above from `axum-extra` | ❌ | 🚧 | | all_types | Enables support for all extractors above | ❌ | 🚧 | diff --git a/src/extra/form.rs b/src/extra/form.rs index e9ab747..a542dd7 100644 --- a/src/extra/form.rs +++ b/src/extra/form.rs @@ -11,3 +11,26 @@ impl HasValidate for Form { &self.0 } } + +#[cfg(test)] +mod tests { + use crate::tests::{ValidTest, ValidTestParameter}; + use axum_extra::extract::Form; + use reqwest::{RequestBuilder, StatusCode}; + + impl ValidTest for Form { + const ERROR_STATUS_CODE: StatusCode = StatusCode::BAD_REQUEST; + + fn set_valid_request(builder: RequestBuilder) -> RequestBuilder { + builder.form(T::valid()) + } + + fn set_error_request(builder: RequestBuilder) -> RequestBuilder { + builder.form(T::error()) + } + + fn set_invalid_request(builder: RequestBuilder) -> RequestBuilder { + builder.form(T::invalid()) + } + } +} diff --git a/src/extra/query.rs b/src/extra/query.rs index 4968187..4e7a371 100644 --- a/src/extra/query.rs +++ b/src/extra/query.rs @@ -11,3 +11,27 @@ impl HasValidate for Query { &self.0 } } + +#[cfg(test)] +mod tests { + use crate::tests::{ValidTest, ValidTestParameter}; + use axum::http::StatusCode; + use axum_extra::extract::Query; + use reqwest::RequestBuilder; + + impl ValidTest for Query { + const ERROR_STATUS_CODE: StatusCode = StatusCode::BAD_REQUEST; + + fn set_valid_request(builder: RequestBuilder) -> RequestBuilder { + builder.query(&T::valid()) + } + + fn set_error_request(builder: RequestBuilder) -> RequestBuilder { + builder.query(T::error()) + } + + fn set_invalid_request(builder: RequestBuilder) -> RequestBuilder { + builder.query(&T::invalid()) + } + } +} diff --git a/src/test.rs b/src/test.rs index ba10e97..253fb5f 100644 --- a/src/test.rs +++ b/src/test.rs @@ -59,12 +59,24 @@ async fn test_main() -> anyhow::Result<()> { #[cfg(feature = "extra")] let router = router - .route(route::extra::CACHED, post(extra::extract_cached)) + .route(extra::route::CACHED, post(extra::extract_cached)) .route( - route::extra::WITH_REJECTION, + extra::route::WITH_REJECTION, post(extra::extract_with_rejection), ); + #[cfg(feature = "extra_query")] + let router = router.route( + extra_query::route::EXTRA_QUERY, + post(extra_query::extract_extra_query), + ); + + #[cfg(feature = "extra_form")] + let router = router.route( + extra_form::route::EXTRA_FORM, + post(extra_form::extract_extra_form), + ); + let server = axum::Server::bind(&SocketAddr::from(([0u8, 0, 0, 0], 0u16))) .serve(router.into_make_service()); let server_addr = server.local_addr(); @@ -134,16 +146,32 @@ async fn test_main() -> anyhow::Result<()> { use axum_extra::extract::{Cached, WithRejection}; use extra::TestRejection; test_executor - .execute::>(Method::POST, route::extra::CACHED) + .execute::>(Method::POST, extra::route::CACHED) .await?; test_executor .execute::>( Method::POST, - route::extra::WITH_REJECTION, + extra::route::WITH_REJECTION, ) .await?; } + #[cfg(feature = "extra_query")] + { + use axum_extra::extract::Query; + test_executor + .execute::>(Method::POST, extra_query::route::EXTRA_QUERY) + .await?; + } + + #[cfg(feature = "extra_form")] + { + use axum_extra::extract::Form; + test_executor + .execute::>(Method::POST, extra_form::route::EXTRA_FORM) + .await?; + } + drop(server_guard); server_handle.await??; Ok(()) @@ -211,12 +239,6 @@ mod route { pub const QUERY: &str = "/query"; pub const FORM: &str = "/form"; pub const JSON: &str = "/json"; - - #[cfg(feature = "extra")] - pub mod extra { - pub const CACHED: &str = "/cached"; - pub const WITH_REJECTION: &str = "/with_rejection"; - } } async fn extract_path(Valid(Path(parameters)): Valid>) -> StatusCode { @@ -327,6 +349,10 @@ mod extra { use axum_extra::extract::{Cached, WithRejection}; use reqwest::RequestBuilder; + pub mod route { + pub const CACHED: &str = "/cached"; + pub const WITH_REJECTION: &str = "/with_rejection"; + } pub const PARAMETERS_HEADER: &str = "parameters-header"; pub const CACHED_REJECTION_STATUS: StatusCode = StatusCode::FORBIDDEN; @@ -428,3 +454,39 @@ mod extra { validate_again(parameters) } } + +#[cfg(feature = "extra_query")] +mod extra_query { + use crate::test::{validate_again, Parameters}; + use crate::Valid; + use axum::http::StatusCode; + use axum_extra::extract::Query; + + pub mod route { + pub const EXTRA_QUERY: &str = "/extra_query"; + } + + pub async fn extract_extra_query( + Valid(Query(parameters)): Valid>, + ) -> StatusCode { + validate_again(parameters) + } +} + +#[cfg(feature = "extra_form")] +pub mod extra_form { + use crate::test::{validate_again, Parameters}; + use crate::Valid; + use axum::http::StatusCode; + use axum_extra::extract::Form; + + pub mod route { + pub const EXTRA_FORM: &str = "/extra_form"; + } + + pub async fn extract_extra_form( + Valid(Form(parameters)): Valid>, + ) -> StatusCode { + validate_again(parameters) + } +}