Skip to content

Commit

Permalink
chore: Add additional logging to matchContentType
Browse files Browse the repository at this point in the history
  • Loading branch information
rholshausen committed Aug 10, 2023
1 parent 8fd8d50 commit 73c6adb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import io.pact.plugins.jvm.core.CatalogueEntry
import io.pact.plugins.jvm.core.CatalogueEntryProviderType
import io.pact.plugins.jvm.core.CatalogueEntryType
import io.github.oshai.kotlinlogging.KotlinLogging
import org.apache.commons.codec.binary.Hex
import org.apache.commons.lang3.time.DateUtils
import org.apache.tika.config.TikaConfig
import org.apache.tika.io.TikaInputStream
Expand Down Expand Up @@ -161,8 +162,7 @@ fun <M : Mismatch> domatch(
is MaxEqualsIgnoreOrderMatcher -> matchMaxEqualsIgnoreOrder(matcher.max, path, expected, actual, mismatchFn)
is MinMaxEqualsIgnoreOrderMatcher -> matchMinEqualsIgnoreOrder(matcher.min, path, expected, actual, mismatchFn) +
matchMaxEqualsIgnoreOrder(matcher.max, path, expected, actual, mismatchFn)
is ContentTypeMatcher ->
matchHeaderWithParameters(path, ContentType.fromString(matcher.contentType), actual, mismatchFn)
is ContentTypeMatcher -> matchContentType(path, ContentType.fromString(matcher.contentType), actual, mismatchFn)
is ArrayContainsMatcher, is EachKeyMatcher, is EachValueMatcher, is ValuesMatcher -> listOf()
is BooleanMatcher -> matchBoolean(path, expected, actual, mismatchFn)
is StatusCodeMatcher ->
Expand Down Expand Up @@ -691,7 +691,7 @@ fun <M : Mismatch> matchNull(path: List<String>, actual: Any?, mismatchFactory:

private val tika = TikaConfig()

fun <M : Mismatch> matchHeaderWithParameters(
fun <M : Mismatch> matchContentType(
path: List<String>,
contentType: ContentType,
actual: Any?,
Expand All @@ -701,6 +701,14 @@ fun <M : Mismatch> matchHeaderWithParameters(
is ByteArray -> actual
else -> actual.toString().toByteArray(contentType.asCharset())
}

val slice = if (binaryData.size > 16) {
binaryData.copyOf(16)
} else {
binaryData
}
logger.debug { "matchContentType: $path, $contentType, ${binaryData.size} bytes starting with ${Hex.encodeHexString(slice)}...)" }

val metadata = Metadata()
val stream = TikaInputStream.get(binaryData)
var detectedContentType = stream.use { stream ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ContentTypeMatcherSpec extends Specification {
}] as MismatchFactory

when:
def result = MatcherExecutorKt.matchHeaderWithParameters(path, contentType, actual, mismatchFactory)
def result = MatcherExecutorKt.matchContentType(path, contentType, actual, mismatchFactory)

then:
result.empty
Expand All @@ -30,7 +30,7 @@ class ContentTypeMatcherSpec extends Specification {
}] as MismatchFactory

when:
def result = MatcherExecutorKt.matchHeaderWithParameters(path, contentType, actual, mismatchFactory)
def result = MatcherExecutorKt.matchContentType(path, contentType, actual, mismatchFactory)

then:
!result.empty
Expand All @@ -49,7 +49,7 @@ class ContentTypeMatcherSpec extends Specification {
}] as MismatchFactory

when:
def result = MatcherExecutorKt.matchHeaderWithParameters(path, contentType, actual, mismatchFactory)
def result = MatcherExecutorKt.matchContentType(path, contentType, actual, mismatchFactory)

then:
result.empty
Expand Down

0 comments on commit 73c6adb

Please sign in to comment.