|
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] |
6 | | - ring.util.test) |
| 7 | + ring.util.test |
| 8 | + [slingshot.slingshot :refer [throw+]]) |
7 | 9 | (:import [java.io PrintStream ByteArrayOutputStream])) |
8 | 10 |
|
9 | 11 | (defmacro without-err |
|
38 | 40 |
|
39 | 41 | (ring.util.test/string-input-stream "foobar") false false)) |
40 | 42 |
|
| 43 | +(def default-options (:exceptions api-middleware-defaults)) |
| 44 | + |
41 | 45 | (facts "wrap-exceptions" |
42 | 46 | (with-out-str |
43 | | - (without-err |
44 | | - (let [exception (RuntimeException. "kosh") |
45 | | - exception-class (.getName (.getClass exception)) |
46 | | - failure (fn [_] (throw exception))] |
47 | | - |
48 | | - (fact "converts exceptions into safe internal server errors" |
49 | | - ((wrap-exceptions failure (:handlers (:exceptions api-middleware-defaults))) ..request..) |
50 | | - => (contains {:status status/internal-server-error |
51 | | - :body (contains {:class exception-class |
52 | | - :type "unknown-exception"})})))))) |
| 47 | + (without-err |
| 48 | + (let [exception (RuntimeException. "kosh") |
| 49 | + exception-class (.getName (.getClass exception)) |
| 50 | + handler (-> (fn [_] (throw exception)) |
| 51 | + (wrap-exceptions default-options))] |
| 52 | + |
| 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"})}))))) |
| 57 | + |
| 58 | + (with-out-str |
| 59 | + (without-err |
| 60 | + (fact "Slingshot exception map type can be matched" |
| 61 | + (let [handler (-> (fn [_] (throw+ {:type ::test} (RuntimeException. "kosh"))) |
| 62 | + (wrap-exceptions (assoc-in default-options [:handlers ::test] (fn [ex _ _] {:status 500 :body "hello"}))))] |
| 63 | + (handler {}) => (contains {:status status/internal-server-error |
| 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