diff --git a/client_test.go b/client_test.go index 9d6ae63..3ae4500 100644 --- a/client_test.go +++ b/client_test.go @@ -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") @@ -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 { diff --git a/integration/model_webhook_integration.go b/integration/model_webhook_integration.go index 562a363..adc5031 100644 --- a/integration/model_webhook_integration.go +++ b/integration/model_webhook_integration.go @@ -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"` } diff --git a/testdata/fixtures/integration/create_webhook_success.json b/testdata/fixtures/integration/create_webhook_success.json index c5c95ac..ae19cd7 100644 --- a/testdata/fixtures/integration/create_webhook_success.json +++ b/testdata/fixtures/integration/create_webhook_success.json @@ -9,5 +9,7 @@ "headers": {}, "sharedSecret": "pa$$word", "type": "Webhook", - "url": "https://webhook.site/" + "url": "https://webhook.site/", + "method": "POST", + "payloadTemplate": "{\"incidentId\": \"{{{incidentId}}}\"}" } diff --git a/webhook_integration_test.go b/webhook_integration_test.go index 43fbc79..90cc0b5 100644 --- a/webhook_integration_test.go +++ b/webhook_integration_test.go @@ -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/", + "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/", + Method: "POST", + PayloadTemplate: "{\"incidentId\": \"{{{incidentId}}}\"}", }) assert.NoError(t, err, "Unexpected error creating integration") assert.Equal(t, "webhoooooook", result.Name, "Name does not match") @@ -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/", + "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/", + Method: "POST", + PayloadTemplate: "{\"incidentId\": \"{{{incidentId}}}\"}", }) assert.NoError(t, err, "Unexpected error creating integration") assert.Equal(t, "webhoooooook", result.Name, "Name does not match")