Skip to content

Commit

Permalink
test: add http client function
Browse files Browse the repository at this point in the history
  • Loading branch information
waltkb committed Oct 10, 2024
1 parent 7c3900a commit 4ef8fcb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
7 changes: 6 additions & 1 deletion waltid-services/waltid-service-commons-test/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@ dependencies {

// Testing
api(kotlin("test"))
api("io.ktor:ktor-server-test-host:2.3.12")
api("io.ktor:ktor-server-test-host:${Versions.KTOR_VERSION}")
api("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.9.0")

implementation("io.ktor:ktor-client-okhttp-jvm:${Versions.KTOR_VERSION}")
implementation("io.ktor:ktor-client-content-negotiation:${Versions.KTOR_VERSION}")
implementation("io.ktor:ktor-serialization-kotlinx-json:${Versions.KTOR_VERSION}")
implementation("io.ktor:ktor-client-logging:${Versions.KTOR_VERSION}")
}

tasks.test {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ import id.walt.commons.ServiceInitialization
import id.walt.commons.ServiceMain
import id.walt.commons.featureflag.AbstractFeature
import id.walt.commons.featureflag.ServiceFeatureCatalog
import io.ktor.client.*
import io.ktor.client.engine.cio.*
import io.ktor.client.plugins.*
import io.ktor.client.plugins.contentnegotiation.*
import io.ktor.client.plugins.logging.*
import io.ktor.client.request.*
import io.ktor.http.*
import io.ktor.serialization.kotlinx.json.*
import io.ktor.server.application.*
import kotlinx.coroutines.test.runTest
import kotlin.time.Duration
Expand All @@ -26,6 +34,23 @@ object E2ETest {
val failed: Int,
)

fun testHttpClient(bearerToken: String? = null, doFollowRedirects: Boolean = true) = HttpClient(CIO) {
install(ContentNegotiation) {
json()
}
install(DefaultRequest) {
contentType(ContentType.Application.Json)
host = HOST
port = PORT

if (bearerToken != null) bearerAuth(bearerToken)
}
install(Logging) {
level = LogLevel.ALL
}
followRedirects = doFollowRedirects
}

var numTests = 0
val testResults = ArrayList<Result<Any?>>()
val testNames = HashMap<Int, String>()
Expand Down

0 comments on commit 4ef8fcb

Please sign in to comment.