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 authored and uglyog committed Aug 2, 2022
1 parent 1f2cf91 commit 8dc11bd
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import kotlin.reflect.full.createInstance

object MatchingConfig {
val bodyMatchers = mapOf(
"application/vnd.schemaregistry.v1\\+json" to "au.com.dius.pact.core.matchers.KafkaJsonSchemaContentMatcher",
"application/.*xml" to "au.com.dius.pact.core.matchers.XmlBodyMatcher",
"text/xml" to "au.com.dius.pact.core.matchers.XmlBodyMatcher",
".*json.*" to "au.com.dius.pact.core.matchers.JsonBodyMatcher",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@ class MatchingConfigSpec extends Specification {
MatchingConfig.lookupBodyMatcher(contentType).class.name == matcherClass

where:
contentType | matcherClass
'application/json' | 'au.com.dius.pact.core.matchers.JsonBodyMatcher'
'application/xml' | 'au.com.dius.pact.core.matchers.XmlBodyMatcher'
'application/hal+json' | 'au.com.dius.pact.core.matchers.JsonBodyMatcher'
'application/thrift+json' | 'au.com.dius.pact.core.matchers.JsonBodyMatcher'
'application/stuff+xml' | 'au.com.dius.pact.core.matchers.XmlBodyMatcher'
'application/json-rpc' | 'au.com.dius.pact.core.matchers.JsonBodyMatcher'
'application/jsonrequest' | 'au.com.dius.pact.core.matchers.JsonBodyMatcher'
'application/x-thrift' | 'au.com.dius.pact.core.matchers.JsonBodyMatcher'
'application/x-bob' | 'au.com.dius.pact.core.matchers.JsonBodyMatcher'
'application/x-other' | 'au.com.dius.pact.core.matchers.PlainTextBodyMatcher'
contentType | matcherClass
'application/vnd.schemaregistry.v1+json' | 'au.com.dius.pact.core.matchers.KafkaJsonSchemaContentMatcher'
'application/json' | 'au.com.dius.pact.core.matchers.JsonBodyMatcher'
'application/xml' | 'au.com.dius.pact.core.matchers.XmlBodyMatcher'
'application/hal+json' | 'au.com.dius.pact.core.matchers.JsonBodyMatcher'
'application/thrift+json' | 'au.com.dius.pact.core.matchers.JsonBodyMatcher'
'application/stuff+xml' | 'au.com.dius.pact.core.matchers.XmlBodyMatcher'
'application/json-rpc' | 'au.com.dius.pact.core.matchers.JsonBodyMatcher'
'application/jsonrequest' | 'au.com.dius.pact.core.matchers.JsonBodyMatcher'
'application/x-thrift' | 'au.com.dius.pact.core.matchers.JsonBodyMatcher'
'application/x-bob' | 'au.com.dius.pact.core.matchers.JsonBodyMatcher'
'application/x-bob' | 'au.com.dius.pact.core.matchers.JsonBodyMatcher'
'application/x-other' | 'au.com.dius.pact.core.matchers.PlainTextBodyMatcher'
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,16 @@ class ContentType(val contentType: MediaType?) {
return if (contentType != null) {
when (overrideForContentType(contentType)) {
"json" -> true
else -> jsonRegex.matches(contentType.subtype.toLowerCase())
else -> {
if ("vnd.schemaregistry.v1+json" == contentType.subtype)
false
else if (jsonRegex.matches(contentType.subtype.toLowerCase())) {
true
} else {
val superType = registry.getSupertype(contentType)
superType != null && superType.type == "application" && superType.subtype == "json"
}
}
}
} else false
}
Expand All @@ -30,6 +39,13 @@ class ContentType(val contentType: MediaType?) {
}
} else false

fun isKafkaSchemaRegistryJson(): Boolean = if (contentType != null) {
when (System.getProperty("pact.content_type.override.${contentType.baseType}")) {
"kafkaSchemaRegistryJson" -> true
else -> contentType.subtype == "vnd.schemaregistry.v1+json"
}
} else false

fun isOctetStream(): Boolean = if (contentType != null)
contentType.baseType.toString() == "application/octet-stream"
else false
Expand Down Expand Up @@ -59,6 +75,7 @@ class ContentType(val contentType: MediaType?) {

fun getBaseType() = contentType?.baseType?.toString()

@Suppress("ComplexMethod")
fun isBinaryType(): Boolean {
return if (contentType != null) {
val superType = registry.getSupertype(contentType) ?: MediaType.OCTET_STREAM
Expand Down Expand Up @@ -102,6 +119,19 @@ class ContentType(val contentType: MediaType?) {
return contentType?.hashCode() ?: 0
}

fun getSupertype() : ContentType? {
return if (contentType != null && contentType.subtype.endsWith("+json")) {
JSON
} else {
val supertype = registry.getSupertype(contentType)
if (supertype != null) {
ContentType(supertype)
} else {
null
}
}
}

/**
* 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|...
Expand Down Expand Up @@ -136,6 +166,8 @@ class ContentType(val contentType: MediaType?) {
val JSON = ContentType("application/json")
@JvmStatic
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,41 @@ class ContentTypeSpec extends Specification {

where:

value || result
'' || false
'text/plain' || false
'application/pdf' || false
'application/json' || true
'application/hal+json' || true
'application/HAL+JSON' || true
'application/x-thrift' || true
'application/x-other' || false
'application/other' || true
value || result
'' || false
'text/plain' || false
'application/pdf' || false
'application/json' || true
'application/hal+json' || true
'application/HAL+JSON' || true
'application/vnd.schemaregistry.v1+json' || false
'application/x-thrift' || true
'application/x-other' || false
'application/graphql' || true
'application/other' || true

contentType = new ContentType(value)
}

@Unroll
def '"#value" is kafka schema registry -> #result'() {
expect:
result == contentType.kafkaSchemaRegistryJson

where:

value || result
'' || false
'text/plain' || false
'application/pdf' || false
'application/json' || false
'application/hal+json' || false
'application/HAL+JSON' || false
'application/vnd.schemaregistry.v1+json' || true
'application/x-thrift' || false
'application/x-other' || false
'application/graphql' || false
'application/xml' || false

contentType = new ContentType(value)
}
Expand Down Expand Up @@ -109,4 +134,32 @@ class ContentTypeSpec extends Specification {

contentType = new ContentType(value)
}

@Unroll
def '"#value" supertype -> #result'() {
expect:
contentType.supertype?.asString() == result

where:

value || result
'' || null
'text/plain' || 'application/octet-stream'
'application/pdf' || 'application/octet-stream'
'application/zip' || 'application/octet-stream'
'application/json' || 'application/javascript'
'application/hal+json' || 'application/json'
'application/HAL+JSON' || 'application/json'
'application/xml' || 'text/plain'
'application/atom+xml' || 'application/xml'
'application/octet-stream' || null
'image/jpeg' || 'application/octet-stream'
'video/H264' || 'application/octet-stream'
'audio/aac' || 'application/octet-stream'
'text/csv' || 'text/plain'
'multipart/form-data' || 'application/octet-stream'
'application/x-www-form-urlencoded' || 'text/plain'

contentType = new ContentType(value)
}
}

0 comments on commit 8dc11bd

Please sign in to comment.