Browse Source

More refactoring and addition of test coverage for /ping

master
Stephen Starkey 2 years ago
parent
commit
9ccbb35106
  1. 28
      test/clj/email_counter/test/handler.clj

28
test/clj/email_counter/test/handler.clj

@ -1,12 +1,12 @@
(ns email-counter.test.handler
(:require
[clojure.string :as str]
[clojure.test :refer :all]
[ring.mock.request :refer :all]
[email-counter.middleware.exception :as exception]
[email-counter.handler :refer :all]
[muuntaja.core :as m]
[mount.core :as mount]
[clojure.string :as str]
[email-counter.middleware.exception :as exception]))
[ring.mock.request :refer :all]))
(use-fixtures
:once
@ -26,15 +26,19 @@
(pr-str response)))
(def ^:private email-route "/api/v1/emailcount")
(defn clean-response [response]
(-> response
(assoc :body (decode-response-body response))
(dissoc :headers)))
(defn post-body [url body]
(let [response ((app) (-> (request :post url)
(json-body body)))]
(-> response
(assoc :body (decode-response-body response))
(dissoc :headers))))
(let [response ((app) (-> (request :post url) (json-body body)))]
(clean-response response)))
(defn request-emails [emails]
(post-body "/api/v1/emailcount" {:emails emails}))
(post-body email-route {:emails emails}))
(defn print-exceptions [exceptions-encountered]
(doseq [e @exceptions-encountered]
@ -46,6 +50,10 @@
(with-redefs [exception/log-error
(fn [e]
(swap! exceptions-encountered conj e))]
(testing "ping"
(is (= {:status 200, :body {:message "pong"}}
(clean-response ((app) (request :get "/api/ping"))))))
(testing "emailcount"
(is (= {:status 200, :body {:total 2}}
(request-emails ["test.email@gmail.com"
@ -61,7 +69,7 @@
(testing "invalid request"
(is (= {:status 400,
:body "{:emails missing-required-key, :test disallowed-key}"}
(post-body "/api/v1/emailcount" {:test "stuff"}))))
(post-body email-route {:test "stuff"}))))
(testing "no exceptions were actually encountered"
(is (= 0 (count @exceptions-encountered))

Loading…
Cancel
Save