-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(smol): add trillium_smol::UnixStream
- Loading branch information
Showing
4 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use crate::SmolRuntime; | ||
use futures_lite::{AsyncRead, AsyncWrite}; | ||
use std::{io, path::Path}; | ||
use trillium_macros::{AsyncRead, AsyncWrite}; | ||
use trillium_server_common::{Connector, Transport, Url}; | ||
|
||
/// A transport that is also a connector. | ||
/// | ||
/// Construct it with [`UnixStream::connect`] | ||
#[derive(AsyncWrite, AsyncRead, Debug, Clone)] | ||
pub struct UnixStream(async_net::unix::UnixStream); | ||
|
||
impl UnixStream { | ||
/// connect a new UnixStream | ||
pub async fn connect(path: impl AsRef<Path>) -> io::Result<Self> { | ||
async_net::unix::UnixStream::connect(path).await.map(Self) | ||
} | ||
} | ||
|
||
impl Transport for UnixStream {} | ||
|
||
impl Connector for UnixStream { | ||
type Transport = Self; | ||
type Runtime = SmolRuntime; | ||
async fn connect(&self, _url: &Url) -> io::Result<Self::Transport> { | ||
Ok(self.clone()) | ||
} | ||
|
||
fn runtime(&self) -> Self::Runtime { | ||
SmolRuntime::default() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#![cfg(unix)] | ||
use test_harness::test; | ||
use trillium_client::Client; | ||
use trillium_smol::{config, UnixStream}; | ||
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 = UnixStream::connect(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; | ||
} |