pact_verifier_cli 1.3.1

Standalone pact verifier for provider pact verification
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
use quick_xml::se::to_utf8_io_writer;
use serde::Serialize;
use pact_matching::Mismatch;
use pact_verifier::verification_result::{VerificationExecutionResult, VerificationMismatchResult};

// XML-specific DTOs — quick_xml's serde serializer does not support enum struct variants,
// so we flatten VerificationMismatchResult and Mismatch into plain structs here.

#[derive(Debug, Serialize)]
#[serde(rename = "report")]
struct XmlReport {
  provider: String,
  result: bool,
  #[serde(skip_serializing_if = "XmlNotices::is_empty")]
  notices: XmlNotices,
  #[serde(skip_serializing_if = "XmlErrors::is_empty")]
  errors: XmlErrors,
  #[serde(skip_serializing_if = "XmlErrors::is_empty")]
  pending_errors: XmlErrors,
  #[serde(skip_serializing_if = "XmlInteractionResults::is_empty")]
  interaction_results: XmlInteractionResults,
}

#[derive(Debug, Default, Serialize)]
struct XmlNotices {
  #[serde(rename = "notice")]
  items: Vec<XmlNotice>,
}

impl XmlNotices {
  fn is_empty(&self) -> bool { self.items.is_empty() }
}

#[derive(Debug, Serialize)]
struct XmlNotice {
  #[serde(rename = "entry")]
  entries: Vec<XmlNoticeEntry>,
}

#[derive(Debug, Serialize)]
struct XmlNoticeEntry {
  key: String,
  value: String,
}

#[derive(Debug, Default, Serialize)]
struct XmlErrors {
  #[serde(rename = "error")]
  items: Vec<XmlError>,
}

impl XmlErrors {
  fn is_empty(&self) -> bool { self.items.is_empty() }
}

#[derive(Debug, Serialize)]
struct XmlError {
  interaction: String,
  mismatch: XmlMismatchResult,
}

#[derive(Debug, Serialize)]
struct XmlMismatchResult {
  #[serde(rename = "@type")]
  kind: String,
  #[serde(skip_serializing_if = "Option::is_none")]
  error_message: Option<String>,
  #[serde(skip_serializing_if = "Option::is_none")]
  interaction_id: Option<String>,
  #[serde(skip_serializing_if = "Option::is_none")]
  mismatches: Option<XmlMismatchList>,
}

#[derive(Debug, Serialize)]
struct XmlMismatchList {
  #[serde(rename = "mismatch")]
  items: Vec<XmlMismatch>,
}

#[derive(Debug, Serialize)]
struct XmlMismatch {
  #[serde(rename = "@type")]
  kind: String,
  description: String,
  #[serde(skip_serializing_if = "Option::is_none")]
  expected: Option<String>,
  #[serde(skip_serializing_if = "Option::is_none")]
  actual: Option<String>,
  #[serde(skip_serializing_if = "Option::is_none")]
  path: Option<String>,
  #[serde(skip_serializing_if = "Option::is_none")]
  key: Option<String>,
  #[serde(skip_serializing_if = "Option::is_none")]
  parameter: Option<String>,
}

#[derive(Debug, Default, Serialize)]
struct XmlInteractionResults {
  #[serde(rename = "interaction")]
  items: Vec<XmlInteractionResult>,
}

impl XmlInteractionResults {
  fn is_empty(&self) -> bool { self.items.is_empty() }
}

#[derive(Debug, Serialize)]
struct XmlInteractionResult {
  description: String,
  result: String,
  pending: bool,
  duration_ms: u128,
}

impl From<(&str, &VerificationExecutionResult)> for XmlReport {
  fn from((provider, exec_result): (&str, &VerificationExecutionResult)) -> Self {
    XmlReport {
      provider: provider.to_string(),
      result: exec_result.result,
      notices: XmlNotices {
        items: exec_result.notices.iter().map(|n| {
          let mut entries: Vec<XmlNoticeEntry> = n.iter().map(|(k, v)| XmlNoticeEntry {
            key: k.clone(),
            value: v.clone(),
          }).collect();
          entries.sort_by(|a, b| a.key.cmp(&b.key));
          XmlNotice { entries }
        }).collect()
      },
      errors: XmlErrors {
        items: exec_result.errors.iter().map(|(interaction, mismatch)| XmlError {
          interaction: interaction.clone(),
          mismatch: XmlMismatchResult::from(mismatch),
        }).collect()
      },
      pending_errors: XmlErrors {
        items: exec_result.pending_errors.iter().map(|(interaction, mismatch)| XmlError {
          interaction: interaction.clone(),
          mismatch: XmlMismatchResult::from(mismatch),
        }).collect()
      },
      interaction_results: XmlInteractionResults {
        items: exec_result.interaction_results.iter().map(|ir| XmlInteractionResult {
          description: ir.interaction_description.clone(),
          result: if ir.result.is_ok() { "OK".to_string() } else { "Error".to_string() },
          pending: ir.pending,
          duration_ms: ir.duration.as_millis(),
        }).collect()
      },
    }
  }
}

impl From<&VerificationMismatchResult> for XmlMismatchResult {
  fn from(result: &VerificationMismatchResult) -> Self {
    match result {
      VerificationMismatchResult::Mismatches { mismatches, interaction_id } => XmlMismatchResult {
        kind: "mismatches".to_string(),
        error_message: None,
        interaction_id: interaction_id.clone(),
        mismatches: Some(XmlMismatchList {
          items: mismatches.iter().map(XmlMismatch::from).collect(),
        }),
      },
      VerificationMismatchResult::Error { error, interaction_id } => XmlMismatchResult {
        kind: "error".to_string(),
        error_message: Some(error.clone()),
        interaction_id: interaction_id.clone(),
        mismatches: None,
      },
    }
  }
}

impl From<&Mismatch> for XmlMismatch {
  fn from(m: &Mismatch) -> Self {
    match m {
      Mismatch::MethodMismatch { expected, actual, mismatch } => XmlMismatch {
        kind: "MethodMismatch".to_string(),
        description: mismatch.clone(),
        expected: Some(expected.clone()),
        actual: Some(actual.clone()),
        path: None, key: None, parameter: None,
      },
      Mismatch::PathMismatch { expected, actual, mismatch } => XmlMismatch {
        kind: "PathMismatch".to_string(),
        description: mismatch.clone(),
        expected: Some(expected.clone()),
        actual: Some(actual.clone()),
        path: None, key: None, parameter: None,
      },
      Mismatch::StatusMismatch { expected, actual, mismatch } => XmlMismatch {
        kind: "StatusMismatch".to_string(),
        description: mismatch.clone(),
        expected: Some(expected.to_string()),
        actual: Some(actual.to_string()),
        path: None, key: None, parameter: None,
      },
      Mismatch::QueryMismatch { parameter, expected, actual, mismatch } => XmlMismatch {
        kind: "QueryMismatch".to_string(),
        description: mismatch.clone(),
        expected: Some(expected.clone()),
        actual: Some(actual.clone()),
        path: None, key: None,
        parameter: Some(parameter.clone()),
      },
      Mismatch::HeaderMismatch { key, expected, actual, mismatch } => XmlMismatch {
        kind: "HeaderMismatch".to_string(),
        description: mismatch.clone(),
        expected: Some(expected.clone()),
        actual: Some(actual.clone()),
        path: None,
        key: Some(key.clone()),
        parameter: None,
      },
      Mismatch::BodyTypeMismatch { expected, actual, mismatch, .. } => XmlMismatch {
        kind: "BodyTypeMismatch".to_string(),
        description: mismatch.clone(),
        expected: Some(expected.clone()),
        actual: Some(actual.clone()),
        path: None, key: None, parameter: None,
      },
      Mismatch::BodyMismatch { path, expected, actual, mismatch } => XmlMismatch {
        kind: "BodyMismatch".to_string(),
        description: mismatch.clone(),
        expected: expected.as_ref().map(|b| String::from_utf8_lossy(b).into_owned()),
        actual: actual.as_ref().map(|b| String::from_utf8_lossy(b).into_owned()),
        path: Some(path.clone()),
        key: None, parameter: None,
      },
      Mismatch::MetadataMismatch { key, expected, actual, mismatch } => XmlMismatch {
        kind: "MetadataMismatch".to_string(),
        description: mismatch.clone(),
        expected: Some(expected.clone()),
        actual: Some(actual.clone()),
        path: None,
        key: Some(key.clone()),
        parameter: None,
      },
    }
  }
}

pub(crate) fn to_xml_string(
  result: &VerificationExecutionResult,
  provider: &str
) -> anyhow::Result<String> {
  let report = XmlReport::from((provider, result));
  let mut buf = Vec::new();
  to_utf8_io_writer(&mut buf, &report)?;
  Ok(String::from_utf8(buf)?)
}

#[cfg(test)]
mod tests {
  use std::collections::HashMap;
  use std::time::Duration;

  use insta::assert_snapshot;
  use pact_matching::Mismatch;
  use pact_verifier::MismatchResult;
  use pact_verifier::verification_result::{
    VerificationExecutionResult, VerificationInteractionResult, VerificationMismatchResult,
  };

  use super::*;

  fn to_xml(result: &VerificationExecutionResult, provider: &str) -> String {
    let report = XmlReport::from((provider, result));
    let mut buf = Vec::new();
    to_utf8_io_writer(&mut buf, &report).unwrap();
    String::from_utf8(buf).unwrap()
  }

  #[test]
  fn empty_result() {
    assert_snapshot!(to_xml(&VerificationExecutionResult::new(), "My Provider"));
  }

  #[test]
  fn passing_result_with_interactions() {
    let result = VerificationExecutionResult {
      result: true,
      interaction_results: vec![
        VerificationInteractionResult {
          interaction_id: None,
          interaction_key: None,
          description: "request 1".to_string(),
          interaction_description: "GET /foo returns 200".to_string(),
          result: Ok(()),
          pending: false,
          duration: Duration::from_millis(42),
        },
        VerificationInteractionResult {
          interaction_id: Some("interaction-123".to_string()),
          interaction_key: Some("key-abc".to_string()),
          description: "request 2".to_string(),
          interaction_description: "POST /bar returns 201".to_string(),
          result: Ok(()),
          pending: false,
          duration: Duration::from_millis(15),
        },
      ],
      ..VerificationExecutionResult::default()
    };
    assert_snapshot!(to_xml(&result, "My Provider"));
  }

  #[test]
  fn failing_with_error_mismatch() {
    let result = VerificationExecutionResult {
      result: false,
      errors: vec![
        (
          "GET /foo returns 200".to_string(),
          VerificationMismatchResult::Error {
            error: "Connection refused".to_string(),
            interaction_id: Some("abc123".to_string()),
          },
        ),
      ],
      interaction_results: vec![
        VerificationInteractionResult {
          interaction_id: None,
          interaction_key: None,
          description: "request 1".to_string(),
          interaction_description: "GET /foo returns 200".to_string(),
          result: Err(MismatchResult::Error("Connection refused".to_string(), None)),
          pending: false,
          duration: Duration::from_millis(5),
        },
      ],
      ..VerificationExecutionResult::default()
    };
    assert_snapshot!(to_xml(&result, "My Provider"));
  }

  #[test]
  fn failing_with_body_mismatch() {
    let result = VerificationExecutionResult {
      result: false,
      errors: vec![
        (
          "GET /foo returns 200".to_string(),
          VerificationMismatchResult::Mismatches {
            mismatches: vec![
              Mismatch::StatusMismatch {
                expected: 200,
                actual: 404,
                mismatch: "Expected status 200 but was 404".to_string(),
              },
              Mismatch::BodyMismatch {
                path: "$.price".to_string(),
                expected: Some("100".into()),
                actual: Some("200".into()),
                mismatch: "Expected 100 but got 200".to_string(),
              },
            ],
            interaction_id: None,
          },
        ),
      ],
      ..VerificationExecutionResult::default()
    };
    assert_snapshot!(to_xml(&result, "My Provider"));
  }

  #[test]
  fn pending_errors() {
    let result = VerificationExecutionResult {
      result: true,
      pending_errors: vec![
        (
          "GET /pending-foo returns 200".to_string(),
          VerificationMismatchResult::Error {
            error: "Provider state setup failed".to_string(),
            interaction_id: None,
          },
        ),
      ],
      interaction_results: vec![
        VerificationInteractionResult {
          interaction_id: None,
          interaction_key: None,
          description: "pending request".to_string(),
          interaction_description: "GET /pending-foo returns 200".to_string(),
          result: Err(MismatchResult::Error("Provider state setup failed".to_string(), None)),
          pending: true,
          duration: Duration::from_millis(3),
        },
      ],
      ..VerificationExecutionResult::default()
    };
    assert_snapshot!(to_xml(&result, "My Provider"));
  }

  #[test]
  fn with_notices() {
    let mut notice = HashMap::new();
    notice.insert("text".to_string(), "This pact is being verified because it is the latest version".to_string());
    notice.insert("type".to_string(), "info".to_string());
    let result = VerificationExecutionResult {
      result: true,
      notices: vec![notice],
      ..VerificationExecutionResult::default()
    };
    assert_snapshot!(to_xml(&result, "My Provider"));
  }

  #[test]
  fn all_mismatch_types() {
    let result = VerificationExecutionResult {
      result: false,
      errors: vec![
        (
          "Complex interaction".to_string(),
          VerificationMismatchResult::Mismatches {
            mismatches: vec![
              Mismatch::MethodMismatch {
                expected: "GET".to_string(),
                actual: "POST".to_string(),
                mismatch: "Expected method GET but received POST".to_string(),
              },
              Mismatch::PathMismatch {
                expected: "/foo".to_string(),
                actual: "/bar".to_string(),
                mismatch: "Expected path /foo but received /bar".to_string(),
              },
              Mismatch::HeaderMismatch {
                key: "Content-Type".to_string(),
                expected: "application/json".to_string(),
                actual: "text/plain".to_string(),
                mismatch: "Expected header Content-Type=application/json but received text/plain".to_string(),
              },
              Mismatch::QueryMismatch {
                parameter: "page".to_string(),
                expected: "1".to_string(),
                actual: "2".to_string(),
                mismatch: "Expected query parameter page=1 but received page=2".to_string(),
              },
              Mismatch::BodyTypeMismatch {
                expected: "application/json".to_string(),
                actual: "text/plain".to_string(),
                mismatch: "Expected body content type application/json but received text/plain".to_string(),
                expected_body: None,
                actual_body: None,
              },
              Mismatch::MetadataMismatch {
                key: "contentType".to_string(),
                expected: "application/json".to_string(),
                actual: "text/xml".to_string(),
                mismatch: "Expected metadata contentType=application/json but received text/xml".to_string(),
              },
            ],
            interaction_id: Some("complex-123".to_string()),
          },
        ),
      ],
      ..VerificationExecutionResult::default()
    };
    assert_snapshot!(to_xml(&result, "My Provider"));
  }
}