Skip to content

Instantly share code, notes, and snippets.

@audax
Created January 24, 2017 09:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save audax/ac07afbe225d0d6a04f57a9fa492bffb to your computer and use it in GitHub Desktop.
Save audax/ac07afbe225d0d6a04f57a9fa492bffb to your computer and use it in GitHub Desktop.
Test that proves that he InMemorySessionStorage crashes when given an invalid session
package de.qabel.teqs.ktor.modules.auth
import org.jetbrains.ktor.application.*
import org.jetbrains.ktor.http.*
import org.jetbrains.ktor.response.*
import org.jetbrains.ktor.routing.*
import org.jetbrains.ktor.sessions.*
import org.jetbrains.ktor.testing.*
import org.jetbrains.ktor.tests.*
import org.junit.*
import kotlin.test.*
class ServerSessionTest {
@Test
fun testSessionById() {
val sessionStorage = inMemorySessionStorage()
withTestApplication {
application.withSessions<TestUserSession> {
withCookieBySessionId(sessionStorage)
}
application.routing {
get("/1") {
call.session(TestUserSession("id2", listOf("item1")))
call.respondText("ok")
}
}
val call = handleRequest {
uri = "/1"
addHeader(HttpHeaders.Cookie,
"SESSION_ID=invalid")
}
val sessionId = call.response.cookies["SESSION_ID"]!!.value
val nextCall = handleRequest {
uri = "/1"
addHeader(HttpHeaders.Cookie, "SESSION_ID=$sessionId")
}
assertEquals(sessionId, nextCall.response.cookies["SESSION_ID"]!!.value)
}
}
data class TestUserSession(val userId: String, val cart: List<String>)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment