kxio/tests/net.rs

268 lines
6.8 KiB
Rust
Raw Normal View History

use assert2::let_assert;
//
2024-11-11 07:44:43 +00:00
use kxio::net::{Error, MatchOn, MockNet, Net};
#[tokio::test]
async fn test_get_url() {
//given
let net = kxio::net::mock();
let client = net.client();
let url = "https://www.example.com";
2024-11-10 17:11:54 +00:00
let request = client.get(url);
let my_response = net.response().status(200).body("Get OK");
net.on(request)
2024-11-10 17:11:54 +00:00
.respond_with_body(my_response)
.expect("on request, respond");
//when
let response = Net::from(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-11 07:44:43 +00:00
let mock_net = kxio::net::mock();
let client = mock_net.client();
let url = "https://www.example.com";
2024-11-10 17:11:54 +00:00
let request = client.get(url);
2024-11-11 07:44:43 +00:00
let my_response = mock_net.response().status(200).body("Get OK");
2024-11-11 07:44:43 +00:00
mock_net
.on(request)
2024-11-10 17:11:54 +00:00
.respond_with_body(my_response)
.expect("on request, respond");
2024-11-11 07:44:43 +00:00
let net = Net::from(mock_net);
//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-11 07:44:43 +00:00
let mock_net = MockNet::try_from(net).expect("recover net");
mock_net.reset();
}
#[tokio::test]
async fn test_post_url() {
//given
let net = kxio::net::mock();
let client = net.client();
let url = "https://www.example.com";
2024-11-10 17:11:54 +00:00
let request = client.post(url);
let my_response = net.response().status(200).body("Post OK");
net.on(request)
2024-11-10 17:11:54 +00:00
.respond_with_body(my_response)
.expect("on request, respond");
//when
let response = Net::from(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
let net = kxio::net::mock();
let client = net.client();
let url = "https://www.example.com";
2024-11-10 17:11:54 +00:00
let request = client.post(url);
let my_response = net.response().status(200);
net.on(request)
.match_on(vec![
MatchOn::Method,
// MatchOn::Url
])
.respond(my_response)
.expect("on request, respond");
//when
// This request is a different url - but should still match
let response = Net::from(net)
.send(client.post("https://some.other.url"))
.await
.expect("response");
//then
assert_eq!(response.status(), http::StatusCode::OK);
2024-11-10 17:11:54 +00:00
assert_eq!(response.bytes().await.expect("response body"), "");
}
#[tokio::test]
async fn test_post_by_url() {
//given
let net = kxio::net::mock();
let client = net.client();
let url = "https://www.example.com";
2024-11-10 17:11:54 +00:00
let request = client.post(url);
let my_response = net.response().status(200).body("Post OK");
net.on(request)
.match_on(vec![
// MatchOn::Method,
MatchOn::Url,
])
2024-11-10 17:11:54 +00:00
.respond_with_body(my_response)
.expect("on request, respond");
//when
// This request is a GET, not POST - but should still match
let response = Net::from(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
let net = kxio::net::mock();
let client = net.client();
let url = "https://www.example.com";
2024-11-10 17:11:54 +00:00
let request = client.post(url).body("match on body");
let my_response = net.response().status(200).body("response body");
net.on(request)
.match_on(vec![
// MatchOn::Method,
// MatchOn::Url
MatchOn::Body,
])
2024-11-10 17:11:54 +00:00
.respond_with_body(my_response)
.expect("on request, respond");
//when
// This request is a GET, not POST - but should still match
let response = Net::from(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
let net = kxio::net::mock();
let client = net.client();
let url = "https://www.example.com";
2024-11-10 17:11:54 +00:00
let request = client.post(url).body("foo").header("test", "match");
let my_response = net.response().status(200).body("response body");
net.on(request)
.match_on(vec![
// MatchOn::Method,
// MatchOn::Url
MatchOn::Headers,
])
2024-11-10 17:11:54 +00:00
.respond_with_body(my_response)
.expect("on request, respond");
//when
// This request is a GET, not POST - but should still match
let response = Net::from(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]
2024-11-11 07:44:43 +00:00
async fn test_unused_post_as_net() {
//given
2024-11-11 07:44:43 +00:00
let mock_net = kxio::net::mock();
let client = mock_net.client();
let url = "https://www.example.com";
2024-11-10 17:11:54 +00:00
let request = client.post(url);
2024-11-11 07:44:43 +00:00
let my_response = mock_net.response().status(200).body("Post OK");
2024-11-11 07:44:43 +00:00
mock_net
.on(request)
2024-11-10 17:11:54 +00:00
.respond_with_body(my_response)
.expect("on request, respond");
2024-11-11 07:44:43 +00:00
let _net = Net::from(mock_net);
//when
// don't send the planned request
// let _response = Net::from(net).send(client.post(url)).await.expect("send");
//then
// Drop implementation for net should panic
}
2024-11-11 07:44:43 +00:00
#[tokio::test]
#[should_panic]
async fn test_unused_post_as_mocknet() {
//given
let mock_net = kxio::net::mock();
let client = mock_net.client();
let url = "https://www.example.com";
let request = client.post(url);
let my_response = mock_net.response().status(200).body("Post OK");
mock_net
.on(request)
.respond_with_body(my_response)
.expect("on request, respond");
//when
// don't send the planned request
// let _response = Net::from(net).send(client.post(url)).await.expect("send");
//then
// Drop implementation for mock_net should panic
}