Merge pull request #75 from imp/response_url

Expose final response URL as it is reported by hyper
This commit is contained in:
Sean McArthur
2017-04-08 15:21:04 -07:00
committed by GitHub
2 changed files with 33 additions and 6 deletions

View File

@@ -4,6 +4,7 @@ use std::io::{self, Read};
use hyper::header::{Headers, ContentEncoding, ContentLength, Encoding, TransferEncoding};
use hyper::status::StatusCode;
use hyper::version::HttpVersion;
use hyper::Url;
use serde::Deserialize;
use serde_json;
@@ -24,13 +25,15 @@ impl fmt::Debug for Response {
return match &self.inner {
&Decoder::PlainText(ref hyper_response) => {
f.debug_struct("Response")
.field("url", &hyper_response.url)
.field("status", &hyper_response.status)
.field("headers", &hyper_response.headers)
.field("version", &hyper_response.version)
.finish()
},
&Decoder::Gzip{ref status, ref version, ref headers, ..} => {
&Decoder::Gzip{ref url, ref status, ref version, ref headers, ..} => {
f.debug_struct("Response")
.field("url", &url)
.field("status", &status)
.field("headers", &headers)
.field("version", &version)
@@ -41,6 +44,15 @@ impl fmt::Debug for Response {
}
impl Response {
/// Get the final `Url` of this response.
#[inline]
pub fn url(&self) -> &Url {
match &self.inner {
&Decoder::PlainText(ref hyper_response) => &hyper_response.url,
&Decoder::Gzip{ref url, ..} => url,
}
}
/// Get the `StatusCode`.
#[inline]
pub fn status(&self) -> &StatusCode {
@@ -81,6 +93,7 @@ enum Decoder {
/// A `Gzip` decoder will uncompress the gziped response content before returning it.
Gzip {
decoder: ::libflate::gzip::Decoder<::hyper::client::Response>,
url: ::hyper::Url,
headers: ::hyper::header::Headers,
version: ::hyper::version::HttpVersion,
status: ::hyper::status::StatusCode,
@@ -121,6 +134,7 @@ impl Decoder {
}
if is_gzip {
return Decoder::Gzip {
url: res.url.clone(),
status: res.status.clone(),
version: res.version.clone(),
headers: res.headers.clone(),

View File

@@ -25,7 +25,10 @@ fn test_get() {
"
};
let mut res = reqwest::get(&format!("http://{}/1", server.addr())).unwrap();
let url = format!("http://{}/1", server.addr());
let mut res = reqwest::get(&url).unwrap();
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())));
@@ -78,9 +81,12 @@ fn test_redirect_301_and_302_and_303_changes_post_to_get() {
"
};
let res = client.post(&format!("http://{}/{}", redirect.addr(), code))
let url = format!("http://{}/{}", redirect.addr(), code);
let dst = format!("http://{}/{}", redirect.addr(), "dst");
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())));
}
@@ -130,10 +136,13 @@ fn test_redirect_307_and_308_tries_to_post_again() {
"
};
let res = client.post(&format!("http://{}/{}", redirect.addr(), code))
let url = format!("http://{}/{}", redirect.addr(), code);
let dst = format!("http://{}/{}", redirect.addr(), "dst");
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())));
}
@@ -167,10 +176,12 @@ fn test_redirect_307_does_not_try_if_reader_cannot_reset() {
", code)
};
let res = client.post(&format!("http://{}/{}", redirect.addr(), code))
let url = format!("http://{}/{}", redirect.addr(), code);
let res = client.post(&url)
.body(reqwest::Body::new(&b"Hello"[..]))
.send()
.unwrap();
assert_eq!(res.url().as_str(), url);
assert_eq!(res.status(), &reqwest::StatusCode::from_u16(code));
}
}
@@ -224,10 +235,12 @@ fn test_redirect_policy_can_stop_redirects_without_an_error() {
let mut client = reqwest::Client::new().unwrap();
client.redirect(reqwest::RedirectPolicy::none());
let res = client.get(&format!("http://{}/no-redirect", server.addr()))
let url = format!("http://{}/no-redirect", server.addr());
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())));
}