style: format imports and refactor ip filter authorization logic

This commit is contained in:
2026-04-29 23:25:15 +02:00
committed by ForgeCode
parent f37befacdd
commit 1672e5367e
9 changed files with 120 additions and 168 deletions
+13 -26
View File
@@ -4,8 +4,8 @@
//! including middleware chains and request handling.
use servme::{
ApiKeyMiddleware, Claims, IpFilterMiddleware, Responder,
ServerBuilder, ServerConfig, ServerError, UrlExtract,
ApiKeyMiddleware, Claims, IpFilterMiddleware, Responder, ServerBuilder, ServerConfig,
ServerError, UrlExtract,
};
use std::net::IpAddr;
@@ -26,8 +26,7 @@ fn test_server_builder_default_config() {
#[test]
fn test_server_builder_with_address() {
let builder = ServerBuilder::new()
.address("0.0.0.0", 3000);
let builder = ServerBuilder::new().address("0.0.0.0", 3000);
assert_eq!(builder.config.ip, "0.0.0.0");
assert_eq!(builder.config.port, 3000);
@@ -96,10 +95,7 @@ fn test_responder_redirect() {
let response = result.unwrap();
assert_eq!(response.status(), http::StatusCode::SEE_OTHER);
assert_eq!(
response.headers().get("location").unwrap(),
"/new-location"
);
assert_eq!(response.headers().get("location").unwrap(), "/new-location");
}
#[test]
@@ -144,25 +140,19 @@ fn test_ip_filter_middleware_validation() {
let result = IpFilterMiddleware::new(
vec!["192.168.1.1".to_string(), "10.0.0.1".to_string()],
false
false,
);
assert!(result.is_ok());
// Invalid IP should fail
let result = IpFilterMiddleware::new(
vec!["not-an-ip".to_string()],
false
);
let result = IpFilterMiddleware::new(vec!["not-an-ip".to_string()], false);
assert!(result.is_err());
}
#[test]
fn test_ip_filter_authorization() {
// Test with checked middleware for valid IPs
let middleware = IpFilterMiddleware::new(
vec!["192.168.1.100".to_string()],
false
).unwrap();
let middleware = IpFilterMiddleware::new(vec!["192.168.1.100".to_string()], false).unwrap();
let allowed_ip: IpAddr = "192.168.1.100".parse().unwrap();
let denied_ip: IpAddr = "192.168.1.200".parse().unwrap();
@@ -173,10 +163,7 @@ fn test_ip_filter_authorization() {
#[test]
fn test_ip_filter_ipv6() {
let middleware = IpFilterMiddleware::new(
vec!["::1".to_string()],
false,
).unwrap();
let middleware = IpFilterMiddleware::new(vec!["::1".to_string()], false).unwrap();
let ipv6_local: IpAddr = "::1".parse().unwrap();
let ipv6_other: IpAddr = "::2".parse().unwrap();
@@ -237,10 +224,10 @@ fn test_claims_username() {
#[test]
fn test_server_error_display() {
let error = ServerError::bind("127.0.0.1:8080", std::io::Error::new(
std::io::ErrorKind::AddrInUse,
"Address already in use"
));
let error = ServerError::bind(
"127.0.0.1:8080",
std::io::Error::new(std::io::ErrorKind::AddrInUse, "Address already in use"),
);
let display = format!("{}", error);
assert!(display.contains("Failed to bind"));
@@ -263,7 +250,7 @@ fn test_server_error_validation() {
#[test]
fn test_constants_values() {
use servme::constants::{
DEFAULT_HOST, DEFAULT_PORT, JWT_COOKIE_NAME, BEARER_PREFIX, FILE_EXTENSIONS,
BEARER_PREFIX, DEFAULT_HOST, DEFAULT_PORT, FILE_EXTENSIONS, JWT_COOKIE_NAME,
};
assert_eq!(DEFAULT_HOST, "127.0.0.1");