Skip to content

Commit e4708d1

Browse files
committed
feat: add timeout to MockOAuth2Server.takeRequest()
* avoid waiting for request indefinitely
1 parent b78660a commit e4708d1

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

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

+7-3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ import no.nav.security.mock.oauth2.token.OAuth2TokenCallback
3232
import okhttp3.HttpUrl
3333
import okhttp3.mockwebserver.MockResponse
3434
import okhttp3.mockwebserver.RecordedRequest
35+
import java.lang.RuntimeException
36+
import java.util.concurrent.TimeUnit
3537

3638
private val log = KotlinLogging.logger { }
3739

@@ -75,9 +77,11 @@ open class MockOAuth2Server(
7577

7678
fun enqueueCallback(oAuth2TokenCallback: OAuth2TokenCallback) = defaultRequestHandler.enqueueTokenCallback(oAuth2TokenCallback)
7779

78-
fun takeRequest(): RecordedRequest =
79-
(httpServer as? MockWebServerWrapper)?.mockWebServer?.takeRequest()
80-
?: throw UnsupportedOperationException("can only takeRequest when httpServer is of type MockWebServer")
80+
@JvmOverloads
81+
fun takeRequest(timeout: Long = 2, unit: TimeUnit = TimeUnit.SECONDS): RecordedRequest =
82+
(httpServer as? MockWebServerWrapper)?.mockWebServer?.let {
83+
it.takeRequest(timeout, unit) ?: throw RuntimeException("no request found in queue within timeout $timeout $unit")
84+
} ?: throw UnsupportedOperationException("can only takeRequest when httpServer is of type MockWebServer")
8185

8286
fun wellKnownUrl(issuerId: String): HttpUrl = url(issuerId).toWellKnownUrl()
8387
fun tokenEndpointUrl(issuerId: String): HttpUrl = url(issuerId).toTokenEndpointUrl()

src/test/kotlin/no/nav/security/mock/oauth2/MockOAuth2ServerTest.kt

+12
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import com.nimbusds.jwt.JWTClaimsSet
77
import com.nimbusds.jwt.SignedJWT
88
import com.nimbusds.oauth2.sdk.GrantType
99
import com.nimbusds.oauth2.sdk.id.Issuer
10+
import io.kotest.assertions.throwables.shouldThrow
1011
import io.kotest.matchers.maps.shouldContainAll
1112
import io.kotest.matchers.shouldBe
1213
import io.kotest.matchers.string.shouldStartWith
@@ -37,6 +38,7 @@ import org.junit.jupiter.api.AfterEach
3738
import org.junit.jupiter.api.BeforeEach
3839
import org.junit.jupiter.api.Disabled
3940
import org.junit.jupiter.api.Test
41+
import java.util.concurrent.TimeUnit
4042

4143
// TODO add more tests for exception handling
4244
class MockOAuth2ServerTest {
@@ -414,6 +416,16 @@ class MockOAuth2ServerTest {
414416
)
415417
}
416418

419+
@Test
420+
fun `takeRequest should time out if no request is received`(){
421+
shouldThrow<java.lang.RuntimeException> {
422+
server.takeRequest(5, TimeUnit.MILLISECONDS)
423+
}
424+
val url = server.wellKnownUrl("1")
425+
client.get(url)
426+
server.takeRequest().requestUrl shouldBe url
427+
}
428+
417429
private fun retrieveJwks(jwksUri: String): JWKSet {
418430
return client.newCall(
419431
Request.Builder()

0 commit comments

Comments
 (0)