-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrule.go
61 lines (52 loc) · 1.55 KB
/
rule.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
package mockhttp
import (
"errors"
"github.com/esammer/mockhttp/matcher"
"github.com/esammer/mockhttp/responder"
"net/http"
)
// A request/response rule.
//
// A rule pairs a matcher with a responder. When the former matches a request, the latter response is invoked to
// generate a response. For convenience, you're encouraged to use NewRule() to create rules.
type Rule struct {
Matcher matcher.Matcher
Responder responder.Responder
}
// Create a new rule.
//
// Panics if either matcher or responder are nil.
func NewRule(matcher matcher.Matcher, responder responder.Responder) *Rule {
if matcher == nil {
panic(errors.New("matcher can not be nil"))
}
if responder == nil {
panic(errors.New("responder can not be nil"))
}
return &Rule{
Matcher: matcher,
Responder: responder,
}
}
// A convenience method that invokes the configured matcher.
//
// This method is equivalent to:
// r.Matcher.Match(req)
func (r *Rule) Match(req *http.Request) bool {
return r.Matcher.Match(req)
}
// A convenience method that invokes the responder if the matcher matches.
//
// If the configured matcher matches, the responder is invoked and its response is returned along with a true value
// indicating there was a match. If there is no match the response is nil and false is returned. Any error produced by
// the responder causes a panic.
func (r *Rule) MatchAndInvoke(req *http.Request) (*http.Response, bool) {
if r.Matcher.Match(req) {
resp, err := r.Responder.Response(req)
if err != nil {
panic(err)
}
return resp, true
}
return nil, false
}