diff --git a/src/clojars/web.clj b/src/clojars/web.clj index cb5300e1..99999ba4 100644 --- a/src/clojars/web.clj +++ b/src/clojars/web.clj @@ -2,6 +2,7 @@ (:require [cemerick.friend :as friend] [cemerick.friend.workflows :as workflows] + [cheshire.core :as json] [clojars.auth :as auth :refer [try-account]] [clojars.config :refer [config]] [clojars.errors :refer [wrap-exceptions]] @@ -32,7 +33,8 @@ [ring.middleware.content-type :refer [wrap-content-type]] [ring.middleware.defaults :as ring-defaults] [ring.middleware.flash :refer [wrap-flash]] - [ring.middleware.not-modified :refer [wrap-not-modified]])) + [ring.middleware.not-modified :refer [wrap-not-modified]] + [ring.util.response :refer [bad-request content-type]])) (defn try-parse-page "Will throw a targeted error if maybe-page doesn't parse as an integer." @@ -127,8 +129,10 @@ (let [explanation (pr-str (m/explain params-schema params))] (log/warn {:tag :invalid-params :explanation explanation}) - {:status 400 - :body explanation})))) + (-> {:explanation explanation} + (json/encode) + (bad-request) + (content-type "application/json")))))) (defn clojars-app [{:as system diff --git a/test/clojars/integration/web_test.clj b/test/clojars/integration/web_test.clj index e61990aa..1338cec4 100644 --- a/test/clojars/integration/web_test.clj +++ b/test/clojars/integration/web_test.clj @@ -1,5 +1,6 @@ (ns clojars.integration.web-test (:require + [cheshire.core :as json] [clj-http.client :as http] [clojars.db :as db] [clojars.errors :as errors] @@ -10,6 +11,7 @@ [kerodon.core :refer [fill-in follow-redirect follow press session visit within]] [kerodon.test :refer [has text?]] + [matcher-combinators.matchers :as m] [matcher-combinators.test] [net.cgrand.enlive-html :as enlive])) @@ -46,12 +48,18 @@ (deftest invalid-params-are-rejected (is (match? {:status 400 - :body "{:schema [:map-of :keyword :string], :value {:q {:a \"b\"}}, :errors ({:path [1], :in [:q], :schema :string, :value {:a \"b\"}})}"} + :body + (m/via json/decode + {"explanation" + "{:schema [:map-of :keyword :string], :value {:q {:a \"b\"}}, :errors ({:path [1], :in [:q], :schema :string, :value {:a \"b\"}})}"})} (search-request "q[a]=b"))) (is (match? {:status 400 - :body "{:schema [:map-of :keyword :string], :value {:q [\"b\"]}, :errors ({:path [1], :in [:q], :schema :string, :value [\"b\"]})}"} + :body + (m/via json/decode + {"explanation" + "{:schema [:map-of :keyword :string], :value {:q [\"b\"]}, :errors ({:path [1], :in [:q], :schema :string, :value [\"b\"]})}"})} (search-request "q[]=b")))) (deftest browse-page-renders-multiple-pages