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 | | key | description |
| -----------------------|-------------| | -----------------------|-------------|
| `:not-found` | 404, no routes matches | `: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`" | `:not-acceptable` | 406, handler returned `nil`"
([] ([]
(create-default-handler (create-default-handler {}))
{:not-found (constantly {:status 404, :body "", :headers {}}) ([{:keys [not-found method-not-allowed not-acceptable]
:method-not-allowed (constantly {:status 405, :body "", :headers {}}) :or {not-found (constantly {:status 404, :body "", :headers {}})
:not-acceptable (constantly {:status 406, :body "", :headers {}})})) method-not-allowed (constantly {:status 405, :body "", :headers {}})
([{:keys [not-found method-not-allowed not-acceptable]}] not-acceptable (constantly {:status 406, :body "", :headers {}})}}]
(fn (fn
([request] ([request]
(if-let [match (::r/match request)] (if-let [match (::r/match request)]

View file

@ -161,8 +161,8 @@
(deftest default-handler-test (deftest default-handler-test
(let [response {:status 200, :body "ok"} (let [response {:status 200, :body "ok"}
router (ring/router router (ring/router
[["/ping" {:get (constantly response)}] [["/ping" {:get (constantly response)}]
["/pong" (constantly nil)]]) ["/pong" (constantly nil)]])
app (ring/ring-handler router)] app (ring/ring-handler router)]
(testing "match" (testing "match"
@ -188,15 +188,21 @@
(testing "with custom http responses" (testing "with custom http responses"
(let [app (ring/ring-handler router (ring/create-default-handler (let [app (ring/ring-handler router (ring/create-default-handler
{:not-found (constantly {:status -404}) {:not-found (constantly {:status -404})
:method-not-allowed (constantly {:status -405}) :method-not-allowed (constantly {:status -405})
:not-acceptable (constantly {:status -406})}))] :not-acceptable (constantly {:status -406})}))]
(testing "route doesn't match" (testing "route doesn't match"
(is (= -404 (:status (app {:request-method :get, :uri "/"}))))) (is (= -404 (:status (app {:request-method :get, :uri "/"})))))
(testing "method doesn't match" (testing "method doesn't match"
(is (= -405 (:status (app {:request-method :post, :uri "/ping"}))))) (is (= -405 (:status (app {:request-method :post, :uri "/ping"})))))
(testing "handler rejects" (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 (deftest default-options-handler-test
(let [response {:status 200, :body "ok"}] (let [response {:status 200, :body "ok"}]