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
|
(ns ring.middleware.default-charset-test
(:use clojure.test
ring.middleware.default-charset
[ring.mock.request :only [request]]
[ring.util.response :only [charset content-type]]))
(deftest test-wrap-charset
(testing "no content-type header present"
(let [handler (wrap-default-charset (constantly {}) "utf-16")
resp (handler request)]
(is (= resp {}))))
(testing "content-type header without charset present"
(let [handler (wrap-default-charset
(constantly (content-type {} "text/html"))
"utf-16")
resp (handler request)]
(is (= (:headers resp)
{"Content-Type" "text/html; charset=utf-16"}))))
(testing "content-type header with charset present"
(let [handler (wrap-default-charset
(constantly (-> {} (content-type "text/html") (charset "utf-8")))
"utf-16")
resp (handler request)]
(is (= (:headers resp)
{"Content-Type" "text/html; charset=utf-8"}))))
(testing "non-text-based content-type"
(let [handler (wrap-default-charset
(constantly (content-type {} "application/gzip"))
"utf-8")
resp (handler request)]
(is (= (:headers resp)
{"Content-Type" "application/gzip"})))))
(deftest test-wrap-charset-cps
(testing "content-type header without charset present"
(let [handler (wrap-default-charset
(fn [_ respond _] (respond (content-type {} "text/html")))
"utf-16")
resp (promise)
ex (promise)]
(handler request resp ex)
(is (not (realized? ex)))
(is (= (:headers @resp) {"Content-Type" "text/html; charset=utf-16"}))))
(testing "content-type header with charset present"
(let [handler (wrap-default-charset
(fn [_ respond _]
(respond (-> {} (content-type "text/html") (charset "utf-8"))))
"utf-16")
resp (promise)
ex (promise)]
(handler request resp ex)
(is (not (realized? ex)))
(is (= (:headers @resp) {"Content-Type" "text/html; charset=utf-8"})))))
|