org.http4k.testing.ApprovalContent.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of http4k-testing-approval Show documentation
Show all versions of http4k-testing-approval Show documentation
Http4k support for Approval Testing
package org.http4k.testing
import org.http4k.core.HttpMessage
import java.io.InputStream
/**
* Determines which parts of the HttpMessage will be compared.
*/
interface ApprovalContent {
operator fun invoke(input: InputStream): InputStream
operator fun invoke(input: HttpMessage): InputStream
companion object {
fun EntireHttpMessage() = object : ApprovalContent {
override fun invoke(input: InputStream) = input
override fun invoke(input: HttpMessage) = input.toString().byteInputStream()
}
fun HttpTextBody(formatter: (String) -> String = { it }) = object : ApprovalContent {
override fun invoke(input: InputStream) = input
override fun invoke(input: HttpMessage) = formatter(input.bodyString()).byteInputStream()
}
fun HttpBinaryBody() = object : ApprovalContent {
override fun invoke(input: InputStream): InputStream = input
override fun invoke(input: HttpMessage): InputStream = invoke(input.body.stream)
}
fun HttpTextMessage(formatBody: (String) -> String) = object : ApprovalContent {
override fun invoke(input: InputStream) = input
override fun invoke(input: HttpMessage) =
input.body(formatBody(input.bodyString())).toString().byteInputStream()
}
@Deprecated("Use HttpTextBody", ReplaceWith("HttpTextBody()"))
fun HttpBodyOnly(formatter: (String) -> String = { it }) = HttpTextBody(formatter)
}
}