From bd927677a0a629a7ca95037e8f8f0977b26e0ad2 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Sat, 29 Apr 2023 14:44:43 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=E3=81=AA=E3=81=9C=E3=81=8B=E5=BF=85?= =?UTF-8?q?=E3=81=9AJson=E3=82=92=E8=BF=94=E3=81=99=E3=82=88=E3=81=86?= =?UTF-8?q?=E3=81=AB=E3=81=AA=E3=81=A3=E3=81=A6=E3=81=84=E3=81=9F=E3=81=AE?= =?UTF-8?q?=E3=81=A7=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../routing/activitypub/UserRouting.kt | 3 +- .../routing/activitypub/UsersAPTest.kt | 45 ++++++++++++++++++- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/dev/usbharu/hideout/routing/activitypub/UserRouting.kt b/src/main/kotlin/dev/usbharu/hideout/routing/activitypub/UserRouting.kt index 2864ba23..559ca0a0 100644 --- a/src/main/kotlin/dev/usbharu/hideout/routing/activitypub/UserRouting.kt +++ b/src/main/kotlin/dev/usbharu/hideout/routing/activitypub/UserRouting.kt @@ -37,8 +37,7 @@ class ContentTypeRouteSelector(private vararg val contentType: ContentType) : Ro val requestContentType = ContentType.parse(context.call.request.accept() ?: return RouteSelectorEvaluation.FailedParameter) - return if (contentType.find { contentType: ContentType -> contentType.match(requestContentType) } - .let { true }) { + return if (contentType.find { contentType: ContentType -> contentType.match(requestContentType) } != null) { RouteSelectorEvaluation.Constant } else { RouteSelectorEvaluation.FailedParameter diff --git a/src/test/kotlin/dev/usbharu/hideout/routing/activitypub/UsersAPTest.kt b/src/test/kotlin/dev/usbharu/hideout/routing/activitypub/UsersAPTest.kt index e5ec1457..9f713056 100644 --- a/src/test/kotlin/dev/usbharu/hideout/routing/activitypub/UsersAPTest.kt +++ b/src/test/kotlin/dev/usbharu/hideout/routing/activitypub/UsersAPTest.kt @@ -9,6 +9,7 @@ import com.fasterxml.jackson.module.kotlin.readValue import dev.usbharu.hideout.domain.model.ap.Image import dev.usbharu.hideout.domain.model.ap.Key import dev.usbharu.hideout.domain.model.ap.Person +import dev.usbharu.hideout.domain.model.hideout.entity.User import dev.usbharu.hideout.plugins.configureRouting import dev.usbharu.hideout.plugins.configureSerialization import dev.usbharu.hideout.service.activitypub.ActivityPubService @@ -25,7 +26,9 @@ import io.ktor.server.testing.* import org.junit.jupiter.api.Test import org.mockito.ArgumentMatchers.anyString import org.mockito.kotlin.doReturn +import org.mockito.kotlin.eq import org.mockito.kotlin.mock +import java.time.Instant import kotlin.test.assertEquals import kotlin.test.assertTrue @@ -168,7 +171,47 @@ class UsersAPTest { // @Disabled fun contentType_Test() { val listOf = listOf(ContentType.Application.JsonLd, ContentType.Application.Activity) - assertTrue(listOf.find { contentType -> contentType.match("application/ld+json; profile=\"\\\"https://www.w3.org/ns/activitystreams\\\",application/activity+json\"") }.let { true }) + assertTrue(listOf.find { contentType -> + contentType.match("application/ld+json; profile=\"\\\"https://www.w3.org/ns/activitystreams\\\",application/activity+json\"") + }.let { it != null }) assertTrue(ContentType.Application.JsonLd.match("application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"")) } + + @Test + fun ユーザーのURLにAcceptヘッダーをhtmlにしてアクセスしたときはただの文字を返す() = testApplication { + environment { + config = ApplicationConfig("empty.conf") + } + val userService = mock { + onBlocking { findByNameLocalUser(eq("test")) } doReturn User( + 1L, + "test", + "example.com", + "test", + "", + "hashedPassword", + "https://example.com/inbox", + "https://example.com/outbox", + "https://example.com", + "-----BEGIN PUBLIC KEY-----...-----END PUBLIC KEY-----", + "-----BEGIN PRIVATE KEY-----...-----END PRIVATE KEY-----", + Instant.now() + ) + } + application { + configureRouting( + mock(), + mock(), + userService, + mock(), + mock() + ) + } + client.get("/users/test") { + accept(ContentType.Text.Html) + }.let { + assertEquals(HttpStatusCode.OK, it.status) + assertTrue(it.contentType()?.match(ContentType.Text.Plain) == true) + } + } }