get -> header
This commit is contained in:
@@ -60,7 +60,7 @@ impl ConnectionPool {
|
||||
}
|
||||
|
||||
// the location header
|
||||
let location = resp.get("location");
|
||||
let location = resp.header("location");
|
||||
if let Some(location) = location {
|
||||
// join location header to current url in case it it relative
|
||||
let new_url = url.join(location)
|
||||
@@ -155,14 +155,14 @@ fn send_payload(request: &Request, payload: Payload, stream: &mut Stream) -> IoR
|
||||
//
|
||||
let (size, reader) = payload.into_read();
|
||||
|
||||
let do_chunk = request.get("transfer-encoding")
|
||||
let do_chunk = request.header("transfer-encoding")
|
||||
// if the user has set an encoding header, obey that.
|
||||
.map(|enc| enc.eq_ignore_ascii_case("chunked"))
|
||||
// if the content has a size
|
||||
.ok_or_else(|| size.
|
||||
// or if the user set a content-length header
|
||||
or_else(||
|
||||
request.get("content-length").map(|len| len.parse::<usize>().unwrap_or(0)))
|
||||
request.header("content-length").map(|len| len.parse::<usize>().unwrap_or(0)))
|
||||
// and that size is larger than 1MB, chunk,
|
||||
.map(|size| size > CHUNK_SIZE))
|
||||
// otherwise, assume chunking since it can be really big.
|
||||
|
||||
@@ -128,19 +128,19 @@ where
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
//#[test]
|
||||
#[test]
|
||||
fn connect_http_google() {
|
||||
let resp = get("http://www.google.com/").call();
|
||||
println!("{:?}", resp);
|
||||
assert_eq!("text/html; charset=ISO-8859-1", resp.get("content-type").unwrap());
|
||||
assert_eq!("text/html; charset=ISO-8859-1", resp.header("content-type").unwrap());
|
||||
assert_eq!("text/html", resp.content_type());
|
||||
}
|
||||
|
||||
//#[test]
|
||||
#[test]
|
||||
fn connect_https_google() {
|
||||
let resp = get("https://www.google.com/").call();
|
||||
println!("{:?}", resp);
|
||||
assert_eq!("text/html; charset=ISO-8859-1", resp.get("content-type").unwrap());
|
||||
assert_eq!("text/html; charset=ISO-8859-1", resp.header("content-type").unwrap());
|
||||
assert_eq!("text/html", resp.content_type());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -189,9 +189,9 @@ impl Request {
|
||||
/// let req = ureq::get("/my_page")
|
||||
/// .set("X-API-Key", "foobar")
|
||||
/// .build();
|
||||
/// assert_eq!("foobar", req.get("x-api-Key").unwrap());
|
||||
/// assert_eq!("foobar", req.header("x-api-Key").unwrap());
|
||||
/// ```
|
||||
pub fn get<'a>(&self, name: &'a str) -> Option<&str> {
|
||||
pub fn header<'a>(&self, name: &'a str) -> Option<&str> {
|
||||
self.headers
|
||||
.iter()
|
||||
.find(|h| h.is_name(name))
|
||||
@@ -207,7 +207,7 @@ impl Request {
|
||||
/// assert_eq!(true, req.has("x-api-Key"));
|
||||
/// ```
|
||||
pub fn has<'a>(&self, name: &'a str) -> bool {
|
||||
self.get(name).is_some()
|
||||
self.header(name).is_some()
|
||||
}
|
||||
|
||||
/// All headers corresponding values for the give name, or empty vector.
|
||||
@@ -217,12 +217,12 @@ impl Request {
|
||||
/// .set("X-Forwarded-For", "1.2.3.4")
|
||||
/// .set("X-Forwarded-For", "2.3.4.5")
|
||||
/// .build();
|
||||
/// assert_eq!(req.get_all("x-forwarded-for"), vec![
|
||||
/// assert_eq!(req.all("x-forwarded-for"), vec![
|
||||
/// "1.2.3.4",
|
||||
/// "2.3.4.5",
|
||||
/// ]);
|
||||
/// ```
|
||||
pub fn get_all<'a>(&self, name: &'a str) -> Vec<&str> {
|
||||
pub fn all<'a>(&self, name: &'a str) -> Vec<&str> {
|
||||
self.headers
|
||||
.iter()
|
||||
.filter(|h| h.is_name(name))
|
||||
|
||||
@@ -53,7 +53,7 @@ impl Response {
|
||||
}
|
||||
|
||||
/// The header corresponding header value for the give name, if any.
|
||||
pub fn get<'a>(&self, name: &'a str) -> Option<&str> {
|
||||
pub fn header<'a>(&self, name: &'a str) -> Option<&str> {
|
||||
self.headers
|
||||
.iter()
|
||||
.find(|h| h.is_name(name))
|
||||
@@ -62,11 +62,11 @@ impl Response {
|
||||
|
||||
/// Tells if the response has the named header.
|
||||
pub fn has<'a>(&self, name: &'a str) -> bool {
|
||||
self.get(name).is_some()
|
||||
self.header(name).is_some()
|
||||
}
|
||||
|
||||
/// All headers corresponding values for the give name, or empty vector.
|
||||
pub fn get_all<'a>(&self, name: &'a str) -> Vec<&str> {
|
||||
pub fn all<'a>(&self, name: &'a str) -> Vec<&str> {
|
||||
self.headers
|
||||
.iter()
|
||||
.filter(|h| h.is_name(name))
|
||||
@@ -105,11 +105,11 @@ impl Response {
|
||||
///
|
||||
/// ```
|
||||
/// let resp = ureq::get("https://www.google.com/").call();
|
||||
/// assert_eq!("text/html; charset=ISO-8859-1", resp.get("content-type").unwrap());
|
||||
/// assert_eq!("text/html; charset=ISO-8859-1", resp.header("content-type").unwrap());
|
||||
/// assert_eq!("text/html", resp.content_type());
|
||||
/// ```
|
||||
pub fn content_type(&self) -> &str {
|
||||
self.get("content-type")
|
||||
self.header("content-type")
|
||||
.map(|header| {
|
||||
header
|
||||
.find(";")
|
||||
@@ -119,7 +119,7 @@ impl Response {
|
||||
.unwrap_or(DEFAULT_CONTENT_TYPE)
|
||||
}
|
||||
pub fn charset(&self) -> &str {
|
||||
self.get("content-type")
|
||||
self.header("content-type")
|
||||
.and_then(|header| {
|
||||
header.find(";").and_then(|semi| {
|
||||
(&header[semi + 1..])
|
||||
@@ -131,10 +131,10 @@ impl Response {
|
||||
}
|
||||
|
||||
pub fn into_reader(self) -> impl Read {
|
||||
let is_chunked = self.get("transfer-encoding")
|
||||
let is_chunked = self.header("transfer-encoding")
|
||||
.map(|enc| enc.len() > 0) // whatever it says, do chunked
|
||||
.unwrap_or(false);
|
||||
let len = self.get("content-length").and_then(|l| l.parse::<usize>().ok());
|
||||
let len = self.header("content-length").and_then(|l| l.parse::<usize>().ok());
|
||||
let reader = self.reader.expect("No reader in response?!");
|
||||
match is_chunked {
|
||||
true => Box::new(chunked_transfer::Decoder::new(reader)),
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
use test;
|
||||
|
||||
use super::super::*;
|
||||
|
||||
#[test]
|
||||
fn agent_reuse_headers() {
|
||||
let agent = agent()
|
||||
.set("Authorization", "Foo 12345")
|
||||
.build();
|
||||
|
||||
test::set_handler("/agent_reuse_headers", |req, _url| {
|
||||
assert!(req.has("Authorization"));
|
||||
assert_eq!(req.get("Authorization").unwrap(), "Foo 12345");
|
||||
test::make_stream(200, "OK", vec!["X-Call: 1"], vec![])
|
||||
});
|
||||
|
||||
let resp = agent.get("test://host/agent_reuse_headers").call();
|
||||
assert_eq!(resp.get("X-Call").unwrap(), "1");
|
||||
|
||||
test::set_handler("/agent_reuse_headers", |req, _url| {
|
||||
assert!(req.has("Authorization"));
|
||||
assert_eq!(req.get("Authorization").unwrap(), "Foo 12345");
|
||||
test::make_stream(200, "OK", vec!["X-Call: 2"], vec![])
|
||||
});
|
||||
|
||||
let resp = agent.get("test://host/agent_reuse_headers").call();
|
||||
assert_eq!(resp.get("X-Call").unwrap(), "2");
|
||||
}
|
||||
52
src/test/agent_test.rs
Normal file
52
src/test/agent_test.rs
Normal file
@@ -0,0 +1,52 @@
|
||||
use test;
|
||||
|
||||
use super::super::*;
|
||||
|
||||
#[test]
|
||||
fn agent_reuse_headers() {
|
||||
let agent = agent()
|
||||
.set("Authorization", "Foo 12345")
|
||||
.build();
|
||||
|
||||
test::set_handler("/agent_reuse_headers", |req, _url| {
|
||||
assert!(req.has("Authorization"));
|
||||
assert_eq!(req.header("Authorization").unwrap(), "Foo 12345");
|
||||
test::make_stream(200, "OK", vec!["X-Call: 1"], vec![])
|
||||
});
|
||||
|
||||
let resp = agent.get("test://host/agent_reuse_headers").call();
|
||||
assert_eq!(resp.header("X-Call").unwrap(), "1");
|
||||
|
||||
test::set_handler("/agent_reuse_headers", |req, _url| {
|
||||
assert!(req.has("Authorization"));
|
||||
assert_eq!(req.header("Authorization").unwrap(), "Foo 12345");
|
||||
test::make_stream(200, "OK", vec!["X-Call: 2"], vec![])
|
||||
});
|
||||
|
||||
let resp = agent.get("test://host/agent_reuse_headers").call();
|
||||
assert_eq!(resp.header("X-Call").unwrap(), "2");
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn agent_cookies() {
|
||||
let agent = agent()
|
||||
.build();
|
||||
|
||||
test::set_handler("/agent_cookies", |_req, _url| {
|
||||
test::make_stream(200, "OK", vec!["Set-Cookie: foo=bar; Path=/; HttpOnly"], vec![])
|
||||
});
|
||||
|
||||
agent.get("test://host/agent_cookies").call();
|
||||
|
||||
assert!(agent.cookie("foo").is_some());
|
||||
assert_eq!(agent.cookie("foo").unwrap().value(), "bar");
|
||||
|
||||
test::set_handler("/agent_cookies", |req, _url| {
|
||||
assert_eq!(req.header("Cookie").unwrap(), "");
|
||||
test::make_stream(200, "OK", vec![], vec![])
|
||||
});
|
||||
|
||||
agent.get("test://host/agent_cookies").call();
|
||||
|
||||
}
|
||||
@@ -5,7 +5,7 @@ use super::super::*;
|
||||
#[test]
|
||||
fn basic_auth() {
|
||||
test::set_handler("/basic_auth", |req, _url| {
|
||||
assert_eq!(req.get("Authorization").unwrap(), "Basic bWFydGluOnJ1YmJlcm1hc2hndW0=");
|
||||
assert_eq!(req.header("Authorization").unwrap(), "Basic bWFydGluOnJ1YmJlcm1hc2hndW0=");
|
||||
test::make_stream(200, "OK", vec![], vec![])
|
||||
});
|
||||
let resp = get("test://host/basic_auth")
|
||||
@@ -17,7 +17,7 @@ fn basic_auth() {
|
||||
#[test]
|
||||
fn kind_auth() {
|
||||
test::set_handler("/kind_auth", |req, _url| {
|
||||
assert_eq!(req.get("Authorization").unwrap(), "Digest abcdefgh123");
|
||||
assert_eq!(req.header("Authorization").unwrap(), "Digest abcdefgh123");
|
||||
test::make_stream(200, "OK", vec![], vec![])
|
||||
});
|
||||
let resp = get("test://host/kind_auth")
|
||||
|
||||
@@ -8,7 +8,7 @@ use stream::Stream;
|
||||
use url::Url;
|
||||
use util::vecread::VecRead;
|
||||
|
||||
mod agent;
|
||||
mod agent_test;
|
||||
mod auth;
|
||||
mod simple;
|
||||
mod body_read;
|
||||
|
||||
@@ -7,20 +7,20 @@ use super::super::*;
|
||||
fn header_passing() {
|
||||
test::set_handler("/header_passing", |req, _url| {
|
||||
assert!(req.has("X-Foo"));
|
||||
assert_eq!(req.get("X-Foo").unwrap(), "bar");
|
||||
assert_eq!(req.header("X-Foo").unwrap(), "bar");
|
||||
test::make_stream(200, "OK", vec!["X-Bar: foo"], vec![])
|
||||
});
|
||||
let resp = get("test://host/header_passing").set("X-Foo", "bar").call();
|
||||
assert_eq!(*resp.status(), 200);
|
||||
assert!(resp.has("X-Bar"));
|
||||
assert_eq!(resp.get("X-Bar").unwrap(), "foo");
|
||||
assert_eq!(resp.header("X-Bar").unwrap(), "foo");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn repeat_non_x_header() {
|
||||
test::set_handler("/repeat_non_x_header", |req, _url| {
|
||||
assert!(req.has("Accept"));
|
||||
assert_eq!(req.get("Accept").unwrap(), "baz");
|
||||
assert_eq!(req.header("Accept").unwrap(), "baz");
|
||||
test::make_stream(200, "OK", vec![], vec![])
|
||||
});
|
||||
let resp = get("test://host/repeat_non_x_header")
|
||||
@@ -34,8 +34,8 @@ fn repeat_non_x_header() {
|
||||
fn repeat_x_header() {
|
||||
test::set_handler("/repeat_x_header", |req, _url| {
|
||||
assert!(req.has("X-Forwarded-For"));
|
||||
assert_eq!(req.get("X-Forwarded-For").unwrap(), "130.240.19.2");
|
||||
assert_eq!(req.get_all("X-Forwarded-For"), vec![
|
||||
assert_eq!(req.header("X-Forwarded-For").unwrap(), "130.240.19.2");
|
||||
assert_eq!(req.all("X-Forwarded-For"), vec![
|
||||
"130.240.19.2",
|
||||
"130.240.19.3",
|
||||
]);
|
||||
|
||||
Reference in New Issue
Block a user