@@ -29,15 +29,15 @@ use tungstenite::connect;
29
29
use url:: Url ;
30
30
31
31
pub enum ServiceAddressProtocol {
32
- Https ,
33
- Wss ,
32
+ Http ,
33
+ Ws ,
34
34
}
35
35
36
36
impl std:: fmt:: Display for ServiceAddressProtocol {
37
37
fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
38
38
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 " ) ,
41
41
}
42
42
}
43
43
}
@@ -47,6 +47,7 @@ pub struct ServiceAddress {
47
47
host : String ,
48
48
port : Option < u16 > ,
49
49
endpoint : Option < String > ,
50
+ use_ssl : bool ,
50
51
}
51
52
52
53
impl ServiceAddress {
@@ -55,6 +56,16 @@ impl ServiceAddress {
55
56
host,
56
57
port,
57
58
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 ,
58
69
}
59
70
}
60
71
@@ -70,8 +81,8 @@ impl ServiceAddress {
70
81
} else {
71
82
"" . to_string ( )
72
83
} ;
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)
75
86
}
76
87
}
77
88
@@ -102,7 +113,7 @@ impl ServerClientImpl {
102
113
pub fn get_configuration ( & self , collection : & ConfigurationId ) -> Result < ConfigurationJson > {
103
114
let url = format ! (
104
115
"{}/feature/v1/instances/{}/config" ,
105
- self . service_address. base_url( ServiceAddressProtocol :: Https ) ,
116
+ self . service_address. base_url( ServiceAddressProtocol :: Http ) ,
106
117
collection. guid
107
118
) ;
108
119
let client = Client :: new ( ) ;
@@ -137,7 +148,7 @@ impl ServerClientImpl {
137
148
) -> Result < ( WebSocket < MaybeTlsStream < TcpStream > > , Response ) > {
138
149
let ws_url = format ! (
139
150
"{}/wsfeature" ,
140
- self . service_address. base_url( ServiceAddressProtocol :: Wss )
151
+ self . service_address. base_url( ServiceAddressProtocol :: Ws )
141
152
) ;
142
153
let mut ws_url = Url :: parse ( & ws_url)
143
154
. map_err ( |e| Error :: Other ( format ! ( "Cannot parse '{}' as URL: {}" , ws_url, e) ) ) ?;
@@ -171,3 +182,52 @@ impl ServerClientImpl {
171
182
Ok ( connect ( request) ?)
172
183
}
173
184
}
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