2

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?

munk
  • 12,340
  • 8
  • 51
  • 71

1 Answers1

1

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.

Community
  • 1
  • 1
kongeor
  • 712
  • 1
  • 6
  • 14
  • Is your `auth-routes` definition correct? Shouldn't your `POST "/user" []` be something like `POST "/user" request`? – kongeor May 26 '16 at 14:32
  • It is correct by the compojure-api examples [here](https://github.com/metosin/compojure-api) – munk May 26 '16 at 14:41
  • OK, I think you need serialization. Take a look [here](https://github.com/metosin/compojure-api/wiki/Testing-api-endpoints#testing-post-endpoint). Also I've created a small [repo](https://github.com/kongeor/compringmock) to test it and it is working. – kongeor May 26 '16 at 15:56
  • Your example works for me, but when I remove the serialization, I get a different error. I see in your example you're using the `api` function to wrap the routes, which I don't do here because I'm pulling them together elsewhere. Because I'm missing that, the :body parameter for compojure-api isn't being respected (of course). – munk May 27 '16 at 11:45