Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Abstracted request and response in tests #33

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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"]]}})
223 changes: 112 additions & 111 deletions test/ring/middleware/test/json.clj
Original file line number Diff line number Diff line change
@@ -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 "<xml></xml>")}
response (handler request)]
(is (= "<xml></xml>") (slurp (:body response)))))
(let [resp (handler (-> (request :get "/")
(content-type "application/xml")
(body "<xml></xml>")))]
(is (= "<xml></xml>") (: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 "<b>Your JSON is wrong!</b>"}
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 "<xml></xml>")
:params {"id" 3}}
response (handler request)]
(is (= "<xml></xml>") (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 "<xml></xml>")))]
(is (= "<xml></xml>") (: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."})))))
Expand All @@ -128,50 +128,51 @@
:headers {"Content-Type" "text/html"}
:body "<b>Your JSON is wrong!</b>"}
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\"}")))))