Skip to content

Commit

Permalink
wip: ktor 3
Browse files Browse the repository at this point in the history
  • Loading branch information
nesk committed Nov 2, 2024
1 parent d03cee7 commit c2ffbdc
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.serialization.kotlinx.json.*
import io.ktor.server.application.*
import io.ktor.server.plugins.contentnegotiation.*
import io.ktor.server.plugins.requestvalidation.*
import io.ktor.server.request.*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package dev.nesk.akkurate.ktor.server
import dev.nesk.akkurate.constraints.ConstraintViolationSet
import io.ktor.server.application.*
import io.ktor.server.response.*
import io.ktor.util.reflect.*
import io.mockk.coEvery
import io.mockk.mockk
import io.mockk.slot
Expand All @@ -33,7 +34,7 @@ class AkkurateConfigTestJvm {
// Arrange
val messageSlot = slot<Any>()
val call = mockk<ApplicationCall>(relaxed = true)
coEvery { call.respond(capture(messageSlot)) } returns Unit
coEvery { call.respond(capture(messageSlot), any(TypeInfo::class)) } returns Unit

// Act
val config = AkkurateConfig()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,17 @@ import kotlin.test.Test
import kotlin.test.assertFails
import kotlin.test.assertIs
import kotlin.test.assertSame
import io.ktor.server.plugins.requestvalidation.Validator as KtorValidator

class RegisterValidatorKtTest {
companion object {
val context = object {}
val value = object {}
val lambdaSlot = slot<suspend (Any) -> ValidationResult.Valid>()
val validatorSlot = slot<KtorValidator>()
val config = mockk<RequestValidationConfig>()

init {
every { config.validate<Any>(capture(lambdaSlot)) } returns Unit
every { config.validate(capture(validatorSlot)) } returns Unit
}
}

Expand All @@ -46,23 +47,23 @@ class RegisterValidatorKtTest {
fun registered_contextual_validator_uses_the_provided_context_and_value() = runBlocking {
val validate = Validator<Any, Any> { ctx -> constrain { ctx === context && it === value } }
config.registerValidator(validate) { context }
val result = lambdaSlot.captured(value)
val result = validatorSlot.captured.validate(value)
assertSame(ValidationResult.Valid, result)
}

@Test
fun registered_contextual_validator_returns__ValidationResult_Valid__when_value_is_valid() = runBlocking {
val validate = Validator<Any, Any> { constrain { true } }
config.registerValidator(validate) { context }
val result = lambdaSlot.captured(value)
val result = validatorSlot.captured.validate(value)
assertSame(ValidationResult.Valid, result)
}

@Test
fun registered_contextual_validator_throws__ValidationResult_Exception__when_value_is_invalid(): Unit = runBlocking {
val validate = Validator<Any, Any> { constrain { false } }
config.registerValidator(validate) { context }
val exception = assertFails { lambdaSlot.captured(value) }
val exception = assertFails { validatorSlot.captured.validate(value) }
assertIs<dev.nesk.akkurate.ValidationResult.Exception>(exception)
}
//endregion
Expand All @@ -72,23 +73,23 @@ class RegisterValidatorKtTest {
fun registered_validator_uses_the_provided_value() = runBlocking {
val validate = Validator<Any> { constrain { it === value } }
config.registerValidator(validate)
val result = lambdaSlot.captured(value)
val result = validatorSlot.captured.validate(value)
assertSame(ValidationResult.Valid, result)
}

@Test
fun registered_validator_returns__ValidationResult_Valid__when_value_is_valid() = runBlocking {
val validate = Validator<Any> { constrain { true } }
config.registerValidator(validate)
val result = lambdaSlot.captured(value)
val result = validatorSlot.captured.validate(value)
assertSame(ValidationResult.Valid, result)
}

@Test
fun registered_validator_throws__ValidationResult_Exception__when_value_is_invalid(): Unit = runBlocking {
val validate = Validator<Any> { constrain { false } }
config.registerValidator(validate)
val exception = assertFails { lambdaSlot.captured(value) }
val exception = assertFails { validatorSlot.captured.validate(value) }
assertIs<dev.nesk.akkurate.ValidationResult.Exception>(exception)
}
//endregion
Expand All @@ -98,23 +99,23 @@ class RegisterValidatorKtTest {
fun registered_async_contextual_validator_uses_the_provided_context_and_value() = runBlocking {
val validate = Validator.suspendable<Any, Any> { ctx -> constrain { ctx === context && it === value } }
config.registerValidator(validate) { context }
val result = lambdaSlot.captured(value)
val result = validatorSlot.captured.validate(value)
assertSame(ValidationResult.Valid, result)
}

@Test
fun registered_async_contextual_validator_returns__ValidationResult_Valid__when_value_is_valid() = runBlocking {
val validate = Validator.suspendable<Any, Any> { constrain { true } }
config.registerValidator(validate) { context }
val result = lambdaSlot.captured(value)
val result = validatorSlot.captured.validate(value)
assertSame(ValidationResult.Valid, result)
}

@Test
fun registered_async_contextual_validator_throws__ValidationResult_Exception__when_value_is_invalid(): Unit = runBlocking {
val validate = Validator.suspendable<Any, Any> { constrain { false } }
config.registerValidator(validate) { context }
val exception = assertFails { lambdaSlot.captured(value) }
val exception = assertFails { validatorSlot.captured.validate(value) }
assertIs<dev.nesk.akkurate.ValidationResult.Exception>(exception)
}
//endregion
Expand All @@ -124,23 +125,23 @@ class RegisterValidatorKtTest {
fun registered_async_validator_uses_the_provided_value() = runBlocking {
val validate = Validator.suspendable<Any> { constrain { it === value } }
config.registerValidator(validate)
val result = lambdaSlot.captured(value)
val result = validatorSlot.captured.validate(value)
assertSame(ValidationResult.Valid, result)
}

@Test
fun registered_async_validator_returns__ValidationResult_Valid__when_value_is_valid() = runBlocking {
val validate = Validator.suspendable<Any> { constrain { true } }
config.registerValidator(validate)
val result = lambdaSlot.captured(value)
val result = validatorSlot.captured.validate(value)
assertSame(ValidationResult.Valid, result)
}

@Test
fun registered_async_validator_throws__ValidationResult_Exception__when_value_is_invalid(): Unit = runBlocking {
val validate = Validator.suspendable<Any> { constrain { false } }
config.registerValidator(validate)
val exception = assertFails { lambdaSlot.captured(value) }
val exception = assertFails { validatorSlot.captured.validate(value) }
assertIs<dev.nesk.akkurate.ValidationResult.Exception>(exception)
}
//endregion
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ include(
dependencyResolutionManagement {
versionCatalogs {
create("libs") {
version("ktor", "2.3.12")
version("ktor", "3.0.0")
version("kotlinx-serialization", "1.7.3")

plugin("kotlinx-serialization", "org.jetbrains.kotlin.plugin.serialization").version("2.0.20")
Expand Down

0 comments on commit c2ffbdc

Please sign in to comment.