ntfy webhook format
parent
54bbca8fea
commit
5b9bead96f
18
main.go
18
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)
|
||||
}
|
||||
|
|
|
|||
19
main_test.go
19
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 = ""
|
||||
|
|
|
|||
Loading…
Reference in New Issue