reitit 2025-06-24

When using reitit-ring is it possible to somehow inject different middleware in tests? We have one authentication method in middleware, that would need to be mocked in tests, but with-redefs has no effect.

I'd recommend just having a function to build the handler fn, taking some parameters, and then building the middleware vector using those options

Or you could just have the mw always enabled, and use route-data (that is set up using options) to control if the mw does anything

As an example: https://github.com/metosin/reitit/blob/master/examples/ring-example/src/example/server.clj#L13-L30 Instead of (def app ...) you'd have (defn create-handler [opts] ...) which is then also used in run-jetty or whatever is the regular entry point

https://github.com/metosin/reitit/blob/master/examples/ring-integrant/src/example/server.clj#L20-L23 IG example is close to this. As the handler fn is created on ig init method, so the db connection or whatever from ig system would be available in the handler code. Same pattern would work for parameters for testing.

Chose to mock one method that the authentication middleware uses. Authentication was not used in all endpoints so this was probably the simplest way.

What if the middleware calls a function that you replace with with-redefs?