Browse Source

client: Fix websocket url parsing (#589)

0xlucius 4 years ago
parent
commit
84c3288f32
1 changed files with 29 additions and 2 deletions
  1. 29 2
      client/src/cluster.rs

+ 29 - 2
client/src/cluster.rs

@@ -40,6 +40,14 @@ impl FromStr for Cluster {
                     ws_url.set_port(Some(8900))
                         .map_err(|_| anyhow!("Unable to set port"))?;
                 }
+                if ws_url.scheme() == "https" {
+                    ws_url.set_scheme("wss")
+                        .map_err(|_| anyhow!("Unable to set scheme"))?;
+                } else {
+                    ws_url.set_scheme("ws")
+                        .map_err(|_| anyhow!("Unable to set scheme"))?;
+                }
+
 
                 Ok(Cluster::Custom(http_url.to_string(), ws_url.to_string()))
             }
@@ -116,7 +124,7 @@ mod tests {
         let url = "http://my-url.com:7000/";
         let cluster = Cluster::from_str(url).unwrap();
         assert_eq!(
-            Cluster::Custom(url.to_string(), "http://my-url.com:7001/".to_string()),
+            Cluster::Custom(url.to_string(), "ws://my-url.com:7001/".to_string()),
             cluster
         );
     }
@@ -126,7 +134,26 @@ mod tests {
         let url = "http://my-url.com/";
         let cluster = Cluster::from_str(url).unwrap();
         assert_eq!(
-            Cluster::Custom(url.to_string(), "http://my-url.com:8900/".to_string()),
+            Cluster::Custom(url.to_string(), "ws://my-url.com:8900/".to_string()),
+            cluster
+        );
+    }
+
+    #[test]
+    fn test_https_port() {
+        let url = "https://my-url.com:7000/";
+        let cluster = Cluster::from_str(url).unwrap();
+        assert_eq!(
+            Cluster::Custom(url.to_string(), "wss://my-url.com:7001/".to_string()),
+            cluster
+        );
+    }
+    #[test]
+    fn test_https_no_port() {
+        let url = "https://my-url.com/";
+        let cluster = Cluster::from_str(url).unwrap();
+        assert_eq!(
+            Cluster::Custom(url.to_string(), "wss://my-url.com:8900/".to_string()),
             cluster
         );
     }