From a5f0d0dc9e73ba46a4ff974a256de770d70ac788 Mon Sep 17 00:00:00 2001 From: eltrac Date: Fri, 30 Oct 2026 11:46:40 +0800 Subject: test, webhook: tidy up webhook; add tests - (send-webhook) does not need to validate URLs; (trigger) already does that; replace validation logic with :pre conditional - (make-request) does not need to fallback to empty string when event-key is nil. it should never be nil. --- src/weepinbell/webhook.clj | 19 +++++++++---------- test/weepinbell/webhook_test.clj | 4 +++- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/weepinbell/webhook.clj b/src/weepinbell/webhook.clj index c93b74a..e777e92 100644 --- a/src/weepinbell/webhook.clj +++ b/src/weepinbell/webhook.clj @@ -13,15 +13,14 @@ ;; utilities (defn- send-webhook [url opts] - (if (valid-http-url? url) - (client/post url opts - (fn async-callback [{:keys [status _ _ error]}] - (cond - error (timbre/error "error ocurred sending webhook to" url ":" error) - (>= status 500) (timbre/error "something's wrong with webhook server of" url) - (>= status 400) (timbre/error "webhook url is invalid:" url) - :else (timbre/info "request sent to" url)))) - (timbre/warn "webhook url not valid, skipped" url))) + {:pre [(valid-http-url? url) (map? opts)]} + (client/post url opts + (fn async-callback [{:keys [status _ _ error]}] + (cond + error (timbre/error "error ocurred sending webhook to" url ":" error) + (>= status 500) (timbre/error "something's wrong with webhook server of" url) + (>= status 400) (timbre/error "webhook url is invalid:" url) + :else (timbre/info "request sent to" url))))) (defn make-request [event-key data request-fn] (cond @@ -41,7 +40,7 @@ source (:source webmention) target (:target webmention)] (-> url - (string/replace "{event}" (or (name event-key) "")) + (string/replace "{event}" (name event-key)) (string/replace "{target}" (or target "")) (string/replace "{source}" (or source "")) (string/replace "{target-encoded}" (if target (url-encode target) "")) diff --git a/test/weepinbell/webhook_test.clj b/test/weepinbell/webhook_test.clj index 593e709..f7530cc 100644 --- a/test/weepinbell/webhook_test.clj +++ b/test/weepinbell/webhook_test.clj @@ -89,7 +89,9 @@ (testing "Webhook constructing request options with custom functions" (let [custom-map {:body "customized!"}] (is (= (webhook/make-request :on-receive test-wm-data (fn [& _] custom-map)) - custom-map))))) + custom-map)))) + (testing "Unknown request-fn type" + (is (nil? (webhook/make-request :on-delete test-wm-data "I am a function"))))) (deftest make-actions-test (testing "Constructing action map" -- cgit v1.2.3