This repository was archived by the owner on Mar 13, 2025. It is now read-only.
forked from wiremock/go-wiremock
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstub_rule.go
More file actions
236 lines (202 loc) · 7.42 KB
/
Copy pathstub_rule.go
File metadata and controls
236 lines (202 loc) · 7.42 KB
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
package wiremock
import (
"encoding/base64"
"encoding/json"
"net/http"
"time"
uuidPkg "github.com/google/uuid"
)
const ScenarioStateStarted = "Started"
// ParamMatcherInterface is pair ParamMatchingStrategy and string matched value
type ParamMatcherInterface interface {
Strategy() ParamMatchingStrategy
Value() string
Flags() map[string]bool
}
// URLMatcherInterface is pair URLMatchingStrategy and string matched value
type URLMatcherInterface interface {
Strategy() URLMatchingStrategy
Value() string
}
type response struct {
body *string
base64Body []byte
bodyFileName *string
jsonBody interface{}
headers map[string]string
status int64
fixedDelayMilliseconds time.Duration
}
// StubRule is struct of http Request body to WireMock
type StubRule struct {
uuid string
request *Request
response response
priority *int64
scenarioName *string
requiredScenarioState *string
newScenarioState *string
}
// 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: response{
status: http.StatusOK,
},
}
}
// 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 ParamMatcherInterface) *StubRule {
s.request.WithQueryParam(param, matcher)
return s
}
// WithHeader adds header to Headers and returns *StubRule
func (s *StubRule) WithHeader(header string, matcher ParamMatcherInterface) *StubRule {
s.request.WithHeader(header, matcher)
return s
}
// WithCookie adds cookie and returns *StubRule
func (s *StubRule) WithCookie(cookie string, matcher ParamMatcherInterface) *StubRule {
s.request.WithCookie(cookie, matcher)
return s
}
// WithBodyPattern adds body pattern and returns *StubRule
func (s *StubRule) WithBodyPattern(matcher ParamMatcher) *StubRule {
s.request.WithBodyPattern(matcher)
return s
}
// WithMultipartPattern adds multipart body pattern and returns *StubRule
func (s *StubRule) WithMultipartPattern(pattern *MultipartPattern) *StubRule {
s.request.WithMultipartPattern(pattern)
return s
}
// WillReturn sets response and returns *StubRule
func (s *StubRule) WillReturn(body string, headers map[string]string, status int64) *StubRule {
s.response.body = &body
s.response.headers = headers
s.response.status = status
return s
}
// WillReturnBinary sets response with binary body and returns *StubRule
func (s *StubRule) WillReturnBinary(body []byte, headers map[string]string, status int64) *StubRule {
s.response.base64Body = body
s.response.headers = headers
s.response.status = status
return s
}
// 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.bodyFileName = &bodyFileName
s.response.headers = headers
s.response.status = status
return s
}
// WillReturnJSON sets response with json body and returns *StubRule
func (s *StubRule) WillReturnJSON(json interface{}, headers map[string]string, status int64) *StubRule {
s.response.jsonBody = json
s.response.headers = headers
s.response.status = status
return s
}
// WithFixedDelayMilliseconds sets fixed delay milliseconds for response
func (s *StubRule) WithFixedDelayMilliseconds(time time.Duration) *StubRule {
s.response.fixedDelayMilliseconds = time
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)
}
// 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 struct {
Body string `json:"body,omitempty"`
Base64Body string `json:"base64Body,omitempty"`
BodyFileName string `json:"bodyFileName,omitempty"`
JSONBody interface{} `json:"jsonBody,omitempty"`
Headers map[string]string `json:"headers,omitempty"`
Status int64 `json:"status,omitempty"`
FixedDelayMilliseconds int `json:"fixedDelayMilliseconds,omitempty"`
} `json:"response"`
}{}
jsonStubRule.Priority = s.priority
jsonStubRule.ScenarioName = s.scenarioName
jsonStubRule.RequiredScenarioScenarioState = s.requiredScenarioState
jsonStubRule.NewScenarioState = s.newScenarioState
if s.response.body != nil {
jsonStubRule.Response.Body = *s.response.body
} else if len(s.response.base64Body) > 0 {
jsonStubRule.Response.Base64Body = base64.StdEncoding.EncodeToString(s.response.base64Body)
} else if s.response.bodyFileName != nil {
jsonStubRule.Response.BodyFileName = *s.response.bodyFileName
} else if s.response.jsonBody != nil {
jsonStubRule.Response.JSONBody = s.response.jsonBody
}
jsonStubRule.Response.Headers = s.response.headers
jsonStubRule.Response.Status = s.response.status
jsonStubRule.Response.FixedDelayMilliseconds = int(s.response.fixedDelayMilliseconds.Milliseconds())
jsonStubRule.Request = s.request
jsonStubRule.ID = s.uuid
jsonStubRule.UUID = s.uuid
return json.Marshal(jsonStubRule)
}