Skip to main content

ntex_net/connect/
uri.rs

1use super::Address;
2
3impl Address for urly::Url {
4    fn host(&self) -> &str {
5        self.host().unwrap_or("")
6    }
7
8    fn port(&self) -> Option<u16> {
9        // Azure Service Bus uses amqps
10        self.port_or_known_default()
11            .or_else(|| (self.scheme_str() == Some("sb")).then_some(5671))
12    }
13}
14
15#[cfg(test)]
16mod tests {
17    use super::*;
18
19    /// STATEMENT: A URL without a port uses the default port of its scheme.
20    ///
21    /// HOW:
22    /// - Query the port of URLs for each known scheme; expect its default port.
23    /// - Expect no port for an unknown scheme.
24    #[test]
25    fn port_tests() {
26        for (s, p) in [
27            ("http", 80),
28            ("https", 443),
29            ("ws", 80),
30            ("wss", 443),
31            ("amqp", 5672),
32            ("amqps", 5671),
33            ("sb", 5671),
34            ("mqtt", 1883),
35            ("mqtts", 8883),
36            ("ftp", 21),
37        ] {
38            let url = urly::Url::try_from(format!("{s}://h/")).unwrap();
39            assert_eq!(Address::port(&url), Some(p), "{s}");
40        }
41        let url = urly::Url::from_static("unknowns://h/");
42        assert_eq!(Address::port(&url), None);
43    }
44
45    /// STATEMENT: A URL's address host and port come from its authority, falling back to the
46    /// scheme's default port.
47    ///
48    /// HOW:
49    /// - Expect the bracketed host and the `mqtts` default port for an IPv6 URL.
50    /// - Expect the host and explicit port for `http://example.com:8080/`.
51    /// - Expect an empty host and no port for a relative URL.
52    #[test]
53    fn url_address() {
54        let url = urly::Url::from_static("mqtts://[::1]/a");
55        assert_eq!(Address::host(&url), "[::1]");
56        assert_eq!(Address::port(&url), Some(8883));
57        let url = urly::Url::from_static("http://example.com:8080/");
58        assert_eq!(Address::host(&url), "example.com");
59        assert_eq!(Address::port(&url), Some(8080));
60        let url = urly::Url::from_static("/a");
61        assert_eq!(Address::host(&url), "");
62        assert_eq!(Address::port(&url), None);
63    }
64}