Search code examples
clojureringcompojure-api

Compojure-api not respecting body from ring.mock.requests


I'm trying to use the ring.mock.requests library to test an http service. I have this bit of code

   (auth-routes    
     (->
       (mock/request :post "/auth/user")
       (mock/body {:username "user" :password "pass"})
       (mock/content-type "application/json")))

The inner part with the thread-first macro seems to work correctly, but it fails when I try to make the mock request to auth-routes

And this is my route:

(def auth-routes
  (context "/auth" []
  :tags ["Authentication"]
    (POST "/user" []
      :return s/Uuid
      :body [request m/User]
      :summary "Create new user"
      (ok (auth/create-user request)))) ...)

This is my schema:

(def User {:username s/Str
           :password s/Str})

The exception I'm seeing is

clojure.lang.ExceptionInfo
   Request validation failed
   {:error (not (map? nil)), :type :compojure.api.exception/request-validation}

It looks like my route is getting nil as the body, and I expect {:username "user" :password "pass"} to be there.

What am I doing wrong and how do I pass a body to this route?


Solution

  • I think you should serialize your data as a json string in your test. Using cheshire:

    (cheshire.core/generate-string {:username "user" :password "pass"})
    

    Take a look here or here.