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) {
|
||||
self.plans.push(Plan { request, response })
|
||||
}
|
||||
|
||||
pub fn assert(&self) -> Result<()> {
|
||||
todo!()
|
||||
}
|
||||
impl<T: NetType> Drop for Net<T> {
|
||||
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>;
|
||||
|
||||
mod get {
|
||||
use super::*;
|
||||
|
||||
#[tokio::test]
|
||||
async fn test() -> TestResult {
|
||||
async fn test_get() -> TestResult {
|
||||
let mut net = kxio::net::mock();
|
||||
let client = net.client();
|
||||
|
||||
let url = "https://www.example.com";
|
||||
let request = net.client().get(url).build()?;
|
||||
let my_response = net.response().status(200).body("OK").unwrap();
|
||||
let request = client.get(url).build().expect("build request");
|
||||
let my_response = net
|
||||
.response()
|
||||
.status(200)
|
||||
.body("Get OK")
|
||||
.expect("request body");
|
||||
|
||||
net.on(request).response(my_response.clone().into());
|
||||
|
||||
let client = net.client();
|
||||
net.on(request).response(my_response.into());
|
||||
|
||||
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?;
|
||||
assert_eq!(response.status(), http::StatusCode::OK);
|
||||
assert_eq!(response.bytes().await.expect("response body"), "Get 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