Skip to main content

mockforge_bench/conformance/
self_test.rs

1//! Positive + per-category negative request driver against a live server.
2//!
3//! Issue #79 round 13 (4) — Srikanth's (e) ask: a way to test both
4//! positive and negative compliance scenarios separately, where the
5//! positive cases should pass and the negative cases should be
6//! rejected.
7//!
8//! This module sits *alongside* the existing conformance executor
9//! (which drives k6 / native checks on a single positive call per
10//! operation). The self-test driver synthesises per-category
11//! deliberately-bad requests and asserts that the server actually
12//! rejects them with a 4xx — useful when verifying that
13//! `validate_request_with_all` is wired correctly for the user's spec
14//! (the exact gap that round-13 (3) fixed).
15//!
16//! Scope of the initial MVP: covers the highest-signal negatives —
17//! empty body when one is required, missing required query/header
18//! params, and wrong-type path params. Doesn't try to mutate every
19//! field of a JSON-Schema-validated body; that's a follow-up.
20
21use super::spec_driven::{AnnotatedOperation, ApiKeyLocation, SecuritySchemeInfo};
22use reqwest::{Client, Method};
23use std::collections::BTreeMap;
24use std::net::IpAddr;
25use std::sync::atomic::{AtomicUsize, Ordering};
26use std::sync::{Arc, Mutex};
27use std::time::Duration;
28
29/// Round 23 (c-iii) — per-direction body cap when capturing
30/// request/response payloads to `conformance-self-test-requests.jsonl`.
31/// 16 KiB keeps a 1000-case run under ~32 MB even if every payload
32/// fills the cap, while still preserving enough of a typical JSON body
33/// (or a stack-trace error response) to debug from.
34const CAPTURE_BODY_CAP_BYTES: usize = 16 * 1024;
35
36/// Round 17.2 — cap on schema-driven negatives per operation. A spec
37/// with 100 properties per body could produce hundreds of mutations
38/// for a single operation; combined with thousands of operations
39/// that's a runaway test matrix. 12 covers the highest-signal
40/// mutations (type mismatch + required-removed + a few constraint
41/// breaks) without exploding wall time on large specs.
42const SCHEMA_MUTATION_CAP: usize = 12;
43
44/// Round 25 (k) — content-type swap probes. For operations declaring a
45/// JSON request body, each entry below produces one probe that lies
46/// about Content-Type while keeping the JSON payload. A spec-compliant
47/// server should respond 415 (or 400). Order matches the order
48/// Srikanth listed in his round-23 reply: XML, YAML, multipart, and
49/// the URL-encoded variant he added in round 24.
50const CONTENT_TYPE_SWAP_VARIANTS: &[(&str, &str)] = &[
51    ("application/xml", "request-body:content-type-mismatch:xml"),
52    ("application/yaml", "request-body:content-type-mismatch:yaml"),
53    ("multipart/form-data", "request-body:content-type-mismatch:multipart"),
54    (
55        "application/x-www-form-urlencoded",
56        "request-body:content-type-mismatch:urlencoded",
57    ),
58];
59
60/// Round 27 (k variant b) — embedded content payloads. Content-Type
61/// stays `application/json` and the envelope IS valid JSON; we just
62/// stuff a non-JSON snippet into a string field's value. The test
63/// surfaces servers that try to parse string field contents (e.g.
64/// XML-EE expanders, YAML loaders, urlencoded parsers) and crash on
65/// the payload — a 5xx here is the finding. Label, payload pairs:
66const EMBEDDED_CONTENT_VARIANTS: &[(&str, &str)] = &[
67    ("request-body:embedded-content:xml", "<root><cmd>execute()</cmd></root>"),
68    ("request-body:embedded-content:yaml", "key: value\n- item1\n- item2"),
69    (
70        "request-body:embedded-content:multipart",
71        "--boundary\r\nContent-Disposition: form-data; name=\"x\"\r\n\r\nval\r\n--boundary--",
72    ),
73    ("request-body:embedded-content:urlencoded", "a=1&b=2&c=hello%20world"),
74];
75
76/// Configuration for a self-test run.
77#[derive(Debug, Clone)]
78pub struct SelfTestConfig {
79    pub target_url: String,
80    pub skip_tls_verify: bool,
81    pub timeout: Duration,
82    /// Optional extra headers to attach to every request (e.g. auth).
83    pub extra_headers: Vec<(String, String)>,
84    /// Delay between requests to avoid hammering the server.
85    pub delay_between_requests: Duration,
86    /// Round 18.1 — base path to prepend to every spec path. When the
87    /// spec declares `/users` and the deployed API is served under
88    /// `/api`, `--base-path /api` should make the self-test hit
89    /// `https://target/api/users` instead of `https://target/users`.
90    /// Pre-fix this was ignored entirely and every operation 404'd
91    /// (Srikanth's vCenter run on 0.3.152: 1275 positives, 1275 4xx).
92    pub base_path: Option<String>,
93    /// Round 18.5 — local source IPs to bind outgoing requests to.
94    /// Each IP must already be assigned to an interface on the host.
95    /// Operations round-robin through the resulting client pool.
96    pub source_ips: Vec<IpAddr>,
97    /// Round 18.5 — fake source IPs to advertise via forwarded-IP
98    /// headers (used to exercise GEODB lookup at the destination).
99    /// Rotated per operation.
100    pub geo_source_ips: Vec<IpAddr>,
101    /// Which forwarded-IP header(s) to populate when `geo_source_ips`
102    /// is non-empty. Empty → no-op; default below sets the standard
103    /// three-header set.
104    pub geo_source_headers: Vec<String>,
105    /// Round 23 (c-iii) — when `Some`, every probe captures method, URL,
106    /// request headers/body and response status/headers/body into this
107    /// sink. Caller drains it after `run_self_test` and writes
108    /// `conformance-self-test-requests.jsonl`. None → no capture (zero
109    /// extra allocations on the hot path).
110    pub capture: Option<Arc<Mutex<Vec<CaseCapture>>>>,
111    /// Round 25 — when true, validate every probe's response body
112    /// against the spec's response schema for the actual status
113    /// returned (closes round 21.3 / Srikanth's a2 / a3 ask). The
114    /// validation result lands in `CaseCapture::response_schema_error`
115    /// (None → matched, or no schema for that status). Default false:
116    /// JSON-Schema validation of large response bodies adds wall-clock
117    /// time and the user has to opt in.
118    pub validate_response_schemas: bool,
119    /// Round 33 (#823) — human-readable label for the OpenAPI spec
120    /// this run is exercising. Stamped on every `CaseCapture` so the
121    /// per-endpoint summary can attribute rows back to a spec in
122    /// multi-spec / multi-target benches. `None` when the bench didn't
123    /// track a spec path.
124    pub spec_label: Option<String>,
125    /// Round 47 (#79) — Srikanth on 0.3.191: "I did not see network
126    /// logs in the mockforge bench and conformance traffic if used
127    /// the [self-test] command". The r46 wire-level event sink only
128    /// existed on the native conformance executor; this matches it on
129    /// the self-test side. When `Some`, every `reqwest::Error` from
130    /// `send().await` is classified and pushed to this sink; caller
131    /// drains it into `conformance-network-events.json` next to the
132    /// JSONL capture. None → no extra allocations on the hot path.
133    pub network_events: Option<Arc<Mutex<Vec<NetworkEvent>>>>,
134    /// Round 49 (#79) — current iteration number (1-indexed). The
135    /// runner stamps it on every CaseCapture so the JSONL line and
136    /// violation rows carry the iteration counter. Defaults to 1
137    /// for non-looping runs.
138    pub current_iteration: u32,
139}
140
141/// Round 47 (#79) — wire-level network event captured by the self-test
142/// driver. Same shape as the native executor's `NetworkEvent` so
143/// downstream tooling can consume one file across executor variants.
144#[derive(Debug, Clone, serde::Serialize)]
145pub struct NetworkEvent {
146    pub timestamp: chrono::DateTime<chrono::Utc>,
147    pub check: String,
148    pub method: String,
149    pub url: String,
150    pub kind: String,
151    pub message: String,
152}
153
154/// Round 23 (c-iii) — one captured request/response pair, one per
155/// probe (positive or negative). Serialised as a JSON line in
156/// `conformance-self-test-requests.jsonl`. Headers are kept as
157/// `BTreeMap` for stable ordering. Bodies are truncated to
158/// `CAPTURE_BODY_CAP_BYTES`; `*_truncated` flags whether more was
159/// dropped.
160#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
161pub struct CaseCapture {
162    pub label: String,
163    pub method: String,
164    pub url: String,
165    pub request_headers: BTreeMap<String, String>,
166    pub request_body: Option<String>,
167    pub request_body_truncated: bool,
168    pub response_status: u16,
169    pub response_headers: BTreeMap<String, String>,
170    pub response_body: Option<String>,
171    pub response_body_truncated: bool,
172    pub error: Option<String>,
173    /// Round 25 — when `validate_response_schemas` is on and the spec
174    /// declares a schema for `response_status`, this carries the
175    /// validation message (or None when the body matched, or no schema
176    /// was declared for that status). Serialised verbatim in the JSONL
177    /// and rendered in the HTML viewer.
178    #[serde(default, skip_serializing_if = "Option::is_none")]
179    pub response_schema_error: Option<String>,
180    /// Round 28 — Srikanth's "Is it possible to put expected response
181    /// code status in both jsonl and jsonl report" ask. Human-readable
182    /// expected status range: `"2xx-3xx"` for positive probes,
183    /// `"4xx"` for negatives. Lets users `jq` for misses
184    /// (`.response_status as $s | .expected_status_range == "4xx"
185    /// and ($s < 400 or $s >= 500)`) and powers the HTML viewer's
186    /// "show mismatches only" filter.
187    #[serde(default)]
188    pub expected_status_range: String,
189    /// Round 33 (#823) — the spec's path template (e.g.
190    /// `/users/{id}`) before path-param substitution. Lets the
191    /// per-endpoint summary collapse `/users/X` and `/users/Y` into
192    /// one row. Empty string when the call site predates this field
193    /// (older `CaseCapture` payloads on disk also deserialise OK).
194    #[serde(default)]
195    pub path_template: String,
196    /// Round 33 (#823) — basename (or fallback to full path) of the
197    /// OpenAPI spec file this probe came from. Lets multi-spec runs
198    /// attribute rows back to the spec they came from. `None` when
199    /// the bench didn't track a spec path.
200    #[serde(default, skip_serializing_if = "Option::is_none")]
201    pub spec_label: Option<String>,
202    /// Round 36 (#876) — mockforge version that ran the probe.
203    /// Stamped from `CARGO_PKG_VERSION` at compile time. Also sent
204    /// as the `X-Mockforge-Client-Version` request header so a
205    /// matching `ServerConformanceViolation.client_mockforge_version`
206    /// can be cross-correlated. Empty string when the capture
207    /// pre-dates this field.
208    #[serde(default)]
209    pub mockforge_version: String,
210    /// Round 36 (#876) — wall-clock moment the bench driver sent the
211    /// request, as RFC3339 / ISO-8601. Also sent as the
212    /// `X-Mockforge-Client-Sent-At` request header so the server-side
213    /// `ServerConformanceViolation.client_sent_at` carries the same
214    /// value. Empty string when the capture pre-dates this field.
215    #[serde(default)]
216    pub client_sent_at: String,
217    /// Round 49 (#79) — Srikanth on 0.3.193: "Is it possible to
218    /// differentiate in the logs what is the iteration count that
219    /// way I will know how many requests are sent with that
220    /// violation." Stamped from the
221    /// `SelfTestConfig::current_iteration` field by the outer loop
222    /// in command.rs before each call to `run_self_test_with_deadline`.
223    /// 1-indexed; defaults to 1 for single-iteration runs so an older
224    /// JSONL that didn't carry the field deserialises as iteration 1.
225    #[serde(default = "default_iteration")]
226    pub iteration: u32,
227}
228
229fn default_iteration() -> u32 {
230    1
231}
232
233impl Default for SelfTestConfig {
234    fn default() -> Self {
235        Self {
236            target_url: "http://localhost:3000".into(),
237            skip_tls_verify: false,
238            timeout: Duration::from_secs(15),
239            extra_headers: Vec::new(),
240            delay_between_requests: Duration::from_millis(0),
241            base_path: None,
242            source_ips: Vec::new(),
243            geo_source_ips: Vec::new(),
244            geo_source_headers: default_geo_source_headers(),
245            capture: None,
246            validate_response_schemas: false,
247            spec_label: None,
248            network_events: None,
249            current_iteration: 1,
250        }
251    }
252}
253
254/// Truncate `body` to `CAPTURE_BODY_CAP_BYTES` on a UTF-8 boundary,
255/// returning the trimmed string and whether truncation occurred. Used
256/// for both request and response bodies in the capture sink.
257fn truncate_body_for_capture(body: &str) -> (String, bool) {
258    if body.len() <= CAPTURE_BODY_CAP_BYTES {
259        return (body.to_string(), false);
260    }
261    let mut end = CAPTURE_BODY_CAP_BYTES;
262    while end > 0 && !body.is_char_boundary(end) {
263        end -= 1;
264    }
265    (body[..end].to_string(), true)
266}
267
268/// Default forwarded-IP header set. Covers the three conventions a
269/// real GEODB front-end is likely to read in this order of
270/// preference: Cloudflare (`CF-Connecting-IP`), Akamai/CloudFront
271/// (`True-Client-IP`), then the de-facto standard
272/// `X-Forwarded-For`. Override via `--geo-source-header` to test a
273/// specific stack.
274pub fn default_geo_source_headers() -> Vec<String> {
275    vec![
276        "X-Forwarded-For".to_string(),
277        "True-Client-IP".to_string(),
278        "CF-Connecting-IP".to_string(),
279    ]
280}
281
282/// Outcome of a single test case (positive or negative).
283#[derive(Debug, Clone, serde::Serialize)]
284pub struct CaseOutcome {
285    pub label: String,
286    pub expected_4xx: bool,
287    pub actual_status: u16,
288    /// True when the response status matches expectation
289    /// (positive → 2xx-3xx, negative → 4xx).
290    pub passed: bool,
291}
292
293/// All cases run against one annotated operation.
294#[derive(Debug, Clone, serde::Serialize)]
295pub struct OperationResult {
296    pub method: String,
297    pub path: String,
298    pub positive: Option<CaseOutcome>,
299    pub negatives: Vec<CaseOutcome>,
300}
301
302/// Summary report rolled up across all operations.
303#[derive(Debug, Default, Clone, serde::Serialize)]
304pub struct SelfTestReport {
305    pub positive_pass: usize,
306    pub positive_fail: usize,
307    /// Per category: count of negative cases the server correctly
308    /// rejected with a 4xx (we caught the spec violation).
309    pub negative_caught: BTreeMap<String, usize>,
310    /// Per category: count of negative cases that should have been
311    /// rejected but came back with a non-4xx (validator gap).
312    pub negative_missed: BTreeMap<String, usize>,
313    pub operations: Vec<OperationResult>,
314}
315
316impl SelfTestReport {
317    /// All-pass means every positive case got 2xx-3xx and every
318    /// negative case got 4xx.
319    pub fn all_passed(&self) -> bool {
320        self.positive_fail == 0 && self.negative_missed.values().sum::<usize>() == 0
321    }
322
323    /// Round 18.1 — detect the "self-test target is misconfigured"
324    /// case where every positive failed with the *same* status code.
325    /// The classic example: `--base-path /api` was forgotten so every
326    /// request hits a path the server doesn't know and returns 404.
327    /// Pre-warning, the user saw all-green negative buckets (because
328    /// "missing route" 404s look like "validator rejected") and no
329    /// indication that the run was meaningless. Returns Some(status)
330    /// when ≥10 positives all failed with the same status, else None.
331    pub fn detect_target_misconfiguration(&self) -> Option<u16> {
332        if self.positive_pass > 0 || self.positive_fail < 10 {
333            return None;
334        }
335        let mut seen: Option<u16> = None;
336        for op in &self.operations {
337            let Some(p) = &op.positive else {
338                continue;
339            };
340            if p.passed {
341                return None;
342            }
343            match seen {
344                None => seen = Some(p.actual_status),
345                Some(s) if s != p.actual_status => return None,
346                _ => {}
347            }
348        }
349        seen
350    }
351
352    /// Round 47 (#79) — fold a second iteration of the self-test into
353    /// this report so multi-iteration runs aggregate counters across
354    /// passes. Per-category caught / missed counters sum; positive
355    /// counters sum; the `operations` vec records every probe outcome
356    /// so the iteration-N misconfiguration detector still works. Used
357    /// by command.rs's `--conformance-self-test-iterations` /
358    /// `--conformance-self-test-duration` loop.
359    pub fn merge_iteration(&mut self, other: SelfTestReport) {
360        self.positive_pass = self.positive_pass.saturating_add(other.positive_pass);
361        self.positive_fail = self.positive_fail.saturating_add(other.positive_fail);
362        for (k, v) in other.negative_caught {
363            let slot = self.negative_caught.entry(k).or_insert(0);
364            *slot = slot.saturating_add(v);
365        }
366        for (k, v) in other.negative_missed {
367            let slot = self.negative_missed.entry(k).or_insert(0);
368            *slot = slot.saturating_add(v);
369        }
370        self.operations.extend(other.operations);
371    }
372
373    /// Human-readable summary string. One line for positives, one per
374    /// category for negatives. Designed to slot into existing
375    /// `TerminalReporter` output.
376    pub fn render_summary(&self) -> String {
377        let mut out = String::new();
378        out.push_str(&format!(
379            "Positives: {} pass / {} fail\n",
380            self.positive_pass, self.positive_fail
381        ));
382        let mut keys: Vec<&String> =
383            self.negative_caught.keys().chain(self.negative_missed.keys()).collect();
384        keys.sort();
385        keys.dedup();
386        for cat in keys {
387            let caught = self.negative_caught.get(cat).copied().unwrap_or(0);
388            let missed = self.negative_missed.get(cat).copied().unwrap_or(0);
389            let mark = if missed == 0 { "✓" } else { "⚠" };
390            out.push_str(&format!(
391                "Negatives [{}]: {} caught / {} missed  {}\n",
392                cat, caught, missed, mark
393            ));
394        }
395        out
396    }
397}
398
399/// Execute the self-test plan against `config.target_url` for every
400/// `AnnotatedOperation`. Returns the aggregated report; callers
401/// decide how to display it (e.g. via `render_summary` or by writing
402/// the JSON serialisation to disk).
403pub async fn run_self_test(
404    operations: &[AnnotatedOperation],
405    config: &SelfTestConfig,
406) -> Result<SelfTestReport, reqwest::Error> {
407    run_self_test_with_deadline(operations, config, None).await
408}
409
410/// Round 49 (#79) — Srikanth on 0.3.193: `--conformance-self-test-
411/// duration 5m` ran 5:46 because the outer iteration loop in
412/// command.rs only checks the deadline AFTER a full matrix pass
413/// completes. For long iterations this can overshoot by minutes,
414/// which breaks automation that relies on a fixed wall-clock budget.
415/// New optional `deadline` parameter lets the runner break out
416/// mid-iteration once the deadline elapses; returns the partial
417/// report with whatever operations finished before the deadline.
418pub async fn run_self_test_with_deadline(
419    operations: &[AnnotatedOperation],
420    config: &SelfTestConfig,
421    deadline: Option<std::time::Instant>,
422) -> Result<SelfTestReport, reqwest::Error> {
423    // Round 18.5 — build a client pool when `source_ips` is set,
424    // one reqwest::Client per IP, each bound to its local address.
425    // Operations round-robin through the pool. Empty pool → single
426    // default client (the pre-18.5 behaviour).
427    let clients = build_client_pool(config)?;
428    let client_cursor = AtomicUsize::new(0);
429    let geo_cursor = AtomicUsize::new(0);
430
431    let mut report = SelfTestReport::default();
432    for op in operations {
433        // Round 49 — mid-iteration deadline check. Breaks out of the
434        // per-operation loop the moment the wall-clock budget
435        // elapses, so a 5m budget never overshoots by more than one
436        // probe's round-trip.
437        if let Some(d) = deadline {
438            if std::time::Instant::now() >= d {
439                break;
440            }
441        }
442        let client_idx = client_cursor.fetch_add(1, Ordering::Relaxed) % clients.len();
443        let client = &clients[client_idx];
444        let geo_ip = if config.geo_source_ips.is_empty() {
445            None
446        } else {
447            let idx = geo_cursor.fetch_add(1, Ordering::Relaxed) % config.geo_source_ips.len();
448            Some(config.geo_source_ips[idx])
449        };
450        let result = test_operation(client, config, op, geo_ip).await;
451        if let Some(p) = &result.positive {
452            if p.passed {
453                report.positive_pass += 1;
454            } else {
455                report.positive_fail += 1;
456            }
457        }
458        for neg in &result.negatives {
459            let cat = neg.label.split(':').next().unwrap_or("other").to_string();
460            if neg.passed {
461                *report.negative_caught.entry(cat).or_insert(0) += 1;
462            } else {
463                *report.negative_missed.entry(cat).or_insert(0) += 1;
464            }
465        }
466        report.operations.push(result);
467        if !config.delay_between_requests.is_zero() {
468            tokio::time::sleep(config.delay_between_requests).await;
469        }
470    }
471    Ok(report)
472}
473
474/// Round 18.5 — append GEODB forwarded-IP headers to the
475/// operation's declared headers. Returns the original vec untouched
476/// when `geo_ip` is None or `geo_headers` is empty.
477///
478/// If the operation already declares one of the geo headers (rare
479/// but legal), we keep the operation's value — the caller's spec
480/// wins.
481fn effective_op_headers(
482    base: &[(String, String)],
483    geo_ip: Option<IpAddr>,
484    geo_headers: &[String],
485) -> Vec<(String, String)> {
486    let mut out = base.to_vec();
487    let Some(ip) = geo_ip else {
488        return out;
489    };
490    let value = ip.to_string();
491    for h in geo_headers {
492        // Case-insensitive duplicate check: don't override the
493        // spec's own declared value for the header.
494        if out.iter().any(|(k, _)| k.eq_ignore_ascii_case(h)) {
495            continue;
496        }
497        out.push((h.clone(), value.clone()));
498    }
499    out
500}
501
502/// Round 18.5 — build a pool of reqwest clients, one per declared
503/// source IP. Empty `source_ips` → a single default client.
504///
505/// The OS must already have each `source_ip` assigned to an
506/// interface; reqwest's `.local_address()` issues a `bind()` syscall
507/// at connect time, so an IP the kernel doesn't recognise surfaces
508/// as `EADDRNOTAVAIL` at request time, not at builder time.
509fn build_client_pool(config: &SelfTestConfig) -> Result<Vec<Client>, reqwest::Error> {
510    let make = |bind: Option<IpAddr>| -> Result<Client, reqwest::Error> {
511        let mut builder = Client::builder().timeout(config.timeout);
512        if config.skip_tls_verify {
513            builder = builder.danger_accept_invalid_certs(true);
514        }
515        if let Some(addr) = bind {
516            builder = builder.local_address(addr);
517        }
518        builder.build()
519    };
520    if config.source_ips.is_empty() {
521        Ok(vec![make(None)?])
522    } else {
523        config.source_ips.iter().map(|ip| make(Some(*ip))).collect()
524    }
525}
526
527async fn test_operation(
528    client: &Client,
529    config: &SelfTestConfig,
530    op: &AnnotatedOperation,
531    geo_ip: Option<IpAddr>,
532) -> OperationResult {
533    // Round 25 — track the sink length BEFORE we run any probes for
534    // this operation, so that after the probes finish we can mutate
535    // exactly the entries that belong to this op (the capture sink is
536    // shared but `run_self_test` iterates operations sequentially).
537    // Used by the response-schema validation pass below.
538    let sink_start = config.capture.as_ref().and_then(|s| s.lock().ok().map(|g| g.len()));
539
540    let url = build_url_with_base(
541        &config.target_url,
542        config.base_path.as_deref(),
543        &op.path,
544        &op.path_params,
545    );
546    let method = Method::from_bytes(op.method.to_uppercase().as_bytes()).unwrap_or(Method::GET);
547
548    // Round 34 (#828) — stamp every `CaseCapture` with the spec
549    // template PREFIXED by `--base-path`, so the per-endpoint
550    // summary's `path` column matches what the user sees in URLs
551    // and logs. Srikanth searched for `/api/appliance/access/...`
552    // and didn't find it because round 33 stored just `/appliance/
553    // access/...`. Same normalization as `build_url_with_base`:
554    // leading `/` auto-added, trailing `/` stripped, empty
555    // base_path → no prefix at all.
556    let path_template = {
557        // Round 55 — shared with build_url_with_base so `--base-path /`
558        // resolves to no prefix instead of a `//path` double slash.
559        let prefix = base_path_prefix(config.base_path.as_deref());
560        let path = if op.path.starts_with('/') {
561            op.path.clone()
562        } else {
563            format!("/{}", op.path)
564        };
565        format!("{prefix}{path}")
566    };
567
568    // Round 18.5 — pre-compute the operation's effective headers
569    // with the geo source IP baked in. Doing it once here keeps the
570    // per-case `send_case` calls below unchanged. When `geo_ip` is
571    // None the result equals `op.header_params`.
572    let op_headers = effective_op_headers(&op.header_params, geo_ip, &config.geo_source_headers);
573
574    // ── Positive case ────────────────────────────────────────────
575    let positive = send_case(
576        client,
577        config,
578        method.clone(),
579        &url,
580        "positive",
581        ExpectedOutcome::Success,
582        op.sample_body.as_deref(),
583        op.query_params.clone(),
584        op_headers.clone(),
585        &path_template,
586    )
587    .await;
588
589    // ── Negative cases ───────────────────────────────────────────
590    let mut negatives = Vec::new();
591
592    // (a) empty body when one is required.
593    //
594    // Round 16 — drop the `sample_body.is_some()` precondition. Operations
595    // whose body annotator couldn't synthesize a sample previously got
596    // zero negatives (so the self-test reported "all passing" even on
597    // POST /resource with a required body). The spec saying the operation
598    // *has* a request body is enough — an empty object is a valid
599    // negative regardless of whether we have a positive sample.
600    if op.request_body_content_type.is_some() {
601        negatives.push(
602            send_case(
603                client,
604                config,
605                method.clone(),
606                &url,
607                "request-body:empty",
608                ExpectedOutcome::ClientError,
609                Some("{}"),
610                op.query_params.clone(),
611                op_headers.clone(),
612                &path_template,
613            )
614            .await,
615        );
616
617        // (b) wrong-shaped body (array instead of object) — exercises
618        // top-level type validation independently of which fields are
619        // required.
620        negatives.push(
621            send_case(
622                client,
623                config,
624                method.clone(),
625                &url,
626                "request-body:wrong-type",
627                ExpectedOutcome::ClientError,
628                Some("[]"),
629                op.query_params.clone(),
630                op_headers.clone(),
631                &path_template,
632            )
633            .await,
634        );
635
636        // Round 25 (k) — content-type swap probes.
637        //
638        // For operations declaring `application/json` request bodies, send
639        // the SAME json payload (or a synthesised one) under four other
640        // content types: `application/xml`, `application/yaml`,
641        // `multipart/form-data`, `application/x-www-form-urlencoded`.
642        // The spec says the endpoint accepts only JSON, so a strict server
643        // should respond 415 Unsupported Media Type (or 400 if it tries
644        // to parse and fails). A 2xx means the server is accepting
645        // payloads outside its declared content negotiation, which is the
646        // failure mode behind a lot of "we crashed on a malformed XML
647        // upload" incidents.
648        //
649        // Variant (a) of Srikanth's round-23 g ask: lie about the
650        // Content-Type header. The body shape is honest JSON; only the
651        // header is swapped. Variant (b) (JSON envelope with embedded
652        // non-JSON field values) is deferred to round 26 because it
653        // requires a schema-aware field walker.
654        if op
655            .request_body_content_type
656            .as_deref()
657            .map(|ct| ct.contains("json"))
658            .unwrap_or(false)
659        {
660            let payload = op.sample_body.as_deref().unwrap_or("{}");
661            for (ct, label) in CONTENT_TYPE_SWAP_VARIANTS {
662                negatives.push(
663                    send_case_with_extra(
664                        client,
665                        config,
666                        method.clone(),
667                        &url,
668                        label,
669                        ExpectedOutcome::ClientError,
670                        Some(payload),
671                        op.query_params.clone(),
672                        // Strip any Content-Type already on the operation
673                        // headers (the spec's positive value) so the
674                        // probe's value is the only one the server sees.
675                        op_headers
676                            .iter()
677                            .filter(|(k, _)| !k.eq_ignore_ascii_case("content-type"))
678                            .cloned()
679                            .collect(),
680                        // The wrong Content-Type rides on `extra_headers`
681                        // so it lands AFTER `send_case_with_extra`'s
682                        // unconditional `application/json` insertion in
683                        // request-body mode. Actually `send_case_with_extra`
684                        // only sets Content-Type when a body is present
685                        // AND there's no manual override; passing the
686                        // override here wins because reqwest preserves
687                        // the last-set header value.
688                        vec![("Content-Type".to_string(), (*ct).to_string())],
689                        &path_template,
690                    )
691                    .await,
692                );
693            }
694
695            // Round 27 (k variant b) — embedded non-JSON content
696            // inside a valid JSON envelope. Content-Type stays
697            // application/json (honest) and the body parses as JSON;
698            // only the string-valued payload changes. We expect 2xx-3xx
699            // because the envelope is spec-shape, so the probe surfaces
700            // servers that crash (5xx) trying to parse the embedded
701            // snippet as XML/YAML/etc. A 4xx is also a finding because
702            // it usually means the server's pattern/format validator
703            // tripped on the payload contents, but the user can decide
704            // from the JSONL whether that's a bug or correct narrow-
705            // string-field behaviour.
706            for (label, snippet) in EMBEDDED_CONTENT_VARIANTS {
707                let payload = op.sample_body.as_deref().unwrap_or("{}");
708                // Round 34 (#829) — skip the probe entirely when the
709                // positive sample has no string leaf we can mutate.
710                // The previous round-27 fallback `{"data": <snippet>}`
711                // produced a body that doesn't match the spec's actual
712                // schema for endpoints like vCenter's `consolecli` PUT
713                // (which wants `{enabled: bool}`), so the server
714                // correctly 400'd and the bench misreported the
715                // mismatch as an expectation failure.
716                let Some(body) = embed_payload_in_first_string_field(payload, snippet) else {
717                    continue;
718                };
719                negatives.push(
720                    send_case(
721                        client,
722                        config,
723                        method.clone(),
724                        &url,
725                        label,
726                        // expected_4xx=false: any non-2xx is a probe
727                        // failure. 5xx in particular is "server panicked
728                        // on the embedded content".
729                        ExpectedOutcome::NotServerError,
730                        Some(&body),
731                        op.query_params.clone(),
732                        op_headers.clone(),
733                        &path_template,
734                    )
735                    .await,
736                );
737            }
738        }
739
740        // Round 17.2 — schema-aware negatives.
741        //
742        // When both a positive sample AND the resolved body schema are
743        // available, mutate the sample per-field (type mismatch,
744        // min/max bounds, pattern, enum out-of-range, required-field
745        // removal) and assert each is rejected with 4xx. Capped at
746        // SCHEMA_MUTATION_CAP per operation so a 100-property body
747        // doesn't explode the test matrix.
748        if let (Some(sample_str), Some(schema)) =
749            (op.sample_body.as_deref(), op.request_body_schema.as_ref())
750        {
751            if let Ok(sample) = serde_json::from_str::<serde_json::Value>(sample_str) {
752                let mutations = super::schema_mutator::mutate_body(&sample, schema);
753                for m in mutations.into_iter().take(SCHEMA_MUTATION_CAP) {
754                    let body_str = serde_json::to_string(&m.body).unwrap_or_default();
755                    negatives.push(
756                        send_case(
757                            client,
758                            config,
759                            method.clone(),
760                            &url,
761                            &m.label,
762                            ExpectedOutcome::ClientError,
763                            Some(&body_str),
764                            op.query_params.clone(),
765                            // Round 24 (f) — was `op.header_params`, which
766                            // skipped the geo-IP header. Use `op_headers`
767                            // so the geo IP rides with the negative probe
768                            // too (positive vs negative coverage must be
769                            // symmetric, otherwise a GEODB front-end sees
770                            // the rotating IP only on positives).
771                            op_headers.clone(),
772                            &path_template,
773                        )
774                        .await,
775                    );
776                }
777            }
778        }
779    }
780
781    // Round 17.2 — URI-length probe. Spec-agnostic but schema-aware in
782    // spirit: most servers cap URIs at 8 KB or so. Append a 9 KB query
783    // string to the URL and expect 414 URI Too Long (or 400). Skipped
784    // for operations that already have a heavy positive query.
785    {
786        let pad = "p=".to_string() + &"x".repeat(9_000);
787        let bad_url = if url.contains('?') {
788            format!("{url}&{pad}")
789        } else {
790            format!("{url}?{pad}")
791        };
792        negatives.push(
793            send_case(
794                client,
795                config,
796                method.clone(),
797                &bad_url,
798                "parameters:uri-too-long",
799                ExpectedOutcome::ClientError,
800                op.sample_body.as_deref(),
801                op.query_params.clone(),
802                // Round 24 (f) — see schema-mutation note above. Use
803                // `op_headers` (carries geo IP) instead of bare
804                // `op.header_params`.
805                op_headers.clone(),
806                &path_template,
807            )
808            .await,
809        );
810    }
811
812    // (e) Round 16 — path-param type probe. Send the first path
813    // parameter as a literal `"self-test-invalid-id"`: a string that
814    // contains hyphens, won't parse as an integer, won't parse as a
815    // UUID, and won't match any typical regex pattern. Operations
816    // whose spec types the param as `integer` or `string` with a
817    // `format`/`pattern` will catch this (caught: server returned
818    // 4xx); operations whose spec lets path params be free-form
819    // strings will let it through (missed: server returned 2xx).
820    // Either outcome is informative: a category that's all "missed"
821    // tells the user their spec is loose on path-param types, which
822    // is itself worth knowing. Addresses Srikanth's "always all
823    // passing" report — operations with a path param now produce at
824    // least one probe instead of zero.
825    if !op.path_params.is_empty() {
826        let mut url_with_placeholder = op.path.clone();
827        if let Some((first_name, _)) = op.path_params.first() {
828            // Substitute every other path-param with its sample so the
829            // route shape stays intact and only the first param is bad.
830            for (name, value) in op.path_params.iter().skip(1) {
831                if !value.is_empty() {
832                    url_with_placeholder =
833                        url_with_placeholder.replace(&format!("{{{name}}}"), value);
834                }
835            }
836            // Substitute the first param with a guaranteed-invalid
837            // sentinel that's unlikely to match any reasonable schema:
838            // contains characters disallowed in numeric IDs *and* UUIDs.
839            url_with_placeholder =
840                url_with_placeholder.replace(&format!("{{{first_name}}}"), "self-test-invalid-id");
841            // Round 18.1 — honour `base_path` here too, otherwise the
842            // probe URL differs from the positive case and the
843            // resulting 404 is misattributed to "bad path param".
844            let bad_url = build_url_with_base(
845                &config.target_url,
846                config.base_path.as_deref(),
847                &url_with_placeholder,
848                &[],
849            );
850            negatives.push(
851                send_case(
852                    client,
853                    config,
854                    method.clone(),
855                    &bad_url,
856                    "parameters:bad-path-param",
857                    ExpectedOutcome::ClientError,
858                    op.sample_body.as_deref(),
859                    op.query_params.clone(),
860                    op_headers.clone(),
861                    &path_template,
862                )
863                .await,
864            );
865        }
866    }
867
868    // (c) drop the first required query param
869    if !op.query_params.is_empty() {
870        let mut q = op.query_params.clone();
871        q.remove(0);
872        negatives.push(
873            send_case(
874                client,
875                config,
876                method.clone(),
877                &url,
878                "parameters:missing-query",
879                ExpectedOutcome::ClientError,
880                op.sample_body.as_deref(),
881                q,
882                op_headers.clone(),
883                &path_template,
884            )
885            .await,
886        );
887    }
888
889    // (s) Round 17.3 — security probes.
890    //
891    // Operations whose spec declares a security requirement get a
892    // dedicated set of negatives. The point isn't to test whether the
893    // server's *real* auth works (the positive case already does that
894    // via `extra_headers`) — it's to check whether deliberately-bad
895    // credentials are still rejected, which is exactly the failure
896    // mode that lets an attacker through a half-wired validator.
897    //
898    // Each probe replaces or omits the relevant auth credential and
899    // expects 401 / 403. A 2xx here is a hard finding: "spec says
900    // this endpoint is protected, server let unauthenticated /
901    // wrong-credential traffic through".
902    //
903    // Bounded: at most one probe per declared scheme kind, so an
904    // operation with 3 security requirements doesn't 4× the request
905    // volume. Skips entirely when `op.security_schemes` is empty.
906    for probe in build_security_probes(&op.security_schemes) {
907        // Strip any pre-existing Authorization or known API-key
908        // header from extra_headers + header_params so the probe
909        // value is the *only* credential the server sees.
910        let stripped_extra = strip_auth(&config.extra_headers, &op.security_schemes);
911        let stripped_headers = strip_auth(&op.header_params, &op.security_schemes);
912        let stripped_query = strip_auth_query(&op.query_params, &op.security_schemes);
913        let mut req_headers = stripped_headers;
914        for (k, v) in &probe.headers {
915            req_headers.push((k.clone(), v.clone()));
916        }
917        // Round 24 (f) — security probes build req_headers from
918        // `op.header_params` directly (we need the stripped-auth
919        // variant), so the geo-IP header doesn't ride along
920        // automatically. Append it here so a GEODB / WAF in front
921        // of the auth layer still sees the rotating source IP.
922        if let Some(ip) = geo_ip {
923            let ip_str = ip.to_string();
924            for h in &config.geo_source_headers {
925                let already = req_headers.iter().any(|(k, _)| k.eq_ignore_ascii_case(h));
926                if !already {
927                    req_headers.push((h.clone(), ip_str.clone()));
928                }
929            }
930        }
931        let mut req_query = stripped_query;
932        for (k, v) in &probe.query {
933            req_query.push((k.clone(), v.clone()));
934        }
935        negatives.push(
936            send_case_with_extra(
937                client,
938                config,
939                method.clone(),
940                &url,
941                &probe.label,
942                ExpectedOutcome::ClientError,
943                op.sample_body.as_deref(),
944                req_query,
945                req_headers,
946                stripped_extra,
947                &path_template,
948            )
949            .await,
950        );
951    }
952
953    // (d) drop the first required header
954    if !op.header_params.is_empty() {
955        // Round 24 (f) — start from `op_headers` (so the geo IP rides
956        // along) and only strip the first OPERATION-declared header.
957        // Slicing past `op.header_params.len()` would otherwise risk
958        // dropping the geo header itself; `op_headers` is built as
959        // `op.header_params ++ geo` so index 0 is always operational.
960        let mut h = op_headers.clone();
961        if !h.is_empty() {
962            h.remove(0);
963        }
964        negatives.push(
965            send_case(
966                client,
967                config,
968                method.clone(),
969                &url,
970                "parameters:missing-header",
971                ExpectedOutcome::ClientError,
972                op.sample_body.as_deref(),
973                op.query_params.clone(),
974                h,
975                &path_template,
976            )
977            .await,
978        );
979    }
980
981    // (w) Round 17.5 — OWASP/WAF unification.
982    //
983    // Pull one canonical payload per OWASP category from the existing
984    // `SecurityPayloads` library and emit an injection probe per
985    // category. Targets in priority order: (1) substitute the first
986    // query param's value, (2) substitute the first string field of
987    // the positive JSON body, (3) skip if neither is available.
988    //
989    // Label format `owasp:<category>`, so the existing
990    // `negative_caught` / `negative_missed` rollup groups all OWASP
991    // findings under one `owasp` bucket. Expected 4xx (server should
992    // reject malicious input). A 5xx is a hard finding (server
993    // crashed on the payload); a 2xx is a soft finding (input passed
994    // through unfiltered — may or may not be a real vuln).
995    //
996    // Bounded: at most one probe per category (7 categories total).
997    // Skips the operation entirely if no injection target is
998    // available — open GET endpoints with no params get zero OWASP
999    // probes, no false signal.
1000    for probe in build_owasp_probes(op) {
1001        negatives.push(
1002            send_case(
1003                client,
1004                config,
1005                method.clone(),
1006                &url,
1007                &probe.label,
1008                ExpectedOutcome::ClientError,
1009                probe.body.as_deref(),
1010                probe.query,
1011                // Round 24 (f) — OWASP injection probes must also
1012                // carry the geo IP, otherwise a WAF / GEODB rule
1013                // tuned to a specific source IP would silently let
1014                // them through.
1015                op_headers.clone(),
1016                &path_template,
1017            )
1018            .await,
1019        );
1020    }
1021
1022    // Round 25 — response-body shape validation pass. For each capture
1023    // this op pushed onto the sink, look up the spec's schema for the
1024    // actual response status and validate. Result lands in
1025    // `response_schema_error` (Some(message) on failure, None on
1026    // pass or no-schema-for-this-status). Runs only when the user
1027    // opted in AND capture is on (we need the body).
1028    if config.validate_response_schemas {
1029        if let (Some(sink), Some(start)) = (config.capture.as_ref(), sink_start) {
1030            if !op.response_schemas.is_empty() {
1031                if let Ok(mut guard) = sink.lock() {
1032                    let end = guard.len();
1033                    for i in start..end {
1034                        let Some(entry) = guard.get_mut(i) else {
1035                            continue;
1036                        };
1037                        let Some(body) = entry.response_body.as_deref() else {
1038                            continue;
1039                        };
1040                        let Some(schema) = op.response_schemas.get(&entry.response_status) else {
1041                            continue;
1042                        };
1043                        entry.response_schema_error = validate_body_against_schema(body, schema);
1044                    }
1045                }
1046            }
1047        }
1048    }
1049
1050    OperationResult {
1051        method: op.method.clone(),
1052        path: op.path.clone(),
1053        positive: Some(positive),
1054        negatives,
1055    }
1056}
1057
1058/// Round 25 — validate a JSON body string against an OpenAPI response
1059/// schema (already converted to a `serde_json::Value`). Returns
1060/// `Some(message)` describing the first violation, or `None` on a
1061/// clean pass / non-JSON body / schema-build failure (in which case
1062/// the absence of an error means "we didn't have anything to compare
1063/// against", not "passed"; the caller-side semantics treat absence as
1064/// success because that's what the user sees as silence).
1065/// Round 27 (k variant b) — return a JSON body string identical to
1066/// `sample` except that the first string-valued leaf has been
1067/// replaced with `snippet`. Walks objects depth-first and stops at
1068/// the first string. Returns `None` when `sample` is not parseable
1069/// JSON or has no string field anywhere; the caller skips emitting
1070/// a probe in that case (Round 34 #829: Srikanth on 0.3.178 found
1071/// that the previous `{"data": <snippet>}` fallback envelope didn't
1072/// match real-API schemas like vCenter's `{enabled: bool}` and the
1073/// server correctly 400'd, which the bench then misreported as a
1074/// `2xx-3xx` expectation miss).
1075fn embed_payload_in_first_string_field(sample: &str, snippet: &str) -> Option<String> {
1076    let mut parsed: serde_json::Value = serde_json::from_str(sample).ok()?;
1077    if !replace_first_string(&mut parsed, snippet) {
1078        return None;
1079    }
1080    serde_json::to_string(&parsed).ok()
1081}
1082
1083/// Helper for `embed_payload_in_first_string_field`: recursively
1084/// walk the value and replace the FIRST string leaf encountered.
1085/// Returns true when a replacement happened. Honors document order
1086/// for objects (BTreeMap-backed `serde_json::Map` iterates in
1087/// insertion order) so the choice of which field to mutate is
1088/// stable across runs.
1089fn replace_first_string(v: &mut serde_json::Value, snippet: &str) -> bool {
1090    match v {
1091        serde_json::Value::String(s) => {
1092            *s = snippet.to_string();
1093            true
1094        }
1095        serde_json::Value::Object(map) => {
1096            for (_k, child) in map.iter_mut() {
1097                if replace_first_string(child, snippet) {
1098                    return true;
1099                }
1100            }
1101            false
1102        }
1103        serde_json::Value::Array(arr) => {
1104            for child in arr.iter_mut() {
1105                if replace_first_string(child, snippet) {
1106                    return true;
1107                }
1108            }
1109            false
1110        }
1111        _ => false,
1112    }
1113}
1114
1115fn validate_body_against_schema(body: &str, schema: &serde_json::Value) -> Option<String> {
1116    let parsed: serde_json::Value = serde_json::from_str(body).ok()?;
1117    let validator = jsonschema::validator_for(schema).ok()?;
1118    let mut errors = validator.iter_errors(&parsed);
1119    let first = errors.next()?;
1120    // Round 28 — Srikanth on 0.3.170 wanted the message to show the
1121    // actual expected schema alongside the kind label so it reads as
1122    // "expected schema {...} but got <kind>". We emit a compact JSON
1123    // serialisation of the schema as a suffix; the kind label still
1124    // names what went wrong in plain English for quick scanning.
1125    // Round 26 — Srikanth on 0.3.169: the prior `format!("{:?}", first.kind)
1126    // .split('(').next()` produced "Type { kind: Single" (broken Rust
1127    // syntax, mismatched braces). Switch to the human-readable mapping
1128    // already used in executor.rs: handle the common kinds (Type,
1129    // Required, AdditionalProperties, Enum, MinLength, MaxLength,
1130    // Minimum, Maximum, Pattern) explicitly; fall back to the
1131    // jsonschema crate's Display impl on the error (which produces
1132    // something like "{...} is not of type \"string\"") for the long
1133    // tail. Combined with `at <instance-path>` for the field location.
1134    let path = first.instance_path.to_string();
1135    let path = if path.is_empty() { "/" } else { path.as_str() };
1136    // Round 31 — Srikanth on 0.3.174 hit the vCenter case where the
1137    // error is "required field missing: comment" but the printed
1138    // schema was the WHOLE parent object schema (with descriptions of
1139    // every property), not just the missing field's sub-schema. The
1140    // jsonschema crate emits `Required` errors with
1141    // `instance_path == /` (the parent), so the round-30 sub-schema
1142    // walker had no extra info to focus the suffix. Carry the missing
1143    // property name out of the kind match so we can descend one more
1144    // step into `properties[property]` for the printed schema.
1145    let mut required_property: Option<String> = None;
1146    let kind_msg: String = match &first.kind {
1147        jsonschema::error::ValidationErrorKind::Type { kind } => {
1148            // `kind` is `TypeKind::Single(JsonType)` or
1149            // `TypeKind::Multiple(JsonTypeSet)`. `JsonType` has its
1150            // own `Display` impl ("string", "object", etc.).
1151            match kind {
1152                jsonschema::error::TypeKind::Single(t) => format!("expected type {t}"),
1153                jsonschema::error::TypeKind::Multiple(_) => "expected one of multiple types".into(),
1154            }
1155        }
1156        jsonschema::error::ValidationErrorKind::Required { property } => {
1157            // `property.to_string()` returns the Display of the JSON
1158            // value, which for a string is `"name"` (with quotes).
1159            // Strip them for the lookup; keep them in the human message.
1160            let raw = property.to_string();
1161            let unquoted = raw
1162                .strip_prefix('"')
1163                .and_then(|s| s.strip_suffix('"'))
1164                .unwrap_or(&raw)
1165                .to_string();
1166            required_property = Some(unquoted);
1167            format!("required field missing: {property}")
1168        }
1169        jsonschema::error::ValidationErrorKind::AdditionalProperties { unexpected } => {
1170            format!("unexpected additional properties: {unexpected:?}")
1171        }
1172        jsonschema::error::ValidationErrorKind::Enum { options } => {
1173            format!("value not in allowed enum: {options}")
1174        }
1175        jsonschema::error::ValidationErrorKind::MinLength { limit } => {
1176            format!("string shorter than min length ({limit})")
1177        }
1178        jsonschema::error::ValidationErrorKind::MaxLength { limit } => {
1179            format!("string longer than max length ({limit})")
1180        }
1181        jsonschema::error::ValidationErrorKind::Minimum { limit } => {
1182            format!("value below minimum ({limit})")
1183        }
1184        jsonschema::error::ValidationErrorKind::Maximum { limit } => {
1185            format!("value above maximum ({limit})")
1186        }
1187        jsonschema::error::ValidationErrorKind::Pattern { pattern } => {
1188            format!("value did not match pattern {pattern}")
1189        }
1190        // Long tail: lean on jsonschema's Display impl, which is the
1191        // built-in human-readable error message ("X is not of type Y").
1192        // Strip trailing newlines so the JSONL line stays one line.
1193        _ => first.to_string().trim().to_string(),
1194    };
1195    // Round 30 — Srikanth on 0.3.173 asked how a deeper nested mismatch
1196    // reads. The prior output printed the WHOLE top-level schema even for
1197    // a single-field mismatch, which buried the actual constraint that
1198    // failed. Walk the instance pointer through the schema's properties
1199    // chain and print the most specific sub-schema we can find. Falls
1200    // back to the full schema for paths the walker can't resolve
1201    // (additionalProperties, oneOf, allOf, $ref un-resolved, etc.).
1202    let mut focused_schema = sub_schema_at_pointer(schema, path).unwrap_or_else(|| schema.clone());
1203    // Round 31 — for Required errors, descend one more step into
1204    // `properties[<missing>]` so the printed schema is the missing
1205    // field's own constraint, not the whole parent.
1206    if let Some(prop_name) = required_property.as_ref() {
1207        if let Some(prop_schema) =
1208            focused_schema.get("properties").and_then(|p| p.get(prop_name.as_str()))
1209        {
1210            focused_schema = prop_schema.clone();
1211        }
1212    }
1213    // Round 34 (#827) — Srikanth on 0.3.178 hit the vCenter
1214    // `enabled: boolean` case where the schema's multi-paragraph
1215    // `description` (and other prose fields) ate the 300-char budget
1216    // before the actually-useful `type` keyword could appear. Strip
1217    // the noise-fields recursively before serializing so the type
1218    // signal survives truncation; constraint keywords (`type`,
1219    // `properties`, `required`, `format`, `items`, etc.) stay.
1220    let focused_schema = strip_schema_noise(&focused_schema);
1221    let schema_str = serde_json::to_string(&focused_schema).unwrap_or_else(|_| "<schema>".into());
1222    let schema_str = if schema_str.len() > 300 {
1223        format!("{}...", &schema_str[..300])
1224    } else {
1225        schema_str
1226    };
1227    // Round 29 — Srikanth on 0.3.172 was confused by `at /:` thinking
1228    // it referenced the URL path; it's actually a JSON pointer into
1229    // the RESPONSE BODY. Reword so that's unambiguous: explicit
1230    // "response body" prefix and a human label for the root case.
1231    let location = if path == "/" {
1232        "response body root".to_string()
1233    } else {
1234        format!("response body at {path}")
1235    };
1236    Some(format!("{location}: {kind_msg}; expected schema {schema_str}"))
1237}
1238
1239/// Round 34 (#827) — drop the human-readable / documentation-only
1240/// fields from a JSON Schema before printing it inside a
1241/// `response_schema_error` message. The validator only cares about
1242/// constraint keywords (`type`, `required`, `properties`, `items`,
1243/// `format`, `enum`, `min*`/`max*`, `pattern`, `oneOf`/`anyOf`/
1244/// `allOf`/`not`); the prose fields can be paragraphs long for real-
1245/// world specs (vCenter's `enabled: bool` field has a multi-paragraph
1246/// description) and were eating the 300-char truncation budget before
1247/// the actually-useful type info could appear. Stripped fields:
1248/// `description`, `example`, `examples`, `summary`, `title`,
1249/// `externalDocs`, `xml`, `discriminator.description`.
1250fn strip_schema_noise(schema: &serde_json::Value) -> serde_json::Value {
1251    const NOISE_KEYS: &[&str] = &[
1252        "description",
1253        "example",
1254        "examples",
1255        "summary",
1256        "title",
1257        "externalDocs",
1258        "xml",
1259    ];
1260    match schema {
1261        serde_json::Value::Object(map) => {
1262            let mut out = serde_json::Map::with_capacity(map.len());
1263            for (k, v) in map {
1264                if NOISE_KEYS.contains(&k.as_str()) {
1265                    continue;
1266                }
1267                out.insert(k.clone(), strip_schema_noise(v));
1268            }
1269            serde_json::Value::Object(out)
1270        }
1271        serde_json::Value::Array(items) => {
1272            serde_json::Value::Array(items.iter().map(strip_schema_noise).collect())
1273        }
1274        other => other.clone(),
1275    }
1276}
1277
1278/// Round 30 — walk a JSON-Pointer-style instance path through a JSON
1279/// Schema and return the sub-schema describing the value at that
1280/// position. For path `/name/age` on
1281/// `{"properties":{"name":{"properties":{"age":{"type":"integer"}}}}}`
1282/// returns `{"type":"integer"}`. Returns `None` for paths the walker
1283/// can't follow (array indices into `items` with no per-index schema,
1284/// `additionalProperties`, `oneOf`/`allOf`, unresolved `$ref`); callers
1285/// should fall back to the full schema in that case.
1286fn sub_schema_at_pointer(schema: &serde_json::Value, pointer: &str) -> Option<serde_json::Value> {
1287    if pointer.is_empty() || pointer == "/" {
1288        return Some(schema.clone());
1289    }
1290    let mut current = schema;
1291    for seg in pointer.trim_start_matches('/').split('/') {
1292        let unescaped = seg.replace("~1", "/").replace("~0", "~");
1293        if let Some(props) = current.get("properties") {
1294            if let Some(sub) = props.get(&unescaped) {
1295                current = sub;
1296                continue;
1297            }
1298        }
1299        if let Some(items) = current.get("items") {
1300            if items.is_object() {
1301                current = items;
1302                continue;
1303            }
1304        }
1305        return None;
1306    }
1307    Some(current.clone())
1308}
1309
1310/// Round 17.5 — one OWASP injection probe to send.
1311#[derive(Debug, Clone)]
1312struct OwaspProbe {
1313    label: String,
1314    body: Option<String>,
1315    query: Vec<(String, String)>,
1316}
1317
1318/// Build one OWASP probe per `SecurityCategory` for `op`. Targets the
1319/// first query param if any, else the first string field of the
1320/// positive JSON body. Returns empty if neither target is available.
1321fn build_owasp_probes(op: &AnnotatedOperation) -> Vec<OwaspProbe> {
1322    use crate::security_payloads::{SecurityCategory, SecurityPayloads};
1323
1324    let categories = [
1325        SecurityCategory::SqlInjection,
1326        SecurityCategory::Xss,
1327        SecurityCategory::CommandInjection,
1328        SecurityCategory::PathTraversal,
1329        SecurityCategory::Ssti,
1330        SecurityCategory::LdapInjection,
1331        SecurityCategory::Xxe,
1332    ];
1333
1334    // Pick an injection target ONCE per operation; reuse it across
1335    // categories. (A single op gets up to 7 probes — one per category
1336    // — all attacking the same field.)
1337    let injection_target = pick_injection_target(op);
1338    let Some(target) = injection_target else {
1339        return Vec::new();
1340    };
1341
1342    let mut probes = Vec::new();
1343    for cat in categories {
1344        // Take the *first* payload from each category. The
1345        // collection's first entry is the canonical low-risk
1346        // representative; later entries include time-based / blind
1347        // probes that aren't useful as a one-shot rejection test.
1348        let Some(payload) = SecurityPayloads::get_by_category(cat).into_iter().next() else {
1349            continue;
1350        };
1351        let mut query = op.query_params.clone();
1352        let mut body = op.sample_body.clone();
1353        match &target {
1354            InjectionTarget::Query(idx) => {
1355                if let Some(slot) = query.get_mut(*idx) {
1356                    slot.1 = payload.payload.clone();
1357                }
1358            }
1359            InjectionTarget::BodyStringField(field) => {
1360                body = inject_into_body_field(body.as_deref(), field, &payload.payload);
1361            }
1362        }
1363        probes.push(OwaspProbe {
1364            label: format!("owasp:{}", cat),
1365            body,
1366            query,
1367        });
1368    }
1369    probes
1370}
1371
1372#[derive(Debug, Clone)]
1373enum InjectionTarget {
1374    Query(usize),
1375    BodyStringField(String),
1376}
1377
1378fn pick_injection_target(op: &AnnotatedOperation) -> Option<InjectionTarget> {
1379    if !op.query_params.is_empty() {
1380        return Some(InjectionTarget::Query(0));
1381    }
1382    let sample = op.sample_body.as_deref()?;
1383    let parsed: serde_json::Value = serde_json::from_str(sample).ok()?;
1384    let obj = parsed.as_object()?;
1385    for (k, v) in obj {
1386        if v.is_string() {
1387            return Some(InjectionTarget::BodyStringField(k.clone()));
1388        }
1389    }
1390    None
1391}
1392
1393/// Replace the value of `field` in a JSON-object body with `payload`.
1394/// Returns the mutated body as a JSON string. Returns `None` if the
1395/// body doesn't parse as a JSON object.
1396fn inject_into_body_field(body: Option<&str>, field: &str, payload: &str) -> Option<String> {
1397    let raw = body?;
1398    let mut parsed: serde_json::Value = serde_json::from_str(raw).ok()?;
1399    let obj = parsed.as_object_mut()?;
1400    obj.insert(field.to_string(), serde_json::json!(payload));
1401    serde_json::to_string(&parsed).ok()
1402}
1403
1404#[allow(clippy::too_many_arguments)]
1405/// Round 17.3 — one synthesised bad credential to send.
1406#[derive(Debug, Clone)]
1407struct SecurityProbe {
1408    /// Self-test label, e.g. `security:bad-bearer`.
1409    label: String,
1410    /// Headers to attach to the probe request.
1411    headers: Vec<(String, String)>,
1412    /// Query parameters to attach (API key in query case).
1413    query: Vec<(String, String)>,
1414}
1415
1416/// For each declared security scheme, produce one bad-credential
1417/// probe plus a single "no auth at all" probe that exercises the
1418/// missing-credential code path. Deduplicates by scheme kind so an
1419/// operation declaring `[bearer, bearer]` only yields one Bearer
1420/// probe.
1421fn build_security_probes(schemes: &[SecuritySchemeInfo]) -> Vec<SecurityProbe> {
1422    if schemes.is_empty() {
1423        return Vec::new();
1424    }
1425    let mut probes: Vec<SecurityProbe> = Vec::new();
1426    let mut seen_bearer = false;
1427    let mut seen_basic = false;
1428    // `(loc_tag, name)` — ApiKeyLocation doesn't implement Ord, so
1429    // we tag it with a short discriminant string for dedup.
1430    let mut seen_apikey: std::collections::BTreeSet<(&'static str, String)> = Default::default();
1431    for s in schemes {
1432        match s {
1433            SecuritySchemeInfo::Bearer if !seen_bearer => {
1434                seen_bearer = true;
1435                probes.push(SecurityProbe {
1436                    label: "security:bad-bearer".into(),
1437                    headers: vec![(
1438                        "Authorization".into(),
1439                        "Bearer self-test-invalid-token".into(),
1440                    )],
1441                    query: Vec::new(),
1442                });
1443            }
1444            SecuritySchemeInfo::Basic if !seen_basic => {
1445                seen_basic = true;
1446                // base64("self-test:invalid") — valid base64, wrong creds.
1447                probes.push(SecurityProbe {
1448                    label: "security:bad-basic".into(),
1449                    headers: vec![(
1450                        "Authorization".into(),
1451                        "Basic c2VsZi10ZXN0OmludmFsaWQ=".into(),
1452                    )],
1453                    query: Vec::new(),
1454                });
1455            }
1456            SecuritySchemeInfo::ApiKey { location, name } => {
1457                let loc_tag = match location {
1458                    ApiKeyLocation::Header => "header",
1459                    ApiKeyLocation::Query => "query",
1460                    ApiKeyLocation::Cookie => "cookie",
1461                };
1462                if seen_apikey.contains(&(loc_tag, name.clone())) {
1463                    continue;
1464                }
1465                seen_apikey.insert((loc_tag, name.clone()));
1466                let label = format!("security:bad-apikey:{}", name);
1467                let bad = "self-test-invalid-key".to_string();
1468                match location {
1469                    ApiKeyLocation::Header => probes.push(SecurityProbe {
1470                        label,
1471                        headers: vec![(name.clone(), bad)],
1472                        query: Vec::new(),
1473                    }),
1474                    ApiKeyLocation::Query => probes.push(SecurityProbe {
1475                        label,
1476                        headers: Vec::new(),
1477                        query: vec![(name.clone(), bad)],
1478                    }),
1479                    ApiKeyLocation::Cookie => probes.push(SecurityProbe {
1480                        label,
1481                        headers: vec![("Cookie".into(), format!("{}={}", name, bad))],
1482                        query: Vec::new(),
1483                    }),
1484                }
1485            }
1486            _ => {}
1487        }
1488    }
1489    // Always add a "no auth at all" probe when *any* security scheme
1490    // is declared — useful even if all schemes failed to resolve to a
1491    // testable kind, because it surfaces validators that aren't
1492    // checking auth presence at all.
1493    probes.push(SecurityProbe {
1494        label: "security:no-auth".into(),
1495        headers: Vec::new(),
1496        query: Vec::new(),
1497    });
1498    probes
1499}
1500
1501/// Remove Authorization and any API-key headers declared by the
1502/// operation's security schemes from `headers`, so a security probe
1503/// can supply its own credential (or none) cleanly.
1504fn strip_auth(
1505    headers: &[(String, String)],
1506    schemes: &[SecuritySchemeInfo],
1507) -> Vec<(String, String)> {
1508    let mut apikey_headers: std::collections::BTreeSet<String> = Default::default();
1509    for s in schemes {
1510        if let SecuritySchemeInfo::ApiKey {
1511            location: ApiKeyLocation::Header,
1512            name,
1513        } = s
1514        {
1515            apikey_headers.insert(name.to_lowercase());
1516        }
1517        if let SecuritySchemeInfo::ApiKey {
1518            location: ApiKeyLocation::Cookie,
1519            ..
1520        } = s
1521        {
1522            apikey_headers.insert("cookie".into());
1523        }
1524    }
1525    headers
1526        .iter()
1527        .filter(|(k, _)| {
1528            let lk = k.to_lowercase();
1529            lk != "authorization" && !apikey_headers.contains(&lk)
1530        })
1531        .cloned()
1532        .collect()
1533}
1534
1535/// Remove API-key query parameters declared by the operation's
1536/// security schemes from `query`, so a probe can supply its own.
1537fn strip_auth_query(
1538    query: &[(String, String)],
1539    schemes: &[SecuritySchemeInfo],
1540) -> Vec<(String, String)> {
1541    let mut apikey_query: std::collections::BTreeSet<String> = Default::default();
1542    for s in schemes {
1543        if let SecuritySchemeInfo::ApiKey {
1544            location: ApiKeyLocation::Query,
1545            name,
1546        } = s
1547        {
1548            apikey_query.insert(name.clone());
1549        }
1550    }
1551    query.iter().filter(|(k, _)| !apikey_query.contains(k)).cloned().collect()
1552}
1553
1554/// Round 35 (#859) — Srikanth on 0.3.179: embedded-content variant-b
1555/// probes were flagging well-behaved 4xx responses as mismatches when
1556/// in reality only a 5xx (server CRASHED trying to parse the embedded
1557/// XML/YAML/multipart/urlencoded payload) is the bug the probe was
1558/// designed to find. Tristate replaces the older `expected_4xx: bool`
1559/// so variant-b probes can opt into "anything but 5xx is fine".
1560#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1561pub(crate) enum ExpectedOutcome {
1562    /// Positive probe: spec-compliant request, expect 2xx or 3xx.
1563    Success,
1564    /// Negative probe: invalid request, expect 4xx.
1565    ClientError,
1566    /// Embedded-content variant-b probe: spec-shape envelope with a
1567    /// non-JSON payload embedded in the first string field. Any
1568    /// response that isn't a 5xx is fine; the probe is here to catch
1569    /// server crashes on the embedded payload.
1570    NotServerError,
1571}
1572
1573impl ExpectedOutcome {
1574    /// Whether `actual_status` counts as a pass for this outcome.
1575    fn passes(self, actual_status: u16) -> bool {
1576        match self {
1577            ExpectedOutcome::Success => (200..400).contains(&actual_status),
1578            ExpectedOutcome::ClientError => (400..500).contains(&actual_status),
1579            ExpectedOutcome::NotServerError => {
1580                actual_status >= 200 && !(500..600).contains(&actual_status)
1581            }
1582        }
1583    }
1584
1585    /// Human-readable hint persisted in the JSONL capture + HTML
1586    /// viewer's "show mismatches only" filter; also what users `jq`
1587    /// against.
1588    fn as_str(self) -> &'static str {
1589        match self {
1590            ExpectedOutcome::Success => "2xx-3xx",
1591            ExpectedOutcome::ClientError => "4xx",
1592            ExpectedOutcome::NotServerError => "2xx-4xx",
1593        }
1594    }
1595}
1596
1597/// Variant of `send_case` that takes an explicit `extra_headers`
1598/// (rather than reading them from `config`). Used by security probes
1599/// to substitute or strip the configured Authorization header.
1600#[allow(clippy::too_many_arguments)]
1601async fn send_case_with_extra(
1602    client: &Client,
1603    config: &SelfTestConfig,
1604    method: Method,
1605    url: &str,
1606    label: &str,
1607    expected: ExpectedOutcome,
1608    body: Option<&str>,
1609    query: Vec<(String, String)>,
1610    headers: Vec<(String, String)>,
1611    extra_headers: Vec<(String, String)>,
1612    // Round 33 (#823) — spec path template (e.g. `/users/{id}`)
1613    // for the operation this probe belongs to. Stamped on the
1614    // capture so the per-endpoint summary can group by template.
1615    path_template: &str,
1616) -> CaseOutcome {
1617    let mut req = client.request(method.clone(), url);
1618    let mut capture_headers: BTreeMap<String, String> = BTreeMap::new();
1619    for (k, v) in &query {
1620        req = req.query(&[(k.as_str(), v.as_str())]);
1621    }
1622    // Round 36 (#876) — stamp the client side first so the same
1623    // `client_sent_at` string flows into both the request headers
1624    // (so the server-side `ServerConformanceViolation` records it
1625    // verbatim) and the on-disk `CaseCapture` JSONL line. Don't
1626    // re-call `Utc::now()` after `req.send()` — that would record
1627    // a different timestamp than the server sees.
1628    let mockforge_version = env!("CARGO_PKG_VERSION").to_string();
1629    let client_sent_at = chrono::Utc::now().to_rfc3339();
1630    // Round 28 — reqwest's `.header(k, v)` APPENDS rather than replaces
1631    // (.headers().insert() would replace but isn't on the builder).
1632    // The previous round-25 fix relied on "last-write-wins" semantics
1633    // that don't exist; for content-type-swap probes the request went
1634    // out with BOTH `Content-Type: application/json` AND `Content-Type:
1635    // application/xml`, and axum's `Json<>` extractor picked the JSON
1636    // one and accepted, so the server-side validator never saw the
1637    // mismatch. Build a `HeaderMap` ourselves so the override
1638    // replaces the body-block default exactly once.
1639    let mut final_headers: reqwest::header::HeaderMap = reqwest::header::HeaderMap::new();
1640    if let Some(_b) = body {
1641        if let Ok(v) = reqwest::header::HeaderValue::from_str("application/json") {
1642            final_headers.insert(reqwest::header::CONTENT_TYPE, v);
1643        }
1644        capture_headers.insert("Content-Type".to_string(), "application/json".to_string());
1645    }
1646    for (k, v) in &headers {
1647        if let (Ok(hn), Ok(hv)) = (
1648            reqwest::header::HeaderName::from_bytes(k.as_bytes()),
1649            reqwest::header::HeaderValue::from_str(v),
1650        ) {
1651            final_headers.insert(hn, hv);
1652        }
1653        capture_headers.insert(k.clone(), v.clone());
1654    }
1655    for (k, v) in &extra_headers {
1656        if let (Ok(hn), Ok(hv)) = (
1657            reqwest::header::HeaderName::from_bytes(k.as_bytes()),
1658            reqwest::header::HeaderValue::from_str(v),
1659        ) {
1660            final_headers.insert(hn, hv);
1661        }
1662        capture_headers.insert(k.clone(), v.clone());
1663    }
1664    // Round 36 (#876) — outbound client stamps. Inserted last so
1665    // they can't be clobbered by user-supplied extra-headers, and
1666    // recorded in `capture_headers` so the JSONL line shows the
1667    // exact bytes that went on the wire.
1668    {
1669        let v_header = mockforge_foundation::conformance_violations::CLIENT_VERSION_HEADER;
1670        let s_header = mockforge_foundation::conformance_violations::CLIENT_SENT_AT_HEADER;
1671        if let (Ok(hn), Ok(hv)) = (
1672            reqwest::header::HeaderName::from_bytes(v_header.as_bytes()),
1673            reqwest::header::HeaderValue::from_str(&mockforge_version),
1674        ) {
1675            final_headers.insert(hn, hv);
1676        }
1677        if let (Ok(hn), Ok(hv)) = (
1678            reqwest::header::HeaderName::from_bytes(s_header.as_bytes()),
1679            reqwest::header::HeaderValue::from_str(&client_sent_at),
1680        ) {
1681            final_headers.insert(hn, hv);
1682        }
1683        capture_headers.insert(v_header.to_string(), mockforge_version.clone());
1684        capture_headers.insert(s_header.to_string(), client_sent_at.clone());
1685    }
1686    if let Some(b) = body {
1687        req = req.body(b.to_string());
1688    }
1689    req = req.headers(final_headers);
1690    let (actual_status, response_capture) = match req.send().await {
1691        Ok(resp) => {
1692            let status = resp.status().as_u16();
1693            if let Some(sink) = &config.capture {
1694                let resp_headers: BTreeMap<String, String> = resp
1695                    .headers()
1696                    .iter()
1697                    .map(|(k, v)| (k.as_str().to_string(), v.to_str().unwrap_or("").to_string()))
1698                    .collect();
1699                let text = resp.text().await.unwrap_or_default();
1700                let (rb, truncated) = truncate_body_for_capture(&text);
1701                (status, Some((Some((rb, truncated)), resp_headers, None, sink.clone())))
1702            } else {
1703                (status, None)
1704            }
1705        }
1706        Err(e) => {
1707            let err_str = e.to_string();
1708            // Round 47 (#79) — classify + push to the wire-level
1709            // network-events sink (when present) so the user has a
1710            // grep-able log of connect/timeout/tls failures during
1711            // self-test, matching the r46 native-executor behaviour.
1712            if let Some(sink) = &config.network_events {
1713                let kind = if e.is_connect() {
1714                    "connect"
1715                } else if e.is_timeout() {
1716                    "timeout"
1717                } else if e.is_request() {
1718                    "request"
1719                } else if e.is_body() {
1720                    "body"
1721                } else if e.is_decode() {
1722                    "decode"
1723                } else if err_str.to_ascii_lowercase().contains("tls") {
1724                    "tls"
1725                } else {
1726                    "other"
1727                };
1728                if let Ok(mut guard) = sink.lock() {
1729                    guard.push(NetworkEvent {
1730                        timestamp: chrono::Utc::now(),
1731                        check: label.to_string(),
1732                        method: method.to_string(),
1733                        url: build_query_url(url, &query),
1734                        kind: kind.to_string(),
1735                        message: err_str.clone(),
1736                    });
1737                }
1738            }
1739            if let Some(sink) = &config.capture {
1740                (0, Some((None, BTreeMap::new(), Some(err_str), sink.clone())))
1741            } else {
1742                (0, None)
1743            }
1744        }
1745    };
1746    let passed = expected.passes(actual_status);
1747    if let Some((resp_body, resp_headers, error, sink)) = response_capture {
1748        let (request_body, request_body_truncated) = match body {
1749            Some(b) => {
1750                let (rb, t) = truncate_body_for_capture(b);
1751                (Some(rb), t)
1752            }
1753            None => (None, false),
1754        };
1755        let (response_body, response_body_truncated) = match resp_body {
1756            Some((rb, t)) => (Some(rb), t),
1757            None => (None, false),
1758        };
1759        let entry = CaseCapture {
1760            label: label.to_string(),
1761            method: method.to_string(),
1762            url: build_query_url(url, &query),
1763            request_headers: capture_headers,
1764            request_body,
1765            request_body_truncated,
1766            response_status: actual_status,
1767            response_headers: resp_headers,
1768            response_body,
1769            response_body_truncated,
1770            error,
1771            // Filled in by the per-operation validation pass after
1772            // every probe finishes; the capture itself is unaware of
1773            // the schema map.
1774            response_schema_error: None,
1775            // Round 28 — derive the expected range from the probe's
1776            // outcome shape so the JSONL line and HTML viewer can
1777            // filter mismatches without re-deriving on the read side.
1778            // Round 35 (#859) — add a third value `"2xx-4xx"` for
1779            // embedded-content variant-b probes whose only failure
1780            // mode is a 5xx server crash.
1781            expected_status_range: expected.as_str().to_string(),
1782            // Round 33 (#823) — path_template carries the spec's
1783            // pre-substitution path so the per-endpoint summary can
1784            // collapse `/users/X` and `/users/Y` into one row.
1785            // spec_label is constant per run, read from the config.
1786            path_template: path_template.to_string(),
1787            spec_label: config.spec_label.clone(),
1788            // Round 36 (#876) — same values that went on the wire as
1789            // request headers, so a server-side
1790            // `ServerConformanceViolation` recorded with
1791            // `client_mockforge_version` + `client_sent_at` matches
1792            // the JSONL line byte-for-byte.
1793            mockforge_version: mockforge_version.clone(),
1794            client_sent_at: client_sent_at.clone(),
1795            iteration: config.current_iteration.max(1),
1796        };
1797        if let Ok(mut guard) = sink.lock() {
1798            guard.push(entry);
1799        }
1800    }
1801    // Round 35 (#859) — keep the `expected_4xx` field on `CaseOutcome`
1802    // semantically tied to "negative probe expecting 400-class", so
1803    // downstream code in `report_html.rs` doesn't have to learn about
1804    // the new tristate. `NotServerError` reports as `expected_4xx:
1805    // false` (it's a positive probe in spirit) and instead carries
1806    // its outcome through the per-capture `expected_status_range`.
1807    let expected_4xx = matches!(expected, ExpectedOutcome::ClientError);
1808    CaseOutcome {
1809        label: label.to_string(),
1810        expected_4xx,
1811        actual_status,
1812        passed,
1813    }
1814}
1815
1816// HTTP request shape needs all of these: client, config (for capture
1817// sink + extra headers), method, url, label (probe id), expected_4xx
1818// (pass/fail decision), body, query, headers. A struct wrapper would
1819// just move the arity from positional to field access without making
1820// the call sites clearer.
1821#[allow(clippy::too_many_arguments)]
1822async fn send_case(
1823    client: &Client,
1824    config: &SelfTestConfig,
1825    method: Method,
1826    url: &str,
1827    label: &str,
1828    expected: ExpectedOutcome,
1829    body: Option<&str>,
1830    query: Vec<(String, String)>,
1831    headers: Vec<(String, String)>,
1832    path_template: &str,
1833) -> CaseOutcome {
1834    // Forwarding to `send_case_with_extra` keeps the capture logic in
1835    // one place so request/response tracing can't drift between the
1836    // two entrypoints.
1837    send_case_with_extra(
1838        client,
1839        config,
1840        method,
1841        url,
1842        label,
1843        expected,
1844        body,
1845        query,
1846        headers,
1847        config.extra_headers.clone(),
1848        path_template,
1849    )
1850    .await
1851}
1852
1853/// Round 23 (c-iii) — rebuild the query-stringified URL for capture so
1854/// the JSONL trace shows the URL that actually went over the wire
1855/// (reqwest applies `.query(..)` after the request URL string is
1856/// rendered, so capturing the raw `url` argument alone loses the
1857/// query params).
1858fn build_query_url(base: &str, query: &[(String, String)]) -> String {
1859    if query.is_empty() {
1860        return base.to_string();
1861    }
1862    let qs: String = query
1863        .iter()
1864        .map(|(k, v)| format!("{}={}", urlencoding::encode(k), urlencoding::encode(v)))
1865        .collect::<Vec<_>>()
1866        .join("&");
1867    if base.contains('?') {
1868        format!("{base}&{qs}")
1869    } else {
1870        format!("{base}?{qs}")
1871    }
1872}
1873
1874/// Substitute `{param}` placeholders in the spec path with their
1875/// sample values from `path_params`, then prepend `target_url`. Empty
1876/// values are kept as `{param}` so an upstream router still matches
1877/// the template — useful when `path_params` is empty and we want to
1878/// hit the same route the spec defines.
1879///
1880/// All current call sites went through `build_url_with_base` after
1881/// round 18.1, so this no-base-path helper is unused; keep it as the
1882/// documented shim for future external callers (one-arg simplification).
1883#[allow(dead_code)]
1884fn build_url(target: &str, path_template: &str, path_params: &[(String, String)]) -> String {
1885    build_url_with_base(target, None, path_template, path_params)
1886}
1887
1888/// Round 55 (#79) — normalise a `--base-path` into a URL path prefix.
1889///
1890/// `Some("/api")` / `Some("api")` / `Some("/api/")` all become `/api`;
1891/// `Some("/")`, `Some("")`, and `None` all become `""` (root, no prefix).
1892///
1893/// Srikanth on 0.3.202 ran with `--base-path /` and got ZERO violations even
1894/// though the console reported thousands of misses. The old logic did
1895/// `"/".trim_end_matches('/')` = `""`, decided `"".starts_with('/')` was
1896/// false, and produced the prefix `"/"`. Combined with the leading `/` on the
1897/// spec path that yielded `<target>//v1/organizations` (double slash), which
1898/// never matched the spec's `/v1/organizations`, so every emitted request was
1899/// skipped by the validator and no violations were logged. Trimming slashes
1900/// from BOTH ends and treating the empty result as "no prefix" fixes it.
1901fn base_path_prefix(base_path: Option<&str>) -> String {
1902    match base_path {
1903        Some(bp) => {
1904            let trimmed = bp.trim_matches('/');
1905            if trimmed.is_empty() {
1906                String::new()
1907            } else {
1908                format!("/{trimmed}")
1909            }
1910        }
1911        None => String::new(),
1912    }
1913}
1914
1915/// Round 18.1 — variant of `build_url` that takes a `base_path`
1916/// (e.g. `Some("/api")`). When set, prepends it to the spec path so a
1917/// spec declaring `/users` against a target served behind `/api`
1918/// resolves to `<target>/api/users`. `base_path` is normalised via
1919/// [`base_path_prefix`] (both ends trimmed; `/` means root/no prefix).
1920fn build_url_with_base(
1921    target: &str,
1922    base_path: Option<&str>,
1923    path_template: &str,
1924    path_params: &[(String, String)],
1925) -> String {
1926    let mut url = path_template.to_string();
1927    for (name, value) in path_params {
1928        let placeholder = format!("{{{}}}", name);
1929        if !value.is_empty() {
1930            url = url.replace(&placeholder, value);
1931        }
1932    }
1933    let target = target.trim_end_matches('/');
1934    let prefix = base_path_prefix(base_path);
1935    let path = if url.starts_with('/') {
1936        url
1937    } else {
1938        format!("/{url}")
1939    };
1940    format!("{target}{prefix}{path}")
1941}
1942
1943#[cfg(test)]
1944mod tests {
1945    use super::*;
1946
1947    fn op(
1948        method: &str,
1949        path: &str,
1950        body: Option<&str>,
1951        query: Vec<(&str, &str)>,
1952        headers: Vec<(&str, &str)>,
1953        path_params: Vec<(&str, &str)>,
1954    ) -> AnnotatedOperation {
1955        AnnotatedOperation {
1956            method: method.into(),
1957            path: path.into(),
1958            features: Vec::new(),
1959            request_body_content_type: body.map(|_| "application/json".into()),
1960            sample_body: body.map(|s| s.to_string()),
1961            query_params: query.into_iter().map(|(a, b)| (a.into(), b.into())).collect(),
1962            header_params: headers.into_iter().map(|(a, b)| (a.into(), b.into())).collect(),
1963            path_params: path_params.into_iter().map(|(a, b)| (a.into(), b.into())).collect(),
1964            response_schema: None,
1965            response_schemas: std::collections::BTreeMap::new(),
1966            request_body_schema: None,
1967            security_schemes: Vec::new(),
1968        }
1969    }
1970
1971    /// Round 36 (#876) — older JSONL lines (written before the stamp
1972    /// fields existed) must still deserialise without error and
1973    /// default to empty strings. Prevents a back-compat regression
1974    /// the next time we extend `CaseCapture`.
1975    #[test]
1976    fn case_capture_back_compat_when_stamp_fields_missing() {
1977        let pre_r36 = serde_json::json!({
1978            "label": "positive",
1979            "method": "GET",
1980            "url": "http://api/users",
1981            "request_headers": {},
1982            "request_body_truncated": false,
1983            "response_status": 200,
1984            "response_headers": {},
1985            "response_body_truncated": false,
1986        });
1987        let capture: CaseCapture =
1988            serde_json::from_value(pre_r36).expect("pre-r36 payload must deserialise");
1989        assert!(capture.mockforge_version.is_empty(), "default to empty");
1990        assert!(capture.client_sent_at.is_empty(), "default to empty");
1991    }
1992
1993    /// Round 36 (#876) — when the bench stamps fields itself (the
1994    /// happy path), they round-trip through serde unchanged. Pins
1995    /// the on-wire shape so tooling that grep's `mockforge_version`
1996    /// out of the JSONL stays valid.
1997    #[test]
1998    fn case_capture_stamps_round_trip_through_serde() {
1999        let stamped = CaseCapture {
2000            label: "positive".into(),
2001            method: "GET".into(),
2002            url: "http://api/users".into(),
2003            request_headers: BTreeMap::new(),
2004            request_body: None,
2005            request_body_truncated: false,
2006            response_status: 200,
2007            response_headers: BTreeMap::new(),
2008            response_body: None,
2009            response_body_truncated: false,
2010            error: None,
2011            response_schema_error: None,
2012            expected_status_range: "2xx-3xx".into(),
2013            path_template: "/users".into(),
2014            spec_label: None,
2015            mockforge_version: "0.3.183".into(),
2016            client_sent_at: "2026-06-17T12:34:56+00:00".into(),
2017            iteration: 1,
2018        };
2019        let json = serde_json::to_string(&stamped).unwrap();
2020        assert!(json.contains("\"mockforge_version\":\"0.3.183\""));
2021        assert!(json.contains("\"client_sent_at\":\"2026-06-17T12:34:56+00:00\""));
2022        let back: CaseCapture = serde_json::from_str(&json).unwrap();
2023        assert_eq!(back.mockforge_version, "0.3.183");
2024        assert_eq!(back.client_sent_at, "2026-06-17T12:34:56+00:00");
2025    }
2026
2027    #[test]
2028    fn build_url_substitutes_path_params() {
2029        let url = build_url(
2030            "https://api.test/",
2031            "/users/{id}/posts/{pid}",
2032            &[("id".into(), "42".into()), ("pid".into(), "7".into())],
2033        );
2034        assert_eq!(url, "https://api.test/users/42/posts/7");
2035    }
2036
2037    /// Round 18.1 — a run where every positive 404s should be flagged
2038    /// as a likely target misconfiguration, not silently treated as a
2039    /// successful conformance run.
2040    #[test]
2041    fn detect_target_misconfiguration_when_all_positives_share_status() {
2042        let mut report = SelfTestReport {
2043            positive_pass: 0,
2044            positive_fail: 50,
2045            ..Default::default()
2046        };
2047        for i in 0..50 {
2048            report.operations.push(OperationResult {
2049                method: "GET".into(),
2050                path: format!("/r/{i}"),
2051                positive: Some(CaseOutcome {
2052                    label: "positive".into(),
2053                    expected_4xx: false,
2054                    actual_status: 404,
2055                    passed: false,
2056                }),
2057                negatives: Vec::new(),
2058            });
2059        }
2060        assert_eq!(report.detect_target_misconfiguration(), Some(404));
2061    }
2062
2063    #[test]
2064    fn detect_target_misconfiguration_returns_none_when_some_pass() {
2065        let mut report = SelfTestReport {
2066            positive_pass: 5,
2067            positive_fail: 50,
2068            ..Default::default()
2069        };
2070        for i in 0..55 {
2071            report.operations.push(OperationResult {
2072                method: "GET".into(),
2073                path: format!("/r/{i}"),
2074                positive: Some(CaseOutcome {
2075                    label: "positive".into(),
2076                    expected_4xx: false,
2077                    actual_status: if i < 5 { 200 } else { 404 },
2078                    passed: i < 5,
2079                }),
2080                negatives: Vec::new(),
2081            });
2082        }
2083        assert_eq!(report.detect_target_misconfiguration(), None);
2084    }
2085
2086    /// Round 18.1 — `--base-path /api` should prepend `/api` to
2087    /// every spec path. Pre-fix, the self-test ignored base_path and
2088    /// 404'd every positive when the deployed API was behind a path
2089    /// prefix.
2090    #[test]
2091    fn build_url_applies_base_path_when_present() {
2092        let url = build_url_with_base(
2093            "https://api.example.com",
2094            Some("/api"),
2095            "/users/{id}",
2096            &[("id".into(), "42".into())],
2097        );
2098        assert_eq!(url, "https://api.example.com/api/users/42");
2099    }
2100
2101    /// Round 18.1 — base_path is normalised: missing leading slash
2102    /// gets one added, trailing slash is stripped, empty string is
2103    /// the same as None.
2104    #[test]
2105    fn build_url_normalises_base_path() {
2106        let no_slash = build_url_with_base("https://t", Some("api"), "/x", &[]);
2107        assert_eq!(no_slash, "https://t/api/x");
2108        let trailing = build_url_with_base("https://t", Some("/api/"), "/x", &[]);
2109        assert_eq!(trailing, "https://t/api/x");
2110        let empty = build_url_with_base("https://t", Some(""), "/x", &[]);
2111        assert_eq!(empty, "https://t/x");
2112        let none = build_url_with_base("https://t", None, "/x", &[]);
2113        assert_eq!(none, "https://t/x");
2114    }
2115
2116    /// Round 55 (#79) — Srikanth on 0.3.202 passed `--base-path /` and got a
2117    /// `//v1/organizations` double slash, which broke all validation. `/`
2118    /// (and `//`) must be treated as root, i.e. no prefix.
2119    #[test]
2120    fn base_path_slash_is_root_no_double_slash() {
2121        assert_eq!(base_path_prefix(Some("/")), "");
2122        assert_eq!(base_path_prefix(Some("//")), "");
2123        assert_eq!(base_path_prefix(Some("")), "");
2124        assert_eq!(base_path_prefix(None), "");
2125        assert_eq!(base_path_prefix(Some("/api")), "/api");
2126        assert_eq!(base_path_prefix(Some("api/")), "/api");
2127        assert_eq!(base_path_prefix(Some("/api/v1")), "/api/v1");
2128        // The end-to-end URL no longer double-slashes.
2129        assert_eq!(
2130            build_url_with_base("https://t", Some("/"), "/v1/organizations", &[]),
2131            "https://t/v1/organizations"
2132        );
2133    }
2134
2135    #[test]
2136    fn build_url_keeps_placeholders_when_no_sample() {
2137        let url = build_url("https://api.test", "/users/{id}", &[]);
2138        assert_eq!(url, "https://api.test/users/{id}");
2139    }
2140
2141    #[test]
2142    fn report_summary_calls_out_misses() {
2143        let r = SelfTestReport {
2144            positive_pass: 3,
2145            positive_fail: 0,
2146            negative_caught: BTreeMap::from([("request-body".into(), 2)]),
2147            negative_missed: BTreeMap::from([("request-body".into(), 1)]),
2148            operations: Vec::new(),
2149        };
2150        let summary = r.render_summary();
2151        assert!(summary.contains("Positives: 3 pass / 0 fail"));
2152        assert!(summary.contains("Negatives [request-body]: 2 caught / 1 missed"));
2153        assert!(summary.contains("⚠"));
2154        assert!(!r.all_passed());
2155    }
2156
2157    #[test]
2158    fn report_all_passed_when_no_miss() {
2159        let r = SelfTestReport {
2160            positive_pass: 5,
2161            positive_fail: 0,
2162            negative_caught: BTreeMap::from([("parameters".into(), 3)]),
2163            negative_missed: BTreeMap::new(),
2164            operations: Vec::new(),
2165        };
2166        assert!(r.all_passed());
2167        assert!(r.render_summary().contains("✓"));
2168    }
2169
2170    #[tokio::test]
2171    async fn run_self_test_against_unreachable_target_marks_all_failed() {
2172        // Use an obviously-dead port so we exercise the timeout/error
2173        // path without needing a live server in tests.
2174        let cfg = SelfTestConfig {
2175            target_url: "http://127.0.0.1:1".into(),
2176            timeout: Duration::from_millis(200),
2177            ..Default::default()
2178        };
2179        let ops = vec![op(
2180            "POST",
2181            "/users",
2182            Some("{\"name\":\"a\"}"),
2183            vec![],
2184            vec![],
2185            vec![],
2186        )];
2187        let report = run_self_test(&ops, &cfg).await.expect("client builds");
2188        // All cases hit the connect-error path → actual_status=0.
2189        // Positive expects 2xx-3xx → 0 is fail. Negatives expect 4xx
2190        // → 0 is also fail (we missed catching).
2191        assert_eq!(report.positive_fail, 1);
2192        assert!(report.negative_missed.values().sum::<usize>() >= 1);
2193        assert!(!report.all_passed());
2194    }
2195
2196    /// Round 17.2 — operations with both a positive sample AND a
2197    /// resolved request-body schema produce schema-driven negatives
2198    /// in addition to the spec-agnostic empty/wrong-type ones. The
2199    /// labels carry the field path so a per-category report can tell
2200    /// you exactly which field caught.
2201    #[tokio::test]
2202    async fn schema_driven_negatives_fire_when_schema_present() {
2203        use openapiv3::{ObjectType, ReferenceOr, Schema, SchemaData, SchemaKind, Type};
2204        let cfg = SelfTestConfig {
2205            target_url: "http://127.0.0.1:1".into(),
2206            timeout: Duration::from_millis(200),
2207            ..Default::default()
2208        };
2209        // Build an operation whose schema has a required `name` string
2210        // and an `age` integer. The mutator should produce, at
2211        // minimum: required-removed:name, required-removed:age,
2212        // type-mismatch:name, type-mismatch:age, integer-as-float:age,
2213        // plus the root-level type-mismatch.
2214        let mut obj = ObjectType::default();
2215        obj.properties.insert(
2216            "name".to_string(),
2217            ReferenceOr::Item(Box::new(Schema {
2218                schema_data: SchemaData::default(),
2219                schema_kind: SchemaKind::Type(Type::String(Default::default())),
2220            })),
2221        );
2222        obj.properties.insert(
2223            "age".to_string(),
2224            ReferenceOr::Item(Box::new(Schema {
2225                schema_data: SchemaData::default(),
2226                schema_kind: SchemaKind::Type(Type::Integer(Default::default())),
2227            })),
2228        );
2229        obj.required = vec!["name".into(), "age".into()];
2230        let schema = Schema {
2231            schema_data: SchemaData::default(),
2232            schema_kind: SchemaKind::Type(Type::Object(obj)),
2233        };
2234
2235        let mut o =
2236            op("POST", "/users", Some(r#"{"name":"Ada","age":30}"#), vec![], vec![], vec![]);
2237        o.request_body_schema = Some(schema);
2238        let report = run_self_test(&[o], &cfg).await.expect("client builds");
2239        // Bucket labels from the operation result.
2240        let labels: std::collections::BTreeSet<String> = report
2241            .operations
2242            .iter()
2243            .flat_map(|op| op.negatives.iter().map(|n| n.label.clone()))
2244            .collect();
2245        assert!(
2246            labels.iter().any(|l| l.starts_with("request-body:type-mismatch:")),
2247            "missing type-mismatch negative; got {labels:?}"
2248        );
2249        assert!(
2250            labels.iter().any(|l| l.starts_with("request-body:required-removed:")),
2251            "missing required-removed negative; got {labels:?}"
2252        );
2253        assert!(
2254            labels.iter().any(|l| l == "parameters:uri-too-long"),
2255            "missing URI-length negative; got {labels:?}"
2256        );
2257    }
2258
2259    /// Round 16 — operations with a body OR a path-param now produce
2260    /// negatives even without a sample body. Previously a POST whose
2261    /// body annotator failed produced *zero* negatives, so the self-test
2262    /// always reported "all passing" for that endpoint.
2263    #[tokio::test]
2264    async fn no_sample_body_still_produces_request_body_negatives() {
2265        let cfg = SelfTestConfig {
2266            target_url: "http://127.0.0.1:1".into(),
2267            timeout: Duration::from_millis(200),
2268            ..Default::default()
2269        };
2270        // POST with a body content type but no sample (annotator gap).
2271        let ops = vec![op("POST", "/x", None, vec![], vec![], vec![])];
2272        // No sample_body but request_body_content_type set:
2273        let mut ops_fixed = ops;
2274        ops_fixed[0].request_body_content_type = Some("application/json".into());
2275        let report = run_self_test(&ops_fixed, &cfg).await.expect("client builds");
2276        // Both request-body negatives (empty + wrong-type) should fire,
2277        // landing in `negative_missed` because the unreachable target
2278        // returns no 4xx. The point: count > 0.
2279        assert!(
2280            report.negative_missed.values().sum::<usize>() >= 2,
2281            "expected ≥2 request-body negatives, got {:?}",
2282            report.negative_missed
2283        );
2284    }
2285
2286    /// Round 16 — operations with a path-param now get a probe even
2287    /// when there's no body / required query / required header.
2288    /// Previously `/teams/{team-id}` with no other required fields
2289    /// produced zero negatives → always "all passing".
2290    #[tokio::test]
2291    async fn path_param_only_endpoint_produces_a_probe() {
2292        let cfg = SelfTestConfig {
2293            target_url: "http://127.0.0.1:1".into(),
2294            timeout: Duration::from_millis(200),
2295            ..Default::default()
2296        };
2297        let ops = vec![op(
2298            "GET",
2299            "/teams/{team-id}",
2300            None,
2301            vec![],
2302            vec![],
2303            vec![("team-id", "1")],
2304        )];
2305        let report = run_self_test(&ops, &cfg).await.expect("client builds");
2306        let total: usize = report.negative_caught.values().sum::<usize>()
2307            + report.negative_missed.values().sum::<usize>();
2308        assert!(total >= 1, "expected ≥1 path-param probe, got {:?}", report);
2309    }
2310
2311    /// Round 18.5 — when `geo_ip` is set, every default forwarded-
2312    /// IP header gets the IP appended (X-Forwarded-For,
2313    /// True-Client-IP, CF-Connecting-IP).
2314    #[test]
2315    fn effective_op_headers_appends_geo_ip_to_default_headers() {
2316        let ip: IpAddr = "203.0.113.42".parse().unwrap();
2317        let headers = effective_op_headers(
2318            &[("Accept".into(), "application/json".into())],
2319            Some(ip),
2320            &default_geo_source_headers(),
2321        );
2322        let names: Vec<&str> = headers.iter().map(|(k, _)| k.as_str()).collect();
2323        assert!(names.contains(&"Accept"));
2324        assert!(names.contains(&"X-Forwarded-For"));
2325        assert!(names.contains(&"True-Client-IP"));
2326        assert!(names.contains(&"CF-Connecting-IP"));
2327        // Every geo header carries the same IP value.
2328        let geo_values: Vec<&str> =
2329            headers.iter().filter(|(k, _)| k != "Accept").map(|(_, v)| v.as_str()).collect();
2330        for v in geo_values {
2331            assert_eq!(v, "203.0.113.42");
2332        }
2333    }
2334
2335    /// Round 18.5 — operations that already declare a forwarded-IP
2336    /// header (rare but legal — some specs hard-code one) keep their
2337    /// declared value; we don't clobber the spec.
2338    #[test]
2339    fn effective_op_headers_respects_spec_declared_header() {
2340        let ip: IpAddr = "203.0.113.99".parse().unwrap();
2341        let headers = effective_op_headers(
2342            &[("x-forwarded-for".into(), "10.0.0.1".into())],
2343            Some(ip),
2344            &["X-Forwarded-For".to_string()],
2345        );
2346        // The spec's lower-case value wins; we shouldn't add a
2347        // second X-Forwarded-For row that overrides it.
2348        let xff: Vec<&str> = headers
2349            .iter()
2350            .filter(|(k, _)| k.eq_ignore_ascii_case("x-forwarded-for"))
2351            .map(|(_, v)| v.as_str())
2352            .collect();
2353        assert_eq!(xff, vec!["10.0.0.1"]);
2354    }
2355
2356    /// Round 18.5 — None geo_ip and/or empty header list is a no-op.
2357    #[test]
2358    fn effective_op_headers_is_a_noop_without_geo_ip() {
2359        let base = vec![("Accept".into(), "json".into())];
2360        let h1 = effective_op_headers(&base, None, &default_geo_source_headers());
2361        assert_eq!(h1, base);
2362        let ip: IpAddr = "10.0.0.1".parse().unwrap();
2363        let h2 = effective_op_headers(&base, Some(ip), &[]);
2364        assert_eq!(h2, base);
2365    }
2366
2367    /// Round 18.5 — empty `source_ips` builds a single default
2368    /// client; a non-empty list builds N clients each attempting to
2369    /// bind. We can't reliably test the actual bind on CI (no
2370    /// loopback aliases), but a loopback IP is always bind-able.
2371    #[test]
2372    fn build_client_pool_one_per_source_ip() {
2373        let mut cfg = SelfTestConfig {
2374            target_url: "http://127.0.0.1:1".into(),
2375            timeout: Duration::from_millis(200),
2376            ..Default::default()
2377        };
2378        // Empty → one default client.
2379        assert_eq!(build_client_pool(&cfg).expect("default builds").len(), 1);
2380        // Non-empty → one per IP. Loopback bind is portable.
2381        cfg.source_ips = vec!["127.0.0.1".parse().unwrap()];
2382        assert_eq!(build_client_pool(&cfg).expect("bind loopback").len(), 1);
2383    }
2384
2385    /// Round 18.5 — geo IPs round-robin across operations. Hits an
2386    /// unreachable target so we can inspect the case outcomes; the
2387    /// point is to confirm `op_headers` carried the geo IP through
2388    /// (CaseOutcome doesn't surface headers directly, so we just
2389    /// verify the run completes without panicking and the result
2390    /// shape is correct when source_ips is non-empty too).
2391    #[tokio::test]
2392    async fn run_self_test_with_geo_source_completes() {
2393        let cfg = SelfTestConfig {
2394            target_url: "http://127.0.0.1:1".into(),
2395            timeout: Duration::from_millis(200),
2396            geo_source_ips: vec![
2397                "203.0.113.1".parse().unwrap(),
2398                "203.0.113.2".parse().unwrap(),
2399            ],
2400            ..Default::default()
2401        };
2402        let ops = vec![
2403            op("GET", "/a", None, vec![], vec![], vec![]),
2404            op("GET", "/b", None, vec![], vec![], vec![]),
2405            op("GET", "/c", None, vec![], vec![], vec![]),
2406        ];
2407        let report = run_self_test(&ops, &cfg).await.expect("client builds");
2408        assert_eq!(report.operations.len(), 3);
2409    }
2410
2411    /// Round 24 (f) — Srikanth saw the geo header on positive probes
2412    /// only; the four negative-probe call sites were passing
2413    /// `op.header_params` directly instead of `op_headers`, so the
2414    /// geo IP got dropped. This test runs a self-test that includes
2415    /// negative probes (uri-too-long, missing-query, etc.) under
2416    /// `--conformance-self-test-capture`, then asserts that EVERY
2417    /// captured probe (positive AND negative) carries one of the
2418    /// configured forwarded-IP headers.
2419    #[tokio::test]
2420    async fn geo_headers_present_on_every_probe_with_capture() {
2421        let sink: Arc<Mutex<Vec<CaseCapture>>> = Arc::new(Mutex::new(Vec::new()));
2422        let cfg = SelfTestConfig {
2423            target_url: "http://127.0.0.1:1".into(),
2424            timeout: Duration::from_millis(50),
2425            geo_source_ips: vec!["203.0.113.5".parse().unwrap()],
2426            capture: Some(sink.clone()),
2427            ..Default::default()
2428        };
2429        // An operation rich enough to trip several negative-probe
2430        // branches: header param (→ missing-header), query param
2431        // (→ missing-query), and a sample body (→ schema mutations
2432        // wouldn't fire without a schema, but uri-too-long always
2433        // does).
2434        let ops = vec![op(
2435            "GET",
2436            "/items",
2437            Some("{}"),
2438            vec![("id", "1")],
2439            vec![("X-Trace", "x")],
2440            vec![],
2441        )];
2442        let _ = run_self_test(&ops, &cfg).await.expect("client builds");
2443        let captures = sink.lock().unwrap();
2444        assert!(!captures.is_empty(), "self-test should record probes");
2445        // For every captured probe, at least one of the default geo
2446        // headers must be present and equal to the configured IP.
2447        let geo_headers: std::collections::HashSet<&str> =
2448            ["X-Forwarded-For", "True-Client-IP", "CF-Connecting-IP"].into_iter().collect();
2449        for c in captures.iter() {
2450            let has_geo = c
2451                .request_headers
2452                .iter()
2453                .any(|(k, v)| geo_headers.contains(k.as_str()) && v == "203.0.113.5");
2454            assert!(
2455                has_geo,
2456                "probe `{}` is missing the geo IP header; got headers: {:?}",
2457                c.label, c.request_headers
2458            );
2459        }
2460    }
2461
2462    /// Round 25 (k) — operations with a JSON request body now get four
2463    /// content-type-swap probes (xml / yaml / multipart / urlencoded).
2464    /// Verify they:
2465    ///   1. fire only when the operation declares a JSON body
2466    ///   2. carry the wrong Content-Type the probe is testing for
2467    ///   3. don't fire on body-less operations
2468    #[tokio::test]
2469    async fn content_type_swap_probes_fire_for_json_bodies() {
2470        let sink: Arc<Mutex<Vec<CaseCapture>>> = Arc::new(Mutex::new(Vec::new()));
2471        let cfg = SelfTestConfig {
2472            target_url: "http://127.0.0.1:1".into(),
2473            timeout: Duration::from_millis(50),
2474            capture: Some(sink.clone()),
2475            ..Default::default()
2476        };
2477        let ops = vec![
2478            op("POST", "/users", Some("{\"name\":\"a\"}"), vec![], vec![], vec![]),
2479            op("GET", "/ping", None, vec![], vec![], vec![]),
2480        ];
2481        let _ = run_self_test(&ops, &cfg).await.expect("client builds");
2482        let captures = sink.lock().unwrap();
2483
2484        let swap_labels: Vec<&str> = captures
2485            .iter()
2486            .filter(|c| c.label.starts_with("request-body:content-type-mismatch:"))
2487            .map(|c| c.label.as_str())
2488            .collect();
2489        assert_eq!(
2490            swap_labels.len(),
2491            4,
2492            "expected 4 content-type-swap probes (one per variant), got: {swap_labels:?}"
2493        );
2494        let expected_labels = [
2495            "request-body:content-type-mismatch:xml",
2496            "request-body:content-type-mismatch:yaml",
2497            "request-body:content-type-mismatch:multipart",
2498            "request-body:content-type-mismatch:urlencoded",
2499        ];
2500        for want in expected_labels {
2501            assert!(swap_labels.contains(&want), "missing swap probe `{want}`");
2502        }
2503
2504        // Each swap probe must carry the wrong Content-Type it's
2505        // testing for — that's the whole point.
2506        for c in captures.iter() {
2507            let Some(suffix) = c.label.strip_prefix("request-body:content-type-mismatch:") else {
2508                continue;
2509            };
2510            let want_ct = match suffix {
2511                "xml" => "application/xml",
2512                "yaml" => "application/yaml",
2513                "multipart" => "multipart/form-data",
2514                "urlencoded" => "application/x-www-form-urlencoded",
2515                _ => continue,
2516            };
2517            let got_ct = c
2518                .request_headers
2519                .iter()
2520                .find(|(k, _)| k.eq_ignore_ascii_case("content-type"))
2521                .map(|(_, v)| v.as_str())
2522                .unwrap_or("");
2523            assert_eq!(got_ct, want_ct, "swap probe `{}` sent wrong CT", c.label);
2524        }
2525
2526        // The body-less operation must NOT produce content-type-swap
2527        // probes (no body → no content type to lie about).
2528        let body_less_swaps = captures
2529            .iter()
2530            .filter(|c| {
2531                c.label.starts_with("request-body:content-type-mismatch:")
2532                    && c.url.ends_with("/ping")
2533            })
2534            .count();
2535        assert_eq!(
2536            body_less_swaps, 0,
2537            "GET /ping has no request body; should not produce content-type-swap probes"
2538        );
2539    }
2540
2541    /// Round 27 (k variant b) — Srikanth's round-23 follow-up on (k):
2542    /// JSON envelope with embedded non-JSON field values. For each
2543    /// JSON-body operation, four extra probes fire that send valid
2544    /// JSON with an XML/YAML/multipart/urlencoded snippet stuffed
2545    /// into a string field. Content-Type stays `application/json`;
2546    /// expected is 2xx-3xx (the body parses); a 5xx flags a server
2547    /// that crashed on the embedded content.
2548    #[tokio::test]
2549    async fn embedded_content_probes_fire_with_honest_content_type() {
2550        let sink: Arc<Mutex<Vec<CaseCapture>>> = Arc::new(Mutex::new(Vec::new()));
2551        let cfg = SelfTestConfig {
2552            target_url: "http://127.0.0.1:1".into(),
2553            timeout: Duration::from_millis(50),
2554            capture: Some(sink.clone()),
2555            ..Default::default()
2556        };
2557        let ops = vec![op(
2558            "POST",
2559            "/users",
2560            Some("{\"name\":\"alice\",\"age\":30}"),
2561            vec![],
2562            vec![],
2563            vec![],
2564        )];
2565        let _ = run_self_test(&ops, &cfg).await.expect("client builds");
2566        let captures = sink.lock().unwrap();
2567        let embedded: Vec<&CaseCapture> = captures
2568            .iter()
2569            .filter(|c| c.label.starts_with("request-body:embedded-content:"))
2570            .collect();
2571        assert_eq!(
2572            embedded.len(),
2573            4,
2574            "expected 4 embedded-content probes, got: {:?}",
2575            embedded.iter().map(|c| &c.label).collect::<Vec<_>>()
2576        );
2577        // Every embedded probe must carry the honest application/json
2578        // Content-Type (NOT lie like the variant-a content-type-swap
2579        // probes do) and a request body that still parses as JSON.
2580        for c in &embedded {
2581            let ct = c
2582                .request_headers
2583                .iter()
2584                .find(|(k, _)| k.eq_ignore_ascii_case("content-type"))
2585                .map(|(_, v)| v.as_str())
2586                .unwrap_or("");
2587            assert!(
2588                ct.contains("application/json"),
2589                "embedded probe `{}` should keep Content-Type honest, got {ct}",
2590                c.label
2591            );
2592            let body = c.request_body.as_deref().unwrap_or("");
2593            assert!(
2594                serde_json::from_str::<serde_json::Value>(body).is_ok(),
2595                "embedded probe `{}` body should still be valid JSON, got: {body}",
2596                c.label
2597            );
2598        }
2599    }
2600
2601    /// `embed_payload_in_first_string_field` walks objects depth-first
2602    /// and replaces only the FIRST string-valued leaf, leaving the
2603    /// surrounding structure intact.
2604    #[test]
2605    fn embed_payload_replaces_first_string_only() {
2606        let sample = r#"{"name":"alice","age":30,"tags":["admin","user"]}"#;
2607        let mutated = embed_payload_in_first_string_field(sample, "<x/>")
2608            .expect("string field present so probe constructed");
2609        let v: serde_json::Value = serde_json::from_str(&mutated).unwrap();
2610        assert_eq!(v["name"], serde_json::json!("<x/>"));
2611        // age stays an integer (not stringified by the mutation).
2612        assert_eq!(v["age"], serde_json::json!(30));
2613        // tags array's strings stay untouched (we only replace the
2614        // first encountered string leaf, depth-first).
2615        assert_eq!(v["tags"][0], serde_json::json!("admin"));
2616        assert_eq!(v["tags"][1], serde_json::json!("user"));
2617    }
2618
2619    /// Round 34 (#829) — Srikanth on 0.3.178: when the positive
2620    /// sample has NO string field, the previous `{"data": <snippet>}`
2621    /// fallback produced an envelope that doesn't match real-API
2622    /// schemas (e.g. vCenter's `consolecli` PUT wants
2623    /// `{enabled: bool}`), so the server correctly 400'd and the
2624    /// bench misreported the 2xx-3xx expectation. Now we return None
2625    /// and the caller skips the probe.
2626    #[test]
2627    fn embed_payload_returns_none_when_no_string_field() {
2628        let no_strings = r#"{"a":1,"b":[2,3]}"#;
2629        assert!(embed_payload_in_first_string_field(no_strings, "<x><y></y></x>").is_none());
2630        // The exact vCenter-style case Srikanth hit.
2631        let bool_only = r#"{"enabled":true}"#;
2632        assert!(embed_payload_in_first_string_field(bool_only, "<x/>").is_none());
2633    }
2634
2635    #[test]
2636    fn embed_payload_returns_none_for_invalid_json_sample() {
2637        assert!(embed_payload_in_first_string_field("garbage", "a=1&b=2").is_none());
2638    }
2639
2640    /// Round 35 (#859) — Srikanth on 0.3.179 saw variant-b probes flag
2641    /// every 4xx as a mismatch when the spec field had a `pattern` /
2642    /// `format` validator that correctly rejected the embedded
2643    /// payload. The probe was only ever meant to catch 5xx (server
2644    /// crashed parsing the embedded content); 4xx is the well-behaved
2645    /// outcome. Tristate `ExpectedOutcome::NotServerError` lets a
2646    /// variant-b probe pass on 2xx-4xx and fail only on 5xx.
2647    #[test]
2648    fn expected_outcome_pass_rules() {
2649        // Success (positive): 2xx-3xx pass, 4xx + 5xx fail.
2650        assert!(ExpectedOutcome::Success.passes(200));
2651        assert!(ExpectedOutcome::Success.passes(201));
2652        assert!(ExpectedOutcome::Success.passes(204));
2653        assert!(ExpectedOutcome::Success.passes(301));
2654        assert!(!ExpectedOutcome::Success.passes(400));
2655        assert!(!ExpectedOutcome::Success.passes(415));
2656        assert!(!ExpectedOutcome::Success.passes(500));
2657        assert!(!ExpectedOutcome::Success.passes(0));
2658
2659        // ClientError (negative): only 4xx pass.
2660        assert!(!ExpectedOutcome::ClientError.passes(200));
2661        assert!(ExpectedOutcome::ClientError.passes(400));
2662        assert!(ExpectedOutcome::ClientError.passes(404));
2663        assert!(ExpectedOutcome::ClientError.passes(422));
2664        assert!(!ExpectedOutcome::ClientError.passes(500));
2665
2666        // NotServerError (variant-b): 2xx-4xx pass, 5xx fails.
2667        assert!(ExpectedOutcome::NotServerError.passes(200));
2668        assert!(ExpectedOutcome::NotServerError.passes(204));
2669        assert!(ExpectedOutcome::NotServerError.passes(400), "Srikanth's vCenter consolecli case: 400 from a pattern validator should NOT be a probe failure");
2670        assert!(ExpectedOutcome::NotServerError.passes(415));
2671        assert!(ExpectedOutcome::NotServerError.passes(422));
2672        assert!(
2673            !ExpectedOutcome::NotServerError.passes(500),
2674            "Server CRASH on embedded content is the only real failure"
2675        );
2676        assert!(!ExpectedOutcome::NotServerError.passes(502));
2677        assert!(!ExpectedOutcome::NotServerError.passes(503));
2678        // status 0 (network error / probe never reached the server) does not pass either
2679        assert!(!ExpectedOutcome::NotServerError.passes(0));
2680    }
2681
2682    /// Round 35 (#859) — the per-capture `expected_status_range`
2683    /// string is what the HTML viewer's "show mismatches only"
2684    /// filter and Srikanth's `jq` pipelines key off, so the new
2685    /// tristate must surface a third distinct value.
2686    #[test]
2687    fn expected_outcome_string_labels() {
2688        assert_eq!(ExpectedOutcome::Success.as_str(), "2xx-3xx");
2689        assert_eq!(ExpectedOutcome::ClientError.as_str(), "4xx");
2690        assert_eq!(ExpectedOutcome::NotServerError.as_str(), "2xx-4xx");
2691    }
2692
2693    /// Round 26 — Srikanth saw `at /: Type { kind: Single` in his
2694    /// 0.3.169 capture for the vCenter `infraprofile/configs` 202
2695    /// response (spec promised `type: string`, server returned a
2696    /// JSON object). The output was a broken-syntax debug string.
2697    /// This test reproduces his exact spec+body and asserts the
2698    /// message is readable.
2699    #[test]
2700    fn response_schema_error_message_is_readable() {
2701        let schema = serde_json::json!({"type": "string"});
2702        let body = r#"{"data":{},"id":"generated_id","status":"created"}"#;
2703        let err = validate_body_against_schema(body, &schema).expect("type-mismatch fires");
2704        // The message must NOT contain Rust debug syntax leftovers
2705        // ("Type { kind:", trailing "{" or "(" tokens). It SHOULD say
2706        // what type was expected.
2707        assert!(!err.contains("Type { kind"), "stale debug output: {err}");
2708        assert!(!err.contains("{ kind:"), "stale debug output: {err}");
2709        assert!(err.contains("string"), "should name expected type: {err}");
2710        // Round 29 — Srikanth on 0.3.172 was confused by `at /:`,
2711        // thinking it pointed to the URL path. The new format
2712        // explicitly says "response body root" for the root case
2713        // (and "response body at /<pointer>" for nested fields).
2714        assert!(
2715            err.contains("response body root"),
2716            "should label root explicitly so reader knows it's not the URL: {err}"
2717        );
2718        // Round 28 — Srikanth wanted the expected schema embedded
2719        // in the message so it reads as 'expected schema {"type":"string"}'.
2720        assert!(
2721            err.contains("expected schema") && err.contains("\"type\":\"string\""),
2722            "should include expected schema JSON: {err}"
2723        );
2724    }
2725
2726    /// Round 29 — for non-root paths the format reads
2727    /// "response body at /name: ...". Catches the case where the
2728    /// root rewording accidentally dropped the JSON-pointer for
2729    /// nested fields.
2730    #[test]
2731    fn response_schema_error_uses_response_body_prefix_for_nested_paths() {
2732        let schema = serde_json::json!({
2733            "type": "object",
2734            "required": ["name"],
2735            "properties": {"name": {"type": "string"}}
2736        });
2737        let body = r#"{"name": 123}"#;
2738        let err = validate_body_against_schema(body, &schema).expect("type-mismatch fires");
2739        assert!(
2740            err.contains("response body at /name"),
2741            "nested path should read 'response body at /name': {err}"
2742        );
2743        assert!(!err.contains("response body root"), "wrong label for nested: {err}");
2744        // Round 30 — the "expected schema" suffix should be the
2745        // sub-schema at /name, not the entire object schema. Reader
2746        // shouldn't have to scan a 300-char object to find the
2747        // constraint that failed.
2748        assert!(
2749            err.contains(r#"expected schema {"type":"string"}"#),
2750            "should show only the /name sub-schema, not the full object: {err}"
2751        );
2752    }
2753
2754    /// Round 30 — Srikanth asked how a deeper nested mismatch reads.
2755    /// Schema: `name.type` should be a string; body has it as a number.
2756    /// JSON pointer is `/name/type`.
2757    #[test]
2758    fn response_schema_error_uses_response_body_prefix_for_deep_nested_paths() {
2759        let schema = serde_json::json!({
2760            "type": "object",
2761            "properties": {
2762                "name": {
2763                    "type": "object",
2764                    "properties": {"type": {"type": "string"}}
2765                }
2766            }
2767        });
2768        let body = r#"{"name": {"type": 123}}"#;
2769        let err = validate_body_against_schema(body, &schema).expect("type-mismatch fires");
2770        assert!(
2771            err.contains("response body at /name/type"),
2772            "deep nested path should read 'response body at /name/type': {err}"
2773        );
2774        // Round 30 — for deep paths the sub-schema is the leaf
2775        // {"type":"string"}, not the wrapping object schemas.
2776        assert!(
2777            err.contains(r#"expected schema {"type":"string"}"#),
2778            "should show only the /name/type leaf sub-schema: {err}"
2779        );
2780    }
2781
2782    /// Round 30 — when the instance pointer can't be resolved through
2783    /// the schema's `properties` chain (e.g. additionalProperties hit),
2784    /// `sub_schema_at_pointer` returns None and the message falls back
2785    /// to the full schema. Verifies the fallback path is wired.
2786    #[test]
2787    fn sub_schema_at_pointer_falls_back_for_unresolvable_paths() {
2788        let schema = serde_json::json!({"type":"object","additionalProperties":true});
2789        // Walker can't resolve /unknown, so we get the full schema back.
2790        assert_eq!(
2791            sub_schema_at_pointer(&schema, "/unknown"),
2792            None,
2793            "unresolvable path should return None to trigger fallback"
2794        );
2795        // Root path returns the whole schema.
2796        assert_eq!(sub_schema_at_pointer(&schema, "/"), Some(schema.clone()));
2797        assert_eq!(sub_schema_at_pointer(&schema, ""), Some(schema));
2798    }
2799
2800    #[test]
2801    fn response_schema_error_required_field_is_readable() {
2802        let schema = serde_json::json!({
2803            "type": "object",
2804            "required": ["id"],
2805            "properties": {"id": {"type": "integer"}}
2806        });
2807        let body = r#"{"other": 1}"#;
2808        let err = validate_body_against_schema(body, &schema).expect("required-missing fires");
2809        assert!(err.contains("required field missing"), "{err}");
2810        assert!(err.contains("id"), "{err}");
2811    }
2812
2813    /// Round 31 — Srikanth's vCenter case on 0.3.174: the
2814    /// `Appliance.Recovery.Backup.SystemName.Archive.Info` schema has
2815    /// a multi-paragraph description and ~6 required fields, of which
2816    /// `comment` was missing in the response. Before this fix the
2817    /// printed schema was the WHOLE parent object schema (parent's
2818    /// description bleeding in, all sibling property schemas dumped)
2819    /// truncated to 300 chars; after the fix it's the missing field's
2820    /// own schema. Verifies (a) parent description is gone and
2821    /// (b) sibling property names don't appear in the message.
2822    #[test]
2823    fn response_schema_error_required_focuses_on_missing_field_only() {
2824        let schema = serde_json::json!({
2825            "description": "The Appliance.Recovery.Backup.SystemName.Archive.Info schema represents backup archive information.\n\nThis schema was added in vSphere API 6.7.",
2826            "type": "object",
2827            "required": ["comment", "location", "parts", "system_name", "timestamp", "version"],
2828            "properties": {
2829                "comment": {
2830                    "type": "string",
2831                    "description": "Custom comment added by the user for this backup."
2832                },
2833                "location": {"type": "string", "description": "Backup location URL."},
2834                "parts": {"type": "array", "items": {"type": "string"}},
2835                "system_name": {"type": "string"},
2836                "timestamp": {"type": "string", "format": "date-time"},
2837                "version": {"type": "string"}
2838            }
2839        });
2840        let body = r#"{"location":"x","parts":[],"system_name":"y","timestamp":"z","version":"v"}"#;
2841        let err = validate_body_against_schema(body, &schema).expect("required-missing fires");
2842        assert!(err.contains("required field missing: \"comment\""), "{err}");
2843        // Parent's description should not appear; only the `comment`
2844        // field's own description (if any) may.
2845        assert!(
2846            !err.contains("Appliance.Recovery.Backup"),
2847            "parent description should not bleed into focused schema: {err}"
2848        );
2849        // No sibling property names should appear in the focused schema
2850        // suffix.
2851        for sibling in ["location", "parts", "system_name", "timestamp", "version"] {
2852            assert!(
2853                !err.contains(&format!("\"{sibling}\"")),
2854                "sibling field {sibling} should not appear in focused schema: {err}"
2855            );
2856        }
2857    }
2858
2859    #[test]
2860    fn response_schema_error_none_on_match() {
2861        let schema = serde_json::json!({"type": "string"});
2862        assert_eq!(validate_body_against_schema("\"hello\"", &schema), None);
2863    }
2864
2865    /// Round 34 (#827) — Srikanth on 0.3.178 hit the vCenter
2866    /// `consolecli` PUT where the `enabled: boolean` property has a
2867    /// multi-paragraph description. The schema printout truncated
2868    /// mid-description, hiding `type: boolean` past the 300-char cap.
2869    /// Stripping `description` (and friends) before serializing must
2870    /// keep the type info visible.
2871    #[test]
2872    fn response_schema_error_strips_description_so_type_survives_truncation() {
2873        // Schema crafted so without stripping, `description` would
2874        // push `type` past the 300-char truncation cap. The
2875        // description we use here is intentionally close to the
2876        // vCenter-spec wording Srikanth quoted.
2877        let big_desc = "In the result of the #get and #list operations this property indicates whether proxying is enabled for a particular protocol. In the input to the test and set operations this property specifies whether proxying should be enabled for a particular protocol. This property was added in vSphere API 6.7. Defaults to enabled if both this field and the value field are unset.";
2878        let schema = serde_json::json!({
2879            "type": "object",
2880            "required": ["enabled"],
2881            "properties": {
2882                "enabled": {
2883                    "type": "boolean",
2884                    "description": big_desc,
2885                    "example": true,
2886                }
2887            }
2888        });
2889        let body = r#"{}"#;
2890        let err = validate_body_against_schema(body, &schema).expect("required-missing fires");
2891        assert!(err.contains("required field missing: \"enabled\""), "{err}");
2892        assert!(
2893            err.contains(r#""type":"boolean""#),
2894            "the `type: boolean` keyword must survive truncation: {err}"
2895        );
2896        // Description should NOT appear (we stripped it) so the
2897        // suffix is type-focused, not prose.
2898        assert!(
2899            !err.contains("proxying is enabled"),
2900            "description should be stripped from the printed schema: {err}"
2901        );
2902        assert!(
2903            !err.contains("\"example\""),
2904            "`example` field should be stripped from the printed schema: {err}"
2905        );
2906    }
2907
2908    /// Round 34 (#827) — strip_schema_noise should keep all
2909    /// constraint keywords intact; only the prose noise goes.
2910    #[test]
2911    fn strip_schema_noise_preserves_constraint_keywords() {
2912        let schema = serde_json::json!({
2913            "type": "object",
2914            "required": ["a", "b"],
2915            "description": "should be stripped",
2916            "title": "should be stripped",
2917            "example": {"a": 1, "b": 2},
2918            "properties": {
2919                "a": {"type": "string", "format": "uri", "minLength": 1, "description": "drop"},
2920                "b": {"type": "integer", "minimum": 0, "maximum": 100, "summary": "drop"},
2921            },
2922        });
2923        let stripped = strip_schema_noise(&schema);
2924        let s = serde_json::to_string(&stripped).unwrap();
2925        // Constraint keywords survive.
2926        for keep in [
2927            "\"type\"",
2928            "\"required\"",
2929            "\"properties\"",
2930            "\"format\"",
2931            "\"minLength\"",
2932            "\"minimum\"",
2933            "\"maximum\"",
2934        ] {
2935            assert!(s.contains(keep), "should keep {keep}: {s}");
2936        }
2937        // Noise fields are gone.
2938        for drop in ["description", "title", "example", "summary"] {
2939            assert!(!s.contains(&format!("\"{drop}\"")), "should strip {drop}: {s}");
2940        }
2941    }
2942
2943    #[test]
2944    fn json_serialises_report() {
2945        let r = SelfTestReport {
2946            positive_pass: 1,
2947            positive_fail: 0,
2948            negative_caught: BTreeMap::new(),
2949            negative_missed: BTreeMap::new(),
2950            operations: vec![OperationResult {
2951                method: "GET".into(),
2952                path: "/x".into(),
2953                positive: Some(CaseOutcome {
2954                    label: "positive".into(),
2955                    expected_4xx: false,
2956                    actual_status: 200,
2957                    passed: true,
2958                }),
2959                negatives: Vec::new(),
2960            }],
2961        };
2962        let json = serde_json::to_value(&r).expect("serialises");
2963        assert_eq!(json["positive_pass"], serde_json::json!(1));
2964        assert_eq!(json["operations"][0]["positive"]["actual_status"], serde_json::json!(200));
2965    }
2966}