-
Notifications
You must be signed in to change notification settings - Fork 2
/
store_test.go
245 lines (193 loc) · 4.27 KB
/
store_test.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package gock_test
import (
"net/http"
"net/url"
"testing"
"github.com/stretchr/testify/assert"
"github.com/tkrop/go-testing/gock"
"github.com/tkrop/go-testing/test"
)
func TestStoreRegister(t *testing.T) {
t.Parallel()
// Given
store := gock.NewStore(nil)
// When
mock := store.NewMock("foo")
// Then
mocks := store.All()
assert.Len(t, mocks, 1)
assert.Equal(t, mocks[0], mock)
assert.Equal(t, mock.Request().Mock, mock)
assert.Equal(t, mock.Response().Mock, mock)
// When
store.Register(mock)
// Then
mocks = store.All()
assert.Len(t, mocks, 1)
assert.Equal(t, mocks[0], mock)
}
func TestStoreGetAll(t *testing.T) {
t.Parallel()
// Given
store := gock.NewStore(nil)
mock := store.NewMock("foo")
// When
mocks := store.All()
// Then
assert.Len(t, mocks, 1)
assert.Equal(t, mocks[0], mock)
assert.Len(t, store.All(), 1)
}
func TestStoreExists(t *testing.T) {
t.Parallel()
// Given
store := gock.NewStore(nil)
mock := store.NewMock("foo")
// When
exists := store.Exists(mock)
// Then
mocks := store.All()
assert.Len(t, mocks, 1)
assert.True(t, exists, "mock exists")
assert.Equal(t, mocks[0], mock)
}
func TestStorePending(t *testing.T) {
t.Parallel()
// Given
store := gock.NewStore(NewFooMatcher())
mock := store.NewMock("foo")
done := store.NewMock("http://foo.com")
done.Request().Get("/bar")
ok, err := done.Match(&http.Request{Method: http.MethodGet, URL: &url.URL{
Scheme: "http", Host: "foo.com", Path: "/baz",
}})
// Then
mocks := store.All()
assert.NoError(t, err)
assert.True(t, ok)
assert.Len(t, mocks, 2)
// When
pending := store.Pending()
// Then
mocks = store.All()
assert.Len(t, mocks, 1)
assert.Equal(t, mocks, pending)
assert.Equal(t, mocks[0], mock)
}
func TestStoreIsPending(t *testing.T) {
t.Parallel()
// Given
store := gock.NewStore(nil)
store.NewMock("foo")
// When
pending := store.IsPending()
// Then
assert.True(t, pending, "mock pending")
// When
store.Flush()
pending = store.IsPending()
// Then
assert.False(t, pending, "no mock pending")
}
func TestStoreIsDone(t *testing.T) {
t.Parallel()
// Given
store := gock.NewStore(nil)
store.NewMock("foo")
// When
done := store.IsDone()
// Then
assert.False(t, done, "mocks not done")
// When
store.Flush()
done = store.IsDone()
// Then
assert.True(t, done, "mocks done")
}
func TestStoreRemove(t *testing.T) {
t.Parallel()
// Given
store := gock.NewStore(nil)
mock := store.NewMock("foo")
// When
exists := store.Exists(mock)
// Then
mocks := store.All()
assert.Len(t, mocks, 1)
assert.True(t, exists, "mock exists")
// When
store.Remove(mock)
exists = store.Exists(mock)
// Then
assert.False(t, exists, "mock exists not")
// When
store.Remove(mock)
exists = store.Exists(mock)
// Then
assert.False(t, exists, "mock exists not")
}
func TestStoreFlush(t *testing.T) {
t.Parallel()
// Given
store := gock.NewStore(nil)
mock1 := store.NewMock("foo")
mock2 := store.NewMock("foo")
// Then
assert.Len(t, store.All(), 2)
assert.True(t, store.Exists(mock1), "mock1 exists")
assert.True(t, store.Exists(mock2), "mock2 exists")
// When
store.Flush()
// Then
assert.Len(t, store.All(), 0)
assert.False(t, store.Exists(mock1), "mock1 exists not")
assert.False(t, store.Exists(mock2), "mock2 exists not")
}
type MatchParams struct {
url string
expectMatch bool
expectError error
}
var testMatchParams = map[string]MatchParams{
"match with bar": {
url: "http://foo.com/bar",
expectMatch: true,
},
"match with baz": {
url: "http://foo.com/baz",
expectMatch: true,
},
"missing host": {
url: "http://bar.com/baz",
},
"missing path": {
url: "http://foo.com/foo",
},
"missing schema": {
url: "https://foo.com/bar",
expectError: errAny,
},
}
func TestMatch(t *testing.T) {
test.Map(t, testMatchParams).
Run(func(t test.Test, param MatchParams) {
// Given
store := gock.NewStore(NewFooMatcher())
mock := store.NewMock(param.url)
// When
uri, _ := url.Parse(param.url)
match, err := store.Match(
&http.Request{URL: uri})
// Then
if param.expectError != nil {
assert.Equal(t, param.expectError, err)
} else {
assert.NoError(t, err)
}
if param.expectMatch {
assert.Equal(t, match, mock)
} else {
assert.Nil(t, match)
}
})
}