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 := regexp.MustCompile(`[^a-zA-Z0-9]`).ReplaceAllString(p, `_`)
|
||||||
cacheP = path.Join(WebhookOnRecursiveMissCacheD, cacheP)
|
cacheP = path.Join(WebhookOnRecursiveMissCacheD, cacheP)
|
||||||
if _, err := os.Stat(cacheP); err != nil {
|
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)
|
u, err := url.Parse(WebhookOnRecursiveMiss)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("WebhookOnRecursiveMiss (%s) invalid: %w", WebhookOnRecursiveMiss, err)
|
return fmt.Errorf("WebhookOnRecursiveMiss (%s) invalid: %w", WebhookOnRecursiveMiss, err)
|
||||||
}
|
}
|
||||||
q := u.Query()
|
user := u.User
|
||||||
q.Set("p", p)
|
u.User = nil
|
||||||
u.RawQuery = q.Encode()
|
if username := user.Username(); username != "" {
|
||||||
resp, err := http.Post(u.String(), "text/plain", strings.NewReader(p))
|
password, _ := user.Password()
|
||||||
|
req.SetBasicAuth(username, password)
|
||||||
|
}
|
||||||
|
req.URL = u
|
||||||
|
|
||||||
|
resp, err := http.DefaultClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to call %s for missing %s: %w", WebhookOnRecursiveMiss, p, err)
|
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"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"slices"
|
"slices"
|
||||||
|
|
@ -153,13 +154,17 @@ func TestRunWith(t *testing.T) {
|
||||||
func TestRecursive(t *testing.T) {
|
func TestRecursive(t *testing.T) {
|
||||||
webhooks := []string{}
|
webhooks := []string{}
|
||||||
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.URL.Query().Get("p") == "" {
|
if r.Method != http.MethodPut {
|
||||||
t.Errorf("webhook wasn't called with ?p: %s", r.URL.String())
|
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)
|
b, _ := ioutil.ReadAll(r.Body)
|
||||||
if string(b) != r.URL.Query().Get("p") {
|
t.Logf("%s { %s }", r.URL.String(), b)
|
||||||
t.Errorf("webhook wasn't called with ?p == {body}: %q vs %q", r.URL.Query().Get("p"), b)
|
|
||||||
}
|
|
||||||
webhooks = append(webhooks, string(b))
|
webhooks = append(webhooks, string(b))
|
||||||
}))
|
}))
|
||||||
t.Cleanup(s.Close)
|
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()
|
main.WebhookOnRecursiveMissCacheD = t.TempDir()
|
||||||
t.Cleanup(func() {
|
t.Cleanup(func() {
|
||||||
main.WebhookOnRecursiveMiss = ""
|
main.WebhookOnRecursiveMiss = ""
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue