Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions serde-querystring-axum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@ rust-version = "1.56"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
axum-core = "0.3.0"

async-trait = "0.1"
http = "0.2"
axum-core = "0.5.0"
http = "1.5"

serde = { version = "1.0.126", features = ["derive"] }
serde-querystring = { version = "0.3.0", features = ["serde"] }

[dev-dependencies]
axum = "0.6"
tokio = { version = "1.23", features = ["full"] }
tower = "0.4"
axum = "0.8"
http-body-util = "0.1"
tokio = { version = "1.53", features = ["full"] }
tower = "0.5"
33 changes: 18 additions & 15 deletions serde-querystring-axum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use std::ops::Deref;
use std::sync::Arc;

use async_trait::async_trait;
use axum_core::{
extract::FromRequestParts,
response::{IntoResponse, Response},
Expand Down Expand Up @@ -44,7 +43,7 @@ pub use serde_querystring::de::ParseMode;
///
/// let app = Router::new().route("/list_things", get(list_things));
/// # async {
/// # axum::Server::bind(&"".parse().unwrap()).serve(app.into_make_service()).await.unwrap();
/// # let _: Router = app;
/// # };
/// ```
///
Expand All @@ -63,14 +62,13 @@ pub use serde_querystring::de::ParseMode;
/// }),
/// ));
/// # async {
/// # axum::Server::bind(&"".parse().unwrap()).serve(app.into_make_service()).await.unwrap();
/// # let _: Router = app;
/// # };
/// ```
///
#[derive(Debug, Clone, Copy, Default)]
pub struct QueryString<T>(pub T);

#[async_trait]
impl<T, S> FromRequestParts<S> for QueryString<T>
where
T: DeserializeOwned,
Expand Down Expand Up @@ -118,7 +116,7 @@ impl<T> Deref for QueryString<T> {
/// }),
/// ));
/// # async {
/// # axum::Server::bind(&"".parse().unwrap()).serve(app.into_make_service()).await.unwrap();
/// # let _: Router = app;
/// # };
/// ```
///
Expand Down Expand Up @@ -185,13 +183,9 @@ impl IntoResponse for QueryStringError {
mod tests {
use std::fmt::Debug;

use axum::{
body::{Body, HttpBody},
extract::FromRequest,
routing::get,
Extension, Router,
};
use axum::{body::Body, extract::FromRequest, routing::get, Extension, Router};
use http::{Request, StatusCode};
use http_body_util::BodyExt;
use serde::Deserialize;
use tower::ServiceExt;

Expand All @@ -201,7 +195,10 @@ mod tests {
where
T: DeserializeOwned + PartialEq + Debug,
{
let req = Request::builder().uri(uri.as_ref()).body(()).unwrap();
let req = Request::builder()
.uri(uri.as_ref())
.body(Body::empty())
.unwrap();
assert_eq!(
QueryString::<T>::from_request(req, &()).await.unwrap().0,
value
Expand Down Expand Up @@ -281,7 +278,10 @@ mod tests {
let (parts, mut body) = res.into_parts();

assert_eq!(parts.status, StatusCode::OK);
assert_eq!(body.data().await.unwrap().unwrap(), "100-300")
assert_eq!(
body.frame().await.unwrap().unwrap().into_data().unwrap(),
"100-300"
)
}

#[tokio::test]
Expand Down Expand Up @@ -309,7 +309,7 @@ mod tests {

assert_eq!(parts.status, StatusCode::BAD_REQUEST);
assert_eq!(
body.data().await.unwrap().unwrap(),
body.frame().await.unwrap().unwrap().into_data().unwrap(),
"Failed to deserialize query string"
);
}
Expand Down Expand Up @@ -346,6 +346,9 @@ mod tests {
let (parts, mut body) = res.into_parts();

assert_eq!(parts.status, StatusCode::BAD_GATEWAY);
assert_eq!(body.data().await.unwrap().unwrap(), "Something went wrong");
assert_eq!(
body.frame().await.unwrap().unwrap().into_data().unwrap(),
"Something went wrong"
);
}
}
Loading