From 1fd78e012d8f16cd2c0e7b681f0873fa1fa52610 Mon Sep 17 00:00:00 2001 From: Alexey <247128645+axkurcom@users.noreply.github.com> Date: Fri, 20 Feb 2026 18:02:02 +0300 Subject: [PATCH] Metrics + Fixes in tests --- src/metrics.rs | 33 ++++++++++++++++----------------- src/protocol/tls.rs | 8 +++++--- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/metrics.rs b/src/metrics.rs index 5222295..940a0d8 100644 --- a/src/metrics.rs +++ b/src/metrics.rs @@ -2,7 +2,7 @@ use std::convert::Infallible; use std::net::SocketAddr; use std::sync::Arc; -use http_body_util::Full; +use http_body_util::{Full, BodyExt}; use hyper::body::Bytes; use hyper::server::conn::http1; use hyper::service::service_fn; @@ -54,7 +54,7 @@ pub async fn serve(port: u16, stats: Arc, whitelist: Vec) { } } -fn handle(req: Request, stats: &Stats) -> Result>, Infallible> { +fn handle(req: Request, stats: &Stats) -> Result>, Infallible> { if req.uri().path() != "/metrics" { let resp = Response::builder() .status(StatusCode::NOT_FOUND) @@ -194,21 +194,20 @@ mod tests { stats.increment_connects_all(); stats.increment_connects_all(); - let port = 19091u16; - let s = stats.clone(); - tokio::spawn(async move { - serve(port, s, vec![]).await; - }); - tokio::time::sleep(std::time::Duration::from_millis(50)).await; + let req = Request::builder() + .uri("/metrics") + .body(()) + .unwrap(); + let resp = handle(req, &stats).unwrap(); + assert_eq!(resp.status(), StatusCode::OK); + let body = resp.into_body().collect().await.unwrap().to_bytes(); + assert!(std::str::from_utf8(body.as_ref()).unwrap().contains("telemt_connections_total 3")); - let resp = reqwest::get(format!("http://127.0.0.1:{}/metrics", port)) - .await.unwrap(); - assert_eq!(resp.status(), 200); - let body = resp.text().await.unwrap(); - assert!(body.contains("telemt_connections_total 3")); - - let resp404 = reqwest::get(format!("http://127.0.0.1:{}/other", port)) - .await.unwrap(); - assert_eq!(resp404.status(), 404); + let req404 = Request::builder() + .uri("/other") + .body(()) + .unwrap(); + let resp404 = handle(req404, &stats).unwrap(); + assert_eq!(resp404.status(), StatusCode::NOT_FOUND); } } diff --git a/src/protocol/tls.rs b/src/protocol/tls.rs index d69dc90..d7afdee 100644 --- a/src/protocol/tls.rs +++ b/src/protocol/tls.rs @@ -899,7 +899,8 @@ mod tests { #[test] fn test_extract_alpn_single() { let mut alpn_data = Vec::new(); - alpn_data.extend_from_slice(&2u16.to_be_bytes()); // list len + // list length = 3 (1 length byte + "h2") + alpn_data.extend_from_slice(&3u16.to_be_bytes()); alpn_data.push(2); alpn_data.extend_from_slice(b"h2"); let ch = build_client_hello_with_exts(vec![(0x0010, alpn_data)], "alpn.test"); @@ -910,10 +911,11 @@ mod tests { #[test] fn test_extract_alpn_multiple() { let mut alpn_data = Vec::new(); - alpn_data.extend_from_slice(&9u16.to_be_bytes()); // list len + // list length = 11 (sum of per-proto lengths including length bytes) + alpn_data.extend_from_slice(&11u16.to_be_bytes()); alpn_data.push(2); alpn_data.extend_from_slice(b"h2"); - alpn_data.push(3); + alpn_data.push(4); alpn_data.extend_from_slice(b"spdy"); alpn_data.push(2); alpn_data.extend_from_slice(b"h3");