0.34.6_repo_cleanup
This commit is contained in:
1 parent
ce1a90397c
commit
133d2f0f91
273 files changed
+5969
-6926
No files matched your search
+116
-36
@@ -82,7 +82,10 @@ mod tests {
|
||||
(StatusCode::REQUEST_TIMEOUT, "408 - Request timeout"),
|
||||
(StatusCode::GONE, "410 - Gone"),
|
||||
(StatusCode::PAYLOAD_TOO_LARGE, "413 - Payload too large"),
|
||||
(StatusCode::INTERNAL_SERVER_ERROR, "500 - Internal server error"),
|
||||
(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
"500 - Internal server error",
|
||||
),
|
||||
(StatusCode::BAD_GATEWAY, "502 - Bad gateway"),
|
||||
] {
|
||||
let page = builtin_gateway_error(code).unwrap();
|
||||
@@ -181,18 +184,27 @@ mod tests {
|
||||
let mk_origin = || {
|
||||
let mut response = Response::new(Body::from("<html>secret application error</html>"));
|
||||
*response.status_mut() = StatusCode::NOT_FOUND;
|
||||
response.headers_mut().insert("content-type", HeaderValue::from_static("text/html"));
|
||||
response
|
||||
.headers_mut()
|
||||
.insert("content-type", HeaderValue::from_static("text/html"));
|
||||
response
|
||||
};
|
||||
let defaults: VhostPages = serde_json::from_str("{}").unwrap();
|
||||
assert!(defaults.intercept_statuses.is_empty());
|
||||
let unchanged = intercept_application_error(mk_origin(), &defaults, &GlobalPages::default());
|
||||
let unchanged =
|
||||
intercept_application_error(mk_origin(), &defaults, &GlobalPages::default());
|
||||
assert_eq!(unchanged.status(), StatusCode::NOT_FOUND);
|
||||
assert_eq!(unchanged.into_body().collect().await.unwrap().to_bytes(),
|
||||
"<html>secret application error</html>");
|
||||
let configured = VhostPages { intercept_statuses: vec![404], ..VhostPages::default() };
|
||||
assert_eq!(
|
||||
unchanged.into_body().collect().await.unwrap().to_bytes(),
|
||||
"<html>secret application error</html>"
|
||||
);
|
||||
let configured = VhostPages {
|
||||
intercept_statuses: vec![404],
|
||||
..VhostPages::default()
|
||||
};
|
||||
configured.validate().unwrap();
|
||||
let replaced = intercept_application_error(mk_origin(), &configured, &GlobalPages::default());
|
||||
let replaced =
|
||||
intercept_application_error(mk_origin(), &configured, &GlobalPages::default());
|
||||
assert_eq!(replaced.status(), StatusCode::NOT_FOUND);
|
||||
assert_eq!(replaced.headers().get("cache-control").unwrap(), "no-store");
|
||||
assert!(replaced.extensions().get::<PageServed>().is_some());
|
||||
@@ -203,60 +215,126 @@ mod tests {
|
||||
|
||||
#[tokio::test]
|
||||
async fn interception_replaces_only_the_body_even_when_page_has_a_status_override() {
|
||||
let mut vhost = VhostPages { intercept_statuses: vec![404], ..VhostPages::default() };
|
||||
vhost.errors.insert(404, Page {
|
||||
kind: Kind::Html,
|
||||
// Overrides are valid for standalone error pages, not an origin
|
||||
// response whose HTTP status must remain 404.
|
||||
status: Some(503),
|
||||
html: "custom 404".into(),
|
||||
});
|
||||
let mut vhost = VhostPages {
|
||||
intercept_statuses: vec![404],
|
||||
..VhostPages::default()
|
||||
};
|
||||
vhost.errors.insert(
|
||||
404,
|
||||
Page {
|
||||
kind: Kind::Html,
|
||||
status: Some(503),
|
||||
html: "custom 404".into(),
|
||||
},
|
||||
);
|
||||
let mut origin = Response::new(Body::from("application 404"));
|
||||
*origin.status_mut() = StatusCode::NOT_FOUND;
|
||||
let intercepted = intercept_application_error(origin, &vhost, &GlobalPages::default());
|
||||
assert_eq!(intercepted.status(), StatusCode::NOT_FOUND);
|
||||
assert_eq!(intercepted.headers().get("cache-control").unwrap(), "no-store");
|
||||
assert_eq!(intercepted.into_body().collect().await.unwrap().to_bytes(), "custom 404");
|
||||
// Outside interception the configured status override still applies.
|
||||
assert_eq!(vhost.errors.get(&404).unwrap().render(StatusCode::NOT_FOUND).status(), StatusCode::SERVICE_UNAVAILABLE);
|
||||
assert_eq!(
|
||||
intercepted.headers().get("cache-control").unwrap(),
|
||||
"no-store"
|
||||
);
|
||||
assert_eq!(
|
||||
intercepted.into_body().collect().await.unwrap().to_bytes(),
|
||||
"custom 404"
|
||||
);
|
||||
assert_eq!(
|
||||
vhost
|
||||
.errors
|
||||
.get(&404)
|
||||
.unwrap()
|
||||
.render(StatusCode::NOT_FOUND)
|
||||
.status(),
|
||||
StatusCode::SERVICE_UNAVAILABLE
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn interception_uses_vhost_then_global_page_and_preserves_challenge() {
|
||||
let mut global = GlobalPages::default();
|
||||
global.errors.insert(401, Page { kind: Kind::Html,
|
||||
html: "global denied".into(), status: None });
|
||||
let mut vhost = VhostPages { intercept_statuses: vec![401], ..VhostPages::default() };
|
||||
global.errors.insert(
|
||||
401,
|
||||
Page {
|
||||
kind: Kind::Html,
|
||||
html: "global denied".into(),
|
||||
status: None,
|
||||
},
|
||||
);
|
||||
let mut vhost = VhostPages {
|
||||
intercept_statuses: vec![401],
|
||||
..VhostPages::default()
|
||||
};
|
||||
let mut response = Response::new(Body::from("application denied"));
|
||||
*response.status_mut() = StatusCode::UNAUTHORIZED;
|
||||
response.headers_mut().insert("www-authenticate", HeaderValue::from_static("Basic realm=\"app\""));
|
||||
response.headers_mut().insert("set-cookie", HeaderValue::from_static("secret=1"));
|
||||
response.headers_mut().insert("content-encoding", HeaderValue::from_static("gzip"));
|
||||
response.headers_mut().insert(
|
||||
"www-authenticate",
|
||||
HeaderValue::from_static("Basic realm=\"app\""),
|
||||
);
|
||||
response
|
||||
.headers_mut()
|
||||
.insert("set-cookie", HeaderValue::from_static("secret=1"));
|
||||
response
|
||||
.headers_mut()
|
||||
.insert("content-encoding", HeaderValue::from_static("gzip"));
|
||||
let global_response = intercept_application_error(response, &vhost, &global);
|
||||
assert_eq!(global_response.headers().get("www-authenticate").unwrap(), "Basic realm=\"app\"");
|
||||
assert_eq!(
|
||||
global_response.headers().get("www-authenticate").unwrap(),
|
||||
"Basic realm=\"app\""
|
||||
);
|
||||
assert!(!global_response.headers().contains_key("set-cookie"));
|
||||
assert!(!global_response.headers().contains_key("content-encoding"));
|
||||
assert_eq!(global_response.into_body().collect().await.unwrap().to_bytes(), "global denied");
|
||||
assert_eq!(
|
||||
global_response
|
||||
.into_body()
|
||||
.collect()
|
||||
.await
|
||||
.unwrap()
|
||||
.to_bytes(),
|
||||
"global denied"
|
||||
);
|
||||
|
||||
vhost.errors.insert(401, Page { kind: Kind::Html,
|
||||
html: "local denied".into(), status: None });
|
||||
vhost.errors.insert(
|
||||
401,
|
||||
Page {
|
||||
kind: Kind::Html,
|
||||
html: "local denied".into(),
|
||||
status: None,
|
||||
},
|
||||
);
|
||||
let mut response = Response::new(Body::from("application denied"));
|
||||
*response.status_mut() = StatusCode::UNAUTHORIZED;
|
||||
let local = intercept_application_error(response, &vhost, &global);
|
||||
assert_eq!(local.into_body().collect().await.unwrap().to_bytes(), "local denied");
|
||||
assert_eq!(
|
||||
local.into_body().collect().await.unwrap().to_bytes(),
|
||||
"local denied"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn interception_skips_nonmatching_and_already_served_responses() {
|
||||
let vhost = VhostPages { intercept_statuses: vec![404], ..VhostPages::default() };
|
||||
let vhost = VhostPages {
|
||||
intercept_statuses: vec![404],
|
||||
..VhostPages::default()
|
||||
};
|
||||
let mut response = Response::new(Body::from("ordinary ok"));
|
||||
*response.status_mut() = StatusCode::OK;
|
||||
let untouched = intercept_application_error(response, &vhost, &GlobalPages::default());
|
||||
assert_eq!(untouched.into_body().collect().await.unwrap().to_bytes(), "ordinary ok");
|
||||
let served = Page { kind: Kind::Html, status: Some(404), html: "internal fallback".into() }
|
||||
.render(StatusCode::NOT_FOUND);
|
||||
assert_eq!(
|
||||
untouched.into_body().collect().await.unwrap().to_bytes(),
|
||||
"ordinary ok"
|
||||
);
|
||||
let served = Page {
|
||||
kind: Kind::Html,
|
||||
status: Some(404),
|
||||
html: "internal fallback".into(),
|
||||
}
|
||||
.render(StatusCode::NOT_FOUND);
|
||||
let preserved = intercept_application_error(served, &vhost, &GlobalPages::default());
|
||||
assert_eq!(preserved.into_body().collect().await.unwrap().to_bytes(), "internal fallback");
|
||||
assert_eq!(
|
||||
preserved.into_body().collect().await.unwrap().to_bytes(),
|
||||
"internal fallback"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -273,6 +351,8 @@ mod tests {
|
||||
assert!(pages.validate().is_err());
|
||||
pages.intercept_statuses.clear();
|
||||
pages.validate().unwrap();
|
||||
assert!(!serde_json::to_string(&pages).unwrap().contains("intercept_statuses"));
|
||||
assert!(!serde_json::to_string(&pages)
|
||||
.unwrap()
|
||||
.contains("intercept_statuses"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user