mirror of
https://github.com/tokio-rs/axum.git
synced 2026-08-24 00:00:16 +02:00
Fix Query and Form extractors giving bad request error when query string is empty (#117)
Co-Authored-By: David Pedersen <[email protected]> Co-authored-by: David Pedersen <[email protected]>
This commit is contained in:
+51
-1
@@ -51,7 +51,7 @@ where
|
||||
.uri()
|
||||
.ok_or(UriAlreadyExtracted)?
|
||||
.query()
|
||||
.ok_or(QueryStringMissing)?;
|
||||
.unwrap_or_default();
|
||||
let value = serde_urlencoded::from_str(query)
|
||||
.map_err(FailedToDeserializeQueryString::new::<T, _>)?;
|
||||
Ok(Query(value))
|
||||
@@ -65,3 +65,53 @@ impl<T> Deref for Query<T> {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::extract::RequestParts;
|
||||
use http::Request;
|
||||
use serde::Deserialize;
|
||||
use std::fmt::Debug;
|
||||
|
||||
async fn check<T: DeserializeOwned + PartialEq + Debug>(uri: impl AsRef<str>, value: T) {
|
||||
let mut req = RequestParts::new(Request::builder().uri(uri.as_ref()).body(()).unwrap());
|
||||
assert_eq!(Query::<T>::from_request(&mut req).await.unwrap().0, value);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_query() {
|
||||
#[derive(Debug, PartialEq, Deserialize)]
|
||||
struct Pagination {
|
||||
size: Option<u64>,
|
||||
page: Option<u64>,
|
||||
}
|
||||
|
||||
check(
|
||||
"http://example.com/test",
|
||||
Pagination {
|
||||
size: None,
|
||||
page: None,
|
||||
},
|
||||
)
|
||||
.await;
|
||||
|
||||
check(
|
||||
"http://example.com/test?size=10",
|
||||
Pagination {
|
||||
size: Some(10),
|
||||
page: None,
|
||||
},
|
||||
)
|
||||
.await;
|
||||
|
||||
check(
|
||||
"http://example.com/test?size=10&page=20",
|
||||
Pagination {
|
||||
size: Some(10),
|
||||
page: Some(20),
|
||||
},
|
||||
)
|
||||
.await;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user