add tests for validify extractors
This commit is contained in:
@@ -464,6 +464,7 @@ This project is licensed under the MIT License.
|
|||||||
* [axum](https://crates.io/crates/axum)
|
* [axum](https://crates.io/crates/axum)
|
||||||
* [validator](https://crates.io/crates/validator)
|
* [validator](https://crates.io/crates/validator)
|
||||||
* [garde](https://crates.io/crates/garde)
|
* [garde](https://crates.io/crates/garde)
|
||||||
|
* [validify](https://crates.io/crates/validify)
|
||||||
* [serde](https://crates.io/crates/serde)
|
* [serde](https://crates.io/crates/serde)
|
||||||
* [axum-extra](https://crates.io/crates/axum-extra)
|
* [axum-extra](https://crates.io/crates/axum-extra)
|
||||||
* [axum-yaml](https://crates.io/crates/axum-yaml)
|
* [axum-yaml](https://crates.io/crates/axum-yaml)
|
||||||
|
|||||||
52
src/extra.rs
52
src/extra.rs
@@ -504,7 +504,7 @@ mod tests {
|
|||||||
#[cfg(feature = "validator")]
|
#[cfg(feature = "validator")]
|
||||||
use crate::Valid;
|
use crate::Valid;
|
||||||
#[cfg(feature = "validify")]
|
#[cfg(feature = "validify")]
|
||||||
use crate::Validated;
|
use crate::{Modified, Validated, ValidifiedByRef};
|
||||||
use axum::http::StatusCode;
|
use axum::http::StatusCode;
|
||||||
use axum_extra::extract::{Cached, WithRejection};
|
use axum_extra::extract::{Cached, WithRejection};
|
||||||
use reqwest::RequestBuilder;
|
use reqwest::RequestBuilder;
|
||||||
@@ -617,4 +617,54 @@ mod tests {
|
|||||||
T::set_invalid_request(builder)
|
T::set_invalid_request(builder)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "validify")]
|
||||||
|
impl<T: ValidTest, R> ValidTest for WithRejection<Modified<T>, 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<T: ValidTest, R> ValidTest for WithRejection<ValidifiedByRef<T>, 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -173,7 +173,15 @@ async fn test_main() -> anyhow::Result<()> {
|
|||||||
)
|
)
|
||||||
.route(
|
.route(
|
||||||
extra::route::WITH_REJECTION_VALIDIFY,
|
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")]
|
#[cfg(feature = "extra_typed_path")]
|
||||||
@@ -594,12 +602,30 @@ async fn test_main() -> anyhow::Result<()> {
|
|||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
// Validated
|
||||||
test_executor
|
test_executor
|
||||||
.execute::<WithRejection<
|
.execute::<WithRejection<
|
||||||
Validated<ParametersValidify>,
|
Validated<ParametersValidify>,
|
||||||
WithRejectionValidifyRejection<ParametersRejection>,
|
WithRejectionValidifyRejection<ParametersRejection>,
|
||||||
>>(Method::POST, extra::route::WITH_REJECTION_VALIDIFY)
|
>>(Method::POST, extra::route::WITH_REJECTION_VALIDIFY)
|
||||||
.await?;
|
.await?;
|
||||||
|
// Modified
|
||||||
|
test_executor
|
||||||
|
.execute_modified::<WithRejection<
|
||||||
|
Modified<ParametersValidify>,
|
||||||
|
ValidifyWithRejectionRejection,
|
||||||
|
>>(Method::POST, extra::route::WITH_REJECTION_VALIDIFY_MODIFIED)
|
||||||
|
.await?;
|
||||||
|
// ValidifiedByRef
|
||||||
|
test_executor
|
||||||
|
.execute::<WithRejection<
|
||||||
|
ValidifiedByRef<ParametersValidify>,
|
||||||
|
WithRejectionValidifyRejection<ParametersRejection>,
|
||||||
|
>>(
|
||||||
|
Method::POST,
|
||||||
|
extra::route::WITH_REJECTION_VALIDIFY_VALIDIFIED_BY_REF,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "extra_typed_path")]
|
#[cfg(feature = "extra_typed_path")]
|
||||||
@@ -1325,7 +1351,7 @@ mod typed_multipart {
|
|||||||
mod extra {
|
mod extra {
|
||||||
use super::{check_modified, check_validated, check_validified, ParametersValidify};
|
use super::{check_modified, check_validated, check_validified, ParametersValidify};
|
||||||
use crate::tests::{Rejection, ValidTest, ValidTestParameter};
|
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::extract::FromRequestParts;
|
||||||
use axum::http::request::Parts;
|
use axum::http::request::Parts;
|
||||||
use axum::http::StatusCode;
|
use axum::http::StatusCode;
|
||||||
@@ -1341,6 +1367,9 @@ mod extra {
|
|||||||
pub const WITH_REJECTION_MODIFIED: &str = "/with_rejection_modified";
|
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_VALIDIFIED_BY_REF: &str = "/with_rejection_validified_by_ref";
|
||||||
pub const WITH_REJECTION_VALIDIFY: &str = "/with_rejection_validify";
|
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 PARAMETERS_HEADER: &str = "parameters-header";
|
||||||
pub const CACHED_REJECTION_STATUS: StatusCode = StatusCode::FORBIDDEN;
|
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 {
|
pub struct ValidifyWithRejectionRejection {
|
||||||
inner: ParametersRejection,
|
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<
|
WithRejection(Validated(parameters), _): WithRejection<
|
||||||
Validated<ParametersValidify>,
|
Validated<ParametersValidify>,
|
||||||
WithRejectionValidifyRejection<ParametersRejection>,
|
WithRejectionValidifyRejection<ParametersRejection>,
|
||||||
@@ -1503,6 +1540,24 @@ mod extra {
|
|||||||
) -> StatusCode {
|
) -> StatusCode {
|
||||||
check_validated(¶meters)
|
check_validated(¶meters)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn extract_with_rejection_validifiy_modified(
|
||||||
|
WithRejection(Modified(parameters), _): WithRejection<
|
||||||
|
Modified<ParametersValidify>,
|
||||||
|
ValidifyWithRejectionRejection,
|
||||||
|
>,
|
||||||
|
) -> StatusCode {
|
||||||
|
check_modified(¶meters)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn extract_with_rejection_validifiy_validified_by_ref(
|
||||||
|
WithRejection(ValidifiedByRef(parameters), _): WithRejection<
|
||||||
|
ValidifiedByRef<ParametersValidify>,
|
||||||
|
WithRejectionValidifyRejection<ParametersRejection>,
|
||||||
|
>,
|
||||||
|
) -> StatusCode {
|
||||||
|
check_validified(¶meters)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "extra_typed_path")]
|
#[cfg(feature = "extra_typed_path")]
|
||||||
|
|||||||
Reference in New Issue
Block a user