From af93c029bfa75ba17dcfaafdd6e3bdad18bc111e Mon Sep 17 00:00:00 2001 From: Ronald Holshausen Date: Thu, 4 Jul 2024 11:29:35 +1000 Subject: [PATCH] fix: Allow core content types to be able to be ovveridden #1812 --- .../dius/pact/core/matchers/MatchingConfig.kt | 21 +++++++++++-------- .../core/matchers/MatchingConfigSpec.groovy | 21 ++++++++++++------- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/core/matchers/src/main/kotlin/au/com/dius/pact/core/matchers/MatchingConfig.kt b/core/matchers/src/main/kotlin/au/com/dius/pact/core/matchers/MatchingConfig.kt index 1b72807d12..2afff37e12 100644 --- a/core/matchers/src/main/kotlin/au/com/dius/pact/core/matchers/MatchingConfig.kt +++ b/core/matchers/src/main/kotlin/au/com/dius/pact/core/matchers/MatchingConfig.kt @@ -38,15 +38,18 @@ object MatchingConfig { } private fun coreContentMatcher(contentType: String): ContentMatcher? { - val matcher = coreBodyMatchers.entries.find { contentType.matches(Regex(it.key)) }?.value - return if (matcher != null) { - val clazz = Class.forName(matcher).kotlin - (clazz.objectInstance ?: clazz.createInstance()) as ContentMatcher? - } else { - when (System.getProperty("pact.content_type.override.$contentType")) { - "json" -> JsonContentMatcher - "text" -> PlainTextContentMatcher() - else -> null + return when (val override = System.getProperty("pact.content_type.override.$contentType")) { + "json" -> JsonContentMatcher + "text" -> PlainTextContentMatcher() + is String -> lookupContentMatcher(override) + else -> { + val matcher = coreBodyMatchers.entries.find { contentType.matches(Regex(it.key)) }?.value + if (matcher != null) { + val clazz = Class.forName(matcher).kotlin + (clazz.objectInstance ?: clazz.createInstance()) as ContentMatcher? + } else { + null + } } } } diff --git a/core/matchers/src/test/groovy/au/com/dius/pact/core/matchers/MatchingConfigSpec.groovy b/core/matchers/src/test/groovy/au/com/dius/pact/core/matchers/MatchingConfigSpec.groovy index 182b235118..b0c922c873 100644 --- a/core/matchers/src/test/groovy/au/com/dius/pact/core/matchers/MatchingConfigSpec.groovy +++ b/core/matchers/src/test/groovy/au/com/dius/pact/core/matchers/MatchingConfigSpec.groovy @@ -1,18 +1,11 @@ package au.com.dius.pact.core.matchers import spock.lang.Specification -import spock.lang.Unroll import spock.util.environment.RestoreSystemProperties @RestoreSystemProperties class MatchingConfigSpec extends Specification { - def setupSpec() { - System.setProperty('pact.content_type.override.application/x-thrift', 'json') - System.setProperty('pact.content_type.override.application/x-other', 'text') - } - - @Unroll def 'maps JSON content types to JSON body matcher'() { expect: MatchingConfig.lookupContentMatcher(contentType).class.name == matcherClass @@ -26,7 +19,21 @@ class MatchingConfigSpec extends Specification { 'application/stuff+xml' | 'au.com.dius.pact.core.matchers.XmlContentMatcher' 'application/json-rpc' | 'au.com.dius.pact.core.matchers.JsonContentMatcher' 'application/jsonrequest' | 'au.com.dius.pact.core.matchers.JsonContentMatcher' + } + + def 'allows content type matchers to be overridden'() { + given: + System.setProperty('pact.content_type.override.application/x-thrift', 'json') + System.setProperty('pact.content_type.override.application/x-other', 'text') + System.setProperty('pact.content_type.override.text/plain', 'application/xml') + + expect: + MatchingConfig.lookupContentMatcher(contentType).class.name == matcherClass + + where: + contentType | matcherClass 'application/x-thrift' | 'au.com.dius.pact.core.matchers.JsonContentMatcher' 'application/x-other' | 'au.com.dius.pact.core.matchers.PlainTextContentMatcher' + 'text/plain' | 'au.com.dius.pact.core.matchers.XmlContentMatcher' } }