From f54d6ff2838b5d1626f3e4fabf2045648ba34d00 Mon Sep 17 00:00:00 2001 From: Zeke Fast Date: Mon, 8 Sep 2025 14:58:45 +0200 Subject: [PATCH] Replace usage of hagrid::web::tests::common::assert::check_response() with hagrid::routes::tests::common::assert::response() to remove code duplication in tests. Changes: - Replace usage of hagrid::web::tests::common::assert::check_response() with hagrid::routes::tests::common::assert::response() to remove code duplication in tests. - Refactor hagrid::web::tests::check_response() tests to use assert::response() helper. - Take &str instead of &'static str as present_page_text argument in assert::response() helper. This was a bug. It affected reusability of the assertion helper function. --- src/routes/mod.rs | 7 +++++-- src/web/mod.rs | 36 +++++++++++++++++++++++------------- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/src/routes/mod.rs b/src/routes/mod.rs index d7df2d4..412aae4 100644 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -221,11 +221,14 @@ pub mod tests { response: LocalResponse, status: Status, content_type: ContentType, - present_page_text: &'static str, + present_page_text: &str, ) { assert_eq!(response.status(), status); assert_eq!(response.content_type(), Some(content_type)); - assert!(response.into_string().unwrap().contains(present_page_text)); + + let body = response.into_string().unwrap(); + println!("{body}"); + assert!(body.contains(present_page_text)); } } } diff --git a/src/web/mod.rs b/src/web/mod.rs index dc5fe9f..1c855e6 100644 --- a/src/web/mod.rs +++ b/src/web/mod.rs @@ -300,6 +300,7 @@ pub mod tests { use ::rocket::http::Header; use ::rocket::http::Status; use ::rocket::local::blocking::Client; + use multipart::client::hyper::content_type; use std::fs; use std::fs::File; use std::io::Write; @@ -511,15 +512,6 @@ pub mod tests { check_index_response(client, &format!("/pks/lookup?op=index&search={}", fp), tpk); } - /// Asserts that the given URI contains the search string. - pub fn check_response(client: &Client, uri: &str, status: Status, needle: &str) { - let response = client.get(uri).dispatch(); - assert_eq!(response.status(), status); - let body = response.into_string().unwrap(); - println!("{}", body); - assert!(body.contains(needle)); - } - /// Asserts that the given URI returns human readable response /// page that contains a URI pointing to the Cert. pub fn check_hr_response( @@ -1140,25 +1132,43 @@ pub mod tests { /// Asserts that the given URI contains the search string. #[rstest] - #[case::search_invalid("/search?q=0x1234abcd", Status::BadRequest, "not supported")] - #[case::search_invalid("/search?q=1234abcd", Status::BadRequest, "not supported")] + #[case::search_invalid( + "/search?q=0x1234abcd", + Status::BadRequest, + ContentType::HTML, + "not supported" + )] + #[case::search_invalid( + "/search?q=1234abcd", + Status::BadRequest, + ContentType::HTML, + "not supported" + )] #[case::search_invalid( "/pks/lookup?op=get&search=0x1234abcd", Status::BadRequest, + ContentType::HTML, "not supported" )] #[case::search_invalid( "/pks/lookup?op=get&search=1234abcd", Status::BadRequest, + ContentType::HTML, "not supported" )] - #[case::wkd_policy("/.well-known/openpgpkey/example.org/policy", Status::Ok, "")] + #[case::wkd_policy( + "/.well-known/openpgpkey/example.org/policy", + Status::Ok, + ContentType::Plain, + "" + )] fn check_response( #[from(client)] (_tmpdir, client): (TempDir, Client), #[case] uri: &str, #[case] status: Status, + #[case] content_type: ContentType, #[case] needle: &str, ) { - common::assert::check_response(&client, uri, status, needle); + assert::response(client.get(uri).dispatch(), status, content_type, needle); } }