run cargo fmt

This commit is contained in:
geoffsee
2025-08-28 14:48:14 -04:00
parent bd5a84fe95
commit 37fa59a88a
4 changed files with 308 additions and 227 deletions

View File

@@ -1,12 +1,12 @@
use axum::{
Router,
extract::{Path, Query},
response::Json,
routing::{get, post, put, delete},
Router,
routing::{delete, get, post, put},
};
use hyper_custom_cert::HttpClient;
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use serde_json::{Value, json};
use std::collections::HashMap;
use std::time::Duration;
@@ -38,39 +38,35 @@ async fn main() {
let app = Router::new()
// Root endpoint with API overview
.route("/", get(api_overview))
// Basic HTTP client tests
.route("/test/client/default", get(test_default_client))
.route("/test/client/builder", get(test_builder_client))
.route("/test/client/timeout", get(test_timeout_client))
.route("/test/client/headers", get(test_headers_client))
.route("/test/client/combined", get(test_combined_config))
// Feature-specific tests
.route("/test/features/native-tls", get(test_native_tls_feature))
.route("/test/features/rustls", get(test_rustls_feature))
.route("/test/features/insecure", get(test_insecure_feature))
// HTTP method tests
.route("/test/methods/get", get(test_get_method))
.route("/test/methods/post", post(test_post_method))
.route("/test/methods/put", put(test_put_method))
.route("/test/methods/delete", delete(test_delete_method))
// Certificate and TLS tests
.route("/test/tls/custom-ca", get(test_custom_ca))
.route("/test/tls/cert-pinning", get(test_cert_pinning))
.route("/test/tls/self-signed", get(test_self_signed))
// Configuration tests
.route("/test/config/timeout/{seconds}", get(test_custom_timeout))
.route("/test/config/headers/{header_count}", get(test_custom_headers))
.route(
"/test/config/headers/{header_count}",
get(test_custom_headers),
)
// Error simulation tests
.route("/test/errors/timeout", get(test_timeout_error))
.route("/test/errors/invalid-url", get(test_invalid_url))
.route("/test/errors/connection", get(test_connection_error))
// Health and status endpoints
.route("/health", get(health_check))
.route("/status", get(status_check));
@@ -80,7 +76,7 @@ async fn main() {
println!("📍 Listening on http://{}", SERVER_ADDRESS);
println!("📖 Visit http://{} for API documentation", SERVER_ADDRESS);
println!("🧪 Ready for integration testing!");
axum::serve(listener, app).await.unwrap();
}
@@ -130,7 +126,7 @@ async fn api_overview() -> Json<Value> {
},
"features_available": [
"native-tls",
"rustls",
"rustls",
"insecure-dangerous"
]
}))
@@ -144,7 +140,7 @@ async fn api_overview() -> Json<Value> {
async fn test_default_client() -> Json<TestResponse> {
let client = HttpClient::new();
let result = client.request("https://httpbin.org/get").await;
Json(TestResponse {
endpoint: "/test/client/default".to_string(),
status: "success".to_string(),
@@ -161,7 +157,7 @@ async fn test_default_client() -> Json<TestResponse> {
async fn test_builder_client() -> Json<TestResponse> {
let client = HttpClient::builder().build();
let result = client.request("https://httpbin.org/get").await;
Json(TestResponse {
endpoint: "/test/client/builder".to_string(),
status: "success".to_string(),
@@ -181,11 +177,14 @@ async fn test_timeout_client(Query(params): Query<TimeoutQuery>) -> Json<TestRes
.with_timeout(Duration::from_secs(timeout_secs))
.build();
let result = client.request("https://httpbin.org/get").await;
Json(TestResponse {
endpoint: "/test/client/timeout".to_string(),
status: "success".to_string(),
message: format!("HttpClient with {}s timeout configured successfully", timeout_secs),
message: format!(
"HttpClient with {}s timeout configured successfully",
timeout_secs
),
features_tested: vec!["timeout-config".to_string()],
error: match result {
Ok(_) => None,
@@ -197,15 +196,16 @@ async fn test_timeout_client(Query(params): Query<TimeoutQuery>) -> Json<TestRes
/// Test custom headers configuration
async fn test_headers_client() -> Json<TestResponse> {
let mut headers = HashMap::new();
headers.insert("User-Agent".to_string(), "hyper-custom-cert-test/1.0".to_string());
headers.insert(
"User-Agent".to_string(),
"hyper-custom-cert-test/1.0".to_string(),
);
headers.insert("X-Test-Header".to_string(), "test-value".to_string());
headers.insert("Accept".to_string(), "application/json".to_string());
let client = HttpClient::builder()
.with_default_headers(headers)
.build();
let client = HttpClient::builder().with_default_headers(headers).build();
let result = client.request("https://httpbin.org/get").await;
Json(TestResponse {
endpoint: "/test/client/headers".to_string(),
status: "success".to_string(),
@@ -221,19 +221,23 @@ async fn test_headers_client() -> Json<TestResponse> {
/// Test combined configuration options
async fn test_combined_config() -> Json<TestResponse> {
let mut headers = HashMap::new();
headers.insert("User-Agent".to_string(), "hyper-custom-cert-combined/1.0".to_string());
headers.insert(
"User-Agent".to_string(),
"hyper-custom-cert-combined/1.0".to_string(),
);
headers.insert("X-Combined-Test".to_string(), "true".to_string());
let client = HttpClient::builder()
.with_timeout(Duration::from_secs(30))
.with_default_headers(headers)
.build();
let result = client.request("https://httpbin.org/get").await;
Json(TestResponse {
endpoint: "/test/client/combined".to_string(),
status: "success".to_string(),
message: "HttpClient with combined configuration (timeout + headers) works correctly".to_string(),
message: "HttpClient with combined configuration (timeout + headers) works correctly"
.to_string(),
features_tested: vec!["timeout-config".to_string(), "custom-headers".to_string()],
error: match result {
Ok(_) => None,
@@ -254,7 +258,7 @@ async fn test_native_tls_feature() -> Json<TestResponse> {
.with_timeout(Duration::from_secs(10))
.build();
let result = client.request("https://httpbin.org/get").await;
Json(TestResponse {
endpoint: "/test/features/native-tls".to_string(),
status: "success".to_string(),
@@ -284,13 +288,13 @@ async fn test_rustls_feature() -> Json<TestResponse> {
{
// Test with sample root CA PEM (this is just a demo cert)
let ca_pem: &[u8] = b"-----BEGIN CERTIFICATE-----\nMIIDSjCCAjKgAwIBAgIQRK+wgNajJ7qJMDmGLvhAazANBgkqhkiG9w0BAQUFADA/\nMSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT\nDkRTVCBSb290IENBIFgzMB4XDTE2MDMxNzE2NDA0NloXDTIxMDMxNzE2NDA0Nlow\nPzEkMCIGA1UEChMbRGlnaXRhbCBTaWduYXR1cmUgVHJ1c3QgQ28uMRcwFQYDVQQD\nEw5EU1QgUm9vdCBDQSBYMzCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB\nAN+v6ZdQCINXtMxiZfaQguzH0yxrMMpb7NnDfcdAwRgUi+DoM3ZJKuM/IUmTrE4O\nrz5Iy2Xu/NMhD2XSKtkyj4zl93ewEnu1lcCJo6m67XMuegwGMoOifooUMM0RoOEq\nOLl5CjH9UL2AZd+3UWODyOKIYepLYYHsUmu5ouJLGiifSKOeDNoJjj4XLh7dIN9b\nxiqKqy69cK3FCxolkHRyxXtqqzTWMIn/5WgTe1QLyNau7Fqckh49ZLOMxt+/yUFw\n7BZy1SbsOFU5Q9D8/RhcQPGX69Wam40dutolucbY38EVAjqr2m7xPi71XAicPNaD\naeQQmxkqtilX4+U9m5/wAl0CAwEAAaNCMEAwDwYDVR0TAQH/BAUwAwEB/zAOBgNV\nHQ8BAf8EBAMCAQYwHQYDVR0OBBYEFMSnsaR7LHH62+FLkHX/xBVghYkQMA0GCSqG\nSIb3DQEBBQUAA4IBAQCjGiybFwBcqR7uKGY3Or+Dxz9LwwmglSBd49lZRNI+DT69\nikugdB/OEIKcdBodfpga3csTS7MgROSR6cz8faXbauX+5v3gTt23ADq1cEmv8uXr\nAvHRAosZy5Q6XkjEGB5YGV8eAlrwDPGxrancWYaLbumR9YbK+rlmM6pZW87ipxZz\nR8srzJmwN0jP41ZL9c8PDHIyh8bwRLtTcm1D9SZImlJnt1ir/md2cXjbDaJWFBM5\nJDGFoqgCWjBH4d1QB7wCCZAA62RjYJsWvIjJEubSfZGL+T0yjWW06XyxV3bqxbYo\nOb8VZRzI9neWagqNdwvYkQsEjgfbKbYK7p2CNTUQ\n-----END CERTIFICATE-----\n";
let client = HttpClient::builder()
.with_timeout(Duration::from_secs(10))
.with_root_ca_pem(ca_pem)
.build();
let result = client.request("https://httpbin.org/get").await;
Json(TestResponse {
endpoint: "/test/features/rustls".to_string(),
status: "success".to_string(),
@@ -321,17 +325,18 @@ async fn test_insecure_feature() -> Json<TestResponse> {
// Test shortcut method
let client = HttpClient::with_self_signed_certs();
let result = client.request("https://self-signed.badssl.com/").await;
// Test builder method
let client2 = HttpClient::builder()
.insecure_accept_invalid_certs(true)
.build();
let result2 = client2.request("https://expired.badssl.com/").await;
Json(TestResponse {
endpoint: "/test/features/insecure".to_string(),
status: "success".to_string(),
message: "insecure-dangerous feature is working (DO NOT USE IN PRODUCTION!)".to_string(),
message: "insecure-dangerous feature is working (DO NOT USE IN PRODUCTION!)"
.to_string(),
features_tested: vec!["insecure-dangerous".to_string()],
error: match (result, result2) {
(Ok(_), Ok(_)) => None,
@@ -345,7 +350,8 @@ async fn test_insecure_feature() -> Json<TestResponse> {
Json(TestResponse {
endpoint: "/test/features/insecure".to_string(),
status: "skipped".to_string(),
message: "insecure-dangerous feature is not enabled (this is good for security!)".to_string(),
message: "insecure-dangerous feature is not enabled (this is good for security!)"
.to_string(),
features_tested: vec![],
error: Some("Feature not enabled".to_string()),
})
@@ -360,7 +366,7 @@ async fn test_insecure_feature() -> Json<TestResponse> {
async fn test_get_method() -> Json<TestResponse> {
let client = HttpClient::new();
let result = client.request("https://httpbin.org/get").await;
Json(TestResponse {
endpoint: "/test/methods/get".to_string(),
status: "success".to_string(),
@@ -378,11 +384,14 @@ async fn test_post_method(Json(payload): Json<PostData>) -> Json<TestResponse> {
let client = HttpClient::new();
let body = serde_json::to_vec(&payload).unwrap_or_default();
let result = client.post("https://httpbin.org/post", &body).await;
Json(TestResponse {
endpoint: "/test/methods/post".to_string(),
status: "success".to_string(),
message: format!("HTTP POST method test completed with data: {}", payload.data),
message: format!(
"HTTP POST method test completed with data: {}",
payload.data
),
features_tested: vec!["post-request".to_string()],
error: match result {
Ok(_) => None,
@@ -396,11 +405,14 @@ async fn test_put_method(Json(payload): Json<PostData>) -> Json<TestResponse> {
let client = HttpClient::new();
let body = serde_json::to_vec(&payload).unwrap_or_default();
let result = client.post("https://httpbin.org/put", &body).await;
Json(TestResponse {
endpoint: "/test/methods/put".to_string(),
status: "success".to_string(),
message: format!("HTTP PUT method test completed (simulated via POST) with data: {}", payload.data),
message: format!(
"HTTP PUT method test completed (simulated via POST) with data: {}",
payload.data
),
features_tested: vec!["put-request-simulation".to_string()],
error: match result {
Ok(_) => None,
@@ -413,7 +425,7 @@ async fn test_put_method(Json(payload): Json<PostData>) -> Json<TestResponse> {
async fn test_delete_method() -> Json<TestResponse> {
let client = HttpClient::new();
let result = client.request("https://httpbin.org/delete").await;
Json(TestResponse {
endpoint: "/test/methods/delete".to_string(),
status: "success".to_string(),
@@ -435,7 +447,7 @@ async fn test_custom_ca() -> Json<TestResponse> {
#[cfg(feature = "rustls")]
{
let ca_pem: &[u8] = b"-----BEGIN CERTIFICATE-----\nMIIDSjCCAjKgAwIBAgIQRK+wgNajJ7qJMDmGLvhAazANBgkqhkiG9w0BAQUFADA/\nMSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT\nDkRTVCBSb290IENBIFgzMB4XDTE2MDMxNzE2NDA0NloXDTIxMDMxNzE2NDA0Nlow\nPzEkMCIGA1UEChMbRGlnaXRhbCBTaWduYXR1cmUgVHJ1c3QgQ28uMRcwFQYDVQQD\nEw5EU1QgUm9vdCBDQSBYMzCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB\nAN+v6ZdQCINXtMxiZfaQguzH0yxrMMpb7NnDfcdAwRgUi+DoM3ZJKuM/IUmTrE4O\nrz5Iy2Xu/NMhD2XSKtkyj4zl93ewEnu1lcCJo6m67XMuegwGMoOifooUMM0RoOEq\nOLl5CjH9UL2AZd+3UWODyOKIYepLYYHsUmu5ouJLGiifSKOeDNoJjj4XLh7dIN9b\nxiqKqy69cK3FCxolkHRyxXtqqzTWMIn/5WgTe1QLyNau7Fqckh49ZLOMxt+/yUFw\n7BZy1SbsOFU5Q9D8/RhcQPGX69Wam40dutolucbY38EVAjqr2m7xPi71XAicPNaD\naeQQmxkqtilX4+U9m5/wAl0CAwEAAaNCMEAwDwYDVR0TAQH/BAUwAwEB/zAOBgNV\nHQ8BAf8EBAMCAQYwHQYDVR0OBBYEFMSnsaR7LHH62+FLkHX/xBVghYkQMA0GCSqG\nSIb3DQEBBQUAA4IBAQCjGiybFwBcqR7uKGY3Or+Dxz9LwwmglSBd49lZRNI+DT69\nikugdB/OEIKcdBodfpga3csTS7MgROSR6cz8faXbauX+5v3gTt23ADq1cEmv8uXr\nAvHRAosZy5Q6XkjEGB5YGV8eAlrwDPGxrancWYaLbumR9YbK+rlmM6pZW87ipxZz\nR8srzJmwN0jP41ZL9c8PDHIyh8bwRLtTcm1D9SZImlJnt1ir/md2cXjbDaJWFBM5\nJDGFoqgCWjBH4d1QB7wCCZAA62RjYJsWvIjJEubSfZGL+T0yjWW06XyxV3bqxbYo\nOb8VZRzI9neWagqNdwvYkQsEjgfbKbYK7p2CNTUQ\n-----END CERTIFICATE-----\n";
let client = HttpClient::builder()
.with_timeout(Duration::from_secs(10))
.with_root_ca_pem(ca_pem)
@@ -443,7 +455,7 @@ async fn test_custom_ca() -> Json<TestResponse> {
let result = client.request("https://httpbin.org/get");
let awaited = result.await;
Json(TestResponse {
endpoint: "/test/tls/custom-ca".to_string(),
status: "success".to_string(),
@@ -472,13 +484,12 @@ async fn test_cert_pinning() -> Json<TestResponse> {
#[cfg(feature = "rustls")]
{
// Example SHA256 fingerprints (these are demo values)
let pins = vec![
[0x1f, 0x2f, 0x3f, 0x4f, 0x5f, 0x6f, 0x7f, 0x8f,
0x9f, 0xaf, 0xbf, 0xcf, 0xdf, 0xef, 0xff, 0x0f,
0x1f, 0x2f, 0x3f, 0x4f, 0x5f, 0x6f, 0x7f, 0x8f,
0x9f, 0xaf, 0xbf, 0xcf, 0xdf, 0xef, 0xff, 0x0f],
];
let pins = vec![[
0x1f, 0x2f, 0x3f, 0x4f, 0x5f, 0x6f, 0x7f, 0x8f, 0x9f, 0xaf, 0xbf, 0xcf, 0xdf, 0xef,
0xff, 0x0f, 0x1f, 0x2f, 0x3f, 0x4f, 0x5f, 0x6f, 0x7f, 0x8f, 0x9f, 0xaf, 0xbf, 0xcf,
0xdf, 0xef, 0xff, 0x0f,
]];
let client = HttpClient::builder()
.with_timeout(Duration::from_secs(10))
.with_pinned_cert_sha256(pins)
@@ -486,7 +497,7 @@ async fn test_cert_pinning() -> Json<TestResponse> {
let result = client.request("https://httpbin.org/get");
let awaited = result.await;
Json(TestResponse {
endpoint: "/test/tls/cert-pinning".to_string(),
status: "success".to_string(),
@@ -518,7 +529,7 @@ async fn test_self_signed() -> Json<TestResponse> {
let result = client.request("https://self-signed.badssl.com/");
let awaited = result.await;
Json(TestResponse {
endpoint: "/test/tls/self-signed".to_string(),
status: "success".to_string(),
@@ -535,7 +546,8 @@ async fn test_self_signed() -> Json<TestResponse> {
Json(TestResponse {
endpoint: "/test/tls/self-signed".to_string(),
status: "skipped".to_string(),
message: "Self-signed test requires insecure-dangerous feature (good for security!)".to_string(),
message: "Self-signed test requires insecure-dangerous feature (good for security!)"
.to_string(),
features_tested: vec![],
error: Some("insecure-dangerous feature not enabled".to_string()),
})
@@ -549,11 +561,9 @@ async fn test_self_signed() -> Json<TestResponse> {
/// Test custom timeout configuration
async fn test_custom_timeout(Path(seconds): Path<u64>) -> Json<TestResponse> {
let timeout_duration = Duration::from_secs(seconds);
let client = HttpClient::builder()
.with_timeout(timeout_duration)
.build();
let client = HttpClient::builder().with_timeout(timeout_duration).build();
let result = client.request("https://httpbin.org/delay/1");
Json(TestResponse {
endpoint: format!("/test/config/timeout/{}", seconds),
status: "success".to_string(),
@@ -569,28 +579,31 @@ async fn test_custom_timeout(Path(seconds): Path<u64>) -> Json<TestResponse> {
/// Test custom headers configuration
async fn test_custom_headers(Path(header_count): Path<usize>) -> Json<TestResponse> {
let mut headers = HashMap::new();
for i in 0..header_count {
headers.insert(
format!("X-Test-Header-{}", i),
format!("test-value-{}", i),
);
headers.insert(format!("X-Test-Header-{}", i), format!("test-value-{}", i));
}
// Add some standard headers
headers.insert("User-Agent".to_string(), "hyper-custom-cert-headers-test/1.0".to_string());
headers.insert(
"User-Agent".to_string(),
"hyper-custom-cert-headers-test/1.0".to_string(),
);
headers.insert("Accept".to_string(), "application/json".to_string());
let client = HttpClient::builder()
.with_timeout(Duration::from_secs(10))
.with_default_headers(headers)
.build();
let result = client.request("https://httpbin.org/headers");
Json(TestResponse {
endpoint: format!("/test/config/headers/{}", header_count),
status: "success".to_string(),
message: format!("Custom headers test with {} headers completed", header_count + 2),
message: format!(
"Custom headers test with {} headers completed",
header_count + 2
),
features_tested: vec!["custom-headers".to_string()],
error: match result.await {
Ok(_) => None,
@@ -614,7 +627,12 @@ async fn test_timeout_error() -> Json<TestResponse> {
let awaited = result.await;
Json(TestResponse {
endpoint: "/test/errors/timeout".to_string(),
status: if awaited.is_err() { "success" } else { "unexpected" }.to_string(),
status: if awaited.is_err() {
"success"
} else {
"unexpected"
}
.to_string(),
message: "Timeout error simulation test completed".to_string(),
features_tested: vec!["timeout-error-handling".to_string()],
error: match awaited {
@@ -633,7 +651,12 @@ async fn test_invalid_url() -> Json<TestResponse> {
Json(TestResponse {
endpoint: "/test/errors/invalid-url".to_string(),
status: if awaited.is_err() { "success" } else { "unexpected" }.to_string(),
status: if awaited.is_err() {
"success"
} else {
"unexpected"
}
.to_string(),
message: "Invalid URL error simulation test completed".to_string(),
features_tested: vec!["url-validation".to_string()],
error: match awaited {
@@ -654,7 +677,12 @@ async fn test_connection_error() -> Json<TestResponse> {
Json(TestResponse {
endpoint: "/test/errors/connection".to_string(),
status: if awaited.is_err() { "success" } else { "unexpected" }.to_string(),
status: if awaited.is_err() {
"success"
} else {
"unexpected"
}
.to_string(),
message: "Connection error simulation test completed".to_string(),
features_tested: vec!["connection-error-handling".to_string()],
error: match awaited {
@@ -675,7 +703,7 @@ async fn health_check() -> Json<Value> {
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs();
Json(json!({
"status": "healthy",
"timestamp": timestamp,
@@ -691,13 +719,13 @@ async fn status_check() -> Json<Value> {
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs();
// Test basic client creation to verify library is working
let client_test = match HttpClient::new().request("https://httpbin.org/get").await {
Ok(_) => "operational",
Err(_) => "degraded"
Err(_) => "degraded",
};
Json(json!({
"service": "hyper-custom-cert-test-harness",
"version": "1.0.0",
@@ -711,7 +739,7 @@ async fn status_check() -> Json<Value> {
"endpoints_available": 18,
"test_categories": [
"basic_client_tests",
"feature_specific_tests",
"feature_specific_tests",
"http_method_tests",
"tls_certificate_tests",
"configuration_tests",
@@ -719,4 +747,4 @@ async fn status_check() -> Json<Value> {
"utility_endpoints"
]
}))
}
}