From c906d5abd046b3b082074b913ba1ec91023fa2bc Mon Sep 17 00:00:00 2001 From: Joe Pinsonault Date: Tue, 21 Apr 2015 16:50:39 -0700 Subject: [PATCH 1/3] (TK-182) coerce-body-type handles nil body --- src/clj/puppetlabs/http/client/async.clj | 4 +++- test/puppetlabs/http/client/async_test.clj | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/clj/puppetlabs/http/client/async.clj b/src/clj/puppetlabs/http/client/async.clj index eb97fb8..00fd839 100644 --- a/src/clj/puppetlabs/http/client/async.clj +++ b/src/clj/puppetlabs/http/client/async.clj @@ -199,7 +199,9 @@ (defmethod coerce-body-type :text [resp] (let [charset (or (get-in resp [:content-type-params :charset] "UTF-8"))] - (assoc resp :body (slurp (:body resp) :encoding charset)))) + (assoc resp :body (if (:body resp) + (slurp (:body resp) :encoding charset) + nil)))) (defn- response-map [opts http-response] diff --git a/test/puppetlabs/http/client/async_test.clj b/test/puppetlabs/http/client/async_test.clj index ac74cc8..75032a6 100644 --- a/test/puppetlabs/http/client/async_test.clj +++ b/test/puppetlabs/http/client/async_test.clj @@ -29,3 +29,9 @@ (let [body "foo"] (is (= (compute-content-type body "text/html") "text/html; charset=UTF-8"))))))) + +(deftest nil-response-body-coerced-as-text + (testing "a nil response body is handled correctly by async/coerce-body-type" + (let [resp {:body nil, :opts {:as :text}}] + (is (= {:body nil, :opts {:as :text}} + (coerce-body-type resp)))))) From 08ca923e69130d81554d048d7dd735726e6a78e8 Mon Sep 17 00:00:00 2001 From: Joe Pinsonault Date: Thu, 23 Apr 2015 13:56:28 -0700 Subject: [PATCH 2/3] (MAINT) nil body is coerced into empty string --- src/clj/puppetlabs/http/client/async.clj | 2 +- test/puppetlabs/http/client/async_test.clj | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/clj/puppetlabs/http/client/async.clj b/src/clj/puppetlabs/http/client/async.clj index 00fd839..f8de60d 100644 --- a/src/clj/puppetlabs/http/client/async.clj +++ b/src/clj/puppetlabs/http/client/async.clj @@ -201,7 +201,7 @@ (let [charset (or (get-in resp [:content-type-params :charset] "UTF-8"))] (assoc resp :body (if (:body resp) (slurp (:body resp) :encoding charset) - nil)))) + "")))) (defn- response-map [opts http-response] diff --git a/test/puppetlabs/http/client/async_test.clj b/test/puppetlabs/http/client/async_test.clj index 75032a6..3e7a175 100644 --- a/test/puppetlabs/http/client/async_test.clj +++ b/test/puppetlabs/http/client/async_test.clj @@ -31,7 +31,7 @@ "text/html; charset=UTF-8"))))))) (deftest nil-response-body-coerced-as-text - (testing "a nil response body is handled correctly by async/coerce-body-type" + (testing "a nil response body is coerced into a string by async/coerce-body-type" (let [resp {:body nil, :opts {:as :text}}] - (is (= {:body nil, :opts {:as :text}} + (is (= {:body "", :opts {:as :text}} (coerce-body-type resp)))))) From c5489e0e436b7760df1548eacdbc92ba542de553 Mon Sep 17 00:00:00 2001 From: Joe Pinsonault Date: Fri, 24 Apr 2015 09:54:40 -0700 Subject: [PATCH 3/3] (MAINT) Added fix/test for java coerceBodyType --- .../com/puppetlabs/http/client/impl/JavaClient.java | 11 +++++++++-- .../puppetlabs/http/client/impl/java_client_test.clj | 6 ++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/java/com/puppetlabs/http/client/impl/JavaClient.java b/src/java/com/puppetlabs/http/client/impl/JavaClient.java index 50fb926..5e59896 100644 --- a/src/java/com/puppetlabs/http/client/impl/JavaClient.java +++ b/src/java/com/puppetlabs/http/client/impl/JavaClient.java @@ -425,12 +425,19 @@ public class JavaClient { charset = contentType.getCharset().name(); } try { - response = IOUtils.toString(body, charset); + if (body == null){ + response = ""; + } + else{ + response = IOUtils.toString(body, charset); + } } catch (IOException e) { throw new HttpClientException("Unable to read body as string", e); } try { - body.close(); + if (body != null){ + body.close(); + } } catch (IOException e) { throw new HttpClientException( "Unable to close response stream", e); diff --git a/test/com/puppetlabs/http/client/impl/java_client_test.clj b/test/com/puppetlabs/http/client/impl/java_client_test.clj index 3614edb..9973529 100644 --- a/test/com/puppetlabs/http/client/impl/java_client_test.clj +++ b/test/com/puppetlabs/http/client/impl/java_client_test.clj @@ -52,3 +52,9 @@ (let [body "foo"] (is (= (compute-content-type body "text/html") "text/html; charset=UTF-8"))))))) + + +(deftest null-response-body-coerced-as-text + (testing "a null response body is coerced into a string by JavaClient.coerceBodyType" + (let [body nil] + (is (= "" (JavaClient/coerceBodyType body ResponseBodyType/TEXT nil))))))