Fork me on GitHub
#reitit
<
2022-05-03
>
oly13:05:47

anyone got any tests that call a router, passing a json body and handling it ? I am noting that my route is not decoding the json so think I am missing a step and would like to see some examples wondering if its something silly like some data I am not sending

Ben Sless15:05:47

Ping me here in a few hours if I forget to share an example

oly07:05:01

many hours later but ping 🙂

Ben Sless08:05:26

okay, what I started doing is using ring.mock.request

Ben Sless08:05:35

refer to request and json-body

Ben Sless08:05:28

then (-> (request :post "/your/route") (json-body {:your body}) your-handler) will invoke the ring handler on it and you can test yourself

Ben Sless08:05:42

Then it's up to you if you want to use example based testing, or even generative tests

oly09:05:24

I actually just got it working, not sure why but I had muuntaja specified in the router config and it was messing up things

oly09:05:52

I ended up creating a test router with this code.

oly09:05:57

(def test-router
  (->
   (ring/ring-handler
    (ring/router [["/test"
                   ["/cors" {:get {:middleware [cors-headers]
                                     :handler (fn [_] {:status 200 :body "ok"})}}]
                   ["/cache" {:get {:middleware [cache-header-one-hour]
                                      :handler (fn [_] {:status 200 :body "ok"})}}]
                   ["/pubsub" {:post {:summary "test"
                                      :parameters {:body [:map {:closed false}]}
                                      :middleware [decode-pubsub-json-middleware]
                                      :handler (fn [payload] {:status 200 :body (-> payload :parameters :body)})}}]]]
                 {:data {:coercion reitit.coercion.malli/coercion
                         :middleware [parameters/parameters-middleware
                                      muuntaja/format-negotiate-middleware
                                      muuntaja/format-response-middleware
                                      muuntaja/format-request-middleware
                                      coercion/coerce-response-middleware
                                      coercion/coerce-request-middleware
                                      coercion/coerce-exceptions-middleware]}}))))

oly09:05:48

than calling it in the test like so

oly09:05:52

(test-router
          {:uri "/test/pubsub" :request-method :post
           :content-type "application/json"
           :body-params {:message {:data "decode"}})

oly09:05:01

cant explain why this wa tripping me up ":muuntaja new-muuntaja-instance" but it was causing my router to be invalid, luckily I dont actually need it for the test 🙂

oly09:05:27

not familiar with ring.mock.request so I will look that up

Ben Sless09:05:20

It's just a utility for building ring requests correctly