2024-11-17 11:24:28 +00:00
|
|
|
use http::StatusCode;
|
2024-11-04 10:22:31 +00:00
|
|
|
//
|
2024-11-17 09:18:18 +00:00
|
|
|
use kxio::net::{Error, MockNet, Net};
|
2024-11-15 09:06:45 +00:00
|
|
|
|
|
|
|
use assert2::let_assert;
|
2024-11-04 10:22:31 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_get_url() {
|
|
|
|
//given
|
2024-11-12 07:13:59 +00:00
|
|
|
let mock_net = kxio::net::mock();
|
2024-11-04 10:22:31 +00:00
|
|
|
|
|
|
|
let url = "https://www.example.com";
|
2024-12-01 20:58:05 +00:00
|
|
|
let url_alpha = format!("{url}/alpha");
|
|
|
|
let url_beta = format!("{url}/beta");
|
2024-11-04 10:22:31 +00:00
|
|
|
|
2024-12-01 20:58:05 +00:00
|
|
|
mock_net
|
|
|
|
.on()
|
|
|
|
.get(&url_alpha)
|
|
|
|
.respond(StatusCode::OK)
|
|
|
|
.body("Get OK alpha")
|
|
|
|
.expect("mock alpha");
|
|
|
|
mock_net
|
|
|
|
.on()
|
|
|
|
.get(&url_beta)
|
|
|
|
.respond(StatusCode::OK)
|
|
|
|
.body("Get OK beta")
|
|
|
|
.expect("mock beta");
|
2024-11-12 07:13:59 +00:00
|
|
|
mock_net
|
2024-11-17 09:18:18 +00:00
|
|
|
.on()
|
2024-11-17 09:18:18 +00:00
|
|
|
.get(url)
|
2024-11-17 11:24:28 +00:00
|
|
|
.respond(StatusCode::OK)
|
2024-11-17 09:18:18 +00:00
|
|
|
.body("Get OK")
|
|
|
|
.expect("mock");
|
2024-12-01 20:58:05 +00:00
|
|
|
let net = Net::from(mock_net);
|
2024-11-04 10:22:31 +00:00
|
|
|
|
|
|
|
//when
|
2024-12-01 20:58:05 +00:00
|
|
|
let response_alpha = net.get(url_alpha).send().await.expect("response alpha");
|
|
|
|
let response_beta = net.get(url_beta).send().await.expect("response beta");
|
|
|
|
let response = net.get(url).send().await.expect("response");
|
2024-11-04 10:22:31 +00:00
|
|
|
|
|
|
|
//then
|
2024-12-01 20:58:05 +00:00
|
|
|
assert_eq!(response_alpha.status(), http::StatusCode::OK);
|
|
|
|
assert_eq!(
|
|
|
|
response_alpha.bytes().await.expect("response body alpha"),
|
|
|
|
"Get OK alpha"
|
|
|
|
);
|
|
|
|
assert_eq!(response_beta.status(), http::StatusCode::OK);
|
|
|
|
assert_eq!(
|
|
|
|
response_beta.bytes().await.expect("response body beta"),
|
|
|
|
"Get OK beta"
|
|
|
|
);
|
2024-11-04 10:22:31 +00:00
|
|
|
assert_eq!(response.status(), http::StatusCode::OK);
|
|
|
|
assert_eq!(response.bytes().await.expect("response body"), "Get OK");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
2024-11-17 09:18:18 +00:00
|
|
|
async fn test_post_url() {
|
2024-11-04 10:22:31 +00:00
|
|
|
//given
|
2024-11-17 09:18:18 +00:00
|
|
|
let net = kxio::net::mock();
|
|
|
|
let client = net.client();
|
2024-11-04 10:22:31 +00:00
|
|
|
|
|
|
|
let url = "https://www.example.com";
|
|
|
|
|
2024-11-17 09:18:18 +00:00
|
|
|
net.on()
|
|
|
|
.post(url)
|
|
|
|
.respond(StatusCode::OK)
|
|
|
|
.body("post OK")
|
|
|
|
.expect("mock");
|
2024-11-04 10:22:31 +00:00
|
|
|
|
2024-11-17 09:18:18 +00:00
|
|
|
//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_put_url() {
|
|
|
|
//given
|
|
|
|
let net = kxio::net::mock();
|
|
|
|
let client = net.client();
|
|
|
|
|
|
|
|
let url = "https://www.example.com";
|
|
|
|
|
2024-11-17 09:18:18 +00:00
|
|
|
net.on()
|
|
|
|
.put(url)
|
|
|
|
.respond(StatusCode::OK)
|
|
|
|
.body("put OK")
|
|
|
|
.expect("mock");
|
2024-11-11 07:44:43 +00:00
|
|
|
|
2024-11-04 10:22:31 +00:00
|
|
|
//when
|
2024-11-17 09:18:18 +00:00
|
|
|
let response = Net::from(net).send(client.put(url)).await.expect("reponse");
|
2024-11-04 10:22:31 +00:00
|
|
|
|
|
|
|
//then
|
2024-11-17 09:18:18 +00:00
|
|
|
assert_eq!(response.status(), http::StatusCode::OK);
|
|
|
|
assert_eq!(response.bytes().await.expect("response body"), "put OK");
|
|
|
|
}
|
2024-11-04 10:22:31 +00:00
|
|
|
|
2024-11-17 09:18:18 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn test_delete_url() {
|
|
|
|
//given
|
|
|
|
let net = kxio::net::mock();
|
|
|
|
let client = net.client();
|
|
|
|
|
|
|
|
let url = "https://www.example.com";
|
|
|
|
|
|
|
|
net.on()
|
|
|
|
.delete(url)
|
2024-11-17 11:24:28 +00:00
|
|
|
.respond(StatusCode::OK)
|
2024-11-17 09:18:18 +00:00
|
|
|
.body("delete OK")
|
|
|
|
.expect("mock");
|
2024-11-17 09:18:18 +00:00
|
|
|
|
|
|
|
//when
|
|
|
|
let response = Net::from(net)
|
|
|
|
.send(client.delete(url))
|
|
|
|
.await
|
|
|
|
.expect("reponse");
|
|
|
|
|
|
|
|
//then
|
|
|
|
assert_eq!(response.status(), http::StatusCode::OK);
|
|
|
|
assert_eq!(response.bytes().await.expect("response body"), "delete OK");
|
2024-11-04 10:22:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
2024-11-17 09:18:18 +00:00
|
|
|
async fn test_head_url() {
|
2024-11-04 10:22:31 +00:00
|
|
|
//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";
|
|
|
|
|
2024-11-17 09:18:18 +00:00
|
|
|
net.on()
|
|
|
|
.head(url)
|
|
|
|
.respond(StatusCode::OK)
|
|
|
|
.body("head OK")
|
|
|
|
.expect("mock");
|
2024-11-04 10:22:31 +00:00
|
|
|
|
|
|
|
//when
|
2024-11-09 12:12:11 +00:00
|
|
|
let response = Net::from(net)
|
2024-11-17 09:18:18 +00:00
|
|
|
.send(client.head(url))
|
|
|
|
.await
|
|
|
|
.expect("reponse");
|
|
|
|
|
|
|
|
//then
|
|
|
|
assert_eq!(response.status(), http::StatusCode::OK);
|
|
|
|
assert_eq!(response.bytes().await.expect("response body"), "head OK");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_patch_url() {
|
|
|
|
//given
|
|
|
|
let net = kxio::net::mock();
|
|
|
|
let client = net.client();
|
|
|
|
|
|
|
|
let url = "https://www.example.com";
|
|
|
|
|
2024-11-17 09:18:18 +00:00
|
|
|
net.on()
|
|
|
|
.patch(url)
|
|
|
|
.respond(StatusCode::OK)
|
|
|
|
.body("patch OK")
|
|
|
|
.expect("mock");
|
2024-11-17 09:18:18 +00:00
|
|
|
|
|
|
|
//when
|
|
|
|
let response = Net::from(net)
|
|
|
|
.send(client.patch(url))
|
2024-11-09 12:12:11 +00:00
|
|
|
.await
|
|
|
|
.expect("reponse");
|
2024-11-04 10:22:31 +00:00
|
|
|
|
|
|
|
//then
|
|
|
|
assert_eq!(response.status(), http::StatusCode::OK);
|
2024-11-17 09:18:18 +00:00
|
|
|
assert_eq!(response.bytes().await.expect("response body"), "patch OK");
|
|
|
|
}
|
|
|
|
|
2024-12-01 20:58:05 +00:00
|
|
|
// #[tokio::test]
|
|
|
|
// async fn test_get_auth_url() {
|
|
|
|
// //given
|
|
|
|
// let net = kxio::net::mock();
|
|
|
|
//
|
|
|
|
// let url = "https://user:pass@www.example.com";
|
|
|
|
//
|
|
|
|
// net.on()
|
|
|
|
// .get(url)
|
|
|
|
// .respond(StatusCode::OK)
|
|
|
|
// .body("post OK")
|
|
|
|
// .expect("mock");
|
|
|
|
//
|
|
|
|
// //when
|
|
|
|
// let response = Net::from(net).get(url).send().await.expect("reponse");
|
|
|
|
//
|
|
|
|
// //then
|
|
|
|
// assert_eq!(response.status(), http::StatusCode::OK);
|
|
|
|
// assert_eq!(response.bytes().await.expect("response body"), "post OK");
|
|
|
|
// }
|
|
|
|
|
2024-11-17 09:18:18 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn test_get_wrong_url() {
|
|
|
|
//given
|
2024-11-17 11:24:28 +00:00
|
|
|
let net = kxio::net::mock();
|
|
|
|
let client = net.client();
|
2024-11-17 09:18:18 +00:00
|
|
|
|
|
|
|
let url = "https://www.example.com";
|
|
|
|
|
2024-11-17 09:18:18 +00:00
|
|
|
net.on()
|
|
|
|
.get(url)
|
|
|
|
.respond(StatusCode::OK)
|
|
|
|
.body("Get OK")
|
|
|
|
.expect("mock");
|
2024-11-17 09:18:18 +00:00
|
|
|
|
2024-11-17 11:24:28 +00:00
|
|
|
let net = Net::from(net);
|
2024-11-17 09:18:18 +00:00
|
|
|
|
|
|
|
//when
|
|
|
|
let_assert!(
|
2024-12-04 22:38:44 +00:00
|
|
|
Err(Error::UnexpectedMockRequest { request }) =
|
2024-11-17 09:18:18 +00:00
|
|
|
net.send(client.get("https://some.other.url/")).await
|
|
|
|
);
|
|
|
|
|
|
|
|
//then
|
2024-12-04 22:38:44 +00:00
|
|
|
assert_eq!(request.url().to_string(), "https://some.other.url/");
|
2024-11-17 09:18:18 +00:00
|
|
|
|
|
|
|
// remove pending unmatched request - we never meant to match against it
|
|
|
|
let mock_net = MockNet::try_from(net).await.expect("recover net");
|
|
|
|
mock_net.reset();
|
2024-11-04 10:22:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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();
|
|
|
|
|
2024-11-17 11:24:28 +00:00
|
|
|
// NOTE: No URL specified - so should match any URL
|
2024-11-17 09:18:18 +00:00
|
|
|
net.on().respond(StatusCode::OK).body("").expect("mock");
|
2024-11-04 10:22:31 +00:00
|
|
|
|
|
|
|
//when
|
2024-11-09 12:12:11 +00:00
|
|
|
let response = Net::from(net)
|
2024-11-04 10:22:31 +00:00
|
|
|
.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"), "");
|
2024-11-04 10:22:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
2024-11-12 07:13:59 +00:00
|
|
|
async fn test_post_by_body() {
|
2024-11-04 10:22:31 +00:00
|
|
|
//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();
|
|
|
|
|
2024-11-17 11:24:28 +00:00
|
|
|
// No URL - so any POST with a matching body
|
2024-11-17 09:18:18 +00:00
|
|
|
net.on()
|
2024-11-12 07:13:59 +00:00
|
|
|
.body("match on body")
|
2024-11-17 11:24:28 +00:00
|
|
|
.respond(StatusCode::OK)
|
2024-11-17 09:18:18 +00:00
|
|
|
.body("response body")
|
|
|
|
.expect("mock");
|
2024-11-04 10:22:31 +00:00
|
|
|
|
|
|
|
//when
|
2024-11-09 12:12:11 +00:00
|
|
|
let response = Net::from(net)
|
2024-11-12 07:13:59 +00:00
|
|
|
.send(client.post("https://some.other.url").body("match on body"))
|
2024-11-09 12:12:11 +00:00
|
|
|
.await
|
|
|
|
.expect("response");
|
2024-11-04 10:22:31 +00:00
|
|
|
|
|
|
|
//then
|
|
|
|
assert_eq!(response.status(), http::StatusCode::OK);
|
2024-11-12 07:13:59 +00:00
|
|
|
assert_eq!(
|
|
|
|
response.bytes().await.expect("response body"),
|
|
|
|
"response body"
|
|
|
|
);
|
2024-11-04 10:22:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
2024-11-12 07:13:59 +00:00
|
|
|
async fn test_post_by_header() {
|
2024-11-04 10:22:31 +00:00
|
|
|
//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();
|
|
|
|
|
2024-11-17 11:24:28 +00:00
|
|
|
net.on()
|
|
|
|
.header("test", "match")
|
|
|
|
.respond(StatusCode::OK)
|
2024-11-17 09:18:18 +00:00
|
|
|
.body("response body")
|
|
|
|
.expect("mock");
|
2024-11-04 10:22:31 +00:00
|
|
|
|
|
|
|
//when
|
2024-11-09 12:12:11 +00:00
|
|
|
let response = Net::from(net)
|
2024-11-12 07:13:59 +00:00
|
|
|
.send(
|
|
|
|
client
|
|
|
|
.post("https://some.other.url")
|
2024-12-01 20:58:05 +00:00
|
|
|
.body("any body")
|
2024-11-12 07:13:59 +00:00
|
|
|
.header("test", "match"),
|
|
|
|
)
|
2024-11-04 10:22:31 +00:00
|
|
|
.await
|
|
|
|
.expect("response");
|
|
|
|
|
|
|
|
//then
|
|
|
|
assert_eq!(response.status(), http::StatusCode::OK);
|
|
|
|
assert_eq!(
|
|
|
|
response.bytes().await.expect("response body"),
|
|
|
|
"response body"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
2024-11-12 07:13:59 +00:00
|
|
|
async fn test_post_by_header_wrong_value() {
|
2024-11-04 10:22:31 +00:00
|
|
|
//given
|
2024-11-12 07:13:59 +00:00
|
|
|
let mock_net = kxio::net::mock();
|
|
|
|
let client = mock_net.client();
|
2024-11-04 10:22:31 +00:00
|
|
|
|
2024-11-17 11:24:28 +00:00
|
|
|
mock_net
|
|
|
|
.on()
|
|
|
|
.header("test", "match")
|
|
|
|
.respond(StatusCode::OK)
|
2024-11-17 09:18:18 +00:00
|
|
|
.body("response body")
|
|
|
|
.expect("mock");
|
2024-11-12 07:13:59 +00:00
|
|
|
let net = Net::from(mock_net);
|
2024-11-04 10:22:31 +00:00
|
|
|
|
|
|
|
//when
|
2024-11-12 07:13:59 +00:00
|
|
|
let response = net
|
2024-11-04 10:22:31 +00:00
|
|
|
.send(
|
|
|
|
client
|
2024-11-12 07:13:59 +00:00
|
|
|
.post("https://some.other.url")
|
2024-12-01 20:58:05 +00:00
|
|
|
.body("any body")
|
2024-11-12 07:13:59 +00:00
|
|
|
.header("test", "no match"),
|
2024-11-04 10:22:31 +00:00
|
|
|
)
|
2024-11-12 07:13:59 +00:00
|
|
|
.await;
|
2024-11-04 10:22:31 +00:00
|
|
|
|
|
|
|
//then
|
2024-12-04 22:38:44 +00:00
|
|
|
let_assert!(Err(kxio::net::Error::UnexpectedMockRequest { request: _ }) = response);
|
2024-11-12 07:13:59 +00:00
|
|
|
|
feat: Add Debug, Clone, Default, PartialEq, Eq, Send, Sync to as many or our types as possible.
- adds tokio::sync as a dependency to provide an async Mutex for Clone of Net and MockNet
## ƒs
- adds `Clone` to `DirItem`
- adds `Default`, `PartialEq` and `Eq` to `FileSystem`, `PathMarker`, `FileMarker` and `DirMarker`
- adds `Default` to `PathReal`
- adds `Clone`, `Debug`, `Default`, `PartialEq` and `Eq` to `Reader`
## net
- `MockNet::try_from` now returns a `Future`, so should be `await`ed
- adds `Debug` to `Plan`
- adds `Debug` and `Default` to `Net`
- adds `Debug`, `Clone` and `Default` to `MockNet`
- adds `Debug`, `Clone`, `PartialEq` and `Eq` to `MatchRequest`
- adds `Debug` and `Clone` to `WhenRequest`
WIP: mutation tests
2024-11-16 08:46:07 +00:00
|
|
|
MockNet::try_from(net).await.expect("recover mock").reset();
|
2024-11-04 10:22:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
2024-12-07 20:37:05 +00:00
|
|
|
#[should_panic(expected = "1 expected requests were not made")]
|
2024-11-11 07:44:43 +00:00
|
|
|
async fn test_unused_post_as_net() {
|
2024-11-04 10:22:31 +00:00
|
|
|
//given
|
2024-11-11 07:44:43 +00:00
|
|
|
let mock_net = kxio::net::mock();
|
2024-11-04 10:22:31 +00:00
|
|
|
|
|
|
|
let url = "https://www.example.com";
|
|
|
|
|
2024-11-17 11:24:28 +00:00
|
|
|
mock_net
|
|
|
|
.on()
|
|
|
|
.post(url)
|
|
|
|
.respond(StatusCode::OK)
|
2024-11-17 09:18:18 +00:00
|
|
|
.body("Post OK")
|
|
|
|
.expect("mock");
|
2024-11-04 10:22:31 +00:00
|
|
|
|
2024-12-07 20:37:05 +00:00
|
|
|
let net = Net::from(mock_net);
|
2024-11-11 07:44:43 +00:00
|
|
|
|
2024-11-04 10:22:31 +00:00
|
|
|
//when
|
|
|
|
// don't send the planned request
|
2024-12-07 20:37:05 +00:00
|
|
|
// let _response = net.send(client.post(url)).await.expect("send");
|
2024-11-04 10:22:31 +00:00
|
|
|
|
|
|
|
//then
|
2024-12-07 20:37:05 +00:00
|
|
|
net.assert_no_unused_plans();
|
2024-11-04 10:22:31 +00:00
|
|
|
}
|
2024-11-11 07:44:43 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
2024-12-07 20:37:05 +00:00
|
|
|
#[should_panic(expected = "1 expected requests were not made")]
|
2024-11-11 07:44:43 +00:00
|
|
|
async fn test_unused_post_as_mocknet() {
|
|
|
|
//given
|
|
|
|
let mock_net = kxio::net::mock();
|
|
|
|
|
|
|
|
let url = "https://www.example.com";
|
|
|
|
|
2024-11-17 11:24:28 +00:00
|
|
|
mock_net
|
|
|
|
.on()
|
|
|
|
.post(url)
|
|
|
|
.respond(StatusCode::OK)
|
2024-11-17 09:18:18 +00:00
|
|
|
.body("Post OK")
|
|
|
|
.expect("mock");
|
2024-11-11 07:44:43 +00:00
|
|
|
|
|
|
|
//when
|
|
|
|
// don't send the planned request
|
2024-12-07 20:37:05 +00:00
|
|
|
// let _response = mock_net.send(client.post(url)).await.expect("send");
|
2024-11-11 07:44:43 +00:00
|
|
|
|
|
|
|
//then
|
2024-12-07 20:37:05 +00:00
|
|
|
mock_net.assert_no_unused_plans();
|
2024-11-11 07:44:43 +00:00
|
|
|
}
|
2024-12-01 20:58:05 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_get_url_with_fragment() {
|
|
|
|
//given
|
|
|
|
let net = kxio::net::mock();
|
|
|
|
let client = net.client();
|
|
|
|
|
|
|
|
let url = "https://www.example.com#test";
|
|
|
|
|
|
|
|
net.on()
|
|
|
|
.get(url)
|
|
|
|
.respond(StatusCode::OK)
|
|
|
|
.body("post OK")
|
|
|
|
.expect("mock");
|
|
|
|
|
|
|
|
//when
|
|
|
|
let response = Net::from(net).send(client.get(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_get_with_query_parameters() {
|
|
|
|
//given
|
|
|
|
let mock_net = kxio::net::mock();
|
|
|
|
let url = "https://www.example.com/path";
|
|
|
|
|
|
|
|
mock_net
|
|
|
|
.on()
|
|
|
|
.get(url)
|
|
|
|
.query("key-1", "value-1")
|
|
|
|
.respond(StatusCode::OK)
|
|
|
|
.body("with query parameters 1/1")
|
|
|
|
.expect("mock");
|
|
|
|
mock_net
|
|
|
|
.on()
|
|
|
|
.get(url)
|
|
|
|
.query("key-1", "value-2")
|
|
|
|
.respond(StatusCode::OK)
|
|
|
|
.body("with query parameters 1/2")
|
|
|
|
.expect("mock");
|
|
|
|
mock_net
|
|
|
|
.on()
|
|
|
|
.get(url)
|
|
|
|
.query("key-2", "value-2")
|
|
|
|
.respond(StatusCode::OK)
|
|
|
|
.body("with query parameters 2/2")
|
|
|
|
.expect("mock");
|
|
|
|
mock_net
|
|
|
|
.on()
|
|
|
|
.get(url)
|
|
|
|
.respond(StatusCode::OK)
|
|
|
|
.body("sans query parameters")
|
|
|
|
.expect("mock");
|
|
|
|
let net = Net::from(mock_net);
|
|
|
|
|
|
|
|
//when
|
|
|
|
// The order of 12 nad 11 should be in that order to ensure we test the discrimination of the
|
|
|
|
// query value when the keys are the same
|
|
|
|
let response_with_12 = net
|
|
|
|
.get(url)
|
|
|
|
.query("key-1", "value-2")
|
|
|
|
.send()
|
|
|
|
.await
|
|
|
|
.expect("response with qp 1/2");
|
|
|
|
let response_with_11 = net
|
|
|
|
.get(url)
|
|
|
|
.query("key-1", "value-1")
|
|
|
|
.send()
|
|
|
|
.await
|
|
|
|
.expect("response with qp 1/1");
|
|
|
|
let response_with_22 = net
|
|
|
|
.get(url)
|
|
|
|
.query("key-2", "value-2")
|
|
|
|
.send()
|
|
|
|
.await
|
|
|
|
.expect("response with qp 2/2");
|
|
|
|
let response_sans_qp = net.get(url).send().await.expect("response sans qp");
|
|
|
|
|
|
|
|
//then
|
|
|
|
assert_eq!(
|
|
|
|
response_with_11.bytes().await.expect("with qp 1/1 body"),
|
|
|
|
"with query parameters 1/1"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
response_with_12.bytes().await.expect("with qp 1/2 body"),
|
|
|
|
"with query parameters 1/2"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
response_with_22.bytes().await.expect("with qp 2/2 body"),
|
|
|
|
"with query parameters 2/2"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
response_sans_qp.bytes().await.expect("sans qp body"),
|
|
|
|
"sans query parameters"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_get_with_duplicate_query_keys() {
|
|
|
|
//given
|
|
|
|
let mock_net = kxio::net::mock();
|
|
|
|
let url = "https://www.example.com/path";
|
|
|
|
|
|
|
|
mock_net
|
|
|
|
.on()
|
|
|
|
.get(url)
|
|
|
|
.query("key", "value-1")
|
|
|
|
.query("key", "value-2")
|
|
|
|
.respond(StatusCode::OK)
|
|
|
|
.body("key:value-1,value-2")
|
|
|
|
.expect("mock");
|
|
|
|
mock_net
|
|
|
|
.on()
|
|
|
|
.get(url)
|
|
|
|
.query("key", "value-3")
|
|
|
|
.query("key", "value-4")
|
|
|
|
.respond(StatusCode::OK)
|
|
|
|
.body("key:value-3,value-4")
|
|
|
|
.expect("mock");
|
|
|
|
let net = Net::from(mock_net);
|
|
|
|
|
|
|
|
//when
|
|
|
|
let response_a = net
|
|
|
|
.get(url)
|
|
|
|
.query("key", "value-2")
|
|
|
|
.query("key", "value-1")
|
|
|
|
.send()
|
|
|
|
.await
|
|
|
|
.expect("response a");
|
|
|
|
let response_b = net
|
|
|
|
.get(url)
|
|
|
|
.query("key", "value-3")
|
|
|
|
.query("key", "value-4")
|
|
|
|
.send()
|
|
|
|
.await
|
|
|
|
.expect("response b");
|
|
|
|
|
|
|
|
//then
|
|
|
|
assert_eq!(
|
|
|
|
response_a.bytes().await.expect("response a bytes"),
|
|
|
|
"key:value-1,value-2"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
response_b.bytes().await.expect("response b bytes"),
|
|
|
|
"key:value-3,value-4"
|
|
|
|
);
|
|
|
|
}
|
2024-12-01 20:58:05 +00:00
|
|
|
|
2024-12-01 20:58:05 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn test_get_with_basic_auth() {
|
|
|
|
//given
|
|
|
|
let mock_net = kxio::net::mock();
|
|
|
|
let url = "https://www.example.com/path";
|
|
|
|
|
|
|
|
mock_net
|
|
|
|
.on()
|
|
|
|
.get(url)
|
|
|
|
.basic_auth("bob", Some("secret"))
|
|
|
|
.respond(StatusCode::OK)
|
|
|
|
.mock()
|
|
|
|
.expect("mock");
|
|
|
|
mock_net
|
|
|
|
.on()
|
|
|
|
.get(url)
|
|
|
|
.basic_auth("bob", None::<String>)
|
|
|
|
.respond(StatusCode::FORBIDDEN)
|
|
|
|
.mock()
|
|
|
|
.expect("mock");
|
|
|
|
let net = Net::from(mock_net);
|
|
|
|
|
|
|
|
//when
|
|
|
|
let invalid = net.get(url).basic_auth("bob", None::<String>).send().await;
|
|
|
|
let valid = net
|
|
|
|
.get(url)
|
|
|
|
.basic_auth("bob", Some("secret"))
|
|
|
|
.send()
|
|
|
|
.await
|
|
|
|
.expect("valid response");
|
|
|
|
|
|
|
|
//then
|
|
|
|
let_assert!(Err(Error::ResponseError { response }) = invalid);
|
2024-12-01 20:58:05 +00:00
|
|
|
assert_eq!(response.status(), StatusCode::FORBIDDEN);
|
|
|
|
assert_eq!(valid.status(), StatusCode::OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_get_with_bearer_auth() {
|
|
|
|
//given
|
|
|
|
let mock_net = kxio::net::mock();
|
|
|
|
let url = "https://www.example.com/path";
|
|
|
|
|
|
|
|
mock_net
|
|
|
|
.on()
|
|
|
|
.get(url)
|
|
|
|
.bearer_auth("token")
|
|
|
|
.respond(StatusCode::OK)
|
|
|
|
.mock()
|
|
|
|
.expect("mock");
|
|
|
|
mock_net
|
|
|
|
.on()
|
|
|
|
.get(url)
|
|
|
|
.bearer_auth("invalid")
|
|
|
|
.respond(StatusCode::FORBIDDEN)
|
|
|
|
.mock()
|
|
|
|
.expect("mock");
|
|
|
|
let net = Net::from(mock_net);
|
|
|
|
|
|
|
|
//when
|
|
|
|
let invalid = net.get(url).bearer_auth("invalid").send().await;
|
|
|
|
let valid = net
|
|
|
|
.get(url)
|
|
|
|
.bearer_auth("token")
|
|
|
|
.send()
|
|
|
|
.await
|
|
|
|
.expect("valid response");
|
|
|
|
|
|
|
|
//then
|
|
|
|
let_assert!(Err(Error::ResponseError { response }) = invalid);
|
2024-12-01 20:58:05 +00:00
|
|
|
assert_eq!(response.status(), StatusCode::FORBIDDEN);
|
|
|
|
assert_eq!(valid.status(), StatusCode::OK);
|
|
|
|
}
|
|
|
|
|
2024-12-01 20:58:05 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn test_get_with_user_agent() {
|
|
|
|
//given
|
|
|
|
let mock_net = kxio::net::mock();
|
|
|
|
let url = "https://www.example.com/path";
|
|
|
|
|
|
|
|
mock_net
|
|
|
|
.on()
|
|
|
|
.get(url)
|
|
|
|
.user_agent("007")
|
|
|
|
.respond(StatusCode::OK)
|
|
|
|
.mock()
|
|
|
|
.expect("mock");
|
|
|
|
mock_net
|
|
|
|
.on()
|
|
|
|
.get(url)
|
|
|
|
.user_agent("orange")
|
|
|
|
.respond(StatusCode::FORBIDDEN)
|
|
|
|
.mock()
|
|
|
|
.expect("mock");
|
|
|
|
let net = Net::from(mock_net);
|
|
|
|
|
|
|
|
//when
|
|
|
|
let invalid = net.get(url).user_agent("orange").send().await;
|
|
|
|
let valid = net
|
|
|
|
.get(url)
|
|
|
|
.user_agent("007")
|
|
|
|
.send()
|
|
|
|
.await
|
|
|
|
.expect("valid response");
|
|
|
|
|
|
|
|
//then
|
|
|
|
let_assert!(Err(Error::ResponseError { response }) = invalid);
|
|
|
|
assert_eq!(response.status(), StatusCode::FORBIDDEN);
|
|
|
|
assert_eq!(valid.status(), StatusCode::OK);
|
|
|
|
}
|