-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #283 from hotwired/visit-response-log
Improve how a TurboVisitResponse is logged
- Loading branch information
Showing
3 changed files
with
93 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 17 additions & 1 deletion
18
turbo/src/main/kotlin/dev/hotwire/turbo/visit/TurboVisitResponse.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" + | ||
")" | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
turbo/src/test/kotlin/dev/hotwire/turbo/visit/TurboVisitResponseTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" + | ||
")" | ||
) | ||
} | ||
} |