-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
01f4bf9
commit 64eed54
Showing
6 changed files
with
211 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
kovibes/src/androidUnitTest/kotlin/io/github/rubenquadros/kovibes/api/test/KtorTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package io.github.rubenquadros.kovibes.api.test | ||
|
||
import io.github.rubenquadros.kovibes.api.getKtorEngine | ||
import io.github.rubenquadros.kovibes.api.getKtorLogger | ||
import io.ktor.client.engine.android.AndroidClientEngine | ||
import io.ktor.client.plugins.logging.MessageLengthLimitingLogger | ||
import kotlin.test.Test | ||
import kotlin.test.assertTrue | ||
|
||
class KtorTest { | ||
@Test | ||
fun `ktor engine is provided`() { | ||
val engine = getKtorEngine() | ||
|
||
assertTrue { engine is AndroidClientEngine } | ||
} | ||
|
||
@Test | ||
fun `logger is provided`() { | ||
val logger = getKtorLogger() | ||
|
||
assertTrue { logger is MessageLengthLimitingLogger } | ||
} | ||
} |
145 changes: 145 additions & 0 deletions
145
...bes/src/commonTest/kotlin/io/github/rubenquadros/kovibes/api/test/ktor/KtorServiceTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
package io.github.rubenquadros.kovibes.api.test.ktor | ||
|
||
import io.github.rubenquadros.kovibes.api.AuthStorage | ||
import io.github.rubenquadros.kovibes.api.KtorService | ||
import io.github.rubenquadros.kovibes.api.config.logger.LogLevel | ||
import io.github.rubenquadros.kovibes.api.test.MockKtorService | ||
import io.github.rubenquadros.kovibes.api.test.MockResponse | ||
import io.github.rubenquadros.kovibes.api.test.errorResponsePath | ||
import io.github.rubenquadros.kovibes.api.test.getExpectedResponse | ||
import io.ktor.client.engine.HttpClientEngine | ||
import io.ktor.client.engine.mock.MockEngine | ||
import io.ktor.client.engine.mock.respond | ||
import io.ktor.client.plugins.logging.Logger | ||
import io.ktor.client.plugins.logging.SIMPLE | ||
import io.ktor.client.request.get | ||
import io.ktor.http.Headers | ||
import io.ktor.http.HttpStatusCode | ||
import io.ktor.http.path | ||
import io.ktor.utils.io.ByteReadChannel | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.Test | ||
import kotlin.test.assertTrue | ||
|
||
class KtorServiceTest { | ||
|
||
private var shouldThrowException = false | ||
|
||
private var shouldThrowAuthError = false | ||
|
||
private val authStorage = AuthStorage() | ||
|
||
private val ktorService = KtorService( | ||
authStorage = authStorage, | ||
logLevel = { LogLevel.NONE }, | ||
ktorEngine = { getKtorEngine() }, | ||
ktorLogger = { Logger.SIMPLE } | ||
) | ||
|
||
@Test | ||
fun `when authorization fails the auth token is refreshed`() = runTest { | ||
MockKtorService.isSuccess = false | ||
ktorService.client.get { | ||
url { | ||
path("browse/categories") | ||
} | ||
} | ||
|
||
assertTrue { authStorage.getAccessToken() == "token123" } | ||
} | ||
|
||
@Test | ||
fun `when token refresh fails then the auth token is not refreshed`() = runTest { | ||
MockKtorService.isSuccess = false | ||
shouldThrowAuthError = false | ||
shouldThrowException = true | ||
|
||
ktorService.client.get { | ||
url { | ||
path("browse/categories") | ||
} | ||
} | ||
|
||
assertTrue { authStorage.getAccessToken().isEmpty() } | ||
} | ||
|
||
@Test | ||
fun `when token refresh fails then auth token is not refreshed`() = runTest { | ||
MockKtorService.isSuccess = false | ||
shouldThrowException = false | ||
shouldThrowAuthError = true | ||
|
||
ktorService.client.get { | ||
url { | ||
path("browse/categories") | ||
} | ||
} | ||
|
||
assertTrue { authStorage.getAccessToken().isEmpty() } | ||
} | ||
|
||
@Test | ||
fun `when a valid auth token is present then it is not refreshed`() = runTest { | ||
authStorage.updateAccessToken("token456") | ||
|
||
MockKtorService.isSuccess = true | ||
shouldThrowAuthError = false | ||
shouldThrowException = false | ||
|
||
ktorService.client.get { | ||
url { | ||
path("browse/categories") | ||
} | ||
} | ||
|
||
assertTrue { authStorage.getAccessToken() == "token456" } | ||
} | ||
|
||
private fun getKtorEngine(): HttpClientEngine = MockEngine { | ||
val url = it.url.toString() | ||
val mockConfig = mapOf( | ||
"browse/categories" to MockResponse( | ||
expectedSuccessResponsePath = "browse/categories.json", | ||
expectedErrorResponsePath = errorResponsePath | ||
), | ||
"api/token" to MockResponse( | ||
expectedSuccessResponsePath = "auth.json", | ||
expectedErrorResponsePath = errorResponsePath | ||
) | ||
) | ||
|
||
for (config in mockConfig.keys) { | ||
if (url.contains(config)) { | ||
val response = mockConfig[config] | ||
|
||
assert(response != null) { | ||
"There was no response for the path: $config" | ||
} | ||
|
||
val (status, body) = when { | ||
url.contains("browse") && !MockKtorService.isSuccess -> { | ||
MockKtorService.isSuccess = true | ||
HttpStatusCode.Unauthorized to getExpectedResponse(response!!.expectedErrorResponsePath) | ||
} | ||
url.contains("api/token") && shouldThrowException -> { | ||
throw Exception("Error when fetching token from server.") | ||
} | ||
url.contains("api/token") && shouldThrowAuthError -> { | ||
HttpStatusCode.InternalServerError to getExpectedResponse(response!!.expectedSuccessResponsePath) | ||
} | ||
else -> { | ||
HttpStatusCode.OK to getExpectedResponse(response!!.expectedSuccessResponsePath) | ||
} | ||
} | ||
|
||
return@MockEngine respond( | ||
content = ByteReadChannel(body), | ||
status = status, | ||
headers = Headers.build { this["content-type"] = "application/json" } | ||
) | ||
} | ||
} | ||
|
||
throw Exception("Request url: $url does not match the config. Please check the provided config.") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"access_token": "token123", | ||
"token_type": "Access", | ||
"expires_in": 3600 | ||
} |
27 changes: 27 additions & 0 deletions
27
kovibes/src/jvmTest/kotlin/io/github/rubenquadros/kovibes/api/test/KtorTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package io.github.rubenquadros.kovibes.api.test | ||
|
||
import io.github.rubenquadros.kovibes.api.getKtorEngine | ||
import io.github.rubenquadros.kovibes.api.getKtorLogger | ||
import io.ktor.client.engine.java.JavaHttpEngine | ||
import io.ktor.client.plugins.logging.Logger | ||
import io.ktor.util.reflect.instanceOf | ||
import kotlin.test.Test | ||
import kotlin.test.assertTrue | ||
|
||
class KtorTest { | ||
|
||
@Test | ||
fun `ktor engine is provided`() { | ||
val engine = getKtorEngine() | ||
|
||
assertTrue { engine is JavaHttpEngine } | ||
} | ||
|
||
@Test | ||
fun `logger is provided`() { | ||
val logger = getKtorLogger() | ||
|
||
// We should have checked for SimpleLogger but it is a private class | ||
assertTrue { logger.instanceOf(Logger::class) } | ||
} | ||
} |