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 3e23c0f91436810bbf6071a63f276892041b5cac..a7e1f3dbb9d60f8bac2834be582fbf188aaceda6 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 89c5e9f81a7742c022ad8e72d79383d475cb71ac..154f43ce7d0b7f02ab2369f07399d069bd3d976c 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 c05e9487a7b1cc2f3953a3c8dcf4629d579c33c9..5ad2e25502da37b6f0242cc52d5ea3dd1886bc96 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 {