reitit

Leena Hyvönen 2025-06-24T09:56:59.754409Z

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.

juhoteperi 2025-06-25T09:24:26.621749Z

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

juhoteperi 2025-06-25T09:24:56.685059Z

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

juhoteperi 2025-06-25T12:22:14.776169Z

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

juhoteperi 2025-06-25T12:23:28.002349Z

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.

Leena Hyvönen 2025-06-26T06:40:45.038679Z

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

DrLjótsson 2025-06-24T15:06:45.649719Z

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