mirror of
https://github.com/bitwarden/android.git
synced 2026-04-29 04:18:52 -05:00
[PM-20193] Migrate DownloadService to network module (#5053)
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
package com.bitwarden.network.service
|
||||
|
||||
import okhttp3.ResponseBody
|
||||
|
||||
/**
|
||||
* Provides an API for querying arbitrary endpoints.
|
||||
*/
|
||||
interface DownloadService {
|
||||
/**
|
||||
* Streams data from [url], returning a raw [ResponseBody].
|
||||
*/
|
||||
suspend fun getDataStream(url: String): Result<ResponseBody>
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.bitwarden.network.service
|
||||
|
||||
import com.bitwarden.network.api.DownloadApi
|
||||
import com.bitwarden.network.util.toResult
|
||||
import okhttp3.ResponseBody
|
||||
|
||||
/**
|
||||
* Default implementation of [DownloadService].
|
||||
*/
|
||||
class DownloadServiceImpl(
|
||||
private val downloadApi: DownloadApi,
|
||||
) : DownloadService {
|
||||
override suspend fun getDataStream(
|
||||
url: String,
|
||||
): Result<ResponseBody> =
|
||||
downloadApi
|
||||
.getDataStream(url = url)
|
||||
.toResult()
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.bitwarden.network.service
|
||||
|
||||
import com.bitwarden.network.api.DownloadApi
|
||||
import com.bitwarden.network.base.BaseServiceTest
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import okhttp3.mockwebserver.MockResponse
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Assertions.assertTrue
|
||||
import org.junit.jupiter.api.Test
|
||||
import retrofit2.create
|
||||
|
||||
class DownloadServiceTest : BaseServiceTest() {
|
||||
private val downloadApi: DownloadApi = retrofit.create()
|
||||
|
||||
private val downloadService: DownloadService = DownloadServiceImpl(
|
||||
downloadApi = downloadApi,
|
||||
)
|
||||
|
||||
@Test
|
||||
fun `getDataStream should return a raw stream RespondBody`() = runTest {
|
||||
server.enqueue(
|
||||
MockResponse()
|
||||
.setResponseCode(200)
|
||||
.setBody("Bitwarden")
|
||||
.setHeader("Content-Type", "application/stream"),
|
||||
)
|
||||
val url = "/test-url"
|
||||
val result = downloadService.getDataStream(url)
|
||||
assertTrue(result.isSuccess)
|
||||
assertEquals("Bitwarden", String(result.getOrThrow().bytes()))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user