test: improve per-content-type coercion tests

- The :headers "Content-Type" case in per-content-type-test was
  unrealistic. Ring would've thrown an exception at the non-string
  :body.
- Test response Content-Type in muuntaja-per-content-type-coercion-test
This commit is contained in:
Joel Kaasinen 2025-10-31 09:38:56 +02:00
parent ae52000b29
commit 55f8d98bde
No known key found for this signature in database

View file

@ -705,13 +705,6 @@
:uri "/foo" :uri "/foo"
:muuntaja/request {:format request-format} :muuntaja/request {:format request-format}
:body-params body})] :body-params body})]
(testing "via :headers \"Content-Type\""
(is (= {:status 200 :body {:request "json" :response "json"} :headers {"Content-Type" "application/json"}}
(normalize-json (call (request "application/json" {:request :json :response :json} {:status 200 :body {:request :json :response :json} :headers {"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} :headers {"Content-Type" "application/json"}})))
"invalid reponse"))
(testing "via :muuntaja/content-type" (testing "via :muuntaja/content-type"
(is (= {:status 200 :body {:request "json" :response "json"} :muuntaja/content-type "application/json"} (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"})))) (normalize-json (call (request "application/json" {:request :json :response :json} {:status 200 :body {:request :json :response :json} :muuntaja/content-type "application/json"}))))
@ -870,65 +863,72 @@
(try (try
(-> (merge {:request-method :post :uri "/foo"} request) (-> (merge {:request-method :post :uri "/foo"} request)
(update :body #(ByteArrayInputStream. (.getBytes % "UTF-8"))) (update :body #(ByteArrayInputStream. (.getBytes % "UTF-8")))
(app) :body (maybe-slurp)) (app))
(catch ExceptionInfo e (catch ExceptionInfo e
#_(ex-data e) #_(ex-data e)
(select-keys (ex-data e) [:in :type])))) (select-keys (ex-data e) [:in :type]))))
read-json #(j/read-value % (j/object-mapper {:decode-key-fn true}))] 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" (testing "response content-type defaults to json"
(is (= {:response "json"} (is (json-response?
(read-json (call {:headers {"content-type" "application/json"}
(call {:headers {"content-type" "application/json"} :body (j/write-value-as-string {:request :json})}
:body (j/write-value-as-string {:request :json})} {:status 200
{:status 200 :body {:response :json}})))
:body {:response :json}})))) (is (json-response?
(is (= {:response "json"} (call {:headers {"content-type" "application/edn"}
(read-json :body (pr-str {:request :edn})}
(call {:headers {"content-type" "application/edn"} {:status 200
:body (pr-str {:request :edn})} :body {:response :json}})))
{:status 200
:body {:response :json}}))))
(is (= {:in [:response :body] :type :reitit.coercion/response-coercion} (is (= {:in [:response :body] :type :reitit.coercion/response-coercion}
(call {:headers {"content-type" "application/json"} (call {:headers {"content-type" "application/json"}
:body (j/write-value-as-string {:request :json})} :body (j/write-value-as-string {:request :json})}
{:status 200 {:status 200
:body {:response :invalid}})))) :body {:response :invalid}}))
"invalid response"))
(testing "response content-type negotiated via accept header" (testing "response content-type negotiated via accept header"
(is (= {:response "json"} (is (json-response?
(read-json (call {:headers {"content-type" "application/json" "accept" "application/json"}
(call {:headers {"content-type" "application/json" "accept" "application/json"} :body (j/write-value-as-string {:request :json})}
:body (j/write-value-as-string {:request :json})} {:status 200
{:status 200 :body {:response :json}})))
:body {:response :json}})))) (is (edn-response?
(is (= {:response :edn} (call {:headers {"content-type" "application/json" "accept" "application/edn"}
(read-string :body (j/write-value-as-string {:request :json})}
(call {:headers {"content-type" "application/json" "accept" "application/edn"} {:status 200
:body (j/write-value-as-string {:request :json})} :body {:response :edn}})))
{:status 200
:body {:response :edn}}))))
(is (= {:in [:response :body] :type :reitit.coercion/response-coercion} (is (= {:in [:response :body] :type :reitit.coercion/response-coercion}
(call {:headers {"content-type" "application/json" "accept" "application/edn"} (call {:headers {"content-type" "application/json" "accept" "application/edn"}
:body (j/write-value-as-string {:request :json})} :body (j/write-value-as-string {:request :json})}
{:status 200 {:status 200
:body {:response :invalid}})))) :body {:response :invalid}}))
"invalid response"))
(testing "response content-type set via :muuntaja/content-type" (testing "response content-type set via :muuntaja/content-type"
(is (= {:response :edn} (is (edn-response?
(read-string (call {:headers {"content-type" "application/json" "accept" "application/json"}
(call {:headers {"content-type" "application/json" "accept" "application/json"} :body (j/write-value-as-string {:request :json})}
:body (j/write-value-as-string {:request :json})} {:status 200
{:status 200 :muuntaja/content-type "application/edn"
:muuntaja/content-type "application/edn" :body {:response :edn}})))
:body {:response :edn}}))))
(is (= {:in [:response :body] :type :reitit.coercion/response-coercion} (is (= {:in [:response :body] :type :reitit.coercion/response-coercion}
(call {:headers {"content-type" "application/json" "accept" "application/json"} (call {:headers {"content-type" "application/json" "accept" "application/json"}
:body (j/write-value-as-string {:request :json})} :body (j/write-value-as-string {:request :json})}
{:status 200 {:status 200
:muuntaja/content-type "application/edn" :muuntaja/content-type "application/edn"
:body {:response :invalid}})))) :body {:response :invalid}}))
"invalid response"))
(testing "response content-type set via Content-Type header. muuntaja disabled for response." (testing "response content-type set via Content-Type header. muuntaja disabled for response."
(is (= "custom data" (is (custom-response?
(call {:headers {"content-type" "application/json" "accept" "application/json"} (call {:headers {"content-type" "application/json" "accept" "application/json"}
:body (j/write-value-as-string {:request :json})} :body (j/write-value-as-string {:request :json})}
{:status 200 {:status 200
:headers {"Content-Type" "application/custom"} :headers {"Content-Type" "application/custom"}
:body "custom data"}))))))) :body "custom data"})))))))