Skip to content

Commit

Permalink
fix: make the use of content type overrides consistent #1569
Browse files Browse the repository at this point in the history
  • Loading branch information
rholshausen committed Jun 16, 2022
1 parent 87a06c8 commit 0fa5b5e
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ object MatchingConfig {
@JvmStatic
fun lookupContentMatcher(contentType: String?): ContentMatcher? {
return if (contentType != null) {
val contentType1 = ContentType(contentType)
val contentMatcher = CatalogueManager.findContentMatcher(contentType1)
val ct = ContentType(contentType)
val contentMatcher = CatalogueManager.findContentMatcher(ct)
if (contentMatcher != null) {
if (!contentMatcher.isCore) {
PluginContentMatcher(contentMatcher, contentType1)
PluginContentMatcher(contentMatcher, ct)
} else {
coreContentMatcher(contentType)
}
Expand All @@ -44,7 +44,8 @@ object MatchingConfig {
val clazz = Class.forName(matcher).kotlin
(clazz.objectInstance ?: clazz.createInstance()) as ContentMatcher?
} else {
when (System.getProperty("pact.content_type.override.$contentType")) {
val ct = ContentType(contentType)
when (ct.override()) {
"json" -> JsonContentMatcher
"text" -> PlainTextContentMatcher()
else -> null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class MatchingConfigSpec extends Specification {

def setupSpec() {
System.setProperty('pact.content_type.override.application/x-thrift', 'json')
System.setProperty('pact.content_type.override.application.x-bob', 'json')
System.setProperty('pact.content_type.override.application/x-other', 'text')
}

Expand All @@ -27,6 +28,7 @@ class MatchingConfigSpec extends Specification {
'application/json-rpc' | 'au.com.dius.pact.core.matchers.JsonContentMatcher'
'application/jsonrequest' | 'au.com.dius.pact.core.matchers.JsonContentMatcher'
'application/x-thrift' | 'au.com.dius.pact.core.matchers.JsonContentMatcher'
'application/x-bob' | 'au.com.dius.pact.core.matchers.JsonBodyMatcher'
'application/x-other' | 'au.com.dius.pact.core.matchers.PlainTextContentMatcher'
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ContentType(val contentType: MediaType?) {

fun isJson(): Boolean {
return if (contentType != null) {
when (System.getProperty("pact.content_type.override.${contentType.baseType}")) {
when (overrideForContentType(contentType)) {
"json" -> true
else -> {
if ("vnd.schemaregistry.v1+json" == contentType.subtype)
Expand All @@ -33,7 +33,7 @@ class ContentType(val contentType: MediaType?) {
}

fun isXml(): Boolean = if (contentType != null) {
when (System.getProperty("pact.content_type.override.${contentType.baseType}")) {
when (overrideForContentType(contentType)) {
"xml" -> true
else -> xmlRegex.matches(contentType.subtype.toLowerCase())
}
Expand Down Expand Up @@ -81,8 +81,7 @@ class ContentType(val contentType: MediaType?) {
val superType = registry.getSupertype(contentType) ?: MediaType.OCTET_STREAM
val type = contentType.type
val baseType = superType.type
val override = System.getProperty("pact.content_type.override.$type.${contentType.subtype}")
?: System.getProperty("pact.content_type.override.$type/${contentType.subtype}")
val override = overrideForContentType(contentType)
when {
override.isNotEmpty() -> override == "binary"
type == "text" || baseType == "text" -> false
Expand Down Expand Up @@ -133,6 +132,12 @@ class ContentType(val contentType: MediaType?) {
}
}

/**
* If there is an override defined for the content type. Overrides are defined with a JVM system property
* in the format pact.content_type.override.<TYPE>.<SUBTYPE>=text|json|binary|...
*/
fun override() = overrideForContentType(this.contentType)

companion object : KLogging() {
@JvmStatic
fun fromString(contentType: String?) = if (contentType.isNullOrEmpty()) {
Expand Down Expand Up @@ -164,5 +169,18 @@ class ContentType(val contentType: MediaType?) {
val XML = ContentType("application/xml")
@JvmStatic
val KAFKA_SCHEMA_REGISTRY_JSON = ContentType("application/vnd.schemaregistry.v1+json")

/**
* If there is an override defined for the given content type. Overrides are defined with a JVM system property
* in the format pact.content_type.override.<TYPE>.<SUBTYPE>=text|json|binary|...
*/
fun overrideForContentType(contentType: MediaType?): String? {
return if (contentType != null) {
(System.getProperty("pact.content_type.override.${contentType.type}.${contentType.subtype}")
?: System.getProperty("pact.content_type.override.${contentType.type}/${contentType.subtype}"))
} else {
null
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ class ContentTypeSpec extends Specification {
System.setProperty('pact.content_type.override.application/x-other', 'text')
System.setProperty('pact.content_type.override.application/x-bin', 'binary')
System.setProperty('pact.content_type.override.application/x-ml', 'xml')
System.setProperty('pact.content_type.override.application.other', 'json')
System.setProperty('pact.content_type.override.application.other-bin', 'binary')
System.setProperty('pact.content_type.override.application.other-text', 'text')
System.setProperty('pact.content_type.override.application.other-xml', 'xml')
}

@Unroll
Expand All @@ -35,6 +39,7 @@ class ContentTypeSpec extends Specification {
'application/x-thrift' || true
'application/x-other' || false
'application/graphql' || true
'application/other' || true

contentType = new ContentType(value)
}
Expand Down Expand Up @@ -78,6 +83,7 @@ class ContentTypeSpec extends Specification {
'application/STUFF+XML' || true
'application/x-ml' || true
'application/x-thrift' || false
'application/other-xml' || true

contentType = new ContentType(value)
}
Expand Down Expand Up @@ -123,6 +129,8 @@ class ContentTypeSpec extends Specification {
'multipart/form-data' || true
'application/x-www-form-urlencoded' || false
'application/x-bin' || true
'application/other-bin' || true
'application/other' || false

contentType = new ContentType(value)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,20 @@ class HttpPartSpec extends Specification {
0 * decoder.decode(_)
result.valueAsString() == '{}'
}

@Issue('#1569')
@RestoreSystemProperties
def 'takes into account content type overrides with dot format'() {
given:
def json = new JsonValue.Object([body: new JsonValue.StringValue('{}'.chars)])
System.setProperty('pact.content_type.override.application.x-thrift', 'json')
def decoder = Mock(Base64.Decoder)

when:
def result = HttpPart.extractBody(json, ContentType.fromString('application/x-thrift'), decoder)

then:
0 * decoder.decode(_)
result.valueAsString() == '{}'
}
}

0 comments on commit 0fa5b5e

Please sign in to comment.