Move tests and create a command to run all tests

This commit is contained in:
2021-01-15 02:40:41 +01:00
parent 7c106f7cf8
commit 459397f8e7
40 changed files with 160 additions and 66 deletions

View File

@@ -0,0 +1,51 @@
package steps
import io.ktor.application.*
import io.ktor.server.testing.*
import java.util.concurrent.TimeUnit
import kotlin.test.fail
class KtorServerContext(useByDefault: Boolean = true, val module: Application.() -> Unit) {
init { if (useByDefault) setDefault() }
companion object {
lateinit var defaultServer: KtorServerContext
}
private val engine = TestApplicationEngine(createTestEnvironment())
private data class RequestSetup(val setup: TestApplicationRequest.() -> Unit, val keepSetup: Boolean = true)
private val preRequestSetup = mutableListOf<RequestSetup>()
var call: TestApplicationCall? = null
fun addPreRequestSetup(keepSetup: Boolean = true, hook: TestApplicationRequest.() -> Unit) {
preRequestSetup.add(RequestSetup(hook, keepSetup))
}
fun handleRequest(setup: TestApplicationRequest.() -> Unit) =
try {
call = engine.handleRequest {
preRequestSetup.forEach { it.setup(this) }
setup(this)
}
} catch (e: Throwable) {
fail("Request fail, $e")
} finally {
preRequestSetup.removeAll { !it.keepSetup }
}
fun setDefault() {
defaultServer = this
}
fun start() {
engine.start()
module(engine.application)
}
fun stop() {
engine.stop(0L, 0L, TimeUnit.MILLISECONDS)
}
}