http server tests
This commit is contained in:
1 parent
58e67cc4b7
commit
3d22ae0638
2 files changed
+68
-2
No files matched your search
+1
-1
@@ -21,7 +21,7 @@ getrandom = { version = "0.4.3", features = ["sys_rng"] }
|
|||||||
gpui-kit = "0.7.0"
|
gpui-kit = "0.7.0"
|
||||||
gpui-omarchy = "0.2.1"
|
gpui-omarchy = "0.2.1"
|
||||||
http-body-util = "0.1.5"
|
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"] }
|
hyper-util = { version = "0.1.21", features = ["tokio"] }
|
||||||
include_dir = "0.7.4"
|
include_dir = "0.7.4"
|
||||||
jiff = { version = "1.0.1", features = ["serde"] }
|
jiff = { version = "1.0.1", features = ["serde"] }
|
||||||
|
|||||||
@@ -119,7 +119,6 @@ pub fn start_creddy_server(path: &Path, app: AsyncApp) -> Result<(), CreddyError
|
|||||||
|
|
||||||
async fn accept(listener: &UnixListener) -> io::Result<(UnixStream, Pid)> {
|
async fn accept(listener: &UnixListener) -> io::Result<(UnixStream, Pid)> {
|
||||||
let (stream, _) = listener.accept().await?;
|
let (stream, _) = listener.accept().await?;
|
||||||
// pid() never returns None on Linux
|
|
||||||
let pid = socket_peercred(&stream)?.pid;
|
let pid = socket_peercred(&stream)?.pid;
|
||||||
Ok((stream, pid))
|
Ok((stream, pid))
|
||||||
}
|
}
|
||||||
@@ -184,3 +183,70 @@ fn text(status: StatusCode, body: &str) -> Result<Response<Full<Bytes>>, CreddyE
|
|||||||
*response.status_mut() = status;
|
*response.status_mut() = status;
|
||||||
Ok(response)
|
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<Response<Incoming>> {
|
||||||
|
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::<Bytes>::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::<PendingRequests>(|cx| {
|
||||||
|
// popping notifies this observer again, so make sure that stops when the queue is empty
|
||||||
|
if cx.global::<PendingRequests>().0.is_empty() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let (_, sender) = cx.global_mut::<PendingRequests>().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(())
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user