From 8d1e26c3051cc02fa071a206f5a5c87ef31908cd Mon Sep 17 00:00:00 2001
From: Awiteb
Date: Sun, 11 Aug 2024 09:29:15 +0000
Subject: [PATCH] refactor: Improve form finder tests with `rstest`
Signed-off-by: Awiteb
---
src/finder/form_finder.rs | 165 ++++++++++++++++++++------------------
1 file changed, 88 insertions(+), 77 deletions(-)
diff --git a/src/finder/form_finder.rs b/src/finder/form_finder.rs
index 1840f27..6eca5bb 100644
--- a/src/finder/form_finder.rs
+++ b/src/finder/form_finder.rs
@@ -76,98 +76,109 @@ impl CaptchaFinder for CaptchaFormFinder {
#[cfg(test)]
mod tests {
- use salvo_core::http::{header, headers::ContentType, HeaderValue, ReqBody};
+ use salvo_core::http::{header, HeaderValue, ReqBody};
use super::*;
#[tokio::test]
- async fn test_captcha_form_finder() {
- let finder = CaptchaFormFinder::new();
+ #[rstest::rstest]
+ #[case::not_found(
+ None,
+ None,
+ None,
+ None,
+ "application/x-www-form-urlencoded",
+ None,
+ None
+ )]
+ #[case::not_found(None, None, None, None, "text/plain", None, None)]
+ #[case::normal(
+ None,
+ None,
+ Some(("captcha_token", "token")),
+ Some(("captcha_answer", "answer")),
+ "application/x-www-form-urlencoded",
+ Some(Some("token")),
+ Some(Some("answer"))
+ )]
+ #[case::custom_keys(
+ Some("custom_token"),
+ Some("custom_answer"),
+ Some(("custom_token", "token")),
+ Some(("custom_answer", "answer")),
+ "application/x-www-form-urlencoded",
+ Some(Some("token")),
+ Some(Some("answer"))
+ )]
+ #[case::custom_not_found(
+ Some("custom_token"),
+ Some("custom_answer"),
+ None,
+ None,
+ "application/x-www-form-urlencoded",
+ None,
+ None
+ )]
+ #[case::custom_not_found_with_body(
+ Some("custom_token"),
+ Some("custom_answer"),
+ Some(("captcha_token", "token")),
+ Some(("captcha_answer", "answer")),
+ "application/x-www-form-urlencoded",
+ None,
+ None
+ )]
+ #[case::invalid_type(
+ None,
+ None,
+ Some(("captcha_token", "token")),
+ Some(("captcha_answer", "answer")),
+ "application/json",
+ None,
+ None
+ )]
+ async fn test_form_finder(
+ #[case] custom_token_key: Option<&'static str>,
+ #[case] custom_answer_key: Option<&'static str>,
+ #[case] token_key_val: Option<(&'static str, &'static str)>,
+ #[case] answer_key_val: Option<(&'static str, &'static str)>,
+ #[case] content_type: &'static str,
+ #[case] excepted_token: Option