- server config outsourced

- test integrated
This commit is contained in:
stefan
2025-04-18 15:15:42 +02:00
parent 44cef36d00
commit b999cb5832
7 changed files with 150 additions and 13 deletions
+2
View File
@@ -20,4 +20,6 @@ dependencies {
testImplementation(libs.kotlin.test.junit)
testImplementation(libs.junit.jupiter)
testImplementation(libs.jupiter.junit.jupiter)
implementation(libs.ktor.server.config.yaml)
testImplementation(libs.junit.junit.jupiter)
}
@@ -1,14 +1,12 @@
package at.mocode
import io.ktor.server.application.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
fun main() {
embeddedServer(Netty, port = SERVER_PORT, host = "0.0.0.0", module = Application::module)
.start(wait = true)
fun main(args: Array<String>) {
EngineMain.main(args)
}
fun Application.module() {
@@ -17,4 +15,4 @@ fun Application.module() {
call.respondText("Ktor: ${Greeting().greet()}")
}
}
}
}
@@ -0,0 +1,19 @@
# Grundkonfiguration für Ktor in YAML
ktor:
deployment:
# Der Port, auf dem der Server lauschen soll
port: 8080
# port: ${PORT:8080} # Alternative: Nutzt Env-Variable PORT, sonst 8080
# Optional für Entwicklung: Server bei Änderungen neu laden
# watch:
# - classes
# - resources
application:
# Hier wird Ktor gesagt, welche Funktion die Konfiguration enthält
# PASSE DEN PFAD AN, falls deine Application.kt oder module() anders heißt/liegt!
modules:
- at.mocode.ApplicationKt.module
# Wenn Application.kt direkt unter at.mocode liegt:
# - at.mocode.ApplicationKt.module
@@ -1,15 +1,27 @@
package at.mocode
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.server.testing.* // Wichtig für testApplication
import kotlin.test.* // Wichtig für assertEquals, assertTrue etc.
class ApplicationTest {
@Test
fun main() {
}
@Test
fun module() {
fun testRootRoute() = testApplication {
application {
module() // Ruft deine Konfigurationsfunktion auf
}
// Sendet eine GET-Anfrage an "/" innerhalb der Test-App
val response = client.get("/")
// Überprüfungen (Assertions)
assertEquals(HttpStatusCode.OK, response.status, "Status Code should be OK")
val content = response.bodyAsText() // Holt den HTML-Body als Text
assertTrue(content.contains("Ktor: Hello, Java 21.0.6!"), "Welcome message missing")
}
}