diff --git a/packages/cli-rs/crates/parity/src/app.rs b/packages/cli-rs/crates/parity/src/app.rs index 8a5fbb086f2..bfe74817bb6 100644 --- a/packages/cli-rs/crates/parity/src/app.rs +++ b/packages/cli-rs/crates/parity/src/app.rs @@ -394,22 +394,30 @@ pub fn is_read(method: &str, path: &str) -> bool { .is_some_and(|plugin| !plugin.is_empty() && !plugin.contains('/')) } -/// `actual` holds every Rust request in order; `unmatched` the ones no route answered. Bodies are compared -/// with client-minted IDs normalized (plan 7.5). +/// `actual` holds every Rust request in order; `unmatched` the ones no route answered. Writes are compared by +/// method, path, sorted query, and body, with client-minted IDs normalized (plan 7.5). Both sides get the status +/// of the same recorded route, so it is not compared. pub fn compare_requests(recording: &Recording, actual: &[Sent], unmatched: &[Sent]) -> RequestComparison { - let writes: Vec<(&str, &str, Option)> = actual + // Bodies stay JSON values, so key order does not count. + let write = |method: &str, path: &str, query: &Query, body: Option<&Value>| { + let query: Vec = + query.iter().flat_map(|(key, values)| values.iter().map(move |value| format!("{key}={value}"))).collect(); + let query = if query.is_empty() { String::new() } else { format!("?{}", query.join("&")) }; + (format!("{method} {path}{query}"), body.map(minted)) + }; + let writes: Vec<(String, Option)> = actual .iter() .filter(|sent| !is_read(&sent.method, &sent.path)) - .map(|sent| (sent.method.as_str(), sent.path.as_str(), sent.body.as_ref().map(minted))) + .map(|sent| write(&sent.method, &sent.path, &sent.query, sent.body.as_ref())) .collect(); - let expected: Vec<(&str, &str, Option)> = recording + let expected: Vec<(String, Option)> = recording .requests .writes .iter() - .map(|sent| (sent.method.as_str(), sent.path.as_str(), sent.body.as_ref().map(minted))) + .map(|sent| write(&sent.method, &sent.path, &sent.query, sent.body.as_ref())) .collect(); - let line = |(method, path, body): &(&str, &str, Option)| { - format!("{method} {path}{}", body.as_ref().map(|body| format!(" {body}")).unwrap_or_default()) + let line = |(head, body): &(String, Option)| { + format!("{head}{}", body.as_ref().map(|body| format!(" {body}")).unwrap_or_default()) }; let writes_differ = (writes != expected).then(|| { let mut out = String::from("writes differ\n"); @@ -534,6 +542,14 @@ mod tests { ); assert_eq!(result.not_sent, ["GET /api/vcs"]); + let queried = [Sent { query: Query::from([("resume".into(), vec!["true".into()])]), ..same[0].clone() }]; + assert!( + compare_requests(&recording, &queried, &[]) + .writes_differ + .unwrap() + .contains("+ rs | POST /api/a?resume=true") + ); + let other = [sent("POST", "/api/a", Some(serde_json::json!({ "n": 2 })))]; let missing = [sent("GET", "/api/nope", None)]; let result = compare_requests(&recording, &other, &missing); diff --git a/packages/cli-rs/parity/sim/compare.ts b/packages/cli-rs/parity/sim/compare.ts index 8c1ddd03ca1..a5e73c456a8 100644 --- a/packages/cli-rs/parity/sim/compare.ts +++ b/packages/cli-rs/parity/sim/compare.ts @@ -163,17 +163,24 @@ export type RequestComparison = { onlyB: string[] } -// Writes must match in order after the normalizers; reads one side sent and the other did not are listed only. +// Writes must match in order after the normalizers, with their sorted query and any status other than 2xx; reads +// one side sent and the other did not are listed only. export function compareRequests(a: Logged[], b: Logged[], root: string): RequestComparison { // A live server stamps real times, so epoch milliseconds are normalized too (plan 7.5, timestamps). // Bodies are compared as JSON values, so key order does not count. - const line = (entry: Logged) => - normalize( - `${entry.method} ${entry.path}${entry.body === undefined ? "" : " " + JSON.stringify(sorted(entry.body))}`, - { root }, - ) + const line = (entry: Logged) => { + const query = new URLSearchParams( + Object.keys(entry.query) + .toSorted() + .flatMap((key) => entry.query[key].map((value) => [key, value])), + ).toString() + const status = + entry.status === undefined || (entry.status >= 200 && entry.status < 300) ? "" : ` -> ${entry.status}` + const body = entry.body === undefined ? "" : " " + JSON.stringify(sorted(entry.body)) + return normalize(`${entry.method} ${entry.path}${query ? "?" + query : ""}${body}${status}`, { root }) .replace(minted, "<$1>") .replace(/\b1\d{12}\b/g, "") + } const writes = (log: Logged[]) => log.filter((entry) => !isRead(entry.method, entry.path)).map(line) const reads = (log: Logged[]) => [ ...new Set( diff --git a/packages/cli-rs/parity/sim/run.ts b/packages/cli-rs/parity/sim/run.ts index d31487844b8..d3417f29f8d 100644 --- a/packages/cli-rs/parity/sim/run.ts +++ b/packages/cli-rs/parity/sim/run.ts @@ -116,11 +116,13 @@ async function runScenario(name: string, scenario: SimScenario) { } } const requests = compareRequests(results[0].requests, results[1].requests, box.root) - const requestsPass = requests.equal || scenario.requests !== undefined + const requestsMissing = scenario.requests !== undefined && !deviationExists(scenario.requests.deviation) + const requestsPass = !requestsMissing && (requests.equal || scenario.requests !== undefined) ok &&= requestsPass lines.push( ` requests: ${requests.equal ? "writes equal" : requestsPass ? "writes differ, allowed" : "writes differ"}`, ) + if (requestsMissing) lines.push(` deviation file missing: ${JSON.stringify(scenario.requests?.deviation)}`) if (!requests.equal) { lines.push(...requests.writes.a.map((write) => ` - ${labels[0]} | ${write}`)) lines.push(...requests.writes.b.map((write) => ` + ${labels[1]} | ${write}`))