diff --git a/CHANGELOG.md b/CHANGELOG.md index 76fef901..c32d7dce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,10 @@ We use [Break Versioning][breakver]. The version numbers follow a `. opts route-request (assoc ::skip #{:body})))] (if (and crc rcs) (into crc (vec rcs)) (or crc rcs))))) -(defn extract-response-format-default [request _] - (-> request :muuntaja/response :format)) +(defn extract-response-format-default [request response] + (or (get-in response [:headers "Content-Type"]) + (:muuntaja/content-type response) + (-> request :muuntaja/response :format))) (defn -format->coercer [coercion {:keys [content body]} _opts] (->> (concat (when body diff --git a/test/cljc/reitit/ring_coercion_test.cljc b/test/cljc/reitit/ring_coercion_test.cljc index aae8d69a..f74f65b9 100644 --- a/test/cljc/reitit/ring_coercion_test.cljc +++ b/test/cljc/reitit/ring_coercion_test.cljc @@ -1,8 +1,10 @@ (ns reitit.ring-coercion-test (:require [clojure.test :refer [deftest is testing]] [malli.experimental.lite :as l] - #?@(:clj [[muuntaja.middleware] - [jsonista.core :as j]]) + #?@(:clj [[muuntaja.core] + [muuntaja.middleware] + [jsonista.core :as j] + [reitit.ring.middleware.muuntaja]]) [malli.core :as m] [malli.util :as mu] [meta-merge.core :refer [meta-merge]] @@ -585,99 +587,131 @@ #?(:clj (deftest per-content-type-test - (doseq [[coercion json-request edn-request default-request json-response edn-response default-response] - [[malli/coercion - [:map [:request [:enum :json]] [:response any?]] - [:map [:request [:enum :edn]] [:response any?]] - [:map [:request [:enum :default]] [:response any?]] - [:map [:request any?] [:response [:enum :json]]] - [:map [:request any?] [:response [:enum :edn]]] - [:map [:request any?] [:response [:enum :default]]]] - [schema/coercion - {:request (s/eq :json) :response s/Any} - {:request (s/eq :edn) :response s/Any} - {:request (s/eq :default) :response s/Any} - {:request s/Any :response (s/eq :json)} - {:request s/Any :response (s/eq :edn)} - {:request s/Any :response (s/eq :default)}] - [spec/coercion - {:request (clojure.spec.alpha/spec #{:json}) :response any?} - {:request (clojure.spec.alpha/spec #{:edn}) :response any?} - {:request (clojure.spec.alpha/spec #{:default}) :response any?} - {:request any? :response (clojure.spec.alpha/spec #{:json})} - {:request any? :response (clojure.spec.alpha/spec #{:end})} - {:request any? :response (clojure.spec.alpha/spec #{:default})}]]] - (testing (str coercion) - (doseq [{:keys [name app]} - [{:name "using top-level :body" - :app (ring/ring-handler - (ring/router - ["/foo" {:post {:request {:content {"application/json" {:schema json-request} - "application/edn" {:schema edn-request}} - :body default-request} - :responses {200 {:content {"application/json" {:schema json-response} - "application/edn" {:schema edn-response}} - :body default-response}} - :handler (fn [req] - {:status 200 - :body (-> req :parameters :request)})}}] - {:validate reitit.ring.spec/validate - :data {:middleware [rrc/coerce-request-middleware - rrc/coerce-response-middleware] - :coercion coercion}}))} - {:name "using :default content" - :app (ring/ring-handler - (ring/router - ["/foo" {:post {:request {:content {"application/json" {:schema json-request} - "application/edn" {:schema edn-request} - :default {:schema default-request}} - :body json-request} ;; not applied as :default exists - :responses {200 {:content {"application/json" {:schema json-response} - "application/edn" {:schema edn-response} - :default {:schema default-response}} - :body json-response}} ;; not applied as :default exists - :handler (fn [req] - {:status 200 - :body (-> req :parameters :request)})}}] - {:validate reitit.ring.spec/validate - :data {:middleware [rrc/coerce-request-middleware - rrc/coerce-response-middleware] - :coercion coercion}}))}]] - (testing name - (let [call (fn [request] + (let [normalize-json (fn [resp] + (update resp :body #(-> % j/write-value-as-string (j/read-value j/keyword-keys-object-mapper))))] + (doseq [[coercion json-request edn-request default-request json-response edn-response default-response] + [[malli/coercion + [:map [:request [:enum :json]] [:response any?]] + [:map [:request [:enum :edn]] [:response any?]] + [:map [:request [:enum :default]] [:response any?]] + [:map [:request any?] [:response [:enum :json]]] + [:map [:request any?] [:response [:enum :edn]]] + [:map [:request any?] [:response [:enum :default]]]] + [schema/coercion + {:request (s/eq :json) :response s/Any} + {:request (s/eq :edn) :response s/Any} + {:request (s/eq :default) :response s/Any} + {:request s/Any :response (s/eq :json)} + {:request s/Any :response (s/eq :edn)} + {:request s/Any :response (s/eq :default)}] + [spec/coercion + {:request (clojure.spec.alpha/spec #{:json}) :response any?} + {:request (clojure.spec.alpha/spec #{:edn}) :response any?} + {:request (clojure.spec.alpha/spec #{:default}) :response any?} + {:request any? :response (clojure.spec.alpha/spec #{:json})} + {:request any? :response (clojure.spec.alpha/spec #{:end})} + {:request any? :response (clojure.spec.alpha/spec #{:default})}]]] + (testing (str coercion) + (doseq [{:keys [name app]} + [{:name "using top-level :body" + :app (ring/ring-handler + (ring/router + ["/foo" {:post {:request {:content {"application/json" {:schema json-request} + "application/edn" {:schema edn-request}} + :body default-request} + :responses {200 {:content {"application/json" {:schema json-response} + "application/edn" {:schema edn-response}} + :body default-response}} + :handler (fn [req] + {:status 200 + :body (-> req :parameters :request)})}}] + {:validate reitit.ring.spec/validate + :data {:middleware [rrc/coerce-request-middleware + rrc/coerce-response-middleware] + :coercion coercion}}))} + {:name "using :default content" + :app (ring/ring-handler + (ring/router + ["/foo" {:post {:request {:content {"application/json" {:schema json-request} + "application/edn" {:schema edn-request} + :default {:schema default-request}} + :body json-request} ;; not applied as :default exists + :responses {200 {:content {"application/json" {:schema json-response} + "application/edn" {:schema edn-response} + :default {:schema default-response}} + :body json-response}} ;; not applied as :default exists + :handler (fn [req] + {:status 200 + :body (-> req :parameters :request)})}}] + {:validate reitit.ring.spec/validate + :data {:middleware [rrc/coerce-request-middleware + rrc/coerce-response-middleware] + :coercion coercion}}))}]] + (testing name + (let [call (fn [request] + (try + (app request) + (catch ExceptionInfo e + (select-keys (ex-data e) [:type :in])))) + request (fn [request-format response-format body] + {:request-method :post + :uri "/foo" + :muuntaja/request {:format request-format} + :muuntaja/response {:format response-format} + :body-params body})] + (testing "succesful call" + (is (= {:status 200 :body {:request "json", :response "json"}} + (normalize-json (call (request "application/json" "application/json" {:request :json :response :json}))))) + (is (= {:status 200 :body {:request "edn", :response "json"}} + (normalize-json (call (request "application/edn" "application/json" {:request :edn :response :json}))))) + (is (= {:status 200 :body {:request :default, :response :default}} + (call (request "application/transit" "application/transit" {:request :default :response :default}))))) + (testing "request validation fails" + (is (= {:type :reitit.coercion/request-coercion :in [:request :body-params]} + (call (request "application/edn" "application/json" {:request :json :response :json})))) + (is (= {:type :reitit.coercion/request-coercion :in [:request :body-params]} + (call (request "application/json" "application/json" {:request :edn :response :json})))) + (is (= {:type :reitit.coercion/request-coercion :in [:request :body-params]} + (call (request "application/transit" "application/json" {:request :edn :response :json}))))) + (testing "response validation fails" + (is (= {:type :reitit.coercion/response-coercion :in [:response :body]} + (call (request "application/json" "application/json" {:request :json :response :edn})))) + (is (= {:type :reitit.coercion/response-coercion :in [:response :body]} + (call (request "application/json" "application/edn" {:request :json :response :json})))) + (is (= {:type :reitit.coercion/response-coercion :in [:response :body]} + (call (request "application/json" "application/transit" {:request :json :response :json})))))))) + (testing "explicit response content type" + (let [response (atom nil) + app (ring/ring-handler + (ring/router + ["/foo" {:post {:responses {200 {:content {"application/json" {:schema json-response} + "application/edn" {:schema edn-response} + :default {:schema default-response}}}} + :handler (fn [req] + @response)}}] + {:validate reitit.ring.spec/validate + :data {:middleware [rrc/coerce-request-middleware + rrc/coerce-response-middleware] + :coercion coercion}})) + call (fn [request] (try (app request) (catch ExceptionInfo e + #_(ex-data e) (select-keys (ex-data e) [:type :in])))) - request (fn [request-format response-format body] + request (fn [request-format body resp] + (reset! response resp) {:request-method :post :uri "/foo" :muuntaja/request {:format request-format} - :muuntaja/response {:format response-format} - :body-params body}) - normalize-json (fn[body] - (-> body j/write-value-as-string (j/read-value j/keyword-keys-object-mapper)))] - (testing "succesful call" - (is (= {:status 200 :body {:request "json", :response "json"}} - (normalize-json (call (request "application/json" "application/json" {:request :json :response :json}))))) - (is (= {:status 200 :body {:request "edn", :response "json"}} - (normalize-json (call (request "application/edn" "application/json" {:request :edn :response :json}))))) - (is (= {:status 200 :body {:request :default, :response :default}} - (call (request "application/transit" "application/transit" {:request :default :response :default}))))) - (testing "request validation fails" - (is (= {:type :reitit.coercion/request-coercion :in [:request :body-params]} - (call (request "application/edn" "application/json" {:request :json :response :json})))) - (is (= {:type :reitit.coercion/request-coercion :in [:request :body-params]} - (call (request "application/json" "application/json" {:request :edn :response :json})))) - (is (= {:type :reitit.coercion/request-coercion :in [:request :body-params]} - (call (request "application/transit" "application/json" {:request :edn :response :json}))))) - (testing "response validation fails" - (is (= {:type :reitit.coercion/response-coercion :in [:response :body]} - (call (request "application/json" "application/json" {:request :json :response :edn})))) - (is (= {:type :reitit.coercion/response-coercion :in [:response :body]} - (call (request "application/json" "application/edn" {:request :json :response :json})))) - (is (= {:type :reitit.coercion/response-coercion :in [:response :body]} - (call (request "application/json" "application/transit" {:request :json :response :json})))))))))))) + :body-params body})] + (testing "via :muuntaja/content-type" + (is (= {:status 200 :body {:request "json" :response "json"} :muuntaja/content-type "application/json"} + (normalize-json (call (request "application/json" {:request :json :response :json} {:status 200 :body {:request :json :response :json} :muuntaja/content-type "application/json"})))) + "valid reponse") + (is (= {:in [:response :body] :type :reitit.coercion/response-coercion} + (call (request "application/json" {:request :json :response :json} {:status 200 :body {:request :json :response :invalid} :muuntaja/content-type "application/json"}))) + "invalid reponse"))))))))) #?(:clj @@ -801,3 +835,100 @@ (app) :body slurp (read-string))] (is (= data-edn (e2e (assoc data-edn :EXTRA "VALUE")))) (is (thrown? ExceptionInfo (e2e data-json)))))))) + +#?(:clj + (deftest muuntaja-per-content-type-coercion-test + ;; Test integration between per-content-type coercion and muuntaja. + ;; Malli-only for now. + (let [response (atom nil) + app (ring/ring-handler + (ring/router + ["/foo" {:post {:request {:content {"application/json" {:schema [:map [:request [:enum :json]]]} + "application/edn" {:schema [:map [:request [:enum :edn]]]} + :default {:schema [:map [:request [:enum :default]]]}}} + :responses {200 {:content {"application/json" {:schema [:map [:response [:enum :json]]]} + "application/edn" {:schema [:map [:response [:enum :edn]]]} + :default {}}}} + :handler (fn [req] @response)}}] + {:data {:middleware [reitit.ring.middleware.muuntaja/format-middleware + rrc/coerce-request-middleware + rrc/coerce-response-middleware] + :muuntaja muuntaja.core/instance + :coercion malli/coercion}})) + maybe-slurp #(if (instance? java.io.InputStream %) + (slurp %) + %) + call (fn [request resp] + (reset! response resp) + (try + (-> (merge {:request-method :post :uri "/foo"} request) + (update :body #(ByteArrayInputStream. (.getBytes % "UTF-8"))) + (app)) + (catch ExceptionInfo e + #_(ex-data e) + (select-keys (ex-data e) [:in :type])))) + read-json #(j/read-value % (j/object-mapper {:decode-key-fn true})) + json-response? (fn [resp] + (and (.startsWith (get-in resp [:headers "Content-Type"]) "application/json") ;; ignore the ;charset=utf-8 part + (= {:response "json"} (read-json (maybe-slurp (:body resp)))))) + edn-response? (fn [resp] + (and (.startsWith (get-in resp [:headers "Content-Type"]) "application/edn") ;; ignore the ;charset=utf-8 part + (= {:response :edn} (read-string (maybe-slurp (:body resp)))))) + custom-response? (fn [resp] + (and (= (get-in resp [:headers "Content-Type"]) "application/custom") + (= "custom data" (maybe-slurp (:body resp)))))] + (testing "response content-type defaults to json" + (is (json-response? + (call {:headers {"content-type" "application/json"} + :body (j/write-value-as-string {:request :json})} + {:status 200 + :body {:response :json}}))) + (is (json-response? + (call {:headers {"content-type" "application/edn"} + :body (pr-str {:request :edn})} + {:status 200 + :body {:response :json}}))) + (is (= {:in [:response :body] :type :reitit.coercion/response-coercion} + (call {:headers {"content-type" "application/json"} + :body (j/write-value-as-string {:request :json})} + {:status 200 + :body {:response :invalid}})) + "invalid response")) + (testing "response content-type negotiated via accept header" + (is (json-response? + (call {:headers {"content-type" "application/json" "accept" "application/json"} + :body (j/write-value-as-string {:request :json})} + {:status 200 + :body {:response :json}}))) + (is (edn-response? + (call {:headers {"content-type" "application/json" "accept" "application/edn"} + :body (j/write-value-as-string {:request :json})} + {:status 200 + :body {:response :edn}}))) + (is (= {:in [:response :body] :type :reitit.coercion/response-coercion} + (call {:headers {"content-type" "application/json" "accept" "application/edn"} + :body (j/write-value-as-string {:request :json})} + {:status 200 + :body {:response :invalid}})) + "invalid response")) + (testing "response content-type set via :muuntaja/content-type" + (is (edn-response? + (call {:headers {"content-type" "application/json" "accept" "application/json"} + :body (j/write-value-as-string {:request :json})} + {:status 200 + :muuntaja/content-type "application/edn" + :body {:response :edn}}))) + (is (= {:in [:response :body] :type :reitit.coercion/response-coercion} + (call {:headers {"content-type" "application/json" "accept" "application/json"} + :body (j/write-value-as-string {:request :json})} + {:status 200 + :muuntaja/content-type "application/edn" + :body {:response :invalid}})) + "invalid response")) + (testing "response content-type set via Content-Type header. muuntaja disabled for response." + (is (custom-response? + (call {:headers {"content-type" "application/json" "accept" "application/json"} + :body (j/write-value-as-string {:request :json})} + {:status 200 + :headers {"Content-Type" "application/custom"} + :body "custom data"})))))))