| 1 | #![cfg (all(feature = "full" , not(target_os = "wasi" )))] // Wasi does not support direct socket operations |
| 2 | |
| 3 | use tokio::net; |
| 4 | use tokio_test::assert_ok; |
| 5 | |
| 6 | use std::io; |
| 7 | use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr}; |
| 8 | |
| 9 | #[tokio::test ] |
| 10 | async fn lookup_socket_addr() { |
| 11 | let addr: SocketAddr = "127.0.0.1:8000" .parse().unwrap(); |
| 12 | |
| 13 | let actual = assert_ok!(net::lookup_host(addr).await).collect::<Vec<_>>(); |
| 14 | assert_eq!(vec![addr], actual); |
| 15 | } |
| 16 | |
| 17 | #[tokio::test ] |
| 18 | async fn lookup_str_socket_addr() { |
| 19 | let addr: SocketAddr = "127.0.0.1:8000" .parse().unwrap(); |
| 20 | |
| 21 | let actual = assert_ok!(net::lookup_host("127.0.0.1:8000" ).await).collect::<Vec<_>>(); |
| 22 | assert_eq!(vec![addr], actual); |
| 23 | } |
| 24 | |
| 25 | #[tokio::test ] |
| 26 | async fn resolve_dns() -> io::Result<()> { |
| 27 | let mut hosts = net::lookup_host("localhost:3000" ).await?; |
| 28 | let host = hosts.next().unwrap(); |
| 29 | |
| 30 | let expected = if host.is_ipv4() { |
| 31 | SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 3000) |
| 32 | } else { |
| 33 | SocketAddr::new(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)), 3000) |
| 34 | }; |
| 35 | assert_eq!(host, expected); |
| 36 | |
| 37 | Ok(()) |
| 38 | } |
| 39 | |