test(net): add the first tests
This commit is contained in:
parent
e6fbfb90f0
commit
82cfaf337d
2 changed files with 43 additions and 24 deletions
|
@ -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())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
48
tests/net.rs
48
tests/net.rs
|
@ -3,30 +3,48 @@ use kxio::net::Error;
|
||||||
|
|
||||||
type TestResult = Result<(), Error>;
|
type TestResult = Result<(), Error>;
|
||||||
|
|
||||||
mod get {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test() -> TestResult {
|
async fn test_get() -> 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()?;
|
|
||||||
|
|
||||||
// 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(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue