|
15 | 15 | // specific language governing permissions and limitations |
16 | 16 | // under the License. |
17 | 17 |
|
18 | | -use crate::{AutoLogin, IggyDuration, IggyError, TcpClientConfig}; |
19 | | -use std::net::SocketAddr; |
| 18 | +use crate::{AutoLogin, IggyDuration, IggyError, TcpClientConfig, validate_server_address}; |
20 | 19 |
|
21 | 20 | /// Builder for the TCP client configuration. |
22 | 21 | /// Allows configuring the TCP client with custom settings or using defaults: |
@@ -103,13 +102,9 @@ impl TcpClientConfigBuilder { |
103 | 102 | } |
104 | 103 |
|
105 | 104 | /// Builds the TCP client configuration. |
106 | | - pub fn build(self) -> Result<TcpClientConfig, IggyError> { |
107 | | - let addr = self.config.server_address.trim(); |
108 | | - |
109 | | - if addr.parse::<SocketAddr>().is_err() { |
110 | | - let (ip, port) = addr.rsplit_once(':').unwrap_or((addr, "")); |
111 | | - return Err(IggyError::InvalidIpAddress(ip.to_owned(), port.to_owned())); |
112 | | - } |
| 105 | + pub fn build(mut self) -> Result<TcpClientConfig, IggyError> { |
| 106 | + self.config.server_address = self.config.server_address.trim().to_string(); |
| 107 | + validate_server_address(&self.config.server_address)?; |
113 | 108 |
|
114 | 109 | Ok(self.config) |
115 | 110 | } |
@@ -148,12 +143,16 @@ mod tests { |
148 | 143 | } |
149 | 144 |
|
150 | 145 | #[test] |
151 | | - fn invalid_ip_should_fail() { |
| 146 | + fn valid_dns_name_should_succeed() { |
| 147 | + let builder = builder_with_address("localhost:8080"); |
| 148 | + assert!(builder.build().is_ok()); |
| 149 | + } |
| 150 | + |
| 151 | + #[test] |
| 152 | + fn unresolvable_hostname_should_succeed() { |
| 153 | + // Format is valid; DNS resolution is not attempted |
152 | 154 | let builder = builder_with_address("invalid.ip:8080"); |
153 | | - assert!(matches!( |
154 | | - builder.build(), |
155 | | - Err(IggyError::InvalidIpAddress(_, _)) |
156 | | - )); |
| 155 | + assert!(builder.build().is_ok()); |
157 | 156 | } |
158 | 157 |
|
159 | 158 | #[test] |
@@ -182,4 +181,10 @@ mod tests { |
182 | 181 | Err(IggyError::InvalidIpAddress(_, _)) |
183 | 182 | )); |
184 | 183 | } |
| 184 | + |
| 185 | + #[test] |
| 186 | + fn docker_compose_service_name_should_succeed() { |
| 187 | + let builder = builder_with_address("iggy-server:8090"); |
| 188 | + assert!(builder.build().is_ok()); |
| 189 | + } |
185 | 190 | } |
0 commit comments