Rewrite your app in Rust, piece by piece, checked against the original at every step. Works with TypeScript, JavaScript, Python, Go, Ruby, Java, C, PHP, C#, Kotlin, PowerShell and Swift.
Rust agent-skillsclaude-codeclicode-migrationcodexffigolangjavajavascriptmcpmigrationnapiperformanceportingpyo3pythonrewriterubyrusttypescript
fix(app): refuse an HTTP case whose two recording runs share nothing of the body
Kam776c42f112eb
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex dbd4308e..b6887c7a 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -72,6 +72,11 @@lists and maps (Kotlin and Android use the same driver). An exception message and a map keystay text, with a lone surrogate replaced by U+FFFD. A recording made before this change stillholds the old escapes: record it again (#1291).+- `app record` on a server refuses a case whose body the two runs share nothing of, and names+ it. Another program can take the port after the check and before the server listens, so one+ run talks to it; the two answers then differed everywhere and the case was saved as `[..]`,+ which accepts any one-line body. A body that is random by design records with+ `--normalize name=regex` for the part that changes (#1268, reported by @AyhamJo7).- Ruby: a crate bridged without `--batch` passes `gate`'s clippy check. The generated `src/lib.rs`imported magnus's `Object` and `method`, which only the batch classes use, so clippy with`-D warnings` failed on `unused imports: Object and method` in generated code the port's authordiff --git a/src/http.rs b/src/http.rsindex c36aefa1..e43fa78a 100644--- a/src/http.rs+++ b/src/http.rs@@ -2101,6 +2101,7 @@ pub(crate) fn record(}let mut settled = Vec::new();let mut literal = Vec::new();+ let mut unrelated = Vec::new();let mut leaked = Vec::new();for (((name, req), (a, _, fx_a)), (b, _, fx_b)) in planned.iter().zip(one).zip(two) {let (Ok(a), Ok(b)) = (a, b) else {@@ -2132,6 +2133,9 @@ pub(crate) fn record(if varied(body_noise) && (wild(&a.body) || wild(&b.body)) {literal.push(format!("{name} (body)"));}+ if varied(body_noise) && !binary && only_wildcards(&body) {+ unrelated.push(name.clone());+ }let wild_header = a.headers.iter().any(|(k, va)| {!never_compared(k)&& !PRESENCE_HEADERS.contains(&k.as_str())@@ -2183,6 +2187,11 @@ pub(crate) fn record("the original prints `[..]` or a `...` line itself in {}, and its two runs also differ there: the recording would read that text as a wildcard. Make the runs agree (`--normalize name=regex` for the part that changes) and record again",literal.join(", "));+ ensure!(+ unrelated.is_empty(),+ "the two runs answered {} with bodies that have nothing in common, so the recording would accept any body there. Another program may have answered on the port in one run (it can take the port after the check and before the server listens): record again. If the answer is random by design, `--normalize name=regex` the part that changes. Nothing was written",+ unrelated.join(", ")+ );ensure!(leaked.is_empty(),"the recording holds the value of an environment variable whose name says it is a secret, in {}: it would be committed with the cases. Record with `env -u NAME` for each variable named, or change the inputs. Nothing was written",@@ -2235,6 +2244,13 @@ pub(crate) fn record(Ok(outcome)}+/// Whether a body settled from two runs kept nothing of either: every line is a wildcard.+fn only_wildcards(body: &[u8]) -> bool {+ String::from_utf8_lossy(body)+ .split('\n')+ .all(|line| line == "..." || line.replace("[..]", "").trim().is_empty())+}+struct Spec {name: String,req: Req,diff --git a/tests/http.rs b/tests/http.rsindex ac21968e..fa7c63c4 100644--- a/tests/http.rs+++ b/tests/http.rs@@ -1698,6 +1698,102 @@ fn a_server_listening_only_on_ipv6_loopback_is_recorded_and_replayed() {std::fs::remove_dir_all(repo).unwrap();}+const SLOW_TO_BIND: &str = r#"import http.server, os, time+class H(http.server.BaseHTTPRequestHandler):+ def do_GET(self):+ body = b"original\n"+ self.send_response(200)+ self.send_header("Content-Length", str(len(body)))+ self.end_headers()+ self.wfile.write(body)+ def log_message(self, *args):+ pass+open("started", "w").close()+deadline = time.time() + 60+while not os.path.exists("released") and time.time() < deadline:+ time.sleep(0.05)+http.server.HTTPServer(("127.0.0.1", int(os.environ["PORT"])), H).serve_forever()+"#;++#[test]+fn a_body_another_program_answered_in_one_run_is_refused_not_recorded_as_noise() {+ use std::io::{Read, Write};+ let Some(python) = common::python() else {+ return;+ };+ let repo = std::env::temp_dir().join(format!(+ "rust-anything-http-port-taken-{}",+ std::process::id()+ ));+ let _ = std::fs::remove_dir_all(&repo);+ std::fs::create_dir_all(&repo).unwrap();+ std::fs::write(repo.join("server.py"), SLOW_TO_BIND).unwrap();+ let port = rust_anything::http::free_port().unwrap();+ let dir = repo.clone();+ let other = std::thread::spawn(move || {+ let started = std::time::Instant::now();+ while !dir.join("started").exists() {+ assert!(started.elapsed() < std::time::Duration::from_secs(60));+ std::thread::sleep(std::time::Duration::from_millis(20));+ }+ let listener = std::net::TcpListener::bind(("127.0.0.1", port)).unwrap();+ for stream in listener.incoming() {+ let mut stream = stream.unwrap();+ let mut request = Vec::new();+ let mut buf = [0u8; 1024];+ while !request.windows(4).any(|w| w == b"\r\n\r\n") {+ match stream.read(&mut buf) {+ Ok(0) | Err(_) => break,+ Ok(n) => request.extend_from_slice(&buf[..n]),+ }+ }+ if request.is_empty() {+ continue;+ }+ let body = b"another program\n";+ write!(+ stream,+ "HTTP/1.0 200 OK\r\nContent-Length: {}\r\n\r\n",+ body.len()+ )+ .unwrap();+ stream.write_all(body).unwrap();+ break;+ }+ drop(listener);+ std::fs::write(dir.join("released"), "").unwrap();+ });+ let out = app::record(&RecordOptions {+ repo: &repo,+ run: &[python.to_string(), "server.py".to_string()],+ inputs: None,+ normalize: &[],+ name: None,+ traps: false,+ timeout_secs: None,+ http: Some(true),+ server_args: None,+ fresh: None,+ pace_ms: None,+ port: Some(port),+ float_tolerance: None,+ float_files: &[],+ effects: None,+ ready_secs: None,+ ready_request: None,+ sandbox: false,+ });+ other.join().unwrap();+ let written = repo.join("rust/app-cases/http").exists();+ let _ = std::fs::remove_dir_all(&repo);+ let Err(err) = out else {+ panic!("the recording was saved");+ };+ let err = format!("{err:#}");+ assert!(err.contains("bodies that have nothing in common"), "{err}");+ assert!(!written, "cases were written: {err}");+}+const PLAIN_SERVER: &str = r#"import http.server, osclass H(http.server.BaseHTTPRequestHandler):def do_GET(self):