Skip to content

Commit

Permalink
Endre test oppsett for ktor 3
Browse files Browse the repository at this point in the history
  • Loading branch information
LudvigHz committed Oct 11, 2024
1 parent dd993ec commit 66854f7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ object KtorServer {
factory: ApplicationEngineFactory<TEngine, TConfiguration>,
port: Int = 8080,
application: Application.() -> Unit,
): TEngine {
): EmbeddedServer<TEngine, TConfiguration> {
val server = embeddedServer(factory, port) {
application()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,49 @@
package no.nav.personoversikt.common.ktor.utils

import io.ktor.client.request.*
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.testing.*
import io.ktor.util.pipeline.*
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test

internal class SecurityTest {
@Test
internal fun `should use auth-header if present`() {
val call = createCall {
this.addHeader(HttpHeaders.Authorization, "Bearer headertoken")
this.addHeader(HttpHeaders.Cookie, "test=cookietoken;other=othertoken")
testIntercept({
headers {
append(HttpHeaders.Authorization, "Bearer headertoken")
append(HttpHeaders.Cookie, "test=cookietoken;other=othertoken")
}
}
val security = Security(
Security.AuthProviderConfig(
name = null,
jwksConfig = Security.JwksConfig.JwksUrl("http://localhost.com", "issuer"),
tokenLocations = listOf(
Security.TokenLocation.Cookie(name = "notfound"),
Security.TokenLocation.Header(),
Security.TokenLocation.Cookie(name = "test"),
) {
val security = Security(
Security.AuthProviderConfig(
name = null,
jwksConfig = Security.JwksConfig.JwksUrl("http://localhost.com", "issuer"),
tokenLocations = listOf(
Security.TokenLocation.Cookie(name = "notfound"),
Security.TokenLocation.Header(),
Security.TokenLocation.Cookie(name = "test"),
)
)
)
)
val token = security.getToken(call)
val token = security.getToken(call)

assertEquals(listOf("Bearer headertoken"), token)
assertEquals(listOf("Bearer headertoken"), token)
}
}

@Test
internal fun `should use first non-null cookie value`() {
val call = createCall {
this.addHeader(HttpHeaders.Authorization, "Bearer headertoken")
this.addHeader(HttpHeaders.Cookie, "test=cookietoken;other=othertoken")
}
testIntercept({
headers {
append(HttpHeaders.Authorization, "Bearer headertoken")
append(HttpHeaders.Cookie, "test=cookietoken;other=othertoken")
}}
){

val security = Security(
Security.AuthProviderConfig(
name = null,
Expand All @@ -50,14 +59,19 @@ internal class SecurityTest {
val token = security.getToken(call)

assertEquals(listOf("Bearer cookietoken"), token)
}
}

@Test
internal fun `should use be able to get tokens for multiple providers`() {
val call = createCall {
this.addHeader(HttpHeaders.Authorization, "Bearer headertoken")
this.addHeader(HttpHeaders.Cookie, "test=cookietoken;other=othertoken")
}
testIntercept({
headers {
append(HttpHeaders.Authorization, "Bearer headertoken")
append(HttpHeaders.Cookie, "test=cookietoken;other=othertoken")
}

}) {

val baseprovider = Security.AuthProviderConfig(
name = null,
jwksConfig = Security.JwksConfig.JwksUrl("http://localhost.com", "issuer"),
Expand All @@ -73,9 +87,15 @@ internal class SecurityTest {

assertEquals(listOf("Bearer headertoken", "Bearer cookietoken", "Bearer othertoken"), token)
}
}

private fun testIntercept(block: HttpRequestBuilder.() -> Unit, interceptBlock: suspend PipelineContext<*, PipelineCall>.() -> Unit) = testApplication {
application {
intercept(ApplicationCallPipeline.Call) {
interceptBlock()
}
}

private fun createCall(block: TestApplicationRequest.() -> Unit): ApplicationCall {
val engine = TestApplicationEngine()
return engine.createCall(setup = block)
client.get(block)
}
}

0 comments on commit 66854f7

Please sign in to comment.