Skip to content

Commit

Permalink
fix: copy contents of request body when converting to OAuth2HttpRequest
Browse files Browse the repository at this point in the history
* current implementation read contents of buffer, making it impossible to get the request body from the MockWebServer.takeRequest() as it had already been consumed
  • Loading branch information
tommytroen committed Dec 14, 2020
1 parent b40a43d commit fd38227
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ import no.nav.security.mock.oauth2.http.OAuth2HttpRequest
import okhttp3.mockwebserver.RecordedRequest

fun RecordedRequest.asOAuth2HttpRequest(): OAuth2HttpRequest =
OAuth2HttpRequest(this.headers, this.method!!, this.requestUrl!!, this.body.readUtf8())
OAuth2HttpRequest(this.headers, this.method!!, this.requestUrl!!, this.body.copy().readUtf8())
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import com.nimbusds.jwt.JWTClaimsSet
import com.nimbusds.jwt.SignedJWT
import com.nimbusds.oauth2.sdk.GrantType
import com.nimbusds.oauth2.sdk.id.Issuer
import io.kotest.matchers.shouldBe
import java.net.URLEncoder
import no.nav.security.mock.oauth2.extensions.verifySignatureAndIssuer
import no.nav.security.mock.oauth2.http.OAuth2TokenResponse
import no.nav.security.mock.oauth2.http.WellKnown
import no.nav.security.mock.oauth2.testutils.post
import no.nav.security.mock.oauth2.token.DefaultOAuth2TokenCallback
import no.nav.security.mock.oauth2.token.OAuth2TokenProvider
import okhttp3.Credentials
Expand Down Expand Up @@ -314,6 +316,35 @@ class MockOAuth2ServerTest {
assertThat(accessToken.jwtClaimsSet.issuer).endsWith("default")
}

@Test
fun takeRequestShouldReturnReceivedRequest() {
server.enqueueResponse(MockResponse().setResponseCode(200).setBody("ok"))
client.post(
server.baseUrl(),
mapOf(
"param1" to "value1"
)
).body?.close()

val recordedRequest1 = server.takeRequest()
recordedRequest1.requestUrl shouldBe server.baseUrl()
recordedRequest1.body.readUtf8() shouldBe "param1=value1"

client.post(
server.tokenEndpointUrl("test"),
mapOf(
"client_id" to "client",
"client_secret" to "sec",
"grant_type" to "client_credentials",
"scope" to "scope1"
)
).body?.close()

val recordedRequest2 = server.takeRequest()
recordedRequest2.requestUrl shouldBe server.tokenEndpointUrl("test")
recordedRequest2.body.readUtf8() shouldBe "client_id=client&client_secret=sec&grant_type=client_credentials&scope=scope1"
}

@Test
fun issueTokenDirectlyFromMockOAuth2Server() {
val signedJWT: SignedJWT = server.issueToken(
Expand Down

0 comments on commit fd38227

Please sign in to comment.