1
1
package examples.kotlin.ktor.resourceserver
2
2
3
3
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
5
7
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
8
9
import no.nav.security.mock.oauth2.MockOAuth2Server
9
10
import no.nav.security.mock.oauth2.withMockOAuth2Server
10
11
import org.junit.jupiter.api.Test
@@ -15,14 +16,13 @@ class OAuth2ResourceServerAppTest {
15
16
fun `http get to secured endpoint without token should return 401` () {
16
17
withMockOAuth2Server {
17
18
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)
25
23
}
24
+ val response = client.get(" /hello1" )
25
+ response.status shouldBe HttpStatusCode .Unauthorized
26
26
}
27
27
}
28
28
}
@@ -32,17 +32,16 @@ class OAuth2ResourceServerAppTest {
32
32
withMockOAuth2Server {
33
33
val mockOAuth2Server = this
34
34
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()} " )
45
41
}
42
+
43
+ response.status shouldBe HttpStatusCode .OK
44
+ response.bodyAsText() shouldBe " hello1 foo from issuer ${mockOAuth2Server.issuerUrl(" provider1" )} "
46
45
}
47
46
}
48
47
}
@@ -52,17 +51,16 @@ class OAuth2ResourceServerAppTest {
52
51
withMockOAuth2Server {
53
52
val mockOAuth2Server = this
54
53
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()} " )
65
61
}
62
+ response.status shouldBe HttpStatusCode .OK
63
+ response.bodyAsText() shouldBe " hello2 foo from issuer ${mockOAuth2Server.issuerUrl(" provider2" )} "
66
64
}
67
65
}
68
66
}
0 commit comments