diff options
| author | eltrac <hi@eltr.ac> | 2026-05-03 10:54:42 +0800 |
|---|---|---|
| committer | eltrac <hi@eltr.ac> | 2026-05-03 10:54:42 +0800 |
| commit | 578acae5192f5934f579ef98cfb5d9d8ce855bf3 (patch) | |
| tree | 1b87b3c5f7ff0caad3f083bb1bb33b90b4cdebe6 | |
| parent | c883f42fbbb630ae60b362cf4a8bc2c2ebf943f4 (diff) | |
test(receiver): unit tests for receiver error handling
| -rw-r--r-- | test/weepinbell/receiver_test.clj | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/test/weepinbell/receiver_test.clj b/test/weepinbell/receiver_test.clj new file mode 100644 index 0000000..c477585 --- /dev/null +++ b/test/weepinbell/receiver_test.clj @@ -0,0 +1,54 @@ +;; The receiver only verifies URLs, and does not make HTTP requests +;; Verifying Content-Type and if target url is present in source +;; is the processor's concern. Thus this test does not contain any +;; test cases that covers those checks. + +;; Receiver does this things: +;; 1. Receive a webmention request from handler +;; 2. Validate request +;; 2.1 Source and target should be valid URLs; +;; 2.2 Source and target should not be identical; +;; 2.3 Domain of target is in allow list. +;; 3. Put validated request into queue. + +(ns weepinbell.receiver-test + (:require [clojure.test :refer [deftest testing is]] + [clojure.core.async :refer [chan]] + [weepinbell.receiver :refer [receive]])) + +;; webmention queue for testing +(def test-wmc (chan)) + +(defn- wrap-400-msg [typ] + (let [msg (typ {:invalid-url "Invalid URLs. Source and target have to be valid http(s) urls." + :identical "Source and target are identical." + :domain-not-allowed "Domain of target is not allowed. This receiver does not receive webmention on its behalf."})] + {:status 400 + :headers {"Content-Type" "application/json; charset=utf-8"} + :body {:status 400 + :message msg}})) + +((deftest receive-test + (testing "Receiver error handling." + (->> [;; happy paths + ["https://bob.blog" "https://alice.blog" nil] + ["http://bob.blog" "http://alice.blog" nil] + ["https://bob.blog/post/123" "https://alice.blog/article/abc" nil] + ["http://bob.blog/post/123" "http://alice.blog/article/abc" nil] + ;; invalid URLs + ["htt://bob.blog" "https://alice.blog/article/abc" (wrap-400-msg :invalid-url)] + ["ftp://bob.blog/file/x" "https://alice.blog/article/abc" (wrap-400-msg :invalid-url)] + ["bob.blog/post/123" "https://alice.blog/article/abc" (wrap-400-msg :invalid-url)] + ["https://bob.blog/post/123" "alice.blog/article/abc" (wrap-400-msg :invalid-url)] + ["https://bob.blog/post/123" "ftp://alice.blog/file/x" (wrap-400-msg :invalid-url)] + ;; identical URLs + ["https://alice.blog/article/abc" "https://alice.blog/article/abc" (wrap-400-msg :identical)] + ;; domain not allowed + ["https://bob.blog/post/123" "https://chris.blog/archive/xxx" (wrap-400-msg :domain-not-allowed)]] + (map (fn [testcase] + (let [[s t expects] testcase] + (is (= expects + (receive {:s s :t t} + test-wmc + {:domains ["alice.blog"]}))))))) + ))) |
