File tree 2 files changed +19
-3
lines changed
main/kotlin/no/nav/security/mock/oauth2
test/kotlin/no/nav/security/mock/oauth2
2 files changed +19
-3
lines changed Original file line number Diff line number Diff line change @@ -32,6 +32,8 @@ import no.nav.security.mock.oauth2.token.OAuth2TokenCallback
32
32
import okhttp3.HttpUrl
33
33
import okhttp3.mockwebserver.MockResponse
34
34
import okhttp3.mockwebserver.RecordedRequest
35
+ import java.lang.RuntimeException
36
+ import java.util.concurrent.TimeUnit
35
37
36
38
private val log = KotlinLogging .logger { }
37
39
@@ -75,9 +77,11 @@ open class MockOAuth2Server(
75
77
76
78
fun enqueueCallback (oAuth2TokenCallback : OAuth2TokenCallback ) = defaultRequestHandler.enqueueTokenCallback(oAuth2TokenCallback)
77
79
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" )
81
85
82
86
fun wellKnownUrl (issuerId : String ): HttpUrl = url(issuerId).toWellKnownUrl()
83
87
fun tokenEndpointUrl (issuerId : String ): HttpUrl = url(issuerId).toTokenEndpointUrl()
Original file line number Diff line number Diff line change @@ -7,6 +7,7 @@ import com.nimbusds.jwt.JWTClaimsSet
7
7
import com.nimbusds.jwt.SignedJWT
8
8
import com.nimbusds.oauth2.sdk.GrantType
9
9
import com.nimbusds.oauth2.sdk.id.Issuer
10
+ import io.kotest.assertions.throwables.shouldThrow
10
11
import io.kotest.matchers.maps.shouldContainAll
11
12
import io.kotest.matchers.shouldBe
12
13
import io.kotest.matchers.string.shouldStartWith
@@ -37,6 +38,7 @@ import org.junit.jupiter.api.AfterEach
37
38
import org.junit.jupiter.api.BeforeEach
38
39
import org.junit.jupiter.api.Disabled
39
40
import org.junit.jupiter.api.Test
41
+ import java.util.concurrent.TimeUnit
40
42
41
43
// TODO add more tests for exception handling
42
44
class MockOAuth2ServerTest {
@@ -414,6 +416,16 @@ class MockOAuth2ServerTest {
414
416
)
415
417
}
416
418
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
+
417
429
private fun retrieveJwks (jwksUri : String ): JWKSet {
418
430
return client.newCall(
419
431
Request .Builder ()
You can’t perform that action at this time.
0 commit comments