diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 9ec6a43f0b..a1c1c25ba9 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -34918,8 +34918,16 @@ fn serve_with( let Ok(mut s) = stream else { continue }; let (body, count, gate, refusals, seen) = (body.clone(), count.clone(), gate.clone(), refusals.clone(), seen.clone()); std::thread::spawn(move || { - let mut buf = [0u8; 4096]; - let read = s.read(&mut buf).unwrap_or(0); + // The whole request head: one `read` can return only the first TCP segment, with the + // `Range` header still on its way, which answered a ranged request with the whole file. + let mut buf = [0u8; 8192]; + let mut read = 0; + while read < buf.len() && !buf[..read].windows(4).any(|w| w == b"\r\n\r\n") { + match s.read(&mut buf[read..]) { + Ok(0) | Err(_) => break, + Ok(n) => read += n, + } + } // Refused from the `refuse_from`th request on: a file whose tiles can't be read. let nth = seen.fetch_add(1, std::sync::atomic::Ordering::Relaxed) + 1; if refuse_from > 0 && nth >= refuse_from { @@ -34973,6 +34981,23 @@ fn serve_with( (format!("http://{addr}"), sent) } +/// The fixture server answers a ranged request whose head arrives in pieces (as a slow or +/// segmenting network delivers it) with the range, not the whole file. +#[test] +fn fixture_server_reads_a_request_head_that_arrives_in_pieces() { + use std::io::{Read, Write}; + let (base, _) = serve_ranges((0..=255u8).collect()); + let mut c = std::net::TcpStream::connect(base.trim_start_matches("http://")).unwrap(); + c.write_all(b"GET /f HTTP/1.1\r\nHost: x\r\n").unwrap(); + c.flush().unwrap(); + std::thread::sleep(std::time::Duration::from_millis(100)); // test-wait: force the request head to arrive in two segments + c.write_all(b"Range: bytes=10-19\r\n\r\n").unwrap(); + let mut answer = Vec::new(); + c.read_to_end(&mut answer).unwrap(); + let answer = String::from_utf8_lossy(&answer); + assert!(answer.starts_with("HTTP/1.1 206 "), "{answer}"); +} + /// A static collection laid out by date (JAXA Earth's) as a data cube: no search, the period's /// date folders read and each item's own file taken, with the scale and no-data in `je:rasters`, /// on the time slider and in a trend; a document missing is told, and clean input tells nothing.