From 5b9bead96ffacbfa17e63ebc9153ec924b6b2367 Mon Sep 17 00:00:00 2001 From: Bel LaPointe <153096461+breel-render@users.noreply.github.com> Date: Tue, 22 Apr 2025 20:40:19 -0600 Subject: [PATCH] ntfy webhook format --- main.go | 18 ++++++++++++++---- main_test.go | 19 +++++++++++++------ 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/main.go b/main.go index 37ef7da..9b5aee7 100644 --- a/main.go +++ b/main.go @@ -91,14 +91,24 @@ func Recursive(ctx context.Context) error { cacheP := regexp.MustCompile(`[^a-zA-Z0-9]`).ReplaceAllString(p, `_`) cacheP = path.Join(WebhookOnRecursiveMissCacheD, cacheP) if _, err := os.Stat(cacheP); err != nil { + req, err := http.NewRequest(http.MethodPut, WebhookOnRecursiveMiss, strings.NewReader(p)) + if err != nil { + panic(err) + } + u, err := url.Parse(WebhookOnRecursiveMiss) if err != nil { return fmt.Errorf("WebhookOnRecursiveMiss (%s) invalid: %w", WebhookOnRecursiveMiss, err) } - q := u.Query() - q.Set("p", p) - u.RawQuery = q.Encode() - resp, err := http.Post(u.String(), "text/plain", strings.NewReader(p)) + user := u.User + u.User = nil + if username := user.Username(); username != "" { + password, _ := user.Password() + req.SetBasicAuth(username, password) + } + req.URL = u + + resp, err := http.DefaultClient.Do(req) if err != nil { return fmt.Errorf("failed to call %s for missing %s: %w", WebhookOnRecursiveMiss, p, err) } diff --git a/main_test.go b/main_test.go index 5322abe..72bb979 100644 --- a/main_test.go +++ b/main_test.go @@ -5,6 +5,7 @@ import ( "io/ioutil" "net/http" "net/http/httptest" + "net/url" "os" "path" "slices" @@ -153,13 +154,17 @@ func TestRunWith(t *testing.T) { func TestRecursive(t *testing.T) { webhooks := []string{} s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if r.URL.Query().Get("p") == "" { - t.Errorf("webhook wasn't called with ?p: %s", r.URL.String()) + if r.Method != http.MethodPut { + t.Errorf("unexpected webhook method %s", r.Method) + } + if r.URL.User.String() != "" { + t.Errorf("unexpected auth on url %s", r.URL.String()) + } + if u, p, _ := r.BasicAuth(); u != "u" || p != "p" { + t.Errorf("webhook didnt translate u:p to basic auth") } b, _ := ioutil.ReadAll(r.Body) - if string(b) != r.URL.Query().Get("p") { - t.Errorf("webhook wasn't called with ?p == {body}: %q vs %q", r.URL.Query().Get("p"), b) - } + t.Logf("%s { %s }", r.URL.String(), b) webhooks = append(webhooks, string(b)) })) t.Cleanup(s.Close) @@ -176,7 +181,9 @@ func TestRecursive(t *testing.T) { } }) - main.WebhookOnRecursiveMiss = s.URL + u, _ := url.Parse(s.URL) + u.User = url.UserPassword("u", "p") + main.WebhookOnRecursiveMiss = u.String() main.WebhookOnRecursiveMissCacheD = t.TempDir() t.Cleanup(func() { main.WebhookOnRecursiveMiss = ""