Skip to content

Commit

Permalink
test(ut): add ut for HttpClient.kt and refactor expect method httpCli…
Browse files Browse the repository at this point in the history
…entEngine
  • Loading branch information
hanrw committed Jun 2, 2024
1 parent 5f4fefb commit 2ae4215
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<HttpClientEngineConfig> {
return Darwin
internal actual fun httpClientEngine(): HttpClientEngine {
return Darwin.create()
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import kotlinx.serialization.json.Json
import kotlin.time.Duration.Companion.seconds


internal expect fun httpClientEngine(): HttpClientEngineFactory<HttpClientEngineConfig>
internal expect fun httpClientEngine(): HttpClientEngine

/**
* Creates a new [HttpClient] with [OkHttp] engine and [ContentNegotiation] plugin.
Expand All @@ -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)
Expand Down Expand Up @@ -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 }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import io.ktor.client.engine.*
import io.ktor.client.engine.okhttp.*


internal actual fun httpClientEngine(): HttpClientEngineFactory<HttpClientEngineConfig> {
return OkHttp
internal actual fun httpClientEngine(): HttpClientEngine {
return OkHttp.create()
}
Original file line number Diff line number Diff line change
@@ -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<String>()
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)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
)
)
Expand Down

0 comments on commit 2ae4215

Please sign in to comment.