-
Notifications
You must be signed in to change notification settings - Fork 3
/
gock.go
53 lines (42 loc) · 1.07 KB
/
gock.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
package httptestutils
import (
http "net/http"
gomock "github.com/golang/mock/gomock"
"gopkg.in/h2non/gock.v1"
)
type GockMatcher struct {
gock gock.Mock
}
func NewGockRequest() *gock.Request {
req := gock.NewRequest()
req.Response = gock.NewResponse()
return req
}
func NewGockMatcher(req *gock.Request) *GockMatcher {
if req.Response == nil {
req.Response = gock.NewResponse()
}
return &GockMatcher{
gock: gock.NewMock(req, req.Response),
}
}
// Matches returns whether x is a match.
func (m *GockMatcher) Matches(x interface{}) bool {
req, ok := x.(*http.Request)
if !ok {
return false
}
match, err := m.gock.Match(req)
if err != nil {
return false
}
return match
}
// String describes what the matcher matches.
func (m *GockMatcher) String() string {
return "HTTP request matching right method"
}
// Gock indicates an expected call of Do.
func (mr *MockSenderMockRecorder) Gock(req *gock.Request) *gomock.Call {
return mr.Do(NewGockMatcher(req)).DoAndReturn(func(r *http.Request) (*http.Response, error) { return gock.Responder(r, req.Response, nil) })
}