S.
03/27/2022, 4:14 PMtestApplication {
client.config {
install(HttpCookies)
install(ContentNegotiation) {
json()
}
}
routing {
get("/login-test") {
call.sessions.set(UserSession(id = 4))
}
}
client.get("/login-test")
val response = client.get("v1/words")
response.status shouldBe HttpStatusCode.OK
}
Aleksei Tirman [JB]
03/27/2022, 4:30 PMget("/login-test")
, e.g. call.respond(HttpStatusCode.OK)
or disable default response validation.S.
03/27/2022, 4:32 PMS.
03/27/2022, 5:00 PMAleksei Tirman [JB]
03/27/2022, 7:14 PMS.
03/27/2022, 8:38 PMfun Application.configureSecurity() {
install(Authentication) {
basic("auth_basic") {
skipWhen { call -> call.sessions.get<UserSession>() != null }
realm = "Access to the '/v1' path"
validate {
UserService.authenticate(it)
}
}
session<UserSession>("auth_session") {
validate { session ->
session
}
challenge {
call.respondText("Failed", status = HttpStatusCode.Unauthorized)
}
}
}
install(Sessions) {
cookie<UserSession>("user_session") {
cookie.path = "/v1"
cookie.maxAgeInSeconds = 21_600
}
}
}
authenticate("auth_basic") {
get("/login") {
val userId = call.principal<UserIdPrincipal>()
call.sessions.set(UserSession(id = userId?.name!!.toInt()))
call.respondText("Login successful", status = HttpStatusCode.Accepted)
}
}
authenticate("auth_session") {
get("/hello") {
val userSession = call.principal<UserSession>()
call.respondText("Hello ${userSession?.id}")
}
wordRouting()
}
S.
03/27/2022, 8:40 PMtestApplication {
client.get("v1/login") {
basicAuth("Kaley", "501")
}
val response = client.get("v1/words")
}
S.
03/27/2022, 8:42 PMAleksei Tirman [JB]
03/28/2022, 10:08 AMimport io.ktor.client.plugins.cookies.*
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.sessions.*
import io.ktor.server.testing.*
import org.junit.Test
import kotlin.test.assertEquals
class SomeTest {
@Test
fun some() {
testApplication {
application {
configureSecurity()
}
val client = createClient {
install(HttpCookies)
}
client.get("v1/login") {
basicAuth("Kaley", "501")
}
val response = client.get("v1/hello")
assertEquals("Hello 123", response.bodyAsText())
}
}
}
fun Application.configureSecurity() {
install(Authentication) {
basic("auth_basic") {
skipWhen { call -> call.sessions.get<UserSession>() != null }
realm = "Access to the '/v1' path"
validate { creds ->
if (creds.name == "Kaley" && creds.password == "501") UserIdPrincipal("123") else null
}
}
session<UserSession>("auth_session") {
validate { session ->
session
}
challenge {
call.respondText("Failed", status = HttpStatusCode.Unauthorized)
}
}
}
install(Sessions) {
cookie<UserSession>("user_session") {
cookie.path = "/v1"
cookie.maxAgeInSeconds = 21_600
}
}
routing {
route("/v1") {
authenticate("auth_basic") {
get("/login") {
val userId = call.principal<UserIdPrincipal>()
call.sessions.set(UserSession(id = userId?.name!!.toInt()))
call.respondText("Login successful", status = HttpStatusCode.Accepted)
}
}
authenticate("auth_session") {
get("/hello") {
val userSession = call.principal<UserSession>()
call.respondText("Hello ${userSession?.id}")
}
// wordRouting()
}
}
}
}
data class UserSession(val id: Int): Principal
Seems like the problem is that the client.config
call doesn’t mutate configuration of an existing HttpClient but returns a new one which you don’t use. I would recommend creating a client in the test using the createClient
method.S.
03/28/2022, 4:12 PMS.
03/28/2022, 4:12 PM