Skip to content

Commit

Permalink
chore: add some content detection debug logs as failing on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronald Holshausen committed Jun 14, 2020
1 parent b8ee6cd commit 9b284c3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import au.com.dius.pact.core.model.ContentType.Companion.HTMLREGEXP
import au.com.dius.pact.core.model.ContentType.Companion.JSONREGEXP
import au.com.dius.pact.core.model.ContentType.Companion.XMLREGEXP
import au.com.dius.pact.core.model.ContentType.Companion.XMLREGEXP2
import mu.KLogging
import org.apache.tika.config.TikaConfig
import org.apache.tika.io.TikaInputStream
import org.apache.tika.metadata.Metadata
Expand All @@ -19,7 +20,9 @@ data class OptionalBody(

init {
if (contentType == ContentType.UNKNOWN) {
logger.debug { "Body content type is unknown, will try detect body contents" }
val detectedContentType = detectContentType()
logger.debug { "Detected content type = $detectedContentType" }
if (detectedContentType != null) {
this.contentType = detectedContentType
}
Expand Down Expand Up @@ -111,7 +114,9 @@ data class OptionalBody(
this.isPresent() -> {
val metadata = Metadata()
val mimetype = tika.detector.detect(TikaInputStream.get(value!!), metadata)
logger.debug { "Tika returned $mimetype" }
if (mimetype.baseType.type == "text") {
logger.debug { "Base type is text, will try match the contents" }
detectStandardTextContentType() ?: ContentType(mimetype)
} else {
ContentType(mimetype)
Expand All @@ -122,8 +127,9 @@ data class OptionalBody(

private fun detectStandardTextContentType(): ContentType? = when {
isPresent() -> {
val newLine = '\n'.toByte()
val s = value!!.take(32).map {
if (it == '\n'.toByte()) ' ' else it.toChar()
if (it == newLine) ' ' else it.toChar()
}.joinToString("")
when {
s.matches(XMLREGEXP) -> ContentType.XML
Expand All @@ -136,7 +142,7 @@ data class OptionalBody(
else -> null
}

companion object {
companion object : KLogging() {

@JvmStatic fun missing(): OptionalBody {
return OptionalBody(State.MISSING)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class OptionalBodySpec extends Specification {
OptionalBody.missing() | 'null'
OptionalBody.body(''.bytes, ContentType.UNKNOWN) | 'null'
OptionalBody.body('{}'.bytes, ContentType.UNKNOWN) | 'application/json'
bodyFromFile('/1070-ApiConsumer-ApiProvider.json') | 'application/json'
bodyFromFile('/v1-pact.json') | 'application/json'
bodyFromFile('/logback-test.xml') | 'application/xml'
bodyFromFile('/RAT.JPG') | 'image/jpeg'
}
Expand Down

0 comments on commit 9b284c3

Please sign in to comment.