pact_plugin_driver/
verification.rs

1//! Module containing code to deal with verifying interactions via plugins
2
3use std::collections::HashMap;
4
5use bytes::Bytes;
6use itertools::Either;
7use pact_models::prelude::OptionalBody;
8use serde_json::Value;
9
10use crate::proto::{VerificationResult, verification_result_item};
11
12/// Data required to execute the verification of an interaction
13#[derive(Clone, Debug, Default)]
14pub struct InteractionVerificationData {
15  /// Data for the request of the interaction
16  pub request_data: OptionalBody,
17  /// Metadata associated with the request
18  pub metadata: HashMap<String, Either<Value, Bytes>>
19}
20
21impl InteractionVerificationData {
22  /// Create a new InteractionVerificationData struct
23  pub fn new(request_data: OptionalBody, metadata: HashMap<String, Either<Value, Bytes>>) -> Self {
24    InteractionVerificationData {
25      request_data,
26      metadata,
27    }
28  }
29}
30
31/// Result of running an integration verification
32#[derive(Clone, Debug, Default)]
33pub struct InteractionVerificationResult {
34  /// If the verification was successful
35  pub ok: bool,
36  /// List of errors if not successful
37  pub details: Vec<InteractionVerificationDetails>,
38  /// Output to display to the user
39  pub output: Vec<String>
40}
41
42/// Details on an individual failure
43#[derive(Clone, Debug)]
44pub enum InteractionVerificationDetails {
45  /// Error occurred
46  Error(String),
47
48  /// Mismatch occurred
49  Mismatch {
50    expected: Bytes,
51    actual: Bytes,
52    mismatch: String,
53    path: String
54  }
55}
56
57impl From<&VerificationResult> for InteractionVerificationResult {
58  fn from(result: &VerificationResult) -> Self {
59    InteractionVerificationResult {
60      ok: result.success,
61      details: result.mismatches.iter()
62        .filter_map(|r| r.result.as_ref().map(|r| match r {
63          verification_result_item::Result::Error(err) => InteractionVerificationDetails::Error(err.to_string()),
64          verification_result_item::Result::Mismatch(mismatch) => InteractionVerificationDetails::Mismatch {
65            expected: mismatch.expected.clone().map(|b| Bytes::from(b)).unwrap_or_default(),
66            actual: mismatch.actual.clone().map(|b| Bytes::from(b)).unwrap_or_default(),
67            mismatch: mismatch.mismatch.to_string(),
68            path: mismatch.path.to_string()
69          }
70        }))
71        .collect(),
72      output: result.output.clone()
73    }
74  }
75}