Attempt to send `hello-nav\n` to the echo server and read the response.
(port: u16)
| 116 | |
| 117 | /// Attempt to send `hello-nav\n` to the echo server and read the response. |
| 118 | async fn try_echo(port: u16) -> Result<String, String> { |
| 119 | let mut stream = TcpStream::connect(format!("127.0.0.1:{port}")) |
| 120 | .await |
| 121 | .map_err(|e| format!("connect: {e}"))?; |
| 122 | |
| 123 | stream |
| 124 | .write_all(b"hello-nav\n") |
| 125 | .await |
| 126 | .map_err(|e| format!("write: {e}"))?; |
| 127 | |
| 128 | let mut buf = vec![0u8; 4096]; |
| 129 | let n = tokio::time::timeout(Duration::from_secs(10), stream.read(&mut buf)) |
| 130 | .await |
| 131 | .map_err(|_| "read timeout".to_string())? |
| 132 | .map_err(|e| format!("read: {e}"))?; |
| 133 | |
| 134 | let response = String::from_utf8_lossy(&buf[..n]) |
| 135 | .trim_end_matches(['\r', '\n']) |
| 136 | .to_string(); |
| 137 | |
| 138 | Ok(response) |
| 139 | } |
no test coverage detected