Repository: algesten/ureq
Pull request #75 (2020-06-20 22:27:19)

Fix up cargo test --no-default-features.
Adds some feature guards, and removes an unnecessary feature guard
around a call to connect_https (there's an implementation available for
non-TLS that returns UnknownScheme).

Also, remove unnecessary agent.state() method that was only available in
TLS builds. The state field is directly accessible within the crate, and
can be used in both TLS and non-TLS builds.


Patch:
diff --git a/src/agent.rs b/src/agent.rs
index 3d5839e..ad8266e 100644
--- a/src/agent.rs
+++ b/src/agent.rs
@@ -249,11 +249,6 @@ impl Agent {
     pub fn patch(&self, path: &str) -> Request {
         self.request("PATCH", path)
     }
-
-    #[cfg(all(test, any(feature = "tls", feature = "native-tls")))]
-    pub(crate) fn state(&self) -> &Arc<Mutex<Option<AgentState>>> {
-        &self.state
-    }
 }
 
 pub(crate) fn basic_auth(user: &str, pass: &str) -> String {
diff --git a/src/pool.rs b/src/pool.rs
index 0b87dab..0fe8a56 100644
--- a/src/pool.rs
+++ b/src/pool.rs
@@ -29,7 +29,7 @@ impl ConnectionPool {
         self.recycle.remove(&PoolKey::new(url))
     }
 
-    #[cfg(all(test, any(feature = "tls", feature = "native-tls")))]
+    #[cfg(test)]
     pub fn len(&self) -> usize {
         self.recycle.len()
     }
diff --git a/src/response.rs b/src/response.rs
index ba14762..aba2c00 100644
--- a/src/response.rs
+++ b/src/response.rs
@@ -218,9 +218,11 @@ impl Response {
     /// Example:
     ///
     /// ```
+    /// # #[cfg(feature = "tls")] {
     /// let resp = ureq::get("https://www.google.com/").call();
     /// 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.header("content-type")
@@ -238,9 +240,11 @@ impl Response {
     /// Example:
     ///
     /// ```
+    /// # #[cfg(feature = "tls")] {
     /// let resp = ureq::get("https://www.google.com/").call();
     /// assert_eq!("text/html; charset=ISO-8859-1", resp.header("content-type").unwrap());
     /// assert_eq!("ISO-8859-1", resp.charset());
+    /// # }
     /// ```
     pub fn charset(&self) -> &str {
         charset_from_content_type(self.header("content-type"))
@@ -257,6 +261,7 @@ impl Response {
     /// Example:
     ///
     /// ```
+    /// # #[cfg(feature = "tls")] {
     /// use std::io::Read;
     ///
     /// let resp =
@@ -272,6 +277,7 @@ impl Response {
     /// reader.read_to_end(&mut bytes);
     ///
     /// assert_eq!(bytes.len(), len);
+    /// # }
     /// ```
     pub fn into_reader(self) -> impl Read {
         //
@@ -331,6 +337,7 @@ impl Response {
     /// Example:
     ///
     /// ```
+    /// # #[cfg(feature = "tls")] {
     /// let resp =
     ///     ureq::get("https://ureq.s3.eu-central-1.amazonaws.com/hello_world.json")
     ///         .call();
@@ -338,6 +345,7 @@ impl Response {
     /// let text = resp.into_string().unwrap();
     ///
     /// assert!(text.contains("hello"));
+    /// # }
     /// ```
     ///
     /// ## Charset support
diff --git a/src/stream.rs b/src/stream.rs
index bd6c856..f7b4088 100644
--- a/src/stream.rs
+++ b/src/stream.rs
@@ -193,6 +193,7 @@ where
     }
 }
 
+#[cfg(all(feature = "tls", not(feature = "native-tls")))]
 fn read_https(
     stream: &mut StreamOwned<ClientSession, TcpStream>,
     buf: &mut [u8],
diff --git a/src/unit.rs b/src/unit.rs
index 9a2a331..2cca994 100644
--- a/src/unit.rs
+++ b/src/unit.rs
@@ -11,11 +11,6 @@ use cookie::{Cookie, CookieJar};
 use crate::agent::AgentState;
 use crate::body::{self, Payload, SizedReader};
 use crate::header;
-#[cfg(any(
-    all(feature = "tls", not(feature = "native-tls")),
-    all(feature = "native-tls", not(feature = "tls")),
-))]
-use crate::stream::connect_https;
 use crate::stream::{self, connect_test, Stream};
 use crate::Proxy;
 use crate::{Error, Header, Request, Response};
@@ -303,11 +298,7 @@ fn connect_socket(unit: &Unit, use_pooled: bool) -> Result<(Stream, bool), Error
     }
     let stream = match unit.url.scheme() {
         "http" => stream::connect_http(&unit),
-        #[cfg(any(
-            all(feature = "tls", not(feature = "native-tls")),
-            all(feature = "native-tls", not(feature = "tls")),
-        ))]
-        "https" => connect_https(&unit),
+        "https" => stream::connect_https(&unit),
         "test" => connect_test(&unit),
         _ => Err(Error::UnknownScheme(unit.url.scheme().to_string())),
     };


Test patch:
diff --git a/src/test/agent_test.rs b/src/test/agent_test.rs
index fa70f9e..55934b9 100644
--- a/src/test/agent_test.rs
+++ b/src/test/agent_test.rs
@@ -99,7 +99,7 @@ fn connection_reuse() {
     resp.into_reader().read_to_end(&mut buf).unwrap();
 
     {
-        let mut guard_state = agent.state().lock().unwrap();
+        let mut guard_state = agent.state.lock().unwrap();
         let mut state = guard_state.take().unwrap();
         assert!(state.pool().len() > 0);
     }
diff --git a/src/test/range.rs b/src/test/range.rs
index a73acee..9c4adbc 100644
--- a/src/test/range.rs
+++ b/src/test/range.rs
@@ -39,7 +39,7 @@ fn agent_pool() {
     assert_eq!(len, 1000);
 
     {
-        let mut lock = agent.state().lock().unwrap();
+        let mut lock = agent.state.lock().unwrap();
         let state = lock.as_mut().unwrap();
         let pool = state.pool();
         assert_eq!(pool.len(), 1);