diff --git a/app/src/main/kotlin/com/subtracks/SubtracksApp.kt b/app/src/main/kotlin/com/subtracks/SubtracksApp.kt index a0876b57..8190e751 100644 --- a/app/src/main/kotlin/com/subtracks/SubtracksApp.kt +++ b/app/src/main/kotlin/com/subtracks/SubtracksApp.kt @@ -9,12 +9,11 @@ import com.subtracks.data.prefs.UserPreferences import com.subtracks.data.repo.ArtworkSeedStore import com.subtracks.di.appModule import com.subtracks.ui.theme.ArtworkSeedCache -import okhttp3.OkHttpClient import org.koin.core.context.startKoin import org.koin.core.context.stopKoin class SubtracksApp : Application() { - private val http = OkHttpClient.Builder().addInterceptor(UserAgent.interceptor()).build() + private val http = UserAgent.httpClient() @OptIn(DelicateCoilApi::class) override fun onCreate() { diff --git a/app/src/main/kotlin/com/subtracks/UserAgent.kt b/app/src/main/kotlin/com/subtracks/UserAgent.kt index f8351b6a..1ae21533 100644 --- a/app/src/main/kotlin/com/subtracks/UserAgent.kt +++ b/app/src/main/kotlin/com/subtracks/UserAgent.kt @@ -1,6 +1,7 @@ package com.subtracks import okhttp3.Interceptor +import okhttp3.OkHttpClient object UserAgent { val value: String = "Subtracks (Android; ${BuildConfig.VERSION_NAME})" @@ -15,4 +16,6 @@ object UserAgent { .build(), ) } + + fun httpClient(): OkHttpClient = OkHttpClient.Builder().addInterceptor(interceptor()).build() } diff --git a/app/src/test/kotlin/com/subtracks/UserAgentTest.kt b/app/src/test/kotlin/com/subtracks/UserAgentTest.kt index 0ef76a95..d66b9207 100644 --- a/app/src/test/kotlin/com/subtracks/UserAgentTest.kt +++ b/app/src/test/kotlin/com/subtracks/UserAgentTest.kt @@ -1,24 +1,29 @@ package com.subtracks -import okhttp3.OkHttpClient +import androidx.test.ext.junit.runners.AndroidJUnit4 import okhttp3.Request import okhttp3.mockwebserver.MockResponse import okhttp3.mockwebserver.MockWebServer import org.junit.Assert.assertEquals import org.junit.Test +import org.junit.runner.RunWith +@RunWith(AndroidJUnit4::class) class UserAgentTest { @Test - fun addsTheSubtracksUserAgent() { + fun theSharedClientSendsTheSubtracksUserAgent() { val server = MockWebServer() server.enqueue(MockResponse()) server.start() - val client = OkHttpClient.Builder().addInterceptor(UserAgent.interceptor()).build() - client.newCall(Request.Builder().url(server.url("/")).build()).execute().close() + UserAgent + .httpClient() + .newCall(Request.Builder().url(server.url("/")).build()) + .execute() + .close() val header = server.takeRequest().getHeader("User-Agent") server.shutdown() - assertEquals(UserAgent.value, header) + assertEquals("Subtracks (Android; ${BuildConfig.VERSION_NAME})", header) } }