【问题标题】:Compojure-api not respecting body from ring.mock.requestsCompojure-api 不尊重来自 ring.mock.requests 的主体
【发布时间】:2016-09-23 20:08:35
【问题描述】:

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

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

带有线程优先宏的内部部分似乎可以正常工作,但是当我尝试向 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 为主体,我希望{:username "user" :password "pass"} 在那里。

我做错了什么以及如何将身体传递到这条路线?

【问题讨论】:

    标签: clojure ring compojure-api


    【解决方案1】:

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

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

    看看herehere

    【讨论】:

    • 您的auth-routes 定义是否正确?你的POST "/user" []不应该是POST "/user" request吗?
    • compojure-api 的例子是正确的here
    • 好的,我想你需要序列化。看看here。我还创建了一个小的repo 来测试它并且它正在工作。
    • 您的示例对我有用,但是当我删除序列化时,我得到一个不同的错误。我在您的示例中看到您正在使用 api 函数来包装路线,我在这里不这样做,因为我将它们放在其他地方。因为我错过了这一点,所以 compojure-api 的 :body 参数没有得到尊重(当然)。
    猜你喜欢
    • 2018-03-23
    • 2015-02-26
    • 1970-01-01
    • 2014-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-21
    相关资源
    最近更新 更多