Skip to content

Commit

Permalink
Return invalid params response as json
Browse files Browse the repository at this point in the history
We didn't have a content-type before, so it was defaulting to
`application/octet-stream`, making browers unhappy.
  • Loading branch information
tobias committed Apr 14, 2024
1 parent a26e1ee commit 89e33a5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
10 changes: 7 additions & 3 deletions src/clojars/web.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
Expand Down Expand Up @@ -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."
Expand Down Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions test/clojars/integration/web_test.clj
Original file line number Diff line number Diff line change
@@ -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]
Expand All @@ -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]))

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 89e33a5

Please sign in to comment.