diff --git a/src/consts.rs b/src/consts.rs index 9c286b5..4cd0f0d 100644 --- a/src/consts.rs +++ b/src/consts.rs @@ -8,7 +8,7 @@ pub const C_TYPE: [&'static str; 3] = [ pub const SEP: &'static str = "\r\n"; pub const DEF_PORT: u16 = 80; -#[cfg(feature = "native_tls")] +#[cfg(feature = "native-tls")] pub const DEF_SSL_PORT: u16 = 443; pub const DEF_ACCEPT: &'static str = "*/*"; pub const DEF_CONN: &'static str = "close"; diff --git a/src/err.rs b/src/err.rs index 5558f61..bf8d2ac 100644 --- a/src/err.rs +++ b/src/err.rs @@ -1,15 +1,15 @@ extern crate serde_json; -#[cfg(feature = "native_tls")] +#[cfg(feature = "native-tls")] extern crate native_tls; use std::fmt; use std::error; use std::io; -#[cfg(feature = "native_tls")] +#[cfg(feature = "native-tls")] use std::net::TcpStream; use std::num::ParseIntError; use url::ParseError; -#[cfg(feature = "native_tls")] +#[cfg(feature = "native-tls")] use native_tls::HandshakeError; #[derive(Debug)] @@ -17,9 +17,9 @@ pub enum HttpError { Parse(ParseError), IO(io::Error), Json(serde_json::Error), - #[cfg(feature = "native_tls")] + #[cfg(feature = "native-tls")] TLS(native_tls::Error), - #[cfg(feature = "native_tls")] + #[cfg(feature = "native-tls")] SSL(HandshakeError), ParseInt(ParseIntError), MissingFeature(String), @@ -43,14 +43,14 @@ impl From for HttpError { } } -#[cfg(feature = "native_tls")] +#[cfg(feature = "native-tls")] impl From for HttpError { fn from(err: native_tls::Error) -> HttpError { HttpError::TLS(err) } } -#[cfg(feature = "native_tls")] +#[cfg(feature = "native-tls")] impl From> for HttpError { fn from(err: HandshakeError) -> HttpError { HttpError::SSL(err) @@ -69,9 +69,9 @@ impl fmt::Display for HttpError { HttpError::Parse(ref err) => write!(f, "Parse error: {}", err), HttpError::IO(ref err) => write!(f, "Parse error: {}", err), HttpError::Json(ref err) => write!(f, "Parse error: {}", err), - #[cfg(feature = "native_tls")] + #[cfg(feature = "native-tls")] HttpError::TLS(ref err) => write!(f, "Parse error: {}", err), - #[cfg(feature = "native_tls")] + #[cfg(feature = "native-tls")] HttpError::SSL(ref err) => write!(f, "Parse error: {}", err), HttpError::ParseInt(ref err) => write!(f, "Parse error: {}", err), HttpError::MissingFeature(ref err) => write!(f, "Missing feature: {}", err), @@ -85,9 +85,9 @@ impl error::Error for HttpError { HttpError::Parse(ref err) => err.description(), HttpError::IO(ref err) => err.description(), HttpError::Json(ref err) => err.description(), - #[cfg(feature = "native_tls")] + #[cfg(feature = "native-tls")] HttpError::TLS(ref err) => err.description(), - #[cfg(feature = "native_tls")] + #[cfg(feature = "native-tls")] HttpError::SSL(ref err) => err.description(), HttpError::ParseInt(ref err) => err.description(), HttpError::MissingFeature(ref err) => err, @@ -99,9 +99,9 @@ impl error::Error for HttpError { HttpError::Parse(ref err) => Some(err), HttpError::IO(ref err) => Some(err), HttpError::Json(ref err) => Some(err), - #[cfg(feature = "native_tls")] + #[cfg(feature = "native-tls")] HttpError::TLS(ref err) => Some(err), - #[cfg(feature = "native_tls")] + #[cfg(feature = "native-tls")] HttpError::SSL(ref err) => Some(err), HttpError::ParseInt(ref err) => Some(err), HttpError::MissingFeature(ref _err) => None, diff --git a/src/lib.rs b/src/lib.rs index ebf18d3..b717a57 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,7 +4,7 @@ extern crate url; extern crate rand; extern crate serde_json; -#[cfg(feature = "native_tls")] +#[cfg(feature = "native-tls")] extern crate native_tls; use std::net::TcpStream; @@ -19,7 +19,7 @@ use url::{Url, ParseError}; use consts::*; use err::HttpError; use response::*; -#[cfg(feature = "native_tls")] +#[cfg(feature = "native-tls")] use native_tls::TlsConnector; mod err; @@ -257,7 +257,7 @@ impl HTTP { stream.write_all(request.as_bytes())?; stream.read_to_string(&mut self.response_str)?; } else { - self.tls_transport(url)?; + self.response_str = self.tls_transport(request, url)?; } response = self.response_str.clone(); @@ -265,8 +265,8 @@ impl HTTP { Ok(resp) } - #[cfg(feature = "native_tls")] - fn tls_transport(&self, url: &str) -> Result<(), HttpError> { + #[cfg(feature = "native-tls")] + fn tls_transport(&self, request: String, url: &str) -> Result { let port = match self.url.port() { Some(p) => p, None => DEF_SSL_PORT, @@ -277,13 +277,15 @@ impl HTTP { let mut stream = connector.connect(&self.host, stream)?; stream.write_all(request.as_bytes())?; - stream.read_to_string(&mut self.response_str)?; + let mut buf = String::new(); + stream.read_to_string(&mut buf)?; + Ok(buf) } - #[cfg(not(feature = "native_tls"))] - fn tls_transport(&self, _url: &str) -> Result<(), HttpError> { + #[cfg(not(feature = "native-tls"))] + fn tls_transport(&self, _request: String, _url: &str) -> Result { Err(HttpError::MissingFeature( - "Lib not compiled with feature native_tls active".into(), + "Lib not compiled with feature native-tls active".into(), )) } /// Create Reqeust String diff --git a/tests/unit.rs b/tests/unit.rs index 9574c11..11bb8f7 100644 --- a/tests/unit.rs +++ b/tests/unit.rs @@ -44,6 +44,15 @@ mod test { let response = http.get().send().unwrap(); let string = response.as_str(); + assert!(!string.is_empty(), "Response shouldn't be empty"); + } + #[cfg(feature = "native-tls")] + #[test] + fn get_tls_response() { + let mut http = HTTP::new("https://google.com/").unwrap(); + let response = http.get().send().unwrap(); + let string = response.as_str(); + assert!(!string.is_empty(), "Response shouldn't be empty"); } }