Skip to content

Commit 600f714

Browse files
committed
Add unit test for journal entry body truncation
1 parent 9a29c18 commit 600f714

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

core/journal/journal.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func (this *Journal) NewEntry(request *http.Request, response *http.Response, mo
114114

115115
payloadResponse := &models.ResponseDetails{
116116
Status: response.StatusCode,
117-
Body: util.TruncateStringWithEllipsis(respBody, this.BodyMemoryLimit.ToBytes()),
117+
Body: respBody,
118118
Headers: response.Header,
119119
}
120120

core/journal/journal_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,43 @@ func Test_Journal_NewEntry_AddsJournalEntryToEntries(t *testing.T) {
7676
Expect(entries[0].Latency).To(BeNumerically("<", 1))
7777
}
7878

79+
func Test_Journal_NewEntryWithMemoryLimit_TruncateBody(t *testing.T) {
80+
RegisterTestingT(t)
81+
82+
unit := journal.NewJournal()
83+
unit.BodyMemoryLimit = 15
84+
85+
request, _ := http.NewRequest("GET", "http://hoverfly.io", io.NopCloser(bytes.NewBufferString("large request body")),)
86+
87+
nowTime := time.Now()
88+
89+
_, err := unit.NewEntry(request, &http.Response{
90+
StatusCode: 200,
91+
Body: io.NopCloser(bytes.NewBufferString("large response body")),
92+
Header: http.Header{
93+
"test-header": []string{
94+
"one", "two",
95+
},
96+
},
97+
}, "test-mode", nowTime)
98+
Expect(err).To(BeNil())
99+
100+
journalView, err := unit.GetEntries(0, 25, nil, nil, "")
101+
entries := journalView.Journal
102+
Expect(err).To(BeNil())
103+
104+
Expect(entries).ToNot(BeNil())
105+
Expect(entries).To(HaveLen(1))
106+
107+
Expect(*entries[0].Request.Method).To(Equal("GET"))
108+
Expect(*entries[0].Request.Destination).To(Equal("hoverfly.io"))
109+
Expect(*entries[0].Request.Body).To(Equal("large reques..."))
110+
111+
Expect(entries[0].Response.Status).To(Equal(200))
112+
Expect(entries[0].Response.Body).To(Equal("large respon..."))
113+
}
114+
115+
79116
func Test_Journal_UpdateEntry_AddsRemotePostServeActionToJournalEntry(t *testing.T) {
80117
RegisterTestingT(t)
81118

functional-tests/core/ft_journal_indexing_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
. "github.com/onsi/ginkgo"
88
. "github.com/onsi/gomega"
99
"io"
10-
"io/ioutil"
1110
"net/http"
1211
"net/http/httptest"
1312
"strings"
@@ -53,7 +52,7 @@ var _ = Describe("Manage journal indexing in hoverfly", func() {
5352
simulationResponse := hoverfly.Proxy(sling.New().Get("http://test-server.com/journaltest"))
5453
Expect(resp.StatusCode).To(Equal(200))
5554

56-
body, err := ioutil.ReadAll(simulationResponse.Body)
55+
body, err := io.ReadAll(simulationResponse.Body)
5756
Expect(err).To(BeNil())
5857

5958
Expect(string(body)).To(Equal("Application Testing"))
@@ -98,7 +97,7 @@ var _ = Describe("Manage journal indexing in hoverfly", func() {
9897
simulationResponse := hoverfly.Proxy(sling.New().Get("http://test-server.com/journaltest"))
9998
Expect(resp.StatusCode).To(Equal(200))
10099

101-
body, err := ioutil.ReadAll(simulationResponse.Body)
100+
body, err := io.ReadAll(simulationResponse.Body)
102101
Expect(err).To(BeNil())
103102

104103
Expect(string(body)).To(Equal("Application Testing"))

0 commit comments

Comments
 (0)