test(net): add the first tests

This commit is contained in:
Paul Campbell 2024-11-06 20:40:56 +00:00
parent e6fbfb90f0
commit 82cfaf337d
2 changed files with 43 additions and 24 deletions

View file

@ -76,9 +76,10 @@ impl Net<Mocked> {
fn _on(&mut self, request: reqwest::Request, response: reqwest::Response) { fn _on(&mut self, request: reqwest::Request, response: reqwest::Response) {
self.plans.push(Plan { request, response }) self.plans.push(Plan { request, response })
} }
}
pub fn assert(&self) -> Result<()> { impl<T: NetType> Drop for Net<T> {
todo!() fn drop(&mut self) {
assert!(self.plans.is_empty())
} }
} }

View file

@ -3,30 +3,48 @@ use kxio::net::Error;
type TestResult = Result<(), Error>; type TestResult = Result<(), Error>;
mod get { #[tokio::test]
use super::*; async fn test_get() -> TestResult {
let mut net = kxio::net::mock();
let client = net.client();
#[tokio::test] let url = "https://www.example.com";
async fn test() -> TestResult { let request = client.get(url).build().expect("build request");
let mut net = kxio::net::mock(); let my_response = net
.response()
.status(200)
.body("Get OK")
.expect("request body");
let url = "https://www.example.com"; net.on(request).response(my_response.into());
let request = net.client().get(url).build()?;
let my_response = net.response().status(200).body("OK").unwrap();
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?; Ok(())
}
assert_eq!(response.status(), my_response.status());
#[tokio::test]
// net_mock.assert()?; async fn test_post() -> TestResult {
let mut net = kxio::net::mock();
// let my_net = net::new(); let client = net.client();
// my_net.send(request).await?;
let url = "https://www.example.com";
Ok(()) 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(())
} }