Skip to content

Commit 9c2e416

Browse files
committed
test(KtorGithubLanguageColorApiTest): KtorGithubLanguageColorApi
1 parent 2d170c9 commit 9c2e416

File tree

2 files changed

+116
-3
lines changed

2 files changed

+116
-3
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package com.hoc081098.github_search_kmm.data.remote
2+
3+
import arrow.core.getOrHandle
4+
import com.hoc081098.github_search_kmm.TestAntilog
5+
import com.hoc081098.github_search_kmm.TestAppCoroutineDispatchers
6+
import com.hoc081098.github_search_kmm.domain.model.ArgbColor
7+
import io.github.aakira.napier.Napier
8+
import io.ktor.client.HttpClient
9+
import io.ktor.client.engine.mock.MockEngine
10+
import io.ktor.client.engine.mock.MockRequestHandler
11+
import io.ktor.client.engine.mock.respond
12+
import io.ktor.http.ContentType
13+
import io.ktor.http.HttpHeaders
14+
import io.ktor.http.HttpMethod
15+
import io.ktor.http.HttpStatusCode
16+
import io.ktor.http.Url
17+
import io.ktor.http.headersOf
18+
import kotlin.test.AfterTest
19+
import kotlin.test.BeforeTest
20+
import kotlin.test.Test
21+
import kotlin.test.assertEquals
22+
import kotlin.test.fail
23+
import kotlinx.coroutines.channels.Channel
24+
import kotlinx.coroutines.test.runTest
25+
26+
class KtorGithubLanguageColorApiTest {
27+
private lateinit var ktorGithubLanguageColorApi: KtorGithubLanguageColorApi
28+
private lateinit var httpClient: HttpClient
29+
private lateinit var handlerChannel: Channel<MockRequestHandler>
30+
31+
private val url = Url("https://127.0.0.1:8080")
32+
private val testAppCoroutineDispatchers = TestAppCoroutineDispatchers()
33+
private val json = createJson()
34+
private val antilog = TestAntilog()
35+
36+
@BeforeTest
37+
fun setup() {
38+
Napier.base(antilog)
39+
40+
handlerChannel = Channel(Channel.UNLIMITED)
41+
httpClient = createHttpClient(MockEngine, json) {
42+
addHandler { request ->
43+
handlerChannel.receive()(request)
44+
}
45+
}
46+
ktorGithubLanguageColorApi = KtorGithubLanguageColorApi(
47+
httpClient = httpClient,
48+
url = url,
49+
appCoroutineDispatchers = testAppCoroutineDispatchers
50+
)
51+
}
52+
53+
@AfterTest
54+
fun teardown() {
55+
httpClient.close()
56+
handlerChannel.close()
57+
Napier.takeLogarithm(antilog)
58+
}
59+
60+
@Test
61+
fun `getColors returns a Right WHEN httpClient returns a successful response and it is valid`() =
62+
runTest(testAppCoroutineDispatchers.testCoroutineDispatcher) {
63+
val colorsResponse = """
64+
|{
65+
| "ABAP CDS": {
66+
| "color": "#555e25",
67+
| "url": "https://github.com/trending?l=ABAP-CDS"
68+
| },
69+
| "Ada": {
70+
| "url": "https://github.com/trending?l=Ada"
71+
| },
72+
| "Agda": {
73+
| "color": "#315665",
74+
| "url": "https://github.com/trending?l=Agda"
75+
| }
76+
|}
77+
""".trimMargin("|")
78+
79+
handlerChannel.trySend { request ->
80+
when (request.url.encodedPath) {
81+
"" -> {
82+
check(request.method == HttpMethod.Get)
83+
84+
respond(
85+
content = colorsResponse,
86+
status = HttpStatusCode.OK,
87+
headers = headersOf(
88+
HttpHeaders.ContentType,
89+
ContentType.Application.Json.toString()
90+
)
91+
)
92+
}
93+
else -> error("Unhandled request ${request.url}")
94+
}
95+
}
96+
97+
val either = ktorGithubLanguageColorApi.getColors()
98+
val colors = either.getOrHandle { throw it }
99+
100+
assertEquals(
101+
mapOf(
102+
"ABAP CDS" to ArgbColor.parse("#555e25").getOrHandle { fail(it) },
103+
"Agda" to ArgbColor.parse("#315665").getOrHandle { fail(it) },
104+
),
105+
colors,
106+
)
107+
}
108+
}

shared/src/commonTest/kotlin/com/hoc081098/github_search_kmm/data/remote/KtorRepoItemApiTest.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ class KtorRepoItemApiTest {
6262
@Test
6363
fun `searchRepoItems returns a Right WHEN httpClient returns a successful response`() =
6464
runTest(testAppCoroutineDispatchers.testCoroutineDispatcher) {
65+
val responseString = readTextResource("search_repositories_response.json")
66+
6567
handlerChannel.trySend { request ->
6668
when (request.url.encodedPath) {
6769
"/search/repositories" -> {
@@ -70,9 +72,12 @@ class KtorRepoItemApiTest {
7072
checkNotNull(request.url.parameters["page"])
7173

7274
respond(
73-
content = readTextResource("search_repositories_response.json"),
75+
content = responseString,
7476
status = HttpStatusCode.OK,
75-
headers = headersOf(HttpHeaders.ContentType, ContentType.Text.Plain.toString())
77+
headers = headersOf(
78+
HttpHeaders.ContentType,
79+
ContentType.Application.Json.toString()
80+
)
7681
)
7782
}
7883
else -> error("Unhandled request ${request.url}")
@@ -85,7 +90,7 @@ class KtorRepoItemApiTest {
8590
)
8691

8792
assertEquals(
88-
json.decodeFromString(readTextResource("search_repositories_response.json")),
93+
json.decodeFromString(responseString),
8994
either.getOrHandle { throw it }
9095
)
9196
}

0 commit comments

Comments
 (0)