1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2025-04-11 04:14:06 +00:00

Merge branch 'master' into rustls0.16

This commit is contained in:
Sven-Hendrik Haase 2019-10-27 09:30:48 +01:00 committed by GitHub
commit dd865aa49e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 0 deletions

View file

@ -1,5 +1,8 @@
# Changes
## [0.2.8] - 2019-10-24
* Add support for setting query from Serialize type for client request.
## [0.2.7] - 2019-09-25

View file

@ -382,6 +382,27 @@ impl ClientRequest {
}
}
/// Sets the query part of the request
pub fn query<T: Serialize>(
mut self,
query: &T,
) -> Result<Self, serde_urlencoded::ser::Error> {
let mut parts = self.head.uri.clone().into_parts();
if let Some(path_and_query) = parts.path_and_query {
let query = serde_urlencoded::to_string(query)?;
let path = path_and_query.path();
parts.path_and_query = format!("{}?{}", path, query).parse().ok();
match Uri::from_parts(parts) {
Ok(uri) => self.head.uri = uri,
Err(e) => self.err = Some(e.into()),
}
}
Ok(self)
}
/// Freeze request builder and construct `FrozenClientRequest`,
/// which could be used for sending same request multiple times.
pub fn freeze(self) -> Result<FrozenClientRequest, FreezeRequestError> {
@ -690,4 +711,13 @@ mod tests {
"Bearer someS3cr3tAutht0k3n"
);
}
#[test]
fn client_query() {
let req = Client::new()
.get("/")
.query(&[("key1", "val1"), ("key2", "val2")])
.unwrap();
assert_eq!(req.get_uri().query().unwrap(), "key1=val1&key2=val2");
}
}