@@ -29,24 +29,25 @@ 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
}
44
44
45
- #[ derive( Debug ) ]
45
+ #[ derive( Debug , Clone ) ]
46
46
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,10 +56,20 @@ 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
61
- pub fn base_url ( & self , protocol : ServiceAddressProtocol ) -> String {
72
+ pub ( crate ) fn base_url ( & self , protocol : ServiceAddressProtocol ) -> String {
62
73
let port = if let Some ( port) = self . port {
63
74
format ! ( ":{port}" )
64
75
} else {
@@ -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 ( ) ;
@@ -119,7 +130,9 @@ impl ServerClientImpl {
119
130
. send ( ) ;
120
131
121
132
match r {
122
- Ok ( response) => response. json ( ) . map_err ( Error :: ReqwestError ) ,
133
+ Ok ( response) => response. json ( ) . map_err ( |_| {
134
+ Error :: ProtocolError ( "Failed to deserialize JSON from server response" . to_string ( ) )
135
+ } ) ,
123
136
Err ( e) => {
124
137
// TODO: Identify if token expired, get new one and retry
125
138
if false {
@@ -137,7 +150,7 @@ impl ServerClientImpl {
137
150
) -> Result < ( WebSocket < MaybeTlsStream < TcpStream > > , Response ) > {
138
151
let ws_url = format ! (
139
152
"{}/wsfeature" ,
140
- self . service_address. base_url( ServiceAddressProtocol :: Wss )
153
+ self . service_address. base_url( ServiceAddressProtocol :: Ws )
141
154
) ;
142
155
let mut ws_url = Url :: parse ( & ws_url)
143
156
. map_err ( |e| Error :: Other ( format ! ( "Cannot parse '{}' as URL: {}" , ws_url, e) ) ) ?;
@@ -171,3 +184,52 @@ impl ServerClientImpl {
171
184
Ok ( connect ( request) ?)
172
185
}
173
186
}
187
+
188
+ #[ cfg( test) ]
189
+ mod tests {
190
+ use super :: * ;
191
+
192
+ #[ test]
193
+ fn test_non_ssl_base_url ( ) {
194
+ let address = ServiceAddress :: new_without_ssl (
195
+ "ibm.com" . to_string ( ) ,
196
+ None ,
197
+ Some ( "endpoint" . to_string ( ) ) ,
198
+ ) ;
199
+ assert_eq ! (
200
+ address. base_url( ServiceAddressProtocol :: Http ) ,
201
+ "http://ibm.com/endpoint"
202
+ ) ;
203
+ assert_eq ! (
204
+ address. base_url( ServiceAddressProtocol :: Ws ) ,
205
+ "ws://ibm.com/endpoint"
206
+ ) ;
207
+ }
208
+
209
+ #[ test]
210
+ fn test_ssl_base_url ( ) {
211
+ let address =
212
+ ServiceAddress :: new ( "ibm.com" . to_string ( ) , None , Some ( "endpoint" . to_string ( ) ) ) ;
213
+ assert_eq ! (
214
+ address. base_url( ServiceAddressProtocol :: Http ) ,
215
+ "https://ibm.com/endpoint"
216
+ ) ;
217
+ assert_eq ! (
218
+ address. base_url( ServiceAddressProtocol :: Ws ) ,
219
+ "wss://ibm.com/endpoint"
220
+ ) ;
221
+ }
222
+
223
+ #[ test]
224
+ fn test_url_with_port ( ) {
225
+ let address = ServiceAddress :: new_without_ssl ( "ibm.com" . to_string ( ) , Some ( 12345 ) , None ) ;
226
+ assert_eq ! (
227
+ address. base_url( ServiceAddressProtocol :: Http ) ,
228
+ "http://ibm.com:12345"
229
+ ) ;
230
+ assert_eq ! (
231
+ address. base_url( ServiceAddressProtocol :: Ws ) ,
232
+ "ws://ibm.com:12345"
233
+ ) ;
234
+ }
235
+ }
0 commit comments