From 82cfaf337d5c8a6d8010d873b144114aea977a6c Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Wed, 6 Nov 2024 20:40:56 +0000 Subject: [PATCH] test(net): add the first tests --- src/net/system.rs | 7 +++--- tests/net.rs | 60 ++++++++++++++++++++++++++++++----------------- 2 files changed, 43 insertions(+), 24 deletions(-) diff --git a/src/net/system.rs b/src/net/system.rs index 206ad1f..58e22a8 100644 --- a/src/net/system.rs +++ b/src/net/system.rs @@ -76,9 +76,10 @@ impl Net { fn _on(&mut self, request: reqwest::Request, response: reqwest::Response) { self.plans.push(Plan { request, response }) } - - pub fn assert(&self) -> Result<()> { - todo!() +} +impl Drop for Net { + fn drop(&mut self) { + assert!(self.plans.is_empty()) } } diff --git a/tests/net.rs b/tests/net.rs index 00253a2..95df8a0 100644 --- a/tests/net.rs +++ b/tests/net.rs @@ -3,30 +3,48 @@ use kxio::net::Error; type TestResult = Result<(), Error>; -mod get { - use super::*; +#[tokio::test] +async fn test_get() -> TestResult { + let mut net = kxio::net::mock(); + let client = net.client(); - #[tokio::test] - async fn test() -> TestResult { - let mut net = kxio::net::mock(); + let url = "https://www.example.com"; + let request = client.get(url).build().expect("build request"); + let my_response = net + .response() + .status(200) + .body("Get OK") + .expect("request body"); - let url = "https://www.example.com"; - let request = net.client().get(url).build()?; - let my_response = net.response().status(200).body("OK").unwrap(); + net.on(request).response(my_response.into()); - net.on(request).response(my_response.clone().into()); + let response = net.send(client.get(url)).await?; - let client = net.client(); + assert_eq!(response.status(), http::StatusCode::OK); + assert_eq!(response.bytes().await.expect("response body"), "Get OK"); - let response = net.send(client.get(url)).await?; - - assert_eq!(response.status(), my_response.status()); - - // net_mock.assert()?; - - // let my_net = net::new(); - // my_net.send(request).await?; - - Ok(()) - } + Ok(()) +} + +#[tokio::test] +async fn test_post() -> TestResult { + let mut net = kxio::net::mock(); + let client = net.client(); + + let url = "https://www.example.com"; + let request = client.post(url).build().expect("build request"); + let my_response = net + .response() + .status(200) + .body("Post OK") + .expect("request body"); + + net.on(request).response(my_response.into()); + + let response = net.send(client.post(url)).await?; + + assert_eq!(response.status(), http::StatusCode::OK); + assert_eq!(response.bytes().await.expect("response body"), "Post OK"); + + Ok(()) }