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 {
#[tokio::test]
async fn test() -> TestResult {
let mut net = kxio::net::mock(); let mut net = kxio::net::mock();
let client = net.client();
let url = "https://www.example.com"; let url = "https://www.example.com";
let request = net.client().get(url).build()?; let request = client.get(url).build().expect("build request");
let my_response = net.response().status(200).body("OK").unwrap(); let my_response = net
.response()
.status(200)
.body("Get OK")
.expect("request body");
net.on(request).response(my_response.clone().into()); net.on(request).response(my_response.into());
let client = net.client();
let response = net.send(client.get(url)).await?; let response = net.send(client.get(url)).await?;
assert_eq!(response.status(), my_response.status()); assert_eq!(response.status(), http::StatusCode::OK);
assert_eq!(response.bytes().await.expect("response body"), "Get OK");
// net_mock.assert()?;
Ok(())
// let my_net = net::new(); }
// my_net.send(request).await?;
#[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(()) Ok(())
}
} }