Skip to content

Commit f81d8fa

Browse files
committed
Dropped usage of withTestApplication due to deprecated
1 parent 7e9ccd7 commit f81d8fa

File tree

1 file changed

+28
-30
lines changed

1 file changed

+28
-30
lines changed

src/test/kotlin/examples/kotlin/ktor/resourceserver/OAuth2ResourceServerAppTest.kt

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package examples.kotlin.ktor.resourceserver
22

33
import 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
57
import 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
89
import no.nav.security.mock.oauth2.MockOAuth2Server
910
import no.nav.security.mock.oauth2.withMockOAuth2Server
1011
import 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

Comments
 (0)