mirror of
https://github.com/metosin/reitit.git
synced 2025-12-17 00:11:11 +00:00
Merge pull request #284 from kardan/master
[#283] Allow to pass some defaults to create-default-handler
This commit is contained in:
commit
26ab2cbf6f
2 changed files with 18 additions and 12 deletions
|
|
@ -143,14 +143,14 @@
|
|||
| key | description |
|
||||
| -----------------------|-------------|
|
||||
| `:not-found` | 404, no routes matches
|
||||
| `:method-not-accepted` | 405, no method matches
|
||||
| `:method-not-allowed` | 405, no method matches
|
||||
| `:not-acceptable` | 406, handler returned `nil`"
|
||||
([]
|
||||
(create-default-handler
|
||||
{:not-found (constantly {:status 404, :body "", :headers {}})
|
||||
:method-not-allowed (constantly {:status 405, :body "", :headers {}})
|
||||
:not-acceptable (constantly {:status 406, :body "", :headers {}})}))
|
||||
([{:keys [not-found method-not-allowed not-acceptable]}]
|
||||
(create-default-handler {}))
|
||||
([{:keys [not-found method-not-allowed not-acceptable]
|
||||
:or {not-found (constantly {:status 404, :body "", :headers {}})
|
||||
method-not-allowed (constantly {:status 405, :body "", :headers {}})
|
||||
not-acceptable (constantly {:status 406, :body "", :headers {}})}}]
|
||||
(fn
|
||||
([request]
|
||||
(if-let [match (::r/match request)]
|
||||
|
|
|
|||
|
|
@ -161,8 +161,8 @@
|
|||
(deftest default-handler-test
|
||||
(let [response {:status 200, :body "ok"}
|
||||
router (ring/router
|
||||
[["/ping" {:get (constantly response)}]
|
||||
["/pong" (constantly nil)]])
|
||||
[["/ping" {:get (constantly response)}]
|
||||
["/pong" (constantly nil)]])
|
||||
app (ring/ring-handler router)]
|
||||
|
||||
(testing "match"
|
||||
|
|
@ -188,15 +188,21 @@
|
|||
|
||||
(testing "with custom http responses"
|
||||
(let [app (ring/ring-handler router (ring/create-default-handler
|
||||
{:not-found (constantly {:status -404})
|
||||
:method-not-allowed (constantly {:status -405})
|
||||
:not-acceptable (constantly {:status -406})}))]
|
||||
{:not-found (constantly {:status -404})
|
||||
:method-not-allowed (constantly {:status -405})
|
||||
:not-acceptable (constantly {:status -406})}))]
|
||||
(testing "route doesn't match"
|
||||
(is (= -404 (:status (app {:request-method :get, :uri "/"})))))
|
||||
(testing "method doesn't match"
|
||||
(is (= -405 (:status (app {:request-method :post, :uri "/ping"})))))
|
||||
(testing "handler rejects"
|
||||
(is (= -406 (:status (app {:request-method :get, :uri "/pong"}))))))))))
|
||||
(is (= -406 (:status (app {:request-method :get, :uri "/pong"})))))))
|
||||
|
||||
(testing "with some custom http responses"
|
||||
(let [app (ring/ring-handler router (ring/create-default-handler
|
||||
{:not-found (constantly {:status -404})}))]
|
||||
(testing "route doesn't match"
|
||||
(is (= 405 (:status (app {:request-method :post, :uri "/ping"}))))))))))
|
||||
|
||||
(deftest default-options-handler-test
|
||||
(let [response {:status 200, :body "ok"}]
|
||||
|
|
|
|||
Loading…
Reference in a new issue