From a65c37b6b87c0dafcdb797dddd70da596f32df11 Mon Sep 17 00:00:00 2001 From: "drake.aren" Date: Thu, 8 Oct 2020 23:09:28 +0600 Subject: [PATCH] Add key-fn tests --- test/ring/middleware/test/json.clj | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/test/ring/middleware/test/json.clj b/test/ring/middleware/test/json.clj index 80c9dc4..554c667 100644 --- a/test/ring/middleware/test/json.clj +++ b/test/ring/middleware/test/json.clj @@ -45,6 +45,13 @@ response (handler request)] (is (= {"foo" "你好"} (:body response)))))) + (let [handler (wrap-json-body identity {:key-fn (fn [k] (.toUpperCase (name k)))})] + (testing "key-fn" + (let [request {:headers {"content-type" "application/json; charset=UTF-8"} + :body (string-input-stream "{\"foo\": \"bar\"}")} + response (handler request)] + (is (= {"FOO" "bar"} (:body response)))))) + (let [handler (wrap-json-body identity {:keywords? true})] (testing "keyword keys" (let [request {:headers {"content-type" "application/json"} @@ -146,7 +153,7 @@ (is (= (get-in @response [:body :bigdecimals]) true))))))) (deftest test-json-params - (let [handler (wrap-json-params identity)] + (let [handler (wrap-json-params identity)] (testing "xml body" (let [request {:headers {"content-type" "application/xml"} :body (string-input-stream "") @@ -164,6 +171,15 @@ (is (= {"id" 3, "foo" "bar"} (:params response))) (is (= {"foo" "bar"} (:json-params response))))) + (testing "key-fn" + (let [request {:headers {"content-type" "application/json; charset=UTF-8"} + :body (string-input-stream "{\"foo\": \"bar\"}") + :params {"id" 3}} + handler (wrap-json-params identity {:key-fn (fn [k] (.toUpperCase (name k)))}) + response (handler request)] + (is (= {"id" 3, "FOO" "bar"} (:params response))) + (is (= {"FOO" "bar"} (:json-params response))))) + (testing "json body with bigdecimals" (let [handler (wrap-json-params identity {:bigdecimals? true}) request {:headers {"content-type" "application/json; charset=UTF-8"} @@ -313,6 +329,13 @@ (is (= (get-in response [:headers "Content-Type"]) "application/json; charset=utf-8")) (is (= (:body response) "[\"foo\",\"bar\"]")))) + (testing "key-fn" + (let [handler (constantly {:status 200 :headers {} :body {:foo "bar" :baz "quz"}}) + response ((wrap-json-response handler {:key-fn (fn [k] (.toUpperCase (name k)))}) {})] + (is (= (get-in response [:headers "Content-Type"]) "application/json; charset=utf-8")) + (is (or (= "{\"FOO\":\"bar\",\"BAZ\":\"quz\"}" (:body response)) + (= "{\"BAZ\":\"quz\",\"FOO\":\"bar\"}" (:body response)))))) + (testing "list body" (let [handler (constantly {:status 200 :headers {} :body '(:foo :bar)}) response ((wrap-json-response handler) {})]