From 70a2075aec0860a7e8fa34bc43d74fb954e39137 Mon Sep 17 00:00:00 2001 From: Jay Ohms Date: Tue, 29 Aug 2023 16:07:45 -0400 Subject: [PATCH] Improve how a TurboVisitResponse is logged by truncating the response html and added the response length --- .../dev/hotwire/turbo/util/TurboExtensions.kt | 14 +++++ .../hotwire/turbo/visit/TurboVisitResponse.kt | 18 +++++- .../turbo/visit/TurboVisitResponseTest.kt | 62 +++++++++++++++++++ 3 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 turbo/src/test/kotlin/dev/hotwire/turbo/visit/TurboVisitResponseTest.kt diff --git a/turbo/src/main/kotlin/dev/hotwire/turbo/util/TurboExtensions.kt b/turbo/src/main/kotlin/dev/hotwire/turbo/util/TurboExtensions.kt index 5f54234f..b570d46b 100644 --- a/turbo/src/main/kotlin/dev/hotwire/turbo/util/TurboExtensions.kt +++ b/turbo/src/main/kotlin/dev/hotwire/turbo/util/TurboExtensions.kt @@ -31,6 +31,20 @@ internal fun String.extract(patternRegex: String): String? { return regex.find(this)?.groups?.get(1)?.value } +internal fun String.truncateMiddle(maxChars: Int): String { + if (maxChars <= 1 || length <= maxChars) { return this } + + return "${take(maxChars / 2)} [...] ${takeLast(maxChars / 2)}" +} + +internal fun String.withoutNewLineChars(): String { + return this.replace("\n", "") +} + +internal fun String.withoutRepeatingWhitespace(): String { + return this.replace(Regex("\\s+"), " ") +} + internal fun File.deleteAllFilesInDirectory() { if (!isDirectory) return diff --git a/turbo/src/main/kotlin/dev/hotwire/turbo/visit/TurboVisitResponse.kt b/turbo/src/main/kotlin/dev/hotwire/turbo/visit/TurboVisitResponse.kt index e07fbbaa..d96db1c6 100644 --- a/turbo/src/main/kotlin/dev/hotwire/turbo/visit/TurboVisitResponse.kt +++ b/turbo/src/main/kotlin/dev/hotwire/turbo/visit/TurboVisitResponse.kt @@ -1,8 +1,24 @@ package dev.hotwire.turbo.visit import com.google.gson.annotations.SerializedName +import dev.hotwire.turbo.util.truncateMiddle +import dev.hotwire.turbo.util.withoutNewLineChars +import dev.hotwire.turbo.util.withoutRepeatingWhitespace data class TurboVisitResponse( @SerializedName("statusCode") val statusCode: Int, @SerializedName("responseHTML") val responseHTML: String? = null -) +) { + override fun toString(): String { + val response = responseHTML + ?.withoutNewLineChars() + ?.withoutRepeatingWhitespace() + ?.truncateMiddle(maxChars = 50) + + return "TurboVisitResponse(" + + "statusCode=$statusCode, " + + "responseHTML=$response, " + + "responseLength=${responseHTML?.length ?: 0}" + + ")" + } +} diff --git a/turbo/src/test/kotlin/dev/hotwire/turbo/visit/TurboVisitResponseTest.kt b/turbo/src/test/kotlin/dev/hotwire/turbo/visit/TurboVisitResponseTest.kt new file mode 100644 index 00000000..d7f313cc --- /dev/null +++ b/turbo/src/test/kotlin/dev/hotwire/turbo/visit/TurboVisitResponseTest.kt @@ -0,0 +1,62 @@ +package dev.hotwire.turbo.visit + +import org.assertj.core.api.Assertions.assertThat +import org.junit.Test + +class TurboVisitResponseTest { + @Test + fun toStringWithNoResponse() { + val response = TurboVisitResponse( + statusCode = 200, + responseHTML = null + ) + + assertThat(response.toString()).isEqualTo( + "TurboVisitResponse(statusCode=200, responseHTML=null, responseLength=0)" + ) + } + + @Test + fun toStringWithShortResponse() { + val response = TurboVisitResponse( + statusCode = 200, + responseHTML = "" + ) + + assertThat(response.toString()).isEqualTo( + "TurboVisitResponse(statusCode=200, responseHTML=, responseLength=26)" + ) + } + + @Test + fun toStringWithTruncatedResponse() { + val response = TurboVisitResponse( + statusCode = 200, + responseHTML = "This is a really long response that is truncated." + ) + + assertThat(response.toString()).isEqualTo( + "TurboVisitResponse(" + + "statusCode=200, " + + "responseHTML=This i [...] that is truncated., " + + "responseLength=75" + + ")" + ) + } + + @Test + fun toStringWithTruncatedResponseAndWhitespace() { + val response = TurboVisitResponse( + statusCode = 200, + responseHTML = "\nThis is a really long response that is truncated.\n" + ) + + assertThat(response.toString()).isEqualTo( + "TurboVisitResponse(" + + "statusCode=200, " + + "responseHTML=This i [...] that is truncated., " + + "responseLength=79" + + ")" + ) + } +}