From 2439368e4e577b0c0d9d64ebe81138fbe0bca17b Mon Sep 17 00:00:00 2001 From: austinried <4966622+austinried@users.noreply.github.com> Date: Thu, 8 Oct 2026 20:32:19 +0000 Subject: [PATCH] net: pin the user agent value and test the shared client Build the shared OkHttpClient through UserAgent.httpClient() so the unit test exercises the same client the app uses, assert the exact 'Subtracks (Android; )' value rather than the property it writes, and run the test under the Android runner. --- app/src/main/kotlin/com/subtracks/SubtracksApp.kt | 3 +-- app/src/main/kotlin/com/subtracks/UserAgent.kt | 3 +++ .../test/kotlin/com/subtracks/UserAgentTest.kt | 15 ++++++++++----- 3 files changed, 14 insertions(+), 7 deletions(-) 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) } }