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.
This commit is contained in:
Zeke Fast
2025-09-08 14:58:45 +02:00
committed by Vincent Breitmoser
parent 766e97107e
commit f54d6ff283
2 changed files with 28 additions and 15 deletions

View File

@@ -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));
}
}
}

View File

@@ -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);
}
}