From 2ae4215cfbe1924d2e16de8dd8908379c39bdce0 Mon Sep 17 00:00:00 2001 From: slam Date: Sun, 2 Jun 2024 21:58:20 +0800 Subject: [PATCH] test(ut): add ut for HttpClient.kt and refactor expect method httpClientEngine --- .../kotlin/com/tddworks/anthropic/di/Koin.kt | 5 ++- .../api/ktor/internal/HttpClient.apple.kt | 4 +-- .../network/api/ktor/internal/HttpClient.kt | 10 +++--- .../api/ktor/internal/HttpClient.jvm.kt | 4 +-- .../api/ktor/internal/HttpClientTest.kt | 33 +++++++++++++++++-- .../kotlin/com/tddworks/ollama/di/Koin.kt | 2 +- 6 files changed, 46 insertions(+), 12 deletions(-) diff --git a/anthropic-client/anthropic-client-core/src/commonMain/kotlin/com/tddworks/anthropic/di/Koin.kt b/anthropic-client/anthropic-client-core/src/commonMain/kotlin/com/tddworks/anthropic/di/Koin.kt index f2f6d40..267fdc6 100644 --- a/anthropic-client/anthropic-client-core/src/commonMain/kotlin/com/tddworks/anthropic/di/Koin.kt +++ b/anthropic-client/anthropic-client-core/src/commonMain/kotlin/com/tddworks/anthropic/di/Koin.kt @@ -18,7 +18,10 @@ import org.koin.dsl.module expect fun platformModule(): Module -fun iniAnthropic(config: AnthropicConfig, appDeclaration: KoinAppDeclaration = {}): Anthropic { +fun iniAnthropic( + config: AnthropicConfig, + appDeclaration: KoinAppDeclaration = {} +): Anthropic { return startKoin { appDeclaration() modules(commonModule(false) + anthropicModules(config)) diff --git a/common/src/appleMain/kotlin/com/tddworks/common/network/api/ktor/internal/HttpClient.apple.kt b/common/src/appleMain/kotlin/com/tddworks/common/network/api/ktor/internal/HttpClient.apple.kt index 0e3595d..94fc4bf 100644 --- a/common/src/appleMain/kotlin/com/tddworks/common/network/api/ktor/internal/HttpClient.apple.kt +++ b/common/src/appleMain/kotlin/com/tddworks/common/network/api/ktor/internal/HttpClient.apple.kt @@ -3,6 +3,6 @@ package com.tddworks.common.network.api.ktor.internal import io.ktor.client.engine.* import io.ktor.client.engine.darwin.* -internal actual fun httpClientEngine(): HttpClientEngineFactory { - return Darwin +internal actual fun httpClientEngine(): HttpClientEngine { + return Darwin.create() } \ No newline at end of file diff --git a/common/src/commonMain/kotlin/com/tddworks/common/network/api/ktor/internal/HttpClient.kt b/common/src/commonMain/kotlin/com/tddworks/common/network/api/ktor/internal/HttpClient.kt index 4696324..6500084 100644 --- a/common/src/commonMain/kotlin/com/tddworks/common/network/api/ktor/internal/HttpClient.kt +++ b/common/src/commonMain/kotlin/com/tddworks/common/network/api/ktor/internal/HttpClient.kt @@ -14,7 +14,7 @@ import kotlinx.serialization.json.Json import kotlin.time.Duration.Companion.seconds -internal expect fun httpClientEngine(): HttpClientEngineFactory +internal expect fun httpClientEngine(): HttpClientEngine /** * Creates a new [HttpClient] with [OkHttp] engine and [ContentNegotiation] plugin. @@ -30,9 +30,10 @@ fun createHttpClient( host: () -> String, port: () -> Int? = { null }, authToken: (() -> String)? = null, - json: Json, + json: Json = Json, + httpClientEngine: HttpClientEngine = httpClientEngine(), ): HttpClient { - return HttpClient(httpClientEngine()) { + return HttpClient(httpClientEngine) { // enable proxy in the future // engine { // proxy = ProxyBuilder.http(url) @@ -95,7 +96,8 @@ fun createHttpClient( defaultRequest { url { - this.protocol = protocol()?.let { URLProtocol.createOrDefault(it) } ?: URLProtocol.HTTPS + this.protocol = protocol()?.let { URLProtocol.createOrDefault(it) } + ?: URLProtocol.HTTPS this.host = host() port()?.let { this.port = it } } diff --git a/common/src/jvmMain/kotlin/com/tddworks/common/network/api/ktor/internal/HttpClient.jvm.kt b/common/src/jvmMain/kotlin/com/tddworks/common/network/api/ktor/internal/HttpClient.jvm.kt index 0d96a3a..c3d12f4 100644 --- a/common/src/jvmMain/kotlin/com/tddworks/common/network/api/ktor/internal/HttpClient.jvm.kt +++ b/common/src/jvmMain/kotlin/com/tddworks/common/network/api/ktor/internal/HttpClient.jvm.kt @@ -4,6 +4,6 @@ import io.ktor.client.engine.* import io.ktor.client.engine.okhttp.* -internal actual fun httpClientEngine(): HttpClientEngineFactory { - return OkHttp +internal actual fun httpClientEngine(): HttpClientEngine { + return OkHttp.create() } \ No newline at end of file diff --git a/common/src/jvmTest/kotlin/com/tddworks/common/network/api/ktor/internal/HttpClientTest.kt b/common/src/jvmTest/kotlin/com/tddworks/common/network/api/ktor/internal/HttpClientTest.kt index b4360cb..c7b7962 100644 --- a/common/src/jvmTest/kotlin/com/tddworks/common/network/api/ktor/internal/HttpClientTest.kt +++ b/common/src/jvmTest/kotlin/com/tddworks/common/network/api/ktor/internal/HttpClientTest.kt @@ -1,15 +1,44 @@ package com.tddworks.common.network.api.ktor.internal +import io.ktor.client.call.* +import io.ktor.client.engine.mock.* import io.ktor.client.engine.okhttp.* +import io.ktor.client.request.* +import io.ktor.http.* +import io.ktor.utils.io.* +import kotlinx.coroutines.runBlocking +import kotlinx.serialization.json.Json +import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Assertions.assertTrue import org.junit.jupiter.api.Test class HttpClientTest { + @Test + fun `should return correct json response with default settings`() { + runBlocking { + val mockEngine = MockEngine { request -> + respond( + content = ByteReadChannel("""{"ip":"127.0.0.1"}"""), + status = HttpStatusCode.OK, + headers = headersOf(HttpHeaders.ContentType, "application/json") + ) + } + val apiClient = createHttpClient( + host = { "some-host" }, + httpClientEngine = mockEngine + ) + + val body = apiClient.get("https://some-host:443").body() + assertEquals("""{"ip":"127.0.0.1"}""", body) + } + } + + @Test fun `should return OkHttp engine`() { - val engineFactory = httpClientEngine() - assertTrue(engineFactory is OkHttp) + val httpClientEngine = httpClientEngine() + assertTrue(httpClientEngine is OkHttpEngine) } } \ No newline at end of file diff --git a/ollama-client/ollama-client-core/src/commonMain/kotlin/com/tddworks/ollama/di/Koin.kt b/ollama-client/ollama-client-core/src/commonMain/kotlin/com/tddworks/ollama/di/Koin.kt index be71102..511d74c 100644 --- a/ollama-client/ollama-client-core/src/commonMain/kotlin/com/tddworks/ollama/di/Koin.kt +++ b/ollama-client/ollama-client-core/src/commonMain/kotlin/com/tddworks/ollama/di/Koin.kt @@ -40,8 +40,8 @@ fun ollamaModules( HttpRequester.default( createHttpClient( protocol = config.protocol, - port = config.port, host = config.baseUrl, + port = config.port, json = get(named("ollamaJson")), ) )