Skip to content

Commit

Permalink
fix: Allow core content types to be able to be ovveridden #1812
Browse files Browse the repository at this point in the history
  • Loading branch information
rholshausen committed Jul 4, 2024
1 parent 029fcaf commit af93c02
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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'
}
}

0 comments on commit af93c02

Please sign in to comment.