Merge pull request #284 from kardan/master

[#283] Allow to pass some defaults to create-default-handler
This commit is contained in:
Tommi Reiman 2019-05-23 14:28:04 +03:00 committed by GitHub
commit 26ab2cbf6f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 12 deletions

View file

@ -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)]

View file

@ -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"}]