Skip to content

Commit

Permalink
refactor(ktor-internal): add uts and remove to correct package.
Browse files Browse the repository at this point in the history
  • Loading branch information
hanrw committed Aug 24, 2024
1 parent 906e8f3 commit 4342dd3
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.tddworks.common.network.api.ktor.internal

data class AuthConfig(
val authToken: (() -> String)? = null
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.tddworks.common.network.api.ktor.internal

import kotlinx.serialization.json.Json

data class ClientFeatures(
val json: Json = Json,
val queryParams: Map<String, String> = emptyMap(),
val expectSuccess: Boolean = true
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.tddworks.common.network.api.ktor.internal

import io.ktor.client.plugins.*
import io.ktor.http.*

interface ConnectionConfig {
fun setupUrl(builder: DefaultRequest.DefaultRequestBuilder) {
builder.setupUrl(this)
}
}

private fun DefaultRequest.DefaultRequestBuilder.setupUrl(connectionConfig: ConnectionConfig) {
when (connectionConfig) {
is HostPortConnectionConfig -> {
url {
protocol =
connectionConfig.protocol()?.let { URLProtocol.createOrDefault(it) }
?: URLProtocol.HTTPS
host = connectionConfig.host()
connectionConfig.port()?.let { port = it }
}
}

is UrlBasedConnectionConfig -> {
connectionConfig.baseUrl().let { url.takeFrom(it) }
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.tddworks.common.network.api.ktor.internal

data class HostPortConnectionConfig(
val protocol: () -> String? = { null },
val host: () -> String = { "" },
val port: () -> Int? = { null },
) : ConnectionConfig
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,11 @@ import io.ktor.client.request.*
import io.ktor.http.*
import io.ktor.serialization.kotlinx.*
import io.ktor.util.*
import kotlinx.serialization.json.Json


internal expect fun httpClientEngine(): HttpClientEngine


interface ConnectionConfig {
fun setupUrl(builder: DefaultRequest.DefaultRequestBuilder) {
builder.setupUrl(this)
}
}

data class UrlBasedConnectionConfig(
val baseUrl: () -> String = { "" }
) : ConnectionConfig

data class HostPortConnectionConfig(
val protocol: () -> String? = { null },
val host: () -> String = { "" },
val port: () -> Int? = { null },
) : ConnectionConfig

data class AuthConfig(
val authToken: (() -> String)? = null
)

data class ClientFeatures(
val json: Json = Json,
val queryParams: Map<String, String> = emptyMap(),
val expectSuccess: Boolean = true
)

fun createHttpClient(
connectionConfig: ConnectionConfig = UrlBasedConnectionConfig(),
authConfig: AuthConfig = AuthConfig(),
Expand Down Expand Up @@ -77,24 +50,6 @@ fun createHttpClient(
}
}

private fun DefaultRequest.DefaultRequestBuilder.setupUrl(connectionConfig: ConnectionConfig) {
when (connectionConfig) {
is HostPortConnectionConfig -> {
url {
protocol =
connectionConfig.protocol()?.let { URLProtocol.createOrDefault(it) }
?: URLProtocol.HTTPS
host = connectionConfig.host()
connectionConfig.port()?.let { port = it }
}
}

is UrlBasedConnectionConfig -> {
connectionConfig.baseUrl().let { url.takeFrom(it) }
}
}
}

private fun DefaultRequest.DefaultRequestBuilder.commonSettings(
queryParams: Map<String, String>,
authToken: (() -> String)?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,40 @@ import org.junit.jupiter.api.Test

class HttpClientTest {

@Test
fun `should return correct response with host and port based config`() = runTest {
val mockEngine = MockEngine { _ ->
respond(
private val mockEngine = MockEngine { request ->
when (request.url.toString()) {
"https://some-host" -> respond(
content = ByteReadChannel("""{"ip":"127.0.0.1"}"""),
status = HttpStatusCode.OK,
headers = headersOf(HttpHeaders.ContentType, "application/json")
)

else -> respond("", HttpStatusCode.NotFound)
}
}

@Test
fun `should return correct response with host and port based config with query parameters`() =
runTest {
val apiClient = createHttpClient(
connectionConfig = HostPortConnectionConfig(
protocol = { "https" },
host = { "some-host" },
port = { 443 }
),
httpClientEngine = mockEngine,
authConfig = AuthConfig(authToken = { "token" }),
features = ClientFeatures(
queryParams = mapOf("key" to "value"),
)
)

val body = apiClient.get("https://some-host").body<String>()
assertEquals("""{"ip":"127.0.0.1"}""", body)
}

@Test
fun `should return correct response with host and port based config`() = runTest {
val apiClient = createHttpClient(
connectionConfig = HostPortConnectionConfig(
protocol = { "https" },
Expand All @@ -37,13 +62,6 @@ class HttpClientTest {

@Test
fun `should return correct response with url based config`() = runTest {
val mockEngine = MockEngine { _ ->
respond(
content = ByteReadChannel("""{"ip":"127.0.0.1"}"""),
status = HttpStatusCode.OK,
headers = headersOf(HttpHeaders.ContentType, "application/json")
)
}
val apiClient = createHttpClient(
connectionConfig = UrlBasedConnectionConfig { "https://some-host" },
httpClientEngine = mockEngine
Expand All @@ -53,6 +71,14 @@ class HttpClientTest {
assertEquals("""{"ip":"127.0.0.1"}""", body)
}

@Test
fun `should return correct response with default`() = runTest {
val apiClient = createHttpClient(httpClientEngine = mockEngine)

val body = apiClient.get("https://some-host").body<String>()
assertEquals("""{"ip":"127.0.0.1"}""", body)
}

@Test
fun `should return OkHttp engine`() {
val httpClientEngine = httpClientEngine()
Expand Down

0 comments on commit 4342dd3

Please sign in to comment.