11package io.rebble.cobble.domain.api
22
3+ import io.ktor.client.HttpClient
34import io.ktor.client.engine.mock.MockEngine
45import io.ktor.client.engine.mock.respond
6+ import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
57import io.ktor.http.HttpHeaders
68import io.ktor.http.HttpMethod
79import io.ktor.http.HttpStatusCode
810import io.ktor.http.headersOf
11+ import io.ktor.serialization.kotlinx.json.json
912import io.rebble.cobble.shared.api.AppstoreClient
1013import 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
1118import kotlin.test.Test
1219import 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