metosin / reitit

A fast data-driven routing library for Clojure/Script

Home Page:https://cljdoc.org/d/metosin/reitit/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Why can't I redef a handler?

wolfgang opened this issue · comments

(defn the-handler [_] {:status 200 :body "ok"})

(def the-router (ring/router [["/handle" {:get the-handler}]]))

(def app (ring/ring-handler the-router))

(app {:request-method :get :uri "/handle"})

(with-redefs [the-handler (constantly {:status 500 :body "not ok"})]
  (app {:request-method :get :uri "/handle"}))

The last form will still output {:status 200, :body "ok"} and I was wondering why that is.

in the line:

(def the-router (ring/router [["/handle" {:get the-handler}]]))

you are passing in function value (e.g. Clojure derefs the var here). Using {:get (fn [request] (the-handler request))} works. Not sure if {:get #'the-handler} works too.

Ah, cool, Thanks for the answer!