Skip to content

Commit af93c02

Browse files
committed
fix: Allow core content types to be able to be ovveridden #1812
1 parent 029fcaf commit af93c02

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

core/matchers/src/main/kotlin/au/com/dius/pact/core/matchers/MatchingConfig.kt

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,18 @@ object MatchingConfig {
3838
}
3939

4040
private fun coreContentMatcher(contentType: String): ContentMatcher? {
41-
val matcher = coreBodyMatchers.entries.find { contentType.matches(Regex(it.key)) }?.value
42-
return if (matcher != null) {
43-
val clazz = Class.forName(matcher).kotlin
44-
(clazz.objectInstance ?: clazz.createInstance()) as ContentMatcher?
45-
} else {
46-
when (System.getProperty("pact.content_type.override.$contentType")) {
47-
"json" -> JsonContentMatcher
48-
"text" -> PlainTextContentMatcher()
49-
else -> null
41+
return when (val override = System.getProperty("pact.content_type.override.$contentType")) {
42+
"json" -> JsonContentMatcher
43+
"text" -> PlainTextContentMatcher()
44+
is String -> lookupContentMatcher(override)
45+
else -> {
46+
val matcher = coreBodyMatchers.entries.find { contentType.matches(Regex(it.key)) }?.value
47+
if (matcher != null) {
48+
val clazz = Class.forName(matcher).kotlin
49+
(clazz.objectInstance ?: clazz.createInstance()) as ContentMatcher?
50+
} else {
51+
null
52+
}
5053
}
5154
}
5255
}
Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
11
package au.com.dius.pact.core.matchers
22

33
import spock.lang.Specification
4-
import spock.lang.Unroll
54
import spock.util.environment.RestoreSystemProperties
65

76
@RestoreSystemProperties
87
class MatchingConfigSpec extends Specification {
98

10-
def setupSpec() {
11-
System.setProperty('pact.content_type.override.application/x-thrift', 'json')
12-
System.setProperty('pact.content_type.override.application/x-other', 'text')
13-
}
14-
15-
@Unroll
169
def 'maps JSON content types to JSON body matcher'() {
1710
expect:
1811
MatchingConfig.lookupContentMatcher(contentType).class.name == matcherClass
@@ -26,7 +19,21 @@ class MatchingConfigSpec extends Specification {
2619
'application/stuff+xml' | 'au.com.dius.pact.core.matchers.XmlContentMatcher'
2720
'application/json-rpc' | 'au.com.dius.pact.core.matchers.JsonContentMatcher'
2821
'application/jsonrequest' | 'au.com.dius.pact.core.matchers.JsonContentMatcher'
22+
}
23+
24+
def 'allows content type matchers to be overridden'() {
25+
given:
26+
System.setProperty('pact.content_type.override.application/x-thrift', 'json')
27+
System.setProperty('pact.content_type.override.application/x-other', 'text')
28+
System.setProperty('pact.content_type.override.text/plain', 'application/xml')
29+
30+
expect:
31+
MatchingConfig.lookupContentMatcher(contentType).class.name == matcherClass
32+
33+
where:
34+
contentType | matcherClass
2935
'application/x-thrift' | 'au.com.dius.pact.core.matchers.JsonContentMatcher'
3036
'application/x-other' | 'au.com.dius.pact.core.matchers.PlainTextContentMatcher'
37+
'text/plain' | 'au.com.dius.pact.core.matchers.XmlContentMatcher'
3138
}
3239
}

0 commit comments

Comments
 (0)