Skip to content

Commit 0a7665e

Browse files
authored
Merge pull request #3247 from spinframework/sad-mac-localhost
outbound-http: Improve test assertion logging
2 parents 45f153f + 3b8e18d commit 0a7665e

File tree

5 files changed

+51
-14
lines changed

5 files changed

+51
-14
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/common/src/assert.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//! Assertion macros.
2+
3+
/// Asserts that the expression matches the pattern.
4+
///
5+
/// This is equivalent to `assert!(matches!(...))` except that it produces nicer
6+
/// errors.
7+
#[macro_export]
8+
macro_rules! assert_matches {
9+
($expr:expr, $pat:pat $(,)?) => {{
10+
let val = $expr;
11+
assert!(
12+
matches!(val, $pat),
13+
"expected {val:?} to match {}",
14+
stringify!($pat),
15+
)
16+
}};
17+
}
18+
19+
/// Asserts that the expression does not match the pattern.
20+
///
21+
/// This is equivalent to `assert!(!matches!(...))` except that it produces
22+
/// nicer errors.
23+
#[macro_export]
24+
macro_rules! assert_not_matches {
25+
($expr:expr, $pat:pat $(,)?) => {{
26+
let val = $expr;
27+
assert!(
28+
!matches!(val, $pat),
29+
"expected {val:?} to NOT match {}",
30+
stringify!($pat),
31+
)
32+
}};
33+
}

crates/common/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// - Code should have at least 2 dependents
1010

1111
pub mod arg_parser;
12+
pub mod assert;
1213
pub mod data_dir;
1314
pub mod paths;
1415
pub mod sha256;

crates/factor-outbound-http/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ wasmtime-wasi = { workspace = true }
2727
wasmtime-wasi-http = { workspace = true }
2828

2929
[dev-dependencies]
30+
spin-common = { path = "../common" }
3031
spin-factor-variables = { path = "../factor-variables" }
3132
spin-factors-test = { path = "../factors-test" }
3233

crates/factor-outbound-http/tests/factor_test.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::time::Duration;
22

33
use anyhow::bail;
44
use http::{Request, Uri};
5+
use spin_common::{assert_matches, assert_not_matches};
56
use spin_factor_outbound_http::{OutboundHttpFactor, SelfRequestOrigin};
67
use spin_factor_outbound_networking::OutboundNetworkingFactor;
78
use spin_factor_variables::VariablesFactor;
@@ -31,10 +32,10 @@ async fn allowed_host_is_allowed() -> anyhow::Result<()> {
3132

3233
// Different systems handle the discard prefix differently; some will
3334
// immediately reject it while others will silently let it time out
34-
match future_resp.unwrap_ready().unwrap() {
35-
Err(ErrorCode::ConnectionRefused | ErrorCode::ConnectionTimeout) => (),
36-
other => bail!("expected Err(ConnectionRefused | ConnectionTimeout), got {other:?}"),
37-
};
35+
assert_matches!(
36+
future_resp.unwrap_ready().unwrap(),
37+
Err(ErrorCode::ConnectionRefused | ErrorCode::ConnectionTimeout),
38+
);
3839
Ok(())
3940
}
4041

@@ -52,10 +53,10 @@ async fn self_request_smoke_test() -> anyhow::Result<()> {
5253

5354
// Different systems handle the discard prefix differently; some will
5455
// immediately reject it while others will silently let it time out
55-
match future_resp.unwrap_ready().unwrap() {
56-
Err(ErrorCode::ConnectionRefused | ErrorCode::ConnectionTimeout) => (),
57-
other => bail!("expected Err(ConnectionRefused | ConnectionTimeout), got {other:?}"),
58-
};
56+
assert_matches!(
57+
future_resp.unwrap_ready().unwrap(),
58+
Err(ErrorCode::ConnectionRefused | ErrorCode::ConnectionTimeout),
59+
);
5960
Ok(())
6061
}
6162

@@ -67,10 +68,10 @@ async fn disallowed_host_fails() -> anyhow::Result<()> {
6768
let req = Request::get("https://denied.test").body(Default::default())?;
6869
let mut future_resp = wasi_http.send_request(req, test_request_config())?;
6970
future_resp.ready().await;
70-
match future_resp.unwrap_ready().unwrap() {
71-
Ok(_) => bail!("expected Err, got Ok"),
72-
Err(err) => assert!(matches!(err, ErrorCode::HttpRequestDenied)),
73-
};
71+
assert_matches!(
72+
future_resp.unwrap_ready().unwrap(),
73+
Err(ErrorCode::HttpRequestDenied),
74+
);
7475
Ok(())
7576
}
7677

@@ -89,11 +90,11 @@ async fn disallowed_private_ips_fails() -> anyhow::Result<()> {
8990
Ok(_) => {}
9091
// If private IPs are disallowed, we should get an error saying the destination is prohibited
9192
Err(err) if !allow_private_ips => {
92-
assert!(matches!(err, ErrorCode::DestinationIpProhibited))
93+
assert_matches!(err, ErrorCode::DestinationIpProhibited);
9394
}
9495
// Otherwise, we should get some non-DestinationIpProhibited error
9596
Err(err) => {
96-
assert!(!matches!(err, ErrorCode::DestinationIpProhibited))
97+
assert_not_matches!(err, ErrorCode::DestinationIpProhibited);
9798
}
9899
};
99100
Ok(())

0 commit comments

Comments
 (0)