Skip to content

Commit

Permalink
fix: bump okhttp dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
nickmcdowall committed Oct 21, 2023
1 parent 47605db commit 90a850b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
2 changes: 1 addition & 1 deletion lsd-interceptors/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ dependencies {
implementation 'org.springframework:spring-web:5.3.16.RELEASE'
implementation 'org.springframework:spring-messaging:5.3.23'
implementation 'org.springframework.integration:spring-integration-core:5.5.15'
implementation 'com.squareup.okhttp3:okhttp:3.8.1'
implementation 'com.squareup.okhttp3:okhttp:4.12.0'

testCompileOnly 'org.springframework:spring-web:5.1.11.RELEASE'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ data class LsdOkHttpInterceptor(
override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request()
val requestCopy = request.newBuilder().build()
val path = request.url().encodedPath()
val requestHeaders = singleValueMap(request.headers().toMultimap())
val path = request.url.encodedPath
val requestHeaders = singleValueMap(request.headers.toMultimap())
handlers.forEach(Consumer { handler: HttpInteractionHandler ->
handler.handleRequest(
request.method(),
request.method,
requestHeaders,
path,
bodyToString(requestCopy)
Expand All @@ -35,10 +35,10 @@ data class LsdOkHttpInterceptor(
val response = chain.proceed(request)

val duration = Duration.ofMillis(System.currentTimeMillis() - start)
val responseHeaders = singleValueMap(response.headers().toMultimap())
val responseHeaders = singleValueMap(response.headers.toMultimap())
handlers.forEach(Consumer { handler: HttpInteractionHandler ->
handler.handleResponse(
response.code().toString() + " " + response.message(),
response.code.toString() + " " + response.message,
requestHeaders,
responseHeaders,
path,
Expand All @@ -56,9 +56,10 @@ data class LsdOkHttpInterceptor(
return response.peekBody(RESPONSE_MAXY_BYTES.toLong()).string()
}

private fun bodyToString(copy: Request): String {
val buffer = Buffer()
copy.body()!!.writeTo(buffer)
return buffer.readUtf8()
}
private fun bodyToString(request: Request): String =
request.body?.let {
val buffer = Buffer()
it.writeTo(buffer)
buffer.readUtf8()
} ?: ""
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import okhttp3.*
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody.Companion.toRequestBody
import okio.Buffer
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import java.io.IOException
import java.time.Duration

class LsdOkHttpInterceptorTest {
Expand All @@ -23,15 +24,13 @@ class LsdOkHttpInterceptorTest {
private lateinit var okHttpInterceptor: Interceptor

@BeforeEach
@Throws(IOException::class)
fun setUp() {
okHttpInterceptor = LsdOkHttpInterceptor(listOf(handler))
every { chain.request() } returns requestFor("PUT", "/user")
every { chain.proceed(any()) } returns okResponse
}

@Test
@Throws(IOException::class)
fun delegatesMessageHandling() {
okHttpInterceptor.intercept(chain)
verify { handler.handleRequest("PUT", emptyMap(), "/user", requestBodyString) }
Expand All @@ -48,33 +47,34 @@ class LsdOkHttpInterceptorTest {
}

@Test
@Throws(IOException::class)
fun requestIsStillIntactAfterIntercept() {
okHttpInterceptor.intercept(chain)
val buffer = Buffer()
chain.request().body()!!.writeTo(buffer)

chain.request().body!!.writeTo(buffer)

assertThat(buffer.readUtf8()).isEqualTo(requestBodyString)
}

@Test
@Throws(IOException::class)
fun returnsExpectedResponse() {
val response = okHttpInterceptor.intercept(chain)
assertThat(response).isEqualTo(okResponse)
}

@Test
@Throws(IOException::class)
fun doesNotCloseResponseBody() {
every { chain.proceed(any()) } returns anOkResponse()

val response = okHttpInterceptor.intercept(chain)
assertThat(response.body()!!.string()).isEqualTo(responseBodyString)

assertThat(response.body!!.string()).isEqualTo(responseBodyString)
}

private fun requestFor(method: String, path: String): Request {
return Request.Builder()
.url("https://localhost:8080$path")
.method(method, RequestBody.create(MEDIA_TYPE, requestBodyString))
.method(method, requestBodyString.toRequestBody(MEDIA_TYPE))
.build()
}

Expand All @@ -89,6 +89,6 @@ class LsdOkHttpInterceptorTest {
}

companion object {
val MEDIA_TYPE = MediaType.parse("application/json")
val MEDIA_TYPE = "application/json".toMediaTypeOrNull()
}
}

0 comments on commit 90a850b

Please sign in to comment.