From c86492dbfda8a8185ae859b5c86e50d1ed95d96d Mon Sep 17 00:00:00 2001 From: TwiN Date: Sun, 9 Oct 2022 22:58:18 -0400 Subject: [PATCH] fix(alerting): Encode ntfy request body using json.Marshal Relevant: #336 --- alerting/provider/ntfy/ntfy.go | 26 ++++++++++++++++++-------- alerting/provider/ntfy/ntfy_test.go | 8 ++++---- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/alerting/provider/ntfy/ntfy.go b/alerting/provider/ntfy/ntfy.go index 1dd30bd9..bef21802 100644 --- a/alerting/provider/ntfy/ntfy.go +++ b/alerting/provider/ntfy/ntfy.go @@ -2,6 +2,7 @@ package ntfy import ( "bytes" + "encoding/json" "fmt" "io" "net/http" @@ -56,8 +57,16 @@ func (provider *AlertProvider) Send(endpoint *core.Endpoint, alert *alert.Alert, return err } +type Body struct { + Topic string `json:"topic"` + Title string `json:"title"` + Message string `json:"message"` + Tags []string `json:"tags"` + Priority int `json:"priority"` +} + // buildRequestBody builds the request body for the provider -func (provider *AlertProvider) buildRequestBody(endpoint *core.Endpoint, alert *alert.Alert, result *core.Result, resolved bool) string { +func (provider *AlertProvider) buildRequestBody(endpoint *core.Endpoint, alert *alert.Alert, result *core.Result, resolved bool) []byte { var message, tag string if len(alert.GetDescription()) > 0 { message = endpoint.DisplayName() + " - " + alert.GetDescription() @@ -69,13 +78,14 @@ func (provider *AlertProvider) buildRequestBody(endpoint *core.Endpoint, alert * } else { tag = "x" } - return fmt.Sprintf(`{ - "topic": "%s", - "title": "Gatus", - "message": "%s", - "tags": ["%s"], - "priority": %d -}`, provider.Topic, message, tag, provider.Priority) + body, _ := json.Marshal(Body{ + Topic: provider.Topic, + Title: "Gatus", + Message: message, + Tags: []string{tag}, + Priority: provider.Priority, + }) + return body } // GetDefaultAlert returns the provider's default alert configuration diff --git a/alerting/provider/ntfy/ntfy_test.go b/alerting/provider/ntfy/ntfy_test.go index fa6a6ac1..c806dee6 100644 --- a/alerting/provider/ntfy/ntfy_test.go +++ b/alerting/provider/ntfy/ntfy_test.go @@ -69,14 +69,14 @@ func TestAlertProvider_buildRequestBody(t *testing.T) { Provider: AlertProvider{URL: "https://ntfy.sh", Topic: "example", Priority: 1}, Alert: alert.Alert{Description: &firstDescription, SuccessThreshold: 5, FailureThreshold: 3}, Resolved: false, - ExpectedBody: "{\n \"topic\": \"example\",\n \"title\": \"Gatus\",\n \"message\": \"endpoint-name - description-1\",\n \"tags\": [\"x\"],\n \"priority\": 1\n}", + ExpectedBody: "{\"topic\":\"example\",\"title\":\"Gatus\",\"message\":\"endpoint-name - description-1\",\"tags\":[\"x\"],\"priority\":1}", }, { Name: "resolved", Provider: AlertProvider{URL: "https://ntfy.sh", Topic: "example", Priority: 2}, Alert: alert.Alert{Description: &secondDescription, SuccessThreshold: 5, FailureThreshold: 3}, Resolved: true, - ExpectedBody: "{\n \"topic\": \"example\",\n \"title\": \"Gatus\",\n \"message\": \"endpoint-name - description-2\",\n \"tags\": [\"white_check_mark\"],\n \"priority\": 2\n}", + ExpectedBody: "{\"topic\":\"example\",\"title\":\"Gatus\",\"message\":\"endpoint-name - description-2\",\"tags\":[\"white_check_mark\"],\"priority\":2}", }, } for _, scenario := range scenarios { @@ -92,11 +92,11 @@ func TestAlertProvider_buildRequestBody(t *testing.T) { }, scenario.Resolved, ) - if body != scenario.ExpectedBody { + if string(body) != scenario.ExpectedBody { t.Errorf("expected %s, got %s", scenario.ExpectedBody, body) } out := make(map[string]interface{}) - if err := json.Unmarshal([]byte(body), &out); err != nil { + if err := json.Unmarshal(body, &out); err != nil { t.Error("expected body to be valid JSON, got error:", err.Error()) } })