test: Add tests for xml and toml

This commit is contained in:
gengteng
2023-12-04 11:56:59 +08:00
parent 8acf2c2ff6
commit 6e0b45b05f
3 changed files with 295 additions and 0 deletions

View File

@@ -140,6 +140,12 @@ async fn test_main() -> anyhow::Result<()> {
post(msgpack::extract_msgpack_raw), 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 router = router.with_state(MyState::default());
let listener = TcpListener::bind(&SocketAddr::from(([0u8, 0, 0, 0], 0u16))).await?; let listener = TcpListener::bind(&SocketAddr::from(([0u8, 0, 0, 0], 0u16))).await?;
@@ -388,6 +394,22 @@ async fn test_main() -> anyhow::Result<()> {
.await?; .await?;
} }
#[cfg(feature = "xml")]
{
use axum_serde::Xml;
test_executor
.execute::<Xml<ParametersGarde>>(Method::POST, xml::route::XML)
.await?;
}
#[cfg(feature = "toml")]
{
use axum_serde::Toml;
test_executor
.execute::<Toml<ParametersGarde>>(Method::POST, toml::route::TOML)
.await?;
}
Ok(()) Ok(())
} }
@@ -887,3 +909,35 @@ mod msgpack {
validate_again(parameters, ()) 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<Xml<ParametersGarde>>) -> 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<Toml<ParametersGarde>>) -> StatusCode {
validate_again(parameters, ())
}
}

View File

@@ -271,6 +271,16 @@ async fn test_main() -> anyhow::Result<()> {
post(msgpack::extract_msgpack_raw_ex), 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 router = router.with_state(state);
let listener = TcpListener::bind(&SocketAddr::from(([0u8, 0, 0, 0], 0u16))).await?; let listener = TcpListener::bind(&SocketAddr::from(([0u8, 0, 0, 0], 0u16))).await?;
@@ -584,6 +594,28 @@ async fn test_main() -> anyhow::Result<()> {
.await?; .await?;
} }
#[cfg(feature = "xml")]
{
use axum_serde::Xml;
test_executor
.execute::<Xml<Parameters>>(Method::POST, xml::route::XML)
.await?;
test_executor
.execute::<Xml<Parameters>>(Method::POST, xml::route::XML_EX)
.await?;
}
#[cfg(feature = "toml")]
{
use axum_serde::Toml;
test_executor
.execute::<Toml<Parameters>>(Method::POST, toml::route::TOML)
.await?;
test_executor
.execute::<Toml<Parameters>>(Method::POST, toml::route::TOML_EX)
.await?;
}
Ok(()) Ok(())
} }
@@ -1360,3 +1392,58 @@ mod msgpack {
validate_again_ex(parameters, args.get()) 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<Xml<Parameters>>) -> StatusCode {
validate_again(parameters)
}
pub async fn extract_xml_ex(
ValidEx(Xml(parameters), args): ValidEx<Xml<ParametersEx>, 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<Toml<Parameters>>) -> StatusCode {
validate_again(parameters)
}
pub async fn extract_toml_ex(
ValidEx(Toml(parameters), args): ValidEx<
Toml<ParametersEx>,
ParametersExValidationArguments,
>,
) -> StatusCode {
validate_again_ex(parameters, args.get())
}
}

View File

@@ -302,6 +302,35 @@ async fn test_main() -> anyhow::Result<()> {
post(msgpack::extract_msgpack_raw_validified_by_ref), 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 listener = TcpListener::bind(&SocketAddr::from(([0u8, 0, 0, 0], 0u16))).await?;
let server_addr = listener.local_addr()?; let server_addr = listener.local_addr()?;
let server = axum::serve(listener, router.into_make_service()); let server = axum::serve(listener, router.into_make_service());
@@ -907,6 +936,53 @@ async fn test_main() -> anyhow::Result<()> {
.await?; .await?;
} }
#[cfg(feature = "xml")]
{
use axum_serde::Xml;
// Validated
test_executor
.execute::<Xml<ParametersValidify>>(Method::POST, xml::route::XML)
.await?;
// Modified
test_executor
.execute_modified::<Xml<ParametersValidify>>(Method::POST, xml::route::XML_MODIFIED)
.await?;
// Validified
test_executor
.execute_validified::<Xml<ParametersValidify>>(Method::POST, xml::route::XML_VALIDIFIED)
.await?;
// ValidifiedByRef
test_executor
.execute::<Xml<ParametersValidify>>(Method::POST, xml::route::XML_VALIDIFIED_BY_REF)
.await?;
}
#[cfg(feature = "toml")]
{
use axum_serde::Toml;
// Validated
test_executor
.execute::<Toml<ParametersValidify>>(Method::POST, toml::route::TOML)
.await?;
// Modified
test_executor
.execute_modified::<Toml<ParametersValidify>>(Method::POST, toml::route::TOML_MODIFIED)
.await?;
// Validified
test_executor
.execute_validified::<Toml<ParametersValidify>>(
Method::POST,
toml::route::TOML_VALIDIFIED,
)
.await?;
// ValidifiedByRef
test_executor
.execute::<Toml<ParametersValidify>>(Method::POST, toml::route::TOML_VALIDIFIED_BY_REF)
.await?;
}
Ok(()) Ok(())
} }
@@ -1869,3 +1945,81 @@ mod msgpack {
check_validified(&parameters) check_validified(&parameters)
} }
} }
#[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<Xml<ParametersValidify>>,
) -> StatusCode {
check_validated(&parameters)
}
pub async fn extract_xml_modified(
Modified(Xml(parameters)): Modified<Xml<ParametersValidify>>,
) -> StatusCode {
check_modified(&parameters)
}
pub async fn extract_xml_validified(
Validified(Xml(parameters)): Validified<Xml<ParametersValidify>>,
) -> StatusCode {
check_validified(&parameters)
}
pub async fn extract_xml_validified_by_ref(
ValidifiedByRef(Xml(parameters)): ValidifiedByRef<Xml<ParametersValidify>>,
) -> StatusCode {
check_validified(&parameters)
}
}
#[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<Toml<ParametersValidify>>,
) -> StatusCode {
check_validated(&parameters)
}
pub async fn extract_toml_modified(
Modified(Toml(parameters)): Modified<Toml<ParametersValidify>>,
) -> StatusCode {
check_modified(&parameters)
}
pub async fn extract_toml_validified(
Validified(Toml(parameters)): Validified<Toml<ParametersValidify>>,
) -> StatusCode {
check_validified(&parameters)
}
pub async fn extract_toml_validified_by_ref(
ValidifiedByRef(Toml(parameters)): ValidifiedByRef<Toml<ParametersValidify>>,
) -> StatusCode {
check_validified(&parameters)
}
}