mirror of
https://github.com/NetworkCats/rustyip.git
synced 2026-11-13 16:17:46 +00:00
fix: return 400 instead of 404 for missing client IP header
This commit is contained in:
@@ -4,6 +4,7 @@ use axum::response::{IntoResponse, Response};
|
||||
pub enum AppError {
|
||||
IpNotFound,
|
||||
InvalidIp,
|
||||
MissingClientIp,
|
||||
DbLookupFailed,
|
||||
}
|
||||
|
||||
@@ -12,6 +13,7 @@ impl IntoResponse for AppError {
|
||||
let (status, message) = match self {
|
||||
Self::IpNotFound => (StatusCode::NOT_FOUND, "IP not found in database"),
|
||||
Self::InvalidIp => (StatusCode::BAD_REQUEST, "Invalid IP address"),
|
||||
Self::MissingClientIp => (StatusCode::BAD_REQUEST, "Missing client IP address"),
|
||||
Self::DbLookupFailed => (StatusCode::INTERNAL_SERVER_ERROR, "Database lookup failed"),
|
||||
};
|
||||
(status, message).into_response()
|
||||
|
||||
+1
-1
@@ -52,7 +52,7 @@ fn extract_client_ip(headers: &HeaderMap) -> Result<IpAddr, AppError> {
|
||||
.get("CF-Connecting-IP")
|
||||
.and_then(|v| v.to_str().ok())
|
||||
.and_then(|s| s.trim().parse().ok())
|
||||
.ok_or(AppError::IpNotFound)
|
||||
.ok_or(AppError::MissingClientIp)
|
||||
}
|
||||
|
||||
fn lookup_ip(db: &SharedDb, ip: IpAddr) -> Result<IpInfo, AppError> {
|
||||
|
||||
@@ -231,7 +231,7 @@ async fn root_without_cf_header_returns_error() {
|
||||
)],
|
||||
)
|
||||
.await;
|
||||
assert_eq!(status, StatusCode::NOT_FOUND);
|
||||
assert_eq!(status, StatusCode::BAD_REQUEST);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
|
||||
Reference in New Issue
Block a user