2024-11-04 10:22:31 +00:00
|
|
|
//
|
|
|
|
use kxio::net::Error;
|
|
|
|
|
|
|
|
type TestResult = Result<(), Error>;
|
|
|
|
|
2024-11-06 20:40:56 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn test_get() -> TestResult {
|
|
|
|
let mut net = kxio::net::mock();
|
|
|
|
let client = net.client();
|
2024-11-04 10:22:31 +00:00
|
|
|
|
2024-11-06 20:40:56 +00:00
|
|
|
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");
|
2024-11-04 10:22:31 +00:00
|
|
|
|
2024-11-06 20:40:56 +00:00
|
|
|
net.on(request).response(my_response.into());
|
2024-11-04 10:22:31 +00:00
|
|
|
|
2024-11-06 20:40:56 +00:00
|
|
|
let response = net.send(client.get(url)).await?;
|
2024-11-04 10:22:31 +00:00
|
|
|
|
2024-11-06 20:40:56 +00:00
|
|
|
assert_eq!(response.status(), http::StatusCode::OK);
|
|
|
|
assert_eq!(response.bytes().await.expect("response body"), "Get OK");
|
2024-11-04 10:22:31 +00:00
|
|
|
|
2024-11-06 20:40:56 +00:00
|
|
|
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");
|
2024-11-04 10:22:31 +00:00
|
|
|
|
2024-11-06 20:40:56 +00:00
|
|
|
net.on(request).response(my_response.into());
|
2024-11-04 10:22:31 +00:00
|
|
|
|
2024-11-06 20:40:56 +00:00
|
|
|
let response = net.send(client.post(url)).await?;
|
2024-11-04 10:22:31 +00:00
|
|
|
|
2024-11-06 20:40:56 +00:00
|
|
|
assert_eq!(response.status(), http::StatusCode::OK);
|
|
|
|
assert_eq!(response.bytes().await.expect("response body"), "Post OK");
|
2024-11-04 10:22:31 +00:00
|
|
|
|
2024-11-06 20:40:56 +00:00
|
|
|
Ok(())
|
2024-11-04 10:22:31 +00:00
|
|
|
}
|