cargo fmt
This commit is contained in:
@@ -324,7 +324,6 @@ impl Agent {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn basic_auth(user: &str, pass: &str) -> String {
|
fn basic_auth(user: &str, pass: &str) -> String {
|
||||||
let safe = match user.find(":") {
|
let safe = match user.find(":") {
|
||||||
Some(idx) => &user[..idx],
|
Some(idx) => &user[..idx],
|
||||||
@@ -333,7 +332,6 @@ fn basic_auth(user: &str, pass: &str) -> String {
|
|||||||
::base64::encode(&format!("{}:{}", safe, pass))
|
::base64::encode(&format!("{}:{}", safe, pass))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn mime_of<S: Into<String>>(s: S) -> String {
|
fn mime_of<S: Into<String>>(s: S) -> String {
|
||||||
let s = s.into();
|
let s = s.into();
|
||||||
match &s[..] {
|
match &s[..] {
|
||||||
@@ -346,7 +344,6 @@ fn mime_of<S: Into<String>>(s: S) -> String {
|
|||||||
}.to_string()
|
}.to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use std::io::Error as IoError;
|
|
||||||
use native_tls::Error as TlsError;
|
use native_tls::Error as TlsError;
|
||||||
use native_tls::HandshakeError;
|
use native_tls::HandshakeError;
|
||||||
|
use std::io::Error as IoError;
|
||||||
use std::net::TcpStream;
|
use std::net::TcpStream;
|
||||||
|
|
||||||
/// Errors that are translated to ["synthetic" responses](struct.Response.html#method.synthetic).
|
/// Errors that are translated to ["synthetic" responses](struct.Response.html#method.synthetic).
|
||||||
@@ -29,7 +29,6 @@ pub enum Error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Error {
|
impl Error {
|
||||||
|
|
||||||
/// For synthetic responses, this is the error code.
|
/// For synthetic responses, this is the error code.
|
||||||
pub fn status(&self) -> u16 {
|
pub fn status(&self) -> u16 {
|
||||||
match self {
|
match self {
|
||||||
@@ -52,7 +51,7 @@ impl Error {
|
|||||||
Error::BadUrl(e) => {
|
Error::BadUrl(e) => {
|
||||||
println!("{}", e);
|
println!("{}", e);
|
||||||
"Bad URL"
|
"Bad URL"
|
||||||
},
|
}
|
||||||
Error::UnknownScheme(_) => "Unknown Scheme",
|
Error::UnknownScheme(_) => "Unknown Scheme",
|
||||||
Error::DnsFailed(_) => "Dns Failed",
|
Error::DnsFailed(_) => "Dns Failed",
|
||||||
Error::ConnectionFailed(_) => "Connection Failed",
|
Error::ConnectionFailed(_) => "Connection Failed",
|
||||||
|
|||||||
@@ -54,9 +54,9 @@ extern crate encoding;
|
|||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate lazy_static;
|
extern crate lazy_static;
|
||||||
extern crate mime_guess;
|
extern crate mime_guess;
|
||||||
|
extern crate native_tls;
|
||||||
extern crate qstring;
|
extern crate qstring;
|
||||||
extern crate serde_json;
|
extern crate serde_json;
|
||||||
extern crate native_tls;
|
|
||||||
extern crate url;
|
extern crate url;
|
||||||
|
|
||||||
mod agent;
|
mod agent;
|
||||||
@@ -69,12 +69,12 @@ mod serde_macros;
|
|||||||
mod test;
|
mod test;
|
||||||
|
|
||||||
pub use agent::{Agent, Request, Response};
|
pub use agent::{Agent, Request, Response};
|
||||||
pub use header::Header;
|
|
||||||
pub use error::Error;
|
pub use error::Error;
|
||||||
|
pub use header::Header;
|
||||||
|
|
||||||
// re-export
|
// re-export
|
||||||
pub use serde_json::{to_value as serde_to_value, Map as SerdeMap, Value as SerdeValue};
|
|
||||||
pub use cookie::Cookie;
|
pub use cookie::Cookie;
|
||||||
|
pub use serde_json::{to_value as serde_to_value, Map as SerdeMap, Value as SerdeValue};
|
||||||
|
|
||||||
/// Agents are used to keep state between requests.
|
/// Agents are used to keep state between requests.
|
||||||
pub fn agent() -> Agent {
|
pub fn agent() -> Agent {
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
/// Create a `HashMap` from a shorthand notation.
|
/// Create a `HashMap` from a shorthand notation.
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
// this file is borrowed in its entirety until macro_reexport stabilizes.
|
// this file is borrowed in its entirety until macro_reexport stabilizes.
|
||||||
// https://github.com/rust-lang/rust/issues/29638
|
// https://github.com/rust-lang/rust/issues/29638
|
||||||
|
|
||||||
|
|||||||
@@ -4,9 +4,7 @@ use super::super::*;
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn agent_reuse_headers() {
|
fn agent_reuse_headers() {
|
||||||
let agent = agent()
|
let agent = agent().set("Authorization", "Foo 12345").build();
|
||||||
.set("Authorization", "Foo 12345")
|
|
||||||
.build();
|
|
||||||
|
|
||||||
test::set_handler("/agent_reuse_headers", |req, _url| {
|
test::set_handler("/agent_reuse_headers", |req, _url| {
|
||||||
assert!(req.has("Authorization"));
|
assert!(req.has("Authorization"));
|
||||||
@@ -27,14 +25,17 @@ fn agent_reuse_headers() {
|
|||||||
assert_eq!(resp.header("X-Call").unwrap(), "2");
|
assert_eq!(resp.header("X-Call").unwrap(), "2");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn agent_cookies() {
|
fn agent_cookies() {
|
||||||
let agent = agent()
|
let agent = agent().build();
|
||||||
.build();
|
|
||||||
|
|
||||||
test::set_handler("/agent_cookies", |_req, _url| {
|
test::set_handler("/agent_cookies", |_req, _url| {
|
||||||
test::make_response(200, "OK", vec!["Set-Cookie: foo=bar%20baz; Path=/; HttpOnly"], vec![])
|
test::make_response(
|
||||||
|
200,
|
||||||
|
"OK",
|
||||||
|
vec!["Set-Cookie: foo=bar%20baz; Path=/; HttpOnly"],
|
||||||
|
vec![],
|
||||||
|
)
|
||||||
});
|
});
|
||||||
|
|
||||||
agent.get("test://host/agent_cookies").call();
|
agent.get("test://host/agent_cookies").call();
|
||||||
@@ -52,5 +53,4 @@ fn agent_cookies() {
|
|||||||
let s = String::from_utf8_lossy(&vec);
|
let s = String::from_utf8_lossy(&vec);
|
||||||
|
|
||||||
assert!(s.contains("Cookie: foo=bar%20baz\r\n"));
|
assert!(s.contains("Cookie: foo=bar%20baz\r\n"));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,10 @@ use super::super::*;
|
|||||||
#[test]
|
#[test]
|
||||||
fn basic_auth() {
|
fn basic_auth() {
|
||||||
test::set_handler("/basic_auth", |req, _url| {
|
test::set_handler("/basic_auth", |req, _url| {
|
||||||
assert_eq!(req.header("Authorization").unwrap(), "Basic bWFydGluOnJ1YmJlcm1hc2hndW0=");
|
assert_eq!(
|
||||||
|
req.header("Authorization").unwrap(),
|
||||||
|
"Basic bWFydGluOnJ1YmJlcm1hc2hndW0="
|
||||||
|
);
|
||||||
test::make_response(200, "OK", vec![], vec![])
|
test::make_response(200, "OK", vec![], vec![])
|
||||||
});
|
});
|
||||||
let resp = get("test://host/basic_auth")
|
let resp = get("test://host/basic_auth")
|
||||||
|
|||||||
@@ -49,7 +49,9 @@ fn content_length_limited() {
|
|||||||
200,
|
200,
|
||||||
"OK",
|
"OK",
|
||||||
vec!["Content-Length: 4", "transfer-encoding: chunked"],
|
vec!["Content-Length: 4", "transfer-encoding: chunked"],
|
||||||
"3\r\nhel\r\nb\r\nlo world!!!\r\n0\r\n\r\n".to_string().into_bytes(),
|
"3\r\nhel\r\nb\r\nlo world!!!\r\n0\r\n\r\n"
|
||||||
|
.to_string()
|
||||||
|
.into_bytes(),
|
||||||
)
|
)
|
||||||
});
|
});
|
||||||
let resp = get("test://host/ignore_content_length_when_chunked").call();
|
let resp = get("test://host/ignore_content_length_when_chunked").call();
|
||||||
|
|||||||
@@ -7,8 +7,7 @@ fn content_length_on_str() {
|
|||||||
test::set_handler("/content_length_on_str", |_req, _url| {
|
test::set_handler("/content_length_on_str", |_req, _url| {
|
||||||
test::make_response(200, "OK", vec![], vec![])
|
test::make_response(200, "OK", vec![], vec![])
|
||||||
});
|
});
|
||||||
let resp = get("test://host/content_length_on_str")
|
let resp = post("test://host/content_length_on_str").send_string("Hello World!!!");
|
||||||
.send_string("Hello World!!!");
|
|
||||||
let vec = resp.to_write_vec();
|
let vec = resp.to_write_vec();
|
||||||
let s = String::from_utf8_lossy(&vec);
|
let s = String::from_utf8_lossy(&vec);
|
||||||
assert!(s.contains("\r\nContent-Length: 14\r\n"));
|
assert!(s.contains("\r\nContent-Length: 14\r\n"));
|
||||||
@@ -19,7 +18,7 @@ fn user_set_content_length_on_str() {
|
|||||||
test::set_handler("/user_set_content_length_on_str", |_req, _url| {
|
test::set_handler("/user_set_content_length_on_str", |_req, _url| {
|
||||||
test::make_response(200, "OK", vec![], vec![])
|
test::make_response(200, "OK", vec![], vec![])
|
||||||
});
|
});
|
||||||
let resp = get("test://host/user_set_content_length_on_str")
|
let resp = post("test://host/user_set_content_length_on_str")
|
||||||
.set("Content-Length", "12345")
|
.set("Content-Length", "12345")
|
||||||
.send_string("Hello World!!!");
|
.send_string("Hello World!!!");
|
||||||
let vec = resp.to_write_vec();
|
let vec = resp.to_write_vec();
|
||||||
@@ -33,9 +32,11 @@ fn content_length_on_json() {
|
|||||||
test::make_response(200, "OK", vec![], vec![])
|
test::make_response(200, "OK", vec![], vec![])
|
||||||
});
|
});
|
||||||
let mut json = SerdeMap::new();
|
let mut json = SerdeMap::new();
|
||||||
json.insert("Hello".to_string(), SerdeValue::String("World!!!".to_string()));
|
json.insert(
|
||||||
let resp = get("test://host/content_length_on_json")
|
"Hello".to_string(),
|
||||||
.send_json(SerdeValue::Object(json));
|
SerdeValue::String("World!!!".to_string()),
|
||||||
|
);
|
||||||
|
let resp = post("test://host/content_length_on_json").send_json(SerdeValue::Object(json));
|
||||||
let vec = resp.to_write_vec();
|
let vec = resp.to_write_vec();
|
||||||
let s = String::from_utf8_lossy(&vec);
|
let s = String::from_utf8_lossy(&vec);
|
||||||
assert!(s.contains("\r\nContent-Length: 20\r\n"));
|
assert!(s.contains("\r\nContent-Length: 20\r\n"));
|
||||||
|
|||||||
@@ -3,10 +3,10 @@ use agent::Stream;
|
|||||||
use error::Error;
|
use error::Error;
|
||||||
use header::Header;
|
use header::Header;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
use std::io::Cursor;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
use url::Url;
|
use url::Url;
|
||||||
use std::io::Cursor;
|
|
||||||
|
|
||||||
mod agent_test;
|
mod agent_test;
|
||||||
mod auth;
|
mod auth;
|
||||||
|
|||||||
@@ -7,8 +7,7 @@ fn no_query_string() {
|
|||||||
test::set_handler("/no_query_string", |_req, _url| {
|
test::set_handler("/no_query_string", |_req, _url| {
|
||||||
test::make_response(200, "OK", vec![], vec![])
|
test::make_response(200, "OK", vec![], vec![])
|
||||||
});
|
});
|
||||||
let resp = get("test://host/no_query_string")
|
let resp = get("test://host/no_query_string").call();
|
||||||
.call();
|
|
||||||
let vec = resp.to_write_vec();
|
let vec = resp.to_write_vec();
|
||||||
let s = String::from_utf8_lossy(&vec);
|
let s = String::from_utf8_lossy(&vec);
|
||||||
assert!(s.contains("GET /no_query_string HTTP/1.1"))
|
assert!(s.contains("GET /no_query_string HTTP/1.1"))
|
||||||
|
|||||||
@@ -35,10 +35,10 @@ fn repeat_x_header() {
|
|||||||
test::set_handler("/repeat_x_header", |req, _url| {
|
test::set_handler("/repeat_x_header", |req, _url| {
|
||||||
assert!(req.has("X-Forwarded-For"));
|
assert!(req.has("X-Forwarded-For"));
|
||||||
assert_eq!(req.header("X-Forwarded-For").unwrap(), "130.240.19.2");
|
assert_eq!(req.header("X-Forwarded-For").unwrap(), "130.240.19.2");
|
||||||
assert_eq!(req.all("X-Forwarded-For"), vec![
|
assert_eq!(
|
||||||
"130.240.19.2",
|
req.all("X-Forwarded-For"),
|
||||||
"130.240.19.3",
|
vec!["130.240.19.2", "130.240.19.3"]
|
||||||
]);
|
);
|
||||||
test::make_response(200, "OK", vec![], vec![])
|
test::make_response(200, "OK", vec![], vec![])
|
||||||
});
|
});
|
||||||
let resp = get("test://host/repeat_x_header")
|
let resp = get("test://host/repeat_x_header")
|
||||||
|
|||||||
Reference in New Issue
Block a user