mirror of
https://github.com/bitwarden/android.git
synced 2026-08-02 02:32:04 -05:00
[PM-20305] Migrate BaseUrlInterceptor and BaseUrlInterceptors to network module (#5068)
This commit is contained in:
+9
-1
@@ -1,6 +1,8 @@
|
||||
package com.x8bit.bitwarden.data.platform.datasource.network.di
|
||||
|
||||
import com.bitwarden.network.interceptor.AuthTokenInterceptor
|
||||
import com.bitwarden.network.interceptor.BaseUrlInterceptors
|
||||
import com.bitwarden.network.interceptor.BaseUrlsProvider
|
||||
import com.bitwarden.network.interceptor.HeadersInterceptor
|
||||
import com.bitwarden.network.service.ConfigService
|
||||
import com.bitwarden.network.service.ConfigServiceImpl
|
||||
@@ -11,7 +13,6 @@ import com.bitwarden.network.service.PushServiceImpl
|
||||
import com.x8bit.bitwarden.data.auth.datasource.disk.AuthDiskSource
|
||||
import com.x8bit.bitwarden.data.auth.manager.AuthTokenManager
|
||||
import com.x8bit.bitwarden.data.platform.datasource.network.authenticator.RefreshAuthenticator
|
||||
import com.x8bit.bitwarden.data.platform.datasource.network.interceptor.BaseUrlInterceptors
|
||||
import com.x8bit.bitwarden.data.platform.datasource.network.retrofit.Retrofits
|
||||
import com.x8bit.bitwarden.data.platform.datasource.network.retrofit.RetrofitsImpl
|
||||
import com.x8bit.bitwarden.data.platform.datasource.network.ssl.SslManager
|
||||
@@ -92,6 +93,13 @@ object PlatformNetworkModule {
|
||||
environmentRepository = environmentRepository,
|
||||
)
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun providesBaseUrlInterceptors(
|
||||
baseUrlsProvider: BaseUrlsProvider,
|
||||
): BaseUrlInterceptors =
|
||||
BaseUrlInterceptors(baseUrlsProvider = baseUrlsProvider)
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideRetrofits(
|
||||
|
||||
-47
@@ -1,47 +0,0 @@
|
||||
package com.x8bit.bitwarden.data.platform.datasource.network.interceptor
|
||||
|
||||
import okhttp3.HttpUrl
|
||||
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
|
||||
import okhttp3.Interceptor
|
||||
import okhttp3.Response
|
||||
|
||||
/**
|
||||
* An [Interceptor] that optionally takes the current base URL of a request and replaces it with
|
||||
* the currently set base URL from the [baseUrlProvider].
|
||||
*/
|
||||
class BaseUrlInterceptor(
|
||||
private val baseUrlProvider: () -> String?,
|
||||
) : Interceptor {
|
||||
|
||||
private val baseHttpUrl: HttpUrl? get() = baseUrlProvider()?.toHttpUrlOrNull()
|
||||
|
||||
override fun intercept(chain: Interceptor.Chain): Response {
|
||||
val request = chain.request()
|
||||
|
||||
// If no base URL is set, we can simply skip
|
||||
val base = baseHttpUrl ?: return chain.proceed(request = request)
|
||||
|
||||
// Update the base URL used.
|
||||
return chain.proceed(
|
||||
request = request
|
||||
.newBuilder()
|
||||
.url(url = request.url.replaceBaseUrlWith(baseUrl = base))
|
||||
.build(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Given a [HttpUrl], replaces the existing base URL with the given [baseUrl].
|
||||
*/
|
||||
private fun HttpUrl.replaceBaseUrlWith(
|
||||
baseUrl: HttpUrl,
|
||||
) = baseUrl
|
||||
.newBuilder()
|
||||
.addEncodedPathSegments(
|
||||
this
|
||||
.encodedPathSegments
|
||||
.joinToString(separator = "/"),
|
||||
)
|
||||
.encodedQuery(this.encodedQuery)
|
||||
.build()
|
||||
-44
@@ -1,44 +0,0 @@
|
||||
package com.x8bit.bitwarden.data.platform.datasource.network.interceptor
|
||||
|
||||
import com.bitwarden.core.annotation.OmitFromCoverage
|
||||
import com.bitwarden.data.repository.model.Environment
|
||||
import com.bitwarden.data.repository.util.baseApiUrl
|
||||
import com.bitwarden.data.repository.util.baseEventsUrl
|
||||
import com.bitwarden.data.repository.util.baseIdentityUrl
|
||||
import com.bitwarden.data.repository.util.toEnvironmentUrlsOrDefault
|
||||
import com.x8bit.bitwarden.data.platform.datasource.disk.EnvironmentDiskSource
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
/**
|
||||
* An overall container for various [BaseUrlInterceptor] implementations for different API groups.
|
||||
*/
|
||||
@OmitFromCoverage
|
||||
@Singleton
|
||||
class BaseUrlInterceptors @Inject constructor(
|
||||
private val environmentDiskSource: EnvironmentDiskSource,
|
||||
) {
|
||||
private val environment: Environment
|
||||
get() = environmentDiskSource.preAuthEnvironmentUrlData.toEnvironmentUrlsOrDefault()
|
||||
|
||||
/**
|
||||
* An interceptor for "/api" calls.
|
||||
*/
|
||||
val apiInterceptor: BaseUrlInterceptor = BaseUrlInterceptor {
|
||||
environment.environmentUrlData.baseApiUrl
|
||||
}
|
||||
|
||||
/**
|
||||
* An interceptor for "/identity" calls.
|
||||
*/
|
||||
val identityInterceptor: BaseUrlInterceptor = BaseUrlInterceptor {
|
||||
environment.environmentUrlData.baseIdentityUrl
|
||||
}
|
||||
|
||||
/**
|
||||
* An interceptor for "/events" calls.
|
||||
*/
|
||||
val eventsInterceptor: BaseUrlInterceptor = BaseUrlInterceptor {
|
||||
environment.environmentUrlData.baseEventsUrl
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
package com.x8bit.bitwarden.data.platform.datasource.network.retrofit
|
||||
|
||||
import com.x8bit.bitwarden.data.platform.datasource.network.interceptor.BaseUrlInterceptors
|
||||
import com.bitwarden.network.interceptor.BaseUrlInterceptors
|
||||
import retrofit2.Retrofit
|
||||
import retrofit2.http.Url
|
||||
|
||||
|
||||
+2
-2
@@ -2,11 +2,11 @@ package com.x8bit.bitwarden.data.platform.datasource.network.retrofit
|
||||
|
||||
import com.bitwarden.network.core.NetworkResultCallAdapterFactory
|
||||
import com.bitwarden.network.interceptor.AuthTokenInterceptor
|
||||
import com.bitwarden.network.interceptor.BaseUrlInterceptor
|
||||
import com.bitwarden.network.interceptor.BaseUrlInterceptors
|
||||
import com.bitwarden.network.interceptor.HeadersInterceptor
|
||||
import com.bitwarden.network.util.HEADER_KEY_AUTHORIZATION
|
||||
import com.x8bit.bitwarden.data.platform.datasource.network.authenticator.RefreshAuthenticator
|
||||
import com.x8bit.bitwarden.data.platform.datasource.network.interceptor.BaseUrlInterceptor
|
||||
import com.x8bit.bitwarden.data.platform.datasource.network.interceptor.BaseUrlInterceptors
|
||||
import com.x8bit.bitwarden.data.platform.datasource.network.ssl.SslManager
|
||||
import com.x8bit.bitwarden.data.platform.util.isDevBuild
|
||||
import kotlinx.serialization.json.Json
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.x8bit.bitwarden.data.platform.provider
|
||||
|
||||
import com.bitwarden.data.repository.util.baseApiUrl
|
||||
import com.bitwarden.data.repository.util.baseEventsUrl
|
||||
import com.bitwarden.data.repository.util.baseIdentityUrl
|
||||
import com.bitwarden.data.repository.util.toEnvironmentUrlsOrDefault
|
||||
import com.bitwarden.network.interceptor.BaseUrlsProvider
|
||||
import com.x8bit.bitwarden.data.platform.datasource.disk.EnvironmentDiskSource
|
||||
|
||||
/**
|
||||
* The default implementation of [BaseUrlsProvider].
|
||||
*/
|
||||
class BaseUrlsProviderImpl(
|
||||
private val environmentDiskSource: EnvironmentDiskSource,
|
||||
) : BaseUrlsProvider {
|
||||
|
||||
override fun getBaseApiUrl(): String = environmentDiskSource
|
||||
.preAuthEnvironmentUrlData
|
||||
.toEnvironmentUrlsOrDefault()
|
||||
.environmentUrlData
|
||||
.baseApiUrl
|
||||
|
||||
override fun getBaseIdentityUrl(): String = environmentDiskSource
|
||||
.preAuthEnvironmentUrlData
|
||||
.toEnvironmentUrlsOrDefault()
|
||||
.environmentUrlData
|
||||
.baseIdentityUrl
|
||||
|
||||
override fun getBaseEventsUrl(): String = environmentDiskSource
|
||||
.preAuthEnvironmentUrlData
|
||||
.toEnvironmentUrlsOrDefault()
|
||||
.environmentUrlData
|
||||
.baseEventsUrl
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.x8bit.bitwarden.data.platform.provider.di
|
||||
|
||||
import com.bitwarden.network.interceptor.BaseUrlsProvider
|
||||
import com.x8bit.bitwarden.data.platform.datasource.disk.EnvironmentDiskSource
|
||||
import com.x8bit.bitwarden.data.platform.provider.BaseUrlsProviderImpl
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import dagger.hilt.InstallIn
|
||||
import dagger.hilt.components.SingletonComponent
|
||||
import javax.inject.Singleton
|
||||
|
||||
/**
|
||||
* This module initializes platform-specific "Provider" dependencies for the application.
|
||||
*/
|
||||
@Module
|
||||
@InstallIn(SingletonComponent::class)
|
||||
object PlatformProvidersModule {
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideBaseUrlsProvider(
|
||||
environmentDiskSource: EnvironmentDiskSource,
|
||||
): BaseUrlsProvider =
|
||||
BaseUrlsProviderImpl(environmentDiskSource = environmentDiskSource)
|
||||
}
|
||||
-38
@@ -1,38 +0,0 @@
|
||||
package com.x8bit.bitwarden.data.platform.datasource.network.interceptor
|
||||
|
||||
import com.bitwarden.network.interceptor.FakeInterceptorChain
|
||||
import okhttp3.Request
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Assertions.assertNotEquals
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class BaseUrlInterceptorTest {
|
||||
private var baseUrl: String? = null
|
||||
private val baseUrlInterceptor = BaseUrlInterceptor { baseUrl }
|
||||
|
||||
@Test
|
||||
fun `intercept with a null base URL should proceed with the original request`() {
|
||||
val request = Request.Builder().url("http://www.fake.com/").build()
|
||||
val chain = FakeInterceptorChain(request)
|
||||
|
||||
val response = baseUrlInterceptor.intercept(chain)
|
||||
|
||||
assertEquals(request, response.request)
|
||||
assertEquals("http", response.request.url.scheme)
|
||||
assertEquals("www.fake.com", response.request.url.host)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `intercept with a non-null base URL should update the base URL used by the request`() {
|
||||
baseUrl = "https://api.bitwarden.com"
|
||||
|
||||
val request = Request.Builder().url("http://www.fake.com/").build()
|
||||
val chain = FakeInterceptorChain(request)
|
||||
|
||||
val response = baseUrlInterceptor.intercept(chain)
|
||||
|
||||
assertNotEquals(request, response.request)
|
||||
assertEquals("https", response.request.url.scheme)
|
||||
assertEquals("api.bitwarden.com", response.request.url.host)
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,10 +1,10 @@
|
||||
package com.x8bit.bitwarden.data.platform.datasource.network.retrofit
|
||||
|
||||
import com.bitwarden.network.interceptor.AuthTokenInterceptor
|
||||
import com.bitwarden.network.interceptor.BaseUrlInterceptors
|
||||
import com.bitwarden.network.interceptor.HeadersInterceptor
|
||||
import com.bitwarden.network.model.NetworkResult
|
||||
import com.x8bit.bitwarden.data.platform.datasource.network.authenticator.RefreshAuthenticator
|
||||
import com.x8bit.bitwarden.data.platform.datasource.network.interceptor.BaseUrlInterceptors
|
||||
import com.x8bit.bitwarden.data.platform.datasource.network.ssl.SslManager
|
||||
import com.x8bit.bitwarden.data.util.mockBuilder
|
||||
import io.mockk.every
|
||||
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
package com.x8bit.bitwarden.data.platform.manager.provider
|
||||
|
||||
import com.bitwarden.data.repository.model.Environment
|
||||
import com.x8bit.bitwarden.data.platform.datasource.disk.FakeEnvironmentDiskSource
|
||||
import com.x8bit.bitwarden.data.platform.provider.BaseUrlsProviderImpl
|
||||
import org.junit.jupiter.api.Assertions
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class BaseUrlsProviderTest {
|
||||
|
||||
private val fakeEnvironmentDiskSource = FakeEnvironmentDiskSource()
|
||||
private val baseUrlsManager = BaseUrlsProviderImpl(
|
||||
environmentDiskSource = fakeEnvironmentDiskSource,
|
||||
)
|
||||
|
||||
@Test
|
||||
fun `getBaseApiUrl should return correct api URL when preAuthEnvironmentUrlData is set`() {
|
||||
fakeEnvironmentDiskSource.preAuthEnvironmentUrlData = Environment.Eu.environmentUrlData
|
||||
Assertions.assertEquals(
|
||||
"https://vault.bitwarden.eu/api",
|
||||
baseUrlsManager.getBaseApiUrl(),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getBaseApiUrl should return default value when preAuthEnvironmentUrlData is null`() {
|
||||
fakeEnvironmentDiskSource.preAuthEnvironmentUrlData = null
|
||||
Assertions.assertEquals(
|
||||
"https://vault.bitwarden.com/api",
|
||||
baseUrlsManager.getBaseApiUrl(),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getBaseIdentityUrl should return correct api URL when preAuthEnvironmentUrlData is set`() {
|
||||
fakeEnvironmentDiskSource.preAuthEnvironmentUrlData = Environment.Eu.environmentUrlData
|
||||
Assertions.assertEquals(
|
||||
"https://vault.bitwarden.eu/identity",
|
||||
baseUrlsManager.getBaseIdentityUrl(),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getBaseIdentityUrl should return default value when preAuthEnvironmentUrlData is null`() {
|
||||
fakeEnvironmentDiskSource.preAuthEnvironmentUrlData = null
|
||||
Assertions.assertEquals(
|
||||
"https://vault.bitwarden.com/identity",
|
||||
baseUrlsManager.getBaseIdentityUrl(),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getBaseEventsUrl should return correct api URL when preAuthEnvironmentUrlData is set`() {
|
||||
fakeEnvironmentDiskSource.preAuthEnvironmentUrlData = Environment.Eu.environmentUrlData
|
||||
Assertions.assertEquals(
|
||||
"https://vault.bitwarden.eu/events",
|
||||
baseUrlsManager.getBaseEventsUrl(),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getBaseEventsUrl should return default value when preAuthEnvironmentUrlData is null`() {
|
||||
fakeEnvironmentDiskSource.preAuthEnvironmentUrlData = null
|
||||
Assertions.assertEquals(
|
||||
"https://vault.bitwarden.com/events",
|
||||
baseUrlsManager.getBaseEventsUrl(),
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user