add test for Form, Query from axum-extra
This commit is contained in:
@@ -59,10 +59,9 @@ When validation errors occur, the extractor will automatically return 400 with v
|
|||||||
| typed_header | Enables support for `TypedHeader` | ❌ | ✅ |
|
| typed_header | Enables support for `TypedHeader` | ❌ | ✅ |
|
||||||
| msgpack | Enables support for `MsgPack` and `MsgPackRaw` from `axum-msgpack` | ❌ | ❌ |
|
| msgpack | Enables support for `MsgPack` and `MsgPackRaw` from `axum-msgpack` | ❌ | ❌ |
|
||||||
| yaml | Enables support for `Yaml` from `axum-yaml` | ❌ | ❌ |
|
| yaml | Enables support for `Yaml` from `axum-yaml` | ❌ | ❌ |
|
||||||
| extra_protobuf | Enables support for `Protobuf` from `axum-extra` | ❌ | ❌ |
|
|
||||||
| extra | Enables support for `Cached`, `WithRejection` from `axum-extra` | ❌ | ✅ |
|
| extra | Enables support for `Cached`, `WithRejection` from `axum-extra` | ❌ | ✅ |
|
||||||
| extra_query | Enables support for `Query` from `axum-extra` | ❌ | ❌ |
|
| extra_query | Enables support for `Query` from `axum-extra` | ❌ | ✅ |
|
||||||
| extra_form | Enables support for `Form` from `axum-extra` | ❌ | ❌ |
|
| extra_form | Enables support for `Form` from `axum-extra` | ❌ | ✅ |
|
||||||
| extra_protobuf | Enables support for `Protobuf` from `axum-extra` | ❌ | ❌ |
|
| extra_protobuf | Enables support for `Protobuf` from `axum-extra` | ❌ | ❌ |
|
||||||
| extra_all | Enables support for all extractors above from `axum-extra` | ❌ | 🚧 |
|
| extra_all | Enables support for all extractors above from `axum-extra` | ❌ | 🚧 |
|
||||||
| all_types | Enables support for all extractors above | ❌ | 🚧 |
|
| all_types | Enables support for all extractors above | ❌ | 🚧 |
|
||||||
|
|||||||
@@ -11,3 +11,26 @@ impl<T: Validate> HasValidate for Form<T> {
|
|||||||
&self.0
|
&self.0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use crate::tests::{ValidTest, ValidTestParameter};
|
||||||
|
use axum_extra::extract::Form;
|
||||||
|
use reqwest::{RequestBuilder, StatusCode};
|
||||||
|
|
||||||
|
impl<T: ValidTestParameter> ValidTest for Form<T> {
|
||||||
|
const ERROR_STATUS_CODE: StatusCode = StatusCode::BAD_REQUEST;
|
||||||
|
|
||||||
|
fn set_valid_request(builder: RequestBuilder) -> RequestBuilder {
|
||||||
|
builder.form(T::valid())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_error_request(builder: RequestBuilder) -> RequestBuilder {
|
||||||
|
builder.form(T::error())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_invalid_request(builder: RequestBuilder) -> RequestBuilder {
|
||||||
|
builder.form(T::invalid())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -11,3 +11,27 @@ impl<T: Validate> HasValidate for Query<T> {
|
|||||||
&self.0
|
&self.0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use crate::tests::{ValidTest, ValidTestParameter};
|
||||||
|
use axum::http::StatusCode;
|
||||||
|
use axum_extra::extract::Query;
|
||||||
|
use reqwest::RequestBuilder;
|
||||||
|
|
||||||
|
impl<T: ValidTestParameter> ValidTest for Query<T> {
|
||||||
|
const ERROR_STATUS_CODE: StatusCode = StatusCode::BAD_REQUEST;
|
||||||
|
|
||||||
|
fn set_valid_request(builder: RequestBuilder) -> RequestBuilder {
|
||||||
|
builder.query(&T::valid())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_error_request(builder: RequestBuilder) -> RequestBuilder {
|
||||||
|
builder.query(T::error())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_invalid_request(builder: RequestBuilder) -> RequestBuilder {
|
||||||
|
builder.query(&T::invalid())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
82
src/test.rs
82
src/test.rs
@@ -59,12 +59,24 @@ async fn test_main() -> anyhow::Result<()> {
|
|||||||
|
|
||||||
#[cfg(feature = "extra")]
|
#[cfg(feature = "extra")]
|
||||||
let router = router
|
let router = router
|
||||||
.route(route::extra::CACHED, post(extra::extract_cached))
|
.route(extra::route::CACHED, post(extra::extract_cached))
|
||||||
.route(
|
.route(
|
||||||
route::extra::WITH_REJECTION,
|
extra::route::WITH_REJECTION,
|
||||||
post(extra::extract_with_rejection),
|
post(extra::extract_with_rejection),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
#[cfg(feature = "extra_query")]
|
||||||
|
let router = router.route(
|
||||||
|
extra_query::route::EXTRA_QUERY,
|
||||||
|
post(extra_query::extract_extra_query),
|
||||||
|
);
|
||||||
|
|
||||||
|
#[cfg(feature = "extra_form")]
|
||||||
|
let router = router.route(
|
||||||
|
extra_form::route::EXTRA_FORM,
|
||||||
|
post(extra_form::extract_extra_form),
|
||||||
|
);
|
||||||
|
|
||||||
let server = axum::Server::bind(&SocketAddr::from(([0u8, 0, 0, 0], 0u16)))
|
let server = axum::Server::bind(&SocketAddr::from(([0u8, 0, 0, 0], 0u16)))
|
||||||
.serve(router.into_make_service());
|
.serve(router.into_make_service());
|
||||||
let server_addr = server.local_addr();
|
let server_addr = server.local_addr();
|
||||||
@@ -134,16 +146,32 @@ async fn test_main() -> anyhow::Result<()> {
|
|||||||
use axum_extra::extract::{Cached, WithRejection};
|
use axum_extra::extract::{Cached, WithRejection};
|
||||||
use extra::TestRejection;
|
use extra::TestRejection;
|
||||||
test_executor
|
test_executor
|
||||||
.execute::<Cached<Parameters>>(Method::POST, route::extra::CACHED)
|
.execute::<Cached<Parameters>>(Method::POST, extra::route::CACHED)
|
||||||
.await?;
|
.await?;
|
||||||
test_executor
|
test_executor
|
||||||
.execute::<WithRejection<Parameters, TestRejection>>(
|
.execute::<WithRejection<Parameters, TestRejection>>(
|
||||||
Method::POST,
|
Method::POST,
|
||||||
route::extra::WITH_REJECTION,
|
extra::route::WITH_REJECTION,
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "extra_query")]
|
||||||
|
{
|
||||||
|
use axum_extra::extract::Query;
|
||||||
|
test_executor
|
||||||
|
.execute::<Query<Parameters>>(Method::POST, extra_query::route::EXTRA_QUERY)
|
||||||
|
.await?;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "extra_form")]
|
||||||
|
{
|
||||||
|
use axum_extra::extract::Form;
|
||||||
|
test_executor
|
||||||
|
.execute::<Form<Parameters>>(Method::POST, extra_form::route::EXTRA_FORM)
|
||||||
|
.await?;
|
||||||
|
}
|
||||||
|
|
||||||
drop(server_guard);
|
drop(server_guard);
|
||||||
server_handle.await??;
|
server_handle.await??;
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -211,12 +239,6 @@ mod route {
|
|||||||
pub const QUERY: &str = "/query";
|
pub const QUERY: &str = "/query";
|
||||||
pub const FORM: &str = "/form";
|
pub const FORM: &str = "/form";
|
||||||
pub const JSON: &str = "/json";
|
pub const JSON: &str = "/json";
|
||||||
|
|
||||||
#[cfg(feature = "extra")]
|
|
||||||
pub mod extra {
|
|
||||||
pub const CACHED: &str = "/cached";
|
|
||||||
pub const WITH_REJECTION: &str = "/with_rejection";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn extract_path(Valid(Path(parameters)): Valid<Path<Parameters>>) -> StatusCode {
|
async fn extract_path(Valid(Path(parameters)): Valid<Path<Parameters>>) -> StatusCode {
|
||||||
@@ -327,6 +349,10 @@ mod extra {
|
|||||||
use axum_extra::extract::{Cached, WithRejection};
|
use axum_extra::extract::{Cached, WithRejection};
|
||||||
use reqwest::RequestBuilder;
|
use reqwest::RequestBuilder;
|
||||||
|
|
||||||
|
pub mod route {
|
||||||
|
pub const CACHED: &str = "/cached";
|
||||||
|
pub const WITH_REJECTION: &str = "/with_rejection";
|
||||||
|
}
|
||||||
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;
|
||||||
|
|
||||||
@@ -428,3 +454,39 @@ mod extra {
|
|||||||
validate_again(parameters)
|
validate_again(parameters)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "extra_query")]
|
||||||
|
mod extra_query {
|
||||||
|
use crate::test::{validate_again, Parameters};
|
||||||
|
use crate::Valid;
|
||||||
|
use axum::http::StatusCode;
|
||||||
|
use axum_extra::extract::Query;
|
||||||
|
|
||||||
|
pub mod route {
|
||||||
|
pub const EXTRA_QUERY: &str = "/extra_query";
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn extract_extra_query(
|
||||||
|
Valid(Query(parameters)): Valid<Query<Parameters>>,
|
||||||
|
) -> StatusCode {
|
||||||
|
validate_again(parameters)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "extra_form")]
|
||||||
|
pub mod extra_form {
|
||||||
|
use crate::test::{validate_again, Parameters};
|
||||||
|
use crate::Valid;
|
||||||
|
use axum::http::StatusCode;
|
||||||
|
use axum_extra::extract::Form;
|
||||||
|
|
||||||
|
pub mod route {
|
||||||
|
pub const EXTRA_FORM: &str = "/extra_form";
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn extract_extra_form(
|
||||||
|
Valid(Form(parameters)): Valid<Form<Parameters>>,
|
||||||
|
) -> StatusCode {
|
||||||
|
validate_again(parameters)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user