Skip to content

Commit

Permalink
Add key-fn tests
Browse files Browse the repository at this point in the history
  • Loading branch information
drake.aren committed Oct 8, 2020
1 parent bff9521 commit 4ff28af
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion test/ring/middleware/test/json.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down Expand Up @@ -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 "<xml></xml>")
Expand All @@ -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"}
Expand Down Expand Up @@ -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) {})]
Expand Down

0 comments on commit 4ff28af

Please sign in to comment.