|
31 | 31 | :default {:schema {:value s/Int}}}
|
32 | 32 | (ok {:value (or value "123")}))]
|
33 | 33 | (testing "200"
|
34 |
| - (is-has-body {:value "123"} (get* (api r-200) "/")) |
35 |
| - (is-fails-with 500 (get* (api r-200) "/" {:value 123}))) |
| 34 | + (is-has-body {:value "123"} (get* (api {:formatter :muuntaja} r-200) "/")) |
| 35 | + (is-fails-with 500 (get* (api {:formatter :muuntaja} r-200) "/" {:value 123}))) |
36 | 36 |
|
37 | 37 | (testing "exception data"
|
38 |
| - (let [ex (get* (api r-200) "/" {:value 123})] |
| 38 | + (let [ex (get* (api {:formatter :muuntaja} r-200) "/" {:value 123})] |
39 | 39 | (is (= 500 (first ex)))
|
40 | 40 | (is (= {:type "compojure.api.exception/response-validation"
|
41 | 41 | :coercion "schema",
|
|
46 | 46 | (select-keys (second ex) [:type :coercion :in :value :schema :errors])))))
|
47 | 47 |
|
48 | 48 | (testing ":default"
|
49 |
| - (is-has-body {:value "123"} (get* (api r-default) "/")) |
50 |
| - (is-fails-with 500 (get* (api r-default) "/" {:value 123}))) |
| 49 | + (is-has-body {:value "123"} (get* (api {:formatter :muuntaja} r-default) "/")) |
| 50 | + (is-fails-with 500 (get* (api {:formatter :muuntaja} r-default) "/" {:value 123}))) |
51 | 51 |
|
52 | 52 | (testing ":default"
|
53 |
| - (is-has-body {:value "123"} (get* (api r-200-default) "/")) |
54 |
| - (is-fails-with 500 (get* (api r-200-default) "/" {:value 123}))))) |
| 53 | + (is-has-body {:value "123"} (get* (api {:formatter :muuntaja} r-200-default) "/")) |
| 54 | + (is-fails-with 500 (get* (api {:formatter :muuntaja} r-200-default) "/" {:value 123}))))) |
55 | 55 |
|
56 | 56 | (testing "custom coercion"
|
57 | 57 |
|
|
62 | 62 |
|
63 | 63 | (testing "by default, applies response coercion"
|
64 | 64 | (let [app (api
|
| 65 | + {:formatter :muuntaja} |
65 | 66 | ping-route)]
|
66 | 67 | (is-fails-with 500 (get* app "/ping"))))
|
67 | 68 |
|
68 | 69 | (testing "response-coercion can be disabled"
|
69 | 70 | (testing "separately"
|
70 | 71 | (let [app (api
|
71 |
| - {:coercion (cs/create-coercion (dissoc cs/default-options :response))} |
| 72 | + {:formatter :muuntaja |
| 73 | + :coercion (cs/create-coercion (dissoc cs/default-options :response))} |
72 | 74 | ping-route)]
|
73 | 75 | (let [[status body] (get* app "/ping")]
|
74 | 76 | (is (= 200 status))
|
75 | 77 | (is (= {:pong 123} body)))))
|
76 | 78 | (testing "all coercion"
|
77 | 79 | (let [app (api
|
78 |
| - {:coercion nil} |
| 80 | + {:formatter :muuntaja |
| 81 | + :coercion nil} |
79 | 82 | ping-route)]
|
80 | 83 | (let [[status body] (get* app "/ping")]
|
81 | 84 | (is (= 200 status))
|
|
84 | 87 | (testing "coercion for async handlers"
|
85 | 88 | (binding [*async?* true]
|
86 | 89 | (testing "successful"
|
87 |
| - (let [app (api (GET "/async" [] |
| 90 | + (let [app (api |
| 91 | + {:formatter :muuntaja} |
| 92 | + (GET "/async" [] |
88 | 93 | :return s/Str
|
89 | 94 | (a/go (ok "abc"))))]
|
90 | 95 | (is-has-body "abc" (get* app "/async"))))
|
91 | 96 | (testing "failing"
|
92 |
| - (let [app (api (GET "/async" [] |
| 97 | + (let [app (api |
| 98 | + {:formatter :muuntaja} |
| 99 | + (GET "/async" [] |
93 | 100 | :return s/Int
|
94 | 101 | (a/go (ok "foo"))))]
|
95 | 102 | (is-fails-with 500 (get* app "/async"))))))))
|
|
101 | 108 |
|
102 | 109 | (testing "by default, applies body coercion (to set)"
|
103 | 110 | (let [app (api
|
| 111 | + {:formatter :muuntaja} |
104 | 112 | beer-route)]
|
105 | 113 | (let [[status body] (post* app "/beer" (json-string {:beers ["ipa" "apa" "ipa"]}))]
|
106 | 114 | (is (= 200 status))
|
|
109 | 117 | (testing "body-coercion can be disabled"
|
110 | 118 | (let [no-body-coercion (cs/create-coercion (dissoc cs/default-options :body))
|
111 | 119 | app (api
|
112 |
| - {:coercion no-body-coercion} |
| 120 | + {:formatter :muuntaja |
| 121 | + :coercion no-body-coercion} |
113 | 122 | beer-route)]
|
114 | 123 | (let [[status body] (post* app "/beer" (json-string {:beers ["ipa" "apa" "ipa"]}))]
|
115 | 124 | (is (= 200 status))
|
116 | 125 | (is (= {:beers ["ipa" "apa" "ipa"]} body))))
|
117 | 126 | (let [app (api
|
118 |
| - {:coercion nil} |
| 127 | + {:formatter :muuntaja |
| 128 | + :coercion nil} |
119 | 129 | beer-route)]
|
120 | 130 | (let [[status body] (post* app "/beer" (json-string {:beers ["ipa" "apa" "ipa"]}))]
|
121 | 131 | (is (= 200 status))
|
|
124 | 134 | (testing "body-coercion can be changed"
|
125 | 135 | (let [nop-body-coercion (cs/create-coercion (assoc cs/default-options :body {:default (constantly nil)}))
|
126 | 136 | app (api
|
127 |
| - {:coercion nop-body-coercion} |
| 137 | + {:formatter :muuntaja |
| 138 | + :coercion nop-body-coercion} |
128 | 139 | beer-route)]
|
129 | 140 | (is-fails-with 400 (post* app "/beer" (json-string {:beers ["ipa" "apa" "ipa"]})))))))
|
130 | 141 |
|
|
135 | 146 |
|
136 | 147 | (testing "by default, applies query coercion (string->int)"
|
137 | 148 | (let [app (api
|
| 149 | + {:formatter :muuntaja} |
138 | 150 | query-route)]
|
139 | 151 | (let [[status body] (get* app "/query" {:i 10})]
|
140 | 152 | (is (= 200 status))
|
|
143 | 155 | (testing "query-coercion can be disabled"
|
144 | 156 | (let [no-query-coercion (cs/create-coercion (dissoc cs/default-options :string))
|
145 | 157 | app (api
|
146 |
| - {:coercion no-query-coercion} |
| 158 | + {:formatter :muuntaja |
| 159 | + :coercion no-query-coercion} |
147 | 160 | query-route)]
|
148 | 161 | (let [[status body] (get* app "/query" {:i 10})]
|
149 | 162 | (is (= 200 status))
|
|
152 | 165 | (testing "query-coercion can be changed"
|
153 | 166 | (let [nop-query-coercion (cs/create-coercion (assoc cs/default-options :string {:default (constantly nil)}))
|
154 | 167 | app (api
|
155 |
| - {:coercion nop-query-coercion} |
| 168 | + {:formatter :muuntaja |
| 169 | + :coercion nop-query-coercion} |
156 | 170 | query-route)]
|
157 | 171 | (is-fails-with 400 (get* app "/query" {:i 10}))))))
|
158 | 172 |
|
159 | 173 | (testing "route-specific coercion"
|
160 | 174 | (let [app (api
|
| 175 | + {:formatter :muuntaja} |
161 | 176 | (GET "/default" []
|
162 | 177 | :query-params [i :- s/Int]
|
163 | 178 | (ok {:i i}))
|
|
0 commit comments