11package examples.kotlin.ktor.resourceserver
22
33import io.kotest.matchers.shouldBe
4- import io.ktor.http.HttpMethod
4+ import io.ktor.client.request.get
5+ import io.ktor.client.request.header
6+ import io.ktor.client.statement.bodyAsText
57import io.ktor.http.HttpStatusCode
6- import io.ktor.server.testing.handleRequest
7- import io.ktor.server.testing.withTestApplication
8+ import io.ktor.server.testing.testApplication
89import no.nav.security.mock.oauth2.MockOAuth2Server
910import no.nav.security.mock.oauth2.withMockOAuth2Server
1011import org.junit.jupiter.api.Test
@@ -15,14 +16,13 @@ class OAuth2ResourceServerAppTest {
1516 fun `http get to secured endpoint without token should return 401` () {
1617 withMockOAuth2Server {
1718 val authConfig = authConfig()
18- withTestApplication({
19- module(authConfig)
20- }) {
21- with (
22- handleRequest(HttpMethod .Get , " /hello1" )
23- ) {
24- response.status() shouldBe HttpStatusCode .Unauthorized
19+
20+ testApplication {
21+ this .application {
22+ module(authConfig)
2523 }
24+ val response = client.get(" /hello1" )
25+ response.status shouldBe HttpStatusCode .Unauthorized
2626 }
2727 }
2828 }
@@ -32,17 +32,16 @@ class OAuth2ResourceServerAppTest {
3232 withMockOAuth2Server {
3333 val mockOAuth2Server = this
3434 val authConfig = authConfig()
35- withTestApplication({
36- module(authConfig)
37- }) {
38- with (
39- handleRequest(HttpMethod .Get , " /hello1" ) {
40- addHeader(" Authorization" , " Bearer ${mockOAuth2Server.tokenFromProvider1()} " )
41- }
42- ) {
43- response.status() shouldBe HttpStatusCode .OK
44- response.content shouldBe " hello1 foo from issuer ${mockOAuth2Server.issuerUrl(" provider1" )} "
35+ testApplication {
36+ this .application {
37+ module(authConfig)
38+ }
39+ val response = client.get(" /hello1" ){
40+ header(" Authorization" , " Bearer ${mockOAuth2Server.tokenFromProvider1()} " )
4541 }
42+
43+ response.status shouldBe HttpStatusCode .OK
44+ response.bodyAsText() shouldBe " hello1 foo from issuer ${mockOAuth2Server.issuerUrl(" provider1" )} "
4645 }
4746 }
4847 }
@@ -52,17 +51,16 @@ class OAuth2ResourceServerAppTest {
5251 withMockOAuth2Server {
5352 val mockOAuth2Server = this
5453 val authConfig = authConfig()
55- withTestApplication({
56- module(authConfig)
57- }) {
58- with (
59- handleRequest(HttpMethod .Get , " /hello2" ) {
60- addHeader(" Authorization" , " Bearer ${mockOAuth2Server.tokenFromProvider2()} " )
61- }
62- ) {
63- response.status() shouldBe HttpStatusCode .OK
64- response.content shouldBe " hello2 foo from issuer ${mockOAuth2Server.issuerUrl(" provider2" )} "
54+ testApplication {
55+ this .application {
56+ module(authConfig)
57+ }
58+
59+ val response = client.get(" /hello2" ){
60+ header(" Authorization" , " Bearer ${mockOAuth2Server.tokenFromProvider2()} " )
6561 }
62+ response.status shouldBe HttpStatusCode .OK
63+ response.bodyAsText() shouldBe " hello2 foo from issuer ${mockOAuth2Server.issuerUrl(" provider2" )} "
6664 }
6765 }
6866 }
0 commit comments