forked from metosin/muuntaja
-
Notifications
You must be signed in to change notification settings - Fork 0
/
format_test.clj
75 lines (69 loc) · 3.07 KB
/
format_test.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
(ns ring.middleware.format-test
(:use [clojure.test]
[ring.middleware.format])
(:require [cheshire.core :as json]
[clj-yaml.core :as yaml])
(:import [java.io ByteArrayInputStream]))
(defn stream [s]
(ByteArrayInputStream. (.getBytes s "UTF-8")))
(def restful-echo
(wrap-restful-format (fn [req] (assoc req :body (vals (:body-params req))))))
(def restful-echo-opts-map
(wrap-restful-format (fn [req] (assoc req :body (vals (:body-params req))))))
(def restful-echo-json
(wrap-restful-format (fn [req] (assoc req :body (vals (:body-params req))))
:formats [:json-kw]))
(def restful-echo-yaml
(wrap-restful-format (fn [req] (assoc req :body (vals (:body-params req))))
:formats [:yaml-kw]))
(deftest test-restful-round-trip
(let [ok-accept "application/edn"
msg {:test :ok}
ok-req {:headers {"accept" ok-accept}
:content-type ok-accept
:body (stream (pr-str msg))}
r-trip (restful-echo ok-req)]
(is (= (get-in r-trip [:headers "Content-Type"])
"application/edn; charset=utf-8"))
(is (= (read-string (slurp (:body r-trip))) (vals msg)))
(is (= (:params r-trip) msg))
(is (.contains (get-in (restful-echo {:headers {"accept" "foo/bar"}})
[:headers "Content-Type"])
"application/json"))
(is (restful-echo {:headers {"accept" "foo/bar"}})))
(let [ok-accept "application/json"
msg {"test" "ok"}
ok-req {:headers {"accept" ok-accept}
:content-type ok-accept
:body (stream (json/encode msg))}
r-trip (restful-echo-json ok-req)]
(is (= (get-in r-trip [:headers "Content-Type"])
"application/json; charset=utf-8"))
(is (= (json/decode (slurp (:body r-trip))) (vals msg)))
(is (= (:params r-trip) {:test "ok"}))
(is (.contains (get-in (restful-echo-json
{:headers {"accept" "application/edn"}
:content-type "application/edn"})
[:headers "Content-Type"])
"application/json")))
(let [ok-accept "application/x-yaml"
msg {"test" "ok"}
ok-req {:headers {"accept" ok-accept}
:content-type ok-accept
:body (stream (yaml/generate-string msg))}
r-trip (restful-echo-yaml ok-req)]
(is (= (:params r-trip) {:test "ok"})))
(let [ok-accept "application/edn"
msg {:test :ok}
ok-req {:headers {"accept" ok-accept}
:content-type ok-accept
:body (stream (pr-str msg))}
r-trip (restful-echo-opts-map ok-req)]
(is (= (get-in r-trip [:headers "Content-Type"])
"application/edn; charset=utf-8"))
(is (= (read-string (slurp (:body r-trip))) (vals msg)))
(is (= (:params r-trip) msg))
(is (.contains (get-in (restful-echo-opts-map {:headers {"accept" "foo/bar"}})
[:headers "Content-Type"])
"application/json"))
(is (restful-echo-opts-map {:headers {"accept" "foo/bar"}}))))