From 9a0eee5f5dfd41e4597dc0725010048e94187794 Mon Sep 17 00:00:00 2001 From: Tommy Situ Date: Wed, 15 Jul 2020 23:14:36 +0100 Subject: [PATCH] Fix regression on journal filter results being serialized to nil --- core/journal/journal.go | 4 +++- core/journal/journal_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/core/journal/journal.go b/core/journal/journal.go index 3f9e9fca0..3cae135c1 100644 --- a/core/journal/journal.go +++ b/core/journal/journal.go @@ -161,8 +161,10 @@ func (this *Journal) GetEntries(offset int, limit int, from *time.Time, to *time } func (this *Journal) GetFilteredEntries(journalEntryFilterView v2.JournalEntryFilterView) ([]v2.JournalEntryView, error) { - var filteredEntries []v2.JournalEntryView + // init an empty slice to prevent serializing to a null value + filteredEntries := []v2.JournalEntryView{} if this.EntryLimit == 0 { + return filteredEntries, fmt.Errorf("Journal disabled") } diff --git a/core/journal/journal_test.go b/core/journal/journal_test.go index bb1588696..eee14b75a 100644 --- a/core/journal/journal_test.go +++ b/core/journal/journal_test.go @@ -460,6 +460,35 @@ func Test_Journal_GetEntries_FilteredByTimeWindow(t *testing.T) { Expect(entries[2].TimeStarted).To(Equal("2018-02-01T02:00:03.000Z")) } +func Test_Journal_GetFilteredEntries_WillReturnEmptySliceIfNoJournalFound(t *testing.T) { + RegisterTestingT(t) + + unit := journal.NewJournal() + + request, _ := http.NewRequest("GET", "http://hoverfly.io/path/one?one=1&two=2", bytes.NewBufferString(`{"meta:{"field": "value"}}`)) + request.Header.Add("Accept", "application/json") + + unit.NewEntry(request, &http.Response{ + StatusCode: 200, + Body: ioutil.NopCloser(bytes.NewBufferString("test body")), + }, "test-mode", time.Now()) + + // Body + entries, err := unit.GetFilteredEntries(v2.JournalEntryFilterView{ + Request: &v2.RequestMatcherViewV5{ + Body: []v2.MatcherViewV5{ + { + Matcher: matchers.Exact, + Value: `{"meta:{"field": "other-value"}}`, + }, + }, + }, + }) + Expect(err).To(BeNil()) + Expect(entries).ToNot(BeNil()) + Expect(entries).To(HaveLen(0)) +} + func Test_Journal_GetFilteredEntries_WillFilterOnRequestFields(t *testing.T) { RegisterTestingT(t)