From 485f0044f88fc63752befb49e93db12840de68b0 Mon Sep 17 00:00:00 2001 From: Hasib Prince Date: Wed, 21 Feb 2024 19:00:01 +0600 Subject: [PATCH] added unit test for 429 --- .../data/playstore/utils/GPlayHttpClient.kt | 4 +- .../e/apps/gplay/GplyHttpClientTest.kt | 68 ++++++++++++++++--- .../java/foundation/e/apps/util/FakeCall.kt | 19 +++--- 3 files changed, 71 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/foundation/e/apps/data/playstore/utils/GPlayHttpClient.kt b/app/src/main/java/foundation/e/apps/data/playstore/utils/GPlayHttpClient.kt index 3e23c0f91..a7e1f3dbb 100644 --- a/app/src/main/java/foundation/e/apps/data/playstore/utils/GPlayHttpClient.kt +++ b/app/src/main/java/foundation/e/apps/data/playstore/utils/GPlayHttpClient.kt @@ -56,8 +56,8 @@ class GPlayHttpClient @Inject constructor( private const val HTTP_TIMEOUT_IN_SECOND = 10L private const val SEARCH_SUGGEST = "searchSuggest" private const val STATUS_CODE_OK = 200 - private const val STATUS_CODE_UNAUTHORIZED = 401 - private const val STATUS_CODE_TOO_MANY_REQUESTS = 429 + const val STATUS_CODE_UNAUTHORIZED = 401 + const val STATUS_CODE_TOO_MANY_REQUESTS = 429 private const val URL_SUBSTRING_PURCHASE = "purchase" const val STATUS_CODE_TIMEOUT = 408 } diff --git a/app/src/test/java/foundation/e/apps/gplay/GplyHttpClientTest.kt b/app/src/test/java/foundation/e/apps/gplay/GplyHttpClientTest.kt index 89c5e9f81..154f43ce7 100644 --- a/app/src/test/java/foundation/e/apps/gplay/GplyHttpClientTest.kt +++ b/app/src/test/java/foundation/e/apps/gplay/GplyHttpClientTest.kt @@ -21,6 +21,7 @@ package foundation.e.apps.gplay import com.aurora.gplayapi.data.models.PlayResponse import foundation.e.apps.data.playstore.utils.GPlayHttpClient import foundation.e.apps.data.login.AuthObject +import foundation.e.apps.data.playstore.utils.GplayHttpRequestException import foundation.e.apps.util.FakeCall import foundation.e.apps.util.MainCoroutineRule import foundation.e.apps.utils.SystemInfoProvider @@ -71,62 +72,109 @@ class GplyHttpClientTest { @Test fun testPostWithMapFailedWhenStatus401() = runTest { - initMocks() + initMocksForStatus401() val response = gPlayHttpClient.post("http://abc.abc", mapOf(), mapOf()) assertResponse(response) } @Test fun testPostWithRequestBodyFailedWhenStatus401() = runTest { - initMocks() + initMocksForStatus401() val response = gPlayHttpClient.post("http://abc.abc", mapOf(), "".toRequestBody()) assertResponse(response) } @Test fun testPostWithByteArrayFailedWhenStatus401() = runTest { - initMocks() + initMocksForStatus401() val response = gPlayHttpClient.post("http://abc.abc", mapOf(), "".toByteArray()) assertResponse(response) } @Test fun testGetWithoutParamsFailedWhenStatus401() = runTest { - initMocks() + initMocksForStatus401() val response = gPlayHttpClient.get(FakeCall.FAKE_URL, mapOf()) assertResponse(response) } @Test fun testGetWithStringParamsFailedWhenStatus401() = runTest { - initMocks() + initMocksForStatus401() val response = gPlayHttpClient.get(FakeCall.FAKE_URL, mapOf(), "") assertResponse(response) } @Test fun testGetWithMapParamsFailedWhenStatus401() = runTest { - initMocks() + initMocksForStatus401() val response = gPlayHttpClient.get(FakeCall.FAKE_URL, mapOf(), mapOf()) assertResponse(response) } @Test fun testPostAuthFailedWhenStatus401() = runTest { - initMocks() + initMocksForStatus401() val response = gPlayHttpClient.postAuth("http://abc.abc", "".toByteArray()) assertResponse(response) } - private fun initMocks() { + @Test + fun testPostAuthFailedWhenStatus429() = runTest { + initMocksForStatus429() + try { + gPlayHttpClient.postAuth("http://abc.abc", "".toByteArray()) + } catch (e: Exception) { + assert429(e) + } + } + + @Test + fun testGetWithMapParamsFailedWhenStatus429() = runTest { + initMocksForStatus429() + try { + gPlayHttpClient.get(FakeCall.FAKE_URL, mapOf(), mapOf()) + } catch (e: Exception) { + assert429(e) + } + } + + @Test + fun testGetWithStringParamsFailedWhenStatus429() = runTest { + initMocksForStatus429() + try { + gPlayHttpClient.get(FakeCall.FAKE_URL, mapOf(), "") + } catch (e: Exception) { + assert429(e) + } + } + + private fun initMocksForStatus401() { call.willThrow401 = true mockkObject(SystemInfoProvider) every { SystemInfoProvider.getAppBuildInfo() } returns "" Mockito.`when`(okHttpClient.newCall(any())).thenReturn(call) } - private suspend fun assertResponse(response: PlayResponse) { + + private suspend fun assert429(e: Exception) { + assertTrue( + "Status429", + e is GplayHttpRequestException && e.status == GPlayHttpClient.STATUS_CODE_TOO_MANY_REQUESTS + ) + val event = EventBus.events.first() + assertTrue(event is AppEvent.TooManyRequests) + } + + private fun initMocksForStatus429() { + call.willThrow429 = true + mockkObject(SystemInfoProvider) + every { SystemInfoProvider.getAppBuildInfo() } returns "" + Mockito.`when`(okHttpClient.newCall(any())).thenReturn(call) + } + + private suspend fun assertResponse(response: PlayResponse, statusValue: Int = 401) { assertFalse(response.isSuccessful) - assertTrue(response.code == 401) + assertTrue(response.code == statusValue) val event = EventBus.events.first() assertTrue(event is AppEvent.InvalidAuthEvent) assertTrue(event.data is String) diff --git a/app/src/test/java/foundation/e/apps/util/FakeCall.kt b/app/src/test/java/foundation/e/apps/util/FakeCall.kt index c05e9487a..5ad2e2550 100644 --- a/app/src/test/java/foundation/e/apps/util/FakeCall.kt +++ b/app/src/test/java/foundation/e/apps/util/FakeCall.kt @@ -29,6 +29,7 @@ import okio.Timeout class FakeCall : Call { var willThrow401 = false + var willThrow429 = false companion object { const val FAKE_URL = "https://abc.abc" @@ -48,16 +49,18 @@ class FakeCall : Call { } override fun execute(): Response { + val builder = Response.Builder() + .request(fakeRequest) + .protocol(Protocol.HTTP_2) + .message("") + .code(401) + .body("".toResponseBody()) if (willThrow401) { - return Response.Builder() - .request(fakeRequest) - .protocol(Protocol.HTTP_2) - .message("") - .code(401) - .body("".toResponseBody()) - .build() + builder.code(401) + } else if (willThrow429) { + builder.code(429) } - return Response.Builder().build() + return builder.build() } override fun isCanceled(): Boolean { -- GitLab