diff --git a/tests/common/mod.rs b/tests/common/mod.rs new file mode 100644 index 0000000..2274d2c --- /dev/null +++ b/tests/common/mod.rs @@ -0,0 +1,37 @@ +//! Raw HTTP/1.1 helpers shared by the integration and TLS test binaries. + +/// One request with `Connection: close`. `host` is used unless `headers` +/// already carries a `Host`. +pub fn request( + method: &str, + path: &str, + host: &str, + headers: &[(&str, &str)], + body: Option<&str>, +) -> String { + let mut req = format!("{method} {path} HTTP/1.1\r\nConnection: close\r\n"); + if !headers.iter().any(|(k, _)| k.eq_ignore_ascii_case("host")) { + req.push_str(&format!("Host: {host}\r\n")); + } + for (k, v) in headers { + req.push_str(&format!("{k}: {v}\r\n")); + } + if let Some(b) = body { + req.push_str(&format!("Content-Length: {}\r\n\r\n{b}", b.len())); + } else { + req.push_str("\r\n"); + } + req +} + +/// Split a raw response into (status, lowercase head, body). +pub fn parse_response(buf: &[u8]) -> (u16, String, String) { + let text = String::from_utf8_lossy(buf); + let (head, body) = text.split_once("\r\n\r\n").unwrap_or((&text, "")); + let status = head + .split_whitespace() + .nth(1) + .and_then(|s| s.parse().ok()) + .unwrap_or(0); + (status, head.to_lowercase(), body.to_owned()) +} diff --git a/tests/integration.rs b/tests/integration.rs index 65c06d6..2fe082f 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -1,8 +1,10 @@ -//! Integration tests (P3 I2): the real assembled router on an ephemeral port +//! Integration tests: the real assembled router on an ephemeral port //! with fake sources, plus the real binary for startup and SIGTERM checks. use std::time::Duration; +mod common; + use skyg_linux_tracker::auth::tokens; use skyg_linux_tracker::config::Config; use skyg_linux_tracker::server; @@ -62,20 +64,7 @@ async fn http( wait: Duration, ) -> (u16, String, String) { let mut s = TcpStream::connect(("127.0.0.1", port)).await.unwrap(); - let mut req = format!("{method} {path} HTTP/1.1\r\nConnection: close\r\n"); - if !headers.iter().any(|(k, _)| k.eq_ignore_ascii_case("host")) { - req.push_str(&format!("Host: 127.0.0.1:{port}\r\n")); - } - for (k, v) in headers { - req.push_str(&format!("{k}: {v}\r\n")); - } - if let Some(b) = body { - req.push_str(&format!("Content-Length: {}\r\n", b.len())); - } - req.push_str("\r\n"); - if let Some(b) = body { - req.push_str(b); - } + let req = common::request(method, path, &format!("127.0.0.1:{port}"), headers, body); s.write_all(req.as_bytes()).await.unwrap(); let mut buf = Vec::new(); let _ = tokio::time::timeout(wait, async { @@ -88,14 +77,7 @@ async fn http( } }) .await; - let text = String::from_utf8_lossy(&buf).into_owned(); - let (head, body) = text.split_once("\r\n\r\n").unwrap_or((&text, "")); - let status = head - .split_whitespace() - .nth(1) - .and_then(|s| s.parse().ok()) - .unwrap_or(0); - (status, head.to_lowercase(), body.to_owned()) + common::parse_response(&buf) } const SHORT: Duration = Duration::from_secs(2); diff --git a/tests/tls.rs b/tests/tls.rs index aca7722..247dc17 100644 --- a/tests/tls.rs +++ b/tests/tls.rs @@ -1,4 +1,4 @@ -//! Native TLS tests (WS-I T6): the real `slinuxm` binary serving HTTPS with +//! Native TLS tests: the real `slinuxm` binary serving HTTPS with //! generated self-signed certificates. use std::path::{Path, PathBuf}; @@ -13,6 +13,8 @@ use tokio::io::{AsyncReadExt, AsyncWriteExt}; use tokio::net::TcpStream; use tokio_rustls::TlsConnector; +mod common; + const BIN: &str = env!("CARGO_BIN_EXE_slinuxm"); struct Pair { @@ -176,18 +178,7 @@ async fn https( let der = &conn.peer_certificates().unwrap()[0]; hex(&Sha256::digest(der.as_ref())) }; - let mut req = - format!("{method} {path} HTTP/1.1\r\nHost: localhost:{port}\r\nConnection: close\r\n"); - for (k, v) in headers { - req.push_str(&format!("{k}: {v}\r\n")); - } - if let Some(b) = body { - req.push_str(&format!("Content-Length: {}\r\n", b.len())); - } - req.push_str("\r\n"); - if let Some(b) = body { - req.push_str(b); - } + let req = common::request(method, path, &format!("localhost:{port}"), headers, body); tls.write_all(req.as_bytes()).await.unwrap(); let mut buf = Vec::new(); let mut chunk = [0u8; 4096]; @@ -198,17 +189,11 @@ async fn https( Ok(Ok(n)) => buf.extend_from_slice(&chunk[..n]), } } - let text = String::from_utf8_lossy(&buf).into_owned(); - let (head, body) = text.split_once("\r\n\r\n").unwrap_or((&text, "")); - let status = head - .split_whitespace() - .nth(1) - .and_then(|s| s.parse().ok()) - .unwrap_or(0); + let (status, head, body) = common::parse_response(&buf); Resp { status, - head: head.to_lowercase(), - body: body.to_owned(), + head, + body, peer_fp, } }