From e3d09cfaaa1cc98519b0c315c9ed0df3e9670163 Mon Sep 17 00:00:00 2001 From: Stefan Tolksdorf Date: Sat, 21 Feb 2026 15:52:04 +0100 Subject: [PATCH] Add http assert matcher NotEmpty --- pkg/httpassert/matcher.go | 13 +++++++++++++ pkg/httpassert/matcher_test.go | 15 +++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/pkg/httpassert/matcher.go b/pkg/httpassert/matcher.go index 4dc332b..65e2bdc 100644 --- a/pkg/httpassert/matcher.go +++ b/pkg/httpassert/matcher.go @@ -56,3 +56,16 @@ func Regex(expr string) Matcher { return assert.Regexp(t, re, value) } } + +// NotEmpty checks if a string is not empty +// Example: ExpectJsonPath("$.data.name", httpassert.NotEmpty()) +func NotEmpty() Matcher { + return func(t *testing.T, value any) bool { + str, ok := value.(string) + if !ok { + return assert.Fail(t, "value is not a string") + } + + return assert.NotEmpty(t, str) + } +} diff --git a/pkg/httpassert/matcher_test.go b/pkg/httpassert/matcher_test.go index 16ce10f..8b5155c 100644 --- a/pkg/httpassert/matcher_test.go +++ b/pkg/httpassert/matcher_test.go @@ -41,3 +41,18 @@ func Test_ContainsMatcher(t *testing.T) { StatusCode(http.StatusOK). JsonPath("$.data.name", Contains("foo")) } + +func Test_NoEmptyMatcher(t *testing.T) { + router := http.NewServeMux() + router.HandleFunc("/api", func(w http.ResponseWriter, r *http.Request) { + _, err := fmt.Fprint(w, `{"data":{"name": "a"}}`) + assert.NoError(t, err) + }) + + request := New(t, router) + + request.Get("/api"). + Expect(). + StatusCode(http.StatusOK). + JsonPath("$.data.name", NotEmpty()) +}