1use crate::crud_flow::{CrudFlowConfig, CrudFlowDetector};
4use crate::data_driven::{DataDistribution, DataDrivenConfig, DataDrivenGenerator, DataMapping};
5use crate::dynamic_params::{DynamicParamProcessor, DynamicPlaceholder};
6use crate::error::{BenchError, Result};
7use crate::executor::K6Executor;
8use crate::invalid_data::{InvalidDataConfig, InvalidDataGenerator};
9use crate::k6_gen::{K6Config, K6ScriptGenerator};
10use crate::mock_integration::{
11 MockIntegrationConfig, MockIntegrationGenerator, MockServerDetector,
12};
13use crate::owasp_api::{OwaspApiConfig, OwaspApiGenerator, OwaspCategory, ReportFormat};
14use crate::parallel_executor::{AggregatedResults, ParallelExecutor};
15use crate::parallel_requests::{ParallelConfig, ParallelRequestGenerator};
16use crate::param_overrides::ParameterOverrides;
17use crate::reporter::TerminalReporter;
18use crate::request_gen::RequestGenerator;
19use crate::scenarios::LoadScenario;
20use crate::security_payloads::{
21 SecurityCategory, SecurityPayload, SecurityPayloads, SecurityTestConfig, SecurityTestGenerator,
22};
23use crate::spec_dependencies::{
24 topological_sort, DependencyDetector, ExtractedValues, SpecDependencyConfig,
25};
26use crate::spec_parser::SpecParser;
27use crate::target_parser::parse_targets_file;
28use crate::wafbench::WafBenchLoader;
29use mockforge_openapi::multi_spec::{
30 load_specs_from_directory, load_specs_from_files, merge_specs, ConflictStrategy,
31};
32use mockforge_openapi::spec::OpenApiSpec;
33use std::collections::{HashMap, HashSet};
34use std::path::{Path, PathBuf};
35use std::str::FromStr;
36
37pub fn parse_header_string(input: &str) -> Result<HashMap<String, String>> {
45 let mut headers = HashMap::new();
46
47 for pair in input.split(',') {
48 let parts: Vec<&str> = pair.splitn(2, ':').collect();
49 if parts.len() != 2 {
50 return Err(BenchError::Other(format!(
51 "Invalid header format: '{}'. Expected 'Key:Value'",
52 pair
53 )));
54 }
55 headers.insert(parts[0].trim().to_string(), parts[1].trim().to_string());
56 }
57
58 Ok(headers)
59}
60
61pub struct BenchCommand {
63 pub spec: Vec<PathBuf>,
65 pub spec_dir: Option<PathBuf>,
67 pub merge_conflicts: String,
69 pub spec_mode: String,
71 pub dependency_config: Option<PathBuf>,
73 pub target: String,
74 pub base_path: Option<String>,
77 pub duration: String,
78 pub vus: u32,
79 pub target_rps: Option<u32>,
85 pub no_keep_alive: bool,
90 pub scenario: String,
91 pub operations: Option<String>,
92 pub exclude_operations: Option<String>,
96 pub auth: Option<String>,
97 pub headers: Option<String>,
98 pub output: PathBuf,
99 pub generate_only: bool,
100 pub script_output: Option<PathBuf>,
101 pub threshold_percentile: String,
102 pub threshold_ms: u64,
103 pub max_error_rate: f64,
104 pub verbose: bool,
105 pub skip_tls_verify: bool,
106 pub chunked_request_bodies: bool,
111 pub targets_file: Option<PathBuf>,
113 pub max_concurrency: Option<u32>,
115 pub results_format: String,
117 pub params_file: Option<PathBuf>,
122
123 pub crud_flow: bool,
126 pub flow_config: Option<PathBuf>,
128 pub extract_fields: Option<String>,
130
131 pub parallel_create: Option<u32>,
134
135 pub data_file: Option<PathBuf>,
138 pub data_distribution: String,
140 pub data_mappings: Option<String>,
142 pub per_uri_control: bool,
144
145 pub error_rate: Option<f64>,
148 pub error_types: Option<String>,
150
151 pub security_test: bool,
154 pub security_payloads: Option<PathBuf>,
156 pub security_categories: Option<String>,
158 pub security_target_fields: Option<String>,
160
161 pub wafbench_dir: Option<String>,
164 pub wafbench_cycle_all: bool,
166
167 pub conformance: bool,
170 pub conformance_api_key: Option<String>,
172 pub conformance_basic_auth: Option<String>,
174 pub conformance_report: PathBuf,
176 pub conformance_categories: Option<String>,
178 pub conformance_report_format: String,
180 pub conformance_headers: Vec<String>,
183 pub conformance_all_operations: bool,
186 pub conformance_custom: Option<PathBuf>,
188 pub conformance_delay_ms: u64,
191 pub use_k6: bool,
193 pub conformance_custom_filter: Option<String>,
197 pub export_requests: bool,
200 pub validate_requests: bool,
203
204 pub owasp_api_top10: bool,
207 pub owasp_categories: Option<String>,
209 pub owasp_auth_header: String,
211 pub owasp_auth_token: Option<String>,
213 pub owasp_admin_paths: Option<PathBuf>,
215 pub owasp_id_fields: Option<String>,
217 pub owasp_report: Option<PathBuf>,
219 pub owasp_report_format: String,
221 pub owasp_iterations: u32,
223}
224
225impl BenchCommand {
226 pub async fn load_and_merge_specs(&self) -> Result<OpenApiSpec> {
228 let mut all_specs: Vec<(PathBuf, OpenApiSpec)> = Vec::new();
229
230 if !self.spec.is_empty() {
232 let specs = load_specs_from_files(self.spec.clone())
233 .await
234 .map_err(|e| BenchError::Other(format!("Failed to load spec files: {}", e)))?;
235 all_specs.extend(specs);
236 }
237
238 if let Some(spec_dir) = &self.spec_dir {
240 let dir_specs = load_specs_from_directory(spec_dir).await.map_err(|e| {
241 BenchError::Other(format!("Failed to load specs from directory: {}", e))
242 })?;
243 all_specs.extend(dir_specs);
244 }
245
246 if all_specs.is_empty() {
247 return Err(BenchError::Other(
248 "No spec files provided. Use --spec or --spec-dir.".to_string(),
249 ));
250 }
251
252 if all_specs.len() == 1 {
254 return Ok(all_specs.into_iter().next().expect("checked len() == 1 above").1);
256 }
257
258 let conflict_strategy = match self.merge_conflicts.as_str() {
260 "first" => ConflictStrategy::First,
261 "last" => ConflictStrategy::Last,
262 _ => ConflictStrategy::Error,
263 };
264
265 merge_specs(all_specs, conflict_strategy)
266 .map_err(|e| BenchError::Other(format!("Failed to merge specs: {}", e)))
267 }
268
269 fn get_spec_display_name(&self) -> String {
271 if self.spec.len() == 1 {
272 self.spec[0].to_string_lossy().to_string()
273 } else if !self.spec.is_empty() {
274 format!("{} spec files", self.spec.len())
275 } else if let Some(dir) = &self.spec_dir {
276 format!("specs from {}", dir.display())
277 } else {
278 "no specs".to_string()
279 }
280 }
281
282 pub async fn execute(&self) -> Result<()> {
284 if let Some(targets_file) = &self.targets_file {
286 if self.conformance {
287 return self.execute_multi_target_conformance(targets_file).await;
288 }
289 return self.execute_multi_target(targets_file).await;
290 }
291
292 if self.spec_mode == "sequential" && (self.spec.len() > 1 || self.spec_dir.is_some()) {
294 return self.execute_sequential_specs().await;
295 }
296
297 TerminalReporter::print_header(
300 &self.get_spec_display_name(),
301 &self.target,
302 0, &self.scenario,
304 Self::parse_duration(&self.duration)?,
305 );
306
307 if !K6Executor::is_k6_installed() {
309 TerminalReporter::print_error("k6 is not installed");
310 TerminalReporter::print_warning(
311 "Install k6 from: https://k6.io/docs/get-started/installation/",
312 );
313 return Err(BenchError::K6NotFound);
314 }
315
316 if self.conformance {
318 return self.execute_conformance_test().await;
319 }
320
321 TerminalReporter::print_progress("Loading OpenAPI specification(s)...");
323 let merged_spec = self.load_and_merge_specs().await?;
324 let parser = SpecParser::from_spec(merged_spec);
325 if self.spec.len() > 1 || self.spec_dir.is_some() {
326 TerminalReporter::print_success(&format!(
327 "Loaded and merged {} specification(s)",
328 self.spec.len() + self.spec_dir.as_ref().map(|_| 1).unwrap_or(0)
329 ));
330 } else {
331 TerminalReporter::print_success("Specification loaded");
332 }
333
334 let mock_config = self.build_mock_config().await;
336 if mock_config.is_mock_server {
337 TerminalReporter::print_progress("Mock server integration enabled");
338 }
339
340 if self.crud_flow {
342 return self.execute_crud_flow(&parser).await;
343 }
344
345 if self.owasp_api_top10 {
347 return self.execute_owasp_test(&parser).await;
348 }
349
350 TerminalReporter::print_progress("Extracting API operations...");
352 let mut operations = if let Some(filter) = &self.operations {
353 parser.filter_operations(filter)?
354 } else {
355 parser.get_operations()
356 };
357
358 if let Some(exclude) = &self.exclude_operations {
360 let before_count = operations.len();
361 operations = parser.exclude_operations(operations, exclude)?;
362 let excluded_count = before_count - operations.len();
363 if excluded_count > 0 {
364 TerminalReporter::print_progress(&format!(
365 "Excluded {} operations matching '{}'",
366 excluded_count, exclude
367 ));
368 }
369 }
370
371 if operations.is_empty() {
372 return Err(BenchError::Other("No operations found in spec".to_string()));
373 }
374
375 TerminalReporter::print_success(&format!("Found {} operations", operations.len()));
376
377 let param_overrides = if let Some(params_file) = &self.params_file {
379 TerminalReporter::print_progress("Loading parameter overrides...");
380 let overrides = ParameterOverrides::from_file(params_file)?;
381 TerminalReporter::print_success(&format!(
382 "Loaded parameter overrides ({} operation-specific, {} defaults)",
383 overrides.operations.len(),
384 if overrides.defaults.is_empty() { 0 } else { 1 }
385 ));
386 Some(overrides)
387 } else {
388 None
389 };
390
391 TerminalReporter::print_progress("Generating request templates...");
393 let templates: Vec<_> = operations
394 .iter()
395 .map(|op| {
396 let op_overrides = param_overrides.as_ref().map(|po| {
397 po.get_for_operation(op.operation_id.as_deref(), &op.method, &op.path)
398 });
399 RequestGenerator::generate_template_with_overrides(op, op_overrides.as_ref())
400 })
401 .collect::<Result<Vec<_>>>()?;
402 TerminalReporter::print_success("Request templates generated");
403
404 let custom_headers = self.parse_headers()?;
406
407 let base_path = self.resolve_base_path(&parser);
409 if let Some(ref bp) = base_path {
410 TerminalReporter::print_progress(&format!("Using base path: {}", bp));
411 }
412
413 TerminalReporter::print_progress("Generating k6 load test script...");
415 let scenario =
416 LoadScenario::from_str(&self.scenario).map_err(BenchError::InvalidScenario)?;
417
418 let security_testing_enabled = self.security_test || self.wafbench_dir.is_some();
419
420 if let Some(rps) = self.target_rps {
427 if self.vus.saturating_mul(10) < rps {
431 TerminalReporter::print_warning(&format!(
432 "--vus {} may be insufficient for --rps {}. k6 needs roughly \
433 rps × avg_request_seconds VUs to sustain the rate; bump --vus \
434 (e.g. --vus {}) if you see \"Insufficient VUs\" warnings.",
435 self.vus,
436 rps,
437 rps.div_ceil(10).max(self.vus + 1),
438 ));
439 }
440 }
441
442 let k6_config = K6Config {
443 target_url: self.target.clone(),
444 base_path,
445 scenario,
446 duration_secs: Self::parse_duration(&self.duration)?,
447 max_vus: self.vus,
448 threshold_percentile: self.threshold_percentile.clone(),
449 threshold_ms: self.threshold_ms,
450 max_error_rate: self.max_error_rate,
451 auth_header: self.auth.clone(),
452 custom_headers,
453 skip_tls_verify: self.skip_tls_verify,
454 security_testing_enabled,
455 chunked_request_bodies: self.chunked_request_bodies,
456 target_rps: self.target_rps,
457 no_keep_alive: self.no_keep_alive,
458 };
459
460 let generator = K6ScriptGenerator::new(k6_config, templates);
461 let mut script = generator.generate()?;
462 TerminalReporter::print_success("k6 script generated");
463
464 let has_advanced_features = self.data_file.is_some()
466 || self.error_rate.is_some()
467 || self.security_test
468 || self.parallel_create.is_some()
469 || self.wafbench_dir.is_some();
470
471 if has_advanced_features {
473 script = self.generate_enhanced_script(&script)?;
474 }
475
476 if mock_config.is_mock_server {
478 let setup_code = MockIntegrationGenerator::generate_setup(&mock_config);
479 let teardown_code = MockIntegrationGenerator::generate_teardown(&mock_config);
480 let helper_code = MockIntegrationGenerator::generate_vu_id_helper();
481
482 if let Some(import_end) = script.find("export const options") {
484 script.insert_str(
485 import_end,
486 &format!(
487 "\n// === Mock Server Integration ===\n{}\n{}\n{}\n",
488 helper_code, setup_code, teardown_code
489 ),
490 );
491 }
492 }
493
494 TerminalReporter::print_progress("Validating k6 script...");
496 let validation_errors = K6ScriptGenerator::validate_script(&script);
497 if !validation_errors.is_empty() {
498 TerminalReporter::print_error("Script validation failed");
499 for error in &validation_errors {
500 eprintln!(" {}", error);
501 }
502 return Err(BenchError::Other(format!(
503 "Generated k6 script has {} validation error(s). Please check the output above.",
504 validation_errors.len()
505 )));
506 }
507 TerminalReporter::print_success("Script validation passed");
508
509 let script_path = if let Some(output) = &self.script_output {
511 output.clone()
512 } else {
513 self.output.join("k6-script.js")
514 };
515
516 if let Some(parent) = script_path.parent() {
517 std::fs::create_dir_all(parent)?;
518 }
519 std::fs::write(&script_path, &script)?;
520 TerminalReporter::print_success(&format!("Script written to: {}", script_path.display()));
521
522 if self.generate_only {
524 println!("\nScript generated successfully. Run it with:");
525 println!(" k6 run {}", script_path.display());
526 return Ok(());
527 }
528
529 TerminalReporter::print_progress("Executing load test...");
531 let executor = K6Executor::new()?;
532
533 std::fs::create_dir_all(&self.output)?;
534
535 let results = executor.execute(&script_path, Some(&self.output), self.verbose).await?;
536
537 let duration_secs = Self::parse_duration(&self.duration)?;
539 TerminalReporter::print_summary_with_mode(&results, duration_secs, self.no_keep_alive);
540
541 println!("\nResults saved to: {}", self.output.display());
542
543 Ok(())
544 }
545
546 async fn execute_multi_target(&self, targets_file: &Path) -> Result<()> {
548 TerminalReporter::print_progress("Parsing targets file...");
549 let targets = parse_targets_file(targets_file)?;
550 let num_targets = targets.len();
551 TerminalReporter::print_success(&format!("Loaded {} targets", num_targets));
552
553 if targets.is_empty() {
554 return Err(BenchError::Other("No targets found in file".to_string()));
555 }
556
557 let max_concurrency = self.max_concurrency.unwrap_or(10) as usize;
559 let max_concurrency = max_concurrency.min(num_targets); TerminalReporter::print_header(
563 &self.get_spec_display_name(),
564 &format!("{} targets", num_targets),
565 0,
566 &self.scenario,
567 Self::parse_duration(&self.duration)?,
568 );
569
570 let executor = ParallelExecutor::new(
572 BenchCommand {
573 spec: self.spec.clone(),
575 spec_dir: self.spec_dir.clone(),
576 merge_conflicts: self.merge_conflicts.clone(),
577 spec_mode: self.spec_mode.clone(),
578 dependency_config: self.dependency_config.clone(),
579 target: self.target.clone(), base_path: self.base_path.clone(),
581 duration: self.duration.clone(),
582 vus: self.vus,
583 target_rps: self.target_rps,
584 no_keep_alive: self.no_keep_alive,
585 scenario: self.scenario.clone(),
586 operations: self.operations.clone(),
587 exclude_operations: self.exclude_operations.clone(),
588 auth: self.auth.clone(),
589 headers: self.headers.clone(),
590 output: self.output.clone(),
591 generate_only: self.generate_only,
592 script_output: self.script_output.clone(),
593 threshold_percentile: self.threshold_percentile.clone(),
594 threshold_ms: self.threshold_ms,
595 max_error_rate: self.max_error_rate,
596 verbose: self.verbose,
597 skip_tls_verify: self.skip_tls_verify,
598 chunked_request_bodies: self.chunked_request_bodies,
599 targets_file: None,
600 max_concurrency: None,
601 results_format: self.results_format.clone(),
602 params_file: self.params_file.clone(),
603 crud_flow: self.crud_flow,
604 flow_config: self.flow_config.clone(),
605 extract_fields: self.extract_fields.clone(),
606 parallel_create: self.parallel_create,
607 data_file: self.data_file.clone(),
608 data_distribution: self.data_distribution.clone(),
609 data_mappings: self.data_mappings.clone(),
610 per_uri_control: self.per_uri_control,
611 error_rate: self.error_rate,
612 error_types: self.error_types.clone(),
613 security_test: self.security_test,
614 security_payloads: self.security_payloads.clone(),
615 security_categories: self.security_categories.clone(),
616 security_target_fields: self.security_target_fields.clone(),
617 wafbench_dir: self.wafbench_dir.clone(),
618 wafbench_cycle_all: self.wafbench_cycle_all,
619 owasp_api_top10: self.owasp_api_top10,
620 owasp_categories: self.owasp_categories.clone(),
621 owasp_auth_header: self.owasp_auth_header.clone(),
622 owasp_auth_token: self.owasp_auth_token.clone(),
623 owasp_admin_paths: self.owasp_admin_paths.clone(),
624 owasp_id_fields: self.owasp_id_fields.clone(),
625 owasp_report: self.owasp_report.clone(),
626 owasp_report_format: self.owasp_report_format.clone(),
627 owasp_iterations: self.owasp_iterations,
628 conformance: false,
629 conformance_api_key: None,
630 conformance_basic_auth: None,
631 conformance_report: PathBuf::from("conformance-report.json"),
632 conformance_categories: None,
633 conformance_report_format: "json".to_string(),
634 conformance_headers: vec![],
635 conformance_all_operations: false,
636 conformance_custom: None,
637 conformance_delay_ms: 0,
638 use_k6: false,
639 conformance_custom_filter: None,
640 export_requests: false,
641 validate_requests: false,
642 },
643 targets,
644 max_concurrency,
645 );
646
647 let start_time = std::time::Instant::now();
649 let aggregated_results = executor.execute_all().await?;
650 let elapsed = start_time.elapsed();
651
652 self.report_multi_target_results(&aggregated_results, elapsed)?;
654
655 Ok(())
656 }
657
658 fn report_multi_target_results(
660 &self,
661 results: &AggregatedResults,
662 elapsed: std::time::Duration,
663 ) -> Result<()> {
664 TerminalReporter::print_multi_target_summary(results);
666
667 let total_secs = elapsed.as_secs();
669 let hours = total_secs / 3600;
670 let minutes = (total_secs % 3600) / 60;
671 let seconds = total_secs % 60;
672 if hours > 0 {
673 println!("\n Total Elapsed Time: {}h {}m {}s", hours, minutes, seconds);
674 } else if minutes > 0 {
675 println!("\n Total Elapsed Time: {}m {}s", minutes, seconds);
676 } else {
677 println!("\n Total Elapsed Time: {}s", seconds);
678 }
679
680 if self.results_format == "aggregated" || self.results_format == "both" {
682 let summary_path = self.output.join("aggregated_summary.json");
683 let summary_json = serde_json::json!({
684 "total_elapsed_seconds": elapsed.as_secs(),
685 "total_targets": results.total_targets,
686 "successful_targets": results.successful_targets,
687 "failed_targets": results.failed_targets,
688 "aggregated_metrics": {
689 "total_requests": results.aggregated_metrics.total_requests,
690 "total_failed_requests": results.aggregated_metrics.total_failed_requests,
691 "avg_duration_ms": results.aggregated_metrics.avg_duration_ms,
692 "p95_duration_ms": results.aggregated_metrics.p95_duration_ms,
693 "p99_duration_ms": results.aggregated_metrics.p99_duration_ms,
694 "error_rate": results.aggregated_metrics.error_rate,
695 "total_rps": results.aggregated_metrics.total_rps,
696 "avg_rps": results.aggregated_metrics.avg_rps,
697 "total_vus_max": results.aggregated_metrics.total_vus_max,
698 },
699 "target_results": results.target_results.iter().map(|r| {
700 serde_json::json!({
701 "target_url": r.target_url,
702 "target_index": r.target_index,
703 "success": r.success,
704 "error": r.error,
705 "total_requests": r.results.total_requests,
706 "failed_requests": r.results.failed_requests,
707 "avg_duration_ms": r.results.avg_duration_ms,
708 "min_duration_ms": r.results.min_duration_ms,
709 "med_duration_ms": r.results.med_duration_ms,
710 "p90_duration_ms": r.results.p90_duration_ms,
711 "p95_duration_ms": r.results.p95_duration_ms,
712 "p99_duration_ms": r.results.p99_duration_ms,
713 "max_duration_ms": r.results.max_duration_ms,
714 "rps": r.results.rps,
715 "vus_max": r.results.vus_max,
716 "output_dir": r.output_dir.to_string_lossy(),
717 })
718 }).collect::<Vec<_>>(),
719 });
720
721 std::fs::write(&summary_path, serde_json::to_string_pretty(&summary_json)?)?;
722 TerminalReporter::print_success(&format!(
723 "Aggregated summary saved to: {}",
724 summary_path.display()
725 ));
726 }
727
728 let csv_path = self.output.join("all_targets.csv");
730 let mut csv = String::from(
731 "target_url,success,requests,failed,rps,vus,min_ms,avg_ms,med_ms,p90_ms,p95_ms,p99_ms,max_ms,error\n",
732 );
733 for r in &results.target_results {
734 csv.push_str(&format!(
735 "{},{},{},{},{:.1},{},{:.1},{:.1},{:.1},{:.1},{:.1},{:.1},{:.1},{}\n",
736 r.target_url,
737 r.success,
738 r.results.total_requests,
739 r.results.failed_requests,
740 r.results.rps,
741 r.results.vus_max,
742 r.results.min_duration_ms,
743 r.results.avg_duration_ms,
744 r.results.med_duration_ms,
745 r.results.p90_duration_ms,
746 r.results.p95_duration_ms,
747 r.results.p99_duration_ms,
748 r.results.max_duration_ms,
749 r.error.as_deref().unwrap_or(""),
750 ));
751 }
752 let _ = std::fs::write(&csv_path, &csv);
753
754 println!("\nResults saved to: {}", self.output.display());
755 println!(" - Per-target results: {}", self.output.join("target_*").display());
756 println!(" - All targets CSV: {}", csv_path.display());
757 if self.results_format == "aggregated" || self.results_format == "both" {
758 println!(
759 " - Aggregated summary: {}",
760 self.output.join("aggregated_summary.json").display()
761 );
762 }
763
764 Ok(())
765 }
766
767 pub fn parse_duration(duration: &str) -> Result<u64> {
769 let duration = duration.trim();
770
771 if let Some(secs) = duration.strip_suffix('s') {
772 secs.parse::<u64>()
773 .map_err(|_| BenchError::Other(format!("Invalid duration: {}", duration)))
774 } else if let Some(mins) = duration.strip_suffix('m') {
775 mins.parse::<u64>()
776 .map(|m| m * 60)
777 .map_err(|_| BenchError::Other(format!("Invalid duration: {}", duration)))
778 } else if let Some(hours) = duration.strip_suffix('h') {
779 hours
780 .parse::<u64>()
781 .map(|h| h * 3600)
782 .map_err(|_| BenchError::Other(format!("Invalid duration: {}", duration)))
783 } else {
784 duration
786 .parse::<u64>()
787 .map_err(|_| BenchError::Other(format!("Invalid duration: {}", duration)))
788 }
789 }
790
791 pub fn parse_headers(&self) -> Result<HashMap<String, String>> {
793 match &self.headers {
794 Some(s) => parse_header_string(s),
795 None => Ok(HashMap::new()),
796 }
797 }
798
799 fn parse_extracted_values(output_dir: &Path) -> Result<ExtractedValues> {
800 let extracted_path = output_dir.join("extracted_values.json");
801 if !extracted_path.exists() {
802 return Ok(ExtractedValues::new());
803 }
804
805 let content = std::fs::read_to_string(&extracted_path)
806 .map_err(|e| BenchError::ResultsParseError(e.to_string()))?;
807 let parsed: serde_json::Value = serde_json::from_str(&content)
808 .map_err(|e| BenchError::ResultsParseError(e.to_string()))?;
809
810 let mut extracted = ExtractedValues::new();
811 if let Some(values) = parsed.as_object() {
812 for (key, value) in values {
813 extracted.set(key.clone(), value.clone());
814 }
815 }
816
817 Ok(extracted)
818 }
819
820 fn resolve_base_path(&self, parser: &SpecParser) -> Option<String> {
829 if let Some(cli_base_path) = &self.base_path {
831 if cli_base_path.is_empty() {
832 return None;
834 }
835 return Some(cli_base_path.clone());
836 }
837
838 parser.get_base_path()
840 }
841
842 async fn build_mock_config(&self) -> MockIntegrationConfig {
844 if MockServerDetector::looks_like_mock_server(&self.target) {
846 if let Ok(info) = MockServerDetector::detect(&self.target).await {
848 if info.is_mockforge {
849 TerminalReporter::print_success(&format!(
850 "Detected MockForge server (version: {})",
851 info.version.as_deref().unwrap_or("unknown")
852 ));
853 return MockIntegrationConfig::mock_server();
854 }
855 }
856 }
857 MockIntegrationConfig::real_api()
858 }
859
860 fn build_crud_flow_config(&self) -> Option<CrudFlowConfig> {
862 if !self.crud_flow {
863 return None;
864 }
865
866 if let Some(config_path) = &self.flow_config {
868 match CrudFlowConfig::from_file(config_path) {
869 Ok(config) => return Some(config),
870 Err(e) => {
871 TerminalReporter::print_warning(&format!(
872 "Failed to load flow config: {}. Using auto-detection.",
873 e
874 ));
875 }
876 }
877 }
878
879 let extract_fields = self
881 .extract_fields
882 .as_ref()
883 .map(|f| f.split(',').map(|s| s.trim().to_string()).collect())
884 .unwrap_or_else(|| vec!["id".to_string(), "uuid".to_string()]);
885
886 Some(CrudFlowConfig {
887 flows: Vec::new(), default_extract_fields: extract_fields,
889 })
890 }
891
892 fn build_data_driven_config(&self) -> Option<DataDrivenConfig> {
894 let data_file = self.data_file.as_ref()?;
895
896 let distribution = DataDistribution::from_str(&self.data_distribution)
897 .unwrap_or(DataDistribution::UniquePerVu);
898
899 let mappings = self
900 .data_mappings
901 .as_ref()
902 .map(|m| DataMapping::parse_mappings(m).unwrap_or_default())
903 .unwrap_or_default();
904
905 Some(DataDrivenConfig {
906 file_path: data_file.to_string_lossy().to_string(),
907 distribution,
908 mappings,
909 csv_has_header: true,
910 per_uri_control: self.per_uri_control,
911 per_uri_columns: crate::data_driven::PerUriColumns::default(),
912 })
913 }
914
915 fn build_invalid_data_config(&self) -> Option<InvalidDataConfig> {
917 let error_rate = self.error_rate?;
918
919 let error_types = self
920 .error_types
921 .as_ref()
922 .map(|types| InvalidDataConfig::parse_error_types(types).unwrap_or_default())
923 .unwrap_or_default();
924
925 Some(InvalidDataConfig {
926 error_rate,
927 error_types,
928 target_fields: Vec::new(),
929 })
930 }
931
932 fn build_security_config(&self) -> Option<SecurityTestConfig> {
934 if !self.security_test {
935 return None;
936 }
937
938 let categories = self
939 .security_categories
940 .as_ref()
941 .map(|cats| SecurityTestConfig::parse_categories(cats).unwrap_or_default())
942 .unwrap_or_else(|| {
943 let mut default = HashSet::new();
944 default.insert(SecurityCategory::SqlInjection);
945 default.insert(SecurityCategory::Xss);
946 default
947 });
948
949 let target_fields = self
950 .security_target_fields
951 .as_ref()
952 .map(|fields| fields.split(',').map(|f| f.trim().to_string()).collect())
953 .unwrap_or_default();
954
955 let custom_payloads_file =
956 self.security_payloads.as_ref().map(|p| p.to_string_lossy().to_string());
957
958 Some(SecurityTestConfig {
959 enabled: true,
960 categories,
961 target_fields,
962 custom_payloads_file,
963 include_high_risk: false,
964 })
965 }
966
967 fn build_parallel_config(&self) -> Option<ParallelConfig> {
969 let count = self.parallel_create?;
970
971 Some(ParallelConfig::new(count))
972 }
973
974 fn load_wafbench_payloads(&self) -> Vec<SecurityPayload> {
976 let Some(ref wafbench_dir) = self.wafbench_dir else {
977 return Vec::new();
978 };
979
980 let mut loader = WafBenchLoader::new();
981
982 if let Err(e) = loader.load_from_pattern(wafbench_dir) {
983 TerminalReporter::print_warning(&format!("Failed to load WAFBench tests: {}", e));
984 return Vec::new();
985 }
986
987 let stats = loader.stats();
988
989 if stats.files_processed == 0 {
990 TerminalReporter::print_warning(&format!(
991 "No WAFBench YAML files found matching '{}'",
992 wafbench_dir
993 ));
994 if !stats.parse_errors.is_empty() {
996 TerminalReporter::print_warning("Some files were found but failed to parse:");
997 for error in &stats.parse_errors {
998 TerminalReporter::print_warning(&format!(" - {}", error));
999 }
1000 }
1001 return Vec::new();
1002 }
1003
1004 TerminalReporter::print_progress(&format!(
1005 "Loaded {} WAFBench files, {} test cases, {} payloads",
1006 stats.files_processed, stats.test_cases_loaded, stats.payloads_extracted
1007 ));
1008
1009 for (category, count) in &stats.by_category {
1011 TerminalReporter::print_progress(&format!(" - {}: {} tests", category, count));
1012 }
1013
1014 for error in &stats.parse_errors {
1016 TerminalReporter::print_warning(&format!(" Parse error: {}", error));
1017 }
1018
1019 loader.to_security_payloads()
1020 }
1021
1022 pub(crate) fn generate_enhanced_script(&self, base_script: &str) -> Result<String> {
1024 let mut enhanced_script = base_script.to_string();
1025 let mut additional_code = String::new();
1026
1027 if let Some(config) = self.build_data_driven_config() {
1029 TerminalReporter::print_progress("Adding data-driven testing support...");
1030 additional_code.push_str(&DataDrivenGenerator::generate_setup(&config));
1031 additional_code.push('\n');
1032 TerminalReporter::print_success("Data-driven testing enabled");
1033 }
1034
1035 if let Some(config) = self.build_invalid_data_config() {
1037 TerminalReporter::print_progress("Adding invalid data testing support...");
1038 additional_code.push_str(&InvalidDataGenerator::generate_invalidation_logic());
1039 additional_code.push('\n');
1040 additional_code
1041 .push_str(&InvalidDataGenerator::generate_should_invalidate(config.error_rate));
1042 additional_code.push('\n');
1043 additional_code
1044 .push_str(&InvalidDataGenerator::generate_type_selection(&config.error_types));
1045 additional_code.push('\n');
1046 TerminalReporter::print_success(&format!(
1047 "Invalid data testing enabled ({}% error rate)",
1048 (self.error_rate.unwrap_or(0.0) * 100.0) as u32
1049 ));
1050 }
1051
1052 let security_config = self.build_security_config();
1054 let wafbench_payloads = self.load_wafbench_payloads();
1055 let security_requested = security_config.is_some() || self.wafbench_dir.is_some();
1056
1057 if security_config.is_some() || !wafbench_payloads.is_empty() {
1058 TerminalReporter::print_progress("Adding security testing support...");
1059
1060 let mut payload_list: Vec<SecurityPayload> = Vec::new();
1062
1063 if let Some(ref config) = security_config {
1064 payload_list.extend(SecurityPayloads::get_payloads(config));
1065 }
1066
1067 if !wafbench_payloads.is_empty() {
1069 TerminalReporter::print_progress(&format!(
1070 "Loading {} WAFBench attack patterns...",
1071 wafbench_payloads.len()
1072 ));
1073 payload_list.extend(wafbench_payloads);
1074 }
1075
1076 let target_fields =
1077 security_config.as_ref().map(|c| c.target_fields.clone()).unwrap_or_default();
1078
1079 additional_code.push_str(&SecurityTestGenerator::generate_payload_selection(
1080 &payload_list,
1081 self.wafbench_cycle_all,
1082 ));
1083 additional_code.push('\n');
1084 additional_code
1085 .push_str(&SecurityTestGenerator::generate_apply_payload(&target_fields));
1086 additional_code.push('\n');
1087 additional_code.push_str(&SecurityTestGenerator::generate_security_checks());
1088 additional_code.push('\n');
1089
1090 let mode = if self.wafbench_cycle_all {
1091 "cycle-all"
1092 } else {
1093 "random"
1094 };
1095 TerminalReporter::print_success(&format!(
1096 "Security testing enabled ({} payloads, {} mode)",
1097 payload_list.len(),
1098 mode
1099 ));
1100 } else if security_requested {
1101 TerminalReporter::print_warning(
1105 "Security testing was requested but no payloads were loaded. \
1106 Ensure --wafbench-dir points to valid CRS YAML files or add --security-test.",
1107 );
1108 additional_code
1109 .push_str(&SecurityTestGenerator::generate_payload_selection(&[], false));
1110 additional_code.push('\n');
1111 additional_code.push_str(&SecurityTestGenerator::generate_apply_payload(&[]));
1112 additional_code.push('\n');
1113 }
1114
1115 if let Some(config) = self.build_parallel_config() {
1117 TerminalReporter::print_progress("Adding parallel execution support...");
1118 additional_code.push_str(&ParallelRequestGenerator::generate_batch_helper(&config));
1119 additional_code.push('\n');
1120 TerminalReporter::print_success(&format!(
1121 "Parallel execution enabled (count: {})",
1122 config.count
1123 ));
1124 }
1125
1126 if !additional_code.is_empty() {
1128 if let Some(import_end) = enhanced_script.find("export const options") {
1130 enhanced_script.insert_str(
1131 import_end,
1132 &format!("\n// === Advanced Testing Features ===\n{}\n", additional_code),
1133 );
1134 }
1135 }
1136
1137 Ok(enhanced_script)
1138 }
1139
1140 async fn execute_sequential_specs(&self) -> Result<()> {
1142 TerminalReporter::print_progress("Sequential spec mode: Loading specs individually...");
1143
1144 let mut all_specs: Vec<(PathBuf, OpenApiSpec)> = Vec::new();
1146
1147 if !self.spec.is_empty() {
1148 let specs = load_specs_from_files(self.spec.clone())
1149 .await
1150 .map_err(|e| BenchError::Other(format!("Failed to load spec files: {}", e)))?;
1151 all_specs.extend(specs);
1152 }
1153
1154 if let Some(spec_dir) = &self.spec_dir {
1155 let dir_specs = load_specs_from_directory(spec_dir).await.map_err(|e| {
1156 BenchError::Other(format!("Failed to load specs from directory: {}", e))
1157 })?;
1158 all_specs.extend(dir_specs);
1159 }
1160
1161 if all_specs.is_empty() {
1162 return Err(BenchError::Other(
1163 "No spec files found for sequential execution".to_string(),
1164 ));
1165 }
1166
1167 TerminalReporter::print_success(&format!("Loaded {} spec(s)", all_specs.len()));
1168
1169 let execution_order = if let Some(config_path) = &self.dependency_config {
1171 TerminalReporter::print_progress("Loading dependency configuration...");
1172 let config = SpecDependencyConfig::from_file(config_path)?;
1173
1174 if !config.disable_auto_detect && config.execution_order.is_empty() {
1175 self.detect_and_sort_specs(&all_specs)?
1177 } else {
1178 config.execution_order.iter().flat_map(|g| g.specs.clone()).collect()
1180 }
1181 } else {
1182 self.detect_and_sort_specs(&all_specs)?
1184 };
1185
1186 TerminalReporter::print_success(&format!(
1187 "Execution order: {}",
1188 execution_order
1189 .iter()
1190 .map(|p| p.file_name().unwrap_or_default().to_string_lossy().to_string())
1191 .collect::<Vec<_>>()
1192 .join(" → ")
1193 ));
1194
1195 let mut extracted_values = ExtractedValues::new();
1197 let total_specs = execution_order.len();
1198
1199 for (index, spec_path) in execution_order.iter().enumerate() {
1200 let spec_name = spec_path.file_name().unwrap_or_default().to_string_lossy().to_string();
1201
1202 TerminalReporter::print_progress(&format!(
1203 "[{}/{}] Executing spec: {}",
1204 index + 1,
1205 total_specs,
1206 spec_name
1207 ));
1208
1209 let spec = all_specs
1211 .iter()
1212 .find(|(p, _)| {
1213 p == spec_path
1214 || p.file_name() == spec_path.file_name()
1215 || p.file_name() == Some(spec_path.as_os_str())
1216 })
1217 .map(|(_, s)| s.clone())
1218 .ok_or_else(|| {
1219 BenchError::Other(format!("Spec not found: {}", spec_path.display()))
1220 })?;
1221
1222 let new_values = self.execute_single_spec(&spec, &spec_name, &extracted_values).await?;
1224
1225 extracted_values.merge(&new_values);
1227
1228 TerminalReporter::print_success(&format!(
1229 "[{}/{}] Completed: {} (extracted {} values)",
1230 index + 1,
1231 total_specs,
1232 spec_name,
1233 new_values.values.len()
1234 ));
1235 }
1236
1237 TerminalReporter::print_success(&format!(
1238 "Sequential execution complete: {} specs executed",
1239 total_specs
1240 ));
1241
1242 Ok(())
1243 }
1244
1245 fn detect_and_sort_specs(&self, specs: &[(PathBuf, OpenApiSpec)]) -> Result<Vec<PathBuf>> {
1247 TerminalReporter::print_progress("Auto-detecting spec dependencies...");
1248
1249 let mut detector = DependencyDetector::new();
1250 let dependencies = detector.detect_dependencies(specs);
1251
1252 if dependencies.is_empty() {
1253 TerminalReporter::print_progress("No dependencies detected, using file order");
1254 return Ok(specs.iter().map(|(p, _)| p.clone()).collect());
1255 }
1256
1257 TerminalReporter::print_progress(&format!(
1258 "Detected {} cross-spec dependencies",
1259 dependencies.len()
1260 ));
1261
1262 for dep in &dependencies {
1263 TerminalReporter::print_progress(&format!(
1264 " {} → {} (via field '{}')",
1265 dep.dependency_spec.file_name().unwrap_or_default().to_string_lossy(),
1266 dep.dependent_spec.file_name().unwrap_or_default().to_string_lossy(),
1267 dep.field_name
1268 ));
1269 }
1270
1271 topological_sort(specs, &dependencies)
1272 }
1273
1274 async fn execute_single_spec(
1276 &self,
1277 spec: &OpenApiSpec,
1278 spec_name: &str,
1279 _external_values: &ExtractedValues,
1280 ) -> Result<ExtractedValues> {
1281 let parser = SpecParser::from_spec(spec.clone());
1282
1283 if self.crud_flow {
1285 self.execute_crud_flow_with_extraction(&parser, spec_name).await
1287 } else {
1288 self.execute_standard_spec(&parser, spec_name).await?;
1290 Ok(ExtractedValues::new())
1291 }
1292 }
1293
1294 async fn execute_crud_flow_with_extraction(
1296 &self,
1297 parser: &SpecParser,
1298 spec_name: &str,
1299 ) -> Result<ExtractedValues> {
1300 let operations = parser.get_operations();
1301 let flows = CrudFlowDetector::detect_flows(&operations);
1302
1303 if flows.is_empty() {
1304 TerminalReporter::print_warning(&format!("No CRUD flows detected in {}", spec_name));
1305 return Ok(ExtractedValues::new());
1306 }
1307
1308 TerminalReporter::print_progress(&format!(
1309 " {} CRUD flow(s) in {}",
1310 flows.len(),
1311 spec_name
1312 ));
1313
1314 let mut handlebars = handlebars::Handlebars::new();
1316 handlebars.register_helper(
1318 "json",
1319 Box::new(
1320 |h: &handlebars::Helper,
1321 _: &handlebars::Handlebars,
1322 _: &handlebars::Context,
1323 _: &mut handlebars::RenderContext,
1324 out: &mut dyn handlebars::Output|
1325 -> handlebars::HelperResult {
1326 let param = h.param(0).map(|v| v.value()).unwrap_or(&serde_json::Value::Null);
1327 out.write(&serde_json::to_string(param).unwrap_or_else(|_| "[]".to_string()))?;
1328 Ok(())
1329 },
1330 ),
1331 );
1332 let template = include_str!("templates/k6_crud_flow.hbs");
1333 let output_dir = self.output.join(format!("{}_results", spec_name.replace('.', "_")));
1334
1335 let custom_headers = self.parse_headers()?;
1336 let config = self.build_crud_flow_config().unwrap_or_default();
1337
1338 let param_overrides = if let Some(params_file) = &self.params_file {
1340 let overrides = ParameterOverrides::from_file(params_file)?;
1341 Some(overrides)
1342 } else {
1343 None
1344 };
1345
1346 let duration_secs = Self::parse_duration(&self.duration)?;
1348 let scenario =
1349 LoadScenario::from_str(&self.scenario).map_err(BenchError::InvalidScenario)?;
1350 let stages = scenario.generate_stages(duration_secs, self.vus);
1351
1352 let api_base_path = self.resolve_base_path(parser);
1354
1355 let mut all_headers = custom_headers.clone();
1357 if let Some(auth) = &self.auth {
1358 all_headers.insert("Authorization".to_string(), auth.clone());
1359 }
1360 let headers_json = serde_json::to_string(&all_headers).unwrap_or_else(|_| "{}".to_string());
1361
1362 let mut all_placeholders: HashSet<DynamicPlaceholder> = HashSet::new();
1364
1365 let flows_data: Vec<serde_json::Value> = flows.iter().map(|f| {
1366 let sanitized_name = K6ScriptGenerator::sanitize_k6_metric_name(&f.name);
1370 serde_json::json!({
1371 "name": sanitized_name.clone(),
1372 "display_name": f.name,
1373 "base_path": f.base_path,
1374 "steps": f.steps.iter().enumerate().map(|(idx, s)| {
1375 let parts: Vec<&str> = s.operation.splitn(2, ' ').collect();
1377 let method_raw = if !parts.is_empty() {
1378 parts[0].to_uppercase()
1379 } else {
1380 "GET".to_string()
1381 };
1382 let method = if !parts.is_empty() {
1383 let m = parts[0].to_lowercase();
1384 if m == "delete" { "del".to_string() } else { m }
1386 } else {
1387 "get".to_string()
1388 };
1389 let raw_path = if parts.len() >= 2 { parts[1] } else { "/" };
1390 let path = if let Some(ref bp) = api_base_path {
1392 format!("{}{}", bp, raw_path)
1393 } else {
1394 raw_path.to_string()
1395 };
1396 let is_get_or_head = method == "get" || method == "head";
1397 let has_body = matches!(method.as_str(), "post" | "put" | "patch");
1399
1400 let body_value = if has_body {
1402 param_overrides.as_ref()
1403 .map(|po| po.get_for_operation(None, &method_raw, raw_path))
1404 .and_then(|oo| oo.body)
1405 .unwrap_or_else(|| serde_json::json!({}))
1406 } else {
1407 serde_json::json!({})
1408 };
1409
1410 let processed_body = DynamicParamProcessor::process_json_body(&body_value);
1412
1413 let body_has_extracted_placeholders = processed_body.value.contains("${extracted.");
1415 let body_is_dynamic = processed_body.is_dynamic || body_has_extracted_placeholders;
1416
1417 serde_json::json!({
1418 "operation": s.operation,
1419 "method": method,
1420 "path": path,
1421 "extract": s.extract,
1422 "use_values": s.use_values,
1423 "use_body": s.use_body,
1424 "merge_body": if s.merge_body.is_empty() { None } else { Some(&s.merge_body) },
1425 "inject_attacks": s.inject_attacks,
1426 "attack_types": s.attack_types,
1427 "description": s.description,
1428 "display_name": s.description.clone().unwrap_or_else(|| format!("Step {}", idx)),
1429 "is_get_or_head": is_get_or_head,
1430 "has_body": has_body,
1431 "body": processed_body.value,
1432 "body_is_dynamic": body_is_dynamic,
1433 "_placeholders": processed_body.placeholders.iter().map(|p| format!("{:?}", p)).collect::<Vec<_>>(),
1434 })
1435 }).collect::<Vec<_>>(),
1436 })
1437 }).collect();
1438
1439 for flow_data in &flows_data {
1441 if let Some(steps) = flow_data.get("steps").and_then(|s| s.as_array()) {
1442 for step in steps {
1443 if let Some(placeholders_arr) =
1444 step.get("_placeholders").and_then(|p| p.as_array())
1445 {
1446 for p_str in placeholders_arr {
1447 if let Some(p_name) = p_str.as_str() {
1448 match p_name {
1449 "VU" => {
1450 all_placeholders.insert(DynamicPlaceholder::VU);
1451 }
1452 "Iteration" => {
1453 all_placeholders.insert(DynamicPlaceholder::Iteration);
1454 }
1455 "Timestamp" => {
1456 all_placeholders.insert(DynamicPlaceholder::Timestamp);
1457 }
1458 "UUID" => {
1459 all_placeholders.insert(DynamicPlaceholder::UUID);
1460 }
1461 "Random" => {
1462 all_placeholders.insert(DynamicPlaceholder::Random);
1463 }
1464 "Counter" => {
1465 all_placeholders.insert(DynamicPlaceholder::Counter);
1466 }
1467 "Date" => {
1468 all_placeholders.insert(DynamicPlaceholder::Date);
1469 }
1470 "VuIter" => {
1471 all_placeholders.insert(DynamicPlaceholder::VuIter);
1472 }
1473 _ => {}
1474 }
1475 }
1476 }
1477 }
1478 }
1479 }
1480 }
1481
1482 let required_imports = DynamicParamProcessor::get_required_imports(&all_placeholders);
1484 let required_globals = DynamicParamProcessor::get_required_globals(&all_placeholders);
1485
1486 let security_testing_enabled = self.wafbench_dir.is_some() || self.security_test;
1488
1489 let data = serde_json::json!({
1490 "base_url": self.target,
1491 "flows": flows_data,
1492 "extract_fields": config.default_extract_fields,
1493 "duration_secs": duration_secs,
1494 "max_vus": self.vus,
1495 "auth_header": self.auth,
1496 "custom_headers": custom_headers,
1497 "skip_tls_verify": self.skip_tls_verify,
1498 "stages": stages.iter().map(|s| serde_json::json!({
1500 "duration": s.duration,
1501 "target": s.target,
1502 })).collect::<Vec<_>>(),
1503 "threshold_percentile": self.threshold_percentile,
1504 "threshold_ms": self.threshold_ms,
1505 "max_error_rate": self.max_error_rate,
1506 "headers": headers_json,
1507 "dynamic_imports": required_imports,
1508 "dynamic_globals": required_globals,
1509 "extracted_values_output_path": output_dir.join("extracted_values.json").to_string_lossy(),
1510 "security_testing_enabled": security_testing_enabled,
1512 "has_custom_headers": !custom_headers.is_empty(),
1513 });
1514
1515 let mut script = handlebars
1516 .render_template(template, &data)
1517 .map_err(|e| BenchError::ScriptGenerationFailed(e.to_string()))?;
1518
1519 if security_testing_enabled {
1521 script = self.generate_enhanced_script(&script)?;
1522 }
1523
1524 let script_path =
1526 self.output.join(format!("k6-{}-crud-flow.js", spec_name.replace('.', "_")));
1527
1528 std::fs::create_dir_all(self.output.clone())?;
1529 std::fs::write(&script_path, &script)?;
1530
1531 if !self.generate_only {
1532 let executor = K6Executor::new()?;
1533 std::fs::create_dir_all(&output_dir)?;
1534
1535 executor.execute(&script_path, Some(&output_dir), self.verbose).await?;
1536
1537 let extracted = Self::parse_extracted_values(&output_dir)?;
1538 TerminalReporter::print_progress(&format!(
1539 " Extracted {} value(s) from {}",
1540 extracted.values.len(),
1541 spec_name
1542 ));
1543 return Ok(extracted);
1544 }
1545
1546 Ok(ExtractedValues::new())
1547 }
1548
1549 async fn execute_standard_spec(&self, parser: &SpecParser, spec_name: &str) -> Result<()> {
1551 let mut operations = if let Some(filter) = &self.operations {
1552 parser.filter_operations(filter)?
1553 } else {
1554 parser.get_operations()
1555 };
1556
1557 if let Some(exclude) = &self.exclude_operations {
1558 operations = parser.exclude_operations(operations, exclude)?;
1559 }
1560
1561 if operations.is_empty() {
1562 TerminalReporter::print_warning(&format!("No operations found in {}", spec_name));
1563 return Ok(());
1564 }
1565
1566 TerminalReporter::print_progress(&format!(
1567 " {} operations in {}",
1568 operations.len(),
1569 spec_name
1570 ));
1571
1572 let templates: Vec<_> = operations
1574 .iter()
1575 .map(RequestGenerator::generate_template)
1576 .collect::<Result<Vec<_>>>()?;
1577
1578 let custom_headers = self.parse_headers()?;
1580
1581 let base_path = self.resolve_base_path(parser);
1583
1584 let scenario =
1586 LoadScenario::from_str(&self.scenario).map_err(BenchError::InvalidScenario)?;
1587
1588 let security_testing_enabled = self.security_test || self.wafbench_dir.is_some();
1589
1590 let k6_config = K6Config {
1591 target_url: self.target.clone(),
1592 base_path,
1593 scenario,
1594 duration_secs: Self::parse_duration(&self.duration)?,
1595 max_vus: self.vus,
1596 threshold_percentile: self.threshold_percentile.clone(),
1597 threshold_ms: self.threshold_ms,
1598 max_error_rate: self.max_error_rate,
1599 auth_header: self.auth.clone(),
1600 custom_headers,
1601 skip_tls_verify: self.skip_tls_verify,
1602 security_testing_enabled,
1603 chunked_request_bodies: self.chunked_request_bodies,
1604 target_rps: self.target_rps,
1605 no_keep_alive: self.no_keep_alive,
1606 };
1607
1608 let generator = K6ScriptGenerator::new(k6_config, templates);
1609 let mut script = generator.generate()?;
1610
1611 let has_advanced_features = self.data_file.is_some()
1613 || self.error_rate.is_some()
1614 || self.security_test
1615 || self.parallel_create.is_some()
1616 || self.wafbench_dir.is_some();
1617
1618 if has_advanced_features {
1619 script = self.generate_enhanced_script(&script)?;
1620 }
1621
1622 let script_path = self.output.join(format!("k6-{}.js", spec_name.replace('.', "_")));
1624
1625 std::fs::create_dir_all(self.output.clone())?;
1626 std::fs::write(&script_path, &script)?;
1627
1628 if !self.generate_only {
1629 let executor = K6Executor::new()?;
1630 let output_dir = self.output.join(format!("{}_results", spec_name.replace('.', "_")));
1631 std::fs::create_dir_all(&output_dir)?;
1632
1633 executor.execute(&script_path, Some(&output_dir), self.verbose).await?;
1634 }
1635
1636 Ok(())
1637 }
1638
1639 async fn execute_crud_flow(&self, parser: &SpecParser) -> Result<()> {
1641 let config = self.build_crud_flow_config().unwrap_or_default();
1643
1644 let flows = if !config.flows.is_empty() {
1646 TerminalReporter::print_progress("Using custom flow configuration...");
1647 config.flows.clone()
1648 } else {
1649 TerminalReporter::print_progress("Detecting CRUD operations...");
1650 let operations = parser.get_operations();
1651 CrudFlowDetector::detect_flows(&operations)
1652 };
1653
1654 if flows.is_empty() {
1655 return Err(BenchError::Other(
1656 "No CRUD flows detected in spec. Ensure spec has POST/GET/PUT/DELETE operations on related paths.".to_string(),
1657 ));
1658 }
1659
1660 if config.flows.is_empty() {
1661 TerminalReporter::print_success(&format!("Detected {} CRUD flow(s)", flows.len()));
1662 } else {
1663 TerminalReporter::print_success(&format!("Loaded {} custom flow(s)", flows.len()));
1664 }
1665
1666 for flow in &flows {
1667 TerminalReporter::print_progress(&format!(
1668 " - {}: {} steps",
1669 flow.name,
1670 flow.steps.len()
1671 ));
1672 }
1673
1674 let mut handlebars = handlebars::Handlebars::new();
1676 handlebars.register_helper(
1678 "json",
1679 Box::new(
1680 |h: &handlebars::Helper,
1681 _: &handlebars::Handlebars,
1682 _: &handlebars::Context,
1683 _: &mut handlebars::RenderContext,
1684 out: &mut dyn handlebars::Output|
1685 -> handlebars::HelperResult {
1686 let param = h.param(0).map(|v| v.value()).unwrap_or(&serde_json::Value::Null);
1687 out.write(&serde_json::to_string(param).unwrap_or_else(|_| "[]".to_string()))?;
1688 Ok(())
1689 },
1690 ),
1691 );
1692 let template = include_str!("templates/k6_crud_flow.hbs");
1693
1694 let custom_headers = self.parse_headers()?;
1695
1696 let param_overrides = if let Some(params_file) = &self.params_file {
1698 TerminalReporter::print_progress("Loading parameter overrides...");
1699 let overrides = ParameterOverrides::from_file(params_file)?;
1700 TerminalReporter::print_success(&format!(
1701 "Loaded parameter overrides ({} operation-specific, {} defaults)",
1702 overrides.operations.len(),
1703 if overrides.defaults.is_empty() { 0 } else { 1 }
1704 ));
1705 Some(overrides)
1706 } else {
1707 None
1708 };
1709
1710 let duration_secs = Self::parse_duration(&self.duration)?;
1712 let scenario =
1713 LoadScenario::from_str(&self.scenario).map_err(BenchError::InvalidScenario)?;
1714 let stages = scenario.generate_stages(duration_secs, self.vus);
1715
1716 let api_base_path = self.resolve_base_path(parser);
1718 if let Some(ref bp) = api_base_path {
1719 TerminalReporter::print_progress(&format!("Using base path: {}", bp));
1720 }
1721
1722 let mut all_headers = custom_headers.clone();
1724 if let Some(auth) = &self.auth {
1725 all_headers.insert("Authorization".to_string(), auth.clone());
1726 }
1727 let headers_json = serde_json::to_string(&all_headers).unwrap_or_else(|_| "{}".to_string());
1728
1729 let mut all_placeholders: HashSet<DynamicPlaceholder> = HashSet::new();
1731
1732 let flows_data: Vec<serde_json::Value> = flows.iter().map(|f| {
1733 let sanitized_name = K6ScriptGenerator::sanitize_k6_metric_name(&f.name);
1738 serde_json::json!({
1739 "name": sanitized_name.clone(), "display_name": f.name, "base_path": f.base_path,
1742 "steps": f.steps.iter().enumerate().map(|(idx, s)| {
1743 let parts: Vec<&str> = s.operation.splitn(2, ' ').collect();
1745 let method_raw = if !parts.is_empty() {
1746 parts[0].to_uppercase()
1747 } else {
1748 "GET".to_string()
1749 };
1750 let method = if !parts.is_empty() {
1751 let m = parts[0].to_lowercase();
1752 if m == "delete" { "del".to_string() } else { m }
1754 } else {
1755 "get".to_string()
1756 };
1757 let raw_path = if parts.len() >= 2 { parts[1] } else { "/" };
1758 let path = if let Some(ref bp) = api_base_path {
1760 format!("{}{}", bp, raw_path)
1761 } else {
1762 raw_path.to_string()
1763 };
1764 let is_get_or_head = method == "get" || method == "head";
1765 let has_body = matches!(method.as_str(), "post" | "put" | "patch");
1767
1768 let body_value = if has_body {
1770 param_overrides.as_ref()
1771 .map(|po| po.get_for_operation(None, &method_raw, raw_path))
1772 .and_then(|oo| oo.body)
1773 .unwrap_or_else(|| serde_json::json!({}))
1774 } else {
1775 serde_json::json!({})
1776 };
1777
1778 let processed_body = DynamicParamProcessor::process_json_body(&body_value);
1780 let body_has_extracted_placeholders = processed_body.value.contains("${extracted.");
1785 let body_is_dynamic = processed_body.is_dynamic || body_has_extracted_placeholders;
1786
1787 serde_json::json!({
1788 "operation": s.operation,
1789 "method": method,
1790 "path": path,
1791 "extract": s.extract,
1792 "use_values": s.use_values,
1793 "use_body": s.use_body,
1794 "merge_body": if s.merge_body.is_empty() { None } else { Some(&s.merge_body) },
1795 "inject_attacks": s.inject_attacks,
1796 "attack_types": s.attack_types,
1797 "description": s.description,
1798 "display_name": s.description.clone().unwrap_or_else(|| format!("Step {}", idx)),
1799 "is_get_or_head": is_get_or_head,
1800 "has_body": has_body,
1801 "body": processed_body.value,
1802 "body_is_dynamic": body_is_dynamic,
1803 "_placeholders": processed_body.placeholders.iter().map(|p| format!("{:?}", p)).collect::<Vec<_>>(),
1804 })
1805 }).collect::<Vec<_>>(),
1806 })
1807 }).collect();
1808
1809 for flow_data in &flows_data {
1811 if let Some(steps) = flow_data.get("steps").and_then(|s| s.as_array()) {
1812 for step in steps {
1813 if let Some(placeholders_arr) =
1814 step.get("_placeholders").and_then(|p| p.as_array())
1815 {
1816 for p_str in placeholders_arr {
1817 if let Some(p_name) = p_str.as_str() {
1818 match p_name {
1820 "VU" => {
1821 all_placeholders.insert(DynamicPlaceholder::VU);
1822 }
1823 "Iteration" => {
1824 all_placeholders.insert(DynamicPlaceholder::Iteration);
1825 }
1826 "Timestamp" => {
1827 all_placeholders.insert(DynamicPlaceholder::Timestamp);
1828 }
1829 "UUID" => {
1830 all_placeholders.insert(DynamicPlaceholder::UUID);
1831 }
1832 "Random" => {
1833 all_placeholders.insert(DynamicPlaceholder::Random);
1834 }
1835 "Counter" => {
1836 all_placeholders.insert(DynamicPlaceholder::Counter);
1837 }
1838 "Date" => {
1839 all_placeholders.insert(DynamicPlaceholder::Date);
1840 }
1841 "VuIter" => {
1842 all_placeholders.insert(DynamicPlaceholder::VuIter);
1843 }
1844 _ => {}
1845 }
1846 }
1847 }
1848 }
1849 }
1850 }
1851 }
1852
1853 let required_imports = DynamicParamProcessor::get_required_imports(&all_placeholders);
1855 let required_globals = DynamicParamProcessor::get_required_globals(&all_placeholders);
1856
1857 let invalid_data_config = self.build_invalid_data_config();
1859 let error_injection_enabled = invalid_data_config.is_some();
1860 let error_rate = self.error_rate.unwrap_or(0.0);
1861 let error_types: Vec<String> = invalid_data_config
1862 .as_ref()
1863 .map(|c| c.error_types.iter().map(|t| format!("{:?}", t)).collect())
1864 .unwrap_or_default();
1865
1866 if error_injection_enabled {
1867 TerminalReporter::print_progress(&format!(
1868 "Error injection enabled ({}% rate)",
1869 (error_rate * 100.0) as u32
1870 ));
1871 }
1872
1873 let security_testing_enabled = self.wafbench_dir.is_some() || self.security_test;
1875
1876 let data = serde_json::json!({
1877 "base_url": self.target,
1878 "flows": flows_data,
1879 "extract_fields": config.default_extract_fields,
1880 "duration_secs": duration_secs,
1881 "max_vus": self.vus,
1882 "auth_header": self.auth,
1883 "custom_headers": custom_headers,
1884 "skip_tls_verify": self.skip_tls_verify,
1885 "stages": stages.iter().map(|s| serde_json::json!({
1887 "duration": s.duration,
1888 "target": s.target,
1889 })).collect::<Vec<_>>(),
1890 "threshold_percentile": self.threshold_percentile,
1891 "threshold_ms": self.threshold_ms,
1892 "max_error_rate": self.max_error_rate,
1893 "headers": headers_json,
1894 "dynamic_imports": required_imports,
1895 "dynamic_globals": required_globals,
1896 "extracted_values_output_path": self
1897 .output
1898 .join("crud_flow_extracted_values.json")
1899 .to_string_lossy(),
1900 "error_injection_enabled": error_injection_enabled,
1902 "error_rate": error_rate,
1903 "error_types": error_types,
1904 "security_testing_enabled": security_testing_enabled,
1906 "has_custom_headers": !custom_headers.is_empty(),
1907 });
1908
1909 let mut script = handlebars
1910 .render_template(template, &data)
1911 .map_err(|e| BenchError::ScriptGenerationFailed(e.to_string()))?;
1912
1913 if security_testing_enabled {
1915 script = self.generate_enhanced_script(&script)?;
1916 }
1917
1918 TerminalReporter::print_progress("Validating CRUD flow script...");
1920 let validation_errors = K6ScriptGenerator::validate_script(&script);
1921 if !validation_errors.is_empty() {
1922 TerminalReporter::print_error("CRUD flow script validation failed");
1923 for error in &validation_errors {
1924 eprintln!(" {}", error);
1925 }
1926 return Err(BenchError::Other(format!(
1927 "CRUD flow script validation failed with {} error(s)",
1928 validation_errors.len()
1929 )));
1930 }
1931
1932 TerminalReporter::print_success("CRUD flow script generated");
1933
1934 let script_path = if let Some(output) = &self.script_output {
1936 output.clone()
1937 } else {
1938 self.output.join("k6-crud-flow-script.js")
1939 };
1940
1941 if let Some(parent) = script_path.parent() {
1942 std::fs::create_dir_all(parent)?;
1943 }
1944 std::fs::write(&script_path, &script)?;
1945 TerminalReporter::print_success(&format!("Script written to: {}", script_path.display()));
1946
1947 if self.generate_only {
1948 println!("\nScript generated successfully. Run it with:");
1949 println!(" k6 run {}", script_path.display());
1950 return Ok(());
1951 }
1952
1953 TerminalReporter::print_progress("Executing CRUD flow test...");
1955 let executor = K6Executor::new()?;
1956 std::fs::create_dir_all(&self.output)?;
1957
1958 let results = executor.execute(&script_path, Some(&self.output), self.verbose).await?;
1959
1960 let duration_secs = Self::parse_duration(&self.duration)?;
1961 TerminalReporter::print_summary_with_mode(&results, duration_secs, self.no_keep_alive);
1962
1963 Ok(())
1964 }
1965
1966 async fn execute_conformance_test(&self) -> Result<()> {
1968 use crate::conformance::generator::{ConformanceConfig, ConformanceGenerator};
1969 use crate::conformance::report::ConformanceReport;
1970 use crate::conformance::spec::ConformanceFeature;
1971
1972 TerminalReporter::print_progress("OpenAPI 3.0.0 Conformance Testing Mode");
1973
1974 TerminalReporter::print_progress(
1977 "Conformance mode runs 1 VU, 1 iteration per endpoint (--vus and -d are ignored)",
1978 );
1979
1980 let categories = self.conformance_categories.as_ref().map(|cats_str| {
1982 cats_str
1983 .split(',')
1984 .filter_map(|s| {
1985 let trimmed = s.trim();
1986 if let Some(canonical) = ConformanceFeature::category_from_cli_name(trimmed) {
1987 Some(canonical.to_string())
1988 } else {
1989 TerminalReporter::print_warning(&format!(
1990 "Unknown conformance category: '{}'. Valid categories: {}",
1991 trimmed,
1992 ConformanceFeature::cli_category_names()
1993 .iter()
1994 .map(|(cli, _)| *cli)
1995 .collect::<Vec<_>>()
1996 .join(", ")
1997 ));
1998 None
1999 }
2000 })
2001 .collect::<Vec<String>>()
2002 });
2003
2004 let custom_headers: Vec<(String, String)> = self
2006 .conformance_headers
2007 .iter()
2008 .filter_map(|h| {
2009 let (name, value) = h.split_once(':')?;
2010 Some((name.trim().to_string(), value.trim().to_string()))
2011 })
2012 .collect();
2013
2014 if !custom_headers.is_empty() {
2015 TerminalReporter::print_progress(&format!(
2016 "Using {} custom header(s) for authentication",
2017 custom_headers.len()
2018 ));
2019 }
2020
2021 if self.conformance_delay_ms > 0 {
2022 TerminalReporter::print_progress(&format!(
2023 "Using {}ms delay between conformance requests",
2024 self.conformance_delay_ms
2025 ));
2026 }
2027
2028 std::fs::create_dir_all(&self.output)?;
2030
2031 let config = ConformanceConfig {
2032 target_url: self.target.clone(),
2033 api_key: self.conformance_api_key.clone(),
2034 basic_auth: self.conformance_basic_auth.clone(),
2035 skip_tls_verify: self.skip_tls_verify,
2036 categories,
2037 base_path: self.base_path.clone(),
2038 custom_headers,
2039 output_dir: Some(self.output.clone()),
2040 all_operations: self.conformance_all_operations,
2041 custom_checks_file: self.conformance_custom.clone(),
2042 request_delay_ms: self.conformance_delay_ms,
2043 custom_filter: self.conformance_custom_filter.clone(),
2044 export_requests: self.export_requests,
2045 validate_requests: self.validate_requests,
2046 };
2047
2048 let annotated_ops = if !self.spec.is_empty() {
2051 TerminalReporter::print_progress("Spec-driven conformance mode: analyzing spec...");
2052 let parser = SpecParser::from_file(&self.spec[0]).await?;
2053 let operations = parser.get_operations();
2054
2055 let annotated =
2056 crate::conformance::spec_driven::SpecDrivenConformanceGenerator::annotate_operations(
2057 &operations,
2058 parser.spec(),
2059 );
2060 TerminalReporter::print_success(&format!(
2061 "Analyzed {} operations, found {} feature annotations",
2062 operations.len(),
2063 annotated.iter().map(|a| a.features.len()).sum::<usize>()
2064 ));
2065 Some(annotated)
2066 } else {
2067 None
2068 };
2069
2070 if self.validate_requests && !self.spec.is_empty() {
2072 TerminalReporter::print_progress("Validating requests against OpenAPI spec...");
2073 let violation_count = crate::conformance::request_validator::run_request_validation(
2074 &self.spec,
2075 self.conformance_custom.as_deref(),
2076 self.base_path.as_deref(),
2077 &self.output,
2078 )
2079 .await?;
2080 if violation_count > 0 {
2081 TerminalReporter::print_warning(&format!(
2082 "{} request validation violation(s) found — see conformance-request-violations.json",
2083 violation_count
2084 ));
2085 } else {
2086 TerminalReporter::print_success("All requests conform to the OpenAPI spec");
2087 }
2088 }
2089
2090 if self.generate_only || self.use_k6 {
2092 let script = if let Some(annotated) = &annotated_ops {
2093 let gen = crate::conformance::spec_driven::SpecDrivenConformanceGenerator::new(
2094 config,
2095 annotated.clone(),
2096 );
2097 let op_count = gen.operation_count();
2098 let (script, check_count) = gen.generate()?;
2099 TerminalReporter::print_success(&format!(
2100 "Conformance: {} operations analyzed, {} unique checks generated",
2101 op_count, check_count
2102 ));
2103 script
2104 } else {
2105 let generator = ConformanceGenerator::new(config);
2106 generator.generate()?
2107 };
2108
2109 let script_path = self.output.join("k6-conformance.js");
2110 std::fs::write(&script_path, &script).map_err(|e| {
2111 BenchError::Other(format!("Failed to write conformance script: {}", e))
2112 })?;
2113 TerminalReporter::print_success(&format!(
2114 "Conformance script generated: {}",
2115 script_path.display()
2116 ));
2117
2118 if self.generate_only {
2119 println!("\nScript generated. Run with:");
2120 println!(" k6 run {}", script_path.display());
2121 return Ok(());
2122 }
2123
2124 if !K6Executor::is_k6_installed() {
2126 TerminalReporter::print_error("k6 is not installed");
2127 TerminalReporter::print_warning(
2128 "Install k6 from: https://k6.io/docs/get-started/installation/",
2129 );
2130 return Err(BenchError::K6NotFound);
2131 }
2132
2133 TerminalReporter::print_progress("Running conformance tests via k6...");
2134 let executor = K6Executor::new()?;
2135 executor.execute(&script_path, Some(&self.output), self.verbose).await?;
2136
2137 let report_path = self.output.join("conformance-report.json");
2138 if report_path.exists() {
2139 let report = ConformanceReport::from_file(&report_path)?;
2140 report.print_report_with_options(self.conformance_all_operations);
2141 self.save_conformance_report(&report, &report_path)?;
2142 } else {
2143 TerminalReporter::print_warning(
2144 "Conformance report not generated (k6 handleSummary may not have run)",
2145 );
2146 }
2147
2148 return Ok(());
2149 }
2150
2151 TerminalReporter::print_progress("Running conformance tests (native executor)...");
2153
2154 let mut executor = crate::conformance::executor::NativeConformanceExecutor::new(config)?;
2155
2156 executor = if let Some(annotated) = &annotated_ops {
2157 executor.with_spec_driven_checks(annotated)
2158 } else {
2159 executor.with_reference_checks()
2160 };
2161 executor = executor.with_custom_checks()?;
2162
2163 TerminalReporter::print_success(&format!(
2164 "Executing {} conformance checks...",
2165 executor.check_count()
2166 ));
2167
2168 let report = executor.execute().await?;
2169 report.print_report_with_options(self.conformance_all_operations);
2170
2171 let failure_details = report.failure_details();
2173 if !failure_details.is_empty() {
2174 let details_path = self.output.join("conformance-failure-details.json");
2175 if let Ok(json) = serde_json::to_string_pretty(&failure_details) {
2176 let _ = std::fs::write(&details_path, json);
2177 TerminalReporter::print_success(&format!(
2178 "Failure details saved to: {}",
2179 details_path.display()
2180 ));
2181 }
2182 }
2183
2184 let report_path = self.output.join("conformance-report.json");
2186 let report_json = serde_json::to_string_pretty(&report.to_json())
2187 .map_err(|e| BenchError::Other(format!("Failed to serialize report: {}", e)))?;
2188 std::fs::write(&report_path, &report_json)
2189 .map_err(|e| BenchError::Other(format!("Failed to write report: {}", e)))?;
2190 TerminalReporter::print_success(&format!("Report saved to: {}", report_path.display()));
2191
2192 self.save_conformance_report(&report, &report_path)?;
2193
2194 Ok(())
2195 }
2196
2197 fn save_conformance_report(
2199 &self,
2200 report: &crate::conformance::report::ConformanceReport,
2201 report_path: &Path,
2202 ) -> Result<()> {
2203 if self.conformance_report_format == "sarif" {
2204 use crate::conformance::sarif::ConformanceSarifReport;
2205 ConformanceSarifReport::write(report, &self.target, &self.conformance_report)?;
2206 TerminalReporter::print_success(&format!(
2207 "SARIF report saved to: {}",
2208 self.conformance_report.display()
2209 ));
2210 } else if self.conformance_report != *report_path {
2211 std::fs::copy(report_path, &self.conformance_report)?;
2212 TerminalReporter::print_success(&format!(
2213 "Report saved to: {}",
2214 self.conformance_report.display()
2215 ));
2216 }
2217 Ok(())
2218 }
2219
2220 async fn execute_multi_target_conformance(&self, targets_file: &Path) -> Result<()> {
2226 use crate::conformance::generator::{ConformanceConfig, ConformanceGenerator};
2227 use crate::conformance::report::ConformanceReport;
2228 use crate::conformance::spec::ConformanceFeature;
2229
2230 TerminalReporter::print_progress("Multi-target OpenAPI 3.0.0 Conformance Testing Mode");
2231
2232 TerminalReporter::print_progress("Parsing targets file...");
2234 let targets = parse_targets_file(targets_file)?;
2235 let num_targets = targets.len();
2236 TerminalReporter::print_success(&format!("Loaded {} targets", num_targets));
2237
2238 if targets.is_empty() {
2239 return Err(BenchError::Other("No targets found in file".to_string()));
2240 }
2241
2242 TerminalReporter::print_progress(
2243 "Conformance mode runs 1 VU, 1 iteration per endpoint (--vus and -d are ignored)",
2244 );
2245
2246 let categories = self.conformance_categories.as_ref().map(|cats_str| {
2248 cats_str
2249 .split(',')
2250 .filter_map(|s| {
2251 let trimmed = s.trim();
2252 if let Some(canonical) = ConformanceFeature::category_from_cli_name(trimmed) {
2253 Some(canonical.to_string())
2254 } else {
2255 TerminalReporter::print_warning(&format!(
2256 "Unknown conformance category: '{}'. Valid categories: {}",
2257 trimmed,
2258 ConformanceFeature::cli_category_names()
2259 .iter()
2260 .map(|(cli, _)| *cli)
2261 .collect::<Vec<_>>()
2262 .join(", ")
2263 ));
2264 None
2265 }
2266 })
2267 .collect::<Vec<String>>()
2268 });
2269
2270 let base_custom_headers: Vec<(String, String)> = self
2272 .conformance_headers
2273 .iter()
2274 .filter_map(|h| {
2275 let (name, value) = h.split_once(':')?;
2276 Some((name.trim().to_string(), value.trim().to_string()))
2277 })
2278 .collect();
2279
2280 if !base_custom_headers.is_empty() {
2281 TerminalReporter::print_progress(&format!(
2282 "Using {} base custom header(s) for authentication",
2283 base_custom_headers.len()
2284 ));
2285 }
2286
2287 let annotated_ops = if !self.spec.is_empty() {
2289 TerminalReporter::print_progress("Spec-driven conformance mode: analyzing spec...");
2290 let parser = SpecParser::from_file(&self.spec[0]).await?;
2291 let operations = parser.get_operations();
2292 let annotated =
2293 crate::conformance::spec_driven::SpecDrivenConformanceGenerator::annotate_operations(
2294 &operations,
2295 parser.spec(),
2296 );
2297 TerminalReporter::print_success(&format!(
2298 "Analyzed {} operations, found {} feature annotations",
2299 operations.len(),
2300 annotated.iter().map(|a| a.features.len()).sum::<usize>()
2301 ));
2302 Some(annotated)
2303 } else {
2304 None
2305 };
2306
2307 std::fs::create_dir_all(&self.output)?;
2309
2310 struct TargetResult {
2312 url: String,
2313 passed: usize,
2314 failed: usize,
2315 elapsed: std::time::Duration,
2316 report_json: serde_json::Value,
2317 owasp_coverage: Vec<crate::conformance::report::OwaspCoverageEntry>,
2318 }
2319
2320 let mut target_results: Vec<TargetResult> = Vec::with_capacity(num_targets);
2321 let total_start = std::time::Instant::now();
2322
2323 for (idx, target) in targets.iter().enumerate() {
2324 tracing::info!(
2325 "Running conformance tests against target {}/{}: {}",
2326 idx + 1,
2327 num_targets,
2328 target.url
2329 );
2330 TerminalReporter::print_progress(&format!(
2331 "\n--- Target {}/{}: {} ---",
2332 idx + 1,
2333 num_targets,
2334 target.url
2335 ));
2336
2337 let mut merged_headers = base_custom_headers.clone();
2339 if let Some(ref target_headers) = target.headers {
2340 for (name, value) in target_headers {
2341 if let Some(existing) = merged_headers.iter_mut().find(|(n, _)| n == name) {
2343 existing.1 = value.clone();
2344 } else {
2345 merged_headers.push((name.clone(), value.clone()));
2346 }
2347 }
2348 }
2349 if let Some(ref auth) = target.auth {
2351 if let Some(existing) =
2352 merged_headers.iter_mut().find(|(n, _)| n.eq_ignore_ascii_case("Authorization"))
2353 {
2354 existing.1 = auth.clone();
2355 } else {
2356 merged_headers.push(("Authorization".to_string(), auth.clone()));
2357 }
2358 }
2359
2360 let target_dir = self.output.join(format!("target_{}", idx));
2366 std::fs::create_dir_all(&target_dir)?;
2367
2368 let config = ConformanceConfig {
2369 target_url: target.url.clone(),
2370 api_key: self.conformance_api_key.clone(),
2371 basic_auth: self.conformance_basic_auth.clone(),
2372 skip_tls_verify: self.skip_tls_verify,
2373 categories: categories.clone(),
2374 base_path: self.base_path.clone(),
2375 custom_headers: merged_headers,
2376 output_dir: Some(target_dir.clone()),
2377 all_operations: self.conformance_all_operations,
2378 custom_checks_file: self.conformance_custom.clone(),
2379 request_delay_ms: self.conformance_delay_ms,
2380 custom_filter: self.conformance_custom_filter.clone(),
2381 export_requests: self.export_requests,
2382 validate_requests: self.validate_requests,
2383 };
2384
2385 let target_start = std::time::Instant::now();
2386 let report = if self.use_k6 {
2387 if !K6Executor::is_k6_installed() {
2388 TerminalReporter::print_error("k6 is not installed");
2389 TerminalReporter::print_warning(
2390 "Install k6 from: https://k6.io/docs/get-started/installation/",
2391 );
2392 return Err(BenchError::K6NotFound);
2393 }
2394
2395 let script = if let Some(ref annotated) = annotated_ops {
2396 let gen = crate::conformance::spec_driven::SpecDrivenConformanceGenerator::new(
2397 config.clone(),
2398 annotated.clone(),
2399 );
2400 let (script, _check_count) = gen.generate()?;
2401 script
2402 } else {
2403 let generator = ConformanceGenerator::new(config.clone());
2404 generator.generate()?
2405 };
2406
2407 let script_path = target_dir.join("k6-conformance.js");
2408 std::fs::write(&script_path, &script).map_err(|e| {
2409 BenchError::Other(format!("Failed to write conformance script: {}", e))
2410 })?;
2411 TerminalReporter::print_success(&format!(
2412 "Conformance script generated: {}",
2413 script_path.display()
2414 ));
2415
2416 TerminalReporter::print_progress(&format!(
2417 "Running conformance tests via k6 against {}...",
2418 target.url
2419 ));
2420 let k6 = K6Executor::new()?;
2421 let api_port = 6565u16.saturating_add(idx as u16);
2423 k6.execute_with_port(&script_path, Some(&target_dir), self.verbose, Some(api_port))
2424 .await?;
2425
2426 let report_path = target_dir.join("conformance-report.json");
2427 if report_path.exists() {
2428 ConformanceReport::from_file(&report_path)?
2429 } else {
2430 TerminalReporter::print_warning(&format!(
2431 "Conformance report not generated for target {} (k6 handleSummary may not have run)",
2432 target.url
2433 ));
2434 continue;
2435 }
2436 } else {
2437 let mut executor =
2438 crate::conformance::executor::NativeConformanceExecutor::new(config)?;
2439
2440 executor = if let Some(ref annotated) = annotated_ops {
2441 executor.with_spec_driven_checks(annotated)
2442 } else {
2443 executor.with_reference_checks()
2444 };
2445 executor = executor.with_custom_checks()?;
2446
2447 TerminalReporter::print_success(&format!(
2448 "Executing {} conformance checks against {}...",
2449 executor.check_count(),
2450 target.url
2451 ));
2452
2453 executor.execute().await?
2454 };
2455 let target_elapsed = target_start.elapsed();
2456
2457 let report_json = report.to_json();
2458
2459 let passed = report_json["summary"]["passed"].as_u64().unwrap_or(0) as usize;
2461 let failed = report_json["summary"]["failed"].as_u64().unwrap_or(0) as usize;
2462 let total_checks = passed + failed;
2463 let rate = if total_checks == 0 {
2464 0.0
2465 } else {
2466 (passed as f64 / total_checks as f64) * 100.0
2467 };
2468
2469 TerminalReporter::print_success(&format!(
2470 "Target {}: {}/{} passed ({:.1}%) in {:.1}s",
2471 target.url,
2472 passed,
2473 total_checks,
2474 rate,
2475 target_elapsed.as_secs_f64()
2476 ));
2477
2478 let target_report_path = target_dir.join("conformance-report.json");
2480 let report_str = serde_json::to_string_pretty(&report_json)
2481 .map_err(|e| BenchError::Other(format!("Failed to serialize report: {}", e)))?;
2482 std::fs::write(&target_report_path, &report_str)
2483 .map_err(|e| BenchError::Other(format!("Failed to write report: {}", e)))?;
2484
2485 let failure_details = report.failure_details();
2487 if !failure_details.is_empty() {
2488 let details_path = target_dir.join("conformance-failure-details.json");
2489 if let Ok(json) = serde_json::to_string_pretty(&failure_details) {
2490 let _ = std::fs::write(&details_path, json);
2491 }
2492 }
2493
2494 let owasp_coverage = report.owasp_coverage_data();
2496
2497 target_results.push(TargetResult {
2498 url: target.url.clone(),
2499 passed,
2500 failed,
2501 elapsed: target_elapsed,
2502 report_json,
2503 owasp_coverage,
2504 });
2505 }
2506
2507 let total_elapsed = total_start.elapsed();
2508
2509 println!("\n{}", "=".repeat(80));
2511 println!(" Multi-Target Conformance Summary");
2512 println!("{}", "=".repeat(80));
2513 println!(
2514 " {:<40} {:>8} {:>8} {:>8} {:>8}",
2515 "Target URL", "Passed", "Failed", "Rate", "Time"
2516 );
2517 println!(" {}", "-".repeat(76));
2518
2519 let mut total_passed = 0usize;
2520 let mut total_failed = 0usize;
2521
2522 for result in &target_results {
2523 let total_checks = result.passed + result.failed;
2524 let rate = if total_checks == 0 {
2525 0.0
2526 } else {
2527 (result.passed as f64 / total_checks as f64) * 100.0
2528 };
2529
2530 let display_url = if result.url.len() > 38 {
2532 format!("{}...", &result.url[..35])
2533 } else {
2534 result.url.clone()
2535 };
2536
2537 println!(
2538 " {:<40} {:>8} {:>8} {:>7.1}% {:>6.1}s",
2539 display_url,
2540 result.passed,
2541 result.failed,
2542 rate,
2543 result.elapsed.as_secs_f64()
2544 );
2545
2546 total_passed += result.passed;
2547 total_failed += result.failed;
2548 }
2549
2550 let grand_total = total_passed + total_failed;
2551 let overall_rate = if grand_total == 0 {
2552 0.0
2553 } else {
2554 (total_passed as f64 / grand_total as f64) * 100.0
2555 };
2556
2557 println!(" {}", "-".repeat(76));
2558 println!(
2559 " {:<40} {:>8} {:>8} {:>7.1}% {:>6.1}s",
2560 format!("TOTAL ({} targets)", num_targets),
2561 total_passed,
2562 total_failed,
2563 overall_rate,
2564 total_elapsed.as_secs_f64()
2565 );
2566 println!("{}", "=".repeat(80));
2567
2568 for result in &target_results {
2570 println!("\n OWASP API Security Top 10 Coverage for {}:", result.url);
2571 for entry in &result.owasp_coverage {
2572 let status = if !entry.tested {
2573 "-"
2574 } else if entry.all_passed {
2575 "pass"
2576 } else {
2577 "FAIL"
2578 };
2579 let via = if entry.via_categories.is_empty() {
2580 String::new()
2581 } else {
2582 format!(" (via {})", entry.via_categories.join(", "))
2583 };
2584 println!(" {:<12} {:<40} {}{}", entry.id, entry.name, status, via);
2585 }
2586 }
2587
2588 let per_target_summaries: Vec<serde_json::Value> = target_results
2590 .iter()
2591 .enumerate()
2592 .map(|(idx, r)| {
2593 let total_checks = r.passed + r.failed;
2594 let rate = if total_checks == 0 {
2595 0.0
2596 } else {
2597 (r.passed as f64 / total_checks as f64) * 100.0
2598 };
2599 let owasp_json: Vec<serde_json::Value> = r
2600 .owasp_coverage
2601 .iter()
2602 .map(|e| {
2603 serde_json::json!({
2604 "id": e.id,
2605 "name": e.name,
2606 "tested": e.tested,
2607 "all_passed": e.all_passed,
2608 "via_categories": e.via_categories,
2609 })
2610 })
2611 .collect();
2612 serde_json::json!({
2613 "target_url": r.url,
2614 "target_index": idx,
2615 "checks_passed": r.passed,
2616 "checks_failed": r.failed,
2617 "total_checks": total_checks,
2618 "pass_rate": rate,
2619 "elapsed_seconds": r.elapsed.as_secs_f64(),
2620 "report": r.report_json,
2621 "owasp_coverage": owasp_json,
2622 })
2623 })
2624 .collect();
2625
2626 let combined_summary = serde_json::json!({
2627 "total_targets": num_targets,
2628 "total_checks_passed": total_passed,
2629 "total_checks_failed": total_failed,
2630 "overall_pass_rate": overall_rate,
2631 "total_elapsed_seconds": total_elapsed.as_secs_f64(),
2632 "targets": per_target_summaries,
2633 });
2634
2635 let summary_path = self.output.join("multi-target-conformance-summary.json");
2636 let summary_str = serde_json::to_string_pretty(&combined_summary)
2637 .map_err(|e| BenchError::Other(format!("Failed to serialize summary: {}", e)))?;
2638 std::fs::write(&summary_path, &summary_str)
2639 .map_err(|e| BenchError::Other(format!("Failed to write summary: {}", e)))?;
2640 TerminalReporter::print_success(&format!(
2641 "Combined summary saved to: {}",
2642 summary_path.display()
2643 ));
2644
2645 Ok(())
2646 }
2647
2648 async fn execute_owasp_test(&self, parser: &SpecParser) -> Result<()> {
2650 TerminalReporter::print_progress("OWASP API Security Top 10 Testing Mode");
2651
2652 let custom_headers = self.parse_headers()?;
2654
2655 let mut config = OwaspApiConfig::new()
2657 .with_auth_header(&self.owasp_auth_header)
2658 .with_verbose(self.verbose)
2659 .with_insecure(self.skip_tls_verify)
2660 .with_concurrency(self.vus as usize)
2661 .with_iterations(self.owasp_iterations as usize)
2662 .with_base_path(self.base_path.clone())
2663 .with_custom_headers(custom_headers);
2664
2665 if let Some(ref token) = self.owasp_auth_token {
2667 config = config.with_valid_auth_token(token);
2668 }
2669
2670 if let Some(ref cats_str) = self.owasp_categories {
2672 let categories: Vec<OwaspCategory> = cats_str
2673 .split(',')
2674 .filter_map(|s| {
2675 let trimmed = s.trim();
2676 match trimmed.parse::<OwaspCategory>() {
2677 Ok(cat) => Some(cat),
2678 Err(e) => {
2679 TerminalReporter::print_warning(&e);
2680 None
2681 }
2682 }
2683 })
2684 .collect();
2685
2686 if !categories.is_empty() {
2687 config = config.with_categories(categories);
2688 }
2689 }
2690
2691 if let Some(ref admin_paths_file) = self.owasp_admin_paths {
2693 config.admin_paths_file = Some(admin_paths_file.clone());
2694 if let Err(e) = config.load_admin_paths() {
2695 TerminalReporter::print_warning(&format!("Failed to load admin paths file: {}", e));
2696 }
2697 }
2698
2699 if let Some(ref id_fields_str) = self.owasp_id_fields {
2701 let id_fields: Vec<String> = id_fields_str
2702 .split(',')
2703 .map(|s| s.trim().to_string())
2704 .filter(|s| !s.is_empty())
2705 .collect();
2706 if !id_fields.is_empty() {
2707 config = config.with_id_fields(id_fields);
2708 }
2709 }
2710
2711 if let Some(ref report_path) = self.owasp_report {
2713 config = config.with_report_path(report_path);
2714 }
2715 if let Ok(format) = self.owasp_report_format.parse::<ReportFormat>() {
2716 config = config.with_report_format(format);
2717 }
2718
2719 let categories = config.categories_to_test();
2721 TerminalReporter::print_success(&format!(
2722 "Testing {} OWASP categories: {}",
2723 categories.len(),
2724 categories.iter().map(|c| c.cli_name()).collect::<Vec<_>>().join(", ")
2725 ));
2726
2727 if config.valid_auth_token.is_some() {
2728 TerminalReporter::print_progress("Using provided auth token for baseline requests");
2729 }
2730
2731 TerminalReporter::print_progress("Generating OWASP security test script...");
2733 let generator = OwaspApiGenerator::new(config, self.target.clone(), parser);
2734
2735 let script = generator.generate()?;
2737 TerminalReporter::print_success("OWASP security test script generated");
2738
2739 let script_path = if let Some(output) = &self.script_output {
2741 output.clone()
2742 } else {
2743 self.output.join("k6-owasp-security-test.js")
2744 };
2745
2746 if let Some(parent) = script_path.parent() {
2747 std::fs::create_dir_all(parent)?;
2748 }
2749 std::fs::write(&script_path, &script)?;
2750 TerminalReporter::print_success(&format!("Script written to: {}", script_path.display()));
2751
2752 if self.generate_only {
2754 println!("\nOWASP security test script generated. Run it with:");
2755 println!(" k6 run {}", script_path.display());
2756 return Ok(());
2757 }
2758
2759 TerminalReporter::print_progress("Executing OWASP security tests...");
2761 let executor = K6Executor::new()?;
2762 std::fs::create_dir_all(&self.output)?;
2763
2764 let results = executor.execute(&script_path, Some(&self.output), self.verbose).await?;
2765
2766 let duration_secs = Self::parse_duration(&self.duration)?;
2767 TerminalReporter::print_summary_with_mode(&results, duration_secs, self.no_keep_alive);
2768
2769 println!("\nOWASP security test results saved to: {}", self.output.display());
2770
2771 Ok(())
2772 }
2773}
2774
2775#[cfg(test)]
2776mod tests {
2777 use super::*;
2778 use tempfile::tempdir;
2779
2780 #[test]
2781 fn test_parse_duration() {
2782 assert_eq!(BenchCommand::parse_duration("30s").unwrap(), 30);
2783 assert_eq!(BenchCommand::parse_duration("5m").unwrap(), 300);
2784 assert_eq!(BenchCommand::parse_duration("1h").unwrap(), 3600);
2785 assert_eq!(BenchCommand::parse_duration("60").unwrap(), 60);
2786 }
2787
2788 #[test]
2789 fn test_parse_duration_invalid() {
2790 assert!(BenchCommand::parse_duration("invalid").is_err());
2791 assert!(BenchCommand::parse_duration("30x").is_err());
2792 }
2793
2794 #[test]
2795 fn test_parse_headers() {
2796 let cmd = BenchCommand {
2797 spec: vec![PathBuf::from("test.yaml")],
2798 spec_dir: None,
2799 merge_conflicts: "error".to_string(),
2800 spec_mode: "merge".to_string(),
2801 dependency_config: None,
2802 target: "http://localhost".to_string(),
2803 base_path: None,
2804 duration: "1m".to_string(),
2805 vus: 10,
2806 scenario: "ramp-up".to_string(),
2807 operations: None,
2808 exclude_operations: None,
2809 auth: None,
2810 headers: Some("X-API-Key:test123,X-Client-ID:client456".to_string()),
2811 output: PathBuf::from("output"),
2812 generate_only: false,
2813 script_output: None,
2814 threshold_percentile: "p(95)".to_string(),
2815 threshold_ms: 500,
2816 max_error_rate: 0.05,
2817 verbose: false,
2818 skip_tls_verify: false,
2819 chunked_request_bodies: false,
2820 target_rps: None,
2821 no_keep_alive: false,
2822 targets_file: None,
2823 max_concurrency: None,
2824 results_format: "both".to_string(),
2825 params_file: None,
2826 crud_flow: false,
2827 flow_config: None,
2828 extract_fields: None,
2829 parallel_create: None,
2830 data_file: None,
2831 data_distribution: "unique-per-vu".to_string(),
2832 data_mappings: None,
2833 per_uri_control: false,
2834 error_rate: None,
2835 error_types: None,
2836 security_test: false,
2837 security_payloads: None,
2838 security_categories: None,
2839 security_target_fields: None,
2840 wafbench_dir: None,
2841 wafbench_cycle_all: false,
2842 owasp_api_top10: false,
2843 owasp_categories: None,
2844 owasp_auth_header: "Authorization".to_string(),
2845 owasp_auth_token: None,
2846 owasp_admin_paths: None,
2847 owasp_id_fields: None,
2848 owasp_report: None,
2849 owasp_report_format: "json".to_string(),
2850 owasp_iterations: 1,
2851 conformance: false,
2852 conformance_api_key: None,
2853 conformance_basic_auth: None,
2854 conformance_report: PathBuf::from("conformance-report.json"),
2855 conformance_categories: None,
2856 conformance_report_format: "json".to_string(),
2857 conformance_headers: vec![],
2858 conformance_all_operations: false,
2859 conformance_custom: None,
2860 conformance_delay_ms: 0,
2861 use_k6: false,
2862 conformance_custom_filter: None,
2863 export_requests: false,
2864 validate_requests: false,
2865 };
2866
2867 let headers = cmd.parse_headers().unwrap();
2868 assert_eq!(headers.get("X-API-Key"), Some(&"test123".to_string()));
2869 assert_eq!(headers.get("X-Client-ID"), Some(&"client456".to_string()));
2870 }
2871
2872 #[test]
2873 fn test_get_spec_display_name() {
2874 let cmd = BenchCommand {
2875 spec: vec![PathBuf::from("test.yaml")],
2876 spec_dir: None,
2877 merge_conflicts: "error".to_string(),
2878 spec_mode: "merge".to_string(),
2879 dependency_config: None,
2880 target: "http://localhost".to_string(),
2881 base_path: None,
2882 duration: "1m".to_string(),
2883 vus: 10,
2884 scenario: "ramp-up".to_string(),
2885 operations: None,
2886 exclude_operations: None,
2887 auth: None,
2888 headers: None,
2889 output: PathBuf::from("output"),
2890 generate_only: false,
2891 script_output: None,
2892 threshold_percentile: "p(95)".to_string(),
2893 threshold_ms: 500,
2894 max_error_rate: 0.05,
2895 verbose: false,
2896 skip_tls_verify: false,
2897 chunked_request_bodies: false,
2898 target_rps: None,
2899 no_keep_alive: false,
2900 targets_file: None,
2901 max_concurrency: None,
2902 results_format: "both".to_string(),
2903 params_file: None,
2904 crud_flow: false,
2905 flow_config: None,
2906 extract_fields: None,
2907 parallel_create: None,
2908 data_file: None,
2909 data_distribution: "unique-per-vu".to_string(),
2910 data_mappings: None,
2911 per_uri_control: false,
2912 error_rate: None,
2913 error_types: None,
2914 security_test: false,
2915 security_payloads: None,
2916 security_categories: None,
2917 security_target_fields: None,
2918 wafbench_dir: None,
2919 wafbench_cycle_all: false,
2920 owasp_api_top10: false,
2921 owasp_categories: None,
2922 owasp_auth_header: "Authorization".to_string(),
2923 owasp_auth_token: None,
2924 owasp_admin_paths: None,
2925 owasp_id_fields: None,
2926 owasp_report: None,
2927 owasp_report_format: "json".to_string(),
2928 owasp_iterations: 1,
2929 conformance: false,
2930 conformance_api_key: None,
2931 conformance_basic_auth: None,
2932 conformance_report: PathBuf::from("conformance-report.json"),
2933 conformance_categories: None,
2934 conformance_report_format: "json".to_string(),
2935 conformance_headers: vec![],
2936 conformance_all_operations: false,
2937 conformance_custom: None,
2938 conformance_delay_ms: 0,
2939 use_k6: false,
2940 conformance_custom_filter: None,
2941 export_requests: false,
2942 validate_requests: false,
2943 };
2944
2945 assert_eq!(cmd.get_spec_display_name(), "test.yaml");
2946
2947 let cmd_multi = BenchCommand {
2949 spec: vec![PathBuf::from("a.yaml"), PathBuf::from("b.yaml")],
2950 spec_dir: None,
2951 merge_conflicts: "error".to_string(),
2952 spec_mode: "merge".to_string(),
2953 dependency_config: None,
2954 target: "http://localhost".to_string(),
2955 base_path: None,
2956 duration: "1m".to_string(),
2957 vus: 10,
2958 scenario: "ramp-up".to_string(),
2959 operations: None,
2960 exclude_operations: None,
2961 auth: None,
2962 headers: None,
2963 output: PathBuf::from("output"),
2964 generate_only: false,
2965 script_output: None,
2966 threshold_percentile: "p(95)".to_string(),
2967 threshold_ms: 500,
2968 max_error_rate: 0.05,
2969 verbose: false,
2970 skip_tls_verify: false,
2971 chunked_request_bodies: false,
2972 target_rps: None,
2973 no_keep_alive: false,
2974 targets_file: None,
2975 max_concurrency: None,
2976 results_format: "both".to_string(),
2977 params_file: None,
2978 crud_flow: false,
2979 flow_config: None,
2980 extract_fields: None,
2981 parallel_create: None,
2982 data_file: None,
2983 data_distribution: "unique-per-vu".to_string(),
2984 data_mappings: None,
2985 per_uri_control: false,
2986 error_rate: None,
2987 error_types: None,
2988 security_test: false,
2989 security_payloads: None,
2990 security_categories: None,
2991 security_target_fields: None,
2992 wafbench_dir: None,
2993 wafbench_cycle_all: false,
2994 owasp_api_top10: false,
2995 owasp_categories: None,
2996 owasp_auth_header: "Authorization".to_string(),
2997 owasp_auth_token: None,
2998 owasp_admin_paths: None,
2999 owasp_id_fields: None,
3000 owasp_report: None,
3001 owasp_report_format: "json".to_string(),
3002 owasp_iterations: 1,
3003 conformance: false,
3004 conformance_api_key: None,
3005 conformance_basic_auth: None,
3006 conformance_report: PathBuf::from("conformance-report.json"),
3007 conformance_categories: None,
3008 conformance_report_format: "json".to_string(),
3009 conformance_headers: vec![],
3010 conformance_all_operations: false,
3011 conformance_custom: None,
3012 conformance_delay_ms: 0,
3013 use_k6: false,
3014 conformance_custom_filter: None,
3015 export_requests: false,
3016 validate_requests: false,
3017 };
3018
3019 assert_eq!(cmd_multi.get_spec_display_name(), "2 spec files");
3020 }
3021
3022 #[test]
3023 fn test_parse_extracted_values_from_output_dir() {
3024 let dir = tempdir().unwrap();
3025 let path = dir.path().join("extracted_values.json");
3026 std::fs::write(
3027 &path,
3028 r#"{
3029 "pool_id": "abc123",
3030 "count": 0,
3031 "enabled": false,
3032 "metadata": { "owner": "team-a" }
3033}"#,
3034 )
3035 .unwrap();
3036
3037 let extracted = BenchCommand::parse_extracted_values(dir.path()).unwrap();
3038 assert_eq!(extracted.get("pool_id"), Some(&serde_json::json!("abc123")));
3039 assert_eq!(extracted.get("count"), Some(&serde_json::json!(0)));
3040 assert_eq!(extracted.get("enabled"), Some(&serde_json::json!(false)));
3041 assert_eq!(extracted.get("metadata"), Some(&serde_json::json!({"owner": "team-a"})));
3042 }
3043
3044 #[test]
3045 fn test_parse_extracted_values_missing_file() {
3046 let dir = tempdir().unwrap();
3047 let extracted = BenchCommand::parse_extracted_values(dir.path()).unwrap();
3048 assert!(extracted.values.is_empty());
3049 }
3050}