@@ -11,60 +11,74 @@ import java.io.IOException
11
11
import java.net.InetAddress
12
12
import java.net.URI
13
13
import java.util.UUID
14
- import java.util.concurrent.BlockingQueue
15
- import java.util.concurrent.LinkedBlockingQueue
16
- import no.nav.security.mock.oauth2.extensions.asOAuth2HttpRequest
14
+ import mu.KotlinLogging
17
15
import no.nav.security.mock.oauth2.extensions.toAuthorizationEndpointUrl
18
16
import no.nav.security.mock.oauth2.extensions.toEndSessionEndpointUrl
19
17
import no.nav.security.mock.oauth2.extensions.toJwksUrl
20
18
import no.nav.security.mock.oauth2.extensions.toTokenEndpointUrl
21
19
import no.nav.security.mock.oauth2.extensions.toWellKnownUrl
20
+ import no.nav.security.mock.oauth2.http.MockWebServerWrapper
22
21
import no.nav.security.mock.oauth2.http.OAuth2HttpRequestHandler
23
- import no.nav.security.mock.oauth2.http.OAuth2HttpResponse
22
+ import no.nav.security.mock.oauth2.http.OAuth2HttpRouter
23
+ import no.nav.security.mock.oauth2.http.OAuth2HttpRouter.Companion.routes
24
+ import no.nav.security.mock.oauth2.http.Route
25
+ import no.nav.security.mock.oauth2.http.route
24
26
import no.nav.security.mock.oauth2.token.DefaultOAuth2TokenCallback
25
27
import no.nav.security.mock.oauth2.token.OAuth2TokenCallback
26
28
import okhttp3.HttpUrl
27
- import okhttp3.mockwebserver.Dispatcher
28
29
import okhttp3.mockwebserver.MockResponse
29
- import okhttp3.mockwebserver.MockWebServer
30
30
import okhttp3.mockwebserver.RecordedRequest
31
31
32
- // TODO make open so others can extend?
32
+ private val log = KotlinLogging .logger { }
33
+
33
34
@Suppress(" unused" , " MemberVisibilityCanBePrivate" )
34
- class MockOAuth2Server (
35
- val config : OAuth2Config = OAuth2Config ()
35
+ open class MockOAuth2Server (
36
+ val config : OAuth2Config = OAuth2Config (),
37
+ vararg additionalRoutes : Route
36
38
) {
37
- private val mockWebServer : MockWebServer = MockWebServer ( )
39
+ constructor ( vararg additionalRoutes : Route ) : this (config = OAuth2Config (), additionalRoutes = additionalRoutes )
38
40
39
- var dispatcher: Dispatcher = MockOAuth2Dispatcher (config)
41
+ private val httpServer = config.httpServer
42
+ private val defaultRequestHandler: OAuth2HttpRequestHandler = OAuth2HttpRequestHandler (config)
43
+ private val router: OAuth2HttpRouter = routes(
44
+ * additionalRoutes,
45
+ route(" " ) {
46
+ defaultRequestHandler.handleRequest(it)
47
+ }
48
+ )
40
49
41
50
@JvmOverloads
42
51
@Throws(IOException ::class )
43
- fun start (
44
- inetAddress : InetAddress = InetAddress .getByName("localhost"),
45
- port : Int = 0
46
- ) {
47
- mockWebServer.start(inetAddress, port)
48
- mockWebServer.dispatcher = dispatcher
52
+ fun start (inetAddress : InetAddress = InetAddress .getByName("localhost"), port : Int = 0) {
53
+ log.debug(" attempt to start server on port=$port " )
54
+ httpServer.start(inetAddress, port, router)
49
55
}
50
56
51
57
@Throws(IOException ::class )
52
58
fun shutdown () {
53
- mockWebServer.shutdown()
59
+ httpServer.stop()
60
+ }
61
+
62
+ fun url (path : String ): HttpUrl = httpServer.url(path)
63
+
64
+ @Deprecated(" Use MockWebServer method/function instead" , ReplaceWith (" MockWebServer.enqueue()" ))
65
+ fun enqueueResponse (response : MockResponse ) {
66
+ throw UnsupportedOperationException (" cannot enqueue MockResponse, please use the MockWebServer directly with QueueDispatcher" )
54
67
}
55
68
56
- fun url (path : String ): HttpUrl = mockWebServer.url(path)
57
- fun enqueueResponse (response : MockResponse ) = (dispatcher as MockOAuth2Dispatcher ).enqueueResponse(response)
58
- fun enqueueCallback (oAuth2TokenCallback : OAuth2TokenCallback ) = (dispatcher as MockOAuth2Dispatcher ).enqueueTokenCallback(oAuth2TokenCallback)
59
- fun takeRequest (): RecordedRequest = mockWebServer.takeRequest()
69
+ fun enqueueCallback (oAuth2TokenCallback : OAuth2TokenCallback ) = defaultRequestHandler.enqueueTokenCallback(oAuth2TokenCallback)
70
+
71
+ fun takeRequest (): RecordedRequest =
72
+ (httpServer as ? MockWebServerWrapper )?.mockWebServer?.takeRequest()
73
+ ? : throw UnsupportedOperationException (" can only takeRequest when httpServer is of type MockWebServer" )
60
74
61
- fun wellKnownUrl (issuerId : String ): HttpUrl = mockWebServer. url(issuerId).toWellKnownUrl()
62
- fun tokenEndpointUrl (issuerId : String ): HttpUrl = mockWebServer. url(issuerId).toTokenEndpointUrl()
63
- fun jwksUrl (issuerId : String ): HttpUrl = mockWebServer. url(issuerId).toJwksUrl()
64
- fun issuerUrl (issuerId : String ): HttpUrl = mockWebServer. url(issuerId)
65
- fun authorizationEndpointUrl (issuerId : String ): HttpUrl = mockWebServer. url(issuerId).toAuthorizationEndpointUrl()
66
- fun endSessionEndpointUrl (issuerId : String ): HttpUrl = mockWebServer. url(issuerId).toEndSessionEndpointUrl()
67
- fun baseUrl (): HttpUrl = mockWebServer. url(" " )
75
+ fun wellKnownUrl (issuerId : String ): HttpUrl = url(issuerId).toWellKnownUrl()
76
+ fun tokenEndpointUrl (issuerId : String ): HttpUrl = url(issuerId).toTokenEndpointUrl()
77
+ fun jwksUrl (issuerId : String ): HttpUrl = url(issuerId).toJwksUrl()
78
+ fun issuerUrl (issuerId : String ): HttpUrl = url(issuerId)
79
+ fun authorizationEndpointUrl (issuerId : String ): HttpUrl = url(issuerId).toAuthorizationEndpointUrl()
80
+ fun endSessionEndpointUrl (issuerId : String ): HttpUrl = url(issuerId).toEndSessionEndpointUrl()
81
+ fun baseUrl (): HttpUrl = url(" " )
68
82
69
83
fun issueToken (issuerId : String , clientId : String , tokenCallback : OAuth2TokenCallback ): SignedJWT {
70
84
val uri = tokenEndpointUrl(issuerId)
@@ -97,29 +111,6 @@ class MockOAuth2Server(
97
111
)
98
112
}
99
113
100
- class MockOAuth2Dispatcher (
101
- config : OAuth2Config
102
- ) : Dispatcher() {
103
- private val httpRequestHandler: OAuth2HttpRequestHandler = OAuth2HttpRequestHandler (config)
104
- private val responseQueue: BlockingQueue <MockResponse > = LinkedBlockingQueue ()
105
-
106
- fun enqueueResponse (mockResponse : MockResponse ) = responseQueue.add(mockResponse)
107
- fun enqueueTokenCallback (oAuth2TokenCallback : OAuth2TokenCallback ) = httpRequestHandler.enqueueTokenCallback(oAuth2TokenCallback)
108
-
109
- override fun dispatch (request : RecordedRequest ): MockResponse =
110
- responseQueue.peek()?.let {
111
- responseQueue.take()
112
- } ? : mockResponse(httpRequestHandler.handleRequest(request.asOAuth2HttpRequest()))
113
-
114
- private fun mockResponse (response : OAuth2HttpResponse ): MockResponse =
115
- MockResponse ()
116
- .setHeaders(response.headers)
117
- .setResponseCode(response.status)
118
- .apply {
119
- response.body?.let { this .setBody(it) }
120
- }
121
- }
122
-
123
114
fun <R > withMockOAuth2Server (
124
115
test : MockOAuth2Server .() -> R
125
116
): R {
0 commit comments