use actix_web::HttpResponse;
use serde_json::Value;
#[cfg(test)]
pub(crate) async fn response_json_for_tests(response: HttpResponse, context: &str) -> Value {
let bytes = actix_web::body::to_bytes(response.into_body())
.await
.unwrap_or_else(|error| panic!("{context} response body should serialize: {error}"));
serde_json::from_slice(&bytes)
.unwrap_or_else(|error| panic!("{context} response body should be valid JSON: {error}"))
}
#[cfg(test)]
pub(crate) fn assert_error_payload_for_tests(
body: &Value,
expected_message: &str,
expected_error_fragments: &[&str],
context: &str,
) {
assert_eq!(
body.get("status").and_then(Value::as_str),
Some("error"),
"unexpected {context} status payload: {body:?}"
);
assert_eq!(
body.get("message").and_then(Value::as_str),
Some(expected_message),
"unexpected {context} message payload: {body:?}"
);
let error = body
.get("error")
.and_then(Value::as_str)
.unwrap_or_else(|| panic!("missing {context} error payload: {body:?}"));
for fragment in expected_error_fragments {
assert!(
error.contains(fragment),
"expected {context} error payload to contain '{fragment}': {body:?}"
);
}
}
#[cfg(test)]
pub(crate) fn assert_success_logging_client_for_tests(body: &Value, context: &str) {
assert_eq!(
body.get("logging_client").and_then(Value::as_str),
Some("athena_logging"),
"unexpected {context} logging_client payload: {body:?}"
);
}
#[cfg(test)]
pub(crate) fn assert_success_payload_field_for_tests(
body: &Value,
required_field: &str,
context: &str,
) {
assert_success_logging_client_for_tests(body, context);
assert!(
body.get(required_field).is_some(),
"expected {required_field} field in {context} payload"
);
}