Skip to content

Commit 27b9107

Browse files
Add httpassert header validation
1 parent 7ef2d65 commit 27b9107

3 files changed

Lines changed: 64 additions & 0 deletions

File tree

pkg/httpassert/matcher.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package httpassert
66

77
import (
88
"fmt"
9+
"regexp"
910
"testing"
1011

1112
"github.com/stretchr/testify/assert"
@@ -45,3 +46,13 @@ func Contains(v string) Matcher {
4546
return valid
4647
}
4748
}
49+
50+
// Regex checks if a string matches the given regular expression
51+
// Example: ExpectJsonPath("$.data.name", httpassert.Regex("^foo.*bar$"))
52+
func Regex(expr string) Matcher {
53+
re := regexp.MustCompile(expr)
54+
55+
return func(t *testing.T, value any) bool {
56+
return assert.Regexp(t, re, value)
57+
}
58+
}

pkg/httpassert/response.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,32 @@ type Response interface {
3636
JsonTemplateFile(path string, values map[string]any) Response
3737
JsonFile(path string) Response
3838

39+
Header(name string, value any) Response
40+
3941
Body(body string) Response
4042
GetJsonBodyObject(target any) Response
4143
GetBody() string
4244

4345
Log() Response
4446
}
4547

48+
func (r *responseImpl) Header(name string, value any) Response {
49+
out := r.response.Header().Get(name)
50+
51+
switch v := value.(type) {
52+
case Extractor:
53+
v(r.t, out)
54+
return r
55+
case Matcher:
56+
v(r.t, out)
57+
return r
58+
59+
default:
60+
assert.Equal(r.t, value, out)
61+
return r
62+
}
63+
}
64+
4665
func (r *responseImpl) StatusCode(expected int) Response {
4766
require.Equal(r.t, expected, r.response.Code)
4867
return r

pkg/httpassert/response_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,40 @@ func TestResponse(t *testing.T) {
143143
})
144144
})
145145

146+
t.Run("Header", func(t *testing.T) {
147+
router := http.NewServeMux()
148+
router.HandleFunc("/api", func(w http.ResponseWriter, r *http.Request) {
149+
w.Header().Set("Content-Type", "application/json")
150+
w.WriteHeader(http.StatusOK)
151+
})
152+
153+
t.Run("compare string", func(t *testing.T) {
154+
m := New(t, router)
155+
156+
m.Get("/api").
157+
Expect().
158+
Header("Content-Type", "application/json")
159+
})
160+
161+
t.Run("extract value to variable", func(t *testing.T) {
162+
m := New(t, router)
163+
164+
var value string
165+
m.Get("/api").
166+
Expect().
167+
Header("Content-Type", ExtractTo(&value))
168+
assert.Equal(t, "application/json", value)
169+
})
170+
171+
t.Run("use matcher", func(t *testing.T) {
172+
m := New(t, router)
173+
174+
m.Get("/api").
175+
Expect().
176+
Header("Content-Type", Regex("application/[json]"))
177+
})
178+
})
179+
146180
t.Run("POST basic JSON", func(t *testing.T) {
147181
request.Post("/json").
148182
ContentType("application/json").

0 commit comments

Comments
 (0)