Skip to content

Commit

Permalink
introduce utf8-bytes and utf8-string helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
mmcgrana committed Aug 7, 2010
1 parent 7f4aef6 commit 68b95f1
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/clj_http/client.clj
Expand Up @@ -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))))

Expand Down Expand Up @@ -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]
Expand Down
8 changes: 7 additions & 1 deletion src/clj_http/util.clj
Expand Up @@ -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]
Expand All @@ -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."
Expand Down
24 changes: 12 additions & 12 deletions test/clj_http/client_test.clj
Expand Up @@ -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"}))
Expand Down Expand Up @@ -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"})]
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions test/clj_http/core_test.clj
Expand Up @@ -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)
Expand Down Expand Up @@ -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)))))

Expand Down

0 comments on commit 68b95f1

Please sign in to comment.