diff --git a/src/msgpack.rs b/src/msgpack.rs index db5d539..d9cfa16 100644 --- a/src/msgpack.rs +++ b/src/msgpack.rs @@ -202,8 +202,17 @@ mod tests { } fn set_error_request(builder: RequestBuilder) -> RequestBuilder { - // `Content-Type` not set, `MsgPack` should return `415 Unsupported Media Type` + #[derive(Serialize, Default)] + struct ErrorData { + error_field0: i32, + error_field1: Option, + } builder + .header(reqwest::header::CONTENT_TYPE, "application/msgpack") + .body( + rmp_serde::to_vec(&ErrorData::default()) + .expect("Failed to serialize parameters to msgpack"), + ) } fn set_invalid_request(builder: RequestBuilder) -> RequestBuilder { @@ -229,8 +238,17 @@ mod tests { } fn set_error_request(builder: RequestBuilder) -> RequestBuilder { - // `Content-Type` not set, `MsgPack` should return `415 Unsupported Media Type` + #[derive(Serialize, Default)] + struct ErrorData { + error_field0: i32, + error_field1: Option, + } builder + .header(reqwest::header::CONTENT_TYPE, "application/msgpack") + .body( + rmp_serde::to_vec(&ErrorData::default()) + .expect("Failed to serialize parameters to msgpack"), + ) } fn set_invalid_request(builder: RequestBuilder) -> RequestBuilder { diff --git a/src/validify/test.rs b/src/validify/test.rs index 6bcdcd4..ca0f8ca 100644 --- a/src/validify/test.rs +++ b/src/validify/test.rs @@ -151,10 +151,26 @@ async fn test_main() -> anyhow::Result<()> { #[cfg(feature = "extra")] let router = router .route(extra::route::CACHED, post(extra::extract_cached)) + .route( + extra::route::CACHED_MODIFIED, + post(extra::extract_cached_modified), + ) + .route( + extra::route::CACHED_VALIDIFIED_BY_REF, + post(extra::extract_cached_validified_by_ref), + ) .route( extra::route::WITH_REJECTION, post(extra::extract_with_rejection), ) + .route( + extra::route::WITH_REJECTION_MODIFIED, + post(extra::extract_with_rejection_modified), + ) + .route( + extra::route::WITH_REJECTION_VALIDIFIED_BY_REF, + post(extra::extract_with_rejection_validified_by_ref), + ) .route( extra::route::WITH_REJECTION_VALIDIFY, post(extra::extract_with_rejection_valid), @@ -176,16 +192,42 @@ async fn test_main() -> anyhow::Result<()> { ); #[cfg(feature = "extra_query")] - let router = router.route( - extra_query::route::EXTRA_QUERY, - post(extra_query::extract_extra_query), - ); + let router = router + .route( + extra_query::route::EXTRA_QUERY, + post(extra_query::extract_extra_query), + ) + .route( + extra_query::route::EXTRA_QUERY_MODIFIED, + post(extra_query::extract_extra_query_modified), + ) + .route( + extra_query::route::EXTRA_QUERY_VALIDIFIED, + post(extra_query::extract_extra_query_validified), + ) + .route( + extra_query::route::EXTRA_QUERY_VALIDIFIED_BY_REF, + post(extra_query::extract_extra_query_validified_by_ref), + ); #[cfg(feature = "extra_form")] - let router = router.route( - extra_form::route::EXTRA_FORM, - post(extra_form::extract_extra_form), - ); + let router = router + .route( + extra_form::route::EXTRA_FORM, + post(extra_form::extract_extra_form), + ) + .route( + extra_form::route::EXTRA_FORM_MODIFIED, + post(extra_form::extract_extra_form_modified), + ) + .route( + extra_form::route::EXTRA_FORM_VALIDIFED, + post(extra_form::extract_extra_form_validifed), + ) + .route( + extra_form::route::EXTRA_FORM_VALIDIFED_BY_REF, + post(extra_form::extract_extra_form_validifed_by_ref), + ); #[cfg(feature = "extra_protobuf")] let router = router @@ -203,14 +245,51 @@ async fn test_main() -> anyhow::Result<()> { ); #[cfg(feature = "yaml")] - let router = router.route(yaml::route::YAML, post(yaml::extract_yaml)); + let router = router + .route(yaml::route::YAML, post(yaml::extract_yaml)) + .route( + yaml::route::YAML_MODIFIED, + post(yaml::extract_yaml_modified), + ) + .route( + yaml::route::YAML_VALIDIFIED, + post(yaml::extract_yaml_validified), + ) + .route( + yaml::route::YAML_VALIDIFIED_BY_REF, + post(yaml::extract_yaml_validified_by_ref), + ); #[cfg(feature = "msgpack")] let router = router .route(msgpack::route::MSGPACK, post(msgpack::extract_msgpack)) + .route( + msgpack::route::MSGPACK_MODIFIED, + post(msgpack::extract_msgpack_modified), + ) + .route( + msgpack::route::MSGPACK_VALIDIFIED, + post(msgpack::extract_msgpack_validified), + ) + .route( + msgpack::route::MSGPACK_VALIDIFIED_BY_REF, + post(msgpack::extract_msgpack_validified_by_ref), + ) .route( msgpack::route::MSGPACK_RAW, post(msgpack::extract_msgpack_raw), + ) + .route( + msgpack::route::MSGPACK_RAW_MODIFIED, + post(msgpack::extract_msgpack_raw_modified), + ) + .route( + msgpack::route::MSGPACK_RAW_VALIDIFIED, + post(msgpack::extract_msgpack_raw_validified), + ) + .route( + msgpack::route::MSGPACK_RAW_VALIDIFIED_BY_REF, + post(msgpack::extract_msgpack_raw_validified_by_ref), ); let server = axum::Server::bind(&SocketAddr::from(([0u8, 0, 0, 0], 0u16))) @@ -329,8 +408,8 @@ async fn test_main() -> anyhow::Result<()> { "Invalid '{}' test failed.", path_type_name ); - #[cfg(feature = "into_json")] if should_check_json { + #[cfg(feature = "into_json")] check_json(path_type_name, invalid_path_response).await; } println!("All {} tests passed.", path_type_name); @@ -474,15 +553,47 @@ async fn test_main() -> anyhow::Result<()> { use extra::{ ParametersRejection, ValidifyWithRejectionRejection, WithRejectionValidifyRejection, }; + // Validated test_executor .execute::>(Method::POST, extra::route::CACHED) .await?; + // Modified + test_executor + .execute_modified::>( + Method::POST, + extra::route::CACHED_MODIFIED, + ) + .await?; + // ValidifiedByRef + test_executor + .execute::>( + Method::POST, + extra::route::CACHED_VALIDIFIED_BY_REF, + ) + .await?; + + // Validated test_executor .execute::>( Method::POST, extra::route::WITH_REJECTION, ) .await?; + // Modified + test_executor + .execute_modified::>( + Method::POST, + extra::route::WITH_REJECTION_MODIFIED, + ) + .await?; + // ValidifiedByRef + test_executor + .execute::>( + Method::POST, + extra::route::WITH_REJECTION_VALIDIFIED_BY_REF, + ) + .await?; + test_executor .execute::, @@ -578,8 +689,9 @@ async fn test_main() -> anyhow::Result<()> { "Invalid '{}' test failed.", extra_typed_path_type_name ); - #[cfg(feature = "into_json")] + if should_check_json { + #[cfg(feature = "into_json")] check_json( extra_typed_path_type_name, invalid_extra_typed_path_response, @@ -604,17 +716,61 @@ async fn test_main() -> anyhow::Result<()> { #[cfg(feature = "extra_query")] { use axum_extra::extract::Query; + // Validated test_executor .execute::>(Method::POST, extra_query::route::EXTRA_QUERY) .await?; + // Modified + test_executor + .execute_modified::>( + Method::POST, + extra_query::route::EXTRA_QUERY_MODIFIED, + ) + .await?; + // Validified + test_executor + .execute_validified::>( + Method::POST, + extra_query::route::EXTRA_QUERY_VALIDIFIED, + ) + .await?; + // ValidifiedByRef + test_executor + .execute::>( + Method::POST, + extra_query::route::EXTRA_QUERY_VALIDIFIED_BY_REF, + ) + .await?; } #[cfg(feature = "extra_form")] { use axum_extra::extract::Form; + // Validated test_executor .execute::>(Method::POST, extra_form::route::EXTRA_FORM) .await?; + // Modified + test_executor + .execute_modified::>( + Method::POST, + extra_form::route::EXTRA_FORM_MODIFIED, + ) + .await?; + // Validified + test_executor + .execute_validified::>( + Method::POST, + extra_form::route::EXTRA_FORM_VALIDIFED, + ) + .await?; + // ValidifiedByRef + test_executor + .execute::>( + Method::POST, + extra_form::route::EXTRA_FORM_VALIDIFED_BY_REF, + ) + .await?; } #[cfg(feature = "extra_protobuf")] @@ -646,20 +802,82 @@ async fn test_main() -> anyhow::Result<()> { #[cfg(feature = "yaml")] { use axum_yaml::Yaml; + + // Validated test_executor .execute::>(Method::POST, yaml::route::YAML) .await?; + // Modified + test_executor + .execute_modified::>(Method::POST, yaml::route::YAML_MODIFIED) + .await?; + // Validified + test_executor + .execute_validified::>( + Method::POST, + yaml::route::YAML_VALIDIFIED, + ) + .await?; + // ValidifiedByRef + test_executor + .execute::>(Method::POST, yaml::route::YAML_VALIDIFIED_BY_REF) + .await?; } #[cfg(feature = "msgpack")] { use axum_msgpack::{MsgPack, MsgPackRaw}; + // Validated test_executor .execute::>(Method::POST, msgpack::route::MSGPACK) .await?; + // Modified + test_executor + .execute_modified::>( + Method::POST, + msgpack::route::MSGPACK_MODIFIED, + ) + .await?; + // Validified + test_executor + .execute_validified::>( + Method::POST, + msgpack::route::MSGPACK_VALIDIFIED, + ) + .await?; + // ValidifiedByRef + test_executor + .execute::>( + Method::POST, + msgpack::route::MSGPACK_VALIDIFIED_BY_REF, + ) + .await?; + + // test_executor .execute::>(Method::POST, msgpack::route::MSGPACK_RAW) .await?; + // + test_executor + .execute_modified::>( + Method::POST, + msgpack::route::MSGPACK_RAW_MODIFIED, + ) + .await?; + // + test_executor + .execute_validified::>( + Method::POST, + msgpack::route::MSGPACK_RAW_VALIDIFIED, + ) + .await?; + // + test_executor + .execute::>( + Method::POST, + msgpack::route::MSGPACK_RAW_VALIDIFIED_BY_REF, + ) + .await?; } drop(server_guard); @@ -776,9 +994,11 @@ impl TestExecutor { type_name, invalid_response.text().await? ); - #[cfg(feature = "into_json")] - if should_check_json && T::JSON_SERIALIZABLE { - check_json(type_name, invalid_response).await; + if should_check_json { + #[cfg(feature = "into_json")] + if T::JSON_SERIALIZABLE { + check_json(type_name, invalid_response).await; + } } println!("All '{}' tests passed.", type_name); @@ -1103,9 +1323,9 @@ mod typed_multipart { #[cfg(feature = "extra")] mod extra { - use super::{check_validated, ParametersValidify}; + use super::{check_modified, check_validated, check_validified, ParametersValidify}; use crate::tests::{Rejection, ValidTest, ValidTestParameter}; - use crate::{Validated, ValidifyRejection}; + use crate::{Modified, Validated, ValidifiedByRef, ValidifyRejection}; use axum::extract::FromRequestParts; use axum::http::request::Parts; use axum::http::StatusCode; @@ -1115,7 +1335,11 @@ mod extra { pub mod route { pub const CACHED: &str = "/cached"; + pub const CACHED_MODIFIED: &str = "/cached_modified"; + pub const CACHED_VALIDIFIED_BY_REF: &str = "/cached_validified_by_ref"; pub const WITH_REJECTION: &str = "/with_rejection"; + 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 PARAMETERS_HEADER: &str = "parameters-header"; @@ -1217,6 +1441,18 @@ mod extra { check_validated(¶meters) } + pub async fn extract_cached_modified( + Modified(Cached(parameters)): Modified>, + ) -> StatusCode { + check_modified(¶meters) + } + + pub async fn extract_cached_validified_by_ref( + ValidifiedByRef(Cached(parameters)): ValidifiedByRef>, + ) -> StatusCode { + check_validified(¶meters) + } + pub async fn extract_with_rejection( Validated(WithRejection(parameters, _)): Validated< WithRejection, @@ -1225,6 +1461,22 @@ mod extra { check_validated(¶meters) } + pub async fn extract_with_rejection_modified( + Modified(WithRejection(parameters, _)): Modified< + WithRejection, + >, + ) -> StatusCode { + check_modified(¶meters) + } + + pub async fn extract_with_rejection_validified_by_ref( + ValidifiedByRef(WithRejection(parameters, _)): ValidifiedByRef< + WithRejection, + >, + ) -> StatusCode { + check_validified(¶meters) + } + pub struct WithRejectionValidifyRejection { inner: ValidifyRejection, } @@ -1351,13 +1603,16 @@ mod extra_typed_path { #[cfg(feature = "extra_query")] mod extra_query { - use super::{check_validated, ParametersValidify}; - use crate::Validated; + use super::{check_modified, check_validated, check_validified, ParametersValidify}; + use crate::{Modified, Validated, Validified, ValidifiedByRef}; use axum::http::StatusCode; use axum_extra::extract::Query; pub mod route { pub const EXTRA_QUERY: &str = "/extra_query"; + pub const EXTRA_QUERY_MODIFIED: &str = "/extra_query_modified"; + pub const EXTRA_QUERY_VALIDIFIED: &str = "/extra_query_validified"; + pub const EXTRA_QUERY_VALIDIFIED_BY_REF: &str = "/extra_query_validified_by_ref"; } pub async fn extract_extra_query( @@ -1365,17 +1620,38 @@ mod extra_query { ) -> StatusCode { check_validated(¶meters) } + + pub async fn extract_extra_query_modified( + Modified(Query(parameters)): Modified>, + ) -> StatusCode { + check_modified(¶meters) + } + + pub async fn extract_extra_query_validified( + Validified(Query(parameters)): Validified>, + ) -> StatusCode { + check_validified(¶meters) + } + + pub async fn extract_extra_query_validified_by_ref( + ValidifiedByRef(Query(parameters)): ValidifiedByRef>, + ) -> StatusCode { + check_validified(¶meters) + } } #[cfg(feature = "extra_form")] mod extra_form { - use super::{check_validated, ParametersValidify}; - use crate::Validated; + use super::{check_modified, check_validated, check_validified, ParametersValidify}; + use crate::{Modified, Validated, Validified, ValidifiedByRef}; use axum::http::StatusCode; use axum_extra::extract::Form; pub mod route { pub const EXTRA_FORM: &str = "/extra_form"; + pub const EXTRA_FORM_MODIFIED: &str = "/extra_form_modified"; + pub const EXTRA_FORM_VALIDIFED: &str = "/extra_form_validifed"; + pub const EXTRA_FORM_VALIDIFED_BY_REF: &str = "/extra_form_validified_by_ref"; } pub async fn extract_extra_form( @@ -1383,6 +1659,24 @@ mod extra_form { ) -> StatusCode { check_validated(¶meters) } + + pub async fn extract_extra_form_modified( + Modified(Form(parameters)): Modified>, + ) -> StatusCode { + check_modified(¶meters) + } + + pub async fn extract_extra_form_validifed( + Validified(Form(parameters)): Validified>, + ) -> StatusCode { + check_validified(¶meters) + } + + pub async fn extract_extra_form_validifed_by_ref( + ValidifiedByRef(Form(parameters)): ValidifiedByRef>, + ) -> StatusCode { + check_validified(¶meters) + } } #[cfg(feature = "extra_protobuf")] @@ -1419,13 +1713,16 @@ mod extra_protobuf { #[cfg(feature = "yaml")] mod yaml { - use super::{check_validated, ParametersValidify}; - use crate::Validated; + use super::{check_modified, check_validated, check_validified, ParametersValidify}; + use crate::{Modified, Validated, Validified, ValidifiedByRef}; use axum::http::StatusCode; use axum_yaml::Yaml; pub mod route { pub const YAML: &str = "/yaml"; + pub const YAML_MODIFIED: &str = "/yaml_modified"; + pub const YAML_VALIDIFIED: &str = "/yaml_validified"; + pub const YAML_VALIDIFIED_BY_REF: &str = "/yaml_validified_by_ref"; } pub async fn extract_yaml( @@ -1433,18 +1730,42 @@ mod yaml { ) -> StatusCode { check_validated(¶meters) } + + pub async fn extract_yaml_modified( + Modified(Yaml(parameters)): Modified>, + ) -> StatusCode { + check_modified(¶meters) + } + + pub async fn extract_yaml_validified( + Validified(Yaml(parameters)): Validified>, + ) -> StatusCode { + check_validified(¶meters) + } + + pub async fn extract_yaml_validified_by_ref( + ValidifiedByRef(Yaml(parameters)): ValidifiedByRef>, + ) -> StatusCode { + check_validified(¶meters) + } } #[cfg(feature = "msgpack")] mod msgpack { - use super::{check_validated, ParametersValidify}; - use crate::Validated; + use super::{check_modified, check_validated, check_validified, ParametersValidify}; + use crate::{Modified, Validated, Validified, ValidifiedByRef}; use axum::http::StatusCode; use axum_msgpack::{MsgPack, MsgPackRaw}; pub mod route { pub const MSGPACK: &str = "/msgpack"; + pub const MSGPACK_MODIFIED: &str = "/msgpack_modified"; + pub const MSGPACK_VALIDIFIED: &str = "/msgpack_validified"; + pub const MSGPACK_VALIDIFIED_BY_REF: &str = "/msgpack_validified_by_ref"; pub const MSGPACK_RAW: &str = "/msgpack_raw"; + pub const MSGPACK_RAW_MODIFIED: &str = "/msgpack_raw_modified"; + pub const MSGPACK_RAW_VALIDIFIED: &str = "/msgpack_raw_validified"; + pub const MSGPACK_RAW_VALIDIFIED_BY_REF: &str = "/msgpack_raw_validified_by_ref"; } pub async fn extract_msgpack( @@ -1452,9 +1773,45 @@ mod msgpack { ) -> StatusCode { check_validated(¶meters) } + + pub async fn extract_msgpack_modified( + Modified(MsgPack(parameters)): Modified>, + ) -> StatusCode { + check_modified(¶meters) + } + + pub async fn extract_msgpack_validified( + Validified(MsgPack(parameters)): Validified>, + ) -> StatusCode { + check_validified(¶meters) + } + + pub async fn extract_msgpack_validified_by_ref( + ValidifiedByRef(MsgPack(parameters)): ValidifiedByRef>, + ) -> StatusCode { + check_validified(¶meters) + } pub async fn extract_msgpack_raw( Validated(MsgPackRaw(parameters)): Validated>, ) -> StatusCode { check_validated(¶meters) } + + pub async fn extract_msgpack_raw_modified( + Modified(MsgPackRaw(parameters)): Modified>, + ) -> StatusCode { + check_modified(¶meters) + } + + pub async fn extract_msgpack_raw_validified( + Validified(MsgPackRaw(parameters)): Validified>, + ) -> StatusCode { + check_validified(¶meters) + } + + pub async fn extract_msgpack_raw_validified_by_ref( + ValidifiedByRef(MsgPackRaw(parameters)): ValidifiedByRef>, + ) -> StatusCode { + check_validified(¶meters) + } } diff --git a/src/yaml.rs b/src/yaml.rs index d5c20b7..d3a079c 100644 --- a/src/yaml.rs +++ b/src/yaml.rs @@ -140,7 +140,7 @@ mod tests { use serde::Serialize; impl ValidTest for Yaml { - const ERROR_STATUS_CODE: StatusCode = StatusCode::UNSUPPORTED_MEDIA_TYPE; + const ERROR_STATUS_CODE: StatusCode = StatusCode::UNPROCESSABLE_ENTITY; fn set_valid_request(builder: RequestBuilder) -> RequestBuilder { builder @@ -152,8 +152,16 @@ mod tests { } fn set_error_request(builder: RequestBuilder) -> RequestBuilder { - // `Content-Type` not set, `Yaml` should return `415 Unsupported Media Type` + #[derive(Serialize, Default)] + struct ErrorData { + error_field: i32, + } builder + .header(reqwest::header::CONTENT_TYPE, "application/yaml") + .body( + serde_yaml::to_string(&ErrorData::default()) + .expect("Failed to serialize parameters to yaml"), + ) } fn set_invalid_request(builder: RequestBuilder) -> RequestBuilder {