BIT-5, BIT-101: Setup networking layer add get config call (#9)

Co-authored-by: joshua-livefront <joshua@livefront.com>
This commit is contained in:
Andrew Haisting
2023-08-25 08:54:47 -05:00
committed by GitHub
parent 4b7b84662d
commit cbd9cf51c4
9 changed files with 305 additions and 0 deletions

View File

@@ -0,0 +1,51 @@
package com.x8bit.bitwarden.example
import com.x8bit.bitwarden.data.datasource.network.ResultCallAdapterFactory
import kotlinx.coroutines.runBlocking
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
import org.junit.After
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
import retrofit2.Retrofit
import retrofit2.create
import retrofit2.http.GET
class ResultCallAdapterTests {
private val server: MockWebServer = MockWebServer().apply { start() }
private val testService: FakeService =
Retrofit.Builder()
.baseUrl(server.url("/").toString())
// add the adapter being tested
.addCallAdapterFactory(ResultCallAdapterFactory())
.build()
.create()
@After
fun after() {
server.shutdown()
}
@Test
fun `when server returns error response code result should be failure`() = runBlocking {
server.enqueue(MockResponse().setResponseCode(500))
val result = testService.requestWithUnitData()
assertTrue(result.isFailure)
}
@Test
fun `when server returns successful response result should be success`() = runBlocking {
server.enqueue(MockResponse())
val result = testService.requestWithUnitData()
assertTrue(result.isSuccess)
}
}
/**
* Fake retrofit service used for testing call adapters.
*/
private interface FakeService {
@GET("/fake")
suspend fun requestWithUnitData(): Result<Unit>
}