diff --git a/smol/Cargo.toml b/smol/Cargo.toml index eb3c6d2ac4..487e59214f 100644 --- a/smol/Cargo.toml +++ b/smol/Cargo.toml @@ -29,6 +29,8 @@ signal-hook-async-std = "0.2.2" [dev-dependencies] env_logger = "0.11.0" +tempfile = "3.10.1" +test-harness = "0.2.0" trillium-client = { path = "../client" } trillium-logger = { path = "../logger" } trillium-testing = { path = "../testing" } diff --git a/smol/src/client.rs b/smol/src/client.rs index 0a468431a5..317f061aa7 100644 --- a/smol/src/client.rs +++ b/smol/src/client.rs @@ -82,3 +82,18 @@ impl Connector for ClientConfig { Ok(tcp) } } + +#[cfg(unix)] +impl Connector for SmolTransport { + type Transport = Self; + + type Runtime = SmolRuntime; + + async fn connect(&self, _url: &Url) -> Result { + Ok(self.clone()) + } + + fn runtime(&self) -> Self::Runtime { + SmolRuntime::default() + } +} diff --git a/smol/src/transport.rs b/smol/src/transport.rs index 38eff92234..22646a797e 100644 --- a/smol/src/transport.rs +++ b/smol/src/transport.rs @@ -43,3 +43,10 @@ impl Transport for SmolTransport { #[cfg(unix)] impl Transport for SmolTransport {} +#[cfg(unix)] +impl SmolTransport { + /// connnect to the provided path as a unix domain socket + pub async fn connect_unix(path: impl AsRef) -> Result { + async_net::unix::UnixStream::connect(path).await.map(Self) + } +} diff --git a/smol/tests/unix_stream.rs b/smol/tests/unix_stream.rs new file mode 100644 index 0000000000..2a21865418 --- /dev/null +++ b/smol/tests/unix_stream.rs @@ -0,0 +1,21 @@ +#![cfg(unix)] +use test_harness::test; +use trillium_client::Client; +use trillium_smol::{config, SmolTransport}; +use trillium_testing::harness; + +#[test(harness)] +async fn smoke() { + let temp_dir = tempfile::tempdir().unwrap(); + let path = temp_dir.path().join("socket"); + + let handle = config().with_host(path.to_str().unwrap()).spawn("ok"); + handle.info().await; + + let stream = SmolTransport::connect_unix(path).await.unwrap(); + let mut conn = Client::new(stream).get("http://localhost/").await.unwrap(); + + assert_eq!(conn.status().unwrap(), 200); + assert_eq!(conn.response_body().read_string().await.unwrap(), "ok"); + handle.shut_down().await; +}