Skip to content

Commit 723a28a

Browse files
authored
fix: add static check for incompatible mockwebserver version (navikt#142)
1 parent 5beaf7a commit 723a28a

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/main/kotlin/no/nav/security/mock/oauth2/MockOAuth2Server.kt

+24
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import no.nav.security.mock.oauth2.token.OAuth2TokenCallback
2929
import okhttp3.HttpUrl
3030
import okhttp3.mockwebserver.MockResponse
3131
import okhttp3.mockwebserver.RecordedRequest
32+
import okio.Buffer
3233
import java.io.IOException
3334
import java.net.InetAddress
3435
import java.net.URI
@@ -139,6 +140,29 @@ open class MockOAuth2Server(
139140
)
140141
)
141142
}
143+
144+
companion object {
145+
/**
146+
* This attempts to reference a method that does not exist in com.squareup.okio:okio < 2.4.0,
147+
* and incidentally also com.squareup.okhttp3:mockwebserver < 4.3.0.
148+
*
149+
* The method is required by mock-oauth2-server, see [no.nav.security.mock.oauth2.extensions.RecordedRequest.asOAuth2HttpRequest()].
150+
*
151+
* If the block throws a RuntimeException, an incompatible version of the okio library was included in the classpath.
152+
*
153+
* This is true for e.g. Spring Boot projects, which as of version 2.6.1 still uses mockwebserver 3.14.9 as the
154+
* [default managed dependency version](https://docs.spring.io/spring-boot/docs/2.6.1/reference/html/dependency-versions.html).
155+
*
156+
* We recommend that users of this library use a matching version of mockwebserver.
157+
*/
158+
init {
159+
try {
160+
Buffer().copy()
161+
} catch (e: NoSuchMethodError) {
162+
throw RuntimeException("Unsupported version of com.squareup.okhttp3:mockwebserver in classpath. Version should be >= 4.9.2", e)
163+
}
164+
}
165+
}
142166
}
143167

144168
internal fun Map<String, Any>.toJwtClaimsSet(): JWTClaimsSet =

0 commit comments

Comments
 (0)