test: ContentTypeRouteSelectorのテストを追加

This commit is contained in:
usbharu 2023-05-05 12:41:04 +09:00
parent 36c35cf45a
commit 6d9ef35903
5 changed files with 183 additions and 35 deletions

View File

@ -163,3 +163,11 @@ detekt {
basePath = rootDir.absolutePath
autoCorrect = true
}
tasks.withType<io.gitlab.arturbosch.detekt.Detekt>().configureEach {
exclude("**/org/koin/ksp/generated/**")
}
tasks.withType<io.gitlab.arturbosch.detekt.DetektCreateBaselineTask>().configureEach {
exclude("**/org/koin/ksp/generated/**")
}

View File

@ -75,7 +75,7 @@ val httpSignaturePlugin = createClientPlugin("HttpSign", ::HttpSignaturePluginCo
}
if (request.headers.contains("Signature")) {
val all = request.headers.getAll("Signature")!!
val all = request.headers.getAll("Signature").orEmpty()
val parameters = mutableListOf<String>()
for (s in all) {
s.split(",").forEach { parameters.add(it) }
@ -170,8 +170,12 @@ class KtorKeyMap(private val userAuthRepository: IUserRepository) : KeyMap {
userAuthRepository.findByNameAndDomain(
username,
Config.configData.domain
)?.publicKey?.replace("-----BEGIN PUBLIC KEY-----", "-----END PUBLIC KEY-----")?.replace("", "")
?.replace("\n", "")
)?.run {
publicKey
.replace("-----BEGIN PUBLIC KEY-----", "")
.replace("-----END PUBLIC KEY-----", "")
.replace("\n", "")
}
)
val x509EncodedKeySpec = X509EncodedKeySpec(publicBytes)
return@runBlocking KeyFactory.getInstance("RSA").generatePublic(x509EncodedKeySpec)
@ -184,8 +188,11 @@ class KtorKeyMap(private val userAuthRepository: IUserRepository) : KeyMap {
userAuthRepository.findByNameAndDomain(
username,
Config.configData.domain
)?.privateKey?.replace("-----BEGIN PRIVATE KEY-----", "")?.replace("-----END PRIVATE KEY-----", "")
?.replace("\n", "")
)?.privateKey?.run {
replace("-----BEGIN PRIVATE KEY-----", "")
.replace("-----END PRIVATE KEY-----", "")
.replace("\n", "")
}
)
val x509EncodedKeySpec = PKCS8EncodedKeySpec(publicBytes)
return@runBlocking KeyFactory.getInstance("RSA").generatePrivate(x509EncodedKeySpec)

View File

@ -37,7 +37,7 @@ class ContentTypeRouteSelector(private vararg val contentType: ContentType) : Ro
context.call.application.log.debug("Accept: ${context.call.request.accept()}")
val requestContentType = context.call.request.accept() ?: return RouteSelectorEvaluation.FailedParameter
return if (requestContentType.split(",")
.find { contentType.find { contentType -> contentType.match(it) } != null } != null
.any { contentType.any { contentType -> contentType.match(it) } }
) {
RouteSelectorEvaluation.Constant
} else {

View File

@ -279,35 +279,34 @@ class ExposedJobRepository(
private fun ResultRow.toScheduledJob(): ScheduledJob {
val single = this
jobs.run {
return ScheduledJob(
id = single[this.id].value.toString(),
status = JobStatus.valueOf(single[status]),
runAt = single[runAt]?.let { Instant.ofEpochMilli(it) },
statusMessage = single[statusMessage],
retries = single[retries],
kjobId = single[kjobId]?.let {
try {
@Suppress("SwallowedException")
UUID.fromString(it)
} catch (ignored: IllegalArgumentException) {
null
}
},
createdAt = Instant.ofEpochMilli(single[createdAt]),
updatedAt = Instant.ofEpochMilli(single[updatedAt]),
settings = JobSettings(
id = single[jobId],
name = single[name],
properties = single[properties].parseJsonMap()
),
progress = JobProgress(
step = single[step].toLong(),
max = single[max]?.toLong(),
startedAt = single[startedAt]?.let { Instant.ofEpochMilli(it) },
completedAt = single[completedAt]?.let { Instant.ofEpochMilli(it) }
)
return ScheduledJob(
id = single[jobs.id].value.toString(),
status = JobStatus.valueOf(single[jobs.status]),
runAt = single[jobs.runAt]?.let { Instant.ofEpochMilli(it) },
statusMessage = single[jobs.statusMessage],
retries = single[jobs.retries],
kjobId = single[jobs.kjobId]?.let {
try {
@Suppress("SwallowedException")
UUID.fromString(it)
} catch (ignored: IllegalArgumentException) {
null
}
},
createdAt = Instant.ofEpochMilli(single[jobs.createdAt]),
updatedAt = Instant.ofEpochMilli(single[jobs.updatedAt]),
settings = JobSettings(
id = single[jobs.jobId],
name = single[jobs.name],
properties = single[jobs.properties].parseJsonMap()
),
progress = JobProgress(
step = single[jobs.step].toLong(),
max = single[jobs.max]?.toLong(),
startedAt = single[jobs.startedAt]?.let { Instant.ofEpochMilli(it) },
completedAt = single[jobs.completedAt]?.let { Instant.ofEpochMilli(it) }
)
}
)
}
}

View File

@ -0,0 +1,134 @@
package dev.usbharu.hideout.routing.activitypub
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.config.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.ktor.server.testing.*
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals
class ContentTypeRouteSelectorTest {
@Test
fun `Content-Typeが一つでマッチする`() = testApplication {
environment {
config = ApplicationConfig("empty.conf")
}
application {
routing {
route("/test") {
createChild(ContentTypeRouteSelector(ContentType.Application.Json)).handle {
call.respondText("OK")
}
get {
call.respondText("NG")
}
}
}
}
client.get("/test"){
accept(ContentType.Text.Html)
}.apply {
assertEquals("NG", bodyAsText())
}
client.get("/test") {
accept(ContentType.Application.Json)
}.apply {
assertEquals("OK", bodyAsText())
}
}
@Test
fun `Content-Typeが一つのとき違うとマッチしない`() = testApplication {
environment {
config = ApplicationConfig("empty.conf")
}
application {
routing {
route("/test") {
createChild(ContentTypeRouteSelector(ContentType.Application.Json)).handle {
call.respondText("OK")
}
get {
call.respondText("NG")
}
}
}
}
client.get("/test"){
accept(ContentType.Text.Html)
}.apply {
assertEquals("NG", bodyAsText())
}
}
@Test
fun `Content-Typeがからのときマッチしない`() = testApplication {
environment {
config = ApplicationConfig("empty.conf")
}
application {
routing {
route("/test") {
createChild(ContentTypeRouteSelector()).handle {
call.respondText("OK")
}
get {
call.respondText("NG")
}
}
}
}
client.get("/test"){
accept(ContentType.Text.Html)
}.apply {
assertEquals("NG", bodyAsText())
}
client.get("/test").apply {
assertEquals("NG", bodyAsText())
}
}
@Test
fun `Content-Typeが複数指定されていてマッチする`() = testApplication {
environment {
config = ApplicationConfig("empty.conf")
}
application {
routing {
route("/test") {
createChild(ContentTypeRouteSelector(ContentType.Application.Json, ContentType.Text.Html)).handle {
call.respondText("OK")
}
get {
call.respondText("NG")
}
}
}
}
client.get("/test"){
accept(ContentType.Text.Html)
}.apply {
assertEquals("OK", bodyAsText())
}
client.get("/test"){
accept(ContentType.Application.Json)
}.apply {
assertEquals("OK", bodyAsText())
}
client.get("/test"){
accept(ContentType.Application.Xml)
}.apply {
assertEquals("NG", bodyAsText())
}
}
}