Compojure-api 不尊重 ring.mock.requests 的 body

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

我正在尝试使用 ring.mock.requests 库来测试 http 服务。我有这段代码

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

带有 thread-first 宏的内部部分似乎工作正常,但是当我尝试向 auth-routes

发出模拟请求时它失败了

这是我的路线:

(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)))) ...)

这是我的架构:

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

我看到的例外情况是

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

看起来我的路线正在 nil 作为 body,我希望 {:username "user" :password "pass"} 在那里。

我做错了什么以及如何将 body 传递给这条路线?

我认为您应该在测试中将数据序列化为 json 字符串。使用 cheshire:

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

看看here or here.