Skip to content

Commit 9a29c18

Browse files
committed
Truncate request response body if journal memory limit is set
1 parent 10b60cd commit 9a29c18

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

core/journal/journal.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,14 @@ func (this *Journal) NewEntry(request *http.Request, response *http.Response, mo
107107

108108
respBody, _ := util.GetResponseBody(response)
109109

110+
if this.BodyMemoryLimit.ToBytes() > 0 {
111+
payloadRequest.Body = util.TruncateStringWithEllipsis(payloadRequest.Body, this.BodyMemoryLimit.ToBytes())
112+
respBody = util.TruncateStringWithEllipsis(respBody, this.BodyMemoryLimit.ToBytes())
113+
}
114+
110115
payloadResponse := &models.ResponseDetails{
111116
Status: response.StatusCode,
112-
Body: respBody,
117+
Body: util.TruncateStringWithEllipsis(respBody, this.BodyMemoryLimit.ToBytes()),
113118
Headers: response.Header,
114119
}
115120

core/util/util.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -558,8 +558,12 @@ func ResolveAndValidatePath(absBasePath, relativePath string) (string, error) {
558558
return resolvedPath, nil
559559
}
560560

561-
func truncateStringWithEllipsis(input string, maxSize int) string {
562-
ellipsis := "..."
561+
func TruncateStringWithEllipsis(input string, maxSize int) string {
562+
ellipsis := "..." // 3 bytes in UTF-8
563+
564+
if maxSize <= 3 {
565+
return ellipsis
566+
}
563567

564568
if len(input) <= maxSize{
565569
return input

core/util/util_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,12 @@ func TestTruncateStringWithEllipsis(t *testing.T) {
432432
maxSize: 10,
433433
expected: "Hello",
434434
},
435+
{
436+
name: "Zero max size",
437+
input: "Hello",
438+
maxSize: 0,
439+
expected: "...",
440+
},
435441
{
436442
name: "Truncate with ellipsis",
437443
input: "Hello, World!",
@@ -473,7 +479,7 @@ func TestTruncateStringWithEllipsis(t *testing.T) {
473479
for _, test := range tests {
474480
t.Run(test.name, func(t *testing.T) {
475481
g := NewWithT(t)
476-
result := truncateStringWithEllipsis(test.input, test.maxSize)
482+
result := TruncateStringWithEllipsis(test.input, test.maxSize)
477483
g.Expect(result).To(Equal(test.expected), "Expected %q but got %q for input %q with maxSize %d", test.expected, result, test.input, test.maxSize)
478484
})
479485
}

0 commit comments

Comments
 (0)