diff --git a/project.clj b/project.clj
index f3d141d..971a992 100644
--- a/project.clj
+++ b/project.clj
@@ -5,9 +5,11 @@
:url "http://opensource.org/licenses/MIT"}
:dependencies [[org.clojure/clojure "1.3.0"]
[cheshire "5.3.1"]
- [ring/ring-core "1.2.2"]]
+ [ring/ring-core "1.3.2"]
+ [ring/ring-defaults "0.1.4"]]
:plugins [[codox "0.8.0"]]
:profiles
- {:1.4 {:dependencies [[org.clojure/clojure "1.4.0"]]}
+ {:dev {:dependencies [[ring/ring-mock "0.2.0"]]}
+ :1.4 {:dependencies [[org.clojure/clojure "1.4.0"]]}
:1.5 {:dependencies [[org.clojure/clojure "1.5.1"]]}
:1.6 {:dependencies [[org.clojure/clojure "1.6.0"]]}})
diff --git a/test/ring/middleware/test/json.clj b/test/ring/middleware/test/json.clj
index 7f8e799..c064ad9 100644
--- a/test/ring/middleware/test/json.clj
+++ b/test/ring/middleware/test/json.clj
@@ -1,124 +1,124 @@
(ns ring.middleware.test.json
- (:use ring.middleware.json
- clojure.test
- ring.util.io))
+ (:require [clojure.test :refer :all]
+ [ring.middleware.json :refer :all]
+ [ring.mock.request :refer [request content-type body]]
+ [ring.util.response :refer [response header]]
+ [ring.middleware.params :refer [wrap-params]]
+ [ring.middleware.defaults :refer [wrap-defaults api-defaults]]))
(deftest test-json-body
(let [handler (wrap-json-body identity)]
(testing "xml body"
- (let [request {:content-type "application/xml"
- :body (string-input-stream "")}
- response (handler request)]
- (is (= "") (slurp (:body response)))))
+ (let [resp (handler (-> (request :get "/")
+ (content-type "application/xml")
+ (body "")))]
+ (is (= "") (:body resp))))
(testing "json body"
- (let [request {:content-type "application/json; charset=UTF-8"
- :body (string-input-stream "{\"foo\": \"bar\"}")}
- response (handler request)]
- (is (= {"foo" "bar"} (:body response)))))
+ (let [resp (handler (-> (request :get "/")
+ (content-type "application/json; charset=UTF-8")
+ (body "{\"foo\": \"bar\"}")))]
+ (is (= {"foo" "bar"} (:body resp)))))
(testing "custom json body"
- (let [request {:content-type "application/vnd.foobar+json; charset=UTF-8"
- :body (string-input-stream "{\"foo\": \"bar\"}")}
- response (handler request)]
- (is (= {"foo" "bar"} (:body response)))))
+ (let [resp (handler (-> (request :get "/")
+ (content-type "application/vnd.foobar+json; charset=UTF-8")
+ (body "{\"foo\": \"bar\"}")))]
+ (is (= {"foo" "bar"} (:body resp)))))
(testing "json patch body"
- (let [json-string "[{\"op\": \"add\",\"path\":\"/foo\",\"value\": \"bar\"}]"
- request {:content-type "application/json-patch+json; charset=UTF-8"
- :body (string-input-stream json-string)}
- response (handler request)]
- (is (= [{"op" "add" "path" "/foo" "value" "bar"}] (:body response)))))
+ (let [resp (handler (-> (request :get "/")
+ (content-type "application/json-patch+json; charset=UTF-8")
+ (body "[{\"op\": \"add\",\"path\":\"/foo\",\"value\": \"bar\"}]")))]
+ (is (= [{"op" "add" "path" "/foo" "value" "bar"}] (:body resp)))))
(testing "malformed json"
- (let [request {:content-type "application/json; charset=UTF-8"
- :body (string-input-stream "{\"foo\": \"bar\"")}]
- (is (= (handler request)
+ (let [resp (handler (-> (request :get "/")
+ (content-type "application/json; charset=UTF-8")
+ (body "{\"foo\": \"bar\"")))]
+ (is (= resp
{:status 400
:headers {"Content-Type" "text/plain"}
:body "Malformed JSON in request body."})))))
(let [handler (wrap-json-body identity {:keywords? true})]
(testing "keyword keys"
- (let [request {:content-type "application/json"
- :body (string-input-stream "{\"foo\": \"bar\"}")}
- response (handler request)]
- (is (= {:foo "bar"} (:body response))))))
+ (let [resp (handler (-> (request :get "/")
+ (content-type "application/json")
+ (body "{\"foo\": \"bar\"}")))]
+ (is (= {:foo "bar"} (:body resp))))))
(let [handler (wrap-json-body identity {:keywords? true :bigdecimals? true})]
(testing "bigdecimal floats"
- (let [request {:content-type "application/json"
- :body (string-input-stream "{\"foo\": 5.5}")}
- response (handler request)]
- (is (decimal? (-> response :body :foo)))
- (is (= {:foo 5.5M} (:body response))))))
+ (let [resp (handler (-> (request :get "/")
+ (content-type "application/json")
+ (body "{\"foo\": 5.5}")))]
+ (is (decimal? (-> resp :body :foo)))
+ (is (= {:foo 5.5M} (:body resp))))))
(testing "custom malformed json"
(let [malformed {:status 400
:headers {"Content-Type" "text/html"}
:body "Your JSON is wrong!"}
- handler (wrap-json-body identity {:malformed-response malformed})
- request {:content-type "application/json"
- :body (string-input-stream "{\"foo\": \"bar\"")}]
- (is (= (handler request) malformed)))))
+ handler (wrap-json-body identity {:malformed-response malformed})]
+ (is (= (handler (-> (request :get "/")
+ (content-type "application/json")
+ (body "{\"foo\": \"bar\"")))
+ malformed)))))
(deftest test-json-params
- (let [handler (wrap-json-params identity)]
+ (let [handler (-> identity
+ (wrap-defaults api-defaults)
+ (wrap-json-params {:bigdecimals? true}))]
(testing "xml body"
- (let [request {:content-type "application/xml"
- :body (string-input-stream "")
- :params {"id" 3}}
- response (handler request)]
- (is (= "") (slurp (:body response)))
- (is (= {"id" 3} (:params response)))
- (is (nil? (:json-params response)))))
+ (let [resp (handler (-> (request :get "/" {"id" 3})
+ (content-type "application/xml")
+ (body "")))]
+ (is (= "") (:body resp))
+ (is (= {:id "3"} (:params resp)))
+ (is (nil? (:json-params resp)))))
(testing "json body"
- (let [request {:content-type "application/json; charset=UTF-8"
- :body (string-input-stream "{\"foo\": \"bar\"}")
- :params {"id" 3}}
- response (handler request)]
- (is (= {"id" 3, "foo" "bar"} (:params response)))
- (is (= {"foo" "bar"} (:json-params response)))))
+ (let [resp (handler (-> (request :get "/" {"id" 3})
+ (content-type "application/json; charset=UTF-8")
+ (body "{\"foo\": \"bar\"}")))]
+ (is (= {:id "3", :foo "bar"} (:params resp)))
+ (is (= {"foo" "bar"} (:json-params resp)))))
(testing "json body with bigdecimals"
- (let [handler (wrap-json-params identity {:bigdecimals? true})
- request {:content-type "application/json; charset=UTF-8"
- :body (string-input-stream "{\"foo\": 5.5}")
- :params {"id" 3}}
- response (handler request)]
- (is (decimal? (get-in response [:params "foo"])))
- (is (decimal? (get-in response [:json-params "foo"])))
- (is (= {"id" 3, "foo" 5.5M} (:params response)))
- (is (= {"foo" 5.5M} (:json-params response)))))
+ (let [resp (handler (-> (request :get "/" {"id" 3})
+ (content-type "application/json; charset=UTF-8")
+ (body "{\"foo\": 5.5}")))]
+ (is (decimal? (get-in resp [:params :foo])))
+ (is (decimal? (get-in resp [:json-params "foo"])))
+ (is (= {:id "3" :foo 5.5M} (:params resp)))
+ (is (= {"foo" 5.5M} (:json-params resp)))))
(testing "custom json body"
- (let [request {:content-type "application/vnd.foobar+json; charset=UTF-8"
- :body (string-input-stream "{\"foo\": \"bar\"}")
- :params {"id" 3}}
- response (handler request)]
- (is (= {"id" 3, "foo" "bar"} (:params response)))
- (is (= {"foo" "bar"} (:json-params response)))))
+ (let [resp (handler (-> (request :get "/" {"id" 3})
+ (content-type "application/vnd.foobar+json; charset=UTF-8")
+ (body "{\"foo\": \"bar\"}")))]
+ (is (= {:id "3", :foo "bar"} (:params resp)))
+ (is (= {"foo" "bar"} (:json-params resp)))))
(testing "json schema body"
- (let [request {:content-type "application/schema+json; charset=UTF-8"
- :body (string-input-stream "{\"type\": \"schema\",\"properties\":{}}")
- :params {"id" 3}}
- response (handler request)]
- (is (= {"id" 3, "type" "schema", "properties" {}} (:params response)))
- (is (= {"type" "schema", "properties" {}} (:json-params response)))))
+ (let [resp (handler (-> (request :get "/" {"id" 3})
+ (content-type "application/schema+json; charset=UTF-8")
+ (body "{\"type\": \"schema\",\"properties\":{}}")))]
+ (is (= {:id "3", :type "schema", :properties {}} (:params resp)))
+ (is (= {"type" "schema", "properties" {}} (:json-params resp)))))
(testing "array json body"
- (let [request {:content-type "application/vnd.foobar+json; charset=UTF-8"
- :body (string-input-stream "[\"foo\"]")
- :params {"id" 3}}
- response (handler request)]
- (is (= {"id" 3} (:params response)))))
+ (let [resp (handler (-> (request :get "/" {"id" 3})
+ (content-type "application/vnd.foobar+json; charset=UTF-8")
+ (body "[\"foo\"]")))]
+ (is (= {:id "3"} (:params resp)))))
(testing "malformed json"
- (let [request {:content-type "application/json; charset=UTF-8"
- :body (string-input-stream "{\"foo\": \"bar\"")}]
- (is (= (handler request)
+ (let [resp (handler (-> (request :get "/" {"id" 3})
+ (content-type "application/vnd.foobar+json; charset=UTF-8")
+ (body "{\"foo\": \"bar\"")))]
+ (is (= resp
{:status 400
:headers {"Content-Type" "text/plain"}
:body "Malformed JSON in request body."})))))
@@ -128,50 +128,51 @@
:headers {"Content-Type" "text/html"}
:body "Your JSON is wrong!"}
handler (wrap-json-params identity {:malformed-response malformed})
- request {:content-type "application/json"
- :body (string-input-stream "{\"foo\": \"bar\"")}]
- (is (= (handler request) malformed)))))
+ resp (handler (-> (request :get "/" {"id" 3})
+ (content-type "application/vnd.foobar+json; charset=UTF-8")
+ (body "{\"foo\": \"bar\"")))]
+ (is (= resp malformed)))))
(deftest test-json-response
(testing "map body"
- (let [handler (constantly {:status 200 :headers {} :body {:foo "bar"}})
- response ((wrap-json-response handler) {})]
- (is (= (get-in response [:headers "Content-Type"]) "application/json; charset=utf-8"))
- (is (= (:body response) "{\"foo\":\"bar\"}"))))
+ (let [handler (constantly (response {"foo" "bar"}))
+ resp ((wrap-json-response handler) {})]
+ (is (= (get-in resp [:headers "Content-Type"]) "application/json; charset=utf-8"))
+ (is (= (:body resp) "{\"foo\":\"bar\"}"))))
(testing "string body"
- (let [handler (constantly {:status 200 :headers {} :body "foobar"})
- response ((wrap-json-response handler) {})]
- (is (= (:headers response) {}))
- (is (= (:body response) "foobar"))))
+ (let [handler (constantly (response "foobar"))
+ resp ((wrap-json-response handler) {})]
+ (is (= (:headers resp) {}))
+ (is (= (:body resp) "foobar"))))
(testing "vector body"
- (let [handler (constantly {:status 200 :headers {} :body [:foo :bar]})
- response ((wrap-json-response handler) {})]
- (is (= (get-in response [:headers "Content-Type"]) "application/json; charset=utf-8"))
- (is (= (:body response) "[\"foo\",\"bar\"]"))))
+ (let [handler (constantly (response [:foo :bar]))
+ resp ((wrap-json-response handler) {})]
+ (is (= (get-in resp [:headers "Content-Type"]) "application/json; charset=utf-8"))
+ (is (= (:body resp) "[\"foo\",\"bar\"]"))))
(testing "list body"
- (let [handler (constantly {:status 200 :headers {} :body '(:foo :bar)})
- response ((wrap-json-response handler) {})]
- (is (= (get-in response [:headers "Content-Type"]) "application/json; charset=utf-8"))
- (is (= (:body response) "[\"foo\",\"bar\"]"))))
+ (let [handler (constantly (response '(:foo :bar)))
+ resp ((wrap-json-response handler) {})]
+ (is (= (get-in resp [:headers "Content-Type"]) "application/json; charset=utf-8"))
+ (is (= (:body resp) "[\"foo\",\"bar\"]"))))
(testing "set body"
- (let [handler (constantly {:status 200 :headers {} :body #{:foo :bar}})
- response ((wrap-json-response handler) {})]
- (is (= (get-in response [:headers "Content-Type"]) "application/json; charset=utf-8"))
- (is (or (= (:body response) "[\"foo\",\"bar\"]")
- (= (:body response) "[\"bar\",\"foo\"]")))))
+ (let [handler (constantly (response #{:foo :bar}))
+ resp ((wrap-json-response handler) {})]
+ (is (= (get-in resp [:headers "Content-Type"]) "application/json; charset=utf-8"))
+ (is (or (= (:body resp) "[\"foo\",\"bar\"]")
+ (= (:body resp) "[\"bar\",\"foo\"]")))))
(testing "JSON options"
- (let [handler (constantly {:status 200 :headers {} :body {:foo "bar" :baz "quz"}})
- response ((wrap-json-response handler {:pretty true}) {})]
- (is (or (= (:body response) "{\n \"foo\" : \"bar\",\n \"baz\" : \"quz\"\n}")
- (= (:body response) "{\n \"baz\" : \"quz\",\n \"foo\" : \"bar\"\n}")))))
+ (let [handler (constantly (response {:foo "bar" :baz "quz"}))
+ resp ((wrap-json-response handler {:pretty true}) {})]
+ (is (or (= (:body resp) "{\n \"foo\" : \"bar\",\n \"baz\" : \"quz\"\n}")
+ (= (:body resp) "{\n \"baz\" : \"quz\",\n \"foo\" : \"bar\"\n}")))))
(testing "don’t overwrite Content-Type if already set"
- (let [handler (constantly {:status 200 :headers {"Content-Type" "application/json; some-param=some-value"} :body {:foo "bar"}})
- response ((wrap-json-response handler) {})]
- (is (= (get-in response [:headers "Content-Type"]) "application/json; some-param=some-value"))
- (is (= (:body response) "{\"foo\":\"bar\"}")))))
+ (let [handler (constantly (header (response {:foo "bar"}) "Content-Type" "application/json; some-param=some-value"))
+ resp ((wrap-json-response handler) {})]
+ (is (= (get-in resp [:headers "Content-Type"]) "application/json; some-param=some-value"))
+ (is (= (:body resp) "{\"foo\":\"bar\"}")))))
\ No newline at end of file