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 15, 2022
1 parent f4d8ebf commit bec44aa
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package au.com.dius.pact.core.matchers

import au.com.dius.pact.core.model.ContentType
import kotlin.reflect.full.createInstance

object MatchingConfig {
Expand All @@ -21,7 +22,8 @@ object MatchingConfig {
val clazz = Class.forName(matcher).kotlin
(clazz.objectInstance ?: clazz.createInstance()) as BodyMatcher?
} else {
when (System.getProperty("pact.content_type.override.$contentType")) {
val ct = ContentType(contentType)
when (ct.override()) {
"json" -> JsonBodyMatcher
"text" -> PlainTextBodyMatcher()
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.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'
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ 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 -> jsonRegex.matches(contentType.subtype.toLowerCase())
}
} else false
}

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 @@ -64,8 +64,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 @@ -103,6 +102,12 @@ class ContentType(val contentType: MediaType?) {
return contentType?.hashCode() ?: 0
}

/**
* 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 @@ -131,5 +136,18 @@ class ContentType(val contentType: MediaType?) {
val JSON = ContentType("application/json")
@JvmStatic
val XML = ContentType("application/xml")

/**
* 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 @@ -33,6 +37,7 @@ class ContentTypeSpec extends Specification {
'application/HAL+JSON' || true
'application/x-thrift' || true
'application/x-other' || false
'application/other' || true

contentType = new ContentType(value)
}
Expand All @@ -53,6 +58,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 @@ -98,6 +104,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 bec44aa

Please sign in to comment.