Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 485f0044 authored by Hasib Prince's avatar Hasib Prince
Browse files

added unit test for 429

parent 043e1f0c
Loading
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -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
    }
+58 −10
Original line number Diff line number Diff line
@@ -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)
+11 −8
Original line number Diff line number Diff line
@@ -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 {
        if (willThrow401) {
            return Response.Builder()
        val builder = Response.Builder()
            .request(fakeRequest)
            .protocol(Protocol.HTTP_2)
            .message("")
            .code(401)
            .body("".toResponseBody())
                .build()
        if (willThrow401) {
            builder.code(401)
        } else if (willThrow429) {
            builder.code(429)
        }
        return Response.Builder().build()
        return builder.build()
    }

    override fun isCanceled(): Boolean {