Repository: algesten/ureq
Pull request #22 (2019-10-20 21:19:22)

Fix compilation errors with the cookies feature
Sorry! I thought it was a bit suspicious that the tests passed first time.

I've made the tests run without `default_features`, but they only pass if both `tls` and `json` are enabled. So I've made them always run.


Patch:
diff --git a/src/unit.rs b/src/unit.rs
index e1a222f..4f8a1b8 100644
--- a/src/unit.rs
+++ b/src/unit.rs
@@ -44,19 +44,10 @@ impl Unit {
             // otherwise, no chunking.
             .unwrap_or(false);
 
-        let is_secure = url.scheme().eq_ignore_ascii_case("https");
-
-        let hostname = url.host_str().unwrap_or(DEFAULT_HOST).to_string();
-
         let query_string = combine_query(&url, &req.query, mix_queries);
 
-        let cookie_headers: Vec<_> = {
-            let state = req.agent.lock().unwrap();
-            match state.as_ref().map(|state| &state.jar) {
-                None => vec![],
-                Some(jar) => match_cookies(jar, &hostname, url.path(), is_secure),
-            }
-        };
+        let cookie_headers: Vec<_> = extract_cookies(&req.agent, &url);
+
         let extra_headers = {
             let mut extra = vec![];
 
@@ -162,9 +153,7 @@ pub(crate) fn connect(
     }
 
     // squirrel away cookies
-    if cfg!(feature = "cookies") {
-        save_cookies(&unit, &resp);
-    }
+    save_cookies(&unit, &resp);
 
     // handle redirects
     if resp.redirect() && req.redirects > 0 {
@@ -210,6 +199,23 @@ pub(crate) fn connect(
     Ok(resp)
 }
 
+#[cfg(feature = "cookie")]
+fn extract_cookies(state: &std::sync::Mutex<Option<AgentState>>, url: &Url) -> Vec<Header> {
+    let state = state.lock().unwrap();
+    let is_secure = url.scheme().eq_ignore_ascii_case("https");
+    let hostname = url.host_str().unwrap_or(DEFAULT_HOST).to_string();
+
+    match state.as_ref().map(|state| &state.jar) {
+        None => vec![],
+        Some(jar) => match_cookies(jar, &hostname, url.path(), is_secure),
+    }
+}
+
+#[cfg(not(feature = "cookie"))]
+fn extract_cookies(_state: &std::sync::Mutex<Option<AgentState>>, url: &Url) -> Vec<Header> {
+    vec![]
+}
+
 // TODO check so cookies can't be set for tld:s
 #[cfg(feature = "cookie")]
 fn match_cookies<'a>(jar: &'a CookieJar, domain: &str, path: &str, is_secure: bool) -> Vec<Header> {
@@ -314,6 +320,9 @@ fn send_prelude(unit: &Unit, stream: &mut Stream, redir: bool) -> IoResult<()> {
     Ok(())
 }
 
+#[cfg(not(feature = "cookie"))]
+fn save_cookies(_unit: &Unit, _resp: &Response) {}
+
 /// Investigate a response for "Set-Cookie" headers.
 #[cfg(feature = "cookie")]
 fn save_cookies(unit: &Unit, resp: &Response) {


Test patch:
diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml
index 811dda4..d2d90a6 100644
--- a/.github/workflows/test.yml
+++ b/.github/workflows/test.yml
@@ -10,7 +10,6 @@ jobs:
       matrix:
         feature:
           - charset
-          - tls
           - cookies
     env:
       RUST_BACKTRACE: "1"
@@ -25,4 +24,4 @@ jobs:
         uses: actions-rs/cargo@v1
         with:
           command: test
-          args: --features json  ${{ matrix.feature }}
+          args: --no-default-features --features json tls ${{ matrix.feature }}
diff --git a/src/test/agent_test.rs b/src/test/agent_test.rs
index 0982b15..f1e4920 100644
--- a/src/test/agent_test.rs
+++ b/src/test/agent_test.rs
@@ -25,6 +25,7 @@ fn agent_reuse_headers() {
     assert_eq!(resp.header("X-Call").unwrap(), "2");
 }
 
+#[cfg(feature = "cookie")]
 #[test]
 fn agent_cookies() {
     let agent = agent();
@@ -53,6 +54,7 @@ fn agent_cookies() {
 }
 
 #[test]
+#[cfg(feature = "tls")]
 fn connection_reuse() {
     use std::io::Read;
     use std::time::Duration;