Skip to content

Commit a851731

Browse files
Add http assert matcher NotEmpty (#291)
## What - http assert matcher NotEmpty ## References <!-- Add identifier for issue tickets, links to other PRs, etc. --> ## Checklist <!-- Remove this section if not applicable to your changes --> - [ ] Tests
1 parent a7658cd commit a851731

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

pkg/httpassert/matcher.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,16 @@ func Regex(expr string) Matcher {
5656
return assert.Regexp(t, re, value)
5757
}
5858
}
59+
60+
// NotEmpty checks if a string is not empty
61+
// Example: ExpectJsonPath("$.data.name", httpassert.NotEmpty())
62+
func NotEmpty() Matcher {
63+
return func(t *testing.T, value any) bool {
64+
str, ok := value.(string)
65+
if !ok {
66+
return assert.Fail(t, "value is not a string")
67+
}
68+
69+
return assert.NotEmpty(t, str)
70+
}
71+
}

pkg/httpassert/matcher_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,18 @@ func Test_ContainsMatcher(t *testing.T) {
4141
StatusCode(http.StatusOK).
4242
JsonPath("$.data.name", Contains("foo"))
4343
}
44+
45+
func Test_NoEmptyMatcher(t *testing.T) {
46+
router := http.NewServeMux()
47+
router.HandleFunc("/api", func(w http.ResponseWriter, r *http.Request) {
48+
_, err := fmt.Fprint(w, `{"data":{"name": "a"}}`)
49+
assert.NoError(t, err)
50+
})
51+
52+
request := New(t, router)
53+
54+
request.Get("/api").
55+
Expect().
56+
StatusCode(http.StatusOK).
57+
JsonPath("$.data.name", NotEmpty())
58+
}

0 commit comments

Comments
 (0)