-
Notifications
You must be signed in to change notification settings - Fork 0
/
body_matcher.go
40 lines (32 loc) · 890 Bytes
/
body_matcher.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
package hex
import (
"fmt"
"net/http"
)
// WithBody adds matching conditions against a request's body.
// See WithQuery for usage instructions
func (e *Expectation) WithBody(args ...interface{}) *Expectation {
matcher, err := makeURLValuesMatcher(args)
if err != nil {
panic(fmt.Sprintf("WithBody: %s", err.Error()))
}
e.matchers = append(e.matchers, &bodyMatcher{
args: args,
urlValuesMatcher: matcher,
})
return e
}
type bodyMatcher struct {
args []interface{}
urlValuesMatcher urlValuesMatcher
}
var _ matcher = &bodyMatcher{}
func (b *bodyMatcher) matches(req *http.Request) bool {
if err := req.ParseForm(); err != nil {
panic("An error occurred while parsing a form")
}
return b.urlValuesMatcher.matches(req.PostForm)
}
func (b *bodyMatcher) String() string {
return fmt.Sprintf("body matching %v", matcherArgsToString(b.args))
}