-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
builder.go
241 lines (202 loc) · 6.72 KB
/
builder.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
package mocha
import (
"net/http"
"github.com/vitorsalgado/mocha/v3/expect"
"github.com/vitorsalgado/mocha/v3/params"
"github.com/vitorsalgado/mocha/v3/reply"
)
// MockBuilder is a builder for mock.Mock.
type MockBuilder struct {
mock *Mock
}
// Request creates a new empty MockBuilder.
func Request() *MockBuilder {
return &MockBuilder{mock: newMock()}
}
// Get inits a mock for GET method.
func Get(m expect.Matcher) *MockBuilder {
return Request().URL(m).Method(http.MethodGet)
}
// Post inits a mock for Post method.
func Post(m expect.Matcher) *MockBuilder {
return Request().URL(m).Method(http.MethodPost)
}
// Put inits a mock for Put method.
func Put(m expect.Matcher) *MockBuilder {
return Request().URL(m).Method(http.MethodPut)
}
// Patch inits a mock for Patch method.
func Patch(u expect.Matcher) *MockBuilder {
return Request().URL(u).Method(http.MethodPatch)
}
// Delete inits a mock for Delete method.
func Delete(m expect.Matcher) *MockBuilder {
return Request().URL(m).Method(http.MethodDelete)
}
// Head inits a mock for Head method.
func Head(m expect.Matcher) *MockBuilder {
return Request().URL(m).Method(http.MethodHead)
}
// Options inits a mock for Options method.
func Options(m expect.Matcher) *MockBuilder {
return Request().URL(m).Method(http.MethodOptions)
}
// Name defines a name for the mock.
// Useful to debug.
func (b *MockBuilder) Name(name string) *MockBuilder {
b.mock.Name = name
return b
}
// Priority sets the priority of the mock.
// A higher priority will take precedence during request matching.
func (b *MockBuilder) Priority(p int) *MockBuilder {
b.mock.Priority = p
return b
}
// Method sets the HTTP request method to be matched.
func (b *MockBuilder) Method(method string) *MockBuilder {
b.mock.Expectations = append(
b.mock.Expectations,
Expectation{
Target: "method",
ValueSelector: func(r *expect.RequestInfo) any { return r.Request.Method },
Matcher: expect.ToEqualFold(method),
Weight: _weightNone,
})
return b
}
// URL defines a matcher to be applied to the http.Request url.URL.
func (b *MockBuilder) URL(m expect.Matcher) *MockBuilder {
b.mock.Expectations = append(
b.mock.Expectations,
Expectation{
Target: "url",
ValueSelector: func(r *expect.RequestInfo) any { return r.Request.URL },
Matcher: m,
Weight: _weightRegular,
})
return b
}
// Header adds a matcher to a specific http.Header key.
func (b *MockBuilder) Header(key string, m expect.Matcher) *MockBuilder {
b.mock.Expectations = append(
b.mock.Expectations,
Expectation{
Target: "header",
ValueSelector: func(r *expect.RequestInfo) any { return r.Request.Header.Get(key) },
Matcher: m,
Weight: _weightLow,
})
return b
}
// Query defines a matcher to a specific query.
func (b *MockBuilder) Query(key string, m expect.Matcher) *MockBuilder {
b.mock.Expectations = append(
b.mock.Expectations,
Expectation{
Target: "query",
ValueSelector: func(r *expect.RequestInfo) any { return r.Request.URL.Query().Get(key) },
Matcher: m,
Weight: _weightVeryLow,
})
return b
}
// Body adds matchers to the request body.
// If request contains a JSON body, you can provide multiple matchers to several fields.
// Example:
//
// m.Body(JSONPath("name", EqualTo("test")), JSONPath("address.street", ToContains("nowhere")))
func (b *MockBuilder) Body(matcherList ...expect.Matcher) *MockBuilder {
for _, m := range matcherList {
b.mock.Expectations = append(b.mock.Expectations,
Expectation{
Target: "body",
ValueSelector: func(r *expect.RequestInfo) any { return r.ParsedBody },
Matcher: m,
Weight: _weightHigh,
})
}
return b
}
// FormField defines a matcher for a specific form field by its key.
func (b *MockBuilder) FormField(field string, m expect.Matcher) *MockBuilder {
b.mock.Expectations = append(b.mock.Expectations,
Expectation{
Target: "form",
ValueSelector: func(r *expect.RequestInfo) any { return r.Request.Form.Get(field) },
Matcher: m,
Weight: _weightVeryLow,
})
return b
}
// Repeat defines to total times that a mock should be served, if request matches.
func (b *MockBuilder) Repeat(times int) *MockBuilder {
b.mock.Repeat = times
return b
}
// RequestMatches defines expect.Matcher to be applied to a http.Request.
func (b *MockBuilder) RequestMatches(m expect.Matcher) *MockBuilder {
b.mock.Expectations = append(
b.mock.Expectations,
Expectation{
Target: "request",
ValueSelector: func(r *expect.RequestInfo) any { return r.Request },
Matcher: m,
Weight: _weightLow,
})
return b
}
// StartScenario sets that this mock will start a new scenario with the given name.
func (b *MockBuilder) StartScenario(name string) *MockBuilder {
b.mock.ScenarioName = name
b.mock.ScenarioRequiredState = _scenarioStateStarted
return b
}
// ScenarioIs mark this mock to be used only within the given scenario.
func (b *MockBuilder) ScenarioIs(scenario string) *MockBuilder {
b.mock.ScenarioName = scenario
return b
}
// ScenarioStateIs mark this mock to be served only if the scenario state is equal to the given required state.
func (b *MockBuilder) ScenarioStateIs(requiredState string) *MockBuilder {
b.mock.ScenarioRequiredState = requiredState
return b
}
// ScenarioStateWillBe defines the state of the scenario after this mock is matched, making the scenario flow continue.
func (b *MockBuilder) ScenarioStateWillBe(newState string) *MockBuilder {
b.mock.ScenarioNewState = newState
return b
}
// PostAction adds a post action to be executed after the mocked response is served.
func (b *MockBuilder) PostAction(action PostAction) *MockBuilder {
b.mock.PostActions = append(b.mock.PostActions, action)
return b
}
// Reply defines a response mock to be served if this mock matches to a request.
func (b *MockBuilder) Reply(rep reply.Reply) *MockBuilder {
b.mock.Reply = rep
return b
}
// ReplyFunction defines a function to will build the response mock.
func (b *MockBuilder) ReplyFunction(fn func(*http.Request, reply.M, params.P) (*reply.Response, error)) *MockBuilder {
b.mock.Reply = reply.Function(fn)
return b
}
// ReplyJust sets the mock to return a simple response with the given status code.
// Optionally, you can provide a reply as well. The status provided in the first parameter will prevail.
// Only the first reply will be used.
func (b *MockBuilder) ReplyJust(status int, r ...*reply.StdReply) *MockBuilder {
if len(r) > 0 {
rep := r[0]
rep.Status(status)
b.mock.Reply = rep
} else {
b.mock.Reply = reply.Status(status)
}
return b
}
// Build builds a mock.Mock with previously configured parameters.
// Used internally by Mocha.
func (b *MockBuilder) Build() *Mock {
return b.mock
}