Skip to main content

mockforge_bench/conformance/
spec_driven.rs

1//! Spec-driven conformance testing
2//!
3//! Analyzes the user's OpenAPI spec to determine which features their API uses,
4//! then generates k6 conformance tests against their real endpoints.
5
6use super::generator::ConformanceConfig;
7use super::schema_validator::SchemaValidatorGenerator;
8use super::spec::ConformanceFeature;
9use crate::error::Result;
10use crate::request_gen::RequestGenerator;
11use crate::spec_parser::ApiOperation;
12use openapiv3::{
13    OpenAPI, Operation, Parameter, ParameterSchemaOrContent, ReferenceOr, RequestBody, Response,
14    Schema, SchemaKind, SecurityScheme, StringFormat, Type, VariantOrUnknownOrEmpty,
15};
16use std::collections::HashSet;
17
18/// Resolve `$ref` references against the OpenAPI components
19mod ref_resolver {
20    use super::*;
21
22    pub fn resolve_parameter<'a>(
23        param_ref: &'a ReferenceOr<Parameter>,
24        spec: &'a OpenAPI,
25    ) -> Option<&'a Parameter> {
26        match param_ref {
27            ReferenceOr::Item(param) => Some(param),
28            ReferenceOr::Reference { reference } => {
29                let name = reference.strip_prefix("#/components/parameters/")?;
30                let components = spec.components.as_ref()?;
31                match components.parameters.get(name)? {
32                    ReferenceOr::Item(param) => Some(param),
33                    ReferenceOr::Reference { .. } => None, // No recursive ref chasing
34                }
35            }
36        }
37    }
38
39    pub fn resolve_request_body<'a>(
40        body_ref: &'a ReferenceOr<RequestBody>,
41        spec: &'a OpenAPI,
42    ) -> Option<&'a RequestBody> {
43        match body_ref {
44            ReferenceOr::Item(body) => Some(body),
45            ReferenceOr::Reference { reference } => {
46                let name = reference.strip_prefix("#/components/requestBodies/")?;
47                let components = spec.components.as_ref()?;
48                match components.request_bodies.get(name)? {
49                    ReferenceOr::Item(body) => Some(body),
50                    ReferenceOr::Reference { .. } => None,
51                }
52            }
53        }
54    }
55
56    pub fn resolve_schema<'a>(
57        schema_ref: &'a ReferenceOr<Schema>,
58        spec: &'a OpenAPI,
59    ) -> Option<&'a Schema> {
60        resolve_schema_with_visited(schema_ref, spec, &mut HashSet::new())
61    }
62
63    fn resolve_schema_with_visited<'a>(
64        schema_ref: &'a ReferenceOr<Schema>,
65        spec: &'a OpenAPI,
66        visited: &mut HashSet<String>,
67    ) -> Option<&'a Schema> {
68        match schema_ref {
69            ReferenceOr::Item(schema) => Some(schema),
70            ReferenceOr::Reference { reference } => {
71                if !visited.insert(reference.clone()) {
72                    return None; // Cycle detected
73                }
74                let name = reference.strip_prefix("#/components/schemas/")?;
75                let components = spec.components.as_ref()?;
76                let nested = components.schemas.get(name)?;
77                resolve_schema_with_visited(nested, spec, visited)
78            }
79        }
80    }
81
82    /// Resolve a boxed schema reference (used by array items and object properties)
83    pub fn resolve_boxed_schema<'a>(
84        schema_ref: &'a ReferenceOr<Box<Schema>>,
85        spec: &'a OpenAPI,
86    ) -> Option<&'a Schema> {
87        match schema_ref {
88            ReferenceOr::Item(schema) => Some(schema.as_ref()),
89            ReferenceOr::Reference { reference } => {
90                // Delegate to the regular schema resolver
91                let name = reference.strip_prefix("#/components/schemas/")?;
92                let components = spec.components.as_ref()?;
93                let nested = components.schemas.get(name)?;
94                resolve_schema_with_visited(nested, spec, &mut HashSet::new())
95            }
96        }
97    }
98
99    pub fn resolve_response<'a>(
100        resp_ref: &'a ReferenceOr<Response>,
101        spec: &'a OpenAPI,
102    ) -> Option<&'a Response> {
103        match resp_ref {
104            ReferenceOr::Item(resp) => Some(resp),
105            ReferenceOr::Reference { reference } => {
106                let name = reference.strip_prefix("#/components/responses/")?;
107                let components = spec.components.as_ref()?;
108                match components.responses.get(name)? {
109                    ReferenceOr::Item(resp) => Some(resp),
110                    ReferenceOr::Reference { .. } => None,
111                }
112            }
113        }
114    }
115}
116
117/// An API operation annotated with the conformance features it exercises
118#[derive(Debug, Clone)]
119pub struct AnnotatedOperation {
120    pub path: String,
121    pub method: String,
122    pub features: Vec<ConformanceFeature>,
123    pub request_body_content_type: Option<String>,
124    pub sample_body: Option<String>,
125    pub query_params: Vec<(String, String)>,
126    pub header_params: Vec<(String, String)>,
127    pub path_params: Vec<(String, String)>,
128    /// Response schema for validation (JSON string of the schema)
129    pub response_schema: Option<Schema>,
130}
131
132/// Generates spec-driven conformance k6 scripts
133pub struct SpecDrivenConformanceGenerator {
134    config: ConformanceConfig,
135    operations: Vec<AnnotatedOperation>,
136}
137
138impl SpecDrivenConformanceGenerator {
139    pub fn new(config: ConformanceConfig, operations: Vec<AnnotatedOperation>) -> Self {
140        Self { config, operations }
141    }
142
143    /// Annotate a list of API operations with conformance features
144    pub fn annotate_operations(
145        operations: &[ApiOperation],
146        spec: &OpenAPI,
147    ) -> Vec<AnnotatedOperation> {
148        operations.iter().map(|op| Self::annotate_operation(op, spec)).collect()
149    }
150
151    /// Analyze an operation and determine which conformance features it exercises
152    fn annotate_operation(op: &ApiOperation, spec: &OpenAPI) -> AnnotatedOperation {
153        let mut features = Vec::new();
154        let mut query_params = Vec::new();
155        let mut header_params = Vec::new();
156        let mut path_params = Vec::new();
157
158        // Detect HTTP method feature
159        match op.method.to_uppercase().as_str() {
160            "GET" => features.push(ConformanceFeature::MethodGet),
161            "POST" => features.push(ConformanceFeature::MethodPost),
162            "PUT" => features.push(ConformanceFeature::MethodPut),
163            "PATCH" => features.push(ConformanceFeature::MethodPatch),
164            "DELETE" => features.push(ConformanceFeature::MethodDelete),
165            "HEAD" => features.push(ConformanceFeature::MethodHead),
166            "OPTIONS" => features.push(ConformanceFeature::MethodOptions),
167            _ => {}
168        }
169
170        // Detect parameter features (resolves $ref)
171        for param_ref in &op.operation.parameters {
172            if let Some(param) = ref_resolver::resolve_parameter(param_ref, spec) {
173                Self::annotate_parameter(
174                    param,
175                    spec,
176                    &mut features,
177                    &mut query_params,
178                    &mut header_params,
179                    &mut path_params,
180                );
181            }
182        }
183
184        // Detect path parameters from the path template itself
185        for segment in op.path.split('/') {
186            if segment.starts_with('{') && segment.ends_with('}') {
187                let name = &segment[1..segment.len() - 1];
188                // Only add if not already found from parameters
189                if !path_params.iter().any(|(n, _)| n == name) {
190                    path_params.push((name.to_string(), "test-value".to_string()));
191                    // Determine type from path params we didn't already handle
192                    if !features.contains(&ConformanceFeature::PathParamString)
193                        && !features.contains(&ConformanceFeature::PathParamInteger)
194                    {
195                        features.push(ConformanceFeature::PathParamString);
196                    }
197                }
198            }
199        }
200
201        // Detect request body features (resolves $ref)
202        let mut request_body_content_type = None;
203        let mut sample_body = None;
204
205        let resolved_body = op
206            .operation
207            .request_body
208            .as_ref()
209            .and_then(|b| ref_resolver::resolve_request_body(b, spec));
210
211        if let Some(body) = resolved_body {
212            for (content_type, _media) in &body.content {
213                match content_type.as_str() {
214                    "application/json" => {
215                        features.push(ConformanceFeature::BodyJson);
216                        request_body_content_type = Some("application/json".to_string());
217                        // Generate sample body from schema
218                        if let Ok(template) = RequestGenerator::generate_template(op) {
219                            if let Some(body_val) = &template.body {
220                                sample_body = Some(body_val.to_string());
221                            }
222                        }
223                    }
224                    "application/x-www-form-urlencoded" => {
225                        features.push(ConformanceFeature::BodyFormUrlencoded);
226                        request_body_content_type =
227                            Some("application/x-www-form-urlencoded".to_string());
228                    }
229                    "multipart/form-data" => {
230                        features.push(ConformanceFeature::BodyMultipart);
231                        request_body_content_type = Some("multipart/form-data".to_string());
232                    }
233                    _ => {}
234                }
235            }
236
237            // Detect schema features in request body (resolves $ref in schema)
238            if let Some(media) = body.content.get("application/json") {
239                if let Some(schema_ref) = &media.schema {
240                    if let Some(schema) = ref_resolver::resolve_schema(schema_ref, spec) {
241                        Self::annotate_schema(schema, spec, &mut features);
242                    }
243                }
244            }
245        }
246
247        // Detect response code features
248        Self::annotate_responses(&op.operation, spec, &mut features);
249
250        // Extract response schema for validation (resolves $ref)
251        let response_schema = Self::extract_response_schema(&op.operation, spec);
252        if response_schema.is_some() {
253            features.push(ConformanceFeature::ResponseValidation);
254        }
255
256        // Detect content negotiation (response with multiple content types)
257        Self::annotate_content_negotiation(&op.operation, spec, &mut features);
258
259        // Detect security features
260        Self::annotate_security(&op.operation, spec, &mut features);
261
262        // Deduplicate features
263        features.sort_by_key(|f| f.check_name());
264        features.dedup_by_key(|f| f.check_name());
265
266        AnnotatedOperation {
267            path: op.path.clone(),
268            method: op.method.to_uppercase(),
269            features,
270            request_body_content_type,
271            sample_body,
272            query_params,
273            header_params,
274            path_params,
275            response_schema,
276        }
277    }
278
279    /// Annotate parameter features
280    fn annotate_parameter(
281        param: &Parameter,
282        spec: &OpenAPI,
283        features: &mut Vec<ConformanceFeature>,
284        query_params: &mut Vec<(String, String)>,
285        header_params: &mut Vec<(String, String)>,
286        path_params: &mut Vec<(String, String)>,
287    ) {
288        let (location, data) = match param {
289            Parameter::Query { parameter_data, .. } => ("query", parameter_data),
290            Parameter::Path { parameter_data, .. } => ("path", parameter_data),
291            Parameter::Header { parameter_data, .. } => ("header", parameter_data),
292            Parameter::Cookie { .. } => {
293                features.push(ConformanceFeature::CookieParam);
294                return;
295            }
296        };
297
298        // Detect type from schema
299        let is_integer = Self::param_schema_is_integer(data, spec);
300        let is_array = Self::param_schema_is_array(data, spec);
301
302        // Generate sample value
303        let sample = if is_integer {
304            "42".to_string()
305        } else if is_array {
306            "a,b".to_string()
307        } else {
308            "test-value".to_string()
309        };
310
311        match location {
312            "path" => {
313                if is_integer {
314                    features.push(ConformanceFeature::PathParamInteger);
315                } else {
316                    features.push(ConformanceFeature::PathParamString);
317                }
318                path_params.push((data.name.clone(), sample));
319            }
320            "query" => {
321                if is_array {
322                    features.push(ConformanceFeature::QueryParamArray);
323                } else if is_integer {
324                    features.push(ConformanceFeature::QueryParamInteger);
325                } else {
326                    features.push(ConformanceFeature::QueryParamString);
327                }
328                query_params.push((data.name.clone(), sample));
329            }
330            "header" => {
331                features.push(ConformanceFeature::HeaderParam);
332                header_params.push((data.name.clone(), sample));
333            }
334            _ => {}
335        }
336
337        // Check for constraint features on the parameter (resolves $ref)
338        if let ParameterSchemaOrContent::Schema(schema_ref) = &data.format {
339            if let Some(schema) = ref_resolver::resolve_schema(schema_ref, spec) {
340                Self::annotate_schema(schema, spec, features);
341            }
342        }
343
344        // Required/optional
345        if data.required {
346            features.push(ConformanceFeature::ConstraintRequired);
347        } else {
348            features.push(ConformanceFeature::ConstraintOptional);
349        }
350    }
351
352    fn param_schema_is_integer(data: &openapiv3::ParameterData, spec: &OpenAPI) -> bool {
353        if let ParameterSchemaOrContent::Schema(schema_ref) = &data.format {
354            if let Some(schema) = ref_resolver::resolve_schema(schema_ref, spec) {
355                return matches!(&schema.schema_kind, SchemaKind::Type(Type::Integer(_)));
356            }
357        }
358        false
359    }
360
361    fn param_schema_is_array(data: &openapiv3::ParameterData, spec: &OpenAPI) -> bool {
362        if let ParameterSchemaOrContent::Schema(schema_ref) = &data.format {
363            if let Some(schema) = ref_resolver::resolve_schema(schema_ref, spec) {
364                return matches!(&schema.schema_kind, SchemaKind::Type(Type::Array(_)));
365            }
366        }
367        false
368    }
369
370    /// Annotate schema-level features (types, composition, formats, constraints)
371    fn annotate_schema(schema: &Schema, spec: &OpenAPI, features: &mut Vec<ConformanceFeature>) {
372        match &schema.schema_kind {
373            SchemaKind::Type(Type::String(s)) => {
374                features.push(ConformanceFeature::SchemaString);
375                // Check format
376                match &s.format {
377                    VariantOrUnknownOrEmpty::Item(StringFormat::Date) => {
378                        features.push(ConformanceFeature::FormatDate);
379                    }
380                    VariantOrUnknownOrEmpty::Item(StringFormat::DateTime) => {
381                        features.push(ConformanceFeature::FormatDateTime);
382                    }
383                    VariantOrUnknownOrEmpty::Unknown(fmt) => match fmt.as_str() {
384                        "email" => features.push(ConformanceFeature::FormatEmail),
385                        "uuid" => features.push(ConformanceFeature::FormatUuid),
386                        "uri" | "url" => features.push(ConformanceFeature::FormatUri),
387                        "ipv4" => features.push(ConformanceFeature::FormatIpv4),
388                        "ipv6" => features.push(ConformanceFeature::FormatIpv6),
389                        _ => {}
390                    },
391                    _ => {}
392                }
393                // Check constraints
394                if s.pattern.is_some() {
395                    features.push(ConformanceFeature::ConstraintPattern);
396                }
397                if !s.enumeration.is_empty() {
398                    features.push(ConformanceFeature::ConstraintEnum);
399                }
400                if s.min_length.is_some() || s.max_length.is_some() {
401                    features.push(ConformanceFeature::ConstraintMinMax);
402                }
403            }
404            SchemaKind::Type(Type::Integer(i)) => {
405                features.push(ConformanceFeature::SchemaInteger);
406                if i.minimum.is_some() || i.maximum.is_some() {
407                    features.push(ConformanceFeature::ConstraintMinMax);
408                }
409                if !i.enumeration.is_empty() {
410                    features.push(ConformanceFeature::ConstraintEnum);
411                }
412            }
413            SchemaKind::Type(Type::Number(n)) => {
414                features.push(ConformanceFeature::SchemaNumber);
415                if n.minimum.is_some() || n.maximum.is_some() {
416                    features.push(ConformanceFeature::ConstraintMinMax);
417                }
418            }
419            SchemaKind::Type(Type::Boolean(_)) => {
420                features.push(ConformanceFeature::SchemaBoolean);
421            }
422            SchemaKind::Type(Type::Array(arr)) => {
423                features.push(ConformanceFeature::SchemaArray);
424                if let Some(item_ref) = &arr.items {
425                    if let Some(item_schema) = ref_resolver::resolve_boxed_schema(item_ref, spec) {
426                        Self::annotate_schema(item_schema, spec, features);
427                    }
428                }
429            }
430            SchemaKind::Type(Type::Object(obj)) => {
431                features.push(ConformanceFeature::SchemaObject);
432                // Check required fields
433                if !obj.required.is_empty() {
434                    features.push(ConformanceFeature::ConstraintRequired);
435                }
436                // Walk properties (resolves $ref)
437                for (_name, prop_ref) in &obj.properties {
438                    if let Some(prop_schema) = ref_resolver::resolve_boxed_schema(prop_ref, spec) {
439                        Self::annotate_schema(prop_schema, spec, features);
440                    }
441                }
442            }
443            SchemaKind::OneOf { .. } => {
444                features.push(ConformanceFeature::CompositionOneOf);
445            }
446            SchemaKind::AnyOf { .. } => {
447                features.push(ConformanceFeature::CompositionAnyOf);
448            }
449            SchemaKind::AllOf { .. } => {
450                features.push(ConformanceFeature::CompositionAllOf);
451            }
452            _ => {}
453        }
454    }
455
456    /// Detect response code features (resolves $ref in responses)
457    fn annotate_responses(
458        operation: &Operation,
459        spec: &OpenAPI,
460        features: &mut Vec<ConformanceFeature>,
461    ) {
462        for (status_code, resp_ref) in &operation.responses.responses {
463            // Only count features for responses that actually resolve
464            if ref_resolver::resolve_response(resp_ref, spec).is_some() {
465                match status_code {
466                    openapiv3::StatusCode::Code(200) => {
467                        features.push(ConformanceFeature::Response200)
468                    }
469                    openapiv3::StatusCode::Code(201) => {
470                        features.push(ConformanceFeature::Response201)
471                    }
472                    openapiv3::StatusCode::Code(204) => {
473                        features.push(ConformanceFeature::Response204)
474                    }
475                    openapiv3::StatusCode::Code(400) => {
476                        features.push(ConformanceFeature::Response400)
477                    }
478                    openapiv3::StatusCode::Code(404) => {
479                        features.push(ConformanceFeature::Response404)
480                    }
481                    _ => {}
482                }
483            }
484        }
485    }
486
487    /// Extract the response schema for the primary success response (200 or 201)
488    /// Resolves $ref for both the response and the schema within it.
489    fn extract_response_schema(operation: &Operation, spec: &OpenAPI) -> Option<Schema> {
490        // Try 200 first, then 201
491        for code in [200u16, 201] {
492            if let Some(resp_ref) =
493                operation.responses.responses.get(&openapiv3::StatusCode::Code(code))
494            {
495                if let Some(response) = ref_resolver::resolve_response(resp_ref, spec) {
496                    if let Some(media) = response.content.get("application/json") {
497                        if let Some(schema_ref) = &media.schema {
498                            if let Some(schema) = ref_resolver::resolve_schema(schema_ref, spec) {
499                                return Some(schema.clone());
500                            }
501                        }
502                    }
503                }
504            }
505        }
506        None
507    }
508
509    /// Detect content negotiation: response supports multiple content types
510    fn annotate_content_negotiation(
511        operation: &Operation,
512        spec: &OpenAPI,
513        features: &mut Vec<ConformanceFeature>,
514    ) {
515        for (_status_code, resp_ref) in &operation.responses.responses {
516            if let Some(response) = ref_resolver::resolve_response(resp_ref, spec) {
517                if response.content.len() > 1 {
518                    features.push(ConformanceFeature::ContentNegotiation);
519                    return; // Only need to detect once per operation
520                }
521            }
522        }
523    }
524
525    /// Detect security scheme features.
526    /// Checks operation-level security first, falling back to global security requirements.
527    /// Resolves scheme names against SecurityScheme definitions in components.
528    fn annotate_security(
529        operation: &Operation,
530        spec: &OpenAPI,
531        features: &mut Vec<ConformanceFeature>,
532    ) {
533        // Use operation-level security if present, otherwise fall back to global
534        let security_reqs = operation.security.as_ref().or(spec.security.as_ref());
535
536        if let Some(security) = security_reqs {
537            for security_req in security {
538                for scheme_name in security_req.keys() {
539                    // Try to resolve the scheme from components for accurate type detection
540                    if let Some(resolved) = Self::resolve_security_scheme(scheme_name, spec) {
541                        match resolved {
542                            SecurityScheme::HTTP { ref scheme, .. } => {
543                                if scheme.eq_ignore_ascii_case("bearer") {
544                                    features.push(ConformanceFeature::SecurityBearer);
545                                } else if scheme.eq_ignore_ascii_case("basic") {
546                                    features.push(ConformanceFeature::SecurityBasic);
547                                }
548                            }
549                            SecurityScheme::APIKey { .. } => {
550                                features.push(ConformanceFeature::SecurityApiKey);
551                            }
552                            // OAuth2 and OpenIDConnect don't map to our current feature set
553                            _ => {}
554                        }
555                    } else {
556                        // Fallback: heuristic name matching for unresolvable schemes
557                        let name_lower = scheme_name.to_lowercase();
558                        if name_lower.contains("bearer") || name_lower.contains("jwt") {
559                            features.push(ConformanceFeature::SecurityBearer);
560                        } else if name_lower.contains("api") && name_lower.contains("key") {
561                            features.push(ConformanceFeature::SecurityApiKey);
562                        } else if name_lower.contains("basic") {
563                            features.push(ConformanceFeature::SecurityBasic);
564                        }
565                    }
566                }
567            }
568        }
569    }
570
571    /// Resolve a security scheme name to its SecurityScheme definition
572    fn resolve_security_scheme<'a>(name: &str, spec: &'a OpenAPI) -> Option<&'a SecurityScheme> {
573        let components = spec.components.as_ref()?;
574        match components.security_schemes.get(name)? {
575            ReferenceOr::Item(scheme) => Some(scheme),
576            ReferenceOr::Reference { .. } => None,
577        }
578    }
579
580    /// Returns the number of operations being tested
581    pub fn operation_count(&self) -> usize {
582        self.operations.len()
583    }
584
585    /// Generate the k6 conformance script.
586    /// Returns (script, check_count) where check_count is the number of unique checks emitted.
587    pub fn generate(&self) -> Result<(String, usize)> {
588        let mut script = String::with_capacity(16384);
589
590        // Imports
591        script.push_str("import http from 'k6/http';\n");
592        script.push_str("import { check, group } from 'k6';\n\n");
593
594        // Options
595        script.push_str("export const options = {\n");
596        script.push_str("  vus: 1,\n");
597        script.push_str("  iterations: 1,\n");
598        if self.config.skip_tls_verify {
599            script.push_str("  insecureSkipTLSVerify: true,\n");
600        }
601        script.push_str("  thresholds: {\n");
602        script.push_str("    checks: ['rate>0'],\n");
603        script.push_str("  },\n");
604        script.push_str("};\n\n");
605
606        // Base URL (includes base_path if configured)
607        script.push_str(&format!("const BASE_URL = '{}';\n\n", self.config.effective_base_url()));
608        script.push_str("const JSON_HEADERS = { 'Content-Type': 'application/json' };\n\n");
609
610        // Default function
611        script.push_str("export default function () {\n");
612
613        if self.config.has_cookie_header() {
614            script.push_str(
615                "  // Clear cookie jar to prevent server Set-Cookie from duplicating custom Cookie header\n",
616            );
617            script.push_str("  http.cookieJar().clear(BASE_URL);\n\n");
618        }
619
620        // Group operations by category
621        let mut category_ops: std::collections::BTreeMap<
622            &'static str,
623            Vec<(&AnnotatedOperation, &ConformanceFeature)>,
624        > = std::collections::BTreeMap::new();
625
626        for op in &self.operations {
627            for feature in &op.features {
628                let category = feature.category();
629                if self.config.should_include_category(category) {
630                    category_ops.entry(category).or_default().push((op, feature));
631                }
632            }
633        }
634
635        // Emit grouped tests
636        let mut total_checks = 0usize;
637        for (category, ops) in &category_ops {
638            script.push_str(&format!("  group('{}', function () {{\n", category));
639
640            if self.config.all_operations {
641                // All-operations mode: test every operation, using path-qualified check names
642                let mut emitted_checks: HashSet<String> = HashSet::new();
643                for (op, feature) in ops {
644                    let qualified = format!("{}:{}", feature.check_name(), op.path);
645                    if emitted_checks.insert(qualified.clone()) {
646                        self.emit_check_named(&mut script, op, feature, &qualified);
647                        total_checks += 1;
648                    }
649                }
650            } else {
651                // Default: one representative operation per feature check name
652                let mut emitted_checks: HashSet<&str> = HashSet::new();
653                for (op, feature) in ops {
654                    if emitted_checks.insert(feature.check_name()) {
655                        self.emit_check(&mut script, op, feature);
656                        total_checks += 1;
657                    }
658                }
659            }
660
661            script.push_str("  });\n\n");
662        }
663
664        script.push_str("}\n\n");
665
666        // handleSummary
667        self.generate_handle_summary(&mut script);
668
669        Ok((script, total_checks))
670    }
671
672    /// Emit a single k6 check for an operation + feature using the feature's default check name
673    fn emit_check(
674        &self,
675        script: &mut String,
676        op: &AnnotatedOperation,
677        feature: &ConformanceFeature,
678    ) {
679        self.emit_check_named(script, op, feature, feature.check_name());
680    }
681
682    /// Emit a single k6 check for an operation + feature with a custom check name
683    fn emit_check_named(
684        &self,
685        script: &mut String,
686        op: &AnnotatedOperation,
687        feature: &ConformanceFeature,
688        check_name: &str,
689    ) {
690        // Escape single quotes in check name since it's embedded in JS single-quoted strings
691        let check_name = check_name.replace('\'', "\\'");
692        let check_name = check_name.as_str();
693
694        script.push_str("    {\n");
695
696        // Build the URL path with parameters substituted
697        let mut url_path = op.path.clone();
698        for (name, value) in &op.path_params {
699            url_path = url_path.replace(&format!("{{{}}}", name), value);
700        }
701
702        // Build query string
703        if !op.query_params.is_empty() {
704            let qs: Vec<String> =
705                op.query_params.iter().map(|(k, v)| format!("{}={}", k, v)).collect();
706            url_path = format!("{}?{}", url_path, qs.join("&"));
707        }
708
709        let full_url = format!("${{BASE_URL}}{}", url_path);
710
711        // Build effective headers: merge spec-derived headers with custom headers.
712        // Custom headers override spec-derived ones with the same name.
713        let effective_headers = self.effective_headers(&op.header_params);
714        let has_headers = !effective_headers.is_empty();
715        let headers_obj = if has_headers {
716            Self::format_headers(&effective_headers)
717        } else {
718            String::new()
719        };
720
721        // Determine HTTP method and emit request
722        match op.method.as_str() {
723            "GET" => {
724                if has_headers {
725                    script.push_str(&format!(
726                        "      let res = http.get(`{}`, {{ headers: {} }});\n",
727                        full_url, headers_obj
728                    ));
729                } else {
730                    script.push_str(&format!("      let res = http.get(`{}`);\n", full_url));
731                }
732            }
733            "POST" => {
734                self.emit_request_with_body(script, "post", &full_url, op, &effective_headers);
735            }
736            "PUT" => {
737                self.emit_request_with_body(script, "put", &full_url, op, &effective_headers);
738            }
739            "PATCH" => {
740                self.emit_request_with_body(script, "patch", &full_url, op, &effective_headers);
741            }
742            "DELETE" => {
743                if has_headers {
744                    script.push_str(&format!(
745                        "      let res = http.del(`{}`, null, {{ headers: {} }});\n",
746                        full_url, headers_obj
747                    ));
748                } else {
749                    script.push_str(&format!("      let res = http.del(`{}`);\n", full_url));
750                }
751            }
752            "HEAD" => {
753                if has_headers {
754                    script.push_str(&format!(
755                        "      let res = http.head(`{}`, {{ headers: {} }});\n",
756                        full_url, headers_obj
757                    ));
758                } else {
759                    script.push_str(&format!("      let res = http.head(`{}`);\n", full_url));
760                }
761            }
762            "OPTIONS" => {
763                if has_headers {
764                    script.push_str(&format!(
765                        "      let res = http.options(`{}`, null, {{ headers: {} }});\n",
766                        full_url, headers_obj
767                    ));
768                } else {
769                    script.push_str(&format!("      let res = http.options(`{}`);\n", full_url));
770                }
771            }
772            _ => {
773                if has_headers {
774                    script.push_str(&format!(
775                        "      let res = http.get(`{}`, {{ headers: {} }});\n",
776                        full_url, headers_obj
777                    ));
778                } else {
779                    script.push_str(&format!("      let res = http.get(`{}`);\n", full_url));
780                }
781            }
782        }
783
784        // Check: emit assertion based on feature type
785        if matches!(
786            feature,
787            ConformanceFeature::Response200
788                | ConformanceFeature::Response201
789                | ConformanceFeature::Response204
790                | ConformanceFeature::Response400
791                | ConformanceFeature::Response404
792        ) {
793            let expected_code = match feature {
794                ConformanceFeature::Response200 => 200,
795                ConformanceFeature::Response201 => 201,
796                ConformanceFeature::Response204 => 204,
797                ConformanceFeature::Response400 => 400,
798                ConformanceFeature::Response404 => 404,
799                _ => 200,
800            };
801            script.push_str(&format!(
802                "      check(res, {{ '{}': (r) => r.status === {} }});\n",
803                check_name, expected_code
804            ));
805        } else if matches!(feature, ConformanceFeature::ResponseValidation) {
806            // Response schema validation — validate the response body against the schema
807            if let Some(schema) = &op.response_schema {
808                let validation_js = SchemaValidatorGenerator::generate_validation(schema);
809                script.push_str(&format!(
810                    "      try {{ let body = res.json(); check(res, {{ '{}': (r) => {{ {} }} }}); }} catch(e) {{ check(res, {{ '{}': () => false }}); }}\n",
811                    check_name, validation_js, check_name
812                ));
813            }
814        } else {
815            script.push_str(&format!(
816                "      check(res, {{ '{}': (r) => r.status >= 200 && r.status < 500 }});\n",
817                check_name
818            ));
819        }
820
821        // Clear cookie jar after each request to prevent Set-Cookie leaking
822        if self.config.has_cookie_header() {
823            script.push_str("      http.cookieJar().clear(BASE_URL);\n");
824        }
825
826        script.push_str("    }\n");
827    }
828
829    /// Emit an HTTP request with a body
830    fn emit_request_with_body(
831        &self,
832        script: &mut String,
833        method: &str,
834        url: &str,
835        op: &AnnotatedOperation,
836        effective_headers: &[(String, String)],
837    ) {
838        if let Some(body) = &op.sample_body {
839            let escaped_body = body.replace('\'', "\\'");
840            let headers = if !effective_headers.is_empty() {
841                format!(
842                    "Object.assign({{}}, JSON_HEADERS, {})",
843                    Self::format_headers(effective_headers)
844                )
845            } else {
846                "JSON_HEADERS".to_string()
847            };
848            script.push_str(&format!(
849                "      let res = http.{}(`{}`, '{}', {{ headers: {} }});\n",
850                method, url, escaped_body, headers
851            ));
852        } else if !effective_headers.is_empty() {
853            script.push_str(&format!(
854                "      let res = http.{}(`{}`, null, {{ headers: {} }});\n",
855                method,
856                url,
857                Self::format_headers(effective_headers)
858            ));
859        } else {
860            script.push_str(&format!("      let res = http.{}(`{}`, null);\n", method, url));
861        }
862    }
863
864    /// Build effective headers by merging spec-derived headers with custom headers.
865    /// Custom headers override spec-derived ones with the same name (case-insensitive).
866    /// Custom headers not in the spec are appended.
867    fn effective_headers(&self, spec_headers: &[(String, String)]) -> Vec<(String, String)> {
868        let custom = &self.config.custom_headers;
869        if custom.is_empty() {
870            return spec_headers.to_vec();
871        }
872
873        let mut result: Vec<(String, String)> = Vec::new();
874
875        // Start with spec headers, replacing values if a custom header matches
876        for (name, value) in spec_headers {
877            if let Some((_, custom_val)) =
878                custom.iter().find(|(cn, _)| cn.eq_ignore_ascii_case(name))
879            {
880                result.push((name.clone(), custom_val.clone()));
881            } else {
882                result.push((name.clone(), value.clone()));
883            }
884        }
885
886        // Append custom headers that aren't already in spec headers
887        for (name, value) in custom {
888            if !spec_headers.iter().any(|(sn, _)| sn.eq_ignore_ascii_case(name)) {
889                result.push((name.clone(), value.clone()));
890            }
891        }
892
893        result
894    }
895
896    /// Format header params as a JS object literal
897    fn format_headers(headers: &[(String, String)]) -> String {
898        let entries: Vec<String> = headers
899            .iter()
900            .map(|(k, v)| format!("'{}': '{}'", k, v.replace('\'', "\\'")))
901            .collect();
902        format!("{{ {} }}", entries.join(", "))
903    }
904
905    /// handleSummary — same format as reference mode for report compatibility
906    fn generate_handle_summary(&self, script: &mut String) {
907        // Use absolute path for report output so k6 writes where the CLI expects
908        let report_path = match &self.config.output_dir {
909            Some(dir) => {
910                let abs = std::fs::canonicalize(dir)
911                    .unwrap_or_else(|_| dir.clone())
912                    .join("conformance-report.json");
913                abs.to_string_lossy().to_string()
914            }
915            None => "conformance-report.json".to_string(),
916        };
917
918        script.push_str("export function handleSummary(data) {\n");
919        script.push_str("  let checks = {};\n");
920        script.push_str("  if (data.metrics && data.metrics.checks) {\n");
921        script.push_str("    checks.overall_pass_rate = data.metrics.checks.values.rate;\n");
922        script.push_str("  }\n");
923        script.push_str("  let checkResults = {};\n");
924        script.push_str("  function walkGroups(group) {\n");
925        script.push_str("    if (group.checks) {\n");
926        script.push_str("      for (let checkObj of group.checks) {\n");
927        script.push_str("        checkResults[checkObj.name] = {\n");
928        script.push_str("          passes: checkObj.passes,\n");
929        script.push_str("          fails: checkObj.fails,\n");
930        script.push_str("        };\n");
931        script.push_str("      }\n");
932        script.push_str("    }\n");
933        script.push_str("    if (group.groups) {\n");
934        script.push_str("      for (let subGroup of group.groups) {\n");
935        script.push_str("        walkGroups(subGroup);\n");
936        script.push_str("      }\n");
937        script.push_str("    }\n");
938        script.push_str("  }\n");
939        script.push_str("  if (data.root_group) {\n");
940        script.push_str("    walkGroups(data.root_group);\n");
941        script.push_str("  }\n");
942        script.push_str("  return {\n");
943        script.push_str(&format!(
944            "    '{}': JSON.stringify({{ checks: checkResults, overall: checks }}, null, 2),\n",
945            report_path
946        ));
947        script.push_str("    stdout: textSummary(data, { indent: '  ', enableColors: true }),\n");
948        script.push_str("  };\n");
949        script.push_str("}\n\n");
950        script.push_str("function textSummary(data, opts) {\n");
951        script.push_str("  return JSON.stringify(data, null, 2);\n");
952        script.push_str("}\n");
953    }
954}
955
956#[cfg(test)]
957mod tests {
958    use super::*;
959    use openapiv3::{
960        Operation, ParameterData, ParameterSchemaOrContent, PathStyle, Response, Schema,
961        SchemaData, SchemaKind, StringType, Type,
962    };
963
964    fn make_op(method: &str, path: &str, operation: Operation) -> ApiOperation {
965        ApiOperation {
966            method: method.to_string(),
967            path: path.to_string(),
968            operation,
969            operation_id: None,
970        }
971    }
972
973    fn empty_spec() -> OpenAPI {
974        OpenAPI::default()
975    }
976
977    #[test]
978    fn test_annotate_get_with_path_param() {
979        let mut op = Operation::default();
980        op.parameters.push(ReferenceOr::Item(Parameter::Path {
981            parameter_data: ParameterData {
982                name: "id".to_string(),
983                description: None,
984                required: true,
985                deprecated: None,
986                format: ParameterSchemaOrContent::Schema(ReferenceOr::Item(Schema {
987                    schema_data: SchemaData::default(),
988                    schema_kind: SchemaKind::Type(Type::String(StringType::default())),
989                })),
990                example: None,
991                examples: Default::default(),
992                explode: None,
993                extensions: Default::default(),
994            },
995            style: PathStyle::Simple,
996        }));
997
998        let api_op = make_op("get", "/users/{id}", op);
999        let annotated = SpecDrivenConformanceGenerator::annotate_operation(&api_op, &empty_spec());
1000
1001        assert!(annotated.features.contains(&ConformanceFeature::MethodGet));
1002        assert!(annotated.features.contains(&ConformanceFeature::PathParamString));
1003        assert!(annotated.features.contains(&ConformanceFeature::ConstraintRequired));
1004        assert_eq!(annotated.path_params.len(), 1);
1005        assert_eq!(annotated.path_params[0].0, "id");
1006    }
1007
1008    #[test]
1009    fn test_annotate_post_with_json_body() {
1010        let mut op = Operation::default();
1011        let mut body = openapiv3::RequestBody {
1012            required: true,
1013            ..Default::default()
1014        };
1015        body.content
1016            .insert("application/json".to_string(), openapiv3::MediaType::default());
1017        op.request_body = Some(ReferenceOr::Item(body));
1018
1019        let api_op = make_op("post", "/items", op);
1020        let annotated = SpecDrivenConformanceGenerator::annotate_operation(&api_op, &empty_spec());
1021
1022        assert!(annotated.features.contains(&ConformanceFeature::MethodPost));
1023        assert!(annotated.features.contains(&ConformanceFeature::BodyJson));
1024    }
1025
1026    #[test]
1027    fn test_annotate_response_codes() {
1028        let mut op = Operation::default();
1029        op.responses
1030            .responses
1031            .insert(openapiv3::StatusCode::Code(200), ReferenceOr::Item(Response::default()));
1032        op.responses
1033            .responses
1034            .insert(openapiv3::StatusCode::Code(404), ReferenceOr::Item(Response::default()));
1035
1036        let api_op = make_op("get", "/items", op);
1037        let annotated = SpecDrivenConformanceGenerator::annotate_operation(&api_op, &empty_spec());
1038
1039        assert!(annotated.features.contains(&ConformanceFeature::Response200));
1040        assert!(annotated.features.contains(&ConformanceFeature::Response404));
1041    }
1042
1043    #[test]
1044    fn test_generate_spec_driven_script() {
1045        let config = ConformanceConfig {
1046            target_url: "http://localhost:3000".to_string(),
1047            api_key: None,
1048            basic_auth: None,
1049            skip_tls_verify: false,
1050            categories: None,
1051            base_path: None,
1052            custom_headers: vec![],
1053            output_dir: None,
1054            all_operations: false,
1055        };
1056
1057        let operations = vec![AnnotatedOperation {
1058            path: "/users/{id}".to_string(),
1059            method: "GET".to_string(),
1060            features: vec![
1061                ConformanceFeature::MethodGet,
1062                ConformanceFeature::PathParamString,
1063            ],
1064            request_body_content_type: None,
1065            sample_body: None,
1066            query_params: vec![],
1067            header_params: vec![],
1068            path_params: vec![("id".to_string(), "test-value".to_string())],
1069            response_schema: None,
1070        }];
1071
1072        let gen = SpecDrivenConformanceGenerator::new(config, operations);
1073        let (script, _check_count) = gen.generate().unwrap();
1074
1075        assert!(script.contains("import http from 'k6/http'"));
1076        assert!(script.contains("/users/test-value"));
1077        assert!(script.contains("param:path:string"));
1078        assert!(script.contains("method:GET"));
1079        assert!(script.contains("handleSummary"));
1080    }
1081
1082    #[test]
1083    fn test_generate_with_category_filter() {
1084        let config = ConformanceConfig {
1085            target_url: "http://localhost:3000".to_string(),
1086            api_key: None,
1087            basic_auth: None,
1088            skip_tls_verify: false,
1089            categories: Some(vec!["Parameters".to_string()]),
1090            base_path: None,
1091            custom_headers: vec![],
1092            output_dir: None,
1093            all_operations: false,
1094        };
1095
1096        let operations = vec![AnnotatedOperation {
1097            path: "/users/{id}".to_string(),
1098            method: "GET".to_string(),
1099            features: vec![
1100                ConformanceFeature::MethodGet,
1101                ConformanceFeature::PathParamString,
1102            ],
1103            request_body_content_type: None,
1104            sample_body: None,
1105            query_params: vec![],
1106            header_params: vec![],
1107            path_params: vec![("id".to_string(), "1".to_string())],
1108            response_schema: None,
1109        }];
1110
1111        let gen = SpecDrivenConformanceGenerator::new(config, operations);
1112        let (script, _check_count) = gen.generate().unwrap();
1113
1114        assert!(script.contains("group('Parameters'"));
1115        assert!(!script.contains("group('HTTP Methods'"));
1116    }
1117
1118    #[test]
1119    fn test_annotate_response_validation() {
1120        use openapiv3::ObjectType;
1121
1122        // Operation with a 200 response that has a JSON schema
1123        let mut op = Operation::default();
1124        let mut response = Response::default();
1125        let mut media = openapiv3::MediaType::default();
1126        let mut obj_type = ObjectType::default();
1127        obj_type.properties.insert(
1128            "name".to_string(),
1129            ReferenceOr::Item(Box::new(Schema {
1130                schema_data: SchemaData::default(),
1131                schema_kind: SchemaKind::Type(Type::String(StringType::default())),
1132            })),
1133        );
1134        obj_type.required = vec!["name".to_string()];
1135        media.schema = Some(ReferenceOr::Item(Schema {
1136            schema_data: SchemaData::default(),
1137            schema_kind: SchemaKind::Type(Type::Object(obj_type)),
1138        }));
1139        response.content.insert("application/json".to_string(), media);
1140        op.responses
1141            .responses
1142            .insert(openapiv3::StatusCode::Code(200), ReferenceOr::Item(response));
1143
1144        let api_op = make_op("get", "/users", op);
1145        let annotated = SpecDrivenConformanceGenerator::annotate_operation(&api_op, &empty_spec());
1146
1147        assert!(
1148            annotated.features.contains(&ConformanceFeature::ResponseValidation),
1149            "Should detect ResponseValidation when response has a JSON schema"
1150        );
1151        assert!(annotated.response_schema.is_some(), "Should extract the response schema");
1152
1153        // Verify generated script includes schema validation with try-catch
1154        let config = ConformanceConfig {
1155            target_url: "http://localhost:3000".to_string(),
1156            api_key: None,
1157            basic_auth: None,
1158            skip_tls_verify: false,
1159            categories: None,
1160            base_path: None,
1161            custom_headers: vec![],
1162            output_dir: None,
1163            all_operations: false,
1164        };
1165        let gen = SpecDrivenConformanceGenerator::new(config, vec![annotated]);
1166        let (script, _check_count) = gen.generate().unwrap();
1167
1168        assert!(
1169            script.contains("response:schema:validation"),
1170            "Script should contain the validation check name"
1171        );
1172        assert!(script.contains("try {"), "Script should wrap validation in try-catch");
1173        assert!(script.contains("res.json()"), "Script should parse response as JSON");
1174    }
1175
1176    #[test]
1177    fn test_annotate_global_security() {
1178        // Spec with global security requirement, operation without its own security
1179        let op = Operation::default();
1180        let mut spec = OpenAPI::default();
1181        let mut global_req = openapiv3::SecurityRequirement::new();
1182        global_req.insert("bearerAuth".to_string(), vec![]);
1183        spec.security = Some(vec![global_req]);
1184        // Define the security scheme in components
1185        let mut components = openapiv3::Components::default();
1186        components.security_schemes.insert(
1187            "bearerAuth".to_string(),
1188            ReferenceOr::Item(SecurityScheme::HTTP {
1189                scheme: "bearer".to_string(),
1190                bearer_format: Some("JWT".to_string()),
1191                description: None,
1192                extensions: Default::default(),
1193            }),
1194        );
1195        spec.components = Some(components);
1196
1197        let api_op = make_op("get", "/protected", op);
1198        let annotated = SpecDrivenConformanceGenerator::annotate_operation(&api_op, &spec);
1199
1200        assert!(
1201            annotated.features.contains(&ConformanceFeature::SecurityBearer),
1202            "Should detect SecurityBearer from global security + components"
1203        );
1204    }
1205
1206    #[test]
1207    fn test_annotate_security_scheme_resolution() {
1208        // Test that security scheme type is resolved from components, not just name heuristic
1209        let mut op = Operation::default();
1210        // Use a generic name that wouldn't match name heuristics
1211        let mut req = openapiv3::SecurityRequirement::new();
1212        req.insert("myAuth".to_string(), vec![]);
1213        op.security = Some(vec![req]);
1214
1215        let mut spec = OpenAPI::default();
1216        let mut components = openapiv3::Components::default();
1217        components.security_schemes.insert(
1218            "myAuth".to_string(),
1219            ReferenceOr::Item(SecurityScheme::APIKey {
1220                location: openapiv3::APIKeyLocation::Header,
1221                name: "X-API-Key".to_string(),
1222                description: None,
1223                extensions: Default::default(),
1224            }),
1225        );
1226        spec.components = Some(components);
1227
1228        let api_op = make_op("get", "/data", op);
1229        let annotated = SpecDrivenConformanceGenerator::annotate_operation(&api_op, &spec);
1230
1231        assert!(
1232            annotated.features.contains(&ConformanceFeature::SecurityApiKey),
1233            "Should detect SecurityApiKey from SecurityScheme::APIKey, not name heuristic"
1234        );
1235    }
1236
1237    #[test]
1238    fn test_annotate_content_negotiation() {
1239        let mut op = Operation::default();
1240        let mut response = Response::default();
1241        // Response with multiple content types
1242        response
1243            .content
1244            .insert("application/json".to_string(), openapiv3::MediaType::default());
1245        response
1246            .content
1247            .insert("application/xml".to_string(), openapiv3::MediaType::default());
1248        op.responses
1249            .responses
1250            .insert(openapiv3::StatusCode::Code(200), ReferenceOr::Item(response));
1251
1252        let api_op = make_op("get", "/items", op);
1253        let annotated = SpecDrivenConformanceGenerator::annotate_operation(&api_op, &empty_spec());
1254
1255        assert!(
1256            annotated.features.contains(&ConformanceFeature::ContentNegotiation),
1257            "Should detect ContentNegotiation when response has multiple content types"
1258        );
1259    }
1260
1261    #[test]
1262    fn test_no_content_negotiation_for_single_type() {
1263        let mut op = Operation::default();
1264        let mut response = Response::default();
1265        response
1266            .content
1267            .insert("application/json".to_string(), openapiv3::MediaType::default());
1268        op.responses
1269            .responses
1270            .insert(openapiv3::StatusCode::Code(200), ReferenceOr::Item(response));
1271
1272        let api_op = make_op("get", "/items", op);
1273        let annotated = SpecDrivenConformanceGenerator::annotate_operation(&api_op, &empty_spec());
1274
1275        assert!(
1276            !annotated.features.contains(&ConformanceFeature::ContentNegotiation),
1277            "Should NOT detect ContentNegotiation for a single content type"
1278        );
1279    }
1280
1281    #[test]
1282    fn test_spec_driven_with_base_path() {
1283        let annotated = AnnotatedOperation {
1284            path: "/users".to_string(),
1285            method: "GET".to_string(),
1286            features: vec![ConformanceFeature::MethodGet],
1287            path_params: vec![],
1288            query_params: vec![],
1289            header_params: vec![],
1290            request_body_content_type: None,
1291            sample_body: None,
1292            response_schema: None,
1293        };
1294        let config = ConformanceConfig {
1295            target_url: "https://192.168.2.86/".to_string(),
1296            api_key: None,
1297            basic_auth: None,
1298            skip_tls_verify: true,
1299            categories: None,
1300            base_path: Some("/api".to_string()),
1301            custom_headers: vec![],
1302            output_dir: None,
1303            all_operations: false,
1304        };
1305        let gen = SpecDrivenConformanceGenerator::new(config, vec![annotated]);
1306        let (script, _check_count) = gen.generate().unwrap();
1307
1308        assert!(
1309            script.contains("const BASE_URL = 'https://192.168.2.86/api'"),
1310            "BASE_URL should include the base_path. Got: {}",
1311            script.lines().find(|l| l.contains("BASE_URL")).unwrap_or("not found")
1312        );
1313    }
1314
1315    #[test]
1316    fn test_spec_driven_with_custom_headers() {
1317        let annotated = AnnotatedOperation {
1318            path: "/users".to_string(),
1319            method: "GET".to_string(),
1320            features: vec![ConformanceFeature::MethodGet],
1321            path_params: vec![],
1322            query_params: vec![],
1323            header_params: vec![
1324                ("X-Avi-Tenant".to_string(), "test-value".to_string()),
1325                ("X-CSRFToken".to_string(), "test-value".to_string()),
1326            ],
1327            request_body_content_type: None,
1328            sample_body: None,
1329            response_schema: None,
1330        };
1331        let config = ConformanceConfig {
1332            target_url: "https://192.168.2.86/".to_string(),
1333            api_key: None,
1334            basic_auth: None,
1335            skip_tls_verify: true,
1336            categories: None,
1337            base_path: Some("/api".to_string()),
1338            custom_headers: vec![
1339                ("X-Avi-Tenant".to_string(), "admin".to_string()),
1340                ("X-CSRFToken".to_string(), "real-csrf-token".to_string()),
1341                ("Cookie".to_string(), "sessionid=abc123".to_string()),
1342            ],
1343            output_dir: None,
1344            all_operations: false,
1345        };
1346        let gen = SpecDrivenConformanceGenerator::new(config, vec![annotated]);
1347        let (script, _check_count) = gen.generate().unwrap();
1348
1349        // Custom headers should override spec-derived test-value placeholders
1350        assert!(
1351            script.contains("'X-Avi-Tenant': 'admin'"),
1352            "Should use custom value for X-Avi-Tenant, not test-value"
1353        );
1354        assert!(
1355            script.contains("'X-CSRFToken': 'real-csrf-token'"),
1356            "Should use custom value for X-CSRFToken, not test-value"
1357        );
1358        // Custom headers not in spec should be appended
1359        assert!(
1360            script.contains("'Cookie': 'sessionid=abc123'"),
1361            "Should include Cookie header from custom_headers"
1362        );
1363        // test-value should NOT appear
1364        assert!(
1365            !script.contains("'test-value'"),
1366            "test-value placeholders should be replaced by custom values"
1367        );
1368    }
1369
1370    #[test]
1371    fn test_effective_headers_merging() {
1372        let config = ConformanceConfig {
1373            target_url: "http://localhost".to_string(),
1374            api_key: None,
1375            basic_auth: None,
1376            skip_tls_verify: false,
1377            categories: None,
1378            base_path: None,
1379            custom_headers: vec![
1380                ("X-Auth".to_string(), "real-token".to_string()),
1381                ("Cookie".to_string(), "session=abc".to_string()),
1382            ],
1383            output_dir: None,
1384            all_operations: false,
1385        };
1386        let gen = SpecDrivenConformanceGenerator::new(config, vec![]);
1387
1388        // Spec headers with a matching custom header
1389        let spec_headers = vec![
1390            ("X-Auth".to_string(), "test-value".to_string()),
1391            ("X-Other".to_string(), "keep-this".to_string()),
1392        ];
1393        let effective = gen.effective_headers(&spec_headers);
1394
1395        // X-Auth should be overridden
1396        assert_eq!(effective[0], ("X-Auth".to_string(), "real-token".to_string()));
1397        // X-Other should be kept as-is
1398        assert_eq!(effective[1], ("X-Other".to_string(), "keep-this".to_string()));
1399        // Cookie should be appended
1400        assert_eq!(effective[2], ("Cookie".to_string(), "session=abc".to_string()));
1401    }
1402}