Skip to content

Commit ec5b9b2

Browse files
authored
refactoring webhook (#17)
1 parent 170000e commit ec5b9b2

File tree

6 files changed

+142
-78
lines changed

6 files changed

+142
-78
lines changed

client_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,14 @@ func TestStubRule_ToJson(t *testing.T) {
5050
WithHeader("Content-Type", "application/json").
5151
WithBody(`{"code": 400, "detail": "detail"}`).
5252
WithFault(FaultConnectionResetByPeer).
53-
WithFixedDelay(time.Second * 5),
53+
WithFixedDelay(time.Second*5),
5454
).
55-
WithPostServeAction("webhook", Webhook().
55+
WithPostServeAction("webhook", NewWebhook().
5656
WithMethod("POST").
5757
WithURL("http://my-target-host/callback").
5858
WithHeader("Content-Type", "application/json").
59-
WithBody(`{ "result": "SUCCESS" }`)).
59+
WithBody(`{ "result": "SUCCESS" }`).
60+
WithFixedDelay(time.Second)).
6061
AtPriority(1).
6162
InScenario("Scenario").
6263
WhenScenarioStateIs("Started").

delay.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package wiremock
22

3-
import "encoding/json"
3+
import (
4+
"encoding/json"
5+
"time"
6+
)
47

58
type DelayInterface interface {
69
ParseDelay() map[string]interface{}
@@ -56,3 +59,23 @@ func (d chunkedDribbleDelay) MarshalJSON() ([]byte, error) {
5659

5760
return json.Marshal(jsonMap)
5861
}
62+
63+
func NewLogNormalRandomDelay(median time.Duration, sigma float64) DelayInterface {
64+
return logNormalRandomDelay{
65+
median: median.Milliseconds(),
66+
sigma: sigma,
67+
}
68+
}
69+
70+
func NewFixedDelay(delay time.Duration) DelayInterface {
71+
return fixedDelay{
72+
milliseconds: delay.Milliseconds(),
73+
}
74+
}
75+
76+
func NewUniformRandomDelay(lower, upper time.Duration) DelayInterface {
77+
return uniformRandomDelay{
78+
lower: lower.Milliseconds(),
79+
upper: upper.Milliseconds(),
80+
}
81+
}

expected-template.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,11 @@
122122
"headers": {
123123
"Content-Type": "application/json"
124124
},
125-
"body": "{ \"result\": \"SUCCESS\" }"
125+
"body": "{ \"result\": \"SUCCESS\" }",
126+
"delay": {
127+
"type": "fixed",
128+
"milliseconds": 1000
129+
}
126130
}
127131
}
128132
]

response.go

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,31 +37,26 @@ func NewResponse() Response {
3737
}
3838

3939
// WithLogNormalRandomDelay sets log normal random delay for response
40-
func (r Response) WithLogNormalRandomDelay(mediana time.Duration, sigma float64) Response {
41-
r.delayDistribution = logNormalRandomDelay{
42-
median: mediana.Milliseconds(),
43-
sigma: sigma,
44-
}
45-
40+
func (r Response) WithLogNormalRandomDelay(median time.Duration, sigma float64) Response {
41+
r.delayDistribution = NewLogNormalRandomDelay(median, sigma)
4642
return r
4743
}
4844

4945
// WithUniformRandomDelay sets uniform random delay for response
5046
func (r Response) WithUniformRandomDelay(lower, upper time.Duration) Response {
51-
r.delayDistribution = uniformRandomDelay{
52-
lower: lower.Milliseconds(),
53-
upper: upper.Milliseconds(),
54-
}
55-
47+
r.delayDistribution = NewUniformRandomDelay(lower, upper)
5648
return r
5749
}
5850

5951
// WithFixedDelay sets fixed delay milliseconds for response
60-
func (r Response) WithFixedDelay(time time.Duration) Response {
61-
r.delayDistribution = fixedDelay{
62-
milliseconds: time.Milliseconds(),
63-
}
52+
func (r Response) WithFixedDelay(delay time.Duration) Response {
53+
r.delayDistribution = NewFixedDelay(delay)
54+
return r
55+
}
6456

57+
// WithDelay sets delay for response
58+
func (r Response) WithDelay(delay DelayInterface) Response {
59+
r.delayDistribution = delay
6560
return r
6661
}
6762

stub_rule.go

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,6 @@ import (
1010

1111
const ScenarioStateStarted = "Started"
1212

13-
type Matcher interface {
14-
StringValueMatcher | MultiValueMatcher
15-
}
16-
17-
type postServeAction struct {
18-
extensionName string
19-
parameters PostServeActionParameters
20-
}
21-
22-
type PostServeActionParameters interface {
23-
ToMap() map[string]interface{}
24-
}
25-
2613
// StubRule is struct of http Request body to WireMock
2714
type StubRule struct {
2815
uuid string
@@ -33,7 +20,7 @@ type StubRule struct {
3320
scenarioName *string
3421
requiredScenarioState *string
3522
newScenarioState *string
36-
postServeActions []postServeAction
23+
postServeActions []WebhookInterface
3724
}
3825

3926
// NewStubRule returns a new *StubRule.
@@ -201,11 +188,8 @@ func Patch(urlMatchingPair URLMatcher) *StubRule {
201188
return NewStubRule(http.MethodPatch, urlMatchingPair)
202189
}
203190

204-
func (s *StubRule) WithPostServeAction(extensionName string, parameters PostServeActionParameters) *StubRule {
205-
s.postServeActions = append(s.postServeActions, postServeAction{
206-
extensionName: extensionName,
207-
parameters: parameters,
208-
})
191+
func (s *StubRule) WithPostServeAction(extensionName string, webhook WebhookInterface) *StubRule {
192+
s.postServeActions = append(s.postServeActions, webhook.WithName(extensionName))
209193
return s
210194
}
211195

@@ -220,26 +204,15 @@ func (s *StubRule) MarshalJSON() ([]byte, error) {
220204
NewScenarioState *string `json:"newScenarioState,omitempty"`
221205
Request *Request `json:"request"`
222206
Response map[string]interface{} `json:"response"`
223-
PostServeActions []struct {
224-
Name string `json:"name"`
225-
Parameters map[string]interface{} `json:"parameters"`
226-
} `json:"postServeActions,omitempty"`
207+
PostServeActions []WebhookInterface `json:"postServeActions,omitempty"`
227208
}{}
209+
228210
jsonStubRule.Priority = s.priority
229211
jsonStubRule.ScenarioName = s.scenarioName
230212
jsonStubRule.RequiredScenarioScenarioState = s.requiredScenarioState
231213
jsonStubRule.NewScenarioState = s.newScenarioState
232214
jsonStubRule.Response = s.response.ParseResponse()
233-
234-
for _, postServeAction := range s.postServeActions {
235-
jsonStubRule.PostServeActions = append(jsonStubRule.PostServeActions, struct {
236-
Name string `json:"name"`
237-
Parameters map[string]interface{} `json:"parameters"`
238-
}{
239-
Name: postServeAction.extensionName,
240-
Parameters: postServeAction.parameters.ToMap(),
241-
})
242-
}
215+
jsonStubRule.PostServeActions = s.postServeActions
243216

244217
if s.fixedDelayMilliseconds != nil {
245218
jsonStubRule.Response["fixedDelayMilliseconds"] = *s.fixedDelayMilliseconds

webhook.go

Lines changed: 93 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,113 @@
11
package wiremock
22

3-
type WebhookDefinition struct {
4-
m map[string]any
3+
import (
4+
"encoding/json"
5+
"time"
6+
)
7+
8+
type WebhookInterface interface {
9+
json.Marshaler
10+
WithName(name string) WebhookInterface
11+
ParseWebhook() map[string]interface{}
12+
}
13+
14+
type Webhook struct {
15+
name string
16+
parameters webhookParameters
17+
}
18+
19+
type webhookParameters struct {
20+
method string
21+
url string
22+
body string
23+
headers map[string]string
24+
delay DelayInterface
25+
}
26+
27+
func (w webhookParameters) MarshalJSON() ([]byte, error) {
28+
jsonMap := map[string]interface{}{
29+
"method": w.method,
30+
"url": w.url,
31+
"body": w.body,
32+
"headers": w.headers,
33+
"delay": w.delay.ParseDelay(),
34+
}
35+
36+
return json.Marshal(jsonMap)
37+
}
38+
39+
// WithName sets the name of the webhook and returns the webhook.
40+
func (w Webhook) WithName(name string) WebhookInterface {
41+
w.name = name
42+
return w
543
}
644

7-
func Webhook() WebhookDefinition {
8-
return WebhookDefinition{
9-
m: make(map[string]any),
45+
// ParseWebhook returns a map representation of the webhook.
46+
func (w Webhook) ParseWebhook() map[string]interface{} {
47+
return map[string]interface{}{
48+
"name": w.name,
49+
"parameters": w.parameters,
1050
}
1151
}
1252

13-
func (d WebhookDefinition) WithMethod(method string) WebhookDefinition {
14-
d.m["method"] = method
15-
return d
53+
// MarshalJSON implements the json.Marshaler interface.
54+
func (w Webhook) MarshalJSON() ([]byte, error) {
55+
return json.Marshal(w.ParseWebhook())
1656
}
1757

18-
func (d WebhookDefinition) WithURL(url string) WebhookDefinition {
19-
d.m["url"] = url
20-
return d
58+
// WithMethod sets the HTTP method of the webhook.
59+
func (w Webhook) WithMethod(method string) Webhook {
60+
w.parameters.method = method
61+
return w
2162
}
2263

23-
func (d WebhookDefinition) WithHeader(key string, value string) WebhookDefinition {
24-
var headers map[string]string
64+
// WithURL sets the URL of the webhook.
65+
func (w Webhook) WithURL(url string) Webhook {
66+
w.parameters.url = url
67+
return w
68+
}
2569

26-
if headersAny, ok := d.m["headers"]; ok {
27-
headers = headersAny.(map[string]string)
28-
} else {
29-
headers = make(map[string]string)
30-
d.m["headers"] = headers
70+
// WithHeader sets a header of the webhook.
71+
func (w Webhook) WithHeader(key string, value string) Webhook {
72+
if w.parameters.headers == nil {
73+
w.parameters.headers = make(map[string]string)
3174
}
3275

33-
headers[key] = value
76+
w.parameters.headers[key] = value
77+
78+
return w
79+
}
80+
81+
// WithBody sets the body of the webhook.
82+
func (w Webhook) WithBody(body string) Webhook {
83+
w.parameters.body = body
84+
return w
85+
}
86+
87+
// WithDelay sets the delay of the webhook.
88+
func (w Webhook) WithDelay(delay DelayInterface) Webhook {
89+
w.parameters.delay = delay
90+
return w
91+
}
92+
93+
// WithFixedDelay sets the fixed delay of the webhook.
94+
func (w Webhook) WithFixedDelay(delay time.Duration) Webhook {
95+
w.parameters.delay = NewFixedDelay(delay)
96+
return w
97+
}
3498

35-
return d
99+
// WithLogNormalRandomDelay sets the log normal delay of the webhook.
100+
func (w Webhook) WithLogNormalRandomDelay(median time.Duration, sigma float64) Webhook {
101+
w.parameters.delay = NewLogNormalRandomDelay(median, sigma)
102+
return w
36103
}
37104

38-
func (d WebhookDefinition) WithBody(body string) WebhookDefinition {
39-
d.m["body"] = body
40-
return d
105+
// WithUniformRandomDelay sets the uniform random delay of the webhook.
106+
func (w Webhook) WithUniformRandomDelay(lower, upper time.Duration) Webhook {
107+
w.parameters.delay = NewUniformRandomDelay(lower, upper)
108+
return w
41109
}
42110

43-
func (d WebhookDefinition) ToMap() map[string]any {
44-
return d.m
111+
func NewWebhook() Webhook {
112+
return Webhook{}
45113
}

0 commit comments

Comments
 (0)