Skip to content

Commit 0606f83

Browse files
authored
feat: Support non-ssl http backends (#57)
Add a new `ServiceAddress::new_without_ssl` so we can create one that doesn't use SSL. It means that the URLs to the server will start with `http://...` and `ws://` Signed-off-by: Rainer Schoenberger <[email protected]>
1 parent a456045 commit 0606f83

File tree

2 files changed

+70
-10
lines changed

2 files changed

+70
-10
lines changed

src/client/app_configuration_ibm_cloud.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,11 @@ mod tests {
9797
let service_address = AppConfigurationClientIBMCloud::create_service_address("region");
9898

9999
assert_eq!(
100-
service_address.base_url(crate::ServiceAddressProtocol::Https),
100+
service_address.base_url(crate::ServiceAddressProtocol::Http),
101101
"https://region.apprapp.cloud.ibm.com/apprapp"
102102
);
103103
assert_eq!(
104-
service_address.base_url(crate::ServiceAddressProtocol::Wss),
104+
service_address.base_url(crate::ServiceAddressProtocol::Ws),
105105
"wss://region.apprapp.cloud.ibm.com/apprapp"
106106
);
107107
}

src/network/http_client.rs

+68-8
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ use tungstenite::connect;
2929
use url::Url;
3030

3131
pub enum ServiceAddressProtocol {
32-
Https,
33-
Wss,
32+
Http,
33+
Ws,
3434
}
3535

3636
impl std::fmt::Display for ServiceAddressProtocol {
3737
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
3838
match self {
39-
ServiceAddressProtocol::Https => write!(f, "https://"),
40-
ServiceAddressProtocol::Wss => write!(f, "wss://"),
39+
ServiceAddressProtocol::Http => write!(f, "http"),
40+
ServiceAddressProtocol::Ws => write!(f, "ws"),
4141
}
4242
}
4343
}
@@ -47,6 +47,7 @@ pub struct ServiceAddress {
4747
host: String,
4848
port: Option<u16>,
4949
endpoint: Option<String>,
50+
use_ssl: bool,
5051
}
5152

5253
impl ServiceAddress {
@@ -55,6 +56,16 @@ impl ServiceAddress {
5556
host,
5657
port,
5758
endpoint,
59+
use_ssl: true,
60+
}
61+
}
62+
63+
pub fn new_without_ssl(host: String, port: Option<u16>, endpoint: Option<String>) -> Self {
64+
Self {
65+
host,
66+
port,
67+
endpoint,
68+
use_ssl: false,
5869
}
5970
}
6071

@@ -70,8 +81,8 @@ impl ServiceAddress {
7081
} else {
7182
"".to_string()
7283
};
73-
74-
format!("{protocol}{}{port}{endpoint}", self.host)
84+
let ssl_suffix = if self.use_ssl { "s" } else { "" };
85+
format!("{protocol}{ssl_suffix}://{}{port}{endpoint}", self.host)
7586
}
7687
}
7788

@@ -102,7 +113,7 @@ impl ServerClientImpl {
102113
pub fn get_configuration(&self, collection: &ConfigurationId) -> Result<ConfigurationJson> {
103114
let url = format!(
104115
"{}/feature/v1/instances/{}/config",
105-
self.service_address.base_url(ServiceAddressProtocol::Https),
116+
self.service_address.base_url(ServiceAddressProtocol::Http),
106117
collection.guid
107118
);
108119
let client = Client::new();
@@ -137,7 +148,7 @@ impl ServerClientImpl {
137148
) -> Result<(WebSocket<MaybeTlsStream<TcpStream>>, Response)> {
138149
let ws_url = format!(
139150
"{}/wsfeature",
140-
self.service_address.base_url(ServiceAddressProtocol::Wss)
151+
self.service_address.base_url(ServiceAddressProtocol::Ws)
141152
);
142153
let mut ws_url = Url::parse(&ws_url)
143154
.map_err(|e| Error::Other(format!("Cannot parse '{}' as URL: {}", ws_url, e)))?;
@@ -171,3 +182,52 @@ impl ServerClientImpl {
171182
Ok(connect(request)?)
172183
}
173184
}
185+
186+
#[cfg(test)]
187+
mod tests {
188+
use super::*;
189+
190+
#[test]
191+
fn test_non_ssl_base_url() {
192+
let address = ServiceAddress::new_without_ssl(
193+
"ibm.com".to_string(),
194+
None,
195+
Some("endpoint".to_string()),
196+
);
197+
assert_eq!(
198+
address.base_url(ServiceAddressProtocol::Http),
199+
"http://ibm.com/endpoint"
200+
);
201+
assert_eq!(
202+
address.base_url(ServiceAddressProtocol::Ws),
203+
"ws://ibm.com/endpoint"
204+
);
205+
}
206+
207+
#[test]
208+
fn test_ssl_base_url() {
209+
let address =
210+
ServiceAddress::new("ibm.com".to_string(), None, Some("endpoint".to_string()));
211+
assert_eq!(
212+
address.base_url(ServiceAddressProtocol::Http),
213+
"https://ibm.com/endpoint"
214+
);
215+
assert_eq!(
216+
address.base_url(ServiceAddressProtocol::Ws),
217+
"wss://ibm.com/endpoint"
218+
);
219+
}
220+
221+
#[test]
222+
fn test_url_with_port() {
223+
let address = ServiceAddress::new_without_ssl("ibm.com".to_string(), Some(12345), None);
224+
assert_eq!(
225+
address.base_url(ServiceAddressProtocol::Http),
226+
"http://ibm.com:12345"
227+
);
228+
assert_eq!(
229+
address.base_url(ServiceAddressProtocol::Ws),
230+
"ws://ibm.com:12345"
231+
);
232+
}
233+
}

0 commit comments

Comments
 (0)