org.http4k.hamkrest.httpMessage.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of http4k-testing-hamkrest Show documentation
Show all versions of http4k-testing-hamkrest Show documentation
A set of Hamkrest matchers for common http4k types
package org.http4k.hamkrest
import com.natpryce.hamkrest.MatchResult
import com.natpryce.hamkrest.Matcher
import com.natpryce.hamkrest.anything
import com.natpryce.hamkrest.equalTo
import com.natpryce.hamkrest.matches
import com.natpryce.hamkrest.present
import org.http4k.core.Body
import org.http4k.core.ContentType
import org.http4k.core.HttpMessage
import org.http4k.format.Json
import org.http4k.lens.BodyLens
import org.http4k.lens.Header
import org.http4k.lens.HeaderLens
internal fun httpMessageHas(name: String, feature: (T) -> R, featureMatcher: Matcher): Matcher = object : Matcher {
override fun invoke(actual: T) =
featureMatcher(feature(actual)).let {
when (it) {
is MatchResult.Mismatch -> MatchResult.Mismatch("had $name that ${it.description}\nin: $actual")
else -> it
}
}
override val description = "has $name that ${featureMatcher.description}"
override val negatedDescription = "does not have $name that ${featureMatcher.description}"
}
fun hasHeader(lens: HeaderLens, matcher: Matcher): Matcher = LensMatcher(httpMessageHas("Header '${lens.meta.name}'", { req: HttpMessage -> lens(req) }, matcher))
@JvmName("hasBodyNullableString")
fun hasHeader(name: String, matcher: Matcher): Matcher = httpMessageHas("Header '$name'", { m: HttpMessage -> m.header(name) }, matcher)
fun hasHeader(name: String, matcher: Matcher): Matcher = httpMessageHas("Header '$name'", { m: HttpMessage -> m.header(name) }, present(matcher))
fun hasHeader(name: String, expected: CharSequence): Matcher = hasHeader(name, equalTo(expected))
fun hasHeader(name: String, expected: Regex): Matcher = hasHeader(name, present(matches(expected)))
fun hasHeader(name: String): Matcher = httpMessageHas("Header '$name'", { m: HttpMessage -> m.header(name) }, present(anything))
fun hasHeader(name: String, expected: List): Matcher = httpMessageHas("Header '$name'", { m: HttpMessage -> m.headerValues(name) }, equalTo(expected))
fun hasContentType(expected: ContentType): Matcher = httpMessageHas("Content-Type", { m: HttpMessage -> Header.CONTENT_TYPE(m) }, equalTo(expected))
fun hasBody(expected: Matcher): Matcher = httpMessageHas("Body", { m: HttpMessage -> m.body }, expected)
@JvmName("hasBodyNullableString")
fun hasBody(expected: Matcher): Matcher = httpMessageHas("Body", { m: HttpMessage -> m.bodyString() }, expected)
@JvmName("hasBodyString")
fun hasBody(expected: Matcher): Matcher = httpMessageHas("Body", { m: HttpMessage -> m.bodyString() }, expected)
fun hasBody(expected: CharSequence): Matcher = hasBody(equalTo(expected))
fun hasBody(expected: Regex): Matcher = hasBody(present(matches(expected)))
fun hasBody(lens: BodyLens, matcher: Matcher): Matcher = LensMatcher(httpMessageHas("Body", { m: HttpMessage -> lens(m) }, matcher))
fun Json.hasBody(expected: NODE): Matcher = httpMessageHas("Body", { m: HttpMessage -> parse(m.bodyString()) }, equalTo(expected))
fun Json.hasBody(expected: Matcher): Matcher = httpMessageHas("Body", { m: HttpMessage -> parse(m.bodyString()) }, expected)
fun Json.hasBody(expected: String): Matcher = httpMessageHas("Body", { m: HttpMessage -> compactify(m.bodyString()) }, equalTo(compactify(expected)))
© 2015 - 2025 Weber Informatics LLC | Privacy Policy