style: apply rustfmt formatting to source and test files

This commit is contained in:
NetworkCats
2026-03-03 00:07:18 +08:00
parent efedf1cd59
commit b0dea405ee
5 changed files with 44 additions and 51 deletions
+1 -1
View File
@@ -7,7 +7,7 @@ use maxminddb::Reader;
use serde::Deserialize;
use crate::models::{
from_mmdb_record, IpInfo, MmdbAsn, MmdbCity, MmdbCountry, MmdbProxy, MmdbRecord, ProxyInfo,
IpInfo, MmdbAsn, MmdbCity, MmdbCountry, MmdbProxy, MmdbRecord, ProxyInfo, from_mmdb_record,
};
pub type DbReader = Reader<Vec<u8>>;
+5 -1
View File
@@ -25,7 +25,11 @@ mod tests {
use super::*;
use http_body_util::BodyExt;
async fn check_error_response(error: AppError, expected_status: StatusCode, expected_body: &str) {
async fn check_error_response(
error: AppError,
expected_status: StatusCode,
expected_body: &str,
) {
let response = error.into_response();
assert_eq!(response.status(), expected_status);
let body = response.into_body().collect().await.unwrap().to_bytes();
+1 -1
View File
@@ -1,5 +1,5 @@
use axum::routing::get;
use axum::Router;
use axum::routing::get;
use tower_http::services::ServeDir;
use crate::handlers::{self, AppState};
+8 -2
View File
@@ -8,7 +8,10 @@ use tracing::error;
use crate::db::{DbReader, SharedDb, load_db};
pub async fn ensure_db_exists(db_path: &Path, update_url: &str) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
pub async fn ensure_db_exists(
db_path: &Path,
update_url: &str,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
if db_path.exists() {
return Ok(());
}
@@ -20,7 +23,10 @@ pub async fn ensure_db_exists(db_path: &Path, update_url: &str) -> Result<(), Bo
download_db(update_url, db_path).await
}
async fn download_db(url: &str, dest: &Path) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
async fn download_db(
url: &str,
dest: &Path,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let tmp_path = dest.with_extension("mmdb.tmp");
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(300))
+29 -46
View File
@@ -237,12 +237,8 @@ async fn root_without_cf_header_returns_error() {
#[tokio::test]
async fn json_current_ip_via_cf_header() {
let app = build_test_app();
let (status, body) = get_with_headers(
&app,
"/json",
vec![("CF-Connecting-IP", "8.8.8.8")],
)
.await;
let (status, body) =
get_with_headers(&app, "/json", vec![("CF-Connecting-IP", "8.8.8.8")]).await;
assert_eq!(status, StatusCode::OK);
let json: serde_json::Value = serde_json::from_str(&body).unwrap();
assert_eq!(json["ip"], "8.8.8.8");
@@ -286,12 +282,8 @@ async fn ipv6_via_cf_header() {
#[tokio::test]
async fn empty_ip_query_falls_back_to_cf_header() {
let app = build_test_app();
let (status, body) = get_with_headers(
&app,
"/json?ip=",
vec![("CF-Connecting-IP", "8.8.8.8")],
)
.await;
let (status, body) =
get_with_headers(&app, "/json?ip=", vec![("CF-Connecting-IP", "8.8.8.8")]).await;
assert_eq!(status, StatusCode::OK);
let json: serde_json::Value = serde_json::from_str(&body).unwrap();
assert_eq!(json["ip"], "8.8.8.8");
@@ -300,12 +292,8 @@ async fn empty_ip_query_falls_back_to_cf_header() {
#[tokio::test]
async fn whitespace_ip_query_falls_back_to_cf_header() {
let app = build_test_app();
let (status, body) = get_with_headers(
&app,
"/json?ip=%20",
vec![("CF-Connecting-IP", "8.8.8.8")],
)
.await;
let (status, body) =
get_with_headers(&app, "/json?ip=%20", vec![("CF-Connecting-IP", "8.8.8.8")]).await;
assert_eq!(status, StatusCode::OK);
let json: serde_json::Value = serde_json::from_str(&body).unwrap();
assert_eq!(json["ip"], "8.8.8.8");
@@ -358,12 +346,7 @@ async fn accept_json_with_query_param_returns_html() {
#[tokio::test]
async fn ip_endpoint_via_cf_header() {
let app = build_test_app();
let (status, body) = get_with_headers(
&app,
"/ip",
vec![("CF-Connecting-IP", "1.1.1.1")],
)
.await;
let (status, body) = get_with_headers(&app, "/ip", vec![("CF-Connecting-IP", "1.1.1.1")]).await;
assert_eq!(status, StatusCode::OK);
assert_eq!(body.trim(), "1.1.1.1");
}
@@ -371,12 +354,8 @@ async fn ip_endpoint_via_cf_header() {
#[tokio::test]
async fn asn_endpoint_via_cf_header() {
let app = build_test_app();
let (status, body) = get_with_headers(
&app,
"/asn",
vec![("CF-Connecting-IP", "1.1.1.1")],
)
.await;
let (status, body) =
get_with_headers(&app, "/asn", vec![("CF-Connecting-IP", "1.1.1.1")]).await;
assert_eq!(status, StatusCode::OK);
assert_eq!(body.trim(), "AS13335");
}
@@ -384,12 +363,8 @@ async fn asn_endpoint_via_cf_header() {
#[tokio::test]
async fn org_endpoint_via_cf_header() {
let app = build_test_app();
let (status, body) = get_with_headers(
&app,
"/org",
vec![("CF-Connecting-IP", "1.1.1.1")],
)
.await;
let (status, body) =
get_with_headers(&app, "/org", vec![("CF-Connecting-IP", "1.1.1.1")]).await;
assert_eq!(status, StatusCode::OK);
assert_eq!(body.trim(), "Cloudflare, Inc.");
}
@@ -397,12 +372,8 @@ async fn org_endpoint_via_cf_header() {
#[tokio::test]
async fn country_endpoint_via_cf_header() {
let app = build_test_app();
let (status, body) = get_with_headers(
&app,
"/country",
vec![("CF-Connecting-IP", "45.77.77.77")],
)
.await;
let (status, body) =
get_with_headers(&app, "/country", vec![("CF-Connecting-IP", "45.77.77.77")]).await;
assert_eq!(status, StatusCode::OK);
assert_eq!(body.trim(), "United States");
}
@@ -411,11 +382,17 @@ async fn country_endpoint_via_cf_header() {
async fn invalid_ip_on_field_endpoints_returns_400() {
let app = build_test_app();
let endpoints = ["/ip", "/asn", "/org", "/country", "/city", "/proxy", "/vpn", "/hosting", "/tor"];
let endpoints = [
"/ip", "/asn", "/org", "/country", "/city", "/proxy", "/vpn", "/hosting", "/tor",
];
for endpoint in endpoints {
let uri = format!("{endpoint}?ip=invalid");
let (status, _) = get(&app, &uri).await;
assert_eq!(status, StatusCode::BAD_REQUEST, "expected 400 for {endpoint}?ip=invalid");
assert_eq!(
status,
StatusCode::BAD_REQUEST,
"expected 400 for {endpoint}?ip=invalid"
);
}
}
@@ -423,9 +400,15 @@ async fn invalid_ip_on_field_endpoints_returns_400() {
async fn field_endpoints_without_ip_or_header_return_400() {
let app = build_test_app();
let endpoints = ["/ip", "/asn", "/org", "/country", "/city", "/proxy", "/vpn", "/hosting", "/tor", "/json"];
let endpoints = [
"/ip", "/asn", "/org", "/country", "/city", "/proxy", "/vpn", "/hosting", "/tor", "/json",
];
for endpoint in endpoints {
let (status, _) = get(&app, endpoint).await;
assert_eq!(status, StatusCode::BAD_REQUEST, "expected 400 for {endpoint} without IP");
assert_eq!(
status,
StatusCode::BAD_REQUEST,
"expected 400 for {endpoint} without IP"
);
}
}