Skip to content

Commit

Permalink
chore: move server connector tests into tests dir
Browse files Browse the repository at this point in the history
  • Loading branch information
jbr committed Apr 19, 2024
1 parent 881cece commit 8acd440
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 63 deletions.
63 changes: 0 additions & 63 deletions testing/src/server_connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,66 +62,3 @@ impl<H: Handler> Connector for ServerConnector<H> {
pub fn connector(handler: impl Handler) -> impl Connector {
ServerConnector::new(handler)
}

#[cfg(test)]
mod test {
use crate::server_connector::ServerConnector;
use trillium_client::Client;

#[test]
fn test() {
crate::block_on(async {
let client = Client::new(ServerConnector::new("test"));
let mut conn = client.get("https://example.com/test").await.unwrap();
assert_eq!(conn.response_body().read_string().await.unwrap(), "test");
});
}

#[test]
fn test_no_dns() {
crate::block_on(async {
let client = Client::new(ServerConnector::new("test"));
let mut conn = client
.get("https://not.a.real.tld.example/test")
.await
.unwrap();
assert_eq!(conn.response_body().read_string().await.unwrap(), "test");
});
}

#[test]
fn test_post() {
crate::block_on(async {
let client = Client::new(ServerConnector::new(
|mut conn: trillium::Conn| async move {
let body = conn.request_body_string().await.unwrap();
let response = format!(
"{} {}://{}{} with body \"{}\"",
conn.method(),
if conn.is_secure() { "https" } else { "http" },
conn.inner().host().unwrap_or_default(),
conn.path(),
body
);

conn.ok(response)
},
));

let body = client
.post("https://example.com/test")
.with_body("some body")
.await
.unwrap()
.response_body()
.read_string()
.await
.unwrap();

assert_eq!(
body,
"POST https://example.com/test with body \"some body\""
);
});
}
}
54 changes: 54 additions & 0 deletions testing/tests/server_connector.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use test_harness::test;
use trillium_client::Client;
use trillium_testing::{harness, ServerConnector};

#[test(harness)]
async fn test() {
let client = Client::new(ServerConnector::new("test"));
let mut conn = client.get("https://example.com/test").await.unwrap();
assert_eq!(conn.response_body().read_string().await.unwrap(), "test");
}

#[test(harness)]
async fn test_no_dns() {
let client = Client::new(ServerConnector::new("test"));
let mut conn = client
.get("https://not.a.real.tld.example/test")
.await
.unwrap();
assert_eq!(conn.response_body().read_string().await.unwrap(), "test");
}

#[test(harness)]
async fn test_post() {
let client = Client::new(ServerConnector::new(
|mut conn: trillium::Conn| async move {
let body = conn.request_body_string().await.unwrap();
let response = format!(
"{} {}://{}{} with body \"{}\"",
conn.method(),
if conn.is_secure() { "https" } else { "http" },
conn.inner().host().unwrap_or_default(),
conn.path(),
body
);

conn.ok(response)
},
));

let body = client
.post("https://example.com/test")
.with_body("some body")
.await
.unwrap()
.response_body()
.read_string()
.await
.unwrap();

assert_eq!(
body,
"POST https://example.com/test with body \"some body\""
);
}

0 comments on commit 8acd440

Please sign in to comment.