Skip to content

Commit 0975906

Browse files
committed
test and fix input coercion
1 parent 4aff0bc commit 0975906

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/clj_http/client.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
(defn wrap-input-coercion [client]
7676
(fn [{:keys [body] :as req}]
7777
(if (string? body)
78-
(client (-> req (assoc :body (.toString body "UTF-8")
78+
(client (-> req (assoc :body (.getBytes body "UTF-8")
7979
:character-encoding "UTF-8")))
8080
(client req))))
8181

test/clj_http/client_test.clj

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
(ns clj-http.client-test
22
(:use clojure.test)
33
(:require [clj-http.client :as client])
4-
(:require [clj-http.util :as util]))
4+
(:require [clj-http.util :as util])
5+
(:import (java.util Arrays)))
56

67
(def base-req
78
{:scheme "http"
@@ -113,6 +114,17 @@
113114
{:uri "/foo"}))
114115

115116

117+
(deftest apply-on-input-coercion
118+
(let [i-client (client/wrap-input-coercion identity)
119+
resp (i-client {:body "foo"})]
120+
(is (= "UTF-8" (:character-encoding resp)))
121+
(is (Arrays/equals (.getBytes "foo" "UTF-8") (:body resp)))))
122+
123+
(deftest pass-on-no-input-coercion
124+
(is-passed client/wrap-input-coercion
125+
{:body (.getBytes "foo" "UTF-8")}))
126+
127+
116128
(deftest apply-on-content-type
117129
(is-applied client/wrap-content-type
118130
{:content-type :json}

0 commit comments

Comments
 (0)