Skip to content

Commit e6f8ae4

Browse files
committed
fix(plugins): Plugin config needs to have seperate values for HTTP request and response parts
1 parent 9cc38a2 commit e6f8ae4

File tree

2 files changed

+306
-31
lines changed

2 files changed

+306
-31
lines changed

rust/pact_matching/src/lib.rs

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,11 @@ use bytes::Bytes;
367367
use itertools::{Either, Itertools};
368368
use lazy_static::*;
369369
use maplit::{hashmap, hashset};
370+
#[cfg(feature = "plugins")] use pact_plugin_driver::catalogue_manager::find_content_matcher;
371+
#[cfg(feature = "plugins")] use pact_plugin_driver::plugin_models::PluginInteractionConfig;
372+
use serde_json::{json, Value};
373+
#[allow(unused_imports)] use tracing::{debug, error, info, instrument, trace, warn};
374+
370375
use pact_models::bodies::OptionalBody;
371376
use pact_models::content_types::ContentType;
372377
use pact_models::generators::{apply_generators, GenerateValue, GeneratorCategory, GeneratorTestMode, VariantMatcher};
@@ -380,18 +385,14 @@ use pact_models::path_exp::DocPath;
380385
use pact_models::v4::http_parts::{HttpRequest, HttpResponse};
381386
use pact_models::v4::message_parts::MessageContents;
382387
use pact_models::v4::sync_message::SynchronousMessage;
383-
#[cfg(feature = "plugins")] use pact_plugin_driver::catalogue_manager::find_content_matcher;
384-
#[cfg(feature = "plugins")] use pact_plugin_driver::plugin_models::PluginInteractionConfig;
385-
use serde::__private::from_utf8_lossy;
386-
use serde_json::{json, Value};
387-
#[allow(unused_imports)] use tracing::{debug, error, info, instrument, trace, warn};
388388

389-
use crate::generators::DefaultVariantMatcher;
390389
use crate::generators::bodies::generators_process_body;
390+
use crate::generators::DefaultVariantMatcher;
391391
use crate::headers::{match_header_value, match_headers};
392392
#[cfg(feature = "plugins")] use crate::json::match_json;
393393
use crate::matchers::*;
394394
use crate::matchingrules::DisplayForMismatch;
395+
#[cfg(feature = "plugins")] use crate::plugin_support::{InteractionPart, setup_plugin_config};
395396
use crate::query::match_query_maps;
396397

397398
/// Simple macro to convert a string slice to a `String` struct.
@@ -415,6 +416,7 @@ pub mod binary_utils;
415416
pub mod headers;
416417
pub mod query;
417418
pub mod form_urlencoded;
419+
#[cfg(feature = "plugins")] mod plugin_support;
418420

419421
#[cfg(not(feature = "plugins"))]
420422
#[derive(Clone, Debug, PartialEq)]
@@ -859,8 +861,8 @@ impl From<Mismatch> for CommonMismatch {
859861
},
860862
Mismatch::BodyMismatch { path, expected, actual, mismatch } => CommonMismatch {
861863
path: path.clone(),
862-
expected: from_utf8_lossy(expected.unwrap_or_default().as_ref()).to_string(),
863-
actual: from_utf8_lossy(actual.unwrap_or_default().as_ref()).to_string(),
864+
expected: String::from_utf8_lossy(expected.unwrap_or_default().as_ref()).to_string(),
865+
actual: String::from_utf8_lossy(actual.unwrap_or_default().as_ref()).to_string(),
864866
description: mismatch.clone()
865867
},
866868
Mismatch::MetadataMismatch { key, expected, actual, mismatch } => CommonMismatch {
@@ -1674,7 +1676,7 @@ pub async fn match_request<'a>(
16741676
#[allow(unused_mut, unused_assignments)] let mut plugin_data = hashmap!{};
16751677
#[cfg(feature = "plugins")]
16761678
{
1677-
plugin_data = setup_plugin_config(pact, interaction);
1679+
plugin_data = setup_plugin_config(pact, interaction, InteractionPart::Request);
16781680
};
16791681
trace!("plugin_data = {:?}", plugin_data);
16801682

@@ -1745,7 +1747,7 @@ pub async fn match_response<'a>(
17451747
#[allow(unused_mut, unused_assignments)] let mut plugin_data = hashmap!{};
17461748
#[cfg(feature = "plugins")]
17471749
{
1748-
plugin_data = setup_plugin_config(pact, interaction);
1750+
plugin_data = setup_plugin_config(pact, interaction, InteractionPart::Response);
17491751
};
17501752
trace!("plugin_data = {:?}", plugin_data);
17511753

@@ -1778,24 +1780,6 @@ pub async fn match_response<'a>(
17781780
mismatches
17791781
}
17801782

1781-
#[cfg(feature = "plugins")]
1782-
fn setup_plugin_config<'a>(
1783-
pact: &Box<dyn Pact + Send + Sync + RefUnwindSafe + 'a>,
1784-
interaction: &Box<dyn Interaction + Send + Sync + RefUnwindSafe>
1785-
) -> HashMap<String, PluginInteractionConfig> {
1786-
pact.plugin_data().iter().map(|data| {
1787-
let interaction_config = if let Some(v4_interaction) = interaction.as_v4() {
1788-
v4_interaction.plugin_config().get(&data.name).cloned().unwrap_or_default()
1789-
} else {
1790-
hashmap! {}
1791-
};
1792-
(data.name.clone(), PluginInteractionConfig {
1793-
pact_configuration: data.configuration.clone(),
1794-
interaction_configuration: interaction_config
1795-
})
1796-
}).collect()
1797-
}
1798-
17991783
/// Matches the actual message contents to the expected one. This takes into account the content type of each.
18001784
#[instrument(level = "trace")]
18011785
pub async fn match_message_contents(
@@ -1917,7 +1901,7 @@ pub async fn match_message<'a>(
19171901
#[allow(unused_mut, unused_assignments)] let mut plugin_data = hashmap!{};
19181902
#[cfg(feature = "plugins")]
19191903
{
1920-
plugin_data = setup_plugin_config(pact, expected);
1904+
plugin_data = setup_plugin_config(pact, expected, InteractionPart::None);
19211905
};
19221906

19231907
let body_context = if expected.is_v4() {
@@ -1976,7 +1960,7 @@ pub async fn match_sync_message_request<'a>(
19761960
#[allow(unused_mut, unused_assignments)] let mut plugin_data = hashmap!{};
19771961
#[cfg(feature = "plugins")]
19781962
{
1979-
plugin_data = setup_plugin_config(pact, &expected.boxed());
1963+
plugin_data = setup_plugin_config(pact, &expected.boxed(), InteractionPart::None);
19801964
};
19811965

19821966
let body_context = CoreMatchingContext {
@@ -2034,7 +2018,7 @@ pub async fn match_sync_message_response<'a>(
20342018
#[allow(unused_mut, unused_assignments)] let mut plugin_data = hashmap!{};
20352019
#[cfg(feature = "plugins")]
20362020
{
2037-
plugin_data = setup_plugin_config(pact, &expected.boxed());
2021+
plugin_data = setup_plugin_config(pact, &expected.boxed(), InteractionPart::None);
20382022
};
20392023
for (expected_response, actual_response) in expected_responses.iter().zip(actual_responses) {
20402024
let matching_rules = &expected_response.matching_rules;

0 commit comments

Comments
 (0)