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 1b72807d1..2afff37e1 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 182b23511..b0c922c87 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' } }