110 lines
2.7 KiB
Rust
110 lines
2.7 KiB
Rust
//
|
|
use kxio::net::{Error, MatchOn};
|
|
|
|
type TestResult = Result<(), Error>;
|
|
|
|
#[tokio::test]
|
|
async fn test_get() -> TestResult {
|
|
//given
|
|
let mut net = kxio::net::mock();
|
|
let client = net.client();
|
|
|
|
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");
|
|
|
|
net.on(request).respond(my_response.into());
|
|
|
|
//when
|
|
let response = net.send(client.get(url)).await?;
|
|
|
|
//then
|
|
assert_eq!(response.status(), http::StatusCode::OK);
|
|
assert_eq!(response.bytes().await.expect("response body"), "Get OK");
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn test_post() {
|
|
//given
|
|
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).respond(my_response.into());
|
|
|
|
//when
|
|
let response = net.send(client.post(url)).await.expect("reponse");
|
|
|
|
//then
|
|
assert_eq!(response.status(), http::StatusCode::OK);
|
|
assert_eq!(response.bytes().await.expect("response body"), "Post OK");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn test_post_by_url() {
|
|
//given
|
|
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)
|
|
.match_on(vec![
|
|
// MatchOn::Method,
|
|
MatchOn::Url
|
|
])
|
|
.respond(my_response.into());
|
|
|
|
//when
|
|
// This request is a GET, not POST - but should still match
|
|
let response = net.send(client.get(url)).await.expect("response");
|
|
|
|
//then
|
|
assert_eq!(response.status(), http::StatusCode::OK);
|
|
assert_eq!(response.bytes().await.expect("response body"), "Post OK");
|
|
}
|
|
|
|
#[tokio::test]
|
|
#[should_panic]
|
|
async fn test_unused_post() {
|
|
//given
|
|
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).respond(my_response.into());
|
|
|
|
//when
|
|
// don't send the planned request
|
|
// let _response = net.send(client.post(url)).await.expect("send");
|
|
|
|
|
|
//then
|
|
// Drop implementation for net should panic
|
|
}
|