summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authoreltrac <hi@eltr.ac>2026-09-16 17:03:51 +0800
committereltrac <hi@eltr.ac>2026-09-19 12:10:45 +0800
commitef9ca471427b636c1fa42244923a04c548a04d82 (patch)
treec152d5cc9e5269f5b0bc1eefdcce5869b7197919 /test
parent41ad0b78bcac340d3b8640be30b9e6f2dcf9bf2b (diff)
processor, webhook: send webhooks with customizable payload
- processor will call hook functions with webmention data - refactor webhook.clj massively - add dynamic parameter substitution for urls, e.g. {source} {target} {event}...... - add config option :webhook-request, a function with parameters [hook, data] that returns a map (resembling {:headers ... :body ...}). webhook.clj uses this function to generate payload. - allow map besids URL string in config option :webhook. if it's a URL, sends webhook request to it with payload generated by :webhook-request; if it's a map, it should be like {:url ... :request-fn ...}, which specifies a payload generator. - add request-fn preset :ntfy
Diffstat (limited to 'test')
-rw-r--r--test/weepinbell/webhook_test.clj50
1 files changed, 45 insertions, 5 deletions
diff --git a/test/weepinbell/webhook_test.clj b/test/weepinbell/webhook_test.clj
index c0fd984..ac2a0e6 100644
--- a/test/weepinbell/webhook_test.clj
+++ b/test/weepinbell/webhook_test.clj
@@ -3,15 +3,55 @@
[org.httpkit.server :refer [run-server]]
[weepinbell.webhook :as webhook]))
+(def test-wm-data {:webmention {:target "https://target.net/"
+ :source "https://source.net/"}
+ :extra {:proxied-photo "https://test.com/photo.png"}})
+
(deftest webhook-test
- (testing "Test webhooks triggers"
+ (testing "Test webhooks triggers with ntfy request"
(let [received (promise)
- handler (fn [_]
+ payload-ok (promise)
+ handler (fn [req]
(deliver received true)
+ (let [headers (:headers req)
+ title (get headers "title")
+ icon (get headers "icon")
+ click (get headers "click")]
+ (when (and (= title "source.net mentioned you!")
+ (= click "https://source.net/")
+ (= icon "https://test.com/photo.png"))
+ (deliver payload-ok true)))
{:status 200 :headers {} :body "ok"})
server (run-server handler {:port 18888 :join? false})]
(webhook/trigger :on-create
- {:on-create ["http://127.0.0.1:18888/webhook-test"]})
- (is (deref received 2000 nil)
- "Webhook request was received")
+ [{:url "http://127.0.0.1:18888/webhook-test"
+ :request-fn :ntfy}]
+ test-wm-data)
+ (is (deref received 2000 nil))
+ (is (deref payload-ok 2000 nil))
(server))))
+
+(def test-make (partial webhook/make-url :on-create test-wm-data))
+(deftest make-url-test
+ (testing "Test webhook replacing dynamic parameters in URL"
+ (is (= (test-make "https://webhook.test/{event}")
+ "https://webhook.test/on-create"))
+ (is (= (test-make "https://webhook.test/{source-domain}_mentioned_you")
+ "https://webhook.test/source.net_mentioned_you"))
+ (is (= (test-make "https://webhook.test/{source-encoded}/{target-encoded}")
+ "https://webhook.test/https%3A%2F%2Fsource.net%2F/https%3A%2F%2Ftarget.net%2F"))
+ (is (= (test-make "https://hook.{target-domain}/update")
+ "https://hook.target.net/update"))
+ (is (= (test-make "https://webhook.test?=source={source}&target={target}")
+ "https://webhook.test?=source=https://source.net/&target=https://target.net/"))))
+
+(deftest make-request-test
+ (testing "Test webhook constructing request options dynamically"
+ (let [test-data {:webmention {:target "https://target.net/"
+ :source "https://source.net/"}
+ :extra {:proxied-photo "https://test.com/photo.png"}}]
+ (is (= (webhook/make-request :on-create test-data :ntfy)
+ {:body "from https://source.net/\nto https://target.net/"
+ :headers {"Title" "source.net mentioned you!"
+ "Click" "https://source.net/"
+ "Icon" "https://test.com/photo.png"}})))))