@@ -6,73 +6,72 @@ import androidx.test.core.app.ApplicationProvider
66import io.mockk.every
77import io.mockk.mockkStatic
88import okhttp3.OkHttpClient
9+ import okhttp3.Request
910import okhttp3.mockwebserver.MockResponse
1011import okhttp3.mockwebserver.MockWebServer
12+ import org.junit.After
1113import org.junit.Assert.assertEquals
1214import org.junit.Before
1315import org.junit.Test
1416import org.junit.runner.RunWith
1517import org.robolectric.RobolectricTestRunner
16- import retrofit2.Call
17- import retrofit2.Retrofit
18- import retrofit2.converter.moshi.MoshiConverterFactory
19- import retrofit2.http.GET
2018import java.util.Locale
2119
2220@RunWith(RobolectricTestRunner ::class )
2321class AcceptedLanguageInterceptorTest {
2422 private val mockWebServer = MockWebServer ()
2523
26- private lateinit var api : TestApi
24+ private lateinit var client : OkHttpClient
2725
2826 private val context = ApplicationProvider .getApplicationContext<Context >()
2927
30- private val testResponse = " \" name\" "
31-
3228 @Before
3329 fun setUp () {
34- val client = OkHttpClient .Builder ()
30+ client = OkHttpClient .Builder ()
3531 .addInterceptor(AcceptedLanguageInterceptor (context))
3632 .build()
33+ }
3734
38- api = Retrofit .Builder ()
39- .baseUrl(mockWebServer.url(" /" ))
40- .addConverterFactory(MoshiConverterFactory .create())
41- .client(client)
42- .build()
43- .create(TestApi ::class .java)
35+ @After
36+ fun after () {
37+ mockWebServer.shutdown()
4438 }
4539
4640 @Test
4741 fun `given one languages when intercepting then Accept-Language header set` () {
4842 mockkStatic(Context ::languagesList)
4943 every { context.languagesList } returns LocaleListCompat .create(Locale .ENGLISH )
50- val successResponse = MockResponse ().setBody(testResponse)
51- mockWebServer.enqueue(successResponse)
5244
53- api.test().execute()
45+ val mockResponse = MockResponse ().setResponseCode(200 )
46+ mockWebServer.enqueue(mockResponse)
47+ mockWebServer.start()
48+
49+ val request = Request .Builder ()
50+ .url(mockWebServer.url(" /test" ))
51+ .build()
52+
53+ client.newCall(request).execute()
5454
55- val request = mockWebServer.takeRequest()
56- val header = request.getHeader(" Accept-Language" )
57- assertEquals(" en" , header)
55+ val recordedRequest = mockWebServer.takeRequest()
56+ assertEquals(" en" , recordedRequest.getHeader(" Accept-Language" ))
5857 }
5958
6059 @Test
6160 fun `given many languages when intercepting then Accept-Language header set` () {
6261 mockkStatic(Context ::languagesList)
6362 every { context.languagesList } returns LocaleListCompat .create(Locale .ENGLISH , Locale .FRENCH , Locale .GERMANY )
64- val successResponse = MockResponse ().setBody(testResponse)
65- mockWebServer.enqueue(successResponse)
6663
67- api.test().execute()
64+ val mockResponse = MockResponse ().setResponseCode(200 )
65+ mockWebServer.enqueue(mockResponse)
66+ mockWebServer.start()
6867
69- val request = mockWebServer.takeRequest()
70- val header = request.getHeader(" Accept-Language" )
71- assertEquals(" en,fr;q=0.9,de;q=0.8" , header)
72- }
73- }
68+ val request = Request .Builder ()
69+ .url(mockWebServer.url(" /test" ))
70+ .build()
7471
75- private interface TestApi {
76- @GET(" /test" )
77- fun test (): Call <String >
72+ client.newCall(request).execute()
73+
74+ val recordedRequest = mockWebServer.takeRequest()
75+ assertEquals(" en,fr;q=0.9,de;q=0.8" , recordedRequest.getHeader(" Accept-Language" ))
76+ }
7877}
0 commit comments