-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpHeadersAuthenticationProviderTest.kt
More file actions
62 lines (52 loc) · 1.77 KB
/
HttpHeadersAuthenticationProviderTest.kt
File metadata and controls
62 lines (52 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import cv.mcdonnell.httpHeaders
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.auth.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.ktor.server.testing.*
import kotlin.test.Test
data class HttpHeadersPrincipal(val headers: Headers)
class HttpHeadersAuthenticationProviderTest {
@Test
fun testRespectsHeader() = testApplication {
application { testModule() }
val response = client.get("/") {
headers {
append("X-Custom-Auth", "letmein")
}
}
assert(response.status.isSuccess())
assert(response.bodyAsText() == "letmein")
}
@Test
fun testRejectIfHeaderIsMissing() = testApplication {
application { testModule() }
val response = client.get("/")
assert(response.status == HttpStatusCode.Unauthorized)
}
private fun Application.testModule() {
install(Authentication) {
httpHeaders {
authenticate { credentials ->
if (credentials.headers["X-Custom-Auth"] == "letmein") {
HttpHeadersPrincipal(credentials.headers)
} else {
null
}
}
}
}
routing {
authenticate {
get("/") {
// If authentication is successful, the principal is set to HttpHeadersPrincipal,
// and the call responds with the value of the "X-Custom-Auth" header.
call.respondText(call.authentication.principal<HttpHeadersPrincipal>()!!.headers["X-Custom-Auth"]!!)
}
}
}
}
}