-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatchers.go
62 lines (51 loc) · 1.18 KB
/
matchers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package mockhttp
import (
"io"
"net/http"
"net/url"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
)
type Matcher2 interface {
Match(r *http.Request) bool
Diff(r *http.Request) string
}
type queryParamMatcher struct {
expected url.Values
}
func (q queryParamMatcher) Match(r *http.Request) bool {
return cmp.Equal(q.expected, r.URL.Query())
}
func (q queryParamMatcher) Diff(r *http.Request) string {
return cmp.Diff(q.expected, r.URL.Query())
}
func MatchQueryParams2(qp url.Values) Matcher2 {
return queryParamMatcher{expected: qp}
}
type Matcher func(t *testing.T, r *http.Request)
func MatchQueryParams(qp url.Values) Matcher {
return func(t *testing.T, r *http.Request) {
t.Helper()
assert.Equal(t, qp, r.URL.Query())
}
}
func MatchHeader(headers http.Header) Matcher {
return func(t *testing.T, r *http.Request) {
t.Helper()
for k, v := range headers {
assert.Equal(t, v, r.Header[k])
}
}
}
func MatchJSONBody(jsonBody string) Matcher {
return func(t *testing.T, r *http.Request) {
t.Helper()
body, err := io.ReadAll(r.Body)
if err != nil {
t.Error(err.Error())
return
}
assert.JSONEq(t, jsonBody, string(body))
}
}