-
Notifications
You must be signed in to change notification settings - Fork 19
/
stub_rule.go
277 lines (234 loc) · 9.24 KB
/
stub_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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package wiremock
import (
"encoding/json"
"net/http"
"time"
uuidPkg "github.com/google/uuid"
)
const ScenarioStateStarted = "Started"
const authorizationHeader = "Authorization"
// StubRule is struct of http Request body to WireMock
type StubRule struct {
uuid string
request *Request
response ResponseInterface
fixedDelayMilliseconds *int64
priority *int64
scenarioName *string
requiredScenarioState *string
newScenarioState *string
postServeActions []WebhookInterface
}
// NewStubRule returns a new *StubRule.
func NewStubRule(method string, urlMatcher URLMatcher) *StubRule {
uuid, _ := uuidPkg.NewRandom()
return &StubRule{
uuid: uuid.String(),
request: NewRequest(method, urlMatcher),
response: NewResponse(),
}
}
// Request is getter for Request
func (s *StubRule) Request() *Request {
return s.request
}
// WithQueryParam adds query param and returns *StubRule
func (s *StubRule) WithQueryParam(param string, matcher MatcherInterface) *StubRule {
s.request.WithQueryParam(param, matcher)
return s
}
// WithPathParam adds path param and returns *StubRule
func (s *StubRule) WithPathParam(param string, matcher MatcherInterface) *StubRule {
s.request.WithPathParam(param, matcher)
return s
}
// WithPort adds port and returns *StubRule
func (s *StubRule) WithPort(port int64) *StubRule {
s.request.WithPort(port)
return s
}
// WithScheme adds scheme and returns *StubRule
func (s *StubRule) WithScheme(scheme string) *StubRule {
s.request.WithScheme(scheme)
return s
}
// WithHost adds host and returns *StubRule
func (s *StubRule) WithHost(host BasicParamMatcher) *StubRule {
s.request.WithHost(host)
return s
}
// WithHeader adds header to Headers and returns *StubRule
func (s *StubRule) WithHeader(header string, matcher MatcherInterface) *StubRule {
s.request.WithHeader(header, matcher)
return s
}
// WithCookie adds cookie and returns *StubRule
func (s *StubRule) WithCookie(cookie string, matcher BasicParamMatcher) *StubRule {
s.request.WithCookie(cookie, matcher)
return s
}
// WithBodyPattern adds body pattern and returns *StubRule
func (s *StubRule) WithBodyPattern(matcher BasicParamMatcher) *StubRule {
s.request.WithBodyPattern(matcher)
return s
}
// WithFormParameter adds form parameter and returns *StubRule
func (s *StubRule) WithFormParameter(param string, matcher BasicParamMatcher) *StubRule {
s.request.WithFormParameter(param, matcher)
return s
}
// WithMultipartPattern adds multipart body pattern and returns *StubRule
func (s *StubRule) WithMultipartPattern(pattern *MultipartPattern) *StubRule {
s.request.WithMultipartPattern(pattern)
return s
}
// WithAuthToken adds Authorization header with Token auth method *StubRule
func (s *StubRule) WithAuthToken(tokenMatcher BasicParamMatcher) *StubRule {
methodPrefix := "Token "
m := addAuthMethodToMatcher(tokenMatcher, methodPrefix)
s.WithHeader(authorizationHeader, StartsWith(methodPrefix).And(m))
return s
}
// WithBearerToken adds Authorization header with Bearer auth method *StubRule
func (s *StubRule) WithBearerToken(tokenMatcher BasicParamMatcher) *StubRule {
methodPrefix := "Bearer "
m := addAuthMethodToMatcher(tokenMatcher, methodPrefix)
s.WithHeader(authorizationHeader, StartsWith(methodPrefix).And(m))
return s
}
// WithDigestAuth adds Authorization header with Digest auth method *StubRule
func (s *StubRule) WithDigestAuth(matcher BasicParamMatcher) *StubRule {
methodPrefix := "Digest "
m := addAuthMethodToMatcher(matcher, methodPrefix)
s.WithHeader(authorizationHeader, StartsWith(methodPrefix).And(m))
return s
}
// Deprecated: Use WillReturnResponse(NewResponse().WithBody(body).WithHeaders(headers).WithStatus(status)) instead
// WillReturn sets response and returns *StubRule
func (s *StubRule) WillReturn(body string, headers map[string]string, status int64) *StubRule {
s.response = NewResponse().WithBody(body).WithStatus(status).WithHeaders(headers)
return s
}
// Deprecated: Use WillReturnResponse(NewResponse().WithBinaryBody(body).WithHeaders(headers).WithStatus(status)) instead
// WillReturnBinary sets response with binary body and returns *StubRule
func (s *StubRule) WillReturnBinary(body []byte, headers map[string]string, status int64) *StubRule {
s.response = NewResponse().WithBinaryBody(body).WithStatus(status).WithHeaders(headers)
return s
}
// Deprecated: Use WillReturnResponse(NewResponse().WithBodyFile(file).WithHeaders(headers).WithStatus(status)) instead
// WillReturnFileContent sets response with some file content and returns *StubRule
func (s *StubRule) WillReturnFileContent(bodyFileName string, headers map[string]string, status int64) *StubRule {
s.response = NewResponse().WithBodyFile(bodyFileName).WithStatus(status).WithHeaders(headers)
return s
}
// Deprecated: Use WillReturnResponse(NewResponse().WithJsonBody(json).WithHeaders(headers).WithStatus(status)) instead
// WillReturnJSON sets response with json body and returns *StubRule
func (s *StubRule) WillReturnJSON(json interface{}, headers map[string]string, status int64) *StubRule {
s.response = NewResponse().WithJSONBody(json).WithStatus(status).WithHeaders(headers)
return s
}
// Deprecated: Use WillReturnResponse(NewResponse().WithFixedDelay(time.Second)) instead
// WithFixedDelayMilliseconds adds delay to response and returns *StubRule
func (s *StubRule) WithFixedDelayMilliseconds(delay time.Duration) *StubRule {
milliseconds := delay.Milliseconds()
s.fixedDelayMilliseconds = &milliseconds
return s
}
// WillReturnResponse sets response and returns *StubRule
func (s *StubRule) WillReturnResponse(response ResponseInterface) *StubRule {
s.response = response
return s
}
// WithBasicAuth adds basic auth credentials
func (s *StubRule) WithBasicAuth(username, password string) *StubRule {
s.request.WithBasicAuth(username, password)
return s
}
// AtPriority sets priority and returns *StubRule
func (s *StubRule) AtPriority(priority int64) *StubRule {
s.priority = &priority
return s
}
// InScenario sets scenarioName and returns *StubRule
func (s *StubRule) InScenario(scenarioName string) *StubRule {
s.scenarioName = &scenarioName
return s
}
// WhenScenarioStateIs sets requiredScenarioState and returns *StubRule
func (s *StubRule) WhenScenarioStateIs(scenarioState string) *StubRule {
s.requiredScenarioState = &scenarioState
return s
}
// WillSetStateTo sets newScenarioState and returns *StubRule
func (s *StubRule) WillSetStateTo(scenarioState string) *StubRule {
s.newScenarioState = &scenarioState
return s
}
// UUID is getter for uuid
func (s *StubRule) UUID() string {
return s.uuid
}
// Post returns *StubRule for POST method.
func Post(urlMatchingPair URLMatcher) *StubRule {
return NewStubRule(http.MethodPost, urlMatchingPair)
}
// Get returns *StubRule for GET method.
func Get(urlMatchingPair URLMatcher) *StubRule {
return NewStubRule(http.MethodGet, urlMatchingPair)
}
// Delete returns *StubRule for DELETE method.
func Delete(urlMatchingPair URLMatcher) *StubRule {
return NewStubRule(http.MethodDelete, urlMatchingPair)
}
// Put returns *StubRule for PUT method.
func Put(urlMatchingPair URLMatcher) *StubRule {
return NewStubRule(http.MethodPut, urlMatchingPair)
}
// Patch returns *StubRule for PATCH method.
func Patch(urlMatchingPair URLMatcher) *StubRule {
return NewStubRule(http.MethodPatch, urlMatchingPair)
}
func (s *StubRule) WithPostServeAction(extensionName string, webhook WebhookInterface) *StubRule {
s.postServeActions = append(s.postServeActions, webhook.WithName(extensionName))
return s
}
// MarshalJSON makes json body for http Request
func (s *StubRule) MarshalJSON() ([]byte, error) {
jsonStubRule := struct {
UUID string `json:"uuid,omitempty"`
ID string `json:"id,omitempty"`
Priority *int64 `json:"priority,omitempty"`
ScenarioName *string `json:"scenarioName,omitempty"`
RequiredScenarioScenarioState *string `json:"requiredScenarioState,omitempty"`
NewScenarioState *string `json:"newScenarioState,omitempty"`
Request *Request `json:"request"`
Response map[string]interface{} `json:"response"`
PostServeActions []WebhookInterface `json:"postServeActions,omitempty"`
}{}
jsonStubRule.Priority = s.priority
jsonStubRule.ScenarioName = s.scenarioName
jsonStubRule.RequiredScenarioScenarioState = s.requiredScenarioState
jsonStubRule.NewScenarioState = s.newScenarioState
jsonStubRule.Response = s.response.ParseResponse()
jsonStubRule.PostServeActions = s.postServeActions
if s.fixedDelayMilliseconds != nil {
jsonStubRule.Response["fixedDelayMilliseconds"] = *s.fixedDelayMilliseconds
}
jsonStubRule.Request = s.request
jsonStubRule.ID = s.uuid
jsonStubRule.UUID = s.uuid
return json.Marshal(jsonStubRule)
}
func addAuthMethodToMatcher(matcher BasicParamMatcher, methodPrefix string) BasicParamMatcher {
switch m := matcher.(type) {
case StringValueMatcher:
return m.addPrefixToMatcher(methodPrefix)
case LogicalMatcher:
for i, operand := range m.operands {
m.operands[i] = addAuthMethodToMatcher(operand, methodPrefix)
}
return m
default:
return matcher
}
}