Skip to content

Commit 8f8dad8

Browse files
committed
Merge remote-tracking branch 'r0man/fix-json-auto'
2 parents b32ae50 + cadc369 commit 8f8dad8

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

src/clj_http/client.clj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -243,16 +243,16 @@
243243
(defmethod coerce-response-body :json [req resp]
244244
(coerce-json-body req resp true))
245245

246-
(defmethod coerce-response-body :json-string-keys [_ resp]
247-
(coerce-json-body resp false))
246+
(defmethod coerce-response-body :json-string-keys [req resp]
247+
(coerce-json-body req resp false))
248248

249249
(defmethod coerce-response-body :clojure [_ {:keys [body] :as resp}]
250250
(if edn-enabled?
251251
(assoc resp :body (parse-edn (String. #^"[B" body "UTF-8")))
252252
(binding [*read-eval* false]
253253
(assoc resp :body (read-string (String. #^"[B" body "UTF-8"))))))
254254

255-
(defmethod coerce-response-body :auto [_ {:keys [body coerce status] :as resp}]
255+
(defmethod coerce-response-body :auto [req {:keys [body coerce status] :as resp}]
256256
(assoc resp
257257
:body
258258
(let [typestring (get-in resp [:headers "content-type"])]
@@ -276,8 +276,8 @@
276276
json-enabled?)
277277
(if-let [charset (second (re-find #"charset=(.*)"
278278
(str typestring)))]
279-
(coerce-json-body resp true charset)
280-
(coerce-json-body resp true "UTF-8"))
279+
(:body (coerce-json-body req resp true charset))
280+
(:body (coerce-json-body req resp true "UTF-8")))
281281

282282
:else
283283
(String. #^"[B" body "UTF-8")))))

test/clj_http/test/core.clj

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
{:status 200 :body "{:foo \"bar\" :baz #=(+ 1 1)}"
3131
:headers {"content-type" "application/clojure"}}
3232
[:get "/json"]
33-
{:status 200 :body "{\"foo\":\"bar\"}"}
33+
{:status 200 :body "{\"foo\":\"bar\"}" :headers {"content-type" "application/json"}}
3434
[:get "/json-bad"]
3535
{:status 400 :body "{\"foo\":\"bar\"}"}
3636
[:get "/redirect"]
@@ -303,6 +303,7 @@
303303
(let [resp (client/get (localhost "/json") {:as :json})
304304
resp-str (client/get (localhost "/json")
305305
{:as :json :coerce :exceptional})
306+
resp-auto (client/get (localhost "/json") {:as :auto})
306307
bad-resp (client/get (localhost "/json-bad")
307308
{:throw-exceptions false :as :json})
308309
bad-resp-json (client/get (localhost "/json-bad")
@@ -311,8 +312,13 @@
311312
bad-resp-json2 (client/get (localhost "/json-bad")
312313
{:throw-exceptions false :as :json
313314
:coerce :unexceptional})]
314-
(is (= 200 (:status resp) (:status resp-str)))
315-
(is (= {:foo "bar"} (:body resp)))
315+
(is (= 200
316+
(:status resp)
317+
(:status resp-str)
318+
(:status resp-auto)))
319+
(is (= {:foo "bar"}
320+
(:body resp)
321+
(:body resp-auto)))
316322
(is (= "{\"foo\":\"bar\"}" (:body resp-str)))
317323
(is (= 400
318324
(:status bad-resp)

0 commit comments

Comments
 (0)