From dfd26a019b0995cbf0d13b14661bba8d3d7d1709 Mon Sep 17 00:00:00 2001 From: Joakim Taule Kartveit Date: Tue, 29 Oct 2024 14:49:30 +0100 Subject: [PATCH] chore: improved messageFor404 to include Known templates for genhtml (#270) --- src/main/kotlin/no/nav/pdfgen/Application.kt | 2 +- .../no/nav/pdfgen/plugins/StatusPages.kt | 31 +++++++++++++++---- src/test/kotlin/no/nav/pdfgen/PdfGenITest.kt | 27 +++++++++++++++- 3 files changed, 52 insertions(+), 8 deletions(-) diff --git a/src/main/kotlin/no/nav/pdfgen/Application.kt b/src/main/kotlin/no/nav/pdfgen/Application.kt index a10eb7d..92de6ea 100644 --- a/src/main/kotlin/no/nav/pdfgen/Application.kt +++ b/src/main/kotlin/no/nav/pdfgen/Application.kt @@ -50,7 +50,7 @@ fun Application.module() { configureLifecycleHooks(applicationState = applicationState) configureContentNegotiation() - configureStatusPages(templates = templates) + configureStatusPages(templates = templates, environment = environment) configureNais(applicationState = applicationState) configureReloadPDFGenCore(environment = environment) configureRouting(environment = environment) diff --git a/src/main/kotlin/no/nav/pdfgen/plugins/StatusPages.kt b/src/main/kotlin/no/nav/pdfgen/plugins/StatusPages.kt index 6a7298e..952277b 100644 --- a/src/main/kotlin/no/nav/pdfgen/plugins/StatusPages.kt +++ b/src/main/kotlin/no/nav/pdfgen/plugins/StatusPages.kt @@ -7,10 +7,12 @@ import io.ktor.server.application.* import io.ktor.server.plugins.statuspages.* import io.ktor.server.request.* import io.ktor.server.response.* +import no.nav.pdfgen.Environment import no.nav.pdfgen.logger fun Application.configureStatusPages( templates: Map, Template>, + environment: Environment, ) { install(StatusPages) { exception { call, cause -> @@ -20,7 +22,7 @@ fun Application.configureStatusPages( status(HttpStatusCode.NotFound) { call, _ -> call.respond( TextContent( - messageFor404(templates, call.request.path()), + messageFor404(templates, call.request.path(), environment), ContentType.Text.Plain.withCharset(Charsets.UTF_8), HttpStatusCode.NotFound, ), @@ -29,8 +31,25 @@ fun Application.configureStatusPages( } } -private fun messageFor404(templates: Map, Template>, path: String) = - "Unkown path '$path'. Known templates:\n" + - templates - .map { (app, _) -> "/api/v1/genpdf/%s/%s".format(app.first, app.second) } - .joinToString("\n") +private fun messageFor404( + templates: Map, Template>, + path: String, + environment: Environment +): String { + if (environment.enableHtmlEndpoint && !environment.disablePdfGet) { + return "Unkown path '$path'. Known templates:\n" + + templates + .map { (app, _) -> "/api/v1/genpdf/%s/%s".format(app.first, app.second) } + .joinToString("\n") + + templates + .map { (app, _) -> "/api/v1/genhtml/%s/%s".format(app.first, app.second) } + .joinToString("\n") + } else { + + return "Unkown path '$path'. Known templates:\n" + + templates + .map { (app, _) -> "/api/v1/genpdf/%s/%s".format(app.first, app.second) } + .joinToString("\n") + + } +} diff --git a/src/test/kotlin/no/nav/pdfgen/PdfGenITest.kt b/src/test/kotlin/no/nav/pdfgen/PdfGenITest.kt index 8bd60b9..164f756 100644 --- a/src/test/kotlin/no/nav/pdfgen/PdfGenITest.kt +++ b/src/test/kotlin/no/nav/pdfgen/PdfGenITest.kt @@ -13,6 +13,7 @@ import java.util.concurrent.Executors import kotlinx.coroutines.* import no.nav.pdfgen.core.Environment as PDFGenCoreEnvironment import no.nav.pdfgen.plugins.configureRouting +import no.nav.pdfgen.plugins.configureStatusPages import org.apache.pdfbox.Loader import org.apache.pdfbox.io.IOUtils import org.junit.jupiter.api.Assertions.assertEquals @@ -231,7 +232,7 @@ internal class PdfGenITest { } @Test - internal fun `Calls to unknown endpoints should respond with helpful information`() { + internal fun `Calls to unknown endpoints should respond with helpful information genpdf`() { testApplication { application { module() } runBlocking { @@ -245,6 +246,30 @@ internal class PdfGenITest { } } + @Test + internal fun `Calls to unknown endpoints should respond with helpful information genpdf and genhtml`() { + testApplication { + val environment = + Environment( + disablePdfGet = false, + enableHtmlEndpoint = true, + ) + application { + configureStatusPages(templates,environment) + } + runBlocking { + runBlocking { client.config { expectSuccess = false }.preparePost("/imnothome") } + .execute { response -> + assertEquals(404, response.status.value) + val text = runBlocking { response.bodyAsText() } + println(text) + assertEquals(true, text.contains("Known templates:\n/api/v1/genpdf")) + assertEquals(true, text.contains("api/v1/genhtml")) + } + } + } + } + @Test internal fun `Simple performance test full multiple-threads`() { testApplication {