7171 (let [response ((wrap-json-body handler {:bigdecimals? false }) {})]
7272 (is (= (get-in response [:body :bigdecimals ]) true )))))))
7373
74+ (deftest test-json-body-cps
75+ (let [identity (fn [request respond _] (respond request))]
76+ (let [handler (wrap-json-body identity)]
77+ (testing " xml body"
78+ (let [request {:headers {" content-type" " application/xml" }
79+ :body (string-input-stream " <xml></xml>" )}
80+ response (promise )
81+ exception (promise )]
82+ (handler request response exception)
83+ (is (= " <xml></xml>" (slurp (:body @response))))
84+ (is (not (realized? exception)))))
85+
86+ (testing " json body"
87+ (let [request {:headers {" content-type" " application/json; charset=UTF-8" }
88+ :body (string-input-stream " {\" foo\" : \" bar\" }" )}
89+ response (promise )
90+ exception (promise )]
91+ (handler request response exception)
92+ (is (= {" foo" " bar" } (:body @response)))
93+ (is (not (realized? exception)))))
94+
95+ (testing " malformed json"
96+ (let [request {:headers {" content-type" " application/json; charset=UTF-8" }
97+ :body (string-input-stream " {\" foo\" : \" bar\" " )}
98+ response (promise )
99+ exception (promise )]
100+ (handler request response exception)
101+ (is (= @response
102+ {:status 400
103+ :headers {" Content-Type" " text/plain" }
104+ :body " Malformed JSON in request body." }))
105+ (is (not (realized? exception))))))
106+
107+ (let [handler (wrap-json-body identity {:keywords? true })]
108+ (testing " keyword keys"
109+ (let [request {:headers {" content-type" " application/json" }
110+ :body (string-input-stream " {\" foo\" : \" bar\" }" )}
111+ response (promise )
112+ exception (promise )]
113+ (handler request response exception)
114+ (is (= {:foo " bar" } (:body @response)))
115+ (is (not (realized? exception))))))
116+
117+ (testing " custom malformed json"
118+ (let [malformed {:status 400
119+ :headers {" Content-Type" " text/html" }
120+ :body " <b>Your JSON is wrong!</b>" }
121+ handler (wrap-json-body identity {:malformed-response malformed})
122+ request {:headers {" content-type" " application/json" }
123+ :body (string-input-stream " {\" foo\" : \" bar\" " )}
124+ response (promise )
125+ exception (promise )]
126+ (handler request response exception)
127+ (is (= @response malformed))
128+ (is (not (realized? exception))))))
129+
130+ (testing " don't overwrite bigdecimal binding"
131+ (let [handler (fn [_ respond _] (respond {:status 200 :headers {} :body {:bigdecimals cheshire.parse/*use-bigdecimals?*}}) )]
132+ (binding [cheshire.parse/*use-bigdecimals?* false ]
133+ (let [response (promise )]
134+ ((wrap-json-body handler {:bigdecimals? true }) {} response (promise ))
135+ (is (= (get-in @response [:body :bigdecimals ]) false ))))
136+ (binding [cheshire.parse/*use-bigdecimals?* true ]
137+ (let [response (promise )]
138+ ((wrap-json-body handler {:bigdecimals? false }) {} response (promise ))
139+ (is (= (get-in @response [:body :bigdecimals ]) true )))))))
74140
75141(deftest test-json-params
76142 (let [handler (wrap-json-params identity)]
151217 (let [response ((wrap-json-params handler {:bigdecimals? false }) {})]
152218 (is (= (get-in response [:body :bigdecimals ]) true )))))))
153219
220+ (deftest test-json-params-cps
221+ (let [identity (fn [request respond _] (respond request))]
222+ (let [handler (wrap-json-params identity)]
223+ (testing " xml body"
224+ (let [request {:headers {" content-type" " application/xml" }
225+ :body (string-input-stream " <xml></xml>" )
226+ :params {" id" 3 }}
227+ response (promise )
228+ exception (promise )]
229+ (handler request response exception)
230+ (is (= " <xml></xml>" (slurp (:body @response))))
231+ (is (= {" id" 3 } (:params @response)))
232+ (is (nil? (:json-params @response)))
233+ (is (not (realized? exception)))))
234+
235+ (testing " json body"
236+ (let [request {:headers {" content-type" " application/json; charset=UTF-8" }
237+ :body (string-input-stream " {\" foo\" : \" bar\" }" )
238+ :params {" id" 3 }}
239+ response (promise )
240+ exception (promise )]
241+ (handler request response exception)
242+ (is (= {" id" 3 , " foo" " bar" } (:params @response)))
243+ (is (= {" foo" " bar" } (:json-params @response)))
244+ (is (not (realized? exception)))))
245+
246+ (testing " malformed json"
247+ (let [request {:headers {" content-type" " application/json; charset=UTF-8" }
248+ :body (string-input-stream " {\" foo\" : \" bar\" " )}
249+ response (promise )
250+ exception (promise )]
251+ (handler request response exception)
252+ (is (= @response
253+ {:status 400
254+ :headers {" Content-Type" " text/plain" }
255+ :body " Malformed JSON in request body." }))
256+ (is (not (realized? exception))))))
257+
258+ (testing " custom malformed json"
259+ (let [malformed {:status 400
260+ :headers {" Content-Type" " text/html" }
261+ :body " <b>Your JSON is wrong!</b>" }
262+ handler (wrap-json-params identity {:malformed-response malformed})
263+ request {:headers {" content-type" " application/json" }
264+ :body (string-input-stream " {\" foo\" : \" bar\" " )}
265+ response (promise )
266+ exception (promise )]
267+ (handler request response exception)
268+ (is (= @response malformed))
269+ (is (not (realized? exception)))))
270+
271+ (testing " don't overwrite bigdecimal binding"
272+ (let [handler (fn [_ respond _] (respond {:status 200 :headers {} :body {:bigdecimals cheshire.parse/*use-bigdecimals?*}}) )]
273+ (binding [cheshire.parse/*use-bigdecimals?* false ]
274+ (let [response (promise )]
275+ ((wrap-json-params handler {:bigdecimals? true }) {} response (promise ))
276+ (is (= (get-in @response [:body :bigdecimals ]) false ))))
277+ (binding [cheshire.parse/*use-bigdecimals?* true ]
278+ (let [response (promise )]
279+ ((wrap-json-params handler {:bigdecimals? false }) {} response (promise ))
280+ (is (= (get-in @response [:body :bigdecimals ]) true ))))))))
281+
154282(deftest test-json-response
155283 (testing " map body"
156284 (let [handler (constantly {:status 200 :headers {} :body {:foo " bar" }})
194322 response ((wrap-json-response handler) {})]
195323 (is (= (get-in response [:headers " Content-Type" ]) " application/json; some-param=some-value" ))
196324 (is (= (:body response) " {\" foo\" :\" bar\" }" )))))
325+
326+ (deftest test-json-response-cps
327+ (testing " map body"
328+ (let [handler (fn [_ respond _] (respond {:status 200 :headers {} :body {:foo " bar" }}))
329+ response (promise )
330+ exception (promise )]
331+ ((wrap-json-response handler) {} response exception)
332+ (is (= (get-in @response [:headers " Content-Type" ]) " application/json; charset=utf-8" ))
333+ (is (= (:body @response) " {\" foo\" :\" bar\" }" ))
334+ (is (not (realized? exception)))))
335+
336+ (testing " string body"
337+ (let [handler (fn [_ respond _] (respond {:status 200 :headers {} :body " foobar" }))
338+ response (promise )
339+ exception (promise )]
340+ ((wrap-json-response handler) {} response exception)
341+ (is (= (:headers @response) {}))
342+ (is (= (:body @response) " foobar" ))
343+ (is (not (realized? exception)))))
344+
345+ (testing " JSON options"
346+ (let [handler (fn [_ respond _] (respond {:status 200 :headers {} :body {:foo " bar" :baz " quz" }}))
347+ response (promise )
348+ exception (promise )]
349+ ((wrap-json-response handler {:pretty true }) {} response exception)
350+ (is (or (= (:body @response) " {\n \" foo\" : \" bar\" ,\n \" baz\" : \" quz\"\n }" )
351+ (= (:body @response) " {\n \" baz\" : \" quz\" ,\n \" foo\" : \" bar\"\n }" )))
352+ (is (not (realized? exception)))))
353+
354+ (testing " don’t overwrite Content-Type if already set"
355+ (let [handler (fn [_ respond _] (respond {:status 200 :headers {" Content-Type" " application/json; some-param=some-value" } :body {:foo " bar" }}))
356+ response (promise )
357+ exception (promise )]
358+ ((wrap-json-response handler) {} response exception)
359+ (is (= (get-in @response [:headers " Content-Type" ]) " application/json; some-param=some-value" ))
360+ (is (= (:body @response) " {\" foo\" :\" bar\" }" ))
361+ (is (not (realized? exception))))))
0 commit comments