Skip to content

Commit

Permalink
chore: improved messageFor404 to include Known templates for genhtml (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
MikAoJk authored Oct 29, 2024
1 parent 5c7ac36 commit dfd26a0
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/no/nav/pdfgen/Application.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
31 changes: 25 additions & 6 deletions src/main/kotlin/no/nav/pdfgen/plugins/StatusPages.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Pair<String, String>, Template>,
environment: Environment,
) {
install(StatusPages) {
exception<Throwable> { call, cause ->
Expand All @@ -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,
),
Expand All @@ -29,8 +31,25 @@ fun Application.configureStatusPages(
}
}

private fun messageFor404(templates: Map<Pair<String, String>, 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<Pair<String, String>, 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")

}
}
27 changes: 26 additions & 1 deletion src/test/kotlin/no/nav/pdfgen/PdfGenITest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit dfd26a0

Please sign in to comment.