From 0975906765e6145d09745016143f7c18d2e440fc Mon Sep 17 00:00:00 2001 From: Mark McGranaghan Date: Sat, 7 Aug 2010 11:27:05 -0700 Subject: [PATCH] test and fix input coercion --- src/clj_http/client.clj | 2 +- test/clj_http/client_test.clj | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/clj_http/client.clj b/src/clj_http/client.clj index 6611c912..8e9b97f1 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 (.toString body "UTF-8") + (client (-> req (assoc :body (.getBytes body "UTF-8") :character-encoding "UTF-8"))) (client req)))) diff --git a/test/clj_http/client_test.clj b/test/clj_http/client_test.clj index 6485926a..3976834f 100644 --- a/test/clj_http/client_test.clj +++ b/test/clj_http/client_test.clj @@ -1,7 +1,8 @@ (ns clj-http.client-test (:use clojure.test) (:require [clj-http.client :as client]) - (:require [clj-http.util :as util])) + (:require [clj-http.util :as util]) + (:import (java.util Arrays))) (def base-req {:scheme "http" @@ -113,6 +114,17 @@ {:uri "/foo"})) +(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))))) + +(deftest pass-on-no-input-coercion + (is-passed client/wrap-input-coercion + {:body (.getBytes "foo" "UTF-8")})) + + (deftest apply-on-content-type (is-applied client/wrap-content-type {:content-type :json}