diff --git a/Cargo.toml b/Cargo.toml index 2763027..6a63822 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,7 +21,7 @@ getrandom = { version = "0.4.3", features = ["sys_rng"] } gpui-kit = "0.7.0" gpui-omarchy = "0.2.1" http-body-util = "0.1.5" -hyper = { version = "1.12.0", features = ["server", "http1"] } +hyper = { version = "1.12.0", features = ["client", "server", "http1"] } hyper-util = { version = "0.1.21", features = ["tokio"] } include_dir = "0.7.4" jiff = { version = "1.0.1", features = ["serde"] } diff --git a/src/srv/creddy_server.rs b/src/srv/creddy_server.rs index bffaf84..2004aa1 100644 --- a/src/srv/creddy_server.rs +++ b/src/srv/creddy_server.rs @@ -119,7 +119,6 @@ pub fn start_creddy_server(path: &Path, app: AsyncApp) -> Result<(), CreddyError async fn accept(listener: &UnixListener) -> io::Result<(UnixStream, Pid)> { let (stream, _) = listener.accept().await?; - // pid() never returns None on Linux let pid = socket_peercred(&stream)?.pid; Ok((stream, pid)) } @@ -184,3 +183,70 @@ fn text(status: StatusCode, body: &str) -> Result>, CreddyE *response.status_mut() = status; Ok(response) } + +#[cfg(test)] +mod tests { + use crate::tests::TestEnv; + use gpui_kit::TestAppContext; + use http_body_util::{BodyExt, Empty}; + use hyper::client::conn::http1::handshake; + use super::*; + use testresult::TestResult; + + async fn get(uri: &str, addr: &Path, cx: &TestAppContext) -> TestResult> { + let stream = UnixStream::connect(addr).await?; + let (mut sender, conn) = handshake(FuturesIo::new(stream)).await?; + cx.foreground_executor.spawn(conn).detach(); + + let req = Request::get(uri).body(Empty::::new())?; + let resp = sender.send_request(req).await?; + Ok(resp) + } + + #[gpui_kit::test] + async fn test_start_server(cx: &mut TestAppContext) -> TestResult { + let env = TestEnv::new()?; + cx.executor().allow_parking(); + let addr = env.config.creddy_server_addr(); + start_creddy_server(&addr, cx.to_async())?; + + let resp = get("/ping", &addr, cx).await?; + assert_eq!(resp.status(), StatusCode::OK); + + let body = resp.into_body().collect().await?.to_bytes(); + assert_eq!(body, "pong"); + + Ok(()) + } + + #[gpui_kit::test] + async fn test_aws_get_missing_credential(cx: &mut TestAppContext) -> TestResult { + let env = TestEnv::new()?; + cx.executor().allow_parking(); + let db = env.db()?; + cx.update(|cx| { + cx.set_global(AppSession::Unlocked(db)); + cx.set_global(PendingRequests::new()); + // stand in for the user, approving requests as they arrive + cx.observe_global::(|cx| { + // popping notifies this observer again, so make sure that stops when the queue is empty + if cx.global::().0.is_empty() { + return; + } + let (_, sender) = cx.global_mut::().0.pop_front().unwrap(); + let _ = sender.send(CredentialResponse::yes_session()); + }) + .detach(); + }); + let addr = env.config.creddy_server_addr(); + start_creddy_server(&addr, cx.to_async())?; + + let resp = get("/aws?name=nonexistent", &addr, cx).await?; + let body = resp.into_body().collect().await?.to_bytes(); + let error: serde_json::Value = serde_json::from_slice(&body)?; + assert_eq!(error["code"], "Db"); + assert_eq!(error["message"], "Error from database: Query returned no rows"); + + Ok(()) + } +}