Files
craft-anything/crates/platform/tests/regression_preview_head.rs
BFlatandAfloat 3ee1642770 Serve previews only to this computer, stream their files and bound their connections (#649)
Replays #597, #598, #600 and #601. The preview server answers only requests addressed to this computer, streams files, sends no body for HEAD on a missing file, caps open connections and gives a request head one deadline.

Co-authored-by: Afloat <132809511+Afloat16@users.noreply.github.com>
2026-11-03 20:07:33 +07:00

28 lines
1.1 KiB
Rust

use craft_anything_platform::serve::serve_dir;
use std::io::{Read, Write};
use std::net::TcpStream;
use std::time::Duration;
fn get(port: u16, path: &str) -> String {
let mut s = TcpStream::connect(("127.0.0.1", port)).unwrap();
s.set_read_timeout(Some(Duration::from_secs(10))).unwrap();
s.write_all(format!("GET {path} HTTP/1.1\r\nHost: 127.0.0.1:{port}\r\n\r\n").as_bytes()).unwrap();
let mut out = String::new();
s.read_to_string(&mut out).unwrap();
out
}
#[test]
fn regression_head_errors_have_no_body() {
let dir = tempfile::tempdir().unwrap();
let srv = serve_dir(dir.path()).unwrap();
let mut s = TcpStream::connect(("127.0.0.1", srv.port)).unwrap();
s.set_read_timeout(Some(Duration::from_secs(10))).unwrap();
s.write_all(format!("HEAD /missing.js HTTP/1.1\r\nHost: 127.0.0.1:{}\r\n\r\n", srv.port).as_bytes()).unwrap();
let mut out = String::new();
s.read_to_string(&mut out).unwrap();
assert!(out.starts_with("HTTP/1.1 404"));
assert!(out.contains("Content-Length: 9\r\n"));
assert!(out.ends_with("\r\n\r\n"), "{out}");
assert!(get(srv.port, "/missing.js").ends_with("Not found"));
}