Hello everyone :wave: I've been a user of http4k ...
# http4k
a
Hello everyone 👋 I've been a user of http4k for several years and recently discovered a feature that significantly simplifies my workflow. It revolves around approval tests. Traditionally, I wrapped the content I needed to approve in a
Response
object, as defined by this interface: Approver.kt. However, upon delving deeper into the code, I noticed some extension functions that aren't part of the interface but allow for direct content approval: Approver.assertApproved. This discovery enabled me to streamline my code from:
Copy code
kotlin
@ExtendWith(YamlApprovalTest::class)
interface YamlMetadataConfigTest {

    val config: YamlConfig

    @Test
    fun `generate metadata`(approver: Approver) {
        approver.assertYamlApproved(YamlBuilder.fromConfig(config))
    }
}

fun Approver.assertYamlApproved(input: String) = Response(OK)
    .body(input)
    .contentType(APPLICATION_YAML)
    .let(::assertApproved)
To a more concise version:
Copy code
kotlin
import org.http4k.testing.assertApproved

@ExtendWith(YamlApprovalTest::class)
interface YamlMetadataConfigTest {

    val config: YamlConfig

    @Test
    fun `generate metadata`(approver: Approver) {
        approver.assertApproved(YamlBuilder.fromConfig(config), APPLICATION_YAML)
    }
}
By simply importing the correct function
import org.http4k.testing.assertApproved
. I've checked the example repositories: - http4k examples - http4k by example but found no examples demonstrating this functionality. Given its utility, I believe this feature deserves more visibility. What are your thoughts on this? If it's welcome, I'd be happy to contribute towards enhancing its documentation or examples.
👍 2
a
Yeah, the approval testing has traditionally been for responses, but it can be very useful for other things too. Definitely worth updating the module readme with a new example.
🙏 1