use axum_core::response::{IntoResponse, Response};
use http::StatusCode;
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum BuildError {
#[error("secret key must not be empty")]
EmptyKey,
#[error("secret must be at least 16 bytes, got {len}")]
ShortKey {
len: usize,
},
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum SessionRejection {
#[error("SessionLayer<T> is not mounted for this request")]
NotMounted,
}
impl IntoResponse for SessionRejection {
fn into_response(self) -> Response {
match self {
Self::NotMounted => (
StatusCode::INTERNAL_SERVER_ERROR,
"session layer not mounted for this request",
)
.into_response(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use http_body_util::BodyExt;
#[test]
fn not_mounted_into_response_is_http_500_ac5_4() {
let response = SessionRejection::NotMounted.into_response();
assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);
}
#[tokio::test]
async fn not_mounted_response_body_mentions_not_mounted_ac5_4() {
let response = SessionRejection::NotMounted.into_response();
let body_bytes = response
.into_body()
.collect()
.await
.expect("collecting an in-memory body never fails")
.to_bytes();
let body_str =
std::str::from_utf8(&body_bytes).expect("body is constructed from a &'static str");
assert!(
body_str.contains("not mounted"),
"expected body to mention \"not mounted\", got {body_str:?}"
);
}
#[test]
fn not_mounted_has_standard_derives_ac5_4() {
let original = SessionRejection::NotMounted;
let cloned = original.clone();
assert_eq!(original, cloned);
let debug_repr = format!("{original:?}");
assert!(
debug_repr.contains("NotMounted"),
"Debug representation should name the variant, got {debug_repr:?}"
);
}
#[test]
fn not_mounted_display_message_is_useful_ac5_4() {
let message = format!("{}", SessionRejection::NotMounted);
assert!(
message.contains("not mounted") || message.contains("SessionLayer"),
"Display should describe the missing layer, got {message:?}"
);
}
}