Add tests for all but the content syntax parser module

This commit is contained in:
Juno Takano 2025-12-25 23:57:22 -03:00
commit e657eb6513
17 changed files with 1072 additions and 127 deletions

View file

@ -25,11 +25,7 @@ pub async fn file(file_path: &str, content_type: &str) -> Response<Body> {
let header = header::CONTENT_TYPE;
if let Ok(header_value) = HeaderValue::from_str(content_type) {
if let Some(h) = response.headers_mut().insert(header, header_value) {
log!(
"Overwrote existing header {h:?} because a header for the same key existed"
);
}
response.headers_mut().append(header, header_value);
} else {
log!("Failed to create content type header value from {content_type}");
}
@ -43,15 +39,70 @@ pub async fn serial(format: &Format) -> Response<Body> {
let body = serialize_graph(format, &graph);
match *format {
Format::Toml => handlers::raw::make_response(
Format::TOML => handlers::raw::make_response(
&body,
200,
&[(header::CONTENT_TYPE, "text/plain")],
),
Format::Json => handlers::raw::make_response(
Format::JSON => handlers::raw::make_response(
&body,
200,
&[(header::CONTENT_TYPE, "application/json")],
),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn serial_toml() {
let response = serial(&Format::TOML).await;
assert!(response.status() == 200);
}
#[tokio::test]
async fn serial_toml_content_type() {
let response = serial(&Format::TOML).await;
assert!(
response.headers().get(header::CONTENT_TYPE).unwrap()
== "text/plain"
);
}
#[tokio::test]
async fn serial_json_content_type() {
let response = serial(&Format::JSON).await;
assert!(
response.headers().get(header::CONTENT_TYPE).unwrap()
== "application/json"
);
}
#[tokio::test]
async fn file_valid_header() {
let payload = "y1mgMhjeIMFsRNZ1tskP52DfWuvhvbRP";
let response = file("./static/graph.toml", payload).await;
assert_eq!(
response.headers().get(header::CONTENT_TYPE).unwrap(),
payload
);
}
#[tokio::test]
async fn file_invalid_header() {
let response = file("./static/graph.toml", "\n").await;
println!("{response:#?}");
assert!(response.headers().get(header::CONTENT_TYPE).is_none());
}
#[tokio::test]
#[should_panic(
expected = "Failed to read IvnhZhdHb1xDnUw4hYDDNIERoaOojkiu \
contents: No such file or directory (os error 2)"
)]
async fn file_invalid_path() {
drop(file("IvnhZhdHb1xDnUw4hYDDNIERoaOojkiu", "text/plain").await);
}
}