From 6e0b45b05f408c304f8a54cee944272dc2a9c7ff Mon Sep 17 00:00:00 2001 From: gengteng Date: Mon, 4 Dec 2023 11:56:59 +0800 Subject: [PATCH] test: Add tests for xml and toml --- src/garde/test.rs | 54 +++++++++++++++ src/validator/test.rs | 87 ++++++++++++++++++++++++ src/validify/test.rs | 154 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 295 insertions(+) diff --git a/src/garde/test.rs b/src/garde/test.rs index 994974b..198cdca 100644 --- a/src/garde/test.rs +++ b/src/garde/test.rs @@ -140,6 +140,12 @@ async fn test_main() -> anyhow::Result<()> { post(msgpack::extract_msgpack_raw), ); + #[cfg(feature = "xml")] + let router = router.route(xml::route::XML, post(xml::extract_xml)); + + #[cfg(feature = "toml")] + let router = router.route(toml::route::TOML, post(toml::extract_toml)); + let router = router.with_state(MyState::default()); let listener = TcpListener::bind(&SocketAddr::from(([0u8, 0, 0, 0], 0u16))).await?; @@ -388,6 +394,22 @@ async fn test_main() -> anyhow::Result<()> { .await?; } + #[cfg(feature = "xml")] + { + use axum_serde::Xml; + test_executor + .execute::>(Method::POST, xml::route::XML) + .await?; + } + + #[cfg(feature = "toml")] + { + use axum_serde::Toml; + test_executor + .execute::>(Method::POST, toml::route::TOML) + .await?; + } + Ok(()) } @@ -887,3 +909,35 @@ mod msgpack { validate_again(parameters, ()) } } + +#[cfg(feature = "xml")] +mod xml { + use super::{validate_again, ParametersGarde}; + use crate::Garde; + use axum::http::StatusCode; + use axum_serde::Xml; + + pub mod route { + pub const XML: &str = "/xml"; + } + + pub async fn extract_xml(Garde(Xml(parameters)): Garde>) -> StatusCode { + validate_again(parameters, ()) + } +} + +#[cfg(feature = "toml")] +mod toml { + use super::{validate_again, ParametersGarde}; + use crate::Garde; + use axum::http::StatusCode; + use axum_serde::Toml; + + pub mod route { + pub const TOML: &str = "/toml"; + } + + pub async fn extract_toml(Garde(Toml(parameters)): Garde>) -> StatusCode { + validate_again(parameters, ()) + } +} diff --git a/src/validator/test.rs b/src/validator/test.rs index 6e966ad..491515d 100644 --- a/src/validator/test.rs +++ b/src/validator/test.rs @@ -271,6 +271,16 @@ async fn test_main() -> anyhow::Result<()> { post(msgpack::extract_msgpack_raw_ex), ); + #[cfg(feature = "xml")] + let router = router + .route(xml::route::XML, post(xml::extract_xml)) + .route(xml::route::XML_EX, post(xml::extract_xml_ex)); + + #[cfg(feature = "toml")] + let router = router + .route(toml::route::TOML, post(toml::extract_toml)) + .route(toml::route::TOML_EX, post(toml::extract_toml_ex)); + let router = router.with_state(state); let listener = TcpListener::bind(&SocketAddr::from(([0u8, 0, 0, 0], 0u16))).await?; @@ -584,6 +594,28 @@ async fn test_main() -> anyhow::Result<()> { .await?; } + #[cfg(feature = "xml")] + { + use axum_serde::Xml; + test_executor + .execute::>(Method::POST, xml::route::XML) + .await?; + test_executor + .execute::>(Method::POST, xml::route::XML_EX) + .await?; + } + + #[cfg(feature = "toml")] + { + use axum_serde::Toml; + test_executor + .execute::>(Method::POST, toml::route::TOML) + .await?; + test_executor + .execute::>(Method::POST, toml::route::TOML_EX) + .await?; + } + Ok(()) } @@ -1360,3 +1392,58 @@ mod msgpack { validate_again_ex(parameters, args.get()) } } + +#[cfg(feature = "xml")] +mod xml { + use super::{ + validate_again, validate_again_ex, Parameters, ParametersEx, + ParametersExValidationArguments, + }; + use crate::{Arguments, Valid, ValidEx}; + use axum::http::StatusCode; + use axum_serde::Xml; + + pub mod route { + pub const XML: &str = "/xml"; + pub const XML_EX: &str = "/xml_ex"; + } + + pub async fn extract_xml(Valid(Xml(parameters)): Valid>) -> StatusCode { + validate_again(parameters) + } + + pub async fn extract_xml_ex( + ValidEx(Xml(parameters), args): ValidEx, ParametersExValidationArguments>, + ) -> StatusCode { + validate_again_ex(parameters, args.get()) + } +} + +#[cfg(feature = "toml")] +mod toml { + use super::{ + validate_again, validate_again_ex, Parameters, ParametersEx, + ParametersExValidationArguments, + }; + use crate::{Arguments, Valid, ValidEx}; + use axum::http::StatusCode; + use axum_serde::Toml; + + pub mod route { + pub const TOML: &str = "/toml"; + pub const TOML_EX: &str = "/toml_ex"; + } + + pub async fn extract_toml(Valid(Toml(parameters)): Valid>) -> StatusCode { + validate_again(parameters) + } + + pub async fn extract_toml_ex( + ValidEx(Toml(parameters), args): ValidEx< + Toml, + ParametersExValidationArguments, + >, + ) -> StatusCode { + validate_again_ex(parameters, args.get()) + } +} diff --git a/src/validify/test.rs b/src/validify/test.rs index eb75418..a19a559 100644 --- a/src/validify/test.rs +++ b/src/validify/test.rs @@ -302,6 +302,35 @@ async fn test_main() -> anyhow::Result<()> { post(msgpack::extract_msgpack_raw_validified_by_ref), ); + #[cfg(feature = "xml")] + let router = router + .route(xml::route::XML, post(xml::extract_xml)) + .route(xml::route::XML_MODIFIED, post(xml::extract_xml_modified)) + .route( + xml::route::XML_VALIDIFIED, + post(xml::extract_xml_validified), + ) + .route( + xml::route::XML_VALIDIFIED_BY_REF, + post(xml::extract_xml_validified_by_ref), + ); + + #[cfg(feature = "toml")] + let router = router + .route(toml::route::TOML, post(toml::extract_toml)) + .route( + toml::route::TOML_MODIFIED, + post(toml::extract_toml_modified), + ) + .route( + toml::route::TOML_VALIDIFIED, + post(toml::extract_toml_validified), + ) + .route( + toml::route::TOML_VALIDIFIED_BY_REF, + post(toml::extract_toml_validified_by_ref), + ); + let listener = TcpListener::bind(&SocketAddr::from(([0u8, 0, 0, 0], 0u16))).await?; let server_addr = listener.local_addr()?; let server = axum::serve(listener, router.into_make_service()); @@ -907,6 +936,53 @@ async fn test_main() -> anyhow::Result<()> { .await?; } + #[cfg(feature = "xml")] + { + use axum_serde::Xml; + + // Validated + test_executor + .execute::>(Method::POST, xml::route::XML) + .await?; + // Modified + test_executor + .execute_modified::>(Method::POST, xml::route::XML_MODIFIED) + .await?; + // Validified + test_executor + .execute_validified::>(Method::POST, xml::route::XML_VALIDIFIED) + .await?; + // ValidifiedByRef + test_executor + .execute::>(Method::POST, xml::route::XML_VALIDIFIED_BY_REF) + .await?; + } + + #[cfg(feature = "toml")] + { + use axum_serde::Toml; + + // Validated + test_executor + .execute::>(Method::POST, toml::route::TOML) + .await?; + // Modified + test_executor + .execute_modified::>(Method::POST, toml::route::TOML_MODIFIED) + .await?; + // Validified + test_executor + .execute_validified::>( + Method::POST, + toml::route::TOML_VALIDIFIED, + ) + .await?; + // ValidifiedByRef + test_executor + .execute::>(Method::POST, toml::route::TOML_VALIDIFIED_BY_REF) + .await?; + } + Ok(()) } @@ -1869,3 +1945,81 @@ mod msgpack { check_validified(¶meters) } } + +#[cfg(feature = "xml")] +mod xml { + use super::{check_modified, check_validated, check_validified, ParametersValidify}; + use crate::{Modified, Validated, Validified, ValidifiedByRef}; + use axum::http::StatusCode; + use axum_serde::Xml; + + pub mod route { + pub const XML: &str = "/xml"; + pub const XML_MODIFIED: &str = "/xml_modified"; + pub const XML_VALIDIFIED: &str = "/xml_validified"; + pub const XML_VALIDIFIED_BY_REF: &str = "/xml_validified_by_ref"; + } + + pub async fn extract_xml( + Validated(Xml(parameters)): Validated>, + ) -> StatusCode { + check_validated(¶meters) + } + + pub async fn extract_xml_modified( + Modified(Xml(parameters)): Modified>, + ) -> StatusCode { + check_modified(¶meters) + } + + pub async fn extract_xml_validified( + Validified(Xml(parameters)): Validified>, + ) -> StatusCode { + check_validified(¶meters) + } + + pub async fn extract_xml_validified_by_ref( + ValidifiedByRef(Xml(parameters)): ValidifiedByRef>, + ) -> StatusCode { + check_validified(¶meters) + } +} + +#[cfg(feature = "toml")] +mod toml { + use super::{check_modified, check_validated, check_validified, ParametersValidify}; + use crate::{Modified, Validated, Validified, ValidifiedByRef}; + use axum::http::StatusCode; + use axum_serde::Toml; + + pub mod route { + pub const TOML: &str = "/toml"; + pub const TOML_MODIFIED: &str = "/toml_modified"; + pub const TOML_VALIDIFIED: &str = "/toml_validified"; + pub const TOML_VALIDIFIED_BY_REF: &str = "/toml_validified_by_ref"; + } + + pub async fn extract_toml( + Validated(Toml(parameters)): Validated>, + ) -> StatusCode { + check_validated(¶meters) + } + + pub async fn extract_toml_modified( + Modified(Toml(parameters)): Modified>, + ) -> StatusCode { + check_modified(¶meters) + } + + pub async fn extract_toml_validified( + Validified(Toml(parameters)): Validified>, + ) -> StatusCode { + check_validified(¶meters) + } + + pub async fn extract_toml_validified_by_ref( + ValidifiedByRef(Toml(parameters)): ValidifiedByRef>, + ) -> StatusCode { + check_validified(¶meters) + } +}