diff options
| author | eltrac <hi@eltr.ac> | 2026-10-10 16:11:48 +0800 |
|---|---|---|
| committer | eltrac <hi@eltr.ac> | 2026-10-10 16:11:48 +0800 |
| commit | e980e902b8dce11bde7a808978e95be1c755532d (patch) | |
| tree | 093e60dc30aa96a7f72a99739b6514c6aa3ebd2b | |
| parent | 15bb3e8bf2339980c6c621de9a9ddbdd24e9444b (diff) | |
test: cover most forms of retriever; set shorter http timeout in verifier test
| -rw-r--r-- | test/weepinbell/retriever_test.clj | 61 | ||||
| -rw-r--r-- | test/weepinbell/verifier_test.clj | 148 |
2 files changed, 136 insertions, 73 deletions
diff --git a/test/weepinbell/retriever_test.clj b/test/weepinbell/retriever_test.clj new file mode 100644 index 0000000..7b5614d --- /dev/null +++ b/test/weepinbell/retriever_test.clj @@ -0,0 +1,61 @@ +(ns weepinbell.retriever-test
+ (:require [clojure.test :refer [deftest testing is]]
+ [weepinbell.webserver.retriever :as ret]))
+
+(def page-size 1)
+(deftest retrieve-by-domain-test
+ (testing "by-domain list paging"
+ (with-redefs [weepinbell.webserver.retriever/default-page-size page-size]
+ (let [mock-data [{:webmention {:source "1" :target "1"}}
+ {:webmention {:source "2" :target "2"}}]
+ mock-store {:get-by-domain (fn [& _] mock-data)}
+ resp0 (ret/by-domain "" 0 mock-store {})
+ resp1 (ret/by-domain "" 1 mock-store {})]
+ (is (= page-size (count (:body resp0))))
+ (is (= page-size (count (:body resp1)))))))
+ (testing "by-domain handle invalid page number"
+ (with-redefs [weepinbell.webserver.retriever/default-page-size page-size]
+ (let [mock-data [{:webmention {:source "1" :target "1"}}
+ {:webmention {:source "2" :target "2"}}]
+ mock-store {:get-by-domain (fn [& _] mock-data)}
+ resp (ret/by-domain "" true mock-store {})]
+ (is (= 400 (:status resp))))))
+ (testing "by-domain return 404 when list is empty"
+ (let [mock-store {:get-by-domain (fn [& _] [])}
+ resp (ret/by-domain "" nil mock-store {})]
+ (is (= 404 (:status resp)))
+ (is (= "The requested webmention list does not exist."
+ (get-in resp [:body :message]))))))
+
+(deftest retrieve-by-url-test
+ (testing "by-url list paging"
+ (with-redefs [weepinbell.webserver.retriever/default-page-size page-size]
+ (let [mock-data [{:webmention {:source "1" :target "2"}}
+ {:webmention {:source "1" :target "2"}}]
+ mock-store {:get-by-domain (fn [& _] mock-data)}
+ resp0 (ret/by-url "2" 0 mock-store {})
+ resp1 (ret/by-url "2" 1 mock-store {})]
+ (is (= page-size (count (:body resp0))))
+ (is (= page-size (count (:body resp1)))))))
+ (testing "by-url with no url provieded"
+ (with-redefs [weepinbell.webserver.retriever/default-page-size page-size]
+ (let [mock-store {:get-by-domain (fn [& _])}
+ resp (ret/by-url nil 0 mock-store {})]
+ (is (= 400 (:status resp)))
+ (is (= "Missing URL. Specify target URL whose webmentions you are querying."
+ (get-in resp [:body :message]))))))
+ (testing "by-url return 404 when list is empty"
+ (with-redefs [weepinbell.webserver.retriever/default-page-size page-size]
+ (let [mock-store {:get-by-domain (fn [& _])}
+ resp (ret/by-url "" 0 mock-store {})]
+ (is (= 404 (:status resp)))
+ (is (= "The requested webmention list does not exist."
+ (get-in resp [:body :message])))))))
+
+(deftest retrieve-by-id-test
+ (testing "by-id return 404 when no webmention is matched"
+ (let [mock-store {:get (fn [& _] [])}
+ resp (ret/by-id "" mock-store {})]
+ (is (= 404 (:status resp)))
+ (is (= "The requested webmention does not exist."
+ (get-in resp [:body :message]))))))
diff --git a/test/weepinbell/verifier_test.clj b/test/weepinbell/verifier_test.clj index 313a4e9..d41516c 100644 --- a/test/weepinbell/verifier_test.clj +++ b/test/weepinbell/verifier_test.clj @@ -1,6 +1,7 @@ (ns weepinbell.verifier-test (:require [clojure.test :refer [deftest testing is]] [weepinbell.processor.verifier :as v] + [weepinbell.http.client] [weepinbell.test-utility :refer [with-webmention]])) (defn- test-verify [source target] @@ -34,77 +35,78 @@ "http://localhost:38888/target")))))) (deftest verification-exceptional-cases-test - (testing "source is not found" - (with-webmention :new ["/source" {:status 404 - :headers {"Content-Type" "text/plain"} - :body "Not Found."} - "/target" {:status 200 - :headers {"Content-Type" "text/plain"}}] - (is (not (test-verify "http://localhost:38888/source" - "http://localhost:38888/target"))))) - (testing "source returns error status 502" - (with-webmention :new ["/source" {:status 502 - :headers {"Content-Type" "text/plain"} - :body "Bad Gateway"} - "/target" {:status 200 - :headers {"Content-Type" "text/plain"}}] - (is (not (test-verify "http://localhost:38888/source" - "http://localhost:38888/target"))))) - (testing "source body does not contain any URL" - (with-webmention :new ["/source" {:status 200 - :headers {"Content-Type" "text/plain"} - :body "I didn't mention anyone"} - "/target" {:status 200 - :headers {"Content-Type" "text/plain"}}] - (is (not (test-verify "http://localhost:38888/source" - "http://localhost:38888/target"))))) - (testing "source body does not contain target URL" - (with-webmention :new ["/source" {:status 200 - :headers {"Content-Type" "text/plain"} - :body (str "I mentioned christ but not alice\n" - "https://chris.blog/archive/xxx")} - "/target" {:status 200 - :headers {"Content-Type" "text/plain"}}] - (is (not (test-verify "http://localhost:38888/source" - "http://localhost:38888/target"))))) - (testing "source response has no Content-Type header" - (with-webmention :new ["/source" {:status 200 - :body (str "I mentioned alice's other post\n" - "https://alice.blog/article/efg")} - "/target" {:status 200 - :headers {"Content-Type" "text/plain"}}] - (is (not (test-verify "http://localhost:38888/source" - "http://localhost:38888/target"))))) - (testing "target response has no Content-Type header" - (with-webmention :new ["/source" {:status 200 - :headers {"Content-Type" "text/plain"} - :body (str "I mentioned alice's other post\n" - "https://alice.blog/article/efg")} - "/target" {:status 200}] - (is (not (test-verify "http://localhost:38888/source" - "http://localhost:38888/target"))))) - (testing "unsupported content type" - (with-webmention :new ["/source" {:status 200 - :headers {"Content-Type" "application/json"} - :body "{\"message\": \"I mentioned alice's other post\n https://alice.blog/article/efg\"}"} - "/target" {:status 200 - :headers {"Content-Type" "text/plain"}}] - (is (not (test-verify "http://localhost:38888/source" - "http://localhost:38888/target"))))) - (testing "responses have no status" - (with-webmention :new ["/source" {:status nil - :headers {"Content-Type" "text/plain"} - :body (str "I mentioned alice's other post\n" - "https://alice.blog/article/efg")} - "/target" {:headers {"Content-Type" "text/plain"}}] - (is (not (test-verify "http://localhost:38888/source" - "http://localhost:38888/target"))))) - (testing "HTTP not complete, 100 status" - (with-webmention :new ["/source" {:status 100 - :headers {"Content-Type" "text/plain"} - :body "oops"} - "/target" {:status 200 - :headers {"Content-Type" "text/plain"}}] - (is (not (test-verify "http://localhost:38888/source" - "http://localhost:38888/target")))))) + (with-redefs [weepinbell.http.client/default-options {:timeout 100}] + (testing "source is not found" + (with-webmention :new ["/source" {:status 404 + :headers {"Content-Type" "text/plain"} + :body "Not Found."} + "/target" {:status 200 + :headers {"Content-Type" "text/plain"}}] + (is (not (test-verify "http://localhost:38888/source" + "http://localhost:38888/target"))))) + (testing "source returns error status 502" + (with-webmention :new ["/source" {:status 502 + :headers {"Content-Type" "text/plain"} + :body "Bad Gateway"} + "/target" {:status 200 + :headers {"Content-Type" "text/plain"}}] + (is (not (test-verify "http://localhost:38888/source" + "http://localhost:38888/target"))))) + (testing "source body does not contain any URL" + (with-webmention :new ["/source" {:status 200 + :headers {"Content-Type" "text/plain"} + :body "I didn't mention anyone"} + "/target" {:status 200 + :headers {"Content-Type" "text/plain"}}] + (is (not (test-verify "http://localhost:38888/source" + "http://localhost:38888/target"))))) + (testing "source body does not contain target URL" + (with-webmention :new ["/source" {:status 200 + :headers {"Content-Type" "text/plain"} + :body (str "I mentioned christ but not alice\n" + "https://chris.blog/archive/xxx")} + "/target" {:status 200 + :headers {"Content-Type" "text/plain"}}] + (is (not (test-verify "http://localhost:38888/source" + "http://localhost:38888/target"))))) + (testing "source response has no Content-Type header" + (with-webmention :new ["/source" {:status 200 + :body (str "I mentioned alice's other post\n" + "https://alice.blog/article/efg")} + "/target" {:status 200 + :headers {"Content-Type" "text/plain"}}] + (is (not (test-verify "http://localhost:38888/source" + "http://localhost:38888/target"))))) + (testing "target response has no Content-Type header" + (with-webmention :new ["/source" {:status 200 + :headers {"Content-Type" "text/plain"} + :body (str "I mentioned alice's other post\n" + "https://alice.blog/article/efg")} + "/target" {:status 200}] + (is (not (test-verify "http://localhost:38888/source" + "http://localhost:38888/target"))))) + (testing "unsupported content type" + (with-webmention :new ["/source" {:status 200 + :headers {"Content-Type" "application/json"} + :body "{\"message\": \"I mentioned alice's other post\n https://alice.blog/article/efg\"}"} + "/target" {:status 200 + :headers {"Content-Type" "text/plain"}}] + (is (not (test-verify "http://localhost:38888/source" + "http://localhost:38888/target"))))) + (testing "responses have no status" + (with-webmention :new ["/source" {:status nil + :headers {"Content-Type" "text/plain"} + :body (str "I mentioned alice's other post\n" + "https://alice.blog/article/efg")} + "/target" {:headers {"Content-Type" "text/plain"}}] + (is (not (test-verify "http://localhost:38888/source" + "http://localhost:38888/target"))))) + (testing "HTTP not complete, 100 status" + (with-webmention :new ["/source" {:status 100 + :headers {"Content-Type" "text/plain"} + :body "oops"} + "/target" {:status 200 + :headers {"Content-Type" "text/plain"}}] + (is (not (test-verify "http://localhost:38888/source" + "http://localhost:38888/target"))))))) |
