upgrade hyper to v0.11
This commit is contained in:
128
src/request.rs
128
src/request.rs
@@ -5,15 +5,14 @@ use serde::Serialize;
|
||||
use serde_json;
|
||||
use serde_urlencoded;
|
||||
|
||||
use body::{self, Body};
|
||||
use header::Headers;
|
||||
use {Body, Client, Method, Url};
|
||||
use {async_impl, Client, Method, Url};
|
||||
|
||||
/// A request which can be executed with `Client::execute()`.
|
||||
pub struct Request {
|
||||
method: Method,
|
||||
url: Url,
|
||||
headers: Headers,
|
||||
body: Option<Body>,
|
||||
inner: async_impl::Request,
|
||||
}
|
||||
|
||||
/// A builder to construct the properties of a `Request`.
|
||||
@@ -27,47 +26,45 @@ impl Request {
|
||||
#[inline]
|
||||
pub fn new(method: Method, url: Url) -> Self {
|
||||
Request {
|
||||
method,
|
||||
url,
|
||||
headers: Headers::new(),
|
||||
body: None,
|
||||
inner: async_impl::Request::new(method, url),
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the method.
|
||||
#[inline]
|
||||
pub fn method(&self) -> &Method {
|
||||
&self.method
|
||||
self.inner.method()
|
||||
}
|
||||
|
||||
/// Get a mutable reference to the method.
|
||||
#[inline]
|
||||
pub fn method_mut(&mut self) -> &mut Method {
|
||||
&mut self.method
|
||||
self.inner.method_mut()
|
||||
}
|
||||
|
||||
/// Get the url.
|
||||
#[inline]
|
||||
pub fn url(&self) -> &Url {
|
||||
&self.url
|
||||
self.inner.url()
|
||||
}
|
||||
|
||||
/// Get a mutable reference to the url.
|
||||
#[inline]
|
||||
pub fn url_mut(&mut self) -> &mut Url {
|
||||
&mut self.url
|
||||
self.inner.url_mut()
|
||||
}
|
||||
|
||||
/// Get the headers.
|
||||
#[inline]
|
||||
pub fn headers(&self) -> &Headers {
|
||||
&self.headers
|
||||
self.inner.headers()
|
||||
}
|
||||
|
||||
/// Get a mutable reference to the headers.
|
||||
#[inline]
|
||||
pub fn headers_mut(&mut self) -> &mut Headers {
|
||||
&mut self.headers
|
||||
self.inner.headers_mut()
|
||||
}
|
||||
|
||||
/// Get the body.
|
||||
@@ -92,18 +89,19 @@ impl RequestBuilder {
|
||||
/// # fn run() -> Result<(), Box<::std::error::Error>> {
|
||||
/// let client = reqwest::Client::new()?;
|
||||
/// let res = client.get("https://www.rust-lang.org")?
|
||||
/// .header(UserAgent("foo".to_string()))
|
||||
/// .header(UserAgent::new("foo"))
|
||||
/// .send()?;
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn header<H>(&mut self, header: H) -> &mut RequestBuilder
|
||||
where
|
||||
H: ::header::Header + ::header::HeaderFormat,
|
||||
H: ::header::Header,
|
||||
{
|
||||
self.request_mut().headers.set(header);
|
||||
self.request_mut().headers_mut().set(header);
|
||||
self
|
||||
}
|
||||
|
||||
/// Add a set of Headers to the existing ones on this Request.
|
||||
///
|
||||
/// The headers will be merged in to any already set.
|
||||
@@ -114,7 +112,7 @@ impl RequestBuilder {
|
||||
///
|
||||
/// fn construct_headers() -> Headers {
|
||||
/// let mut headers = Headers::new();
|
||||
/// headers.set(UserAgent("reqwest".to_string()));
|
||||
/// headers.set(UserAgent::new("reqwest"));
|
||||
/// headers.set(ContentType::png());
|
||||
/// headers
|
||||
/// }
|
||||
@@ -130,7 +128,7 @@ impl RequestBuilder {
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn headers(&mut self, headers: ::header::Headers) -> &mut RequestBuilder {
|
||||
self.request_mut().headers.extend(headers.iter());
|
||||
self.request_mut().headers_mut().extend(headers.iter());
|
||||
self
|
||||
}
|
||||
|
||||
@@ -193,7 +191,7 @@ impl RequestBuilder {
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn body<T: Into<Body>>(&mut self, body: T) -> &mut RequestBuilder {
|
||||
self.request_mut().body = Some(body.into());
|
||||
*self.request_mut().body_mut() = Some(body.into());
|
||||
self
|
||||
}
|
||||
|
||||
@@ -224,12 +222,13 @@ impl RequestBuilder {
|
||||
/// This method fails if the passed value cannot be serialized into
|
||||
/// url encoded format
|
||||
pub fn form<T: Serialize>(&mut self, form: &T) -> ::Result<&mut RequestBuilder> {
|
||||
|
||||
{
|
||||
// check request_mut() before running serde
|
||||
let mut req = self.request_mut();
|
||||
let body = try_!(serde_urlencoded::to_string(form));
|
||||
req.headers.set(ContentType::form_url_encoded());
|
||||
req.body = Some(body.into());
|
||||
req.headers_mut().set(ContentType::form_url_encoded());
|
||||
*req.body_mut() = Some(body.into());
|
||||
}
|
||||
Ok(self)
|
||||
}
|
||||
@@ -264,8 +263,8 @@ impl RequestBuilder {
|
||||
// check request_mut() before running serde
|
||||
let mut req = self.request_mut();
|
||||
let body = try_!(serde_json::to_vec(json));
|
||||
req.headers.set(ContentType::json());
|
||||
req.body = Some(body.into());
|
||||
req.headers_mut().set(ContentType::json());
|
||||
*req.body_mut() = Some(body.into());
|
||||
}
|
||||
Ok(self)
|
||||
}
|
||||
@@ -324,9 +323,9 @@ impl fmt::Debug for RequestBuilder {
|
||||
}
|
||||
|
||||
fn fmt_request_fields<'a, 'b>(f: &'a mut fmt::DebugStruct<'a, 'b>, req: &Request) -> &'a mut fmt::DebugStruct<'a, 'b> {
|
||||
f.field("method", &req.method)
|
||||
.field("url", &req.url)
|
||||
.field("headers", &req.headers)
|
||||
f.field("method", req.method())
|
||||
.field("url", req.url())
|
||||
.field("headers", req.headers())
|
||||
}
|
||||
|
||||
// pub(crate)
|
||||
@@ -340,16 +339,25 @@ pub fn builder(client: Client, req: Request) -> RequestBuilder {
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn pieces(req: Request) -> (Method, Url, Headers, Option<Body>) {
|
||||
(req.method, req.url, req.headers, req.body)
|
||||
pub fn async(req: Request) -> (async_impl::Request, Option<body::Sender>) {
|
||||
use header::ContentLength;
|
||||
|
||||
let mut req_async = req.inner;
|
||||
let body = req.body.and_then(|body| {
|
||||
let (tx, body, len) = body::async(body);
|
||||
if let Some(len) = len {
|
||||
req_async.headers_mut().set(ContentLength(len));
|
||||
}
|
||||
*req_async.body_mut() = Some(body);
|
||||
tx
|
||||
});
|
||||
(req_async, body)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use body;
|
||||
use client::Client;
|
||||
use hyper::method::Method;
|
||||
use hyper::header::{Host, Headers, ContentType};
|
||||
use {body, Client, Method};
|
||||
use header::{Host, Headers, ContentType};
|
||||
use std::collections::HashMap;
|
||||
use serde_urlencoded;
|
||||
use serde_json;
|
||||
@@ -360,8 +368,8 @@ mod tests {
|
||||
let some_url = "https://google.com/";
|
||||
let r = client.get(some_url).unwrap().build();
|
||||
|
||||
assert_eq!(r.method, Method::Get);
|
||||
assert_eq!(r.url.as_str(), some_url);
|
||||
assert_eq!(r.method(), &Method::Get);
|
||||
assert_eq!(r.url().as_str(), some_url);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -370,8 +378,8 @@ mod tests {
|
||||
let some_url = "https://google.com/";
|
||||
let r = client.head(some_url).unwrap().build();
|
||||
|
||||
assert_eq!(r.method, Method::Head);
|
||||
assert_eq!(r.url.as_str(), some_url);
|
||||
assert_eq!(r.method(), &Method::Head);
|
||||
assert_eq!(r.url().as_str(), some_url);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -380,8 +388,8 @@ mod tests {
|
||||
let some_url = "https://google.com/";
|
||||
let r = client.post(some_url).unwrap().build();
|
||||
|
||||
assert_eq!(r.method, Method::Post);
|
||||
assert_eq!(r.url.as_str(), some_url);
|
||||
assert_eq!(r.method(), &Method::Post);
|
||||
assert_eq!(r.url().as_str(), some_url);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -390,8 +398,8 @@ mod tests {
|
||||
let some_url = "https://google.com/";
|
||||
let r = client.put(some_url).unwrap().build();
|
||||
|
||||
assert_eq!(r.method, Method::Put);
|
||||
assert_eq!(r.url.as_str(), some_url);
|
||||
assert_eq!(r.method(), &Method::Put);
|
||||
assert_eq!(r.url().as_str(), some_url);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -400,8 +408,8 @@ mod tests {
|
||||
let some_url = "https://google.com/";
|
||||
let r = client.patch(some_url).unwrap().build();
|
||||
|
||||
assert_eq!(r.method, Method::Patch);
|
||||
assert_eq!(r.url.as_str(), some_url);
|
||||
assert_eq!(r.method(), &Method::Patch);
|
||||
assert_eq!(r.url().as_str(), some_url);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -410,8 +418,8 @@ mod tests {
|
||||
let some_url = "https://google.com/";
|
||||
let r = client.delete(some_url).unwrap().build();
|
||||
|
||||
assert_eq!(r.method, Method::Delete);
|
||||
assert_eq!(r.url.as_str(), some_url);
|
||||
assert_eq!(r.method(), &Method::Delete);
|
||||
assert_eq!(r.url().as_str(), some_url);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -420,16 +428,13 @@ mod tests {
|
||||
let some_url = "https://google.com/";
|
||||
let mut r = client.post(some_url).unwrap();
|
||||
|
||||
let header = Host {
|
||||
hostname: "google.com".to_string(),
|
||||
port: None,
|
||||
};
|
||||
let header = Host::new("google.com", None);
|
||||
|
||||
// Add a copy of the header to the request builder
|
||||
let r = r.header(header.clone()).build();
|
||||
|
||||
// then check it was actually added
|
||||
assert_eq!(r.headers.get::<Host>(), Some(&header));
|
||||
assert_eq!(r.headers().get::<Host>(), Some(&header));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -438,10 +443,7 @@ mod tests {
|
||||
let some_url = "https://google.com/";
|
||||
let mut r = client.post(some_url).unwrap();
|
||||
|
||||
let header = Host {
|
||||
hostname: "google.com".to_string(),
|
||||
port: None,
|
||||
};
|
||||
let header = Host::new("google.com", None);
|
||||
|
||||
let mut headers = Headers::new();
|
||||
headers.set(header);
|
||||
@@ -450,7 +452,7 @@ mod tests {
|
||||
let r = r.headers(headers.clone()).build();
|
||||
|
||||
// then make sure they were added correctly
|
||||
assert_eq!(r.headers, headers);
|
||||
assert_eq!(r.headers(), &headers);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -461,9 +463,9 @@ mod tests {
|
||||
|
||||
let body = "Some interesting content";
|
||||
|
||||
let r = r.body(body).build();
|
||||
let mut r = r.body(body).build();
|
||||
|
||||
let buf = body::read_to_string(r.body.unwrap()).unwrap();
|
||||
let buf = body::read_to_string(r.body_mut().take().unwrap()).unwrap();
|
||||
|
||||
assert_eq!(buf, body);
|
||||
}
|
||||
@@ -477,13 +479,13 @@ mod tests {
|
||||
let mut form_data = HashMap::new();
|
||||
form_data.insert("foo", "bar");
|
||||
|
||||
let r = r.form(&form_data).unwrap().build();
|
||||
let mut r = r.form(&form_data).unwrap().build();
|
||||
|
||||
// Make sure the content type was set
|
||||
assert_eq!(r.headers.get::<ContentType>(),
|
||||
assert_eq!(r.headers().get::<ContentType>(),
|
||||
Some(&ContentType::form_url_encoded()));
|
||||
|
||||
let buf = body::read_to_string(r.body.unwrap()).unwrap();
|
||||
let buf = body::read_to_string(r.body_mut().take().unwrap()).unwrap();
|
||||
|
||||
let body_should_be = serde_urlencoded::to_string(&form_data).unwrap();
|
||||
assert_eq!(buf, body_should_be);
|
||||
@@ -498,12 +500,12 @@ mod tests {
|
||||
let mut json_data = HashMap::new();
|
||||
json_data.insert("foo", "bar");
|
||||
|
||||
let r = r.json(&json_data).unwrap().build();
|
||||
let mut r = r.json(&json_data).unwrap().build();
|
||||
|
||||
// Make sure the content type was set
|
||||
assert_eq!(r.headers.get::<ContentType>(), Some(&ContentType::json()));
|
||||
assert_eq!(r.headers().get::<ContentType>(), Some(&ContentType::json()));
|
||||
|
||||
let buf = body::read_to_string(r.body.unwrap()).unwrap();
|
||||
let buf = body::read_to_string(r.body_mut().take().unwrap()).unwrap();
|
||||
|
||||
let body_should_be = serde_json::to_string(&json_data).unwrap();
|
||||
assert_eq!(buf, body_should_be);
|
||||
@@ -525,7 +527,7 @@ mod tests {
|
||||
let client = Client::new().unwrap();
|
||||
let some_url = "https://google.com/";
|
||||
let mut r = client.post(some_url).unwrap();
|
||||
let json_data = MyStruct{};
|
||||
let json_data = MyStruct;
|
||||
assert!(r.json(&json_data).unwrap_err().is_serialization());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user