Expand test coverage

This commit is contained in:
Juno Takano 2026-01-19 01:45:54 -03:00
commit 5151c53a2b
20 changed files with 773 additions and 121 deletions

View file

@ -168,4 +168,13 @@ mod tests {
== "application/json"
);
}
#[tokio::test]
async fn not_found() {
let state = GlobalState {
graph: Graph::default(),
};
let response = file(Path("/k/j/m".to_string()), State(state)).await;
assert!(response.status() == StatusCode::NOT_FOUND);
}
}

View file

@ -17,18 +17,11 @@ pub async fn node(
let instant = now();
let result = state.graph.find_node(&id);
let found = result.node.is_some();
let node = result
.node
.unwrap_or(Node::new(Some(format!("Could not find node ID {id}."))));
let node = result.node.unwrap_or(Node::not_found(Some(format!(
"Could not find node ID {id}."
))));
if !node.redirect.is_empty() {
return Redirect::permanent(
format!("/node/{}", node.redirect).as_str(),
)
.into_response();
}
if found && !result.exact {
if found && (!result.exact || result.redirect) {
return Redirect::permanent(format!("/node/{}", node.id).as_str())
.into_response();
}
@ -60,7 +53,7 @@ mod tests {
http::{HeaderName, StatusCode},
};
use crate::graph::Graph;
use crate::graph::{Format, Graph};
use super::*;
@ -108,4 +101,21 @@ mod tests {
let response = wrap_node("docs").await;
assert_eq!(response.status(), StatusCode::PERMANENT_REDIRECT);
}
#[tokio::test]
async fn standalone_graph_redirect() {
let toml = "\
[nodes.n1]\n\
redirect = 'n2'\n\
\n\
[nodes.n2]\n\
text = 'n2 text'\n\
";
let graph = Graph::from_serial(toml, &Format::TOML).unwrap();
let state = GlobalState { graph };
let response =
node(Path("n1".to_string()), axum::extract::State(state)).await;
assert_eq!(response.status(), StatusCode::PERMANENT_REDIRECT);
}
}

View file

@ -92,3 +92,52 @@ impl Mime {
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn smoke() {
let m = Mime::guess("/home/jane/top/inner/kitty.png");
assert_eq!(String::from(m), "image/png");
}
#[test]
fn all() {
let pairs = [
("file.txt", "text/plain"),
("file.csv", "text/csv"),
("file.css", "text/css"),
("file.ttf", "font/ttf"),
("file.otf", "font/otf"),
("file.woff", "font/woff"),
("file.woff2", "font/woff2"),
("file.svg", "image/svg+xml"),
("file.ico", "image/x-icon"),
("file.jpeg", "image/jpeg"),
("file.png", "image/png"),
("file.apng", "image/apng"),
("caddy.gif", "image/gif"),
("file.webp", "image/webp"),
("file.avif", "image/avif"),
("file.toml", "application/toml"),
("file.xml", "application/xml"),
("file.json", "application/json"),
("file.js", "text/javascript"),
("file.pdf", "application/pdf"),
("book.epub", "application/epub+zip"),
("weird.xzx", "application/octet-stream"),
];
for (file, mime) in pairs {
assert_eq!(String::from(Mime::guess(file)), mime);
}
}
#[test]
fn unknown() {
let u = Mime::guess("x");
assert!(matches!(u, Mime::Unknown));
}
}