use secure_errors::panic::{catch_panic_to_safe_response, PanicSafeLayer};
#[test]
fn panic_caught_at_boundary() {
let result = catch_panic_to_safe_response(|| {
panic!("unexpected state");
});
let (status, body) = result;
assert_eq!(status, 500);
let json: serde_json::Value = serde_json::from_str(&body).expect("must be valid JSON");
assert_eq!(json["code"], "internal_error");
assert!(
!body.contains("unexpected state"),
"panic message must not leak"
);
}
#[test]
fn panic_does_not_crash_service() {
let _ = catch_panic_to_safe_response(|| {
panic!("crash me");
});
let result = catch_panic_to_safe_response(|| {
"ok"
});
let (status, _body) = result;
assert_eq!(
status, 200,
"service must still respond normally after a panic"
);
}
#[test]
fn panic_safe_layer_is_clone_send_sync() {
fn assert_clone_send_sync<T: Clone + Send + Sync>() {}
assert_clone_send_sync::<PanicSafeLayer>();
}