add tests
This commit is contained in:
@@ -49,7 +49,7 @@ optional = true
|
|||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
anyhow = "1.0.72"
|
anyhow = "1.0.72"
|
||||||
axum = { version = "0.6.20" }
|
axum = { version = "0.6.20", features = ["macros"] }
|
||||||
tokio = { version = "1.29.1", features = ["full"] }
|
tokio = { version = "1.29.1", features = ["full"] }
|
||||||
hyper = { version = "0.14.27", features = ["full"] }
|
hyper = { version = "0.14.27", features = ["full"] }
|
||||||
reqwest = { version = "0.11.18", features = ["json", "multipart"] }
|
reqwest = { version = "0.11.18", features = ["json", "multipart"] }
|
||||||
|
|||||||
@@ -213,7 +213,7 @@ impl<E: IntoResponse> IntoResponse for ValidRejection<E> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Trait for types that can provide a reference that can be validated for correctness.
|
/// Trait for types that can supply a reference that can be validated.
|
||||||
///
|
///
|
||||||
/// Extractor types `T` that implement this trait can be used with `Valid`.
|
/// Extractor types `T` that implement this trait can be used with `Valid`.
|
||||||
///
|
///
|
||||||
@@ -224,9 +224,12 @@ pub trait HasValidate {
|
|||||||
fn get_validate(&self) -> &Self::Validate;
|
fn get_validate(&self) -> &Self::Validate;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Trait for types that can supply a reference that can be validated using arguments.
|
||||||
|
///
|
||||||
|
/// Extractor types `T` that implement this trait can be used with `ValidEx`.
|
||||||
///
|
///
|
||||||
pub trait HasValidateArgs<'v> {
|
pub trait HasValidateArgs<'v> {
|
||||||
/// Inner type that can be validated for correctness
|
/// Inner type that can be validated using arguments
|
||||||
type ValidateArgs: ValidateArgs<'v>;
|
type ValidateArgs: ValidateArgs<'v>;
|
||||||
/// Get the inner value
|
/// Get the inner value
|
||||||
fn get_validate_args(&self) -> &Self::ValidateArgs;
|
fn get_validate_args(&self) -> &Self::ValidateArgs;
|
||||||
|
|||||||
25
src/test.rs
25
src/test.rs
@@ -1,6 +1,6 @@
|
|||||||
use crate::tests::{ValidTest, ValidTestParameter};
|
use crate::tests::{ValidTest, ValidTestParameter};
|
||||||
use crate::{Arguments, HasValidate, Valid, ValidEx, ValidationContext, VALIDATION_ERROR_STATUS};
|
use crate::{Arguments, HasValidate, Valid, ValidEx, ValidationContext, VALIDATION_ERROR_STATUS};
|
||||||
use axum::extract::{Path, Query};
|
use axum::extract::{FromRef, Path, Query};
|
||||||
use axum::routing::{get, post};
|
use axum::routing::{get, post};
|
||||||
use axum::{Form, Json, Router};
|
use axum::{Form, Json, Router};
|
||||||
use hyper::Method;
|
use hyper::Method;
|
||||||
@@ -107,6 +107,11 @@ impl HasValidate for Parameters {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, FromRef)]
|
||||||
|
struct MyState {
|
||||||
|
validation_ctx: ValidationContext<ParametersExValidationArguments>,
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_main() -> anyhow::Result<()> {
|
async fn test_main() -> anyhow::Result<()> {
|
||||||
let router = Router::new()
|
let router = Router::new()
|
||||||
@@ -117,8 +122,11 @@ async fn test_main() -> anyhow::Result<()> {
|
|||||||
|
|
||||||
let router_ex = Router::new()
|
let router_ex = Router::new()
|
||||||
.route(route::QUERY_EX, get(extract_query_ex))
|
.route(route::QUERY_EX, get(extract_query_ex))
|
||||||
|
.route(route::FORM_EX, post(extract_form_ex))
|
||||||
.route(route::JSON_EX, post(extract_json_ex))
|
.route(route::JSON_EX, post(extract_json_ex))
|
||||||
.with_state(ValidationContext::<ParametersExValidationArguments>::default());
|
.with_state(MyState {
|
||||||
|
validation_ctx: ValidationContext::<ParametersExValidationArguments>::default(),
|
||||||
|
});
|
||||||
|
|
||||||
let router = router.merge(router_ex);
|
let router = router.merge(router_ex);
|
||||||
|
|
||||||
@@ -257,10 +265,16 @@ async fn test_main() -> anyhow::Result<()> {
|
|||||||
.execute::<Query<Parameters>>(Method::GET, route::QUERY_EX)
|
.execute::<Query<Parameters>>(Method::GET, route::QUERY_EX)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
// Valid
|
||||||
test_executor
|
test_executor
|
||||||
.execute::<Form<Parameters>>(Method::POST, route::FORM)
|
.execute::<Form<Parameters>>(Method::POST, route::FORM)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
// ValidEx
|
||||||
|
test_executor
|
||||||
|
.execute::<Form<Parameters>>(Method::POST, route::FORM_EX)
|
||||||
|
.await?;
|
||||||
|
|
||||||
// Valid
|
// Valid
|
||||||
test_executor
|
test_executor
|
||||||
.execute::<Json<Parameters>>(Method::POST, route::JSON)
|
.execute::<Json<Parameters>>(Method::POST, route::JSON)
|
||||||
@@ -506,6 +520,7 @@ mod route {
|
|||||||
pub const QUERY: &str = "/query";
|
pub const QUERY: &str = "/query";
|
||||||
pub const QUERY_EX: &str = "/query_ex";
|
pub const QUERY_EX: &str = "/query_ex";
|
||||||
pub const FORM: &str = "/form";
|
pub const FORM: &str = "/form";
|
||||||
|
pub const FORM_EX: &str = "/form_ex";
|
||||||
pub const JSON: &str = "/json";
|
pub const JSON: &str = "/json";
|
||||||
pub const JSON_EX: &str = "/json_ex";
|
pub const JSON_EX: &str = "/json_ex";
|
||||||
}
|
}
|
||||||
@@ -528,6 +543,12 @@ async fn extract_form(Valid(Form(parameters)): Valid<Form<Parameters>>) -> Statu
|
|||||||
validate_again(parameters)
|
validate_again(parameters)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn extract_form_ex(
|
||||||
|
ValidEx(Form(parameters), args): ValidEx<Form<ParametersEx>, ParametersExValidationArguments>,
|
||||||
|
) -> StatusCode {
|
||||||
|
validate_again_ex(parameters, args.get())
|
||||||
|
}
|
||||||
|
|
||||||
async fn extract_json(Valid(Json(parameters)): Valid<Json<Parameters>>) -> StatusCode {
|
async fn extract_json(Valid(Json(parameters)): Valid<Json<Parameters>>) -> StatusCode {
|
||||||
validate_again(parameters)
|
validate_again(parameters)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user