Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions pkg/httpassert/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
15 changes: 15 additions & 0 deletions pkg/httpassert/matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Loading