Skip to content

Commit c7cbfd1

Browse files
authored
Merge pull request #184 from svdm/master
Avoid exception when coercion in context fails
2 parents 7c4de64 + 7d43e11 commit c7cbfd1

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

src/compojure/core.clj

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,12 @@
282282
(defn ^:no-doc make-context [route make-handler]
283283
(letfn [(handler
284284
([request]
285-
((make-handler request) request))
285+
(when-let [context-handler (make-handler request)]
286+
(context-handler request)))
286287
([request respond raise]
287-
((make-handler request) request respond raise)))]
288+
(if-let [context-handler (make-handler request)]
289+
(context-handler request respond raise)
290+
(respond nil))))]
288291
(if (#{":__path-info" "/:__path-info"} (:source route))
289292
handler
290293
(fn

test/compojure/core_test.clj

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,16 @@
6161
(is (nil? ((GET "/foo/:x" [x :<< coercions/as-int] (str x))
6262
(mock/request :get "/foo/bar")))))
6363

64+
(testing "nil coercions in contexts"
65+
(is (not (nil? ((context "/foo/:x" [x] (GET "/" [] (str x)))
66+
(mock/request :get "/foo/bar")))))
67+
(is (not (nil? ((context "/foo/:x" [x :<< coercions/as-int] (GET "/" [] (str x)))
68+
(mock/request :get "/foo/100")))))
69+
(is (not (nil? ((context "/foo/:x" [x :<< #(Boolean/valueOf %)] (GET "/" [] (str x)))
70+
(mock/request :get "/foo/false")))))
71+
(is (nil? ((context "/foo/:x" [x :<< coercions/as-int] (GET "/" [] (str x)))
72+
(mock/request :get "/foo/bar")))))
73+
6474
(testing "map arguments"
6575
((GET "/foo" {params :params}
6676
(is (= params {:x "a", :y "b"}))
@@ -397,7 +407,25 @@
397407
exception (promise)]
398408
(route request response exception)
399409
(is (not (realized? exception)))
400-
(is (nil? @response))))))
410+
(is (nil? @response)))))
411+
412+
(testing "with coercion"
413+
(let [route (context "/:id" [id :<< coercions/as-int]
414+
(GET "/" [] (str id)))]
415+
(testing "matching request"
416+
(let [request (mock/request :get "/123")
417+
response (promise)
418+
exception (promise)]
419+
(route request response exception)
420+
(is (not (realized? exception)))
421+
(is (= (:body @response) "123"))))
422+
(testing "not-matching request"
423+
(let [request (mock/request :get "/foo")
424+
response (promise)
425+
exception (promise)]
426+
(route request response exception)
427+
(is (not (realized? exception)))
428+
(is (nil? @response)))))))
401429

402430
(testing "wrap-routes"
403431
(let [route (wrap-routes

0 commit comments

Comments
 (0)