xwt_tests/tests/
closed_uni_stream.rs

1use xwt_core::prelude::*;
2
3#[derive(Debug, thiserror::Error)]
4pub enum Error<Endpoint>
5where
6    Endpoint: xwt_core::endpoint::Connect + std::fmt::Debug,
7    Endpoint::Connecting: std::fmt::Debug,
8    ConnectSessionFor<Endpoint>: xwt_core::session::stream::OpenUni + std::fmt::Debug,
9{
10    #[error("connect: {0}")]
11    Connect(#[source] xwt_error::Connect<Endpoint>),
12    #[error("open uni stream: {0}")]
13    OpenUniStream(#[source] UniStreamOpenErrorFor<ConnectSessionFor<Endpoint>>),
14    #[error("opening uni stream: {0}")]
15    OpeningUniStream(#[source] UniStreamOpeningErrorFor<ConnectSessionFor<Endpoint>>),
16    #[error("write stream aborted: {0}")]
17    WriteStreamAborted(#[source] WriteAbortedErrorFor<SendStreamFor<ConnectSessionFor<Endpoint>>>),
18    #[error("no error code was returned")]
19    NoErrorCode,
20    #[error("error code conversion to u32 failed")]
21    ErrorCodeConversion,
22    #[error("error code mismatch: got code {0}")]
23    ErrorCodeMismatch(u32),
24}
25
26pub async fn run<Endpoint>(
27    endpoint: Endpoint,
28    url: &str,
29    expected_error_code: u32,
30) -> Result<(), Error<Endpoint>>
31where
32    Endpoint: xwt_core::endpoint::Connect + std::fmt::Debug,
33    Endpoint::Connecting: std::fmt::Debug,
34    ConnectSessionFor<Endpoint>: xwt_core::session::stream::OpenUni + std::fmt::Debug,
35    SendStreamFor<ConnectSessionFor<Endpoint>>: xwt_core::stream::WriteAborted,
36{
37    let session = crate::utils::connect(&endpoint, url)
38        .await
39        .map_err(Error::Connect)?;
40
41    let opening = session.open_uni().await.map_err(Error::OpenUniStream)?;
42    let send_stream = opening.wait_uni().await.map_err(Error::OpeningUniStream)?;
43
44    let error_code = send_stream
45        .aborted()
46        .await
47        .map_err(Error::WriteStreamAborted)?;
48
49    let error_code: u32 = error_code
50        .try_into()
51        .map_err(|_| Error::ErrorCodeConversion)?;
52
53    if error_code != expected_error_code {
54        return Err(Error::ErrorCodeMismatch(error_code));
55    }
56
57    Ok(())
58}