-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstore.go
152 lines (128 loc) · 3.7 KB
/
store.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package gock
import (
"net/http"
"net/url"
"sync"
"github.com/h2non/gock"
)
// MockStore store for HTTP request/response mocks.
type MockStore struct {
mocks []gock.Mock
mutex sync.RWMutex
// Matcher template used when creating new HTTP request/response mocks.
Matcher *gock.MockMatcher
}
// NewStore creates a new mock registry for HTTP request/response mocks. If nil
// is provided as matcher the default matcher is used.
func NewStore(matcher *gock.MockMatcher) *MockStore {
if matcher != nil {
return &MockStore{Matcher: matcher}
}
return &MockStore{Matcher: gock.NewMatcher()}
}
// NewMock creates and registers a new HTTP request/response mock with default
// settings and returns the mock.
func (s *MockStore) NewMock(uri string) gock.Mock {
res := gock.NewResponse()
req := gock.NewRequest()
req.URLStruct, res.Error = url.Parse(uri)
// Create the new mock expectation
mock := gock.NewMock(req, res)
mock.SetMatcher(s.Matcher.Clone())
s.Register(mock)
return mock
}
// Register registers a new HTTP request/response mocks in the current stack.
func (s *MockStore) Register(mock gock.Mock) *MockStore {
if s.Exists(mock) {
return s
}
// Expose mock in request/response for delegation
mock.Request().Mock = mock
mock.Response().Mock = mock
// Make ops thread safe
s.mutex.Lock()
defer s.mutex.Unlock()
// Registers the mock in the global store
s.mocks = append(s.mocks, mock)
return s
}
// Match matches the given `http.Request` with the registered HTTP request
// mocks. It is returning the mock that matches or an error, if a matching
// function fails. If no HTTP mock request matches nothing is returned.
func (s *MockStore) Match(req *http.Request) (gock.Mock, error) {
for _, mock := range s.All() {
matches, err := mock.Match(req)
if err != nil {
return nil, err //nolint:wrapcheck // transparent wrapper
}
if matches {
return mock, nil
}
}
return nil, nil //nolint:nilnil // externally defined behavior
}
// IsDone returns true if all the registered HTTP request/response mocks have
// been triggered successfully.
func (s *MockStore) IsDone() bool {
return !s.IsPending()
}
// IsPending returns true if there are pending HTTP request/response mocks.
func (s *MockStore) IsPending() bool {
return len(s.Pending()) > 0
}
// All returns the current list of registered HTTP request/response mocks.
func (s *MockStore) All() []gock.Mock {
s.mutex.RLock()
defer s.mutex.RUnlock()
return append([]gock.Mock{}, s.mocks...)
}
// Pending returns a slice of the pending HTTP request/response mocks. As a
// side effect the mock store is cleaned up similar as calling `Clean` on it.
func (s *MockStore) Pending() []gock.Mock {
return s.Clean().All()
}
// Exists checks if the given HTTP request/response mocks is already registered.
func (s *MockStore) Exists(m gock.Mock) bool {
s.mutex.RLock()
defer s.mutex.RUnlock()
for _, mock := range s.mocks {
if mock == m {
return true
}
}
return false
}
// Remove removes a registered HTTP request/response mocks by reference.
func (s *MockStore) Remove(m gock.Mock) *MockStore {
for i, mock := range s.mocks {
if mock == m {
s.mutex.Lock()
s.mocks = append(s.mocks[:i], s.mocks[i+1:]...)
s.mutex.Unlock()
}
}
return s
}
// Flush flushes the current stack of registered HTTP request/response mocks.
func (s *MockStore) Flush() *MockStore {
s.mutex.Lock()
defer s.mutex.Unlock()
s.mocks = []gock.Mock{}
return s
}
// Clean cleans the store removing disabled or obsolete HTTP request/response
// mocks.
func (s *MockStore) Clean() *MockStore {
s.mutex.Lock()
defer s.mutex.Unlock()
buf := []gock.Mock{}
for _, mock := range s.mocks {
if mock.Done() {
continue
}
buf = append(buf, mock)
}
s.mocks = buf
return s
}