Compare commits

...

2 commits

Author SHA1 Message Date
cee9c9ff9e
refactor: Improve header finder tests with rstest
All checks were successful
Rust CI / Rust CI (push) Successful in 1m44s
Signed-off-by: Awiteb <a@4rs.nl>
2024-08-10 20:22:49 +00:00
702bbad7ae
chore: Add rstest to dev dependencies
Signed-off-by: Awiteb <a@4rs.nl>
2024-08-10 20:21:54 +00:00
2 changed files with 61 additions and 71 deletions

View file

@ -28,6 +28,7 @@ tempfile = "3.9"
tokio = { version = "1.35", features = ["macros", "rt-multi-thread"] }
base64 = "0.21"
salvo = { version = ">= 0.65, < 0.69", default-features = false, features = ["server", "http1","http2", "affix"] }
rstest = "0.22.0"
[[example]]
name = "simple_login"

View file

@ -78,85 +78,74 @@ mod tests {
use salvo_core::http::HeaderValue;
#[tokio::test]
async fn test_captcha_header_finder() {
let finder = CaptchaHeaderFinder::new();
#[rstest::rstest]
#[case::not_found(None, None, None, None, None, None)]
#[case::normal(
None,
None,
Some(("x-captcha-token", "token")),
Some(("x-captcha-answer", "answer")),
Some(Some("token")),
Some(Some("answer"))
)
]
#[case::custom_headers(
Some("custom-token"),
Some("custom-answer"),
Some(("custom-token", "token")),
Some(("custom-answer", "answer")),
Some(Some("token")),
Some(Some("answer"))
)
]
#[case::custom_not_found(Some("custom-token"), Some("custom-answer"), None, None, None, None)]
#[case::custom_not_found_with_headers(
Some("custom-token"),
Some("custom-answer"),
Some(("x-captcha-token", "token")),
Some(("x-captcha-answer", "answer")),
None,
None
)
]
async fn test_header_finder(
#[case] custom_token_header: Option<&'static str>,
#[case] custom_answer_header: Option<&'static str>,
#[case] token_header_name_value: Option<(&'static str, &'static str)>,
#[case] answer_header_name_value: Option<(&'static str, &'static str)>,
#[case] excepted_token: Option<Option<&'static str>>,
#[case] excepted_answer: Option<Option<&'static str>>,
) {
let mut finder = CaptchaHeaderFinder::new();
if let Some(custom_token) = custom_token_header {
finder = finder.token_header(HeaderName::from_static(custom_token));
}
if let Some(custom_answer) = custom_answer_header {
finder = finder.answer_header(HeaderName::from_static(custom_answer));
}
let mut req = Request::default();
let headers = req.headers_mut();
headers.insert(
HeaderName::from_static("x-captcha-token"),
HeaderValue::from_str("token").unwrap(),
);
headers.insert(
HeaderName::from_static("x-captcha-answer"),
HeaderValue::from_static("answer"),
);
if let Some((token_header_name, token_header_value)) = token_header_name_value {
headers.insert(
HeaderName::from_static(token_header_name),
HeaderValue::from_static(token_header_value),
);
}
if let Some((answer_header_name, answer_header_value)) = answer_header_name_value {
headers.insert(
HeaderName::from_static(answer_header_name),
HeaderValue::from_static(answer_header_value),
);
}
assert_eq!(
finder.find_token(&mut req).await,
Some(Some("token".to_owned()))
excepted_token.map(|o| o.map(ToOwned::to_owned))
);
assert_eq!(
finder.find_answer(&mut req).await,
Some(Some("answer".to_owned()))
excepted_answer.map(|o| o.map(ToOwned::to_owned))
);
}
#[tokio::test]
async fn test_captcha_header_finder_customized() {
let finder = CaptchaHeaderFinder::new()
.token_header(HeaderName::from_static("token"))
.answer_header(HeaderName::from_static("answer"));
let mut req = Request::default();
let headers = req.headers_mut();
headers.insert(
HeaderName::from_static("token"),
HeaderValue::from_str("token").unwrap(),
);
headers.insert(
HeaderName::from_static("answer"),
HeaderValue::from_static("answer"),
);
assert_eq!(
finder.find_token(&mut req).await,
Some(Some("token".to_owned()))
);
assert_eq!(
finder.find_answer(&mut req).await,
Some(Some("answer".to_owned()))
);
}
#[tokio::test]
async fn test_captcha_header_finder_none() {
let finder = CaptchaHeaderFinder::new();
let mut req = Request::default();
assert_eq!(finder.find_token(&mut req).await, None);
assert_eq!(finder.find_answer(&mut req).await, None);
}
#[tokio::test]
async fn test_captcha_header_finder_customized_none() {
let finder = CaptchaHeaderFinder::new()
.token_header(HeaderName::from_static("token"))
.answer_header(HeaderName::from_static("answer"));
let mut req = Request::default();
let headers = req.headers_mut();
headers.insert(
HeaderName::from_static("x-captcha-token"),
HeaderValue::from_str("token").unwrap(),
);
headers.insert(
HeaderName::from_static("x-captcha-answer"),
HeaderValue::from_static("answer"),
);
assert_eq!(finder.find_token(&mut req).await, None);
assert_eq!(finder.find_answer(&mut req).await, None);
}
}