mirror of
https://github.com/bitwarden/android.git
synced 2026-06-01 02:06:52 -05:00
BIT-685: Add request headers to all network requests in the app (#300)
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
package com.x8bit.bitwarden.data.platform.datasource.network.interceptor
|
||||
|
||||
import android.os.Build
|
||||
import com.x8bit.bitwarden.BuildConfig
|
||||
import com.x8bit.bitwarden.ui.platform.base.BaseRobolectricTest
|
||||
import okhttp3.Request
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Test
|
||||
|
||||
class HeadersInterceptorTest : BaseRobolectricTest() {
|
||||
|
||||
private val headersInterceptors = HeadersInterceptor()
|
||||
|
||||
@Test
|
||||
fun `intercept should modify original request to include custom headers`() {
|
||||
// We reference the real BuildConfig here, since we don't want the test to break on every
|
||||
// version bump. We are also doing the same thing for Build when the SDK gets incremented.
|
||||
val versionName = BuildConfig.VERSION_NAME
|
||||
val release = Build.VERSION.RELEASE
|
||||
val sdk = Build.VERSION.SDK_INT
|
||||
val originalRequest = Request.Builder().url("http://www.fake.com/").build()
|
||||
val chain = FakeInterceptorChain(originalRequest)
|
||||
|
||||
val response = headersInterceptors.intercept(chain)
|
||||
|
||||
val request = response.request
|
||||
assertEquals(
|
||||
"Bitwarden_Mobile/$versionName (Android $release; SDK $sdk; Model robolectric)",
|
||||
request.header("User-Agent"),
|
||||
)
|
||||
assertEquals("mobile", request.header("Bitwarden-Client-Name"))
|
||||
assertEquals(versionName, request.header("Bitwarden-Client-Version"))
|
||||
assertEquals("0", request.header("Device-Type"))
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package com.x8bit.bitwarden.data.platform.datasource.network.retrofit
|
||||
import com.x8bit.bitwarden.data.platform.datasource.network.authenticator.RefreshAuthenticator
|
||||
import com.x8bit.bitwarden.data.platform.datasource.network.interceptor.AuthTokenInterceptor
|
||||
import com.x8bit.bitwarden.data.platform.datasource.network.interceptor.BaseUrlInterceptors
|
||||
import com.x8bit.bitwarden.data.platform.datasource.network.interceptor.HeadersInterceptor
|
||||
import io.mockk.every
|
||||
import io.mockk.mockk
|
||||
import io.mockk.slot
|
||||
@@ -37,6 +38,9 @@ class RetrofitsTest {
|
||||
mockIntercept { isEventsInterceptorCalled = true }
|
||||
}
|
||||
}
|
||||
private val headersInterceptors = mockk<HeadersInterceptor> {
|
||||
mockIntercept { isheadersInterceptorCalled = true }
|
||||
}
|
||||
private val refreshAuthenticator = mockk<RefreshAuthenticator> {
|
||||
mockAuthenticate { isRefreshAuthenticatorCalled = true }
|
||||
}
|
||||
@@ -46,12 +50,14 @@ class RetrofitsTest {
|
||||
private val retrofits = RetrofitsImpl(
|
||||
authTokenInterceptor = authTokenInterceptor,
|
||||
baseUrlInterceptors = baseUrlInterceptors,
|
||||
headersInterceptor = headersInterceptors,
|
||||
refreshAuthenticator = refreshAuthenticator,
|
||||
json = json,
|
||||
)
|
||||
|
||||
private var isAuthInterceptorCalled = false
|
||||
private var isApiInterceptorCalled = false
|
||||
private var isheadersInterceptorCalled = false
|
||||
private var isIdentityInterceptorCalled = false
|
||||
private var isEventsInterceptorCalled = false
|
||||
private var isRefreshAuthenticatorCalled = false
|
||||
@@ -122,6 +128,7 @@ class RetrofitsTest {
|
||||
|
||||
assertTrue(isAuthInterceptorCalled)
|
||||
assertTrue(isApiInterceptorCalled)
|
||||
assertTrue(isheadersInterceptorCalled)
|
||||
assertFalse(isIdentityInterceptorCalled)
|
||||
assertFalse(isEventsInterceptorCalled)
|
||||
}
|
||||
@@ -139,6 +146,7 @@ class RetrofitsTest {
|
||||
|
||||
assertFalse(isAuthInterceptorCalled)
|
||||
assertTrue(isApiInterceptorCalled)
|
||||
assertTrue(isheadersInterceptorCalled)
|
||||
assertFalse(isIdentityInterceptorCalled)
|
||||
assertFalse(isEventsInterceptorCalled)
|
||||
}
|
||||
@@ -156,6 +164,7 @@ class RetrofitsTest {
|
||||
|
||||
assertFalse(isAuthInterceptorCalled)
|
||||
assertFalse(isApiInterceptorCalled)
|
||||
assertTrue(isheadersInterceptorCalled)
|
||||
assertTrue(isIdentityInterceptorCalled)
|
||||
assertFalse(isEventsInterceptorCalled)
|
||||
}
|
||||
@@ -175,6 +184,7 @@ class RetrofitsTest {
|
||||
|
||||
assertFalse(isAuthInterceptorCalled)
|
||||
assertFalse(isApiInterceptorCalled)
|
||||
assertTrue(isheadersInterceptorCalled)
|
||||
assertFalse(isIdentityInterceptorCalled)
|
||||
assertFalse(isEventsInterceptorCalled)
|
||||
}
|
||||
|
||||
@@ -3,30 +3,16 @@ package com.x8bit.bitwarden.ui.platform.base
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.test.junit4.createComposeRule
|
||||
import com.x8bit.bitwarden.ui.platform.theme.BitwardenTheme
|
||||
import dagger.hilt.android.testing.HiltTestApplication
|
||||
import org.junit.Rule
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
import org.robolectric.shadows.ShadowLog
|
||||
|
||||
/**
|
||||
* A base class that can be used for performing Compose-layer testing using Robolectric, Compose
|
||||
* Testing, and JUnit 4.
|
||||
*/
|
||||
@Config(
|
||||
application = HiltTestApplication::class,
|
||||
sdk = [Config.NEWEST_SDK],
|
||||
)
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
abstract class BaseComposeTest {
|
||||
abstract class BaseComposeTest : BaseRobolectricTest() {
|
||||
@get:Rule
|
||||
val composeTestRule = createComposeRule()
|
||||
|
||||
init {
|
||||
ShadowLog.stream = System.out
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper for testing a basic Composable function that only requires a Composable environment
|
||||
* with the [BitwardenTheme].
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.x8bit.bitwarden.ui.platform.base
|
||||
|
||||
import dagger.hilt.android.testing.HiltTestApplication
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
import org.robolectric.shadows.ShadowLog
|
||||
|
||||
/**
|
||||
* A base class that can be used for performing tests that use Robolectric and JUnit 4.
|
||||
*/
|
||||
@Config(
|
||||
application = HiltTestApplication::class,
|
||||
sdk = [Config.NEWEST_SDK],
|
||||
)
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
abstract class BaseRobolectricTest {
|
||||
init {
|
||||
ShadowLog.stream = System.out
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user