Phase 3 — Alerts (P3-05/06/07) #7
@@ -6,6 +6,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -49,13 +50,15 @@ func (c *NtfyChannel) Kind() string { return "ntfy" }
|
|||||||
// with ntfy headers. Returns (statusCode, latency, err). 4xx/5xx
|
// with ntfy headers. Returns (statusCode, latency, err). 4xx/5xx
|
||||||
// responses are returned as errors with the status code set.
|
// responses are returned as errors with the status code set.
|
||||||
func (c *NtfyChannel) Send(ctx context.Context, p Payload) (int, time.Duration, error) {
|
func (c *NtfyChannel) Send(ctx context.Context, p Payload) (int, time.Duration, error) {
|
||||||
url := c.cfg.ServerURL + "/" + c.cfg.Topic
|
server := strings.TrimRight(c.cfg.ServerURL, "/")
|
||||||
|
url := server + "/" + c.cfg.Topic
|
||||||
|
|
||||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewBufferString(p.Message))
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewBufferString(p.Message))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, 0, fmt.Errorf("ntfy: build request: %w", err)
|
return 0, 0, fmt.Errorf("ntfy: build request: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
req.Header.Set("Content-Type", "text/plain")
|
||||||
req.Header.Set("Title", fmt.Sprintf("[%s] %s %s", p.Severity, p.HostName, p.Kind))
|
req.Header.Set("Title", fmt.Sprintf("[%s] %s %s", p.Severity, p.HostName, p.Kind))
|
||||||
req.Header.Set("Tags", p.Severity+","+p.Kind)
|
req.Header.Set("Tags", p.Severity+","+p.Kind)
|
||||||
req.Header.Set("Priority", priorityForSeverity(p.Severity, c.defaultPriority))
|
req.Header.Set("Priority", priorityForSeverity(p.Severity, c.defaultPriority))
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ func TestNtfySendsHeadersAndBody(t *testing.T) {
|
|||||||
gotTags string
|
gotTags string
|
||||||
gotClick string
|
gotClick string
|
||||||
gotAuth string
|
gotAuth string
|
||||||
|
gotContentType string
|
||||||
gotBody string
|
gotBody string
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -26,6 +27,7 @@ func TestNtfySendsHeadersAndBody(t *testing.T) {
|
|||||||
gotTags = r.Header.Get("Tags")
|
gotTags = r.Header.Get("Tags")
|
||||||
gotClick = r.Header.Get("Click")
|
gotClick = r.Header.Get("Click")
|
||||||
gotAuth = r.Header.Get("Authorization")
|
gotAuth = r.Header.Get("Authorization")
|
||||||
|
gotContentType = r.Header.Get("Content-Type")
|
||||||
b, _ := io.ReadAll(r.Body)
|
b, _ := io.ReadAll(r.Body)
|
||||||
gotBody = string(b)
|
gotBody = string(b)
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
@@ -73,6 +75,9 @@ func TestNtfySendsHeadersAndBody(t *testing.T) {
|
|||||||
if want := "Bearer tk1"; gotAuth != want {
|
if want := "Bearer tk1"; gotAuth != want {
|
||||||
t.Errorf("Authorization: got %q want %q", gotAuth, want)
|
t.Errorf("Authorization: got %q want %q", gotAuth, want)
|
||||||
}
|
}
|
||||||
|
if gotContentType != "text/plain" {
|
||||||
|
t.Errorf("Content-Type: got %q want %q", gotContentType, "text/plain")
|
||||||
|
}
|
||||||
if gotBody != "errors found" {
|
if gotBody != "errors found" {
|
||||||
t.Errorf("body: got %q want %q", gotBody, "errors found")
|
t.Errorf("body: got %q want %q", gotBody, "errors found")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user