-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
142 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,113 @@ | ||
package wiremock | ||
|
||
type WebhookDefinition struct { | ||
m map[string]any | ||
import ( | ||
"encoding/json" | ||
"time" | ||
) | ||
|
||
type WebhookInterface interface { | ||
json.Marshaler | ||
WithName(name string) WebhookInterface | ||
ParseWebhook() map[string]interface{} | ||
} | ||
|
||
type Webhook struct { | ||
name string | ||
parameters webhookParameters | ||
} | ||
|
||
type webhookParameters struct { | ||
method string | ||
url string | ||
body string | ||
headers map[string]string | ||
delay DelayInterface | ||
} | ||
|
||
func (w webhookParameters) MarshalJSON() ([]byte, error) { | ||
jsonMap := map[string]interface{}{ | ||
"method": w.method, | ||
"url": w.url, | ||
"body": w.body, | ||
"headers": w.headers, | ||
"delay": w.delay.ParseDelay(), | ||
} | ||
|
||
return json.Marshal(jsonMap) | ||
} | ||
|
||
// WithName sets the name of the webhook and returns the webhook. | ||
func (w Webhook) WithName(name string) WebhookInterface { | ||
w.name = name | ||
return w | ||
} | ||
|
||
func Webhook() WebhookDefinition { | ||
return WebhookDefinition{ | ||
m: make(map[string]any), | ||
// ParseWebhook returns a map representation of the webhook. | ||
func (w Webhook) ParseWebhook() map[string]interface{} { | ||
return map[string]interface{}{ | ||
"name": w.name, | ||
"parameters": w.parameters, | ||
} | ||
} | ||
|
||
func (d WebhookDefinition) WithMethod(method string) WebhookDefinition { | ||
d.m["method"] = method | ||
return d | ||
// MarshalJSON implements the json.Marshaler interface. | ||
func (w Webhook) MarshalJSON() ([]byte, error) { | ||
return json.Marshal(w.ParseWebhook()) | ||
} | ||
|
||
func (d WebhookDefinition) WithURL(url string) WebhookDefinition { | ||
d.m["url"] = url | ||
return d | ||
// WithMethod sets the HTTP method of the webhook. | ||
func (w Webhook) WithMethod(method string) Webhook { | ||
w.parameters.method = method | ||
return w | ||
} | ||
|
||
func (d WebhookDefinition) WithHeader(key string, value string) WebhookDefinition { | ||
var headers map[string]string | ||
// WithURL sets the URL of the webhook. | ||
func (w Webhook) WithURL(url string) Webhook { | ||
w.parameters.url = url | ||
return w | ||
} | ||
|
||
if headersAny, ok := d.m["headers"]; ok { | ||
headers = headersAny.(map[string]string) | ||
} else { | ||
headers = make(map[string]string) | ||
d.m["headers"] = headers | ||
// WithHeader sets a header of the webhook. | ||
func (w Webhook) WithHeader(key string, value string) Webhook { | ||
if w.parameters.headers == nil { | ||
w.parameters.headers = make(map[string]string) | ||
} | ||
|
||
headers[key] = value | ||
w.parameters.headers[key] = value | ||
|
||
return w | ||
} | ||
|
||
// WithBody sets the body of the webhook. | ||
func (w Webhook) WithBody(body string) Webhook { | ||
w.parameters.body = body | ||
return w | ||
} | ||
|
||
// WithDelay sets the delay of the webhook. | ||
func (w Webhook) WithDelay(delay DelayInterface) Webhook { | ||
w.parameters.delay = delay | ||
return w | ||
} | ||
|
||
// WithFixedDelay sets the fixed delay of the webhook. | ||
func (w Webhook) WithFixedDelay(delay time.Duration) Webhook { | ||
w.parameters.delay = NewFixedDelay(delay) | ||
return w | ||
} | ||
|
||
return d | ||
// WithLogNormalRandomDelay sets the log normal delay of the webhook. | ||
func (w Webhook) WithLogNormalRandomDelay(median time.Duration, sigma float64) Webhook { | ||
w.parameters.delay = NewLogNormalRandomDelay(median, sigma) | ||
return w | ||
} | ||
|
||
func (d WebhookDefinition) WithBody(body string) WebhookDefinition { | ||
d.m["body"] = body | ||
return d | ||
// WithUniformRandomDelay sets the uniform random delay of the webhook. | ||
func (w Webhook) WithUniformRandomDelay(lower, upper time.Duration) Webhook { | ||
w.parameters.delay = NewUniformRandomDelay(lower, upper) | ||
return w | ||
} | ||
|
||
func (d WebhookDefinition) ToMap() map[string]any { | ||
return d.m | ||
func NewWebhook() Webhook { | ||
return Webhook{} | ||
} |