Skip to main content

mockforge_bench/conformance/
generator.rs

1//! k6 script generator for OpenAPI 3.0.0 conformance testing
2
3use crate::error::{BenchError, Result};
4use std::path::{Path, PathBuf};
5
6use super::custom::CustomConformanceConfig;
7
8/// Configuration for conformance test generation
9#[derive(Default, Clone)]
10pub struct ConformanceConfig {
11    /// Target base URL
12    pub target_url: String,
13    /// API key for security scheme tests
14    pub api_key: Option<String>,
15    /// Basic auth credentials (user:pass) for security scheme tests
16    pub basic_auth: Option<String>,
17    /// Skip TLS verification
18    pub skip_tls_verify: bool,
19    /// Optional category filter — None means all categories
20    pub categories: Option<Vec<String>>,
21    /// Optional base path prefix for all generated URLs (e.g., "/api")
22    pub base_path: Option<String>,
23    /// Custom headers to inject into every conformance request (e.g., auth headers).
24    /// Each entry is (header_name, header_value). When a custom header matches
25    /// a spec-derived header name, the custom value replaces the placeholder.
26    pub custom_headers: Vec<(String, String)>,
27    /// Output directory for the conformance report (absolute path).
28    /// Used to write `conformance-report.json` to a deterministic location
29    /// so the CLI can find it after k6 execution.
30    pub output_dir: Option<PathBuf>,
31    /// When true, test ALL operations for method/response/body categories
32    /// instead of just one representative per feature check name.
33    pub all_operations: bool,
34    /// Optional path to a YAML file with custom conformance checks
35    pub custom_checks_file: Option<PathBuf>,
36    /// Delay in milliseconds between consecutive conformance requests.
37    /// Useful when testing against rate-limited APIs. Default: 0 (no delay).
38    pub request_delay_ms: u64,
39    /// Optional regex to filter custom checks by name or path.
40    /// Only checks whose name or path matches the regex are included.
41    pub custom_filter: Option<String>,
42    /// When true, export all request/response pairs to a JSON file
43    /// in the output directory (`conformance-requests.json`).
44    pub export_requests: bool,
45    /// When true, validate each request against the OpenAPI spec before
46    /// sending and report violations to `conformance-request-violations.json`.
47    pub validate_requests: bool,
48}
49
50impl ConformanceConfig {
51    /// Check if a category should be included based on the filter
52    pub fn should_include_category(&self, category: &str) -> bool {
53        match &self.categories {
54            None => true,
55            Some(cats) => cats.iter().any(|c| c.eq_ignore_ascii_case(category)),
56        }
57    }
58
59    /// Returns true if custom headers are configured
60    pub fn has_custom_headers(&self) -> bool {
61        !self.custom_headers.is_empty()
62    }
63
64    /// Returns true if custom headers contain a Cookie header.
65    /// When true, k6's automatic cookie jar should be disabled to prevent
66    /// duplicate cookies on subsequent requests.
67    pub fn has_cookie_header(&self) -> bool {
68        self.custom_headers.iter().any(|(k, _)| k.eq_ignore_ascii_case("cookie"))
69    }
70
71    /// Format custom headers as a JS object literal string
72    pub fn custom_headers_js_object(&self) -> String {
73        let entries: Vec<String> = self
74            .custom_headers
75            .iter()
76            .map(|(k, v)| format!("'{}': '{}'", k, v.replace('\'', "\\'")))
77            .collect();
78        format!("{{ {} }}", entries.join(", "))
79    }
80
81    /// Generate a k6 group block for custom checks, if configured.
82    /// Returns `Ok(None)` if no custom checks file is configured.
83    /// Respects `custom_filter` to include only matching checks.
84    ///
85    /// Round 39 (#79) — returns BOTH the init-scope code (e.g.
86    /// `const __file_0 = open('/path', 'b')` for file uploads) and
87    /// the group body. The caller splices `init_code` near the top of
88    /// the script (after `BASE_URL`, before `export default
89    /// function`) and `group_body` inside the default function. k6
90    /// requires `open()` to live at init scope.
91    pub fn generate_custom_group(
92        &self,
93    ) -> Result<Option<crate::conformance::custom::K6CustomEmit>> {
94        let path = match &self.custom_checks_file {
95            Some(p) => p,
96            None => return Ok(None),
97        };
98        let mut config = CustomConformanceConfig::from_file(path)?;
99        if config.custom_checks.is_empty() {
100            return Ok(None);
101        }
102
103        // Apply regex filter if provided
104        if let Some(ref pattern) = self.custom_filter {
105            let re = regex::Regex::new(pattern).map_err(|e| {
106                BenchError::Other(format!("Invalid --conformance-custom-filter regex: {}", e))
107            })?;
108            let total = config.custom_checks.len();
109            config.custom_checks.retain(|c| re.is_match(&c.name) || re.is_match(&c.path));
110            tracing::info!(
111                "Custom check filter: {}/{} checks matched pattern",
112                config.custom_checks.len(),
113                total
114            );
115            if config.custom_checks.is_empty() {
116                return Ok(None);
117            }
118        }
119
120        Ok(Some(config.emit_k6_with_options(
121            "BASE_URL",
122            &self.custom_headers,
123            self.export_requests,
124        )))
125    }
126
127    /// Returns the effective base URL with base_path appended.
128    /// Handles trailing/leading slash normalization to avoid double slashes.
129    /// Always trims trailing slashes from the result so that `${BASE_URL}/path`
130    /// never produces `//path`.
131    pub fn effective_base_url(&self) -> String {
132        let base = match &self.base_path {
133            None => self.target_url.trim_end_matches('/').to_string(),
134            Some(bp) if bp.is_empty() => self.target_url.trim_end_matches('/').to_string(),
135            Some(bp) => {
136                let url = self.target_url.trim_end_matches('/');
137                let path = if bp.starts_with('/') {
138                    bp.as_str()
139                } else {
140                    return format!("{}/{}", url, bp).trim_end_matches('/').to_string();
141                };
142                format!("{}{}", url, path).trim_end_matches('/').to_string()
143            }
144        };
145        base
146    }
147}
148
149/// Generates k6 scripts for OpenAPI 3.0.0 conformance testing
150pub struct ConformanceGenerator {
151    config: ConformanceConfig,
152}
153
154impl ConformanceGenerator {
155    pub fn new(config: ConformanceConfig) -> Self {
156        Self { config }
157    }
158
159    /// Generate the conformance test k6 script
160    pub fn generate(&self) -> Result<String> {
161        let mut script = String::with_capacity(16384);
162
163        // Imports
164        script.push_str("import http from 'k6/http';\n");
165        script.push_str("import { check, group } from 'k6';\n");
166        if self.config.request_delay_ms > 0 {
167            script.push_str("import { sleep } from 'k6';\n");
168        }
169        script.push('\n');
170
171        // Tell k6 that all HTTP status codes are "expected" in conformance mode.
172        // Without this, k6 counts 4xx responses (e.g. intentional 404 tests) as
173        // http_req_failed errors, producing a misleading error rate percentage.
174        script.push_str(
175            "http.setResponseCallback(http.expectedStatuses({ min: 100, max: 599 }));\n\n",
176        );
177
178        // Options: 1 VU, 1 iteration (functional test, not load test)
179        script.push_str("export const options = {\n");
180        script.push_str("  vus: 1,\n");
181        script.push_str("  iterations: 1,\n");
182        if self.config.skip_tls_verify {
183            script.push_str("  insecureSkipTLSVerify: true,\n");
184        }
185        script.push_str("  thresholds: {\n");
186        script.push_str("    checks: ['rate>0'],\n");
187        script.push_str("  },\n");
188        script.push_str("};\n\n");
189
190        // Base URL (includes base_path if configured)
191        script.push_str(&format!("const BASE_URL = '{}';\n\n", self.config.effective_base_url()));
192
193        // Delay between requests (seconds) to avoid rate limiting
194        if self.config.request_delay_ms > 0 {
195            script.push_str(&format!(
196                "const REQUEST_DELAY = {:.3};\n\n",
197                self.config.request_delay_ms as f64 / 1000.0
198            ));
199        }
200
201        // Helper: JSON headers
202        script.push_str("const JSON_HEADERS = { 'Content-Type': 'application/json' };\n\n");
203
204        // Round 39 (#79) — emit init-scope code (e.g. `open()` calls
205        // for file uploads in custom checks) here, before any
206        // function declarations. k6 requires `open()` to live at
207        // script init scope; placing it inside `export default
208        // function` is a runtime ReferenceError.
209        let custom_emit = self.config.generate_custom_group()?;
210        if let Some(emit) = &custom_emit {
211            if !emit.init_code.is_empty() {
212                script.push_str("// Round 39 (#79) — preloaded upload bytes for custom checks\n");
213                script.push_str(&emit.init_code);
214                script.push('\n');
215            }
216        }
217
218        // Failure detail collector — logs req/res info for failed checks via console.log
219        script.push_str("function __captureFailure(checkName, res, expected) {\n");
220        script.push_str("  let bodyStr = '';\n");
221        script.push_str("  try { if (res.body) { const __n = res.body.length; bodyStr = res.body.substring(0, 65536); if (__n > 65536) bodyStr = bodyStr + ' <truncated at 65536 bytes; full body was ' + __n + ' bytes>'; } else { bodyStr = ''; } } catch(e) { bodyStr = '<unreadable>'; }\n");
222        script.push_str("  let reqHeaders = {};\n");
223        script.push_str(
224            "  if (res.request && res.request.headers) { reqHeaders = res.request.headers; }\n",
225        );
226        script.push_str("  let reqBody = '';\n");
227        script.push_str("  if (res.request && res.request.body) { try { const __m = res.request.body.length; reqBody = res.request.body.substring(0, 65536); if (__m > 65536) reqBody = reqBody + ' <truncated at 65536 bytes; full body was ' + __m + ' bytes>'; } catch(e) {} }\n");
228        script.push_str("  console.log('MOCKFORGE_FAILURE:' + JSON.stringify({\n");
229        script.push_str("    check: checkName,\n");
230        script.push_str("    request: {\n");
231        script.push_str("      method: res.request ? res.request.method : 'unknown',\n");
232        script.push_str("      url: res.request ? res.request.url : res.url || 'unknown',\n");
233        script.push_str("      headers: reqHeaders,\n");
234        script.push_str("      body: reqBody,\n");
235        script.push_str("    },\n");
236        script.push_str("    response: {\n");
237        script.push_str("      status: res.status,\n");
238        script.push_str("      headers: res.headers ? Object.fromEntries(Object.entries(res.headers).slice(0, 20)) : {},\n");
239        script.push_str("      body: bodyStr,\n");
240        script.push_str("    },\n");
241        script.push_str("    expected: expected,\n");
242        script.push_str("  }));\n");
243        script.push_str("}\n\n");
244
245        // Request/response capture for --export-requests (uses console.log since
246        // k6's handleSummary runs in a separate JS context with no access to
247        // module-level variables — the CLI parses the output log after k6 exits).
248        //
249        // Round 44 (#79) — Srikanth on 0.3.188: he reported `MOCKFORGE_UPLOAD_PARTS`
250        // appearing in k6-output.log but `MOCKFORGE_EXCHANGE` for the same check
251        // missing entirely from `conformance-requests.json` / `-failure-details.json`.
252        // The most likely failure mode is an exception inside `JSON.stringify`
253        // (multipart bodies can include bytes that produce surrogate-half strings
254        // k6's stringifier chokes on; very large request URLs can also bust k6's
255        // console-line length). Wrap the entire payload build + stringify in a
256        // try/catch and ALWAYS emit a fallback `MOCKFORGE_EXCHANGE` line — even
257        // when stringify fails — so the request never silently disappears from
258        // the export. The fallback carries `check`, method, URL, status, and an
259        // `_export_error` flag so a downstream consumer can tell a degraded
260        // entry from a clean one.
261        if self.config.export_requests {
262            script.push_str("function __captureExchange(checkName, res) {\n");
263            script.push_str("  try {\n");
264            script.push_str("    let bodyStr = '';\n");
265            script.push_str("    try { if (res.body) { const __n = res.body.length; bodyStr = res.body.substring(0, 65536); if (__n > 65536) bodyStr = bodyStr + ' <truncated at 65536 bytes; full body was ' + __n + ' bytes>'; } else { bodyStr = ''; } } catch(e) { bodyStr = '<unreadable>'; }\n");
266            script.push_str("    let reqHeaders = {};\n");
267            script.push_str(
268                "    if (res.request && res.request.headers) { reqHeaders = res.request.headers; }\n",
269            );
270            // Round 41 (#79) — Srikanth on 0.3.185: "When run without
271            // Spec the export request file has blank entry". k6's
272            // `res.request.body` is empty for multipart uploads (k6
273            // serialises the form internally and the JS-side body
274            // string is null). Fall back to a content-type-derived
275            // summary so the export at least surfaces "multipart/form-data; N parts"
276            // instead of an empty string. Real bodies still surface
277            // unchanged.
278            // Round 46 (#79) — Srikanth on 0.3.190: a 13MB multipart
279            // upload landed `[]` in conformance-requests.json even
280            // though `MOCKFORGE_EXCHANGE:` was present in the k6 log.
281            // Root cause: k6's logfmt encoder doesn't fully escape
282            // binary bytes that JSON.stringify emits as raw chars
283            // (control codepoints above 0x1F, surrogate halves, etc),
284            // so the resulting line wasn't valid JSON-in-logfmt and the
285            // parser dropped it on the floor. Fix: when Content-Type is
286            // `multipart/`, NEVER include raw body bytes in the export.
287            // Walk the part boundaries server-side (in JS) and emit a
288            // structured summary list of `{name, filename, contentType,
289            // bytes}` per part, then a one-line preamble showing the
290            // boundary + total byte count. This survives JSON-stringify
291            // + logfmt cleanly and gives the user a strictly more
292            // useful view than the raw envelope ever did.
293            script.push_str("    let reqBody = '';\n");
294            script.push_str("    {\n");
295            script.push_str(
296                "      const ct = (reqHeaders['Content-Type'] || reqHeaders['content-type'] || '').toString();\n",
297            );
298            script.push_str("      const isMultipart = ct.startsWith('multipart/');\n");
299            script.push_str(
300                "      if (isMultipart && res.request && res.request.body) {\n\
301                 \x20\x20\x20\x20\x20\x20\x20\x20try {\n\
302                 \x20\x20\x20\x20\x20\x20\x20\x20  const raw = res.request.body;\n\
303                 \x20\x20\x20\x20\x20\x20\x20\x20  let totalBytes = raw.length;\n\
304                 \x20\x20\x20\x20\x20\x20\x20\x20  let envelopeBytes = 0;\n\
305                 \x20\x20\x20\x20\x20\x20\x20\x20  const boundaryMatch = ct.match(/boundary=([^;]+)/);\n\
306                 \x20\x20\x20\x20\x20\x20\x20\x20  const boundary = boundaryMatch ? boundaryMatch[1].replace(/^\"|\"$/g, '') : '';\n\
307                 \x20\x20\x20\x20\x20\x20\x20\x20  const parts = [];\n\
308                 \x20\x20\x20\x20\x20\x20\x20\x20  if (boundary) {\n\
309                 \x20\x20\x20\x20\x20\x20\x20\x20    const sep = '--' + boundary;\n\
310                 \x20\x20\x20\x20\x20\x20\x20\x20    let cursor = raw.indexOf(sep);\n\
311                 \x20\x20\x20\x20\x20\x20\x20\x20    while (cursor !== -1 && parts.length < 100) {\n\
312                 \x20\x20\x20\x20\x20\x20\x20\x20      const next = raw.indexOf(sep, cursor + sep.length);\n\
313                 \x20\x20\x20\x20\x20\x20\x20\x20      if (next === -1) break;\n\
314                 \x20\x20\x20\x20\x20\x20\x20\x20      const slice = raw.substring(cursor + sep.length, next);\n\
315                 \x20\x20\x20\x20\x20\x20\x20\x20      const headerEnd = slice.indexOf('\\r\\n\\r\\n');\n\
316                 \x20\x20\x20\x20\x20\x20\x20\x20      const partHeaders = headerEnd === -1 ? slice : slice.substring(0, headerEnd);\n\
317                 \x20\x20\x20\x20\x20\x20\x20\x20      const partBody = headerEnd === -1 ? '' : slice.substring(headerEnd + 4);\n\
318                 \x20\x20\x20\x20\x20\x20\x20\x20      // Round 50 #79 — the envelope (sep + part headers + the\n\
319                 \x20\x20\x20\x20\x20\x20\x20\x20      // header/body CRLFs + the trailing CRLF) is pure ASCII, so\n\
320                 \x20\x20\x20\x20\x20\x20\x20\x20      // its .length equals its byte count even when binary part\n\
321                 \x20\x20\x20\x20\x20\x20\x20\x20      // bodies mangle raw.length. sep=--boundary; +4 = header\n\
322                 \x20\x20\x20\x20\x20\x20\x20\x20      // separator CRLFCRLF; +2 = trailing CRLF after the body.\n\
323                 \x20\x20\x20\x20\x20\x20\x20\x20      envelopeBytes += sep.length + partHeaders.length + 6;\n\
324                 \x20\x20\x20\x20\x20\x20\x20\x20      const nameMatch = partHeaders.match(/name=\"([^\"]+)\"/);\n\
325                 \x20\x20\x20\x20\x20\x20\x20\x20      const filenameMatch = partHeaders.match(/filename=\"([^\"]+)\"/);\n\
326                 \x20\x20\x20\x20\x20\x20\x20\x20      const partCtMatch = partHeaders.match(/Content-Type:\\s*([^\\r\\n]+)/i);\n\
327                 \x20\x20\x20\x20\x20\x20\x20\x20      parts.push({\n\
328                 \x20\x20\x20\x20\x20\x20\x20\x20        name: nameMatch ? nameMatch[1] : '',\n\
329                 \x20\x20\x20\x20\x20\x20\x20\x20        filename: filenameMatch ? filenameMatch[1] : '',\n\
330                 \x20\x20\x20\x20\x20\x20\x20\x20        contentType: partCtMatch ? partCtMatch[1].trim() : '',\n\
331                 \x20\x20\x20\x20\x20\x20\x20\x20        bytes: Math.max(0, partBody.length - 2),\n\
332                 \x20\x20\x20\x20\x20\x20\x20\x20      });\n\
333                 \x20\x20\x20\x20\x20\x20\x20\x20      cursor = next;\n\
334                 \x20\x20\x20\x20\x20\x20\x20\x20    }\n\
335                 \x20\x20\x20\x20\x20\x20\x20\x20    // Closing boundary: --boundary--CRLF.\n\
336                 \x20\x20\x20\x20\x20\x20\x20\x20    if (parts.length) { envelopeBytes += sep.length + 4; }\n\
337                 \x20\x20\x20\x20\x20\x20\x20\x20  }\n\
338                 \x20\x20\x20\x20\x20\x20\x20\x20  // Round 47 #79 — overlay accurate on-disk byte counts from\n\
339                 \x20\x20\x20\x20\x20\x20\x20\x20  // the per-check size map written at init scope; falls back\n\
340                 \x20\x20\x20\x20\x20\x20\x20\x20  // to the JS-string-derived bytes when no entry exists.\n\
341                 \x20\x20\x20\x20\x20\x20\x20\x20  // Round 48 #79 — Srikanth on 0.3.192: per-file counts were\n\
342                 \x20\x20\x20\x20\x20\x20\x20\x20  // exact but the total was still off because we kept using\n\
343                 \x20\x20\x20\x20\x20\x20\x20\x20  // raw.length (UTF-16 code units). Recompute totalBytes as\n\
344                 \x20\x20\x20\x20\x20\x20\x20\x20  // the SUM of per-part bytes once they've been overlaid; only\n\
345                 \x20\x20\x20\x20\x20\x20\x20\x20  // every part's true byte count came from disk does the sum\n\
346                 \x20\x20\x20\x20\x20\x20\x20\x20  // equal the actual upload size (the multipart envelope\n\
347                 \x20\x20\x20\x20\x20\x20\x20\x20  // overhead bytes stay reported as the raw.length delta).\n\
348                 \x20\x20\x20\x20\x20\x20\x20\x20  const __mfSizes = (globalThis.__mfUploadSizes || {})[checkName] || {};\n\
349                 \x20\x20\x20\x20\x20\x20\x20\x20  let __allKnown = parts.length > 0;\n\
350                 \x20\x20\x20\x20\x20\x20\x20\x20  parts.forEach(function (p) { if (typeof __mfSizes[p.name] === 'number') { p.bytes = __mfSizes[p.name]; } else { __allKnown = false; } });\n\
351                 \x20\x20\x20\x20\x20\x20\x20\x20  const partsTotal = parts.reduce(function (acc, p) { return acc + p.bytes; }, 0);\n\
352                 \x20\x20\x20\x20\x20\x20\x20\x20  if (__allKnown) totalBytes = partsTotal;\n\
353                 \x20\x20\x20\x20\x20\x20\x20\x20  // Round 49 #79 — Srikanth on 0.3.193 asked why our proxy\n\
354                 \x20\x20\x20\x20\x20\x20\x20\x20  // counted 57998271 bytes vs mockforge's 57996316 (disk\n\
355                 \x20\x20\x20\x20\x20\x20\x20\x20  // sum). The diff is the multipart envelope (boundaries,\n\
356                 \x20\x20\x20\x20\x20\x20\x20\x20  // per-part Content-Disposition / Content-Type lines,\n\
357                 \x20\x20\x20\x20\x20\x20\x20\x20  // CRLFs, the final closing boundary). Surface both:\n\
358                 \x20\x20\x20\x20\x20\x20\x20\x20  // `total` stays the disk-sum payload (what a receiver\n\
359                 \x20\x20\x20\x20\x20\x20\x20\x20  // writes back to disk); `wire` adds the envelope so\n\
360                 \x20\x20\x20\x20\x20\x20\x20\x20  // packet captures / proxy byte counters match.\n\
361                 \x20\x20\x20\x20\x20\x20\x20\x20  // Round 50 #79 — Srikanth on 0.3.194 saw wire (56344432)\n\
362                 \x20\x20\x20\x20\x20\x20\x20\x20  // come out SMALLER than total (57996316). raw.length is a\n\
363                 \x20\x20\x20\x20\x20\x20\x20\x20  // UTF-8-decoded JS string, so binary part bytes collapse\n\
364                 \x20\x20\x20\x20\x20\x20\x20\x20  // and it UNDERcounts. The envelope is only ~2KB for 9\n\
365                 \x20\x20\x20\x20\x20\x20\x20\x20  // parts, so wire can never be less than total. Compute it\n\
366                 \x20\x20\x20\x20\x20\x20\x20\x20  // from the disk-accurate payload plus the ASCII envelope.\n\
367                 \x20\x20\x20\x20\x20\x20\x20\x20  const wireBytes = __allKnown ? (partsTotal + envelopeBytes) : ((typeof raw === 'string' && raw.length) ? raw.length : totalBytes);\n\
368                 \x20\x20\x20\x20\x20\x20\x20\x20  const summary = parts.map(function (p) { return '\\'' + p.name + '\\':\\'' + p.filename + '\\' (' + p.contentType + ', ' + p.bytes + ' bytes)'; }).join(', ');\n\
369                 \x20\x20\x20\x20\x20\x20\x20\x20  reqBody = '<multipart/form-data; boundary=' + boundary + '; ' + parts.length + ' part(s); total ' + totalBytes + ' bytes (wire ' + wireBytes + ' bytes w/ envelope): ' + summary + '>';\n\
370                 \x20\x20\x20\x20\x20\x20\x20\x20} catch (e) {\n\
371                 \x20\x20\x20\x20\x20\x20\x20\x20  reqBody = '<multipart upload; summary failed: ' + (e && e.message ? e.message : 'unknown') + '>';\n\
372                 \x20\x20\x20\x20\x20\x20\x20\x20}\n\
373                 \x20\x20\x20\x20\x20\x20} else if (isMultipart) {\n\
374                 \x20\x20\x20\x20\x20\x20\x20\x20reqBody = '<multipart upload; body bytes not surfaced by k6 res.request.body>';\n\
375                 \x20\x20\x20\x20\x20\x20} else if (res.request && res.request.body) {\n\
376                 \x20\x20\x20\x20\x20\x20\x20\x20try { const __m = res.request.body.length; reqBody = res.request.body.substring(0, 65536); if (__m > 65536) reqBody = reqBody + ' <truncated at 65536 bytes; full body was ' + __m + ' bytes>'; } catch (e) {}\n\
377                 \x20\x20\x20\x20\x20\x20}\n\
378                 \x20\x20\x20\x20}\n",
379            );
380            // Round 47 (#79) — emit a separate MOCKFORGE_NETWORK_EVENT
381            // line when the request never completed (k6 returns
382            // status=0 with an error_code/error string for connect /
383            // tls / timeout failures). The CLI executor harvests these
384            // into `conformance-network-events.json` for the k6 path,
385            // matching the native + self-test paths.
386            script.push_str(
387                "    if (res && res.status === 0) {\n\
388                 \x20\x20\x20\x20\x20\x20const ec = (res.error_code != null) ? res.error_code : 0;\n\
389                 \x20\x20\x20\x20\x20\x20const em = (res.error != null) ? String(res.error) : '';\n\
390                 \x20\x20\x20\x20\x20\x20// k6 error_code ranges: 1200s = TCP/DNS, 1300s = TLS, 1400s = timeout, 1500s = HTTP/2, others. Map coarsely.\n\
391                 \x20\x20\x20\x20\x20\x20let kind = 'other';\n\
392                 \x20\x20\x20\x20\x20\x20if (ec >= 1200 && ec < 1300) kind = 'connect';\n\
393                 \x20\x20\x20\x20\x20\x20else if (ec >= 1300 && ec < 1400) kind = 'tls';\n\
394                 \x20\x20\x20\x20\x20\x20else if (ec >= 1400 && ec < 1500) kind = 'timeout';\n\
395                 \x20\x20\x20\x20\x20\x20else if (em.toLowerCase().indexOf('eof') !== -1) kind = 'connect';\n                 \x20\x20\x20\x20\x20\x20else if (em.toLowerCase().indexOf('timeout') !== -1) kind = 'timeout';\n\
396                 \x20\x20\x20\x20\x20\x20else if (em.toLowerCase().indexOf('tls') !== -1) kind = 'tls';\n\
397                 \x20\x20\x20\x20\x20\x20else if (em.toLowerCase().indexOf('connect') !== -1 || em.toLowerCase().indexOf('refused') !== -1) kind = 'connect';\n\
398                 \x20\x20\x20\x20\x20\x20console.log('MOCKFORGE_NETWORK_EVENT:' + JSON.stringify({\n\
399                 \x20\x20\x20\x20\x20\x20  timestamp: new Date().toISOString(),\n\
400                 \x20\x20\x20\x20\x20\x20  check: checkName,\n\
401                 \x20\x20\x20\x20\x20\x20  method: res.request ? res.request.method : 'unknown',\n\
402                 \x20\x20\x20\x20\x20\x20  url: res.request ? res.request.url : res.url || 'unknown',\n\
403                 \x20\x20\x20\x20\x20\x20  kind: kind,\n\
404                 \x20\x20\x20\x20\x20\x20  error_code: ec,\n\
405                 \x20\x20\x20\x20\x20\x20  message: em,\n\
406                 \x20\x20\x20\x20\x20\x20}));\n\
407                 \x20\x20\x20\x20}\n",
408            );
409            script.push_str("    console.log('MOCKFORGE_EXCHANGE:' + JSON.stringify({\n");
410            script.push_str("      check: checkName,\n");
411            script.push_str("      request: {\n");
412            script.push_str("        method: res.request ? res.request.method : 'unknown',\n");
413            script.push_str("        url: res.request ? res.request.url : res.url || 'unknown',\n");
414            script.push_str("        headers: reqHeaders,\n");
415            script.push_str("        body: reqBody,\n");
416            script.push_str("      },\n");
417            script.push_str("      response: {\n");
418            script.push_str("        status: res.status,\n");
419            script.push_str("        headers: res.headers ? Object.fromEntries(Object.entries(res.headers).slice(0, 30)) : {},\n");
420            script.push_str("        body: bodyStr,\n");
421            script.push_str("      },\n");
422            script.push_str("    }));\n");
423            script.push_str("  } catch (e) {\n");
424            // Fallback path: still emit SOMETHING the parser can pick up
425            // so the request doesn't vanish from the export. Stays short
426            // on purpose — bigger payload was what tripped the primary
427            // path.
428            script.push_str("    try {\n");
429            script.push_str("      console.log('MOCKFORGE_EXCHANGE:' + JSON.stringify({\n");
430            script.push_str("        check: checkName,\n");
431            script.push_str("        request: {\n");
432            script.push_str(
433                "          method: (res && res.request) ? res.request.method : 'unknown',\n",
434            );
435            script.push_str("          url: (res && res.request) ? res.request.url : (res && res.url) || 'unknown',\n");
436            script.push_str("          headers: {},\n");
437            script.push_str("          body: '<exchange capture failed: ' + (e && e.message ? e.message : 'unknown error') + '>',\n");
438            script.push_str("        },\n");
439            script.push_str("        response: {\n");
440            script.push_str("          status: (res && res.status) || 0,\n");
441            script.push_str("          headers: {},\n");
442            script.push_str("          body: '',\n");
443            script.push_str("        },\n");
444            script.push_str("        _export_error: (e && e.message) ? e.message : String(e),\n");
445            script.push_str("      }));\n");
446            script.push_str("    } catch (e2) {\n");
447            // Last-resort: a hand-rolled JSON string so even if a
448            // second stringify fails, we still flag the failure.
449            script.push_str("      console.log('MOCKFORGE_EXCHANGE:{\"check\":\"' + checkName + '\",\"request\":{\"method\":\"unknown\",\"url\":\"unknown\",\"headers\":{},\"body\":\"\"},\"response\":{\"status\":0,\"headers\":{},\"body\":\"\"},\"_export_error\":\"double-fault\"}');\n");
450            script.push_str("    }\n");
451            script.push_str("  }\n");
452            script.push_str("}\n\n");
453        }
454
455        // Default function
456        script.push_str("export default function () {\n");
457
458        if self.config.has_cookie_header() {
459            script.push_str(
460                "  // Clear cookie jar to prevent server Set-Cookie from duplicating custom Cookie header\n",
461            );
462            script.push_str("  http.cookieJar().clear(BASE_URL);\n\n");
463        }
464
465        // Helper to insert a delay between groups when --conformance-delay is set
466        let delay_between = if self.config.request_delay_ms > 0 {
467            "  sleep(REQUEST_DELAY);\n".to_string()
468        } else {
469            String::new()
470        };
471
472        // Round 39 (#79) — Srikanth on 0.3.183: "In the exported
473        // request I see it is sending request to
474        // api/conformance/params/hello and some other URLs". When the
475        // user passed `--conformance-custom` without `--spec`, the
476        // generator still emitted the 47 built-in reference checks
477        // against `/conformance/...` paths, which 404 on a real
478        // target. Skip them when custom checks are the only input —
479        // matching the native executor's `custom_only` branch.
480        let custom_only = self.config.custom_checks_file.is_some()
481            && !self.config.target_url.is_empty()
482            // Reference checks ARE the right answer when the user
483            // explicitly listed categories with --conformance-category.
484            && self.config.categories.is_none();
485        if !custom_only {
486            if self.config.should_include_category("Parameters") {
487                self.generate_parameters_group(&mut script);
488                script.push_str(&delay_between);
489            }
490            if self.config.should_include_category("Request Bodies") {
491                self.generate_request_bodies_group(&mut script);
492                script.push_str(&delay_between);
493            }
494            if self.config.should_include_category("Schema Types") {
495                self.generate_schema_types_group(&mut script);
496                script.push_str(&delay_between);
497            }
498            if self.config.should_include_category("Composition") {
499                self.generate_composition_group(&mut script);
500                script.push_str(&delay_between);
501            }
502            if self.config.should_include_category("String Formats") {
503                self.generate_string_formats_group(&mut script);
504                script.push_str(&delay_between);
505            }
506            if self.config.should_include_category("Constraints") {
507                self.generate_constraints_group(&mut script);
508                script.push_str(&delay_between);
509            }
510            if self.config.should_include_category("Response Codes") {
511                self.generate_response_codes_group(&mut script);
512                script.push_str(&delay_between);
513            }
514            if self.config.should_include_category("HTTP Methods") {
515                self.generate_http_methods_group(&mut script);
516                script.push_str(&delay_between);
517            }
518            if self.config.should_include_category("Content Types") {
519                self.generate_content_negotiation_group(&mut script);
520                script.push_str(&delay_between);
521            }
522            if self.config.should_include_category("Security") {
523                self.generate_security_group(&mut script);
524            }
525        }
526
527        // Custom checks from YAML file — round 39: we already called
528        // `generate_custom_group()` above to emit init-scope code, so
529        // here we just splice the group body inside the default
530        // function.
531        if let Some(emit) = custom_emit {
532            script.push_str(&emit.group_body);
533        }
534
535        script.push_str("}\n\n");
536
537        // handleSummary for conformance report output
538        self.generate_handle_summary(&mut script);
539
540        Ok(script)
541    }
542
543    /// Write the generated script to a file
544    pub fn write_script(&self, path: &Path) -> Result<()> {
545        let script = self.generate()?;
546        if let Some(parent) = path.parent() {
547            std::fs::create_dir_all(parent)?;
548        }
549        std::fs::write(path, script)
550            .map_err(|e| BenchError::Other(format!("Failed to write conformance script: {}", e)))
551    }
552
553    /// Returns a JS expression for merging custom headers with provided headers.
554    /// If no custom headers, returns the input as-is.
555    /// If custom headers exist, wraps with Object.assign using inline header object.
556    fn merge_with_custom_headers(&self, headers_expr: &str) -> String {
557        if self.config.has_custom_headers() {
558            format!(
559                "Object.assign({{}}, {}, {})",
560                headers_expr,
561                self.config.custom_headers_js_object()
562            )
563        } else {
564            headers_expr.to_string()
565        }
566    }
567
568    /// Emit a GET request with optional custom headers merged in.
569    fn emit_get(&self, script: &mut String, url: &str, extra_headers: Option<&str>) {
570        let has_custom = self.config.has_custom_headers();
571        let custom_obj = self.config.custom_headers_js_object();
572        match (extra_headers, has_custom) {
573            (None, false) => {
574                script.push_str(&format!("      let res = http.get(`{}`);\n", url));
575            }
576            (None, true) => {
577                script.push_str(&format!(
578                    "      let res = http.get(`{}`, {{ headers: {} }});\n",
579                    url, custom_obj
580                ));
581            }
582            (Some(hdrs), false) => {
583                script.push_str(&format!(
584                    "      let res = http.get(`{}`, {{ headers: {} }});\n",
585                    url, hdrs
586                ));
587            }
588            (Some(hdrs), true) => {
589                script.push_str(&format!(
590                    "      let res = http.get(`{}`, {{ headers: Object.assign({{}}, {}, {}) }});\n",
591                    url, hdrs, custom_obj
592                ));
593            }
594        }
595        self.maybe_clear_cookie_jar(script);
596        self.maybe_capture_exchange(script);
597    }
598
599    /// Emit a POST/PUT/PATCH request with optional custom headers merged in.
600    fn emit_post_like(
601        &self,
602        script: &mut String,
603        method: &str,
604        url: &str,
605        body: &str,
606        headers_expr: &str,
607    ) {
608        let merged = self.merge_with_custom_headers(headers_expr);
609        script.push_str(&format!(
610            "      let res = http.{}(`{}`, {}, {{ headers: {} }});\n",
611            method, url, body, merged
612        ));
613        self.maybe_clear_cookie_jar(script);
614        self.maybe_capture_exchange(script);
615    }
616
617    /// Emit a DELETE/HEAD/OPTIONS request with optional custom headers.
618    fn emit_no_body(&self, script: &mut String, method: &str, url: &str) {
619        if self.config.has_custom_headers() {
620            script.push_str(&format!(
621                "      let res = http.{}(`{}`, {{ headers: {} }});\n",
622                method,
623                url,
624                self.config.custom_headers_js_object()
625            ));
626        } else {
627            script.push_str(&format!("      let res = http.{}(`{}`);\n", method, url));
628        }
629        self.maybe_clear_cookie_jar(script);
630        self.maybe_capture_exchange(script);
631    }
632
633    /// Emit `__captureExchange` call when `--export-requests` is enabled.
634    fn maybe_capture_exchange(&self, script: &mut String) {
635        if self.config.export_requests {
636            script.push_str(
637                "      if (typeof __captureExchange === 'function') __captureExchange('', res);\n",
638            );
639        }
640    }
641
642    /// Emit cookie jar clearing after a request when custom Cookie headers are used.
643    /// Prevents k6's internal cookie jar from re-sending server Set-Cookie values
644    /// alongside the custom Cookie header on subsequent requests.
645    fn maybe_clear_cookie_jar(&self, script: &mut String) {
646        if self.config.has_cookie_header() {
647            script.push_str("      http.cookieJar().clear(BASE_URL);\n");
648        }
649    }
650
651    fn generate_parameters_group(&self, script: &mut String) {
652        script.push_str("  group('Parameters', function () {\n");
653
654        // Path param: string
655        script.push_str("    {\n");
656        self.emit_get(script, "${BASE_URL}/conformance/params/hello", None);
657        script.push_str(
658            "      check(res, { 'param:path:string': (r) => r.status >= 200 && r.status < 500 });\n",
659        );
660        script.push_str("    }\n");
661
662        // Path param: integer
663        script.push_str("    {\n");
664        self.emit_get(script, "${BASE_URL}/conformance/params/42", None);
665        script.push_str(
666            "      check(res, { 'param:path:integer': (r) => r.status >= 200 && r.status < 500 });\n",
667        );
668        script.push_str("    }\n");
669
670        // Query param: string
671        script.push_str("    {\n");
672        self.emit_get(script, "${BASE_URL}/conformance/params/query?name=test", None);
673        script.push_str(
674            "      check(res, { 'param:query:string': (r) => r.status >= 200 && r.status < 500 });\n",
675        );
676        script.push_str("    }\n");
677
678        // Query param: integer
679        script.push_str("    {\n");
680        self.emit_get(script, "${BASE_URL}/conformance/params/query?count=10", None);
681        script.push_str(
682            "      check(res, { 'param:query:integer': (r) => r.status >= 200 && r.status < 500 });\n",
683        );
684        script.push_str("    }\n");
685
686        // Query param: array
687        script.push_str("    {\n");
688        self.emit_get(script, "${BASE_URL}/conformance/params/query?tags=a&tags=b", None);
689        script.push_str(
690            "      check(res, { 'param:query:array': (r) => r.status >= 200 && r.status < 500 });\n",
691        );
692        script.push_str("    }\n");
693
694        // Header param
695        script.push_str("    {\n");
696        self.emit_get(
697            script,
698            "${BASE_URL}/conformance/params/header",
699            Some("{ 'X-Custom-Param': 'test-value' }"),
700        );
701        script.push_str(
702            "      check(res, { 'param:header': (r) => r.status >= 200 && r.status < 500 });\n",
703        );
704        script.push_str("    }\n");
705
706        // Cookie param
707        script.push_str("    {\n");
708        script.push_str("      let jar = http.cookieJar();\n");
709        script.push_str("      jar.set(BASE_URL, 'session', 'abc123');\n");
710        self.emit_get(script, "${BASE_URL}/conformance/params/cookie", None);
711        script.push_str(
712            "      check(res, { 'param:cookie': (r) => r.status >= 200 && r.status < 500 });\n",
713        );
714        script.push_str("    }\n");
715
716        script.push_str("  });\n\n");
717    }
718
719    fn generate_request_bodies_group(&self, script: &mut String) {
720        script.push_str("  group('Request Bodies', function () {\n");
721
722        // JSON body
723        script.push_str("    {\n");
724        self.emit_post_like(
725            script,
726            "post",
727            "${BASE_URL}/conformance/body/json",
728            "JSON.stringify({ name: 'test', value: 42 })",
729            "JSON_HEADERS",
730        );
731        script.push_str(
732            "      check(res, { 'body:json': (r) => r.status >= 200 && r.status < 500 });\n",
733        );
734        script.push_str("    }\n");
735
736        // Form-urlencoded body
737        script.push_str("    {\n");
738        if self.config.has_custom_headers() {
739            script.push_str(&format!(
740                "      let res = http.post(`${{BASE_URL}}/conformance/body/form`, {{ field1: 'value1', field2: 'value2' }}, {{ headers: {} }});\n",
741                self.config.custom_headers_js_object()
742            ));
743        } else {
744            script.push_str(
745                "      let res = http.post(`${BASE_URL}/conformance/body/form`, { field1: 'value1', field2: 'value2' });\n",
746            );
747        }
748        self.maybe_clear_cookie_jar(script);
749        script.push_str(
750            "      check(res, { 'body:form-urlencoded': (r) => r.status >= 200 && r.status < 500 });\n",
751        );
752        script.push_str("    }\n");
753
754        // Multipart body
755        script.push_str("    {\n");
756        script.push_str(
757            "      let data = { field: http.file('test content', 'test.txt', 'text/plain') };\n",
758        );
759        if self.config.has_custom_headers() {
760            script.push_str(&format!(
761                "      let res = http.post(`${{BASE_URL}}/conformance/body/multipart`, data, {{ headers: {} }});\n",
762                self.config.custom_headers_js_object()
763            ));
764        } else {
765            script.push_str(
766                "      let res = http.post(`${BASE_URL}/conformance/body/multipart`, data);\n",
767            );
768        }
769        self.maybe_clear_cookie_jar(script);
770        script.push_str(
771            "      check(res, { 'body:multipart': (r) => r.status >= 200 && r.status < 500 });\n",
772        );
773        script.push_str("    }\n");
774
775        script.push_str("  });\n\n");
776    }
777
778    fn generate_schema_types_group(&self, script: &mut String) {
779        script.push_str("  group('Schema Types', function () {\n");
780
781        let types = [
782            ("string", r#"{ "value": "hello" }"#, "schema:string"),
783            ("integer", r#"{ "value": 42 }"#, "schema:integer"),
784            ("number", r#"{ "value": 3.14 }"#, "schema:number"),
785            ("boolean", r#"{ "value": true }"#, "schema:boolean"),
786            ("array", r#"{ "value": [1, 2, 3] }"#, "schema:array"),
787            ("object", r#"{ "value": { "nested": "data" } }"#, "schema:object"),
788        ];
789
790        for (type_name, body, check_name) in types {
791            script.push_str("    {\n");
792            let url = format!("${{BASE_URL}}/conformance/schema/{}", type_name);
793            let body_str = format!("'{}'", body);
794            self.emit_post_like(script, "post", &url, &body_str, "JSON_HEADERS");
795            script.push_str(&format!(
796                "      check(res, {{ '{}': (r) => r.status >= 200 && r.status < 500 }});\n",
797                check_name
798            ));
799            script.push_str("    }\n");
800        }
801
802        script.push_str("  });\n\n");
803    }
804
805    fn generate_composition_group(&self, script: &mut String) {
806        script.push_str("  group('Composition', function () {\n");
807
808        let compositions = [
809            ("oneOf", r#"{ "type": "string", "value": "test" }"#, "composition:oneOf"),
810            ("anyOf", r#"{ "value": "test" }"#, "composition:anyOf"),
811            ("allOf", r#"{ "name": "test", "id": 1 }"#, "composition:allOf"),
812        ];
813
814        for (kind, body, check_name) in compositions {
815            script.push_str("    {\n");
816            let url = format!("${{BASE_URL}}/conformance/composition/{}", kind);
817            let body_str = format!("'{}'", body);
818            self.emit_post_like(script, "post", &url, &body_str, "JSON_HEADERS");
819            script.push_str(&format!(
820                "      check(res, {{ '{}': (r) => r.status >= 200 && r.status < 500 }});\n",
821                check_name
822            ));
823            script.push_str("    }\n");
824        }
825
826        script.push_str("  });\n\n");
827    }
828
829    fn generate_string_formats_group(&self, script: &mut String) {
830        script.push_str("  group('String Formats', function () {\n");
831
832        let formats = [
833            ("date", r#"{ "value": "2024-01-15" }"#, "format:date"),
834            ("date-time", r#"{ "value": "2024-01-15T10:30:00Z" }"#, "format:date-time"),
835            ("email", r#"{ "value": "test@example.com" }"#, "format:email"),
836            ("uuid", r#"{ "value": "550e8400-e29b-41d4-a716-446655440000" }"#, "format:uuid"),
837            ("uri", r#"{ "value": "https://example.com/path" }"#, "format:uri"),
838            ("ipv4", r#"{ "value": "192.168.1.1" }"#, "format:ipv4"),
839            ("ipv6", r#"{ "value": "::1" }"#, "format:ipv6"),
840        ];
841
842        for (fmt, body, check_name) in formats {
843            script.push_str("    {\n");
844            let url = format!("${{BASE_URL}}/conformance/formats/{}", fmt);
845            let body_str = format!("'{}'", body);
846            self.emit_post_like(script, "post", &url, &body_str, "JSON_HEADERS");
847            script.push_str(&format!(
848                "      check(res, {{ '{}': (r) => r.status >= 200 && r.status < 500 }});\n",
849                check_name
850            ));
851            script.push_str("    }\n");
852        }
853
854        script.push_str("  });\n\n");
855    }
856
857    fn generate_constraints_group(&self, script: &mut String) {
858        script.push_str("  group('Constraints', function () {\n");
859
860        let constraints = [
861            (
862                "required",
863                "JSON.stringify({ required_field: 'present' })",
864                "constraint:required",
865            ),
866            ("optional", "JSON.stringify({})", "constraint:optional"),
867            ("minmax", "JSON.stringify({ value: 50 })", "constraint:minmax"),
868            ("pattern", "JSON.stringify({ value: 'ABC-123' })", "constraint:pattern"),
869            ("enum", "JSON.stringify({ status: 'active' })", "constraint:enum"),
870        ];
871
872        for (kind, body, check_name) in constraints {
873            script.push_str("    {\n");
874            let url = format!("${{BASE_URL}}/conformance/constraints/{}", kind);
875            self.emit_post_like(script, "post", &url, body, "JSON_HEADERS");
876            script.push_str(&format!(
877                "      check(res, {{ '{}': (r) => r.status >= 200 && r.status < 500 }});\n",
878                check_name
879            ));
880            script.push_str("    }\n");
881        }
882
883        script.push_str("  });\n\n");
884    }
885
886    fn generate_response_codes_group(&self, script: &mut String) {
887        script.push_str("  group('Response Codes', function () {\n");
888
889        let codes = [
890            ("200", "response:200"),
891            ("201", "response:201"),
892            ("204", "response:204"),
893            ("400", "response:400"),
894            ("404", "response:404"),
895        ];
896
897        for (code, check_name) in codes {
898            script.push_str("    {\n");
899            let url = format!("${{BASE_URL}}/conformance/responses/{}", code);
900            self.emit_get(script, &url, None);
901            script.push_str(&format!(
902                "      check(res, {{ '{}': (r) => r.status === {} }});\n",
903                check_name, code
904            ));
905            script.push_str("    }\n");
906        }
907
908        script.push_str("  });\n\n");
909    }
910
911    fn generate_http_methods_group(&self, script: &mut String) {
912        script.push_str("  group('HTTP Methods', function () {\n");
913
914        // GET
915        script.push_str("    {\n");
916        self.emit_get(script, "${BASE_URL}/conformance/methods", None);
917        script.push_str(
918            "      check(res, { 'method:GET': (r) => r.status >= 200 && r.status < 500 });\n",
919        );
920        script.push_str("    }\n");
921
922        // POST
923        script.push_str("    {\n");
924        self.emit_post_like(
925            script,
926            "post",
927            "${BASE_URL}/conformance/methods",
928            "JSON.stringify({ action: 'create' })",
929            "JSON_HEADERS",
930        );
931        script.push_str(
932            "      check(res, { 'method:POST': (r) => r.status >= 200 && r.status < 500 });\n",
933        );
934        script.push_str("    }\n");
935
936        // PUT
937        script.push_str("    {\n");
938        self.emit_post_like(
939            script,
940            "put",
941            "${BASE_URL}/conformance/methods",
942            "JSON.stringify({ action: 'update' })",
943            "JSON_HEADERS",
944        );
945        script.push_str(
946            "      check(res, { 'method:PUT': (r) => r.status >= 200 && r.status < 500 });\n",
947        );
948        script.push_str("    }\n");
949
950        // PATCH
951        script.push_str("    {\n");
952        self.emit_post_like(
953            script,
954            "patch",
955            "${BASE_URL}/conformance/methods",
956            "JSON.stringify({ action: 'patch' })",
957            "JSON_HEADERS",
958        );
959        script.push_str(
960            "      check(res, { 'method:PATCH': (r) => r.status >= 200 && r.status < 500 });\n",
961        );
962        script.push_str("    }\n");
963
964        // DELETE
965        script.push_str("    {\n");
966        self.emit_no_body(script, "del", "${BASE_URL}/conformance/methods");
967        script.push_str(
968            "      check(res, { 'method:DELETE': (r) => r.status >= 200 && r.status < 500 });\n",
969        );
970        script.push_str("    }\n");
971
972        // HEAD
973        script.push_str("    {\n");
974        self.emit_no_body(script, "head", "${BASE_URL}/conformance/methods");
975        script.push_str(
976            "      check(res, { 'method:HEAD': (r) => r.status >= 200 && r.status < 500 });\n",
977        );
978        script.push_str("    }\n");
979
980        // OPTIONS
981        script.push_str("    {\n");
982        self.emit_no_body(script, "options", "${BASE_URL}/conformance/methods");
983        script.push_str(
984            "      check(res, { 'method:OPTIONS': (r) => r.status >= 200 && r.status < 500 });\n",
985        );
986        script.push_str("    }\n");
987
988        script.push_str("  });\n\n");
989    }
990
991    fn generate_content_negotiation_group(&self, script: &mut String) {
992        script.push_str("  group('Content Types', function () {\n");
993
994        script.push_str("    {\n");
995        self.emit_get(
996            script,
997            "${BASE_URL}/conformance/content-types",
998            Some("{ 'Accept': 'application/json' }"),
999        );
1000        script.push_str(
1001            "      check(res, { 'content:negotiation': (r) => r.status >= 200 && r.status < 500 });\n",
1002        );
1003        script.push_str("    }\n");
1004
1005        script.push_str("  });\n\n");
1006    }
1007
1008    fn generate_security_group(&self, script: &mut String) {
1009        script.push_str("  group('Security', function () {\n");
1010
1011        // Bearer token
1012        script.push_str("    {\n");
1013        self.emit_get(
1014            script,
1015            "${BASE_URL}/conformance/security/bearer",
1016            Some("{ 'Authorization': 'Bearer test-token-123' }"),
1017        );
1018        script.push_str(
1019            "      check(res, { 'security:bearer': (r) => r.status >= 200 && r.status < 500 });\n",
1020        );
1021        script.push_str("    }\n");
1022
1023        // API Key
1024        let api_key = self.config.api_key.as_deref().unwrap_or("test-api-key-123");
1025        script.push_str("    {\n");
1026        let api_key_hdrs = format!("{{ 'X-API-Key': '{}' }}", api_key);
1027        self.emit_get(script, "${BASE_URL}/conformance/security/apikey", Some(&api_key_hdrs));
1028        script.push_str(
1029            "      check(res, { 'security:apikey': (r) => r.status >= 200 && r.status < 500 });\n",
1030        );
1031        script.push_str("    }\n");
1032
1033        // Basic auth
1034        let basic_creds = self.config.basic_auth.as_deref().unwrap_or("user:pass");
1035        let encoded = base64_encode(basic_creds);
1036        script.push_str("    {\n");
1037        let basic_hdrs = format!("{{ 'Authorization': 'Basic {}' }}", encoded);
1038        self.emit_get(script, "${BASE_URL}/conformance/security/basic", Some(&basic_hdrs));
1039        script.push_str(
1040            "      check(res, { 'security:basic': (r) => r.status >= 200 && r.status < 500 });\n",
1041        );
1042        script.push_str("    }\n");
1043
1044        script.push_str("  });\n\n");
1045    }
1046
1047    fn generate_handle_summary(&self, script: &mut String) {
1048        // Determine the report output path. When output_dir is set, use an absolute
1049        // path so k6 writes the file where the CLI expects to find it regardless of CWD.
1050        let report_path = match &self.config.output_dir {
1051            Some(dir) => {
1052                let abs = std::fs::canonicalize(dir)
1053                    .unwrap_or_else(|_| dir.clone())
1054                    .join("conformance-report.json");
1055                abs.to_string_lossy().to_string()
1056            }
1057            None => "conformance-report.json".to_string(),
1058        };
1059
1060        script.push_str("export function handleSummary(data) {\n");
1061        script.push_str("  // Extract check results for conformance reporting\n");
1062        script.push_str("  let checks = {};\n");
1063        script.push_str("  if (data.metrics && data.metrics.checks) {\n");
1064        script.push_str("    // Overall check pass rate\n");
1065        script.push_str("    checks.overall_pass_rate = data.metrics.checks.values.rate;\n");
1066        script.push_str("  }\n");
1067        script.push_str("  // Collect per-check results from root_group\n");
1068        script.push_str("  let checkResults = {};\n");
1069        script.push_str("  function walkGroups(group) {\n");
1070        script.push_str("    if (group.checks) {\n");
1071        script.push_str("      for (let checkObj of group.checks) {\n");
1072        script.push_str("        checkResults[checkObj.name] = {\n");
1073        script.push_str("          passes: checkObj.passes,\n");
1074        script.push_str("          fails: checkObj.fails,\n");
1075        script.push_str("        };\n");
1076        script.push_str("      }\n");
1077        script.push_str("    }\n");
1078        script.push_str("    if (group.groups) {\n");
1079        script.push_str("      for (let subGroup of group.groups) {\n");
1080        script.push_str("        walkGroups(subGroup);\n");
1081        script.push_str("      }\n");
1082        script.push_str("    }\n");
1083        script.push_str("  }\n");
1084        script.push_str("  if (data.root_group) {\n");
1085        script.push_str("    walkGroups(data.root_group);\n");
1086        script.push_str("  }\n");
1087        script.push_str("  let result = {\n");
1088        script.push_str(&format!(
1089            "    '{}': JSON.stringify({{ checks: checkResults, overall: checks }}, null, 2),\n",
1090            report_path
1091        ));
1092        script.push_str("    'summary.json': JSON.stringify(data),\n");
1093        script.push_str("    stdout: textSummary(data, { indent: '  ', enableColors: true }),\n");
1094        script.push_str("  };\n");
1095        script.push_str("  return result;\n");
1096        script.push_str("}\n\n");
1097        script.push_str("// textSummary fallback\n");
1098        script.push_str("function textSummary(data, opts) {\n");
1099        script.push_str("  return JSON.stringify(data, null, 2);\n");
1100        script.push_str("}\n");
1101    }
1102}
1103
1104/// Simple base64 encoding for basic auth
1105fn base64_encode(input: &str) -> String {
1106    const CHARS: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1107    let bytes = input.as_bytes();
1108    let mut result = String::with_capacity(bytes.len().div_ceil(3) * 4);
1109    for chunk in bytes.chunks(3) {
1110        let b0 = chunk[0] as u32;
1111        let b1 = if chunk.len() > 1 { chunk[1] as u32 } else { 0 };
1112        let b2 = if chunk.len() > 2 { chunk[2] as u32 } else { 0 };
1113        let triple = (b0 << 16) | (b1 << 8) | b2;
1114        result.push(CHARS[((triple >> 18) & 0x3F) as usize] as char);
1115        result.push(CHARS[((triple >> 12) & 0x3F) as usize] as char);
1116        if chunk.len() > 1 {
1117            result.push(CHARS[((triple >> 6) & 0x3F) as usize] as char);
1118        } else {
1119            result.push('=');
1120        }
1121        if chunk.len() > 2 {
1122            result.push(CHARS[(triple & 0x3F) as usize] as char);
1123        } else {
1124            result.push('=');
1125        }
1126    }
1127    result
1128}
1129
1130#[cfg(test)]
1131mod tests {
1132    use super::*;
1133
1134    #[test]
1135    fn test_generate_conformance_script() {
1136        let config = ConformanceConfig {
1137            target_url: "http://localhost:8080".to_string(),
1138            api_key: None,
1139            basic_auth: None,
1140            skip_tls_verify: false,
1141            categories: None,
1142            base_path: None,
1143            custom_headers: vec![],
1144            output_dir: None,
1145            all_operations: false,
1146            custom_checks_file: None,
1147            request_delay_ms: 0,
1148            custom_filter: None,
1149            export_requests: false,
1150            validate_requests: false,
1151        };
1152        let generator = ConformanceGenerator::new(config);
1153        let script = generator.generate().unwrap();
1154
1155        assert!(script.contains("import http from 'k6/http'"));
1156        assert!(script.contains("vus: 1"));
1157        assert!(script.contains("iterations: 1"));
1158        assert!(script.contains("group('Parameters'"));
1159        assert!(script.contains("group('Request Bodies'"));
1160        assert!(script.contains("group('Schema Types'"));
1161        assert!(script.contains("group('Composition'"));
1162        assert!(script.contains("group('String Formats'"));
1163        assert!(script.contains("group('Constraints'"));
1164        assert!(script.contains("group('Response Codes'"));
1165        assert!(script.contains("group('HTTP Methods'"));
1166        assert!(script.contains("group('Content Types'"));
1167        assert!(script.contains("group('Security'"));
1168        assert!(script.contains("handleSummary"));
1169    }
1170
1171    #[test]
1172    fn test_base64_encode() {
1173        assert_eq!(base64_encode("user:pass"), "dXNlcjpwYXNz");
1174        assert_eq!(base64_encode("a"), "YQ==");
1175        assert_eq!(base64_encode("ab"), "YWI=");
1176        assert_eq!(base64_encode("abc"), "YWJj");
1177    }
1178
1179    #[test]
1180    fn test_conformance_script_with_custom_auth() {
1181        let config = ConformanceConfig {
1182            target_url: "https://api.example.com".to_string(),
1183            api_key: Some("my-api-key".to_string()),
1184            basic_auth: Some("admin:secret".to_string()),
1185            skip_tls_verify: true,
1186            categories: None,
1187            base_path: None,
1188            custom_headers: vec![],
1189            output_dir: None,
1190            all_operations: false,
1191            custom_checks_file: None,
1192            request_delay_ms: 0,
1193            custom_filter: None,
1194            export_requests: false,
1195            validate_requests: false,
1196        };
1197        let generator = ConformanceGenerator::new(config);
1198        let script = generator.generate().unwrap();
1199
1200        assert!(script.contains("insecureSkipTLSVerify: true"));
1201        assert!(script.contains("my-api-key"));
1202        assert!(script.contains(&base64_encode("admin:secret")));
1203    }
1204
1205    #[test]
1206    fn test_should_include_category_none_includes_all() {
1207        let config = ConformanceConfig {
1208            target_url: "http://localhost:8080".to_string(),
1209            api_key: None,
1210            basic_auth: None,
1211            skip_tls_verify: false,
1212            categories: None,
1213            base_path: None,
1214            custom_headers: vec![],
1215            output_dir: None,
1216            all_operations: false,
1217            custom_checks_file: None,
1218            request_delay_ms: 0,
1219            custom_filter: None,
1220            export_requests: false,
1221            validate_requests: false,
1222        };
1223        assert!(config.should_include_category("Parameters"));
1224        assert!(config.should_include_category("Security"));
1225        assert!(config.should_include_category("Anything"));
1226    }
1227
1228    #[test]
1229    fn test_should_include_category_filtered() {
1230        let config = ConformanceConfig {
1231            target_url: "http://localhost:8080".to_string(),
1232            api_key: None,
1233            basic_auth: None,
1234            skip_tls_verify: false,
1235            categories: Some(vec!["Parameters".to_string(), "Security".to_string()]),
1236            base_path: None,
1237            custom_headers: vec![],
1238            output_dir: None,
1239            all_operations: false,
1240            custom_checks_file: None,
1241            request_delay_ms: 0,
1242            custom_filter: None,
1243            export_requests: false,
1244            validate_requests: false,
1245        };
1246        assert!(config.should_include_category("Parameters"));
1247        assert!(config.should_include_category("Security"));
1248        assert!(config.should_include_category("parameters")); // case-insensitive
1249        assert!(!config.should_include_category("Composition"));
1250        assert!(!config.should_include_category("Schema Types"));
1251    }
1252
1253    #[test]
1254    fn test_generate_with_category_filter() {
1255        let config = ConformanceConfig {
1256            target_url: "http://localhost:8080".to_string(),
1257            api_key: None,
1258            basic_auth: None,
1259            skip_tls_verify: false,
1260            categories: Some(vec!["Parameters".to_string(), "Security".to_string()]),
1261            base_path: None,
1262            custom_headers: vec![],
1263            output_dir: None,
1264            all_operations: false,
1265            custom_checks_file: None,
1266            request_delay_ms: 0,
1267            custom_filter: None,
1268            export_requests: false,
1269            validate_requests: false,
1270        };
1271        let generator = ConformanceGenerator::new(config);
1272        let script = generator.generate().unwrap();
1273
1274        assert!(script.contains("group('Parameters'"));
1275        assert!(script.contains("group('Security'"));
1276        assert!(!script.contains("group('Request Bodies'"));
1277        assert!(!script.contains("group('Schema Types'"));
1278        assert!(!script.contains("group('Composition'"));
1279    }
1280
1281    #[test]
1282    fn test_effective_base_url_no_base_path() {
1283        let config = ConformanceConfig {
1284            target_url: "https://example.com".to_string(),
1285            api_key: None,
1286            basic_auth: None,
1287            skip_tls_verify: false,
1288            categories: None,
1289            base_path: None,
1290            custom_headers: vec![],
1291            output_dir: None,
1292            all_operations: false,
1293            custom_checks_file: None,
1294            request_delay_ms: 0,
1295            custom_filter: None,
1296            export_requests: false,
1297            validate_requests: false,
1298        };
1299        assert_eq!(config.effective_base_url(), "https://example.com");
1300    }
1301
1302    #[test]
1303    fn test_effective_base_url_with_base_path() {
1304        let config = ConformanceConfig {
1305            target_url: "https://example.com".to_string(),
1306            api_key: None,
1307            basic_auth: None,
1308            skip_tls_verify: false,
1309            categories: None,
1310            base_path: Some("/api".to_string()),
1311            custom_headers: vec![],
1312            output_dir: None,
1313            all_operations: false,
1314            custom_checks_file: None,
1315            request_delay_ms: 0,
1316            custom_filter: None,
1317            export_requests: false,
1318            validate_requests: false,
1319        };
1320        assert_eq!(config.effective_base_url(), "https://example.com/api");
1321    }
1322
1323    #[test]
1324    fn test_effective_base_url_trailing_slash_normalization() {
1325        let config = ConformanceConfig {
1326            target_url: "https://example.com/".to_string(),
1327            api_key: None,
1328            basic_auth: None,
1329            skip_tls_verify: false,
1330            categories: None,
1331            base_path: Some("/api".to_string()),
1332            custom_headers: vec![],
1333            output_dir: None,
1334            all_operations: false,
1335            custom_checks_file: None,
1336            request_delay_ms: 0,
1337            custom_filter: None,
1338            export_requests: false,
1339            validate_requests: false,
1340        };
1341        assert_eq!(config.effective_base_url(), "https://example.com/api");
1342    }
1343
1344    #[test]
1345    fn test_effective_base_url_trailing_slash_no_base_path() {
1346        // Regression: --target https://192.168.2.86/ without --base-path
1347        // must not produce double slashes when combined with /path
1348        let config = ConformanceConfig {
1349            target_url: "https://192.168.2.86/".to_string(),
1350            api_key: None,
1351            basic_auth: None,
1352            skip_tls_verify: false,
1353            categories: None,
1354            base_path: None,
1355            custom_headers: vec![],
1356            output_dir: None,
1357            all_operations: false,
1358            custom_checks_file: None,
1359            request_delay_ms: 0,
1360            custom_filter: None,
1361            export_requests: false,
1362            validate_requests: false,
1363        };
1364        assert_eq!(config.effective_base_url(), "https://192.168.2.86");
1365    }
1366
1367    #[test]
1368    fn test_generate_script_with_base_path() {
1369        let config = ConformanceConfig {
1370            target_url: "https://192.168.2.86".to_string(),
1371            api_key: None,
1372            basic_auth: None,
1373            skip_tls_verify: true,
1374            categories: None,
1375            base_path: Some("/api".to_string()),
1376            custom_headers: vec![],
1377            output_dir: None,
1378            all_operations: false,
1379            custom_checks_file: None,
1380            request_delay_ms: 0,
1381            custom_filter: None,
1382            export_requests: false,
1383            validate_requests: false,
1384        };
1385        let generator = ConformanceGenerator::new(config);
1386        let script = generator.generate().unwrap();
1387
1388        assert!(script.contains("const BASE_URL = 'https://192.168.2.86/api'"));
1389        // Verify URLs include the base path via BASE_URL
1390        assert!(script.contains("${BASE_URL}/conformance/"));
1391    }
1392
1393    #[test]
1394    fn test_generate_with_custom_headers() {
1395        let config = ConformanceConfig {
1396            target_url: "https://192.168.2.86".to_string(),
1397            api_key: None,
1398            basic_auth: None,
1399            skip_tls_verify: true,
1400            categories: Some(vec!["Parameters".to_string()]),
1401            base_path: Some("/api".to_string()),
1402            custom_headers: vec![
1403                ("X-Avi-Tenant".to_string(), "admin".to_string()),
1404                ("X-CSRFToken".to_string(), "real-token".to_string()),
1405            ],
1406            output_dir: None,
1407            all_operations: false,
1408            custom_checks_file: None,
1409            request_delay_ms: 0,
1410            custom_filter: None,
1411            export_requests: false,
1412            validate_requests: false,
1413        };
1414        let generator = ConformanceGenerator::new(config);
1415        let script = generator.generate().unwrap();
1416
1417        // Custom headers should be inlined into requests (no separate const)
1418        assert!(
1419            !script.contains("const CUSTOM_HEADERS"),
1420            "Script should NOT declare a CUSTOM_HEADERS const"
1421        );
1422        assert!(script.contains("'X-Avi-Tenant': 'admin'"));
1423        assert!(script.contains("'X-CSRFToken': 'real-token'"));
1424    }
1425
1426    #[test]
1427    fn test_custom_headers_js_object() {
1428        let config = ConformanceConfig {
1429            target_url: "http://localhost".to_string(),
1430            api_key: None,
1431            basic_auth: None,
1432            skip_tls_verify: false,
1433            categories: None,
1434            base_path: None,
1435            custom_headers: vec![
1436                ("Authorization".to_string(), "Bearer abc123".to_string()),
1437                ("X-Custom".to_string(), "value".to_string()),
1438            ],
1439            output_dir: None,
1440            all_operations: false,
1441            custom_checks_file: None,
1442            request_delay_ms: 0,
1443            custom_filter: None,
1444            export_requests: false,
1445            validate_requests: false,
1446        };
1447        let js = config.custom_headers_js_object();
1448        assert!(js.contains("'Authorization': 'Bearer abc123'"));
1449        assert!(js.contains("'X-Custom': 'value'"));
1450    }
1451}