2024-11-04 10:22:31 +00:00
|
|
|
use assert2::let_assert;
|
|
|
|
//
|
|
|
|
use kxio::net::{Error, MatchOn};
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_get_url() {
|
|
|
|
//given
|
2024-11-09 12:13:06 +00:00
|
|
|
let net = kxio::net::mock();
|
2024-11-04 10:22:31 +00:00
|
|
|
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");
|
|
|
|
|
2024-11-09 12:13:06 +00:00
|
|
|
net.on(request).respond(my_response.into()).expect("on request, respond");
|
2024-11-04 10:22:31 +00:00
|
|
|
|
|
|
|
//when
|
|
|
|
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"), "Get OK");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_get_wrong_url() {
|
|
|
|
//given
|
2024-11-09 12:13:06 +00:00
|
|
|
let net = kxio::net::mock();
|
2024-11-04 10:22:31 +00:00
|
|
|
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");
|
|
|
|
|
2024-11-09 12:13:06 +00:00
|
|
|
net.on(request).respond(my_response.into()).expect("on request, respond");
|
2024-11-04 10:22:31 +00:00
|
|
|
|
|
|
|
//when
|
|
|
|
let_assert!(
|
|
|
|
Err(Error::UnexpectedMockRequest(invalid_request)) =
|
|
|
|
net.send(client.get("https://some.other.url/")).await
|
|
|
|
);
|
|
|
|
|
|
|
|
//then
|
|
|
|
assert_eq!(invalid_request.url().to_string(), "https://some.other.url/");
|
|
|
|
|
|
|
|
// remove pending unmatched request - we never meant to match against it
|
2024-11-09 12:13:06 +00:00
|
|
|
net.reset().expect("reset");
|
2024-11-04 10:22:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_post_url() {
|
|
|
|
//given
|
2024-11-09 12:13:06 +00:00
|
|
|
let net = kxio::net::mock();
|
2024-11-04 10:22:31 +00:00
|
|
|
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-09 12:13:06 +00:00
|
|
|
net.on(request).respond(my_response.into()).expect("on request, respond");
|
2024-11-04 10:22:31 +00:00
|
|
|
|
|
|
|
//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_method() {
|
|
|
|
//given
|
2024-11-09 12:13:06 +00:00
|
|
|
let net = kxio::net::mock();
|
2024-11-04 10:22:31 +00:00
|
|
|
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
|
|
|
|
])
|
2024-11-09 12:13:06 +00:00
|
|
|
.respond(my_response.into()).expect("on request, respond");
|
2024-11-04 10:22:31 +00:00
|
|
|
|
|
|
|
//when
|
|
|
|
// This request is a different url - but should still match
|
|
|
|
let response = net
|
|
|
|
.send(client.post("https://some.other.url"))
|
|
|
|
.await
|
|
|
|
.expect("response");
|
|
|
|
|
|
|
|
//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
|
2024-11-09 12:13:06 +00:00
|
|
|
let net = kxio::net::mock();
|
2024-11-04 10:22:31 +00:00
|
|
|
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,
|
|
|
|
])
|
2024-11-09 12:13:06 +00:00
|
|
|
.respond(my_response.into()).expect("on request, respond");
|
2024-11-04 10:22:31 +00:00
|
|
|
|
|
|
|
//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]
|
|
|
|
async fn test_post_by_body() {
|
|
|
|
//given
|
2024-11-09 12:13:06 +00:00
|
|
|
let net = kxio::net::mock();
|
2024-11-04 10:22:31 +00:00
|
|
|
let client = net.client();
|
|
|
|
|
|
|
|
let url = "https://www.example.com";
|
|
|
|
let request = client
|
|
|
|
.post(url)
|
|
|
|
.body("match on body")
|
|
|
|
.build()
|
|
|
|
.expect("build request");
|
|
|
|
let my_response = net
|
|
|
|
.response()
|
|
|
|
.status(200)
|
|
|
|
.body("response body")
|
|
|
|
.expect("request body");
|
|
|
|
|
|
|
|
net.on(request)
|
|
|
|
.match_on(vec![
|
|
|
|
// MatchOn::Method,
|
|
|
|
// MatchOn::Url
|
|
|
|
MatchOn::Body,
|
|
|
|
])
|
2024-11-09 12:13:06 +00:00
|
|
|
.respond(my_response.into()).expect("on request, respond");
|
2024-11-04 10:22:31 +00:00
|
|
|
|
|
|
|
//when
|
|
|
|
// This request is a GET, not POST - but should still match
|
|
|
|
let response = net
|
|
|
|
.send(client.get("https://some.other.url").body("match on body"))
|
|
|
|
.await
|
|
|
|
.expect("response");
|
|
|
|
|
|
|
|
//then
|
|
|
|
assert_eq!(response.status(), http::StatusCode::OK);
|
|
|
|
assert_eq!(
|
|
|
|
response.bytes().await.expect("response body"),
|
|
|
|
"response body"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_post_by_headers() {
|
|
|
|
//given
|
2024-11-09 12:13:06 +00:00
|
|
|
let net = kxio::net::mock();
|
2024-11-04 10:22:31 +00:00
|
|
|
let client = net.client();
|
|
|
|
|
|
|
|
let url = "https://www.example.com";
|
|
|
|
let request = client
|
|
|
|
.post(url)
|
|
|
|
.body("foo")
|
|
|
|
.header("test", "match")
|
|
|
|
.build()
|
|
|
|
.expect("build request");
|
|
|
|
let my_response = net
|
|
|
|
.response()
|
|
|
|
.status(200)
|
|
|
|
.body("response body")
|
|
|
|
.expect("request body");
|
|
|
|
|
|
|
|
net.on(request)
|
|
|
|
.match_on(vec![
|
|
|
|
// MatchOn::Method,
|
|
|
|
// MatchOn::Url
|
|
|
|
MatchOn::Headers,
|
|
|
|
])
|
2024-11-09 12:13:06 +00:00
|
|
|
.respond(my_response.into()).expect("on request, respond");
|
2024-11-04 10:22:31 +00:00
|
|
|
|
|
|
|
//when
|
|
|
|
// This request is a GET, not POST - but should still match
|
|
|
|
let response = net
|
|
|
|
.send(
|
|
|
|
client
|
|
|
|
.get("https://some.other.url")
|
|
|
|
.body("match on body")
|
|
|
|
.header("test", "match"),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.expect("response");
|
|
|
|
|
|
|
|
//then
|
|
|
|
assert_eq!(response.status(), http::StatusCode::OK);
|
|
|
|
assert_eq!(
|
|
|
|
response.bytes().await.expect("response body"),
|
|
|
|
"response body"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
#[should_panic]
|
|
|
|
async fn test_unused_post() {
|
|
|
|
//given
|
2024-11-09 12:13:06 +00:00
|
|
|
let net = kxio::net::mock();
|
2024-11-04 10:22:31 +00:00
|
|
|
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-09 12:13:06 +00:00
|
|
|
net.on(request).respond(my_response.into()).expect("on request, respond");
|
2024-11-04 10:22:31 +00:00
|
|
|
|
|
|
|
//when
|
|
|
|
// don't send the planned request
|
|
|
|
// let _response = net.send(client.post(url)).await.expect("send");
|
|
|
|
|
|
|
|
//then
|
|
|
|
// Drop implementation for net should panic
|
|
|
|
}
|