diff --git a/src/clj_http/client.clj b/src/clj_http/client.clj index 8e9b97f1..7915a0f3 100644 --- a/src/clj_http/client.clj +++ b/src/clj_http/client.clj @@ -75,7 +75,7 @@ (defn wrap-input-coercion [client] (fn [{:keys [body] :as req}] (if (string? body) - (client (-> req (assoc :body (.getBytes body "UTF-8") + (client (-> req (assoc :body (util/utf8-bytes body) :character-encoding "UTF-8"))) (client req)))) @@ -131,7 +131,7 @@ (defn basic-auth-value [user password] (str "Basic " - (util/base64-encode (.getBytes (str user ":" password) "UTF-8")))) + (util/base64-encode (util/utf8-bytes (str user ":" password))))) (defn wrap-basic-auth [client] (fn [req] diff --git a/src/clj_http/util.clj b/src/clj_http/util.clj index 2e77e7d5..dbf60f27 100644 --- a/src/clj_http/util.clj +++ b/src/clj_http/util.clj @@ -7,6 +7,12 @@ GZIPInputStream GZIPOutputStream)) (:import (org.apache.commons.io IOUtils))) +(defn utf8-bytes [#^String s] + (.getBytes s "UTF-8")) + +(defn utf8-string [b] + (String. b "UTF-8")) + (defn url-encode "Returns an UTF-8 URL encoded version of the given string." [unencoded] @@ -15,7 +21,7 @@ (defn base64-encode "Encode an array of bytes into a base64 encoded string." [unencoded] - (String. (Base64/encodeBase64 unencoded))) + (utf8-string (Base64/encodeBase64 unencoded))) (defn gunzip "Returns a gunzip'd version of the given byte array." diff --git a/test/clj_http/client_test.clj b/test/clj_http/client_test.clj index e1f2f73d..5915cc04 100644 --- a/test/clj_http/client_test.clj +++ b/test/clj_http/client_test.clj @@ -75,18 +75,18 @@ (deftest apply-on-compressed - (let [client (fn [req] {:body (util/gzip (.getBytes "foofoofoo" "UTF-8")) + (let [client (fn [req] {:body (util/gzip (util/utf8-bytes "foofoofoo")) :headers {"Content-Encoding" "gzip"}}) c-client (client/wrap-decompression client) resp (c-client {})] - (is (= "foofoofoo" (String. (:body resp) "UTF-8"))))) + (is (= "foofoofoo" (util/utf8-string (:body resp)))))) (deftest apply-on-deflated - (let [client (fn [req] {:body (util/deflate (.getBytes "barbarbar" "UTF-8")) + (let [client (fn [req] {:body (util/deflate (util/utf8-bytes "barbarbar")) :headers {"Content-Encoding" "deflate"}}) c-client (client/wrap-decompression client) resp (c-client {})] - (is (= "barbarbar" (String. (:body resp) "UTF-8"))))) + (is (= "barbarbar" (util/utf8-string (:body resp)))))) (deftest pass-on-non-compressed (let [c-client (client/wrap-decompression (fn [req] {:body "foo"})) @@ -115,6 +115,12 @@ (deftest apply-on-output-coercion + (let [client (fn [req] {:body (util/utf8-bytes "foo")}) + o-client (client/wrap-output-coercion client) + resp (o-client {:uri "/foo"})] + (is (= "foo" (:body resp))))) + +(deftest pass-on-no-output-coercion (let [client (fn [req] {:body nil}) o-client (client/wrap-output-coercion client) resp (o-client {:uri "/foo"})] @@ -124,22 +130,16 @@ resp (o-client {:uri "/foo" :as :bytes})] (is (= :thebytes (:body resp))))) -(deftest pass-on-no-output-coercion - (let [client (fn [req] {:body (.getBytes "foo" "UTF-8")}) - o-client (client/wrap-output-coercion client) - resp (o-client {:uri "/foo"})] - (is (= "foo" (:body resp))))) - (deftest apply-on-input-coercion (let [i-client (client/wrap-input-coercion identity) resp (i-client {:body "foo"})] (is (= "UTF-8" (:character-encoding resp))) - (is (Arrays/equals (.getBytes "foo" "UTF-8") (:body resp))))) + (is (Arrays/equals (util/utf8-bytes "foo") (:body resp))))) (deftest pass-on-no-input-coercion (is-passed client/wrap-input-coercion - {:body (.getBytes "foo" "UTF-8")})) + {:body (util/utf8-bytes "foo")})) (deftest apply-on-content-type diff --git a/test/clj_http/core_test.clj b/test/clj_http/core_test.clj index 3426b9f2..2e77f0c1 100644 --- a/test/clj_http/core_test.clj +++ b/test/clj_http/core_test.clj @@ -2,7 +2,8 @@ (:use clojure.test) (:require [clojure.contrib.pprint :as pp]) (:require [clojure.contrib.io :as io]) - (:require [clj-http.core :as core])) + (:require [clj-http.core :as core]) + (:require [clj-http.util :as util])) (defn handler [req] (pp/pprint req) @@ -59,7 +60,7 @@ (deftest sends-and-returns-byte-array-body (let [resp (request {:request-method :post :uri "/post" - :body (.getBytes "contents" "UTF-8")})] + :body (util/utf8-bytes "contents")})] (is (= 200 (:status resp))) (is (= "contents\n" (slurp-body resp)))))