Skip to content

Commit

Permalink
Extend Webhook integration with fields: method, payloadTemplate (#212)
Browse files Browse the repository at this point in the history
  • Loading branch information
tdudzik-splunk authored May 16, 2024
1 parent acc6e03 commit 6a0456b
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 8 deletions.
20 changes: 20 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ func verifyRequest(t *testing.T, method string, expectToken bool, status int, pa
})
}

func verifyRequestWithJsonBody(t *testing.T, method string, expectToken bool, status int, params url.Values, jsonBody string, resultPath string) func(w http.ResponseWriter, r *http.Request) {
return createResponse(t, status, resultPath, func(t *testing.T, r *http.Request) {
verifyHeaders(t, r, expectToken)
verifyParams(t, r, params)
verifyJsonBody(t, r, jsonBody)

assert.Equal(t, method, r.Method, "Incorrect HTTP method")
})
}

func verifyHeaders(t *testing.T, r *http.Request, expectToken bool) {
if val, ok := r.Header[AuthHeaderKey]; ok {
assert.Equal(t, []string{TestToken}, val, "Incorrect auth token in headers")
Expand Down Expand Up @@ -79,6 +89,16 @@ func verifyParams(t *testing.T, r *http.Request, params url.Values) {
}
}

func verifyJsonBody(t *testing.T, r *http.Request, jsonBody string) {
actualBody, err := ioutil.ReadAll(r.Body)
if err != nil {
assert.Fail(t, "Error reading request body: %v", err)
}

actualJsonBody := string(actualBody)
assert.JSONEq(t, jsonBody, actualJsonBody, "Expected body: %s, got: %s", jsonBody, actualJsonBody)
}

func createResponse(t *testing.T, status int, resultPath string, requestValidator func(t *testing.T, r *http.Request)) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
if requestValidator != nil {
Expand Down
8 changes: 5 additions & 3 deletions integration/model_webhook_integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ type WebhookIntegration struct {
Name string `json:"name,omitempty"`
Type Type `json:"type"`
// Webhook URL
Url string `json:"url,omitempty"`
SharedSecret string `json:"sharedSecret,omitempty"`
Headers map[string]interface{} `json:"headers,omitempty"`
Url string `json:"url,omitempty"`
SharedSecret string `json:"sharedSecret,omitempty"`
Headers map[string]interface{} `json:"headers,omitempty"`
Method string `json:"method,omitempty"`
PayloadTemplate string `json:"payloadTemplate,omitempty"`
}
4 changes: 3 additions & 1 deletion testdata/fixtures/integration/create_webhook_success.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@
"headers": {},
"sharedSecret": "pa$$word",
"type": "Webhook",
"url": "https://webhook.site/<key>"
"url": "https://webhook.site/<key>",
"method": "POST",
"payloadTemplate": "{\"incidentId\": \"{{{incidentId}}}\"}"
}
34 changes: 30 additions & 4 deletions webhook_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,23 @@ func TestCreateWebhookIntegration(t *testing.T) {
teardown := setup()
defer teardown()

mux.HandleFunc("/v2/integration", verifyRequest(t, "POST", true, http.StatusOK, nil, "integration/create_webhook_success.json"))
jsonBody := `{
"type": "Webhook",
"name": "webhoooooook",
"enabled": true,
"url": "https://webhook.site/<key>",
"method": "POST",
"payloadTemplate": "{\"incidentId\": \"{{{incidentId}}}\"}"
}`
mux.HandleFunc("/v2/integration", verifyRequestWithJsonBody(t, "POST", true, http.StatusOK, nil, jsonBody, "integration/create_webhook_success.json"))

result, err := client.CreateWebhookIntegration(context.Background(), &integration.WebhookIntegration{
Type: "Webhook",
Type: "Webhook",
Name: "webhoooooook",
Enabled: true,
Url: "https://webhook.site/<key>",
Method: "POST",
PayloadTemplate: "{\"incidentId\": \"{{{incidentId}}}\"}",
})
assert.NoError(t, err, "Unexpected error creating integration")
assert.Equal(t, "webhoooooook", result.Name, "Name does not match")
Expand All @@ -37,10 +50,23 @@ func TestUpdateWebhookIntegration(t *testing.T) {
teardown := setup()
defer teardown()

mux.HandleFunc("/v2/integration/id", verifyRequest(t, "PUT", true, http.StatusOK, nil, "integration/create_webhook_success.json"))
jsonBody := `{
"type": "Webhook",
"name": "webhoooooook",
"enabled": true,
"url": "https://webhook.site/<key>",
"method": "POST",
"payloadTemplate": "{\"incidentId\": \"{{{incidentId}}}\"}"
}`
mux.HandleFunc("/v2/integration/id", verifyRequestWithJsonBody(t, "PUT", true, http.StatusOK, nil, jsonBody, "integration/create_webhook_success.json"))

result, err := client.UpdateWebhookIntegration(context.Background(), "id", &integration.WebhookIntegration{
Type: "Webhook",
Type: "Webhook",
Name: "webhoooooook",
Enabled: true,
Url: "https://webhook.site/<key>",
Method: "POST",
PayloadTemplate: "{\"incidentId\": \"{{{incidentId}}}\"}",
})
assert.NoError(t, err, "Unexpected error creating integration")
assert.Equal(t, "webhoooooook", result.Name, "Name does not match")
Expand Down

0 comments on commit 6a0456b

Please sign in to comment.