Skip to content

Commit b21a600

Browse files
committed
fix: update critical test files for OkHttp 4 compatibility
- Fixed addInterceptor overload ambiguity in NylasClientTest.kt - Updated function calls to properties (body(), code(), url(), etc.) - Fixed MediaType.parse() and HttpUrl.get() migration issues - Updated Headers.of() to Headers.headersOf() - Core NylasClient and backwards compatibility tests now work Note: Additional test files still need OkHttp 4 migration
1 parent fc45093 commit b21a600

File tree

3 files changed

+69
-67
lines changed

3 files changed

+69
-67
lines changed

src/test/kotlin/com/nylas/NylasClientTest.kt

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class NylasClientTest {
6161
fun `builder sets correct httpClient`() {
6262
val mockOkHttpClientBuilder: OkHttpClient.Builder = mock()
6363
val mockOkHttpClient: OkHttpClient = mock()
64-
whenever(mockOkHttpClientBuilder.addInterceptor(any())).thenReturn(mockOkHttpClientBuilder)
64+
whenever(mockOkHttpClientBuilder.addInterceptor(any<Interceptor>())).thenReturn(mockOkHttpClientBuilder)
6565
whenever(mockOkHttpClientBuilder.build()).thenReturn(mockOkHttpClient)
6666

6767
val client = NylasClient.Builder("testApiKey")
@@ -90,7 +90,7 @@ class NylasClientTest {
9090
fun `initializing the NylasClient with values sets them correctly`() {
9191
val mockOkHttpClientBuilder: OkHttpClient.Builder = mock()
9292
val mockOkHttpClient: OkHttpClient = mock()
93-
whenever(mockOkHttpClientBuilder.addInterceptor(any())).thenReturn(mockOkHttpClientBuilder)
93+
whenever(mockOkHttpClientBuilder.addInterceptor(any<Interceptor>())).thenReturn(mockOkHttpClientBuilder)
9494
whenever(mockOkHttpClientBuilder.build()).thenReturn(mockOkHttpClient)
9595

9696
val client = NylasClient(apiKey = "testApiKey", httpClientBuilder = mockOkHttpClientBuilder, apiUri = "https://custom-api.nylas.com/")
@@ -192,13 +192,13 @@ class NylasClientTest {
192192
fun setup() {
193193
MockitoAnnotations.openMocks(this)
194194
val mockOkHttpClientBuilder: OkHttpClient.Builder = mock()
195-
whenever(mockOkHttpClientBuilder.addInterceptor(any())).thenReturn(mockOkHttpClientBuilder)
195+
whenever(mockOkHttpClientBuilder.addInterceptor(any<Interceptor>())).thenReturn(mockOkHttpClientBuilder)
196196
whenever(mockOkHttpClientBuilder.build()).thenReturn(mockHttpClient)
197197
whenever(mockHttpClient.newCall(any())).thenReturn(mockCall)
198198
whenever(mockCall.execute()).thenReturn(mockResponse)
199199
whenever(mockResponse.isSuccessful).thenReturn(true)
200-
whenever(mockResponse.body()).thenReturn(mockResponseBody)
201-
whenever(mockResponse.headers()).thenReturn(mockHeaderResponse)
200+
whenever(mockResponse.body).thenReturn(mockResponseBody)
201+
whenever(mockResponse.headers).thenReturn(mockHeaderResponse)
202202
whenever(mockHeaderResponse.toMultimap()).thenReturn(headersMultiMap)
203203
nylasClient = NylasClient("testApiKey", mockOkHttpClientBuilder)
204204
}
@@ -213,8 +213,8 @@ class NylasClientTest {
213213
val capturedRequest = requestCaptor.value
214214

215215
assertEquals("bar", result["foo"])
216-
assertEquals(capturedRequest.url().toString(), "https://api.us.nylas.com/")
217-
assertEquals(capturedRequest.method(), "GET")
216+
assertEquals(capturedRequest.url.toString(), "https://api.us.nylas.com/")
217+
assertEquals(capturedRequest.method, "GET")
218218
}
219219

220220
@Test
@@ -223,7 +223,7 @@ class NylasClientTest {
223223
val revokeUrlBuilder = nylasClient.newUrlBuilder().addPathSegments("v3/connect/revoke")
224224
val oauthUrls = listOf(tokenUrlBuilder, revokeUrlBuilder)
225225
whenever(mockResponse.isSuccessful).thenReturn(false)
226-
whenever(mockResponse.code()).thenReturn(401)
226+
whenever(mockResponse.code).thenReturn(401)
227227
whenever(mockResponseBody.string()).thenReturn("{ \"error\": \"internal_error\", \"error_code\": 500, \"error_description\": \"Internal error, contact administrator\", \"error_uri\": \"https://accounts.nylas.io/#tag/Event-Codes\", \"request_id\": \"eccc9c3f-7150-48e1-965e-4f89714ab51a\" }")
228228

229229
for (urlBuilder in oauthUrls) {
@@ -247,7 +247,7 @@ class NylasClientTest {
247247
val revokeUrlBuilder = nylasClient.newUrlBuilder().addPathSegments("v3/connect/revoke")
248248
val oauthUrls = listOf(tokenUrlBuilder, revokeUrlBuilder)
249249
whenever(mockResponse.isSuccessful).thenReturn(false)
250-
whenever(mockResponse.code()).thenReturn(500)
250+
whenever(mockResponse.code).thenReturn(500)
251251
whenever(mockResponseBody.string()).thenReturn("not a json")
252252

253253
for (urlBuilder in oauthUrls) {
@@ -268,7 +268,7 @@ class NylasClientTest {
268268
fun `should throw NylasApiError on error from non-oauth endpoints`() {
269269
val urlBuilder = nylasClient.newUrlBuilder().addPathSegments("v3/some/other/path")
270270
whenever(mockResponse.isSuccessful).thenReturn(false)
271-
whenever(mockResponse.code()).thenReturn(400)
271+
whenever(mockResponse.code).thenReturn(400)
272272
whenever(mockResponseBody.string()).thenReturn("{ \"request_id\": \"4c2740b4-52a4-412e-bdee-49a6c6671b22\", \"error\": { \"type\": \"provider_error\", \"message\": \"Unauthorized\", \"provider_error\": { \"error\": \"Request had invalid authentication credentials.\" } } }")
273273

274274
val exception = assertFailsWith<NylasApiError> {
@@ -287,7 +287,7 @@ class NylasClientTest {
287287
fun `should throw NylasApiError on error from non-oauth endpoints even if unexpected response from API`() {
288288
val urlBuilder = nylasClient.newUrlBuilder().addPathSegments("v3/some/other/path")
289289
whenever(mockResponse.isSuccessful).thenReturn(false)
290-
whenever(mockResponse.code()).thenReturn(400)
290+
whenever(mockResponse.code).thenReturn(400)
291291
whenever(mockResponseBody.string()).thenReturn("not a json")
292292

293293
val exception = assertFailsWith<NylasApiError> {
@@ -327,7 +327,7 @@ class NylasClientTest {
327327
@Test
328328
fun `should handle unexpected null response body`() {
329329
val urlBuilder = nylasClient.newUrlBuilder()
330-
whenever(mockResponse.body()).thenReturn(null)
330+
whenever(mockResponse.body).thenReturn(null)
331331

332332
val exception = assertFailsWith<NylasApiError> {
333333
nylasClient.executeRequest(urlBuilder, NylasClient.HttpMethod.GET, null, String::class.java)
@@ -357,8 +357,8 @@ class NylasClientTest {
357357
val capturedRequest = requestCaptor.value
358358

359359
assertEquals(mockResponseBody, result)
360-
assertEquals(capturedRequest.url().toString(), "https://api.us.nylas.com/test/path")
361-
assertEquals(capturedRequest.method(), "GET")
360+
assertEquals(capturedRequest.url.toString(), "https://api.us.nylas.com/test/path")
361+
assertEquals(capturedRequest.method, "GET")
362362
}
363363

364364
@Test
@@ -377,8 +377,8 @@ class NylasClientTest {
377377

378378
verify(mockHttpClient).newCall(requestCaptor.capture())
379379
val capturedRequest = requestCaptor.value
380-
assertEquals(capturedRequest.url().toString(), "https://api.us.nylas.com/test/path")
381-
assertEquals(capturedRequest.method(), "POST")
380+
assertEquals(capturedRequest.url.toString(), "https://api.us.nylas.com/test/path")
381+
assertEquals(capturedRequest.method, "POST")
382382
}
383383

384384
@Test
@@ -401,8 +401,8 @@ class NylasClientTest {
401401
verify(mockHttpClient).newCall(requestCaptor.capture())
402402
val capturedRequest = requestCaptor.value
403403

404-
assertEquals(capturedRequest.url().toString(), "https://api.us.nylas.com/test/path?foo=bar&list=a&list=b&list=c&map=key1%3Avalue1&map=key2%3Avalue2")
405-
assertEquals(capturedRequest.method(), "GET")
404+
assertEquals(capturedRequest.url.toString(), "https://api.us.nylas.com/test/path?foo=bar&list=a&list=b&list=c&map=key1%3Avalue1&map=key2%3Avalue2")
405+
assertEquals(capturedRequest.method, "GET")
406406
}
407407

408408
@Test
@@ -414,10 +414,10 @@ class NylasClientTest {
414414

415415
verify(mockHttpClient).newCall(requestCaptor.capture())
416416
val capturedRequest = requestCaptor.value
417-
val requestBodyBuffer = capturedRequest.body().asString()
417+
val requestBodyBuffer = capturedRequest.body.asString()
418418

419-
assertEquals(capturedRequest.url().toString(), "https://api.us.nylas.com/test/path")
420-
assertEquals(capturedRequest.method(), "PUT")
419+
assertEquals(capturedRequest.url.toString(), "https://api.us.nylas.com/test/path")
420+
assertEquals(capturedRequest.method, "PUT")
421421
assertEquals(requestBodyBuffer, putBody)
422422
}
423423

@@ -430,10 +430,10 @@ class NylasClientTest {
430430

431431
verify(mockHttpClient).newCall(requestCaptor.capture())
432432
val capturedRequest = requestCaptor.value
433-
val requestBodyBuffer = capturedRequest.body().asString()
433+
val requestBodyBuffer = capturedRequest.body.asString()
434434

435-
assertEquals(capturedRequest.url().toString(), "https://api.us.nylas.com/test/path")
436-
assertEquals(capturedRequest.method(), "PATCH")
435+
assertEquals(capturedRequest.url.toString(), "https://api.us.nylas.com/test/path")
436+
assertEquals(capturedRequest.method, "PATCH")
437437
assertEquals(requestBodyBuffer, patchBody)
438438
}
439439

@@ -446,10 +446,10 @@ class NylasClientTest {
446446

447447
verify(mockHttpClient).newCall(requestCaptor.capture())
448448
val capturedRequest = requestCaptor.value
449-
val requestBodyBuffer = capturedRequest.body().asString()
449+
val requestBodyBuffer = capturedRequest.body.asString()
450450

451-
assertEquals(capturedRequest.url().toString(), "https://api.us.nylas.com/test/path")
452-
assertEquals(capturedRequest.method(), "POST")
451+
assertEquals(capturedRequest.url.toString(), "https://api.us.nylas.com/test/path")
452+
assertEquals(capturedRequest.method, "POST")
453453
assertEquals(requestBodyBuffer, postBody)
454454
}
455455

@@ -462,8 +462,8 @@ class NylasClientTest {
462462
verify(mockHttpClient).newCall(requestCaptor.capture())
463463
val capturedRequest = requestCaptor.value
464464

465-
assertEquals(capturedRequest.url().toString(), "https://api.us.nylas.com/test/path")
466-
assertEquals(capturedRequest.method(), "DELETE")
465+
assertEquals(capturedRequest.url.toString(), "https://api.us.nylas.com/test/path")
466+
assertEquals(capturedRequest.method, "DELETE")
467467
}
468468

469469
/**

src/test/kotlin/com/nylas/OkHttpBackwardsCompatibilityTest.kt

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import com.nylas.interceptors.ContentHeadersInterceptor
55
import com.nylas.interceptors.HttpLoggingInterceptor
66
import com.nylas.util.FileUtils.Companion.toStreamingRequestBody
77
import okhttp3.*
8+
import okhttp3.HttpUrl.Companion.toHttpUrl
9+
import okhttp3.MediaType.Companion.toMediaType
810
import okio.Buffer
911
import org.junit.jupiter.api.Assertions.*
1012
import org.junit.jupiter.api.BeforeEach
@@ -85,17 +87,17 @@ class OkHttpBackwardsCompatibilityTest {
8587
whenever(mockChain.request()).thenReturn(mockRequest)
8688
whenever(mockChain.proceed(org.mockito.kotlin.any())).thenReturn(mockResponse)
8789
whenever(mockRequest.newBuilder()).thenReturn(Request.Builder().url("https://test.com"))
88-
whenever(mockRequest.url()).thenReturn(HttpUrl.get("https://test.com")!!)
90+
whenever(mockRequest.url).thenReturn("https://test.com".toHttpUrl()!!)
8991
whenever(mockRequest.header(org.mockito.kotlin.any())).thenReturn(null)
90-
whenever(mockRequest.body()).thenReturn(null)
91-
whenever(mockRequest.headers()).thenReturn(Headers.of())
92-
whenever(mockRequest.method()).thenReturn("GET")
92+
whenever(mockRequest.body).thenReturn(null)
93+
whenever(mockRequest.headers).thenReturn(Headers.headersOf())
94+
whenever(mockRequest.method).thenReturn("GET")
9395

9496
// Setup response for logging
95-
whenever(mockResponse.code()).thenReturn(200)
96-
whenever(mockResponse.message()).thenReturn("OK")
97-
whenever(mockResponse.headers()).thenReturn(Headers.of())
98-
whenever(mockResponse.body()).thenReturn(null)
97+
whenever(mockResponse.code).thenReturn(200)
98+
whenever(mockResponse.message).thenReturn("OK")
99+
whenever(mockResponse.headers).thenReturn(Headers.headersOf())
100+
whenever(mockResponse.body).thenReturn(null)
99101
}
100102

101103
@Test
@@ -151,8 +153,8 @@ class OkHttpBackwardsCompatibilityTest {
151153
.build()
152154

153155
assertNotNull(request)
154-
assertEquals("GET", request.method())
155-
assertEquals("https://api.nylas.com/test", request.url().toString())
156+
assertEquals("GET", request.method)
157+
assertEquals("https://api.nylas.com/test", request.url.toString())
156158
assertEquals("Bearer test-token", request.header("Authorization"))
157159
}
158160

@@ -162,39 +164,39 @@ class OkHttpBackwardsCompatibilityTest {
162164
val mockBody: ResponseBody = mock()
163165
val mockHeaders: Headers = mock()
164166

165-
whenever(mockResponse.body()).thenReturn(mockBody)
166-
whenever(mockResponse.headers()).thenReturn(mockHeaders)
167-
whenever(mockResponse.code()).thenReturn(200)
168-
whenever(mockResponse.message()).thenReturn("OK")
167+
whenever(mockResponse.body).thenReturn(mockBody)
168+
whenever(mockResponse.headers).thenReturn(mockHeaders)
169+
whenever(mockResponse.code).thenReturn(200)
170+
whenever(mockResponse.message).thenReturn("OK")
169171
whenever(mockResponse.isSuccessful).thenReturn(true)
170172

171173
// Basic response operations should work
172-
assertEquals(200, mockResponse.code())
173-
assertEquals("OK", mockResponse.message())
174+
assertEquals(200, mockResponse.code)
175+
assertEquals("OK", mockResponse.message)
174176
assertTrue(mockResponse.isSuccessful)
175-
assertNotNull(mockResponse.body())
176-
assertNotNull(mockResponse.headers())
177+
assertNotNull(mockResponse.body)
178+
assertNotNull(mockResponse.headers)
177179
}
178180

179181
@Test
180182
fun `MediaType operations work correctly`() {
181-
// Test MediaType.parse - this should become String.toMediaType() in OkHttp 4
182-
val mediaType = MediaType.parse("application/json")
183+
// Test ".toMediaType - this should become String.toMediaType() in OkHttp 4
184+
val mediaType = "application/json".toMediaType()
183185
assertNotNull(mediaType)
184186

185187
// Test common media type operations
186-
assertNotNull(MediaType.parse("text/plain"))
187-
assertNotNull(MediaType.parse("multipart/form-data"))
188+
assertNotNull("text/plain".toMediaType())
189+
assertNotNull("multipart/form-data".toMediaType())
188190
}
189191

190192
@Test
191193
fun `RequestBody creation works correctly`() {
192-
val jsonMediaType = MediaType.parse("application/json")
194+
val jsonMediaType = "application/json".toMediaType()
193195
val body = RequestBody.create(jsonMediaType, """{"test": "data"}""")
194196

195197
assertNotNull(body)
196198
// MediaType might include charset, so check that it starts with our media type
197-
assertTrue(body.contentType.toString().startsWith("application/json"))
199+
assertTrue(body.contentType().toString().startsWith("application/json"))
198200
assertTrue(body.contentLength() > 0)
199201
}
200202

@@ -207,8 +209,8 @@ class OkHttpBackwardsCompatibilityTest {
207209
.build()
208210

209211
assertNotNull(multipartBody)
210-
assertEquals(MultipartBody.FORM, multipartBody.type())
211-
assertEquals(2, multipartBody.size())
212+
assertEquals(MultipartBody.FORM, multipartBody.type)
213+
assertEquals(2, multipartBody.size)
212214
}
213215
}
214216

@@ -219,7 +221,7 @@ class OkHttpBackwardsCompatibilityTest {
219221
fun `InputStream toStreamingRequestBody extension works correctly`() {
220222
val testData = "test file content"
221223
val inputStream = ByteArrayInputStream(testData.toByteArray())
222-
val mediaType = MediaType.parse("text/plain")
224+
val mediaType = "text/plain".toMediaType()
223225

224226
val requestBody = inputStream.toStreamingRequestBody(mediaType)
225227

@@ -237,7 +239,7 @@ class OkHttpBackwardsCompatibilityTest {
237239
fun `MultipartBody form building with file attachments works`() {
238240
val testContent = "file content"
239241
val inputStream = ByteArrayInputStream(testContent.toByteArray())
240-
val mediaType = MediaType.parse("text/plain")
242+
val mediaType = "text/plain".toMediaType()
241243

242244
val requestBody = inputStream.toStreamingRequestBody(mediaType)
243245

@@ -248,8 +250,8 @@ class OkHttpBackwardsCompatibilityTest {
248250
.build()
249251

250252
assertNotNull(multipartBody)
251-
assertEquals(MultipartBody.FORM, multipartBody.type())
252-
assertEquals(2, multipartBody.size())
253+
assertEquals(MultipartBody.FORM, multipartBody.type)
254+
assertEquals(2, multipartBody.size)
253255
}
254256
}
255257

@@ -263,7 +265,7 @@ class OkHttpBackwardsCompatibilityTest {
263265
.build()
264266

265267
assertNotNull(httpClient)
266-
assertEquals(listOf(Protocol.HTTP_1_1), httpClient.protocols())
268+
assertEquals(listOf(Protocol.HTTP_1_1), httpClient.protocols)
267269
}
268270

269271
@Test
@@ -276,10 +278,10 @@ class OkHttpBackwardsCompatibilityTest {
276278
.build()
277279

278280
assertNotNull(httpClient)
279-
assertEquals(30000, httpClient.connectTimeoutMillis())
280-
assertEquals(60000, httpClient.readTimeoutMillis())
281-
assertEquals(45000, httpClient.writeTimeoutMillis())
282-
assertEquals(120000, httpClient.callTimeoutMillis())
281+
assertEquals(30000, httpClient.connectTimeoutMillis)
282+
assertEquals(60000, httpClient.readTimeoutMillis)
283+
assertEquals(45000, httpClient.writeTimeoutMillis)
284+
assertEquals(120000, httpClient.callTimeoutMillis)
283285
}
284286

285287
@Test
@@ -292,8 +294,8 @@ class OkHttpBackwardsCompatibilityTest {
292294
.build()
293295

294296
assertNotNull(httpClient)
295-
assertTrue(httpClient.interceptors().contains(interceptor))
296-
assertTrue(httpClient.networkInterceptors().contains(interceptor))
297+
assertTrue(httpClient.interceptors.contains(interceptor))
298+
assertTrue(httpClient.networkInterceptors.contains(interceptor))
297299
}
298300
}
299301
}

src/test/kotlin/com/nylas/OkHttpKotlinTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class OkHttpKotlinTest {
2222
assertNotNull(body)
2323

2424
// Test contentType property access (this is what was failing)
25-
val contentType = body.contentType
25+
val contentType = body.contentType()
2626
assertNotNull(contentType)
2727
assertEquals("application/json", contentType!!.type)
2828
assertEquals("json", contentType.subtype)

0 commit comments

Comments
 (0)