Skip to content

Commit

Permalink
Merge pull request #283 from hotwired/visit-response-log
Browse files Browse the repository at this point in the history
Improve how a TurboVisitResponse is logged
  • Loading branch information
jayohms authored Aug 29, 2023
2 parents 125a8ed + 70a2075 commit d3f0798
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 1 deletion.
14 changes: 14 additions & 0 deletions turbo/src/main/kotlin/dev/hotwire/turbo/util/TurboExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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}" +
")"
}
}
Original file line number Diff line number Diff line change
@@ -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 = "<html><head></head></html>"
)

assertThat(response.toString()).isEqualTo(
"TurboVisitResponse(statusCode=200, responseHTML=<html><head></head></html>, responseLength=26)"
)
}

@Test
fun toStringWithTruncatedResponse() {
val response = TurboVisitResponse(
statusCode = 200,
responseHTML = "<html><head></head>This is a really long response that is truncated.</html>"
)

assertThat(response.toString()).isEqualTo(
"TurboVisitResponse(" +
"statusCode=200, " +
"responseHTML=<html><head></head>This i [...] that is truncated.</html>, " +
"responseLength=75" +
")"
)
}

@Test
fun toStringWithTruncatedResponseAndWhitespace() {
val response = TurboVisitResponse(
statusCode = 200,
responseHTML = "<html>\n<head></head>This is a really long response that is truncated.\n</html>"
)

assertThat(response.toString()).isEqualTo(
"TurboVisitResponse(" +
"statusCode=200, " +
"responseHTML=<html><head></head>This i [...] that is truncated.</html>, " +
"responseLength=79" +
")"
)
}
}

0 comments on commit d3f0798

Please # to comment.