Make an HTTP/1.1 GET request and return (status, body).
(addr: SocketAddr, uri: &str, headers: &[(&str, &str)])
| 182 | |
| 183 | /// Make an HTTP/1.1 GET request and return (status, body). |
| 184 | async fn http_get(addr: SocketAddr, uri: &str, headers: &[(&str, &str)]) -> (StatusCode, String) { |
| 185 | let stream = tokio::net::TcpStream::connect(addr).await.unwrap(); |
| 186 | let (mut sender, conn) = hyper::client::conn::http1::Builder::new() |
| 187 | .handshake(TokioIo::new(stream)) |
| 188 | .await |
| 189 | .unwrap(); |
| 190 | tokio::spawn(async move { |
| 191 | let _ = conn.await; |
| 192 | }); |
| 193 | |
| 194 | let mut builder = Request::builder().method("GET").uri(uri); |
| 195 | for (key, value) in headers { |
| 196 | builder = builder.header(*key, *value); |
| 197 | } |
| 198 | let req = builder.body(Empty::<Bytes>::new()).unwrap(); |
| 199 | let resp = sender.send_request(req).await.unwrap(); |
| 200 | let status = resp.status(); |
| 201 | let body_bytes = http_body_util::BodyExt::collect(resp.into_body()) |
| 202 | .await |
| 203 | .unwrap() |
| 204 | .to_bytes(); |
| 205 | let body = String::from_utf8_lossy(&body_bytes).to_string(); |
| 206 | (status, body) |
| 207 | } |
| 208 | |
| 209 | // =========================================================================== |
| 210 | // Tests |
no test coverage detected