This commit is contained in:
Mateusz Gruszczyński committed 2026-10-04 11:15:58 +02:00
1 parent 87e571c170
commit fa7abc9e53
308 files changed
+49746 -50237

No files matched your search

+148
View File
@@ -0,0 +1,148 @@
mod tests {
use super::*;
use http_body_util::BodyExt;
#[tokio::test]
async fn error_replacement_drops_encoding_and_retains_challenge() {
let mut response = Response::new(Body::from("compressed bytes"));
*response.status_mut() = StatusCode::UNAUTHORIZED;
response.headers_mut().insert(
"www-authenticate",
HeaderValue::from_static("Basic realm=\"app\""),
);
response
.headers_mut()
.insert("content-encoding", HeaderValue::from_static("gzip"));
response
.headers_mut()
.insert("set-cookie", HeaderValue::from_static("secret=1"));
replace_error(
&mut response,
&Page {
kind: Kind::Html,
html: "<h1>Sign in</h1>".into(),
status: None,
},
);
assert_eq!(response.status(), 401);
assert!(response.headers().contains_key("www-authenticate"));
assert!(!response.headers().contains_key("content-encoding"));
assert!(!response.headers().contains_key("set-cookie"));
assert_eq!(
response.into_body().collect().await.unwrap().to_bytes(),
"<h1>Sign in</h1>"
);
}
#[test]
fn local_page_method_rejection_is_still_marked_non_cacheable() {
let page = Page::default();
let response = page.serve(&Method::POST, StatusCode::NOT_FOUND);
assert_eq!(response.status(), StatusCode::METHOD_NOT_ALLOWED);
assert_eq!(response.headers().get("cache-control").unwrap(), "no-store");
assert!(response.extensions().get::<PageServed>().is_some());
}
#[test]
fn builtin_gateway_pages_cover_edge_policy_and_gateway_errors() {
assert!(builtin_gateway_error(StatusCode::UNAUTHORIZED)
.unwrap()
.contains("401 - Authentication required"));
assert!(builtin_gateway_error(StatusCode::FORBIDDEN)
.unwrap()
.contains("403 - Access denied"));
assert!(builtin_gateway_error(StatusCode::NOT_FOUND)
.unwrap()
.contains("404 - Not found"));
assert!(builtin_gateway_error(StatusCode::TOO_MANY_REQUESTS)
.unwrap()
.contains("429 - Too many requests"));
assert!(builtin_gateway_error(StatusCode::SERVICE_UNAVAILABLE)
.unwrap()
.contains("503 - Service unavailable"));
assert!(builtin_gateway_error(StatusCode::GATEWAY_TIMEOUT)
.unwrap()
.contains("504 - Gateway timeout"));
assert!(builtin_gateway_error(StatusCode::BAD_GATEWAY).is_none());
}
#[test]
fn validated_bounded_pages_and_ip_detection() {
assert!(Page {
html: "x".repeat(MAX_HTML_BYTES + 1),
..Page::default()
}
.validate()
.is_err());
assert!(Page {
status: Some(204),
..Page::default()
}
.validate()
.is_err());
assert!(Page {
kind: Kind::Empty,
status: Some(204),
html: String::new()
}
.validate()
.is_ok());
assert!(Page {
status: Some(302),
..Page::default()
}
.validate()
.is_err());
for authority in ["127.0.0.1:8080", "[::1]:8443", "10.0.0.1"] {
assert!(literal_ip_authority(authority));
}
for authority in ["app.test:80", "evil@127.0.0.1", "not-an-ip"] {
assert!(!literal_ip_authority(authority));
}
}
#[test]
fn local_response_accepts_full_final_status_range_with_bodyless_guards() {
assert!(Page {
kind: Kind::Html,
status: Some(418),
html: "teapot".into(),
}
.validate_local_response()
.is_ok());
assert!(Page {
kind: Kind::Empty,
status: Some(599),
html: String::new(),
}
.validate_local_response()
.is_ok());
assert!(Page {
kind: Kind::Html,
status: Some(304),
html: "not allowed".into(),
}
.validate_local_response()
.is_err());
assert!(Page {
kind: Kind::Empty,
status: Some(600),
html: String::new(),
}
.validate_local_response()
.is_err());
}
#[test]
fn builtin_welcome_is_minimal_and_never_cacheable() {
let page = Page::default();
let response = page.render(StatusCode::OK);
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(response.headers().get("cache-control").unwrap(), "no-store");
assert!(!response.headers().contains_key("x-content-type-options"));
assert!(response.extensions().get::<MinimalWelcome>().is_some());
}
#[test]
fn static_pages_do_not_accept_writes() {
let page = Page::default();
assert_eq!(page.serve(&Method::POST, StatusCode::OK).status(), 405);
assert_eq!(page.serve(&Method::GET, StatusCode::OK).status(), 200);
}
}