add tests

This commit is contained in:
gengteng
2023-09-28 23:46:22 +08:00
parent a35d52ee20
commit 9b28922201
3 changed files with 29 additions and 5 deletions

View File

@@ -49,7 +49,7 @@ optional = true
[dev-dependencies]
anyhow = "1.0.72"
axum = { version = "0.6.20" }
axum = { version = "0.6.20", features = ["macros"] }
tokio = { version = "1.29.1", features = ["full"] }
hyper = { version = "0.14.27", features = ["full"] }
reqwest = { version = "0.11.18", features = ["json", "multipart"] }

View File

@@ -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`.
///
@@ -224,9 +224,12 @@ pub trait HasValidate {
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> {
/// Inner type that can be validated for correctness
/// Inner type that can be validated using arguments
type ValidateArgs: ValidateArgs<'v>;
/// Get the inner value
fn get_validate_args(&self) -> &Self::ValidateArgs;

View File

@@ -1,6 +1,6 @@
use crate::tests::{ValidTest, ValidTestParameter};
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::{Form, Json, Router};
use hyper::Method;
@@ -107,6 +107,11 @@ impl HasValidate for Parameters {
}
}
#[derive(Debug, Clone, FromRef)]
struct MyState {
validation_ctx: ValidationContext<ParametersExValidationArguments>,
}
#[tokio::test]
async fn test_main() -> anyhow::Result<()> {
let router = Router::new()
@@ -117,8 +122,11 @@ async fn test_main() -> anyhow::Result<()> {
let router_ex = Router::new()
.route(route::QUERY_EX, get(extract_query_ex))
.route(route::FORM_EX, post(extract_form_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);
@@ -257,10 +265,16 @@ async fn test_main() -> anyhow::Result<()> {
.execute::<Query<Parameters>>(Method::GET, route::QUERY_EX)
.await?;
// Valid
test_executor
.execute::<Form<Parameters>>(Method::POST, route::FORM)
.await?;
// ValidEx
test_executor
.execute::<Form<Parameters>>(Method::POST, route::FORM_EX)
.await?;
// Valid
test_executor
.execute::<Json<Parameters>>(Method::POST, route::JSON)
@@ -506,6 +520,7 @@ mod route {
pub const QUERY: &str = "/query";
pub const QUERY_EX: &str = "/query_ex";
pub const FORM: &str = "/form";
pub const FORM_EX: &str = "/form_ex";
pub const JSON: &str = "/json";
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)
}
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 {
validate_again(parameters)
}