|
1 | 1 | (ns compojure.api.middleware-test |
2 | 2 | (:require [compojure.api.middleware :refer :all] |
| 3 | + [compojure.api.exception :as ex] |
3 | 4 | [midje.sweet :refer :all] |
4 | 5 | [ring.util.http-response :refer [ok]] |
5 | 6 | [ring.util.http-status :as status] |
|
39 | 40 |
|
40 | 41 | (ring.util.test/string-input-stream "foobar") false false)) |
41 | 42 |
|
| 43 | +(def default-options (:exceptions api-middleware-defaults)) |
| 44 | + |
42 | 45 | (facts "wrap-exceptions" |
43 | 46 | (with-out-str |
44 | | - (without-err |
45 | | - (let [exception (RuntimeException. "kosh") |
46 | | - exception-class (.getName (.getClass exception)) |
47 | | - handler (-> (fn [_] (throw exception)) |
48 | | - (wrap-exceptions {}))] |
| 47 | + (without-err |
| 48 | + (let [exception (RuntimeException. "kosh") |
| 49 | + exception-class (.getName (.getClass exception)) |
| 50 | + handler (-> (fn [_] (throw exception)) |
| 51 | + (wrap-exceptions default-options))] |
49 | 52 |
|
50 | | - (fact "converts exceptions into safe internal server errors" |
51 | | - (handler {}) => (contains {:status status/internal-server-error |
52 | | - :body (contains {:class exception-class |
53 | | - :type "unknown-exception"})}))))) |
| 53 | + (fact "converts exceptions into safe internal server errors" |
| 54 | + (handler {}) => (contains {:status status/internal-server-error |
| 55 | + :body (contains {:class exception-class |
| 56 | + :type "unknown-exception"})}))))) |
54 | 57 |
|
55 | 58 | (with-out-str |
56 | 59 | (without-err |
57 | 60 | (fact "Slingshot exception map type can be matched" |
58 | 61 | (let [handler (-> (fn [_] (throw+ {:type ::test} (RuntimeException. "kosh"))) |
59 | | - (wrap-exceptions {:handlers {::test (fn [ex _ _] {:status 500 :body "hello"})}}))] |
| 62 | + (wrap-exceptions (assoc-in default-options [:handlers ::test] (fn [ex _ _] {:status 500 :body "hello"}))))] |
60 | 63 | (handler {}) => (contains {:status status/internal-server-error |
61 | | - :body "hello"})))))) |
| 64 | + :body "hello"}))))) |
| 65 | + |
| 66 | + (without-err |
| 67 | + (fact "Default handler logs exceptions to console" |
| 68 | + (let [handler (-> (fn [_] (throw (RuntimeException. "kosh"))) |
| 69 | + (wrap-exceptions default-options))] |
| 70 | + (with-out-str (handler {})) => "ERROR kosh\n"))) |
| 71 | + |
| 72 | + (without-err |
| 73 | + (fact "Default request-parsing handler does not log messages" |
| 74 | + (let [handler (-> (fn [_] (throw (ex-info "Error parsing request" {:type ::ex/request-parsing} (RuntimeException. "Kosh")))) |
| 75 | + (wrap-exceptions default-options))] |
| 76 | + (with-out-str (handler {})) => ""))) |
| 77 | + |
| 78 | + (without-err |
| 79 | + (fact "Logging can be added to a exception handler" |
| 80 | + (let [handler (-> (fn [_] (throw (ex-info "Error parsing request" {:type ::ex/request-parsing} (RuntimeException. "Kosh")))) |
| 81 | + (wrap-exceptions (assoc-in default-options [:handlers ::ex/request-parsing] (ex/with-logging ex/request-parsing-handler))))] |
| 82 | + (with-out-str (handler {})) => "ERROR Error parsing request\n")))) |
0 commit comments