Skip to content

Commit fb418ae

Browse files
authored
Merge pull request #319 from WhosNickDoglio/ndoglio/fix-test
Fix broken AppstoreClientTest test
2 parents 0a67b85 + 4fe42e3 commit fb418ae

File tree

3 files changed

+31
-13
lines changed

3 files changed

+31
-13
lines changed

android/gradle/libs.versions.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ androidx-datastore-preferences = { module = "androidx.datastore:datastore-prefer
7777
koin-core = { module = "io.insert-koin:koin-core", version.ref = "koinVersion" }
7878
koin-android = { module = "io.insert-koin:koin-android", version.ref = "koinVersion" }
7979
koin-compose = { module = "io.insert-koin:koin-compose", version.ref = "koinVersion" }
80+
koin-test = { module = "io.insert-koin:koin-test", version.ref = "koinVersion" }
8081
kotlin-reflect = { module = "org.jetbrains.kotlin:kotlin-reflect", version.ref = "kotlin" }
8182
kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "coroutinesVersion" }
8283
kotlinx-coroutines-debug = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-debug", version.ref = "coroutinesVersion" }

android/shared/build.gradle.kts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ kotlin {
8787
commonTest.dependencies {
8888
implementation(kotlin("test"))
8989
implementation(libs.ktor.client.mock)
90+
implementation(libs.koin.test)
9091
}
9192
}
9293
room {
@@ -111,4 +112,4 @@ dependencies {
111112
implementation(libs.androidx.security.crypto.ktx)
112113
add("kspCommonMainMetadata", libs.androidx.room.compiler)
113114
add("kspAndroid", libs.androidx.room.compiler)
114-
}
115+
}

android/shared/src/commonTest/kotlin/io/rebble/cobble/domain/api/AppstoreClientTest.kt

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
package io.rebble.cobble.domain.api
22

3+
import io.ktor.client.HttpClient
34
import io.ktor.client.engine.mock.MockEngine
45
import io.ktor.client.engine.mock.respond
6+
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
57
import io.ktor.http.HttpHeaders
68
import io.ktor.http.HttpMethod
79
import io.ktor.http.HttpStatusCode
810
import io.ktor.http.headersOf
11+
import io.ktor.serialization.kotlinx.json.json
912
import io.rebble.cobble.shared.api.AppstoreClient
1013
import io.rebble.libpebblecommon.util.runBlocking
14+
import org.koin.core.context.startKoin
15+
import org.koin.core.context.stopKoin
16+
import org.koin.dsl.module
17+
import org.koin.test.KoinTest
1118
import kotlin.test.Test
1219
import kotlin.test.assertEquals
1320

14-
class AppstoreClientTest {
21+
class AppstoreClientTest : KoinTest {
22+
1523
@Test
1624
fun `Get locker data`() = runBlocking {
1725
val data = """
@@ -1422,29 +1430,37 @@ class AppstoreClientTest {
14221430
when (request.url.encodedPath) {
14231431
"/api/v1/locker" -> {
14241432
if (request.headers[HttpHeaders.Authorization] != "Bearer x") {
1425-
respond(
1426-
"Unauthorized",
1427-
status = HttpStatusCode.Unauthorized
1428-
)
1433+
respond("Unauthorized", status = HttpStatusCode.Unauthorized)
14291434
}
14301435
val response = when (request.method) {
14311436
HttpMethod.Get -> {
1432-
respond(
1433-
data,
1434-
headers = headersOf(
1435-
HttpHeaders.ContentType, "application/json"
1436-
)
1437-
)
1437+
respond(data, headers = headersOf(HttpHeaders.ContentType, "application/json"))
14381438
}
1439+
14391440
else -> error("Unsupported method")
14401441
}
14411442
response
14421443
}
1444+
14431445
else -> error("Unhandled ${request.url.encodedPath}")
14441446
}
14451447
}
1446-
val client = AppstoreClient("https://appstore-api.rebble.io/api", "x", mockEngine)
1448+
1449+
startKoin {
1450+
modules(module {
1451+
factory {
1452+
HttpClient(engine = mockEngine) {
1453+
install(ContentNegotiation) {
1454+
json()
1455+
}
1456+
}
1457+
}
1458+
})
1459+
}
1460+
1461+
val client = AppstoreClient("https://appstore-api.rebble.io/api", "x")
14471462
val locker = client.getLocker()
14481463
assertEquals(13, locker.size)
1464+
stopKoin()
14491465
}
14501466
}

0 commit comments

Comments
 (0)