From 21800c7a8f3a1d13119cfb7d5482cb5c1f474f94 Mon Sep 17 00:00:00 2001 From: "dzianis.lituyeu" Date: Tue, 7 Apr 2026 12:44:04 +0300 Subject: [PATCH 1/2] Fix headers processing --- Cargo.lock | 306 +++++++++++++++++++++++++++++++++++++++- Cargo.toml | 1 + src/auth/headers.rs | 52 ++++--- src/auth/validator.rs | 10 +- src/lib.rs | 1 + src/proxy/directives.rs | 85 ++++++++++- 6 files changed, 417 insertions(+), 38 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e419fb8..316bd47 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -113,6 +113,12 @@ version = "2.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "843867be96c8daad0d758b57df9392b6d8d271134fce549de6ce169ff98a92af" +[[package]] +name = "bumpalo" +version = "3.20.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d20789868f4b01b2f2caec9f5c4e0213b41e3e5702a50157d699ae31ced2fcb" + [[package]] name = "bytes" version = "1.11.1" @@ -293,6 +299,12 @@ version = "1.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719" +[[package]] +name = "equivalent" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" + [[package]] name = "errno" version = "0.3.14" @@ -315,6 +327,12 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" +[[package]] +name = "foldhash" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2" + [[package]] name = "fs_extra" version = "1.3.0" @@ -373,8 +391,21 @@ checksum = "899def5c37c4fd7b2664648c28120ecec138e4d395b459e5ca34f9cce2dd77fd" dependencies = [ "cfg-if", "libc", - "r-efi", + "r-efi 5.3.0", + "wasip2", +] + +[[package]] +name = "getrandom" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0de51e6874e94e7bf76d726fc5d13ba782deca734ff60d5bb2fb2607c7406555" +dependencies = [ + "cfg-if", + "libc", + "r-efi 6.0.0", "wasip2", + "wasip3", ] [[package]] @@ -388,6 +419,21 @@ dependencies = [ "zerocopy", ] +[[package]] +name = "hashbrown" +version = "0.15.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9229cfe53dfd69f0609a49f65461bd93001ea1ef889cd5529dd176593f5338a1" +dependencies = [ + "foldhash", +] + +[[package]] +name = "hashbrown" +version = "0.16.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "841d1cc9bed7f9236f321df977030373f4a4163ae1a7dbfe1a51a2c1a51d9100" + [[package]] name = "heck" version = "0.5.0" @@ -507,6 +553,24 @@ dependencies = [ "tracing", ] +[[package]] +name = "id-arena" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d3067d79b975e8844ca9eb072e16b31c3c1c36928edf9c6789548c524d0d954" + +[[package]] +name = "indexmap" +version = "2.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "45a8a2b9cb3e0b0c1803dbb0758ffac5de2f425b23c28f518faabd9d805342ff" +dependencies = [ + "equivalent", + "hashbrown 0.16.1", + "serde", + "serde_core", +] + [[package]] name = "is-terminal" version = "0.4.17" @@ -549,12 +613,28 @@ dependencies = [ "libc", ] +[[package]] +name = "js-sys" +version = "0.3.94" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e04e2ef80ce82e13552136fabeef8a5ed1f985a96805761cbb9a2c34e7664d9" +dependencies = [ + "once_cell", + "wasm-bindgen", +] + [[package]] name = "lazy_static" version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" +[[package]] +name = "leb128fmt" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09edd9e8b54e49e587e4f6295a7d29c3ea94d469cb40ab8ca70b288248a81db2" + [[package]] name = "libc" version = "0.2.177" @@ -657,6 +737,16 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" +[[package]] +name = "prettyplease" +version = "0.2.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "479ca8adacdd7ce8f1fb39ce9ecccbfe93a3f1344b3d0d97f20bc0196208f62b" +dependencies = [ + "proc-macro2", + "syn", +] + [[package]] name = "proc-macro2" version = "1.0.103" @@ -681,6 +771,12 @@ version = "5.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" +[[package]] +name = "r-efi" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf" + [[package]] name = "regex" version = "1.12.3" @@ -772,6 +868,12 @@ dependencies = [ "untrusted", ] +[[package]] +name = "rustversion" +version = "1.0.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" + [[package]] name = "same-file" version = "1.0.6" @@ -813,6 +915,12 @@ dependencies = [ "libc", ] +[[package]] +name = "semver" +version = "1.0.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a7852d02fc848982e0c167ef163aaff9cd91dc640ba85e263cb1ce46fae51cd" + [[package]] name = "serde" version = "1.0.228" @@ -969,6 +1077,7 @@ dependencies = [ "tokio", "tracing", "tracing-subscriber", + "uuid", ] [[package]] @@ -1097,6 +1206,12 @@ version = "1.0.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5" +[[package]] +name = "unicode-xid" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853" + [[package]] name = "untrusted" version = "0.9.0" @@ -1109,6 +1224,17 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" +[[package]] +name = "uuid" +version = "1.23.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ac8b6f42ead25368cf5b098aeb3dc8a1a2c05a3eee8a9a1a68c640edbfc79d9" +dependencies = [ + "getrandom 0.4.2", + "js-sys", + "wasm-bindgen", +] + [[package]] name = "valuable" version = "0.1.1" @@ -1146,7 +1272,95 @@ version = "1.0.1+wasi-0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0562428422c63773dad2c345a1882263bbf4d65cf3f42e90921f787ef5ad58e7" dependencies = [ - "wit-bindgen", + "wit-bindgen 0.46.0", +] + +[[package]] +name = "wasip3" +version = "0.4.0+wasi-0.3.0-rc-2026-01-06" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5428f8bf88ea5ddc08faddef2ac4a67e390b88186c703ce6dbd955e1c145aca5" +dependencies = [ + "wit-bindgen 0.51.0", +] + +[[package]] +name = "wasm-bindgen" +version = "0.2.117" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0551fc1bb415591e3372d0bc4780db7e587d84e2a7e79da121051c5c4b89d0b0" +dependencies = [ + "cfg-if", + "once_cell", + "rustversion", + "wasm-bindgen-macro", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.117" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fbdf9a35adf44786aecd5ff89b4563a90325f9da0923236f6104e603c7e86be" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.117" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dca9693ef2bab6d4e6707234500350d8dad079eb508dca05530c85dc3a529ff2" +dependencies = [ + "bumpalo", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.117" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39129a682a6d2d841b6c429d0c51e5cb0ed1a03829d8b3d1e69a011e62cb3d3b" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "wasm-encoder" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "990065f2fe63003fe337b932cfb5e3b80e0b4d0f5ff650e6985b1048f62c8319" +dependencies = [ + "leb128fmt", + "wasmparser", +] + +[[package]] +name = "wasm-metadata" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb0e353e6a2fbdc176932bbaab493762eb1255a7900fe0fea1a2f96c296cc909" +dependencies = [ + "anyhow", + "indexmap", + "wasm-encoder", + "wasmparser", +] + +[[package]] +name = "wasmparser" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47b807c72e1bac69382b3a6fb3dbe8ea4c0ed87ff5629b8685ae6b9a611028fe" +dependencies = [ + "bitflags", + "hashbrown 0.15.5", + "indexmap", + "semver", ] [[package]] @@ -1326,6 +1540,94 @@ version = "0.46.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f17a85883d4e6d00e8a97c586de764dabcc06133f7f1d55dce5cdc070ad7fe59" +[[package]] +name = "wit-bindgen" +version = "0.51.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7249219f66ced02969388cf2bb044a09756a083d0fab1e566056b04d9fbcaa5" +dependencies = [ + "wit-bindgen-rust-macro", +] + +[[package]] +name = "wit-bindgen-core" +version = "0.51.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea61de684c3ea68cb082b7a88508a8b27fcc8b797d738bfc99a82facf1d752dc" +dependencies = [ + "anyhow", + "heck", + "wit-parser", +] + +[[package]] +name = "wit-bindgen-rust" +version = "0.51.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7c566e0f4b284dd6561c786d9cb0142da491f46a9fbed79ea69cdad5db17f21" +dependencies = [ + "anyhow", + "heck", + "indexmap", + "prettyplease", + "syn", + "wasm-metadata", + "wit-bindgen-core", + "wit-component", +] + +[[package]] +name = "wit-bindgen-rust-macro" +version = "0.51.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c0f9bfd77e6a48eccf51359e3ae77140a7f50b1e2ebfe62422d8afdaffab17a" +dependencies = [ + "anyhow", + "prettyplease", + "proc-macro2", + "quote", + "syn", + "wit-bindgen-core", + "wit-bindgen-rust", +] + +[[package]] +name = "wit-component" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d66ea20e9553b30172b5e831994e35fbde2d165325bec84fc43dbf6f4eb9cb2" +dependencies = [ + "anyhow", + "bitflags", + "indexmap", + "log", + "serde", + "serde_derive", + "serde_json", + "wasm-encoder", + "wasm-metadata", + "wasmparser", + "wit-parser", +] + +[[package]] +name = "wit-parser" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ecc8ac4bc1dc3381b7f59c34f00b67e18f910c2c0f50015669dde7def656a736" +dependencies = [ + "anyhow", + "id-arena", + "indexmap", + "log", + "semver", + "serde", + "serde_derive", + "serde_json", + "unicode-xid", + "wasmparser", +] + [[package]] name = "zerocopy" version = "0.8.42" diff --git a/Cargo.toml b/Cargo.toml index 30f88ee..573dd4e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -43,6 +43,7 @@ anyhow = "1.0" thiserror = "2.0.18" bytes = "1.0" num_cpus = "1.16" +uuid = { version = "1", features = ["v4"] } serde = { version = "1.0", features = ["derive"], optional = true } serde_json = { version = "1.0", optional = true } diff --git a/src/auth/headers.rs b/src/auth/headers.rs index b3faff7..7c9e3ff 100644 --- a/src/auth/headers.rs +++ b/src/auth/headers.rs @@ -3,7 +3,7 @@ //! This module provides utilities for processing header value substitutions //! including request headers, UUIDs, and environment variables. -use hyper::{body::Incoming, Request}; +use hyper::Request; /// Process header value substitutions /// @@ -33,7 +33,7 @@ use hyper::{body::Incoming, Request}; /// # Ok(()) /// # } /// ``` -pub fn process_header_substitution(value: &str, req: &Request) -> anyhow::Result { +pub fn process_header_substitution(value: &str, req: &Request) -> anyhow::Result { let mut result = value.to_string(); // Process {header.Name} substitutions @@ -90,7 +90,7 @@ pub fn process_header_substitution(value: &str, req: &Request) -> anyh /// # Returns /// /// The remote IP address if found, None otherwise -pub fn extract_remote_ip(req: &Request) -> Option { +pub fn extract_remote_ip(req: &Request) -> Option { // Check X-Forwarded-For header (set by proxies) if let Some(xff) = req.headers().get("X-Forwarded-For") { if let Ok(xff_str) = xff.to_str() { @@ -113,14 +113,24 @@ pub fn extract_remote_ip(req: &Request) -> Option { #[cfg(test)] mod tests { use super::*; - use hyper::{body::Incoming, Request}; + use bytes::Bytes; + use http_body_util::Empty; + use hyper::Request; + + fn make_request() -> Request> { + Request::builder().body(Empty::new()).unwrap() + } + + fn make_request_with_header(name: &str, value: &str) -> Request> { + Request::builder() + .header(name, value) + .body(Empty::new()) + .unwrap() + } #[test] fn test_process_header_substitution_header() { - let mut req = Request::builder() - .header("X-User-ID", "12345") - .body(hyper::body::Incoming::empty()) - .unwrap(); + let req = make_request_with_header("X-User-ID", "12345"); let result = process_header_substitution("User: {header.X-User-ID}", &req).unwrap(); assert_eq!(result, "User: 12345"); @@ -129,9 +139,7 @@ mod tests { #[test] fn test_process_header_substitution_env() { std::env::set_var("TEST_VAR", "test-value"); - let req = Request::builder() - .body(hyper::body::Incoming::empty()) - .unwrap(); + let req = make_request(); let result = process_header_substitution("Value: {env.TEST_VAR}", &req).unwrap(); assert_eq!(result, "Value: test-value"); @@ -140,9 +148,7 @@ mod tests { #[test] fn test_process_header_substitution_uuid() { - let req = Request::builder() - .body(hyper::body::Incoming::empty()) - .unwrap(); + let req = make_request(); let result = process_header_substitution("ID: {uuid}", &req).unwrap(); assert!(result.starts_with("ID: ")); @@ -151,9 +157,7 @@ mod tests { #[test] fn test_process_header_substitution_missing_header() { - let req = Request::builder() - .body(hyper::body::Incoming::empty()) - .unwrap(); + let req = make_request(); let result = process_header_substitution("Value: {header.Missing}", &req).unwrap(); assert_eq!(result, "Value: "); @@ -161,10 +165,7 @@ mod tests { #[test] fn test_extract_remote_ip_xff() { - let req = Request::builder() - .header("X-Forwarded-For", "192.168.1.1, 10.0.0.1") - .body(hyper::body::Incoming::empty()) - .unwrap(); + let req = make_request_with_header("X-Forwarded-For", "192.168.1.1, 10.0.0.1"); let ip = extract_remote_ip(&req); assert_eq!(ip, Some("192.168.1.1".to_string())); @@ -172,10 +173,7 @@ mod tests { #[test] fn test_extract_remote_ip_xri() { - let req = Request::builder() - .header("X-Real-IP", "192.168.1.2") - .body(hyper::body::Incoming::empty()) - .unwrap(); + let req = make_request_with_header("X-Real-IP", "192.168.1.2"); let ip = extract_remote_ip(&req); assert_eq!(ip, Some("192.168.1.2".to_string())); @@ -183,9 +181,7 @@ mod tests { #[test] fn test_extract_remote_ip_none() { - let req = Request::builder() - .body(hyper::body::Incoming::empty()) - .unwrap(); + let req = make_request(); let ip = extract_remote_ip(&req); assert!(ip.is_none()); diff --git a/src/auth/validator.rs b/src/auth/validator.rs index a15b9ed..f1a775a 100644 --- a/src/auth/validator.rs +++ b/src/auth/validator.rs @@ -4,7 +4,7 @@ //! against external authentication services. use hyper::body::Incoming; -use hyper::{Request, StatusCode}; +use hyper::Request; /// Validate an authentication token from a request /// @@ -100,13 +100,13 @@ pub async fn validate_token_with_header( #[cfg(test)] mod tests { use super::*; + use bytes::Bytes; + use http_body_util::Empty; use hyper::Request; #[test] fn test_validate_token_missing_header() { - let req = Request::builder() - .body(hyper::body::Incoming::empty()) - .unwrap(); + let req = Request::builder().body(Empty::::new()).unwrap(); // This would fail in a real async test, but we're just testing the logic let validator_url = "http://auth-service:8080/validate"; @@ -119,7 +119,7 @@ mod tests { fn test_validate_token_with_custom_header() { let req = Request::builder() .header("X-Auth-Token", "test-token-123") - .body(hyper::body::Incoming::empty()) + .body(Empty::::new()) .unwrap(); let validator_url = "http://auth-service:8080/validate"; diff --git a/src/lib.rs b/src/lib.rs index dee85a9..a210c9a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -76,6 +76,7 @@ //! //! For more configuration options, see the [config] module documentation. +pub mod auth; #[cfg(feature = "cli")] pub mod cli; pub mod config; diff --git a/src/proxy/directives.rs b/src/proxy/directives.rs index 30874ef..4aeb4cf 100644 --- a/src/proxy/directives.rs +++ b/src/proxy/directives.rs @@ -2,6 +2,8 @@ use hyper::body::Incoming; use hyper::Request; use tracing::info; +use crate::auth::process_header_substitution; + use crate::proxy::ActionResult; /// Handle reverse_proxy directive @@ -23,14 +25,19 @@ pub fn handle_respond(status: &u16, body: &str) -> ActionResult { } /// Handle header directive - add or replace header in request -pub fn handle_header(name: &str, value: &str, req: &mut Request) -> anyhow::Result<()> { +/// Supports placeholder substitution: {uuid}, {header.Name}, {env.VAR} +pub fn handle_header(name: &str, value: &str, req: &mut Request) -> anyhow::Result<()> { use hyper::header::{HeaderName, HeaderValue}; let header_name = HeaderName::from_bytes(name.as_bytes())?; - let header_value = HeaderValue::from_str(value)?; + + // Process placeholders like {uuid}, {header.Name}, {env.VAR} + let processed_value = process_header_substitution(value, req)?; + + let header_value = HeaderValue::from_str(&processed_value)?; req.headers_mut().insert(header_name, header_value); - info!(" Applied header: {}", name); + info!(" Applied header: {} = {}", name, processed_value); Ok(()) } @@ -47,3 +54,75 @@ pub fn handle_method(methods: &[String], req: &Request) -> bool { .iter() .any(|m| m.eq_ignore_ascii_case(req.method().as_str())) } + +#[cfg(test)] +mod tests { + use super::*; + use bytes::Bytes; + use http_body_util::Empty; + + fn make_request() -> Request> { + Request::builder() + .header("Authorization", "Bearer secret-token") + .body(Empty::new()) + .unwrap() + } + + #[test] + fn test_handle_header_static_value() { + let mut req = make_request(); + handle_header("X-Static", "hello-world", &mut req).unwrap(); + + let value = req.headers().get("X-Static").unwrap().to_str().unwrap(); + assert_eq!(value, "hello-world"); + } + + #[test] + fn test_handle_header_uuid_placeholder() { + // {uuid} should be replaced with a real UUID like "550e8400-e29b-41d4-..." + let mut req = make_request(); + handle_header("X-Request-ID", "{uuid}", &mut req).unwrap(); + + let value = req.headers().get("X-Request-ID").unwrap().to_str().unwrap(); + + assert_ne!(value, "{uuid}", "Should not be literal placeholder"); + assert!(value.contains("-"), "UUID should contain dashes"); + assert_eq!(value.len(), 36, "UUID should be 36 characters"); + } + + #[test] + fn test_handle_header_header_placeholder() { + // {header.Authorization} should be replaced with "Bearer secret-token" + let mut req = make_request(); + handle_header("X-Client-Auth", "{header.Authorization}", &mut req).unwrap(); + + let value = req + .headers() + .get("X-Client-Auth") + .unwrap() + .to_str() + .unwrap(); + + assert_eq!( + value, "Bearer secret-token", + "Should extract value from Authorization header" + ); + } + + #[test] + fn test_handle_header_env_placeholder() { + std::env::set_var("TEST_PROXY_VAR", "test-value-123"); + + let mut req = make_request(); + handle_header("X-Env-Test", "{env.TEST_PROXY_VAR}", &mut req).unwrap(); + + let value = req.headers().get("X-Env-Test").unwrap().to_str().unwrap(); + + assert_eq!( + value, "test-value-123", + "Should substitute environment variable" + ); + + std::env::remove_var("TEST_PROXY_VAR"); + } +} From 3a1cdd70be92e7c2f6ada0c2168750e4044ea6c4 Mon Sep 17 00:00:00 2001 From: "dzianis.lituyeu" Date: Tue, 7 Apr 2026 12:54:21 +0300 Subject: [PATCH 2/2] Fix test and lint --- src/auth/headers.rs | 6 ++++-- src/auth/validator.rs | 22 +++++++++++----------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/auth/headers.rs b/src/auth/headers.rs index 7c9e3ff..b0ac926 100644 --- a/src/auth/headers.rs +++ b/src/auth/headers.rs @@ -24,10 +24,12 @@ use hyper::Request; /// # Example /// /// ```no_run -/// # use hyper::{Request, body::Incoming}; +/// # use hyper::Request; +/// # use bytes::Bytes; +/// # use http_body_util::Empty; /// # use tiny_proxy::auth::headers::process_header_substitution; /// # fn main() -> anyhow::Result<()> { -/// # let req = Request::builder().body(hyper::body::Incoming::empty()).unwrap(); +/// # let req = Request::builder().body(Empty::::new()).unwrap(); /// let result = process_header_substitution("X-Request-ID: {uuid}", &req)?; /// assert!(result.contains("X-Request-ID:")); /// # Ok(()) diff --git a/src/auth/validator.rs b/src/auth/validator.rs index f1a775a..e680806 100644 --- a/src/auth/validator.rs +++ b/src/auth/validator.rs @@ -3,7 +3,6 @@ //! This module provides functionality for validating tokens //! against external authentication services. -use hyper::body::Incoming; use hyper::Request; /// Validate an authentication token from a request @@ -27,15 +26,17 @@ use hyper::Request; /// ```no_run /// use tiny_proxy::auth::validator::validate_token; /// use hyper::Request; +/// use bytes::Bytes; +/// use http_body_util::Empty; /// /// # #[tokio::main] /// # async fn main() -> anyhow::Result<()> { -/// # let req = Request::builder().body(hyper::body::Incoming::empty()).unwrap(); +/// # let req = Request::builder().body(Empty::::new()).unwrap(); /// let is_valid = validate_token(&req, "http://auth-service:8080/validate").await?; /// # Ok(()) /// # } /// ``` -pub async fn validate_token(req: &Request, validator_url: &str) -> anyhow::Result { +pub async fn validate_token(req: &Request, validator_url: &str) -> anyhow::Result { // Extract token from Authorization header let auth_header = req .headers() @@ -74,8 +75,8 @@ pub async fn validate_token(req: &Request, validator_url: &str) -> any /// * `Ok(true)` - Token is valid /// * `Ok(false)` - Token is invalid /// * `Err(...)` - Error occurred during validation -pub async fn validate_token_with_header( - req: &Request, +pub async fn validate_token_with_header( + req: &Request, validator_url: &str, header_name: &str, ) -> anyhow::Result { @@ -99,17 +100,16 @@ pub async fn validate_token_with_header( #[cfg(test)] mod tests { - use super::*; use bytes::Bytes; use http_body_util::Empty; use hyper::Request; #[test] fn test_validate_token_missing_header() { - let req = Request::builder().body(Empty::::new()).unwrap(); + let _req = Request::builder().body(Empty::::new()).unwrap(); // This would fail in a real async test, but we're just testing the logic - let validator_url = "http://auth-service:8080/validate"; + let _validator_url = "http://auth-service:8080/validate"; // Note: This test demonstrates the function signature // In a real test, you'd use a tokio::test and mock the HTTP client @@ -117,13 +117,13 @@ mod tests { #[test] fn test_validate_token_with_custom_header() { - let req = Request::builder() + let _req = Request::builder() .header("X-Auth-Token", "test-token-123") .body(Empty::::new()) .unwrap(); - let validator_url = "http://auth-service:8080/validate"; - let header_name = "X-Auth-Token"; + let _validator_url = "http://auth-service:8080/validate"; + let _header_name = "X-Auth-Token"; // Note: This test demonstrates the function signature // In a real test, you'd use a tokio::test and mock the HTTP client