gpt4 book ai didi

clojure - 如何使用 ring.mock.request 模拟 PUT 请求

转载 作者:行者123 更新时间:2023-12-01 11:27:16 25 4
gpt4 key购买 nike

我如何让这个测试通过:

(ns imp-rest.parser-test-rest
(:require [clojure.test :refer :all])
(:require [ring.mock.request :as mock] )
(:require [imp-rest.web :as w]))

(deftest test-parser-rest
(testing "put settings"
(w/app
(mock/request :put "/settings/coordinateName" "FOO" ))

(let [response (w/app (mock/request :get "/settings"))]
(println response )
(is (= (get (:body response) :coordinateName) "FOO")))))

它失败了:
FAIL in (test-parser-rest) (parser_test_rest.clj:30)
put settings
expected: (= (get (:body response) :coordinateName) "FOO")
actual: (not (= nil "FOO"))

这是我的处理程序:
(ns imp-rest.web
(:use compojure.core)
(:use ring.middleware.json-params)
(:require [clj-json.core :as json])
(:require [ring.util.response :as response])
(:require [compojure.route :as route])
(:require [imp-rest.settings :as s]))

(defn json-response [data & [status]]
{:status (or status 200)
:headers {"Content-Type" "application/json"}
:body (json/generate-string data)})

(defroutes handler

(GET "/settings" []
(json-response (s/get-settings)))

(GET "/settings/:id" [id]
(json-response (s/get-setting id)))

(PUT "/settings" [id value]
(json-response (s/put-setting id value)))

(route/not-found "Page not found") )

(def app
(-> handler
wrap-json-params))

它公开了这张 map (设置):
(ns imp-rest.settings)

(def settings
(atom
{:coordinateName nil
:burnin nil
:nslices nil
:mrsd nil
}))

(defn get-settings []
@settings)

(defn get-setting [id]
(@settings (keyword id)))

(defn put-setting [id value]
(swap! settings assoc (keyword id) value)
value)

和入口点:
(ns imp-rest.core
(:use ring.adapter.jetty)
(:require [imp-rest.web :as web]))

(defn -main
"Entry point"
[& args]
(do
(run-jetty #'web/app {:port 8080})
);END;do
);END: main

现在,当我 'lein run' 时,我可以发出这样的(工作)请求:
curl -X PUT -H "Content-Type: application/json" \
-d '{"id" : "coordinateName", "value" : "FOO"}' \
http://localhost:8080/settings

这就是我试图用测试来模拟的。任何帮助表示赞赏。

最佳答案

如果你想拥有 :id在您的 PUT /settings/:id路由接受体格式 {"value": "..."} ,您需要更改您的路线定义:

(defroutes handler

(GET "/settings" []
(json-response (s/get-settings)))

(GET "/settings/:id" [id]
(json-response (s/get-setting id)))

(PUT "/settings/:id" [id value]
(json-response (s/put-setting id value)))

(route/not-found "Page not found"))

并改变您如何称呼您的 PUT测试中的端点:
(w/app 
(-> (mock/request
:put
"/settings/coordinateName"
(json/generate-string {:value "FOO"}))
(mock/content-type "application/json")))

改变了什么?
  • :id在您的 PUT URL 路由定义 ( /settings -> /settings/:id )
  • 您的 PUT request 没有发送正确的请求和内容类型。

  • 如果您想拥有一个 PUT /settings路线期待 {"id": "...", "value": "..."}请求正文,那么您需要更改创建模拟请求的方式:
    (w/app 
    (-> (mock/request
    :put
    "/settings"
    (json/generate-string {:id "coordinateName" :value "FOO"}))
    (mock/content-type "application/json"))

    关于clojure - 如何使用 ring.mock.request 模拟 PUT 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36350637/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com