Merge pull request #126 from tomprince/rustfmt-no-controversy
Apply some uncontoversial rustfmt changes.
This commit is contained in:
@@ -11,6 +11,7 @@ matrix:
|
||||
- rust: nightly
|
||||
|
||||
sudo: false
|
||||
dist: trusty
|
||||
|
||||
cache:
|
||||
apt: true
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
//! `cargo run --example response_json`
|
||||
extern crate reqwest;
|
||||
#[macro_use] extern crate serde_derive;
|
||||
#[macro_use] extern crate error_chain;
|
||||
#[macro_use]
|
||||
extern crate serde_derive;
|
||||
#[macro_use]
|
||||
extern crate error_chain;
|
||||
|
||||
error_chain! {
|
||||
foreign_links {
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
//! `cargo run --example simple`
|
||||
extern crate reqwest;
|
||||
extern crate env_logger;
|
||||
#[macro_use] extern crate error_chain;
|
||||
#[macro_use]
|
||||
extern crate error_chain;
|
||||
|
||||
error_chain! {
|
||||
foreign_links {
|
||||
@@ -11,8 +12,7 @@ error_chain! {
|
||||
}
|
||||
|
||||
fn run() -> Result<()> {
|
||||
env_logger::init()
|
||||
.expect("Failed to initialize logger");
|
||||
env_logger::init().expect("Failed to initialize logger");
|
||||
|
||||
println!("GET https://www.rust-lang.org");
|
||||
|
||||
|
||||
@@ -49,13 +49,10 @@ impl Body {
|
||||
pub fn read_to_string(mut body: Body) -> ::std::io::Result<String> {
|
||||
let mut s = String::new();
|
||||
match body.reader {
|
||||
Kind::Reader(ref mut reader, _) => {
|
||||
reader.read_to_string(&mut s)
|
||||
Kind::Reader(ref mut reader, _) => reader.read_to_string(&mut s),
|
||||
Kind::Bytes(ref mut bytes) => (&**bytes).read_to_string(&mut s),
|
||||
}
|
||||
Kind::Bytes(ref mut bytes) => {
|
||||
(&**bytes).read_to_string(&mut s)
|
||||
}
|
||||
}.map(|_| s)
|
||||
.map(|_| s)
|
||||
}
|
||||
|
||||
enum Kind {
|
||||
|
||||
@@ -5,11 +5,11 @@ use std::time::Duration;
|
||||
|
||||
use hyper::client::IntoUrl;
|
||||
use hyper::header::{Headers, ContentType, Location, Referer, UserAgent, Accept, Encoding,
|
||||
AcceptEncoding, Range, qitem};
|
||||
AcceptEncoding, Range, qitem};
|
||||
use hyper::method::Method;
|
||||
use hyper::status::StatusCode;
|
||||
use hyper::version::HttpVersion;
|
||||
use hyper::{Url};
|
||||
use hyper::Url;
|
||||
|
||||
use hyper_native_tls::{NativeTlsClient, native_tls};
|
||||
|
||||
@@ -17,11 +17,12 @@ use serde::Serialize;
|
||||
use serde_json;
|
||||
use serde_urlencoded;
|
||||
|
||||
use ::body::{self, Body};
|
||||
use ::redirect::{self, RedirectPolicy, check_redirect, remove_sensitive_headers};
|
||||
use ::response::Response;
|
||||
use body::{self, Body};
|
||||
use redirect::{self, RedirectPolicy, check_redirect, remove_sensitive_headers};
|
||||
use response::Response;
|
||||
|
||||
static DEFAULT_USER_AGENT: &'static str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));
|
||||
static DEFAULT_USER_AGENT: &'static str =
|
||||
concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));
|
||||
|
||||
/// A `Client` to make Requests with.
|
||||
///
|
||||
@@ -76,7 +77,8 @@ impl Certificate {
|
||||
pub fn from_der(der: &[u8]) -> ::Result<Certificate> {
|
||||
let inner = try_!(
|
||||
native_tls::Certificate::from_der(der)
|
||||
.map_err(|e| ::hyper::Error::Ssl(Box::new(e))));
|
||||
.map_err(|e| ::hyper::Error::Ssl(Box::new(e)))
|
||||
);
|
||||
Ok(Certificate(inner))
|
||||
}
|
||||
}
|
||||
@@ -121,7 +123,8 @@ impl ClientBuilder {
|
||||
pub fn new() -> ::Result<ClientBuilder> {
|
||||
let tls_connector_builder = try_!(
|
||||
native_tls::TlsConnector::builder()
|
||||
.map_err(|e| ::hyper::Error::Ssl(Box::new(e))));
|
||||
.map_err(|e| ::hyper::Error::Ssl(Box::new(e)))
|
||||
);
|
||||
Ok(ClientBuilder {
|
||||
config: Some(Config {
|
||||
hostname_verification: true,
|
||||
@@ -140,7 +143,11 @@ impl ClientBuilder {
|
||||
let config = self.take_config();
|
||||
|
||||
let tls_connector = try_!(
|
||||
config.tls.build().map_err(|e| ::hyper::Error::Ssl(Box::new(e))));
|
||||
config
|
||||
.tls
|
||||
.build()
|
||||
.map_err(|e| ::hyper::Error::Ssl(Box::new(e)))
|
||||
);
|
||||
let mut tls_client = NativeTlsClient::from(tls_connector);
|
||||
if !config.hostname_verification {
|
||||
tls_client.danger_disable_hostname_verification(true);
|
||||
@@ -149,7 +156,9 @@ impl ClientBuilder {
|
||||
let mut hyper_client = ::hyper::Client::with_connector(
|
||||
::hyper::client::Pool::with_connector(
|
||||
Default::default(),
|
||||
::hyper::net::HttpsConnector::new(tls_client)));
|
||||
::hyper::net::HttpsConnector::new(tls_client),
|
||||
)
|
||||
);
|
||||
|
||||
hyper_client.set_redirect_policy(::hyper::client::RedirectPolicy::FollowNone);
|
||||
|
||||
@@ -168,8 +177,12 @@ impl ClientBuilder {
|
||||
/// This can be used to connect to a server that has a self-signed
|
||||
/// certificate for example.
|
||||
pub fn add_root_certificate(&mut self, cert: Certificate) -> ::Result<&mut ClientBuilder> {
|
||||
try_!(self.config_mut().tls.add_root_certificate(cert.0)
|
||||
.map_err(|e| ::hyper::Error::Ssl(Box::new(e))));
|
||||
try_!(
|
||||
self.config_mut()
|
||||
.tls
|
||||
.add_root_certificate(cert.0)
|
||||
.map_err(|e| ::hyper::Error::Ssl(Box::new(e)))
|
||||
);
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
@@ -192,11 +205,15 @@ impl ClientBuilder {
|
||||
|
||||
// private
|
||||
fn config_mut(&mut self) -> &mut Config {
|
||||
self.config.as_mut().expect("ClientBuilder cannot be reused after building a Client")
|
||||
self.config
|
||||
.as_mut()
|
||||
.expect("ClientBuilder cannot be reused after building a Client")
|
||||
}
|
||||
|
||||
fn take_config(&mut self) -> Config {
|
||||
self.config.take().expect("ClientBuilder cannot be reused after building a Client")
|
||||
self.config
|
||||
.take()
|
||||
.expect("ClientBuilder cannot be reused after building a Client")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -327,7 +344,10 @@ impl RequestBuilder {
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn header<H: ::header::Header + ::header::HeaderFormat>(mut self, header: H) -> RequestBuilder {
|
||||
pub fn header<H>(mut self, header: H) -> RequestBuilder
|
||||
where
|
||||
H: ::header::Header + ::header::HeaderFormat,
|
||||
{
|
||||
self.headers.set(header);
|
||||
self
|
||||
}
|
||||
@@ -341,7 +361,9 @@ impl RequestBuilder {
|
||||
|
||||
/// Enable HTTP basic authentication.
|
||||
pub fn basic_auth<U, P>(self, username: U, password: Option<P>) -> RequestBuilder
|
||||
where U: Into<String>, P: Into<String>
|
||||
where
|
||||
U: Into<String>,
|
||||
P: Into<String>,
|
||||
{
|
||||
self.header(::header::Authorization(::header::Basic{
|
||||
username: username.into(),
|
||||
@@ -456,13 +478,13 @@ impl RequestBuilder {
|
||||
StatusCode::SeeOther => {
|
||||
body = None;
|
||||
match method {
|
||||
Method::Get | Method::Head => {},
|
||||
Method::Get | Method::Head => {}
|
||||
_ => {
|
||||
method = Method::Get;
|
||||
}
|
||||
}
|
||||
true
|
||||
},
|
||||
}
|
||||
StatusCode::TemporaryRedirect |
|
||||
StatusCode::PermanentRedirect => {
|
||||
if let Some(ref body) = body {
|
||||
@@ -470,7 +492,7 @@ impl RequestBuilder {
|
||||
} else {
|
||||
true
|
||||
}
|
||||
},
|
||||
}
|
||||
_ => false,
|
||||
};
|
||||
|
||||
@@ -492,33 +514,34 @@ impl RequestBuilder {
|
||||
}
|
||||
}
|
||||
urls.push(url);
|
||||
let action = check_redirect(&client.redirect_policy.lock().unwrap(), &loc, &urls);
|
||||
let action =
|
||||
check_redirect(&client.redirect_policy.lock().unwrap(), &loc, &urls);
|
||||
|
||||
match action {
|
||||
redirect::Action::Follow => loc,
|
||||
redirect::Action::Stop => {
|
||||
debug!("redirect_policy disallowed redirection to '{}'", loc);
|
||||
return Ok(::response::new(res, client.auto_ungzip.load(Ordering::Relaxed)));
|
||||
},
|
||||
}
|
||||
redirect::Action::LoopDetected => {
|
||||
return Err(::error::loop_detected(res.url.clone()));
|
||||
},
|
||||
}
|
||||
redirect::Action::TooManyRedirects => {
|
||||
return Err(::error::too_many_redirects(res.url.clone()));
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
Err(e) => {
|
||||
debug!("Location header had invalid URI: {:?}", e);
|
||||
|
||||
return Ok(::response::new(res, client.auto_ungzip.load(Ordering::Relaxed)))
|
||||
return Ok(::response::new(res, client.auto_ungzip.load(Ordering::Relaxed)));
|
||||
}
|
||||
};
|
||||
|
||||
remove_sensitive_headers(&mut headers, &url, &urls);
|
||||
debug!("redirecting to {:?} '{}'", method, url);
|
||||
} else {
|
||||
return Ok(::response::new(res, client.auto_ungzip.load(Ordering::Relaxed)))
|
||||
return Ok(::response::new(res, client.auto_ungzip.load(Ordering::Relaxed)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -549,7 +572,7 @@ fn make_referer(next: &Url, previous: &Url) -> Option<Referer> {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use ::body;
|
||||
use body;
|
||||
use hyper::method::Method;
|
||||
use hyper::Url;
|
||||
use hyper::header::{Host, Headers, ContentType};
|
||||
@@ -683,7 +706,8 @@ mod tests {
|
||||
r = r.form(&form_data);
|
||||
|
||||
// Make sure the content type was set
|
||||
assert_eq!(r.headers.get::<ContentType>(), Some(&ContentType::form_url_encoded()));
|
||||
assert_eq!(r.headers.get::<ContentType>(),
|
||||
Some(&ContentType::form_url_encoded()));
|
||||
|
||||
let buf = body::read_to_string(r.body.unwrap().unwrap()).unwrap();
|
||||
|
||||
|
||||
20
src/error.rs
20
src/error.rs
@@ -1,7 +1,7 @@
|
||||
use std::error::Error as StdError;
|
||||
use std::fmt;
|
||||
|
||||
use ::Url;
|
||||
use Url;
|
||||
|
||||
/// The Errors that may occur when processing a `Request`.
|
||||
#[derive(Debug)]
|
||||
@@ -153,10 +153,12 @@ impl From<InternalFrom<Error>> for Error {
|
||||
}
|
||||
|
||||
impl<T> From<InternalFrom<T>> for Error
|
||||
where T: Into<Kind> {
|
||||
where
|
||||
T: Into<Kind>,
|
||||
{
|
||||
#[inline]
|
||||
fn from(other: InternalFrom<T>) -> Error {
|
||||
Error {
|
||||
Error {
|
||||
kind: other.0.into(),
|
||||
url: other.1,
|
||||
}
|
||||
@@ -165,7 +167,9 @@ where T: Into<Kind> {
|
||||
|
||||
#[inline]
|
||||
pub fn from<T>(err: T) -> Error
|
||||
where T: Into<Kind> {
|
||||
where
|
||||
T: Into<Kind>,
|
||||
{
|
||||
InternalFrom(err, None).into()
|
||||
}
|
||||
|
||||
@@ -188,11 +192,13 @@ pub fn too_many_redirects(url: Url) -> Error {
|
||||
#[test]
|
||||
fn test_error_get_ref_downcasts() {
|
||||
let err: Error = from(::hyper::Error::Status);
|
||||
let cause = err.get_ref().unwrap()
|
||||
.downcast_ref::<::hyper::Error>().unwrap();
|
||||
let cause = err.get_ref()
|
||||
.unwrap()
|
||||
.downcast_ref::<::hyper::Error>()
|
||||
.unwrap();
|
||||
|
||||
match cause {
|
||||
&::hyper::Error::Status => (),
|
||||
_ => panic!("unexpected downcast: {:?}", cause)
|
||||
_ => panic!("unexpected downcast: {:?}", cause),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -115,7 +115,8 @@
|
||||
//! [serde]: http://serde.rs
|
||||
extern crate hyper;
|
||||
|
||||
#[macro_use] extern crate log;
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
extern crate libflate;
|
||||
extern crate hyper_native_tls;
|
||||
extern crate serde;
|
||||
|
||||
@@ -2,7 +2,7 @@ use std::fmt;
|
||||
|
||||
use hyper::header::{Headers, Authorization, Cookie};
|
||||
|
||||
use ::Url;
|
||||
use Url;
|
||||
|
||||
/// A type that controls the policy on how to handle the following of redirects.
|
||||
///
|
||||
@@ -77,7 +77,9 @@ impl RedirectPolicy {
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn custom<T>(policy: T) -> RedirectPolicy
|
||||
where T: Fn(RedirectAttempt) -> RedirectAction + Send + Sync + 'static {
|
||||
where
|
||||
T: Fn(RedirectAttempt) -> RedirectAction + Send + Sync + 'static,
|
||||
{
|
||||
RedirectPolicy {
|
||||
inner: Policy::Custom(Box::new(policy)),
|
||||
}
|
||||
@@ -94,7 +96,7 @@ impl RedirectPolicy {
|
||||
} else {
|
||||
attempt.follow()
|
||||
}
|
||||
},
|
||||
}
|
||||
Policy::None => attempt.stop(),
|
||||
}
|
||||
}
|
||||
@@ -187,8 +189,8 @@ pub fn check_redirect(policy: &RedirectPolicy, next: &Url, previous: &[Url]) ->
|
||||
|
||||
pub fn remove_sensitive_headers(headers: &mut Headers, next: &Url, previous: &[Url]) {
|
||||
if let Some(previous) = previous.last() {
|
||||
let cross_host = next.host_str() != previous.host_str()
|
||||
|| next.port_or_known_default() != previous.port_or_known_default();
|
||||
let cross_host = next.host_str() != previous.host_str() ||
|
||||
next.port_or_known_default() != previous.port_or_known_default();
|
||||
if cross_host {
|
||||
headers.remove::<Authorization<String>>();
|
||||
headers.remove::<Cookie>();
|
||||
@@ -229,7 +231,8 @@ fn test_redirect_policy_limit() {
|
||||
|
||||
previous.push(Url::parse("http://a.b.d/e/33").unwrap());
|
||||
|
||||
assert_eq!(check_redirect(&policy, &next, &previous), Action::TooManyRedirects);
|
||||
assert_eq!(check_redirect(&policy, &next, &previous),
|
||||
Action::TooManyRedirects);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -256,13 +259,9 @@ fn test_remove_sensitive_headers() {
|
||||
let mut headers = Headers::new();
|
||||
headers.set(Accept::star());
|
||||
headers.set(Authorization("let me in".to_owned()));
|
||||
headers.set(
|
||||
Cookie(vec![
|
||||
String::from("foo=bar")
|
||||
])
|
||||
);
|
||||
headers.set(Cookie(vec![String::from("foo=bar")]));
|
||||
|
||||
let next = Url::parse("http://initial-domain.com/path").unwrap();
|
||||
let next = Url::parse("http://initial-domain.com/path").unwrap();
|
||||
let mut prev = vec![Url::parse("http://initial-domain.com/new_path").unwrap()];
|
||||
let mut filtered_headers = headers.clone();
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ pub struct Response {
|
||||
pub fn new(res: ::hyper::client::Response, gzip: bool) -> Response {
|
||||
info!("Response: '{}' for {}", res.status, res.url);
|
||||
Response {
|
||||
inner: Decoder::from_hyper_response(res, gzip)
|
||||
inner: Decoder::from_hyper_response(res, gzip),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,8 +32,8 @@ impl fmt::Debug for Response {
|
||||
.field("headers", &hyper_response.headers)
|
||||
.field("version", &hyper_response.version)
|
||||
.finish()
|
||||
},
|
||||
Decoder::Gzip{ ref head, .. } |
|
||||
}
|
||||
Decoder::Gzip { ref head, .. } |
|
||||
Decoder::Errored { ref head, .. } => {
|
||||
f.debug_struct("Response")
|
||||
.field("url", &head.url)
|
||||
@@ -52,7 +52,7 @@ impl Response {
|
||||
pub fn url(&self) -> &Url {
|
||||
match self.inner {
|
||||
Decoder::PlainText(ref hyper_response) => &hyper_response.url,
|
||||
Decoder::Gzip{ ref head, .. } |
|
||||
Decoder::Gzip { ref head, .. } |
|
||||
Decoder::Errored { ref head, .. } => &head.url,
|
||||
}
|
||||
}
|
||||
@@ -62,7 +62,7 @@ impl Response {
|
||||
pub fn status(&self) -> &StatusCode {
|
||||
match self.inner {
|
||||
Decoder::PlainText(ref hyper_response) => &hyper_response.status,
|
||||
Decoder::Gzip{ ref head, .. } |
|
||||
Decoder::Gzip { ref head, .. } |
|
||||
Decoder::Errored { ref head, .. } => &head.status,
|
||||
}
|
||||
}
|
||||
@@ -72,7 +72,7 @@ impl Response {
|
||||
pub fn headers(&self) -> &Headers {
|
||||
match self.inner {
|
||||
Decoder::PlainText(ref hyper_response) => &hyper_response.headers,
|
||||
Decoder::Gzip{ ref head, .. } |
|
||||
Decoder::Gzip { ref head, .. } |
|
||||
Decoder::Errored { ref head, .. } => &head.headers,
|
||||
}
|
||||
}
|
||||
@@ -82,7 +82,7 @@ impl Response {
|
||||
pub fn version(&self) -> &HttpVersion {
|
||||
match self.inner {
|
||||
Decoder::PlainText(ref hyper_response) => &hyper_response.version,
|
||||
Decoder::Gzip{ ref head, .. } |
|
||||
Decoder::Gzip { ref head, .. } |
|
||||
Decoder::Errored { ref head, .. } => &head.version,
|
||||
}
|
||||
}
|
||||
@@ -148,12 +148,13 @@ impl Decoder {
|
||||
}
|
||||
let content_encoding_gzip: bool;
|
||||
let mut is_gzip = {
|
||||
content_encoding_gzip = res.headers.get::<ContentEncoding>().map_or(false, |encs|{
|
||||
encs.contains(&Encoding::Gzip)
|
||||
});
|
||||
content_encoding_gzip || res.headers.get::<TransferEncoding>().map_or(false, |encs|{
|
||||
encs.contains(&Encoding::Gzip)
|
||||
})
|
||||
content_encoding_gzip = res.headers
|
||||
.get::<ContentEncoding>()
|
||||
.map_or(false, |encs| encs.contains(&Encoding::Gzip));
|
||||
content_encoding_gzip ||
|
||||
res.headers
|
||||
.get::<TransferEncoding>()
|
||||
.map_or(false, |encs| encs.contains(&Encoding::Gzip))
|
||||
};
|
||||
if is_gzip {
|
||||
if let Some(content_length) = res.headers.get::<ContentLength>() {
|
||||
@@ -184,7 +185,7 @@ fn new_gzip(mut res: ::hyper::client::Response) -> Decoder {
|
||||
Ok(0) => return Decoder::PlainText(res),
|
||||
Ok(n) => {
|
||||
debug_assert_eq!(n, 1);
|
||||
},
|
||||
}
|
||||
Err(e) => return Decoder::Errored {
|
||||
err: Some(e),
|
||||
head: Head {
|
||||
@@ -193,7 +194,7 @@ fn new_gzip(mut res: ::hyper::client::Response) -> Decoder {
|
||||
url: res.url.clone(),
|
||||
version: res.version,
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
let head = Head {
|
||||
@@ -249,12 +250,8 @@ impl Read for Peeked {
|
||||
impl Read for Decoder {
|
||||
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
||||
match *self {
|
||||
Decoder::PlainText(ref mut hyper_response) => {
|
||||
hyper_response.read(buf)
|
||||
},
|
||||
Decoder::Gzip{ref mut decoder, ..} => {
|
||||
decoder.read(buf)
|
||||
},
|
||||
Decoder::PlainText(ref mut hyper_response) => hyper_response.read(buf),
|
||||
Decoder::Gzip { ref mut decoder, .. } => decoder.read(buf),
|
||||
Decoder::Errored { ref mut err, .. } => {
|
||||
Err(err.take().unwrap_or_else(previously_errored))
|
||||
}
|
||||
@@ -274,4 +271,3 @@ impl Read for Response {
|
||||
self.inner.read(buf)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
extern crate reqwest;
|
||||
extern crate libflate;
|
||||
|
||||
#[macro_use] mod server;
|
||||
#[macro_use]
|
||||
mod server;
|
||||
|
||||
use std::io::Read;
|
||||
use std::io::prelude::*;
|
||||
@@ -31,8 +32,10 @@ fn test_get() {
|
||||
assert_eq!(res.url().as_str(), &url);
|
||||
assert_eq!(res.status(), &reqwest::StatusCode::Ok);
|
||||
assert_eq!(res.version(), &reqwest::HttpVersion::Http11);
|
||||
assert_eq!(res.headers().get(), Some(&reqwest::header::Server("test".to_string())));
|
||||
assert_eq!(res.headers().get(), Some(&reqwest::header::ContentLength(0)));
|
||||
assert_eq!(res.headers().get(),
|
||||
Some(&reqwest::header::Server("test".to_string())));
|
||||
assert_eq!(res.headers().get(),
|
||||
Some(&reqwest::header::ContentLength(0)));
|
||||
|
||||
let mut buf = [0; 1024];
|
||||
let n = res.read(&mut buf).unwrap();
|
||||
@@ -83,12 +86,11 @@ fn test_redirect_301_and_302_and_303_changes_post_to_get() {
|
||||
|
||||
let url = format!("http://{}/{}", redirect.addr(), code);
|
||||
let dst = format!("http://{}/{}", redirect.addr(), "dst");
|
||||
let res = client.post(&url)
|
||||
.send()
|
||||
.unwrap();
|
||||
let res = client.post(&url).send().unwrap();
|
||||
assert_eq!(res.url().as_str(), dst);
|
||||
assert_eq!(res.status(), &reqwest::StatusCode::Ok);
|
||||
assert_eq!(res.headers().get(), Some(&reqwest::header::Server("test-dst".to_string())));
|
||||
assert_eq!(res.headers().get(),
|
||||
Some(&reqwest::header::Server("test-dst".to_string())));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -138,13 +140,11 @@ fn test_redirect_307_and_308_tries_to_post_again() {
|
||||
|
||||
let url = format!("http://{}/{}", redirect.addr(), code);
|
||||
let dst = format!("http://{}/{}", redirect.addr(), "dst");
|
||||
let res = client.post(&url)
|
||||
.body("Hello")
|
||||
.send()
|
||||
.unwrap();
|
||||
let res = client.post(&url).body("Hello").send().unwrap();
|
||||
assert_eq!(res.url().as_str(), dst);
|
||||
assert_eq!(res.status(), &reqwest::StatusCode::Ok);
|
||||
assert_eq!(res.headers().get(), Some(&reqwest::header::Server("test-dst".to_string())));
|
||||
assert_eq!(res.headers().get(),
|
||||
Some(&reqwest::header::Server("test-dst".to_string())));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -177,7 +177,8 @@ fn test_redirect_307_does_not_try_if_reader_cannot_reset() {
|
||||
};
|
||||
|
||||
let url = format!("http://{}/{}", redirect.addr(), code);
|
||||
let res = client.post(&url)
|
||||
let res = client
|
||||
.post(&url)
|
||||
.body(reqwest::Body::new(&b"Hello"[..]))
|
||||
.send()
|
||||
.unwrap();
|
||||
@@ -226,13 +227,11 @@ fn test_redirect_removes_sensitive_headers() {
|
||||
|
||||
let mut client = reqwest::Client::new().unwrap();
|
||||
client.referer(false);
|
||||
client.get(&format!("http://{}/sensitive", mid_server.addr()))
|
||||
.header(
|
||||
reqwest::header::Cookie(vec![
|
||||
String::from("foo=bar")
|
||||
])
|
||||
)
|
||||
.send().unwrap();
|
||||
client
|
||||
.get(&format!("http://{}/sensitive", mid_server.addr()))
|
||||
.header(reqwest::header::Cookie(vec![String::from("foo=bar")]))
|
||||
.send()
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -282,13 +281,12 @@ fn test_redirect_policy_can_stop_redirects_without_an_error() {
|
||||
client.redirect(reqwest::RedirectPolicy::none());
|
||||
|
||||
let url = format!("http://{}/no-redirect", server.addr());
|
||||
let res = client.get(&url)
|
||||
.send()
|
||||
.unwrap();
|
||||
let res = client.get(&url).send().unwrap();
|
||||
|
||||
assert_eq!(res.url().as_str(), url);
|
||||
assert_eq!(res.status(), &reqwest::StatusCode::Found);
|
||||
assert_eq!(res.headers().get(), Some(&reqwest::header::Server("test-dont".to_string())));
|
||||
assert_eq!(res.headers().get(),
|
||||
Some(&reqwest::header::Server("test-dont".to_string())));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -328,8 +326,10 @@ fn test_referer_is_not_set_if_disabled() {
|
||||
};
|
||||
let mut client = reqwest::Client::new().unwrap();
|
||||
client.referer(false);
|
||||
client.get(&format!("http://{}/no-refer", server.addr()))
|
||||
.send().unwrap();
|
||||
client
|
||||
.get(&format!("http://{}/no-refer", server.addr()))
|
||||
.send()
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -352,7 +352,8 @@ fn test_accept_header_is_not_changed_if_set() {
|
||||
};
|
||||
let client = reqwest::Client::new().unwrap();
|
||||
|
||||
let res = client.get(&format!("http://{}/accept", server.addr()))
|
||||
let res = client
|
||||
.get(&format!("http://{}/accept", server.addr()))
|
||||
.header(reqwest::header::Accept::json())
|
||||
.send()
|
||||
.unwrap();
|
||||
@@ -395,7 +396,7 @@ fn test_gzip_response() {
|
||||
let mut encoder = ::libflate::gzip::Encoder::new(Vec::new()).unwrap();
|
||||
match encoder.write(b"test request") {
|
||||
Ok(n) => assert!(n > 0, "Failed to write to encoder."),
|
||||
_ => panic!("Failed to gzip encode string.")
|
||||
_ => panic!("Failed to gzip encode string."),
|
||||
};
|
||||
|
||||
let gzipped_content = encoder.finish().into_result().unwrap();
|
||||
@@ -420,13 +421,12 @@ fn test_gzip_response() {
|
||||
",
|
||||
response: response
|
||||
};
|
||||
let mut res = reqwest::get(&format!("http://{}/gzip", server.addr()))
|
||||
.unwrap();
|
||||
let mut res = reqwest::get(&format!("http://{}/gzip", server.addr())).unwrap();
|
||||
|
||||
let mut body = ::std::string::String::new();
|
||||
match res.read_to_string(&mut body) {
|
||||
Ok(n) => assert!(n > 0, "Failed to write to buffer."),
|
||||
_ => panic!("Failed to write to buffer.")
|
||||
_ => panic!("Failed to write to buffer."),
|
||||
};
|
||||
|
||||
assert_eq!(body, "test request");
|
||||
@@ -452,7 +452,8 @@ fn test_gzip_empty_body() {
|
||||
};
|
||||
|
||||
let client = reqwest::Client::new().unwrap();
|
||||
let mut res = client.head(&format!("http://{}/gzip", server.addr()))
|
||||
let mut res = client
|
||||
.head(&format!("http://{}/gzip", server.addr()))
|
||||
.send()
|
||||
.unwrap();
|
||||
|
||||
@@ -482,8 +483,7 @@ fn test_gzip_invalid_body() {
|
||||
0"
|
||||
};
|
||||
|
||||
let mut res = reqwest::get(&format!("http://{}/gzip", server.addr()))
|
||||
.unwrap();
|
||||
let mut res = reqwest::get(&format!("http://{}/gzip", server.addr())).unwrap();
|
||||
// this tests that the request.send() didn't error, but that the error
|
||||
// is in reading the body
|
||||
|
||||
|
||||
@@ -14,13 +14,14 @@ impl Server {
|
||||
}
|
||||
}
|
||||
|
||||
static DEFAULT_USER_AGENT: &'static str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));
|
||||
static DEFAULT_USER_AGENT: &'static str =
|
||||
concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));
|
||||
|
||||
pub fn spawn(txns: Vec<(Vec<u8>, Vec<u8>)>) -> Server {
|
||||
let listener = net::TcpListener::bind("127.0.0.1:0").unwrap();
|
||||
let addr = listener.local_addr().unwrap();
|
||||
thread::spawn(move || {
|
||||
for (mut expected, reply) in txns {
|
||||
thread::spawn(
|
||||
move || for (mut expected, reply) in txns {
|
||||
let (mut socket, _addr) = listener.accept().unwrap();
|
||||
replace_expected_vars(&mut expected, addr.to_string().as_ref(), DEFAULT_USER_AGENT.as_ref());
|
||||
let mut buf = [0; 4096];
|
||||
@@ -28,11 +29,11 @@ pub fn spawn(txns: Vec<(Vec<u8>, Vec<u8>)>) -> Server {
|
||||
|
||||
match (::std::str::from_utf8(&expected), ::std::str::from_utf8(&buf[..n])) {
|
||||
(Ok(expected), Ok(received)) => assert_eq!(expected, received),
|
||||
_ => assert_eq!(expected, &buf[..n])
|
||||
_ => assert_eq!(expected, &buf[..n]),
|
||||
}
|
||||
socket.write_all(&reply).unwrap();
|
||||
}
|
||||
});
|
||||
);
|
||||
|
||||
Server {
|
||||
addr: addr,
|
||||
|
||||
Reference in New Issue
Block a user