1use 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
18mod 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 {
34 reference: inner_ref,
35 } => {
36 let inner_name = inner_ref.strip_prefix("#/components/parameters/")?;
38 match components.parameters.get(inner_name)? {
39 ReferenceOr::Item(param) => Some(param),
40 ReferenceOr::Reference { .. } => None,
41 }
42 }
43 }
44 }
45 }
46 }
47
48 pub fn resolve_request_body<'a>(
49 body_ref: &'a ReferenceOr<RequestBody>,
50 spec: &'a OpenAPI,
51 ) -> Option<&'a RequestBody> {
52 match body_ref {
53 ReferenceOr::Item(body) => Some(body),
54 ReferenceOr::Reference { reference } => {
55 let name = reference.strip_prefix("#/components/requestBodies/")?;
56 let components = spec.components.as_ref()?;
57 match components.request_bodies.get(name)? {
58 ReferenceOr::Item(body) => Some(body),
59 ReferenceOr::Reference {
60 reference: inner_ref,
61 } => {
62 let inner_name = inner_ref.strip_prefix("#/components/requestBodies/")?;
64 match components.request_bodies.get(inner_name)? {
65 ReferenceOr::Item(body) => Some(body),
66 ReferenceOr::Reference { .. } => None,
67 }
68 }
69 }
70 }
71 }
72 }
73
74 pub fn resolve_schema<'a>(
75 schema_ref: &'a ReferenceOr<Schema>,
76 spec: &'a OpenAPI,
77 ) -> Option<&'a Schema> {
78 resolve_schema_with_visited(schema_ref, spec, &mut HashSet::new())
79 }
80
81 fn resolve_schema_with_visited<'a>(
82 schema_ref: &'a ReferenceOr<Schema>,
83 spec: &'a OpenAPI,
84 visited: &mut HashSet<String>,
85 ) -> Option<&'a Schema> {
86 match schema_ref {
87 ReferenceOr::Item(schema) => Some(schema),
88 ReferenceOr::Reference { reference } => {
89 if !visited.insert(reference.clone()) {
90 return None; }
92 let name = reference.strip_prefix("#/components/schemas/")?;
93 let components = spec.components.as_ref()?;
94 let nested = components.schemas.get(name)?;
95 resolve_schema_with_visited(nested, spec, visited)
96 }
97 }
98 }
99
100 pub fn resolve_boxed_schema<'a>(
102 schema_ref: &'a ReferenceOr<Box<Schema>>,
103 spec: &'a OpenAPI,
104 ) -> Option<&'a Schema> {
105 match schema_ref {
106 ReferenceOr::Item(schema) => Some(schema.as_ref()),
107 ReferenceOr::Reference { reference } => {
108 let name = reference.strip_prefix("#/components/schemas/")?;
110 let components = spec.components.as_ref()?;
111 let nested = components.schemas.get(name)?;
112 resolve_schema_with_visited(nested, spec, &mut HashSet::new())
113 }
114 }
115 }
116
117 pub fn resolve_response<'a>(
118 resp_ref: &'a ReferenceOr<Response>,
119 spec: &'a OpenAPI,
120 ) -> Option<&'a Response> {
121 match resp_ref {
122 ReferenceOr::Item(resp) => Some(resp),
123 ReferenceOr::Reference { reference } => {
124 let name = reference.strip_prefix("#/components/responses/")?;
125 let components = spec.components.as_ref()?;
126 match components.responses.get(name)? {
127 ReferenceOr::Item(resp) => Some(resp),
128 ReferenceOr::Reference {
129 reference: inner_ref,
130 } => {
131 let inner_name = inner_ref.strip_prefix("#/components/responses/")?;
133 match components.responses.get(inner_name)? {
134 ReferenceOr::Item(resp) => Some(resp),
135 ReferenceOr::Reference { .. } => None,
136 }
137 }
138 }
139 }
140 }
141 }
142}
143
144#[derive(Debug, Clone)]
146pub enum SecuritySchemeInfo {
147 Bearer,
149 Basic,
151 ApiKey {
153 location: ApiKeyLocation,
154 name: String,
155 },
156}
157
158#[derive(Debug, Clone, PartialEq)]
160pub enum ApiKeyLocation {
161 Header,
162 Query,
163 Cookie,
164}
165
166#[derive(Debug, Clone)]
168pub struct AnnotatedOperation {
169 pub path: String,
170 pub method: String,
171 pub features: Vec<ConformanceFeature>,
172 pub request_body_content_type: Option<String>,
173 pub sample_body: Option<String>,
174 pub query_params: Vec<(String, String)>,
175 pub header_params: Vec<(String, String)>,
176 pub path_params: Vec<(String, String)>,
177 pub response_schema: Option<Schema>,
179 pub security_schemes: Vec<SecuritySchemeInfo>,
181}
182
183pub struct SpecDrivenConformanceGenerator {
185 config: ConformanceConfig,
186 operations: Vec<AnnotatedOperation>,
187}
188
189impl SpecDrivenConformanceGenerator {
190 pub fn new(config: ConformanceConfig, operations: Vec<AnnotatedOperation>) -> Self {
191 Self { config, operations }
192 }
193
194 pub fn annotate_operations(
196 operations: &[ApiOperation],
197 spec: &OpenAPI,
198 ) -> Vec<AnnotatedOperation> {
199 operations.iter().map(|op| Self::annotate_operation(op, spec)).collect()
200 }
201
202 fn annotate_operation(op: &ApiOperation, spec: &OpenAPI) -> AnnotatedOperation {
204 let mut features = Vec::new();
205 let mut query_params = Vec::new();
206 let mut header_params = Vec::new();
207 let mut path_params = Vec::new();
208
209 match op.method.to_uppercase().as_str() {
211 "GET" => features.push(ConformanceFeature::MethodGet),
212 "POST" => features.push(ConformanceFeature::MethodPost),
213 "PUT" => features.push(ConformanceFeature::MethodPut),
214 "PATCH" => features.push(ConformanceFeature::MethodPatch),
215 "DELETE" => features.push(ConformanceFeature::MethodDelete),
216 "HEAD" => features.push(ConformanceFeature::MethodHead),
217 "OPTIONS" => features.push(ConformanceFeature::MethodOptions),
218 _ => {}
219 }
220
221 for param_ref in &op.operation.parameters {
223 if let Some(param) = ref_resolver::resolve_parameter(param_ref, spec) {
224 Self::annotate_parameter(
225 param,
226 spec,
227 &mut features,
228 &mut query_params,
229 &mut header_params,
230 &mut path_params,
231 );
232 }
233 }
234
235 for segment in op.path.split('/') {
237 if segment.starts_with('{') && segment.ends_with('}') {
238 let name = &segment[1..segment.len() - 1];
239 if !path_params.iter().any(|(n, _)| n == name) {
241 path_params.push((name.to_string(), "test-value".to_string()));
242 if !features.contains(&ConformanceFeature::PathParamString)
244 && !features.contains(&ConformanceFeature::PathParamInteger)
245 {
246 features.push(ConformanceFeature::PathParamString);
247 }
248 }
249 }
250 }
251
252 let mut request_body_content_type = None;
254 let mut sample_body = None;
255
256 let resolved_body = op
257 .operation
258 .request_body
259 .as_ref()
260 .and_then(|b| ref_resolver::resolve_request_body(b, spec));
261
262 if let Some(body) = resolved_body {
263 for (content_type, _media) in &body.content {
264 match content_type.as_str() {
265 "application/json" => {
266 features.push(ConformanceFeature::BodyJson);
267 request_body_content_type = Some("application/json".to_string());
268 if let Ok(template) = RequestGenerator::generate_template(op) {
270 if let Some(body_val) = &template.body {
271 sample_body = Some(body_val.to_string());
272 }
273 }
274 }
275 "application/x-www-form-urlencoded" => {
276 features.push(ConformanceFeature::BodyFormUrlencoded);
277 request_body_content_type =
278 Some("application/x-www-form-urlencoded".to_string());
279 }
280 "multipart/form-data" => {
281 features.push(ConformanceFeature::BodyMultipart);
282 request_body_content_type = Some("multipart/form-data".to_string());
283 }
284 _ => {}
285 }
286 }
287
288 if let Some(media) = body.content.get("application/json") {
290 if let Some(schema_ref) = &media.schema {
291 if let Some(schema) = ref_resolver::resolve_schema(schema_ref, spec) {
292 Self::annotate_schema(schema, spec, &mut features);
293 }
294 }
295 }
296 }
297
298 Self::annotate_responses(&op.operation, spec, &mut features);
300
301 let response_schema = Self::extract_response_schema(&op.operation, spec);
303 if response_schema.is_some() {
304 features.push(ConformanceFeature::ResponseValidation);
305 }
306
307 Self::annotate_content_negotiation(&op.operation, spec, &mut features);
309
310 let mut security_schemes = Vec::new();
312 Self::annotate_security(&op.operation, spec, &mut features, &mut security_schemes);
313
314 features.sort_by_key(|f| f.check_name());
316 features.dedup_by_key(|f| f.check_name());
317
318 AnnotatedOperation {
319 path: op.path.clone(),
320 method: op.method.to_uppercase(),
321 features,
322 request_body_content_type,
323 sample_body,
324 query_params,
325 header_params,
326 path_params,
327 response_schema,
328 security_schemes,
329 }
330 }
331
332 fn annotate_parameter(
334 param: &Parameter,
335 spec: &OpenAPI,
336 features: &mut Vec<ConformanceFeature>,
337 query_params: &mut Vec<(String, String)>,
338 header_params: &mut Vec<(String, String)>,
339 path_params: &mut Vec<(String, String)>,
340 ) {
341 let (location, data) = match param {
342 Parameter::Query { parameter_data, .. } => ("query", parameter_data),
343 Parameter::Path { parameter_data, .. } => ("path", parameter_data),
344 Parameter::Header { parameter_data, .. } => ("header", parameter_data),
345 Parameter::Cookie { .. } => {
346 features.push(ConformanceFeature::CookieParam);
347 return;
348 }
349 };
350
351 let is_integer = Self::param_schema_is_integer(data, spec);
353 let is_array = Self::param_schema_is_array(data, spec);
354
355 let sample = if is_integer {
357 "42".to_string()
358 } else if is_array {
359 "a,b".to_string()
360 } else {
361 "test-value".to_string()
362 };
363
364 match location {
365 "path" => {
366 if is_integer {
367 features.push(ConformanceFeature::PathParamInteger);
368 } else {
369 features.push(ConformanceFeature::PathParamString);
370 }
371 path_params.push((data.name.clone(), sample));
372 }
373 "query" => {
374 if is_array {
375 features.push(ConformanceFeature::QueryParamArray);
376 } else if is_integer {
377 features.push(ConformanceFeature::QueryParamInteger);
378 } else {
379 features.push(ConformanceFeature::QueryParamString);
380 }
381 query_params.push((data.name.clone(), sample));
382 }
383 "header" => {
384 features.push(ConformanceFeature::HeaderParam);
385 header_params.push((data.name.clone(), sample));
386 }
387 _ => {}
388 }
389
390 if let ParameterSchemaOrContent::Schema(schema_ref) = &data.format {
392 if let Some(schema) = ref_resolver::resolve_schema(schema_ref, spec) {
393 Self::annotate_schema(schema, spec, features);
394 }
395 }
396
397 if data.required {
399 features.push(ConformanceFeature::ConstraintRequired);
400 } else {
401 features.push(ConformanceFeature::ConstraintOptional);
402 }
403 }
404
405 fn param_schema_is_integer(data: &openapiv3::ParameterData, spec: &OpenAPI) -> bool {
406 if let ParameterSchemaOrContent::Schema(schema_ref) = &data.format {
407 if let Some(schema) = ref_resolver::resolve_schema(schema_ref, spec) {
408 return matches!(&schema.schema_kind, SchemaKind::Type(Type::Integer(_)));
409 }
410 }
411 false
412 }
413
414 fn param_schema_is_array(data: &openapiv3::ParameterData, spec: &OpenAPI) -> bool {
415 if let ParameterSchemaOrContent::Schema(schema_ref) = &data.format {
416 if let Some(schema) = ref_resolver::resolve_schema(schema_ref, spec) {
417 return matches!(&schema.schema_kind, SchemaKind::Type(Type::Array(_)));
418 }
419 }
420 false
421 }
422
423 fn annotate_schema(schema: &Schema, spec: &OpenAPI, features: &mut Vec<ConformanceFeature>) {
425 match &schema.schema_kind {
426 SchemaKind::Type(Type::String(s)) => {
427 features.push(ConformanceFeature::SchemaString);
428 match &s.format {
430 VariantOrUnknownOrEmpty::Item(StringFormat::Date) => {
431 features.push(ConformanceFeature::FormatDate);
432 }
433 VariantOrUnknownOrEmpty::Item(StringFormat::DateTime) => {
434 features.push(ConformanceFeature::FormatDateTime);
435 }
436 VariantOrUnknownOrEmpty::Unknown(fmt) => match fmt.as_str() {
437 "email" => features.push(ConformanceFeature::FormatEmail),
438 "uuid" => features.push(ConformanceFeature::FormatUuid),
439 "uri" | "url" => features.push(ConformanceFeature::FormatUri),
440 "ipv4" => features.push(ConformanceFeature::FormatIpv4),
441 "ipv6" => features.push(ConformanceFeature::FormatIpv6),
442 _ => {}
443 },
444 _ => {}
445 }
446 if s.pattern.is_some() {
448 features.push(ConformanceFeature::ConstraintPattern);
449 }
450 if !s.enumeration.is_empty() {
451 features.push(ConformanceFeature::ConstraintEnum);
452 }
453 if s.min_length.is_some() || s.max_length.is_some() {
454 features.push(ConformanceFeature::ConstraintMinMax);
455 }
456 }
457 SchemaKind::Type(Type::Integer(i)) => {
458 features.push(ConformanceFeature::SchemaInteger);
459 if i.minimum.is_some() || i.maximum.is_some() {
460 features.push(ConformanceFeature::ConstraintMinMax);
461 }
462 if !i.enumeration.is_empty() {
463 features.push(ConformanceFeature::ConstraintEnum);
464 }
465 }
466 SchemaKind::Type(Type::Number(n)) => {
467 features.push(ConformanceFeature::SchemaNumber);
468 if n.minimum.is_some() || n.maximum.is_some() {
469 features.push(ConformanceFeature::ConstraintMinMax);
470 }
471 }
472 SchemaKind::Type(Type::Boolean(_)) => {
473 features.push(ConformanceFeature::SchemaBoolean);
474 }
475 SchemaKind::Type(Type::Array(arr)) => {
476 features.push(ConformanceFeature::SchemaArray);
477 if let Some(item_ref) = &arr.items {
478 if let Some(item_schema) = ref_resolver::resolve_boxed_schema(item_ref, spec) {
479 Self::annotate_schema(item_schema, spec, features);
480 }
481 }
482 }
483 SchemaKind::Type(Type::Object(obj)) => {
484 features.push(ConformanceFeature::SchemaObject);
485 if !obj.required.is_empty() {
487 features.push(ConformanceFeature::ConstraintRequired);
488 }
489 for (_name, prop_ref) in &obj.properties {
491 if let Some(prop_schema) = ref_resolver::resolve_boxed_schema(prop_ref, spec) {
492 Self::annotate_schema(prop_schema, spec, features);
493 }
494 }
495 }
496 SchemaKind::OneOf { .. } => {
497 features.push(ConformanceFeature::CompositionOneOf);
498 }
499 SchemaKind::AnyOf { .. } => {
500 features.push(ConformanceFeature::CompositionAnyOf);
501 }
502 SchemaKind::AllOf { .. } => {
503 features.push(ConformanceFeature::CompositionAllOf);
504 }
505 _ => {}
506 }
507 }
508
509 fn annotate_responses(
511 operation: &Operation,
512 spec: &OpenAPI,
513 features: &mut Vec<ConformanceFeature>,
514 ) {
515 for (status_code, resp_ref) in &operation.responses.responses {
516 if ref_resolver::resolve_response(resp_ref, spec).is_some() {
518 match status_code {
519 openapiv3::StatusCode::Code(200) => {
520 features.push(ConformanceFeature::Response200)
521 }
522 openapiv3::StatusCode::Code(201) => {
523 features.push(ConformanceFeature::Response201)
524 }
525 openapiv3::StatusCode::Code(204) => {
526 features.push(ConformanceFeature::Response204)
527 }
528 openapiv3::StatusCode::Code(400) => {
529 features.push(ConformanceFeature::Response400)
530 }
531 openapiv3::StatusCode::Code(404) => {
532 features.push(ConformanceFeature::Response404)
533 }
534 _ => {}
535 }
536 }
537 }
538 }
539
540 fn extract_response_schema(operation: &Operation, spec: &OpenAPI) -> Option<Schema> {
543 for code in [200u16, 201] {
545 if let Some(resp_ref) =
546 operation.responses.responses.get(&openapiv3::StatusCode::Code(code))
547 {
548 if let Some(response) = ref_resolver::resolve_response(resp_ref, spec) {
549 if let Some(media) = response.content.get("application/json") {
550 if let Some(schema_ref) = &media.schema {
551 if let Some(schema) = ref_resolver::resolve_schema(schema_ref, spec) {
552 return Some(schema.clone());
553 }
554 }
555 }
556 }
557 }
558 }
559 None
560 }
561
562 fn annotate_content_negotiation(
564 operation: &Operation,
565 spec: &OpenAPI,
566 features: &mut Vec<ConformanceFeature>,
567 ) {
568 for (_status_code, resp_ref) in &operation.responses.responses {
569 if let Some(response) = ref_resolver::resolve_response(resp_ref, spec) {
570 if response.content.len() > 1 {
571 features.push(ConformanceFeature::ContentNegotiation);
572 return; }
574 }
575 }
576 }
577
578 fn annotate_security(
582 operation: &Operation,
583 spec: &OpenAPI,
584 features: &mut Vec<ConformanceFeature>,
585 security_schemes: &mut Vec<SecuritySchemeInfo>,
586 ) {
587 let security_reqs = operation.security.as_ref().or(spec.security.as_ref());
589
590 if let Some(security) = security_reqs {
591 for security_req in security {
592 for scheme_name in security_req.keys() {
593 if let Some(resolved) = Self::resolve_security_scheme(scheme_name, spec) {
595 match resolved {
596 SecurityScheme::HTTP { ref scheme, .. } => {
597 if scheme.eq_ignore_ascii_case("bearer") {
598 features.push(ConformanceFeature::SecurityBearer);
599 security_schemes.push(SecuritySchemeInfo::Bearer);
600 } else if scheme.eq_ignore_ascii_case("basic") {
601 features.push(ConformanceFeature::SecurityBasic);
602 security_schemes.push(SecuritySchemeInfo::Basic);
603 }
604 }
605 SecurityScheme::APIKey { location, name, .. } => {
606 features.push(ConformanceFeature::SecurityApiKey);
607 let loc = match location {
608 openapiv3::APIKeyLocation::Query => ApiKeyLocation::Query,
609 openapiv3::APIKeyLocation::Header => ApiKeyLocation::Header,
610 openapiv3::APIKeyLocation::Cookie => ApiKeyLocation::Cookie,
611 };
612 security_schemes.push(SecuritySchemeInfo::ApiKey {
613 location: loc,
614 name: name.clone(),
615 });
616 }
617 _ => {}
619 }
620 } else {
621 let name_lower = scheme_name.to_lowercase();
623 if name_lower.contains("bearer") || name_lower.contains("jwt") {
624 features.push(ConformanceFeature::SecurityBearer);
625 security_schemes.push(SecuritySchemeInfo::Bearer);
626 } else if name_lower.contains("api") && name_lower.contains("key") {
627 features.push(ConformanceFeature::SecurityApiKey);
628 security_schemes.push(SecuritySchemeInfo::ApiKey {
629 location: ApiKeyLocation::Header,
630 name: "X-API-Key".to_string(),
631 });
632 } else if name_lower.contains("basic") {
633 features.push(ConformanceFeature::SecurityBasic);
634 security_schemes.push(SecuritySchemeInfo::Basic);
635 }
636 }
637 }
638 }
639 }
640 }
641
642 fn resolve_security_scheme<'a>(name: &str, spec: &'a OpenAPI) -> Option<&'a SecurityScheme> {
644 let components = spec.components.as_ref()?;
645 match components.security_schemes.get(name)? {
646 ReferenceOr::Item(scheme) => Some(scheme),
647 ReferenceOr::Reference { .. } => None,
648 }
649 }
650
651 pub fn operation_count(&self) -> usize {
653 self.operations.len()
654 }
655
656 pub fn generate(&self) -> Result<(String, usize)> {
659 let mut script = String::with_capacity(16384);
660
661 script.push_str("import http from 'k6/http';\n");
663 script.push_str("import { check, group } from 'k6';\n");
664 if self.config.request_delay_ms > 0 {
665 script.push_str("import { sleep } from 'k6';\n");
666 }
667 script.push('\n');
668
669 script.push_str(
673 "http.setResponseCallback(http.expectedStatuses({ min: 100, max: 599 }));\n\n",
674 );
675
676 script.push_str("export const options = {\n");
678 script.push_str(" vus: 1,\n");
679 script.push_str(" iterations: 1,\n");
680 if self.config.skip_tls_verify {
681 script.push_str(" insecureSkipTLSVerify: true,\n");
682 }
683 script.push_str(" thresholds: {\n");
684 script.push_str(" checks: ['rate>0'],\n");
685 script.push_str(" },\n");
686 script.push_str("};\n\n");
687
688 script.push_str(&format!("const BASE_URL = '{}';\n\n", self.config.effective_base_url()));
690 script.push_str("const JSON_HEADERS = { 'Content-Type': 'application/json' };\n\n");
691
692 script
695 .push_str("function __captureFailure(checkName, res, expected, schemaViolations) {\n");
696 script.push_str(" let bodyStr = '';\n");
697 script.push_str(" try { bodyStr = res.body ? res.body.substring(0, 2000) : ''; } catch(e) { bodyStr = '<unreadable>'; }\n");
698 script.push_str(" let reqHeaders = {};\n");
699 script.push_str(
700 " if (res.request && res.request.headers) { reqHeaders = res.request.headers; }\n",
701 );
702 script.push_str(" let reqBody = '';\n");
703 script.push_str(" if (res.request && res.request.body) { try { reqBody = res.request.body.substring(0, 2000); } catch(e) {} }\n");
704 script.push_str(" let payload = {\n");
705 script.push_str(" check: checkName,\n");
706 script.push_str(" request: {\n");
707 script.push_str(" method: res.request ? res.request.method : 'unknown',\n");
708 script.push_str(" url: res.request ? res.request.url : res.url || 'unknown',\n");
709 script.push_str(" headers: reqHeaders,\n");
710 script.push_str(" body: reqBody,\n");
711 script.push_str(" },\n");
712 script.push_str(" response: {\n");
713 script.push_str(" status: res.status,\n");
714 script.push_str(" headers: res.headers ? Object.fromEntries(Object.entries(res.headers).slice(0, 20)) : {},\n");
715 script.push_str(" body: bodyStr,\n");
716 script.push_str(" },\n");
717 script.push_str(" expected: expected,\n");
718 script.push_str(" };\n");
719 script.push_str(" if (schemaViolations && schemaViolations.length > 0) { payload.schema_violations = schemaViolations; }\n");
720 script.push_str(" console.log('MOCKFORGE_FAILURE:' + JSON.stringify(payload));\n");
721 script.push_str("}\n\n");
722
723 if self.config.export_requests {
725 script.push_str("function __captureExchange(checkName, res) {\n");
726 script.push_str(" let bodyStr = '';\n");
727 script.push_str(" try { bodyStr = res.body ? res.body.substring(0, 2000) : ''; } catch(e) { bodyStr = '<unreadable>'; }\n");
728 script.push_str(" let reqHeaders = {};\n");
729 script.push_str(
730 " if (res.request && res.request.headers) { reqHeaders = res.request.headers; }\n",
731 );
732 script.push_str(" let reqBody = '';\n");
733 script.push_str(" if (res.request && res.request.body) { try { reqBody = res.request.body.substring(0, 2000); } catch(e) {} }\n");
734 script.push_str(" console.log('MOCKFORGE_EXCHANGE:' + JSON.stringify({\n");
735 script.push_str(" check: checkName,\n");
736 script.push_str(" request: {\n");
737 script.push_str(" method: res.request ? res.request.method : 'unknown',\n");
738 script.push_str(" url: res.request ? res.request.url : res.url || 'unknown',\n");
739 script.push_str(" headers: reqHeaders,\n");
740 script.push_str(" body: reqBody,\n");
741 script.push_str(" },\n");
742 script.push_str(" response: {\n");
743 script.push_str(" status: res.status,\n");
744 script.push_str(" headers: res.headers ? Object.fromEntries(Object.entries(res.headers).slice(0, 30)) : {},\n");
745 script.push_str(" body: bodyStr,\n");
746 script.push_str(" },\n");
747 script.push_str(" }));\n");
748 script.push_str("}\n\n");
749 }
750
751 script.push_str("export default function () {\n");
753
754 if self.config.has_cookie_header() {
755 script.push_str(
756 " // Clear cookie jar to prevent server Set-Cookie from duplicating custom Cookie header\n",
757 );
758 script.push_str(" http.cookieJar().clear(BASE_URL);\n\n");
759 }
760
761 let mut category_ops: std::collections::BTreeMap<
763 &'static str,
764 Vec<(&AnnotatedOperation, &ConformanceFeature)>,
765 > = std::collections::BTreeMap::new();
766
767 for op in &self.operations {
768 for feature in &op.features {
769 let category = feature.category();
770 if self.config.should_include_category(category) {
771 category_ops.entry(category).or_default().push((op, feature));
772 }
773 }
774 }
775
776 let mut total_checks = 0usize;
778 for (category, ops) in &category_ops {
779 script.push_str(&format!(" group('{}', function () {{\n", category));
780
781 if self.config.all_operations {
782 let mut emitted_checks: HashSet<String> = HashSet::new();
784 for (op, feature) in ops {
785 let qualified = format!("{}:{}", feature.check_name(), op.path);
786 if emitted_checks.insert(qualified.clone()) {
787 self.emit_check_named(&mut script, op, feature, &qualified);
788 total_checks += 1;
789 }
790 }
791 } else {
792 let mut emitted_features: HashSet<&str> = HashSet::new();
795 for (op, feature) in ops {
796 if emitted_features.insert(feature.check_name()) {
797 let qualified = format!("{}:{}", feature.check_name(), op.path);
798 self.emit_check_named(&mut script, op, feature, &qualified);
799 total_checks += 1;
800 }
801 }
802 }
803
804 script.push_str(" });\n\n");
805 }
806
807 if let Some(custom_group) = self.config.generate_custom_group()? {
809 script.push_str(&custom_group);
810 }
811
812 script.push_str("}\n\n");
813
814 self.generate_handle_summary(&mut script);
816
817 Ok((script, total_checks))
818 }
819
820 fn emit_check_named(
822 &self,
823 script: &mut String,
824 op: &AnnotatedOperation,
825 feature: &ConformanceFeature,
826 check_name: &str,
827 ) {
828 let check_name = check_name.replace('\'', "\\'");
830 let check_name = check_name.as_str();
831
832 script.push_str(" {\n");
833
834 let mut url_path = op.path.clone();
836 for (name, value) in &op.path_params {
837 url_path = url_path.replace(&format!("{{{}}}", name), value);
838 }
839
840 if !op.query_params.is_empty() {
842 let qs: Vec<String> =
843 op.query_params.iter().map(|(k, v)| format!("{}={}", k, v)).collect();
844 url_path = format!("{}?{}", url_path, qs.join("&"));
845 }
846
847 let full_url = format!("${{BASE_URL}}{}", url_path);
848
849 let mut effective_headers = self.effective_headers(&op.header_params);
852
853 if matches!(feature, ConformanceFeature::Response400 | ConformanceFeature::Response404) {
856 let expected_code = match feature {
857 ConformanceFeature::Response400 => "400",
858 ConformanceFeature::Response404 => "404",
859 _ => unreachable!(),
860 };
861 effective_headers
862 .push(("X-Mockforge-Response-Status".to_string(), expected_code.to_string()));
863 }
864
865 let needs_auth = matches!(
869 feature,
870 ConformanceFeature::SecurityBearer
871 | ConformanceFeature::SecurityBasic
872 | ConformanceFeature::SecurityApiKey
873 ) || !op.security_schemes.is_empty();
874
875 if needs_auth {
876 self.inject_security_headers(&op.security_schemes, &mut effective_headers);
877 }
878
879 let has_headers = !effective_headers.is_empty();
880 let headers_obj = if has_headers {
881 Self::format_headers(&effective_headers)
882 } else {
883 String::new()
884 };
885
886 match op.method.as_str() {
888 "GET" => {
889 if has_headers {
890 script.push_str(&format!(
891 " let res = http.get(`{}`, {{ headers: {} }});\n",
892 full_url, headers_obj
893 ));
894 } else {
895 script.push_str(&format!(" let res = http.get(`{}`);\n", full_url));
896 }
897 }
898 "POST" => {
899 self.emit_request_with_body(script, "post", &full_url, op, &effective_headers);
900 }
901 "PUT" => {
902 self.emit_request_with_body(script, "put", &full_url, op, &effective_headers);
903 }
904 "PATCH" => {
905 self.emit_request_with_body(script, "patch", &full_url, op, &effective_headers);
906 }
907 "DELETE" => {
908 if has_headers {
909 script.push_str(&format!(
910 " let res = http.del(`{}`, null, {{ headers: {} }});\n",
911 full_url, headers_obj
912 ));
913 } else {
914 script.push_str(&format!(" let res = http.del(`{}`);\n", full_url));
915 }
916 }
917 "HEAD" => {
918 if has_headers {
919 script.push_str(&format!(
920 " let res = http.head(`{}`, {{ headers: {} }});\n",
921 full_url, headers_obj
922 ));
923 } else {
924 script.push_str(&format!(" let res = http.head(`{}`);\n", full_url));
925 }
926 }
927 "OPTIONS" => {
928 if has_headers {
929 script.push_str(&format!(
930 " let res = http.options(`{}`, null, {{ headers: {} }});\n",
931 full_url, headers_obj
932 ));
933 } else {
934 script.push_str(&format!(" let res = http.options(`{}`);\n", full_url));
935 }
936 }
937 _ => {
938 if has_headers {
939 script.push_str(&format!(
940 " let res = http.get(`{}`, {{ headers: {} }});\n",
941 full_url, headers_obj
942 ));
943 } else {
944 script.push_str(&format!(" let res = http.get(`{}`);\n", full_url));
945 }
946 }
947 }
948
949 if matches!(
951 feature,
952 ConformanceFeature::Response200
953 | ConformanceFeature::Response201
954 | ConformanceFeature::Response204
955 | ConformanceFeature::Response400
956 | ConformanceFeature::Response404
957 ) {
958 let expected_code = match feature {
959 ConformanceFeature::Response200 => 200,
960 ConformanceFeature::Response201 => 201,
961 ConformanceFeature::Response204 => 204,
962 ConformanceFeature::Response400 => 400,
963 ConformanceFeature::Response404 => 404,
964 _ => 200,
965 };
966 script.push_str(&format!(
967 " {{ let ok = check(res, {{ '{}': (r) => r.status === {} }}); if (!ok) __captureFailure('{}', res, 'status === {}'); }}\n",
968 check_name, expected_code, check_name, expected_code
969 ));
970 } else if matches!(feature, ConformanceFeature::ResponseValidation) {
971 if let Some(schema) = &op.response_schema {
976 let validation_js = SchemaValidatorGenerator::generate_validation(schema);
977 let schema_json = serde_json::to_string(schema).unwrap_or_default();
978 let schema_json_escaped = schema_json.replace('\\', "\\\\").replace('`', "\\`");
980 script.push_str(&format!(
981 concat!(
982 " try {{\n",
983 " let body = res.json();\n",
984 " let ok = check(res, {{ '{check}': (r) => ( {validation} ) }});\n",
985 " if (!ok) {{\n",
986 " let __violations = [];\n",
987 " try {{\n",
988 " let __schema = JSON.parse(`{schema}`);\n",
989 " function __collectErrors(schema, data, path) {{\n",
990 " if (!schema || typeof schema !== 'object') return;\n",
991 " let st = schema.type || (schema.schema_kind && schema.schema_kind.Type && Object.keys(schema.schema_kind.Type)[0]);\n",
992 " if (st) {{ st = st.toLowerCase(); }}\n",
993 " if (st === 'object') {{\n",
994 " if (typeof data !== 'object' || data === null) {{ __violations.push({{ field_path: path || '/', violation_type: 'type', expected: 'object', actual: typeof data }}); return; }}\n",
995 " let props = schema.properties || (schema.schema_kind && schema.schema_kind.Type && schema.schema_kind.Type.Object && schema.schema_kind.Type.Object.properties) || {{}};\n",
996 " let req = schema.required || (schema.schema_kind && schema.schema_kind.Type && schema.schema_kind.Type.Object && schema.schema_kind.Type.Object.required) || [];\n",
997 " for (let f of req) {{ if (!(f in data)) {{ __violations.push({{ field_path: path + '/' + f, violation_type: 'required', expected: 'present', actual: 'missing' }}); }} }}\n",
998 " for (let [k, v] of Object.entries(props)) {{ if (data[k] !== undefined) {{ let ps = v.Item || v; __collectErrors(ps, data[k], path + '/' + k); }} }}\n",
999 " }} else if (st === 'array') {{\n",
1000 " if (!Array.isArray(data)) {{ __violations.push({{ field_path: path || '/', violation_type: 'type', expected: 'array', actual: typeof data }}); return; }}\n",
1001 " let items = schema.items || (schema.schema_kind && schema.schema_kind.Type && schema.schema_kind.Type.Array && schema.schema_kind.Type.Array.items);\n",
1002 " if (items) {{ let is = items.Item || items; for (let i = 0; i < Math.min(data.length, 5); i++) {{ __collectErrors(is, data[i], path + '/' + i); }} }}\n",
1003 " }} else if (st === 'string') {{\n",
1004 " if (typeof data !== 'string') {{ __violations.push({{ field_path: path || '/', violation_type: 'type', expected: 'string', actual: typeof data }}); }}\n",
1005 " }} else if (st === 'integer') {{\n",
1006 " if (typeof data !== 'number' || !Number.isInteger(data)) {{ __violations.push({{ field_path: path || '/', violation_type: 'type', expected: 'integer', actual: typeof data }}); }}\n",
1007 " }} else if (st === 'number') {{\n",
1008 " if (typeof data !== 'number') {{ __violations.push({{ field_path: path || '/', violation_type: 'type', expected: 'number', actual: typeof data }}); }}\n",
1009 " }} else if (st === 'boolean') {{\n",
1010 " if (typeof data !== 'boolean') {{ __violations.push({{ field_path: path || '/', violation_type: 'type', expected: 'boolean', actual: typeof data }}); }}\n",
1011 " }}\n",
1012 " }}\n",
1013 " __collectErrors(__schema, body, '');\n",
1014 " }} catch(_e) {{}}\n",
1015 " __captureFailure('{check}', res, 'schema validation', __violations);\n",
1016 " }}\n",
1017 " }} catch(e) {{ check(res, {{ '{check}': () => false }}); __captureFailure('{check}', res, 'JSON parse failed: ' + e.message); }}\n",
1018 ),
1019 check = check_name,
1020 validation = validation_js,
1021 schema = schema_json_escaped,
1022 ));
1023 }
1024 } else if matches!(
1025 feature,
1026 ConformanceFeature::SecurityBearer
1027 | ConformanceFeature::SecurityBasic
1028 | ConformanceFeature::SecurityApiKey
1029 ) {
1030 script.push_str(&format!(
1032 " {{ let ok = check(res, {{ '{}': (r) => r.status >= 200 && r.status < 400 }}); if (!ok) __captureFailure('{}', res, 'status >= 200 && status < 400 (auth accepted)'); }}\n",
1033 check_name, check_name
1034 ));
1035 } else {
1036 script.push_str(&format!(
1037 " {{ let ok = check(res, {{ '{}': (r) => r.status >= 200 && r.status < 500 }}); if (!ok) __captureFailure('{}', res, 'status >= 200 && status < 500'); }}\n",
1038 check_name, check_name
1039 ));
1040 }
1041
1042 let has_cookie = self.config.has_cookie_header()
1044 || effective_headers.iter().any(|(h, _)| h.eq_ignore_ascii_case("Cookie"));
1045 if has_cookie {
1046 script.push_str(" http.cookieJar().clear(BASE_URL);\n");
1047 }
1048
1049 script.push_str(" }\n");
1050
1051 if self.config.request_delay_ms > 0 {
1053 script.push_str(&format!(
1054 " sleep({:.3});\n",
1055 self.config.request_delay_ms as f64 / 1000.0
1056 ));
1057 }
1058 }
1059
1060 fn emit_request_with_body(
1062 &self,
1063 script: &mut String,
1064 method: &str,
1065 url: &str,
1066 op: &AnnotatedOperation,
1067 effective_headers: &[(String, String)],
1068 ) {
1069 if let Some(body) = &op.sample_body {
1070 let escaped_body = body.replace('\'', "\\'");
1071 let headers = if !effective_headers.is_empty() {
1072 format!(
1073 "Object.assign({{}}, JSON_HEADERS, {})",
1074 Self::format_headers(effective_headers)
1075 )
1076 } else {
1077 "JSON_HEADERS".to_string()
1078 };
1079 script.push_str(&format!(
1080 " let res = http.{}(`{}`, '{}', {{ headers: {} }});\n",
1081 method, url, escaped_body, headers
1082 ));
1083 } else if !effective_headers.is_empty() {
1084 script.push_str(&format!(
1085 " let res = http.{}(`{}`, null, {{ headers: {} }});\n",
1086 method,
1087 url,
1088 Self::format_headers(effective_headers)
1089 ));
1090 } else {
1091 script.push_str(&format!(" let res = http.{}(`{}`, null);\n", method, url));
1092 }
1093 }
1094
1095 fn effective_headers(&self, spec_headers: &[(String, String)]) -> Vec<(String, String)> {
1099 let custom = &self.config.custom_headers;
1100 if custom.is_empty() {
1101 return spec_headers.to_vec();
1102 }
1103
1104 let mut result: Vec<(String, String)> = Vec::new();
1105
1106 for (name, value) in spec_headers {
1108 if let Some((_, custom_val)) =
1109 custom.iter().find(|(cn, _)| cn.eq_ignore_ascii_case(name))
1110 {
1111 result.push((name.clone(), custom_val.clone()));
1112 } else {
1113 result.push((name.clone(), value.clone()));
1114 }
1115 }
1116
1117 for (name, value) in custom {
1119 if !spec_headers.iter().any(|(sn, _)| sn.eq_ignore_ascii_case(name)) {
1120 result.push((name.clone(), value.clone()));
1121 }
1122 }
1123
1124 result
1125 }
1126
1127 fn inject_security_headers(
1130 &self,
1131 schemes: &[SecuritySchemeInfo],
1132 headers: &mut Vec<(String, String)>,
1133 ) {
1134 let mut to_add: Vec<(String, String)> = Vec::new();
1135
1136 let has_header = |name: &str, headers: &[(String, String)]| {
1137 headers.iter().any(|(h, _)| h.eq_ignore_ascii_case(name))
1138 || self.config.custom_headers.iter().any(|(h, _)| h.eq_ignore_ascii_case(name))
1139 };
1140
1141 let has_cookie_auth = has_header("Cookie", headers);
1143
1144 for scheme in schemes {
1145 match scheme {
1146 SecuritySchemeInfo::Bearer => {
1147 if !has_cookie_auth && !has_header("Authorization", headers) {
1148 to_add.push((
1150 "Authorization".to_string(),
1151 "Bearer mockforge-conformance-test-token".to_string(),
1152 ));
1153 }
1154 }
1155 SecuritySchemeInfo::Basic => {
1156 if !has_cookie_auth && !has_header("Authorization", headers) {
1157 let creds = self.config.basic_auth.as_deref().unwrap_or("test:test");
1158 use base64::Engine;
1159 let encoded =
1160 base64::engine::general_purpose::STANDARD.encode(creds.as_bytes());
1161 to_add.push(("Authorization".to_string(), format!("Basic {}", encoded)));
1162 }
1163 }
1164 SecuritySchemeInfo::ApiKey { location, name } => match location {
1165 ApiKeyLocation::Header => {
1166 if !has_header(name, headers) {
1167 let key = self
1168 .config
1169 .api_key
1170 .as_deref()
1171 .unwrap_or("mockforge-conformance-test-key");
1172 to_add.push((name.clone(), key.to_string()));
1173 }
1174 }
1175 ApiKeyLocation::Cookie => {
1176 if !has_header("Cookie", headers) {
1177 to_add.push((
1178 "Cookie".to_string(),
1179 format!("{}=mockforge-conformance-test-session", name),
1180 ));
1181 }
1182 }
1183 ApiKeyLocation::Query => {
1184 }
1186 },
1187 }
1188 }
1189
1190 headers.extend(to_add);
1191 }
1192
1193 fn format_headers(headers: &[(String, String)]) -> String {
1195 let entries: Vec<String> = headers
1196 .iter()
1197 .map(|(k, v)| format!("'{}': '{}'", k, v.replace('\'', "\\'")))
1198 .collect();
1199 format!("{{ {} }}", entries.join(", "))
1200 }
1201
1202 fn generate_handle_summary(&self, script: &mut String) {
1204 let report_path = match &self.config.output_dir {
1206 Some(dir) => {
1207 let abs = std::fs::canonicalize(dir)
1208 .unwrap_or_else(|_| dir.clone())
1209 .join("conformance-report.json");
1210 abs.to_string_lossy().to_string()
1211 }
1212 None => "conformance-report.json".to_string(),
1213 };
1214
1215 script.push_str("export function handleSummary(data) {\n");
1216 script.push_str(" let checks = {};\n");
1217 script.push_str(" if (data.metrics && data.metrics.checks) {\n");
1218 script.push_str(" checks.overall_pass_rate = data.metrics.checks.values.rate;\n");
1219 script.push_str(" }\n");
1220 script.push_str(" let checkResults = {};\n");
1221 script.push_str(" function walkGroups(group) {\n");
1222 script.push_str(" if (group.checks) {\n");
1223 script.push_str(" for (let checkObj of group.checks) {\n");
1224 script.push_str(" checkResults[checkObj.name] = {\n");
1225 script.push_str(" passes: checkObj.passes,\n");
1226 script.push_str(" fails: checkObj.fails,\n");
1227 script.push_str(" };\n");
1228 script.push_str(" }\n");
1229 script.push_str(" }\n");
1230 script.push_str(" if (group.groups) {\n");
1231 script.push_str(" for (let subGroup of group.groups) {\n");
1232 script.push_str(" walkGroups(subGroup);\n");
1233 script.push_str(" }\n");
1234 script.push_str(" }\n");
1235 script.push_str(" }\n");
1236 script.push_str(" if (data.root_group) {\n");
1237 script.push_str(" walkGroups(data.root_group);\n");
1238 script.push_str(" }\n");
1239 script.push_str(" return {\n");
1240 script.push_str(&format!(
1241 " '{}': JSON.stringify({{ checks: checkResults, overall: checks }}, null, 2),\n",
1242 report_path
1243 ));
1244 script.push_str(" 'summary.json': JSON.stringify(data),\n");
1245 script.push_str(" stdout: textSummary(data, { indent: ' ', enableColors: true }),\n");
1246 script.push_str(" };\n");
1247 script.push_str("}\n\n");
1248 script.push_str("function textSummary(data, opts) {\n");
1249 script.push_str(" return JSON.stringify(data, null, 2);\n");
1250 script.push_str("}\n");
1251 }
1252}
1253
1254#[cfg(test)]
1255mod tests {
1256 use super::*;
1257 use openapiv3::{
1258 Operation, ParameterData, ParameterSchemaOrContent, PathStyle, Response, Schema,
1259 SchemaData, SchemaKind, StringType, Type,
1260 };
1261
1262 fn make_op(method: &str, path: &str, operation: Operation) -> ApiOperation {
1263 ApiOperation {
1264 method: method.to_string(),
1265 path: path.to_string(),
1266 operation,
1267 operation_id: None,
1268 }
1269 }
1270
1271 fn empty_spec() -> OpenAPI {
1272 OpenAPI::default()
1273 }
1274
1275 #[test]
1276 fn test_annotate_get_with_path_param() {
1277 let mut op = Operation::default();
1278 op.parameters.push(ReferenceOr::Item(Parameter::Path {
1279 parameter_data: ParameterData {
1280 name: "id".to_string(),
1281 description: None,
1282 required: true,
1283 deprecated: None,
1284 format: ParameterSchemaOrContent::Schema(ReferenceOr::Item(Schema {
1285 schema_data: SchemaData::default(),
1286 schema_kind: SchemaKind::Type(Type::String(StringType::default())),
1287 })),
1288 example: None,
1289 examples: Default::default(),
1290 explode: None,
1291 extensions: Default::default(),
1292 },
1293 style: PathStyle::Simple,
1294 }));
1295
1296 let api_op = make_op("get", "/users/{id}", op);
1297 let annotated = SpecDrivenConformanceGenerator::annotate_operation(&api_op, &empty_spec());
1298
1299 assert!(annotated.features.contains(&ConformanceFeature::MethodGet));
1300 assert!(annotated.features.contains(&ConformanceFeature::PathParamString));
1301 assert!(annotated.features.contains(&ConformanceFeature::ConstraintRequired));
1302 assert_eq!(annotated.path_params.len(), 1);
1303 assert_eq!(annotated.path_params[0].0, "id");
1304 }
1305
1306 #[test]
1307 fn test_annotate_post_with_json_body() {
1308 let mut op = Operation::default();
1309 let mut body = openapiv3::RequestBody {
1310 required: true,
1311 ..Default::default()
1312 };
1313 body.content
1314 .insert("application/json".to_string(), openapiv3::MediaType::default());
1315 op.request_body = Some(ReferenceOr::Item(body));
1316
1317 let api_op = make_op("post", "/items", op);
1318 let annotated = SpecDrivenConformanceGenerator::annotate_operation(&api_op, &empty_spec());
1319
1320 assert!(annotated.features.contains(&ConformanceFeature::MethodPost));
1321 assert!(annotated.features.contains(&ConformanceFeature::BodyJson));
1322 }
1323
1324 #[test]
1325 fn test_annotate_response_codes() {
1326 let mut op = Operation::default();
1327 op.responses
1328 .responses
1329 .insert(openapiv3::StatusCode::Code(200), ReferenceOr::Item(Response::default()));
1330 op.responses
1331 .responses
1332 .insert(openapiv3::StatusCode::Code(404), ReferenceOr::Item(Response::default()));
1333
1334 let api_op = make_op("get", "/items", op);
1335 let annotated = SpecDrivenConformanceGenerator::annotate_operation(&api_op, &empty_spec());
1336
1337 assert!(annotated.features.contains(&ConformanceFeature::Response200));
1338 assert!(annotated.features.contains(&ConformanceFeature::Response404));
1339 }
1340
1341 #[test]
1342 fn test_generate_spec_driven_script() {
1343 let config = ConformanceConfig {
1344 target_url: "http://localhost:3000".to_string(),
1345 api_key: None,
1346 basic_auth: None,
1347 skip_tls_verify: false,
1348 categories: None,
1349 base_path: None,
1350 custom_headers: vec![],
1351 output_dir: None,
1352 all_operations: false,
1353 custom_checks_file: None,
1354 request_delay_ms: 0,
1355 custom_filter: None,
1356 export_requests: false,
1357 };
1358
1359 let operations = vec![AnnotatedOperation {
1360 path: "/users/{id}".to_string(),
1361 method: "GET".to_string(),
1362 features: vec![
1363 ConformanceFeature::MethodGet,
1364 ConformanceFeature::PathParamString,
1365 ],
1366 request_body_content_type: None,
1367 sample_body: None,
1368 query_params: vec![],
1369 header_params: vec![],
1370 path_params: vec![("id".to_string(), "test-value".to_string())],
1371 response_schema: None,
1372 security_schemes: vec![],
1373 }];
1374
1375 let gen = SpecDrivenConformanceGenerator::new(config, operations);
1376 let (script, _check_count) = gen.generate().unwrap();
1377
1378 assert!(script.contains("import http from 'k6/http'"));
1379 assert!(script.contains("/users/test-value"));
1380 assert!(script.contains("param:path:string"));
1381 assert!(script.contains("method:GET"));
1382 assert!(script.contains("handleSummary"));
1383 }
1384
1385 #[test]
1386 fn test_generate_with_category_filter() {
1387 let config = ConformanceConfig {
1388 target_url: "http://localhost:3000".to_string(),
1389 api_key: None,
1390 basic_auth: None,
1391 skip_tls_verify: false,
1392 categories: Some(vec!["Parameters".to_string()]),
1393 base_path: None,
1394 custom_headers: vec![],
1395 output_dir: None,
1396 all_operations: false,
1397 custom_checks_file: None,
1398 request_delay_ms: 0,
1399 custom_filter: None,
1400 export_requests: false,
1401 };
1402
1403 let operations = vec![AnnotatedOperation {
1404 path: "/users/{id}".to_string(),
1405 method: "GET".to_string(),
1406 features: vec![
1407 ConformanceFeature::MethodGet,
1408 ConformanceFeature::PathParamString,
1409 ],
1410 request_body_content_type: None,
1411 sample_body: None,
1412 query_params: vec![],
1413 header_params: vec![],
1414 path_params: vec![("id".to_string(), "1".to_string())],
1415 response_schema: None,
1416 security_schemes: vec![],
1417 }];
1418
1419 let gen = SpecDrivenConformanceGenerator::new(config, operations);
1420 let (script, _check_count) = gen.generate().unwrap();
1421
1422 assert!(script.contains("group('Parameters'"));
1423 assert!(!script.contains("group('HTTP Methods'"));
1424 }
1425
1426 #[test]
1427 fn test_annotate_response_validation() {
1428 use openapiv3::ObjectType;
1429
1430 let mut op = Operation::default();
1432 let mut response = Response::default();
1433 let mut media = openapiv3::MediaType::default();
1434 let mut obj_type = ObjectType::default();
1435 obj_type.properties.insert(
1436 "name".to_string(),
1437 ReferenceOr::Item(Box::new(Schema {
1438 schema_data: SchemaData::default(),
1439 schema_kind: SchemaKind::Type(Type::String(StringType::default())),
1440 })),
1441 );
1442 obj_type.required = vec!["name".to_string()];
1443 media.schema = Some(ReferenceOr::Item(Schema {
1444 schema_data: SchemaData::default(),
1445 schema_kind: SchemaKind::Type(Type::Object(obj_type)),
1446 }));
1447 response.content.insert("application/json".to_string(), media);
1448 op.responses
1449 .responses
1450 .insert(openapiv3::StatusCode::Code(200), ReferenceOr::Item(response));
1451
1452 let api_op = make_op("get", "/users", op);
1453 let annotated = SpecDrivenConformanceGenerator::annotate_operation(&api_op, &empty_spec());
1454
1455 assert!(
1456 annotated.features.contains(&ConformanceFeature::ResponseValidation),
1457 "Should detect ResponseValidation when response has a JSON schema"
1458 );
1459 assert!(annotated.response_schema.is_some(), "Should extract the response schema");
1460
1461 let config = ConformanceConfig {
1463 target_url: "http://localhost:3000".to_string(),
1464 api_key: None,
1465 basic_auth: None,
1466 skip_tls_verify: false,
1467 categories: None,
1468 base_path: None,
1469 custom_headers: vec![],
1470 output_dir: None,
1471 all_operations: false,
1472 custom_checks_file: None,
1473 request_delay_ms: 0,
1474 custom_filter: None,
1475 export_requests: false,
1476 };
1477 let gen = SpecDrivenConformanceGenerator::new(config, vec![annotated]);
1478 let (script, _check_count) = gen.generate().unwrap();
1479
1480 assert!(
1481 script.contains("response:schema:validation"),
1482 "Script should contain the validation check name"
1483 );
1484 assert!(script.contains("try {"), "Script should wrap validation in try-catch");
1485 assert!(script.contains("res.json()"), "Script should parse response as JSON");
1486 }
1487
1488 #[test]
1489 fn test_annotate_global_security() {
1490 let op = Operation::default();
1492 let mut spec = OpenAPI::default();
1493 let mut global_req = openapiv3::SecurityRequirement::new();
1494 global_req.insert("bearerAuth".to_string(), vec![]);
1495 spec.security = Some(vec![global_req]);
1496 let mut components = openapiv3::Components::default();
1498 components.security_schemes.insert(
1499 "bearerAuth".to_string(),
1500 ReferenceOr::Item(SecurityScheme::HTTP {
1501 scheme: "bearer".to_string(),
1502 bearer_format: Some("JWT".to_string()),
1503 description: None,
1504 extensions: Default::default(),
1505 }),
1506 );
1507 spec.components = Some(components);
1508
1509 let api_op = make_op("get", "/protected", op);
1510 let annotated = SpecDrivenConformanceGenerator::annotate_operation(&api_op, &spec);
1511
1512 assert!(
1513 annotated.features.contains(&ConformanceFeature::SecurityBearer),
1514 "Should detect SecurityBearer from global security + components"
1515 );
1516 }
1517
1518 #[test]
1519 fn test_annotate_security_scheme_resolution() {
1520 let mut op = Operation::default();
1522 let mut req = openapiv3::SecurityRequirement::new();
1524 req.insert("myAuth".to_string(), vec![]);
1525 op.security = Some(vec![req]);
1526
1527 let mut spec = OpenAPI::default();
1528 let mut components = openapiv3::Components::default();
1529 components.security_schemes.insert(
1530 "myAuth".to_string(),
1531 ReferenceOr::Item(SecurityScheme::APIKey {
1532 location: openapiv3::APIKeyLocation::Header,
1533 name: "X-API-Key".to_string(),
1534 description: None,
1535 extensions: Default::default(),
1536 }),
1537 );
1538 spec.components = Some(components);
1539
1540 let api_op = make_op("get", "/data", op);
1541 let annotated = SpecDrivenConformanceGenerator::annotate_operation(&api_op, &spec);
1542
1543 assert!(
1544 annotated.features.contains(&ConformanceFeature::SecurityApiKey),
1545 "Should detect SecurityApiKey from SecurityScheme::APIKey, not name heuristic"
1546 );
1547 }
1548
1549 #[test]
1550 fn test_annotate_content_negotiation() {
1551 let mut op = Operation::default();
1552 let mut response = Response::default();
1553 response
1555 .content
1556 .insert("application/json".to_string(), openapiv3::MediaType::default());
1557 response
1558 .content
1559 .insert("application/xml".to_string(), openapiv3::MediaType::default());
1560 op.responses
1561 .responses
1562 .insert(openapiv3::StatusCode::Code(200), ReferenceOr::Item(response));
1563
1564 let api_op = make_op("get", "/items", op);
1565 let annotated = SpecDrivenConformanceGenerator::annotate_operation(&api_op, &empty_spec());
1566
1567 assert!(
1568 annotated.features.contains(&ConformanceFeature::ContentNegotiation),
1569 "Should detect ContentNegotiation when response has multiple content types"
1570 );
1571 }
1572
1573 #[test]
1574 fn test_no_content_negotiation_for_single_type() {
1575 let mut op = Operation::default();
1576 let mut response = Response::default();
1577 response
1578 .content
1579 .insert("application/json".to_string(), openapiv3::MediaType::default());
1580 op.responses
1581 .responses
1582 .insert(openapiv3::StatusCode::Code(200), ReferenceOr::Item(response));
1583
1584 let api_op = make_op("get", "/items", op);
1585 let annotated = SpecDrivenConformanceGenerator::annotate_operation(&api_op, &empty_spec());
1586
1587 assert!(
1588 !annotated.features.contains(&ConformanceFeature::ContentNegotiation),
1589 "Should NOT detect ContentNegotiation for a single content type"
1590 );
1591 }
1592
1593 #[test]
1594 fn test_spec_driven_with_base_path() {
1595 let annotated = AnnotatedOperation {
1596 path: "/users".to_string(),
1597 method: "GET".to_string(),
1598 features: vec![ConformanceFeature::MethodGet],
1599 path_params: vec![],
1600 query_params: vec![],
1601 header_params: vec![],
1602 request_body_content_type: None,
1603 sample_body: None,
1604 response_schema: None,
1605 security_schemes: vec![],
1606 };
1607 let config = ConformanceConfig {
1608 target_url: "https://192.168.2.86/".to_string(),
1609 api_key: None,
1610 basic_auth: None,
1611 skip_tls_verify: true,
1612 categories: None,
1613 base_path: Some("/api".to_string()),
1614 custom_headers: vec![],
1615 output_dir: None,
1616 all_operations: false,
1617 custom_checks_file: None,
1618 request_delay_ms: 0,
1619 custom_filter: None,
1620 export_requests: false,
1621 };
1622 let gen = SpecDrivenConformanceGenerator::new(config, vec![annotated]);
1623 let (script, _check_count) = gen.generate().unwrap();
1624
1625 assert!(
1626 script.contains("const BASE_URL = 'https://192.168.2.86/api'"),
1627 "BASE_URL should include the base_path. Got: {}",
1628 script.lines().find(|l| l.contains("BASE_URL")).unwrap_or("not found")
1629 );
1630 }
1631
1632 #[test]
1633 fn test_spec_driven_with_custom_headers() {
1634 let annotated = AnnotatedOperation {
1635 path: "/users".to_string(),
1636 method: "GET".to_string(),
1637 features: vec![ConformanceFeature::MethodGet],
1638 path_params: vec![],
1639 query_params: vec![],
1640 header_params: vec![
1641 ("X-Avi-Tenant".to_string(), "test-value".to_string()),
1642 ("X-CSRFToken".to_string(), "test-value".to_string()),
1643 ],
1644 request_body_content_type: None,
1645 sample_body: None,
1646 response_schema: None,
1647 security_schemes: vec![],
1648 };
1649 let config = ConformanceConfig {
1650 target_url: "https://192.168.2.86/".to_string(),
1651 api_key: None,
1652 basic_auth: None,
1653 skip_tls_verify: true,
1654 categories: None,
1655 base_path: Some("/api".to_string()),
1656 custom_headers: vec![
1657 ("X-Avi-Tenant".to_string(), "admin".to_string()),
1658 ("X-CSRFToken".to_string(), "real-csrf-token".to_string()),
1659 ("Cookie".to_string(), "sessionid=abc123".to_string()),
1660 ],
1661 output_dir: None,
1662 all_operations: false,
1663 custom_checks_file: None,
1664 request_delay_ms: 0,
1665 custom_filter: None,
1666 export_requests: false,
1667 };
1668 let gen = SpecDrivenConformanceGenerator::new(config, vec![annotated]);
1669 let (script, _check_count) = gen.generate().unwrap();
1670
1671 assert!(
1673 script.contains("'X-Avi-Tenant': 'admin'"),
1674 "Should use custom value for X-Avi-Tenant, not test-value"
1675 );
1676 assert!(
1677 script.contains("'X-CSRFToken': 'real-csrf-token'"),
1678 "Should use custom value for X-CSRFToken, not test-value"
1679 );
1680 assert!(
1682 script.contains("'Cookie': 'sessionid=abc123'"),
1683 "Should include Cookie header from custom_headers"
1684 );
1685 assert!(
1687 !script.contains("'test-value'"),
1688 "test-value placeholders should be replaced by custom values"
1689 );
1690 }
1691
1692 #[test]
1693 fn test_effective_headers_merging() {
1694 let config = ConformanceConfig {
1695 target_url: "http://localhost".to_string(),
1696 api_key: None,
1697 basic_auth: None,
1698 skip_tls_verify: false,
1699 categories: None,
1700 base_path: None,
1701 custom_headers: vec![
1702 ("X-Auth".to_string(), "real-token".to_string()),
1703 ("Cookie".to_string(), "session=abc".to_string()),
1704 ],
1705 output_dir: None,
1706 all_operations: false,
1707 custom_checks_file: None,
1708 request_delay_ms: 0,
1709 custom_filter: None,
1710 export_requests: false,
1711 };
1712 let gen = SpecDrivenConformanceGenerator::new(config, vec![]);
1713
1714 let spec_headers = vec![
1716 ("X-Auth".to_string(), "test-value".to_string()),
1717 ("X-Other".to_string(), "keep-this".to_string()),
1718 ];
1719 let effective = gen.effective_headers(&spec_headers);
1720
1721 assert_eq!(effective[0], ("X-Auth".to_string(), "real-token".to_string()));
1723 assert_eq!(effective[1], ("X-Other".to_string(), "keep-this".to_string()));
1725 assert_eq!(effective[2], ("Cookie".to_string(), "session=abc".to_string()));
1727 }
1728}