mirror of
https://github.com/austinried/subtracks.git
synced 2026-11-10 15:11:58 +01:00
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; <version>)' value rather than the property it writes, and run the test under the Android runner.
This commit is contained in:
@@ -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() {
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user