-
Notifications
You must be signed in to change notification settings - Fork 0
/
body.go
55 lines (41 loc) · 1019 Bytes
/
body.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
package matcher
import (
"net/http"
"go.nhat.io/matcher/v2"
"go.nhat.io/httpmock/value"
)
const initActual = "<could not decode>"
var _ matcher.Matcher = (*BodyMatcher)(nil)
// BodyMatcher matches a body of a request.
type BodyMatcher struct {
matcher matcher.Matcher
actual string
}
// Matcher returns the underlay matcher.
func (m *BodyMatcher) Matcher() matcher.Matcher {
return m.matcher
}
// Match satisfies matcher.Matcher interface.
func (m *BodyMatcher) Match(in any) (bool, error) {
m.actual = initActual
actual, err := value.GetBody(in.(*http.Request))
if err != nil {
return false, err
}
m.actual = string(actual)
return m.matcher.Match(m.actual)
}
// Actual returns the decoded input.
func (m BodyMatcher) Actual() string {
return m.actual
}
// Expected returns the expectation.
func (m BodyMatcher) Expected() string {
return m.matcher.Expected()
}
// Body initiates a new body matcher.
func Body(v any) *BodyMatcher {
return &BodyMatcher{
matcher: matcher.Match(v),
}
}