org.http4k.datastar.DatastarFragmentApprovalTest.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
The newest version!
package org.http4k.datastar
import com.github.underscore.Xml.formatXml
import org.http4k.core.Body
import org.http4k.core.ContentType
import org.http4k.core.Response
import org.http4k.core.Status.Companion.OK
import org.http4k.lens.datastarFragments
import org.http4k.testing.ApprovalSource
import org.http4k.testing.ContentTypeAwareApprovalTest
import org.http4k.testing.FileSystemApprovalSource
import org.http4k.testing.TestNamer
import org.opentest4j.AssertionFailedError
import java.io.File
/**
* Approval JUnit5 extension configured to compare prettified-HTML Datastar Fragment messages.
*/
class DatastarFragmentApprovalTest(
testNamer: TestNamer = TestNamer.ClassAndMethod,
approvalSource: ApprovalSource = FileSystemApprovalSource(File("src/test/resources"))
) : ContentTypeAwareApprovalTest(ContentType.TEXT_EVENT_STREAM, testNamer, approvalSource) {
override fun format(input: String): String = try {
val fragments = Body.datastarFragments().toLens()(Response(OK).body(input))
.flatMap { it.fragments.map { it.value } }
.joinToString("\n\n")
formatXml("$fragments").removePrefix("").removeSuffix("")
} catch (e: IllegalArgumentException) {
throw AssertionFailedError("Invalid HTML generated", "", input, e)
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy