test(net): add test to check that all expected requests are made
This commit is contained in:
parent
82cfaf337d
commit
3b2610d6c9
1 changed files with 32 additions and 0 deletions
32
tests/net.rs
32
tests/net.rs
|
@ -5,6 +5,7 @@ type TestResult = Result<(), Error>;
|
|||
|
||||
#[tokio::test]
|
||||
async fn test_get() -> TestResult {
|
||||
//given
|
||||
let mut net = kxio::net::mock();
|
||||
let client = net.client();
|
||||
|
||||
|
@ -18,8 +19,10 @@ async fn test_get() -> TestResult {
|
|||
|
||||
net.on(request).response(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");
|
||||
|
||||
|
@ -28,6 +31,7 @@ async fn test_get() -> TestResult {
|
|||
|
||||
#[tokio::test]
|
||||
async fn test_post() -> TestResult {
|
||||
//given
|
||||
let mut net = kxio::net::mock();
|
||||
let client = net.client();
|
||||
|
||||
|
@ -41,10 +45,38 @@ async fn test_post() -> TestResult {
|
|||
|
||||
net.on(request).response(my_response.into());
|
||||
|
||||
//when
|
||||
let response = net.send(client.post(url)).await?;
|
||||
|
||||
//then
|
||||
assert_eq!(response.status(), http::StatusCode::OK);
|
||||
assert_eq!(response.bytes().await.expect("response body"), "Post OK");
|
||||
|
||||
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).response(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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue