mirror of
https://github.com/actix/actix-web.git
synced 2024-12-29 11:30:41 +00:00
Merge pull request #306 from eddomuke/master
add ClientRequestBuilder::form()
This commit is contained in:
commit
b679b4cabc
4 changed files with 54 additions and 0 deletions
|
@ -4,6 +4,8 @@
|
|||
|
||||
### Added
|
||||
|
||||
* Add `ClientRequestBuilder::form()` for sending `application/x-www-form-urlencoded` requests.
|
||||
|
||||
* Add methods to `HttpResponse` to retrieve, add, and delete cookies
|
||||
|
||||
* Add `.set_content_type()` and `.set_content_disposition()` methods
|
||||
|
|
|
@ -10,6 +10,7 @@ use futures::Stream;
|
|||
use percent_encoding::{percent_encode, USERINFO_ENCODE_SET};
|
||||
use serde::Serialize;
|
||||
use serde_json;
|
||||
use serde_urlencoded;
|
||||
use url::Url;
|
||||
|
||||
use super::body::ClientBody;
|
||||
|
@ -658,6 +659,24 @@ impl ClientRequestBuilder {
|
|||
|
||||
self.body(body)
|
||||
}
|
||||
|
||||
/// Set a urlencoded body and generate `ClientRequest`
|
||||
///
|
||||
/// `ClientRequestBuilder` can not be used after this call.
|
||||
pub fn form<T: Serialize>(&mut self, value: T) -> Result<ClientRequest, Error> {
|
||||
let body = serde_urlencoded::to_string(&value)?;
|
||||
|
||||
let contains = if let Some(parts) = parts(&mut self.request, &self.err) {
|
||||
parts.headers.contains_key(header::CONTENT_TYPE)
|
||||
} else {
|
||||
true
|
||||
};
|
||||
if !contains {
|
||||
self.header(header::CONTENT_TYPE, "application/x-www-form-urlencoded");
|
||||
}
|
||||
|
||||
self.body(body)
|
||||
}
|
||||
|
||||
/// Set a streaming body and generate `ClientRequest`.
|
||||
///
|
||||
|
|
|
@ -16,6 +16,7 @@ use http_range::HttpRangeParseError;
|
|||
use httparse;
|
||||
use serde::de::value::Error as DeError;
|
||||
use serde_json::error::Error as JsonError;
|
||||
use serde_urlencoded::ser::Error as FormError;
|
||||
use tokio_timer::Error as TimerError;
|
||||
pub use url::ParseError as UrlParseError;
|
||||
|
||||
|
@ -205,6 +206,9 @@ impl From<failure::Error> for Error {
|
|||
/// `InternalServerError` for `JsonError`
|
||||
impl ResponseError for JsonError {}
|
||||
|
||||
/// `InternalServerError` for `FormError`
|
||||
impl ResponseError for FormError {}
|
||||
|
||||
/// `InternalServerError` for `TimerError`
|
||||
impl ResponseError for TimerError {}
|
||||
|
||||
|
|
|
@ -96,6 +96,35 @@ fn test_async_extractor_async() {
|
|||
assert_eq!(bytes, Bytes::from_static(b"{\"test\":1}"));
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
struct FormData {
|
||||
username: String,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_form_extractor() {
|
||||
let mut srv = test::TestServer::new(|app| {
|
||||
app.resource("/{username}/index.html", |r| {
|
||||
r.route().with(|form: Form<FormData>| {
|
||||
format!("{}", form.username)
|
||||
})
|
||||
});
|
||||
});
|
||||
|
||||
// client request
|
||||
let request = srv
|
||||
.post()
|
||||
.uri(srv.url("/test1/index.html"))
|
||||
.form(FormData{username: "test".to_string()})
|
||||
.unwrap();
|
||||
let response = srv.execute(request.send()).unwrap();
|
||||
assert!(response.status().is_success());
|
||||
|
||||
// read response
|
||||
let bytes = srv.execute(response.body()).unwrap();
|
||||
assert_eq!(bytes, Bytes::from_static(b"test"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_path_and_query_extractor() {
|
||||
let mut srv = test::TestServer::new(|app| {
|
||||
|
|
Loading…
Reference in a new issue