Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sugyan committed Nov 8, 2023
1 parent 6a62c2c commit 7d39891
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 1 deletion.
9 changes: 8 additions & 1 deletion .github/workflows/xrpc-client.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ jobs:
- name: Build
run: cargo build -p atrium-xrpc-client --verbose
- name: Run tests
run: cargo test -p atrium-xrpc-client --verbose --lib
run: |
cargo test -p atrium-xrpc-client --verbose --lib
cargo test -p atrium-xrpc-client --verbose --lib --no-default-features
cargo test -p atrium-xrpc-client --verbose --lib --no-default-features --features=reqwest-native
cargo test -p atrium-xrpc-client --verbose --lib --no-default-features --features=reqwest-rustls
cargo test -p atrium-xrpc-client --verbose --lib --no-default-features --features=isahc
cargo test -p atrium-xrpc-client --verbose --lib --no-default-features --features=surf
cargo test -p atrium-xrpc-client --verbose --lib --all-features
- name: Run doctests
run: cargo test -p atrium-xrpc-client --verbose --doc --all-features
4 changes: 4 additions & 0 deletions atrium-xrpc-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ surf = ["dep:surf"]
[dev-dependencies]
surf = { version = "2.3.2", default-features = false, features = ["h1-client-rustls"] }
http-client = { version = "6.5.3", default-features = false, features = ["h1_client", "rustls"] }
mockito = "1.2.0"
tokio = { version = "1.33.0", features = ["macros"] }
serde = { version = "1.0.192", features = ["derive"] }
futures = { version = "0.3.29", default-features = false }

[package.metadata.docs.rs]
all-features = true
Expand Down
3 changes: 3 additions & 0 deletions atrium-xrpc-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ pub mod reqwest;
#[cfg_attr(docsrs, doc(cfg(feature = "surf")))]
#[cfg(feature = "surf")]
pub mod surf;

#[cfg(test)]
mod tests;
149 changes: 149 additions & 0 deletions atrium-xrpc-client/src/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
use atrium_xrpc::{OutputDataOrBytes, XrpcClient, XrpcRequest};
use futures::future::join_all;
use http::Method;
use mockito::{Matcher, Server};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug)]
struct Parameters {
query: String,
}

#[derive(Serialize, Deserialize, Debug)]
struct Output {
data: String,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(tag = "error", content = "message")]
enum Error {
Bad,
}

#[tokio::test]
async fn test_query() -> Result<(), Box<dyn std::error::Error>> {
let mut server = Server::new_async().await;
let mock_ok = server
.mock("GET", "/xrpc/test/ok")
.match_query(Matcher::UrlEncoded("query".into(), "bar".into()))
.with_status(200)
.with_header("content-type", "application/json")
.with_body(r#"{"data": "foo"}"#)
.create_async()
.await;
let mock_err = server
.mock("GET", "/xrpc/test/err")
.match_query(Matcher::UrlEncoded("query".into(), "bar".into()))
.with_status(400)
.with_body(r#"{"error": "Bad"}"#)
.create_async()
.await;

async fn run(
client: impl XrpcClient + Send + Sync,
ok: bool,
) -> Result<Output, atrium_xrpc::error::Error<Error>> {
let response = client
.send_xrpc::<_, (), _, _>(&XrpcRequest {
method: Method::GET,
path: (if ok { "test/ok" } else { "test/err" }).into(),
parameters: Some(Parameters {
query: "bar".into(),
}),
input: None,
encoding: None,
})
.await?;
match response {
OutputDataOrBytes::Bytes(_) => Err(atrium_xrpc::error::Error::UnexpectedResponseType),
OutputDataOrBytes::Data(out) => Ok(out),
}
}
{
let handles = vec![
#[cfg(feature = "isahc")]
tokio::spawn(run(
crate::isahc::IsahcClientBuilder::new(server.url())
.client(isahc::HttpClient::builder().build()?)
.build(),
true,
)),
#[cfg(feature = "reqwest-native")]
tokio::spawn(run(
crate::reqwest::ReqwestClientBuilder::new(server.url())
.client(reqwest::ClientBuilder::new().use_native_tls().build()?)
.build(),
true,
)),
#[cfg(feature = "reqwest-rustls")]
tokio::spawn(run(
crate::reqwest::ReqwestClientBuilder::new(server.url())
.client(reqwest::ClientBuilder::new().use_rustls_tls().build()?)
.build(),
true,
)),
#[cfg(feature = "surf")]
tokio::spawn(run(
crate::surf::SurfClient::new(
server.url(),
surf::Client::with_http_client(http_client::h1::H1Client::new()),
),
true,
)),
];
let len = handles.len();
for result in join_all(handles).await {
let output = result?.expect("xrpc response should be ok");
assert_eq!(output.data, "foo");
}
mock_ok.expect(len).assert_async().await;
}
{
let handles = vec![
#[cfg(feature = "isahc")]
tokio::spawn(run(
crate::isahc::IsahcClientBuilder::new(server.url())
.client(isahc::HttpClient::builder().build()?)
.build(),
false,
)),
#[cfg(feature = "reqwest-native")]
tokio::spawn(run(
crate::reqwest::ReqwestClientBuilder::new(server.url())
.client(reqwest::ClientBuilder::new().use_native_tls().build()?)
.build(),
false,
)),
#[cfg(feature = "reqwest-rustls")]
tokio::spawn(run(
crate::reqwest::ReqwestClientBuilder::new(server.url())
.client(reqwest::ClientBuilder::new().use_rustls_tls().build()?)
.build(),
false,
)),
#[cfg(feature = "surf")]
tokio::spawn(run(
crate::surf::SurfClient::new(
server.url(),
surf::Client::with_http_client(http_client::h1::H1Client::new()),
),
false,
)),
];
let len = handles.len();
for result in join_all(handles).await {
let err = result?.expect_err("xrpc response should be error");
if let atrium_xrpc::error::Error::XrpcResponse(e) = err {
assert_eq!(e.status, 400);
if let Some(atrium_xrpc::error::XrpcErrorKind::Custom(Error::Bad)) = e.error {
} else {
panic!("unexpected error kind: {e:?}");
}
} else {
panic!("unexpected error: {err:?}");
}
}
mock_err.expect(len).assert_async().await;
}
Ok(())
}

0 comments on commit 7d39891

Please sign in to comment.