Why can't I redef a handler?
wolfgang opened this issue · comments
Wolfgang Deutsch commented
(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.
Tommi Reiman commented
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.
Wolfgang Deutsch commented
Ah, cool, Thanks for the answer!