Skip to content

Commit 4aff0bc

Browse files
committed
test decompression
1 parent 804221e commit 4aff0bc

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

src/clj_http/util.clj

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
"Helper functions for the HTTP client."
33
(:import (java.net URLEncoder))
44
(:import (org.apache.commons.codec.binary Base64))
5-
(:import (java.io ByteArrayInputStream))
6-
(:import (java.util.zip InflaterInputStream GZIPInputStream))
5+
(:import (java.io ByteArrayInputStream ByteArrayOutputStream))
6+
(:import (java.util.zip InflaterInputStream DeflaterInputStream
7+
GZIPInputStream GZIPOutputStream))
78
(:import (org.apache.commons.io IOUtils)))
89

910
(defn url-encode
@@ -21,7 +22,21 @@
2122
[b]
2223
(IOUtils/toByteArray (GZIPInputStream. (ByteArrayInputStream. b))))
2324

25+
(defn gzip
26+
"Returns a gzip'd version of the given byte array."
27+
[b]
28+
(let [baos (ByteArrayOutputStream.)
29+
gos (GZIPOutputStream. baos)]
30+
(IOUtils/copy (ByteArrayInputStream. b) gos)
31+
(.close gos)
32+
(.toByteArray baos)))
33+
2434
(defn inflate
2535
"Returns a zlib inflate'd version of the given byte array."
2636
[b]
2737
(IOUtils/toByteArray (InflaterInputStream. (ByteArrayInputStream. b))))
38+
39+
(defn deflate
40+
"Returns a deflate'd version of the given byte array."
41+
[b]
42+
(IOUtils/toByteArray (DeflaterInputStream. (ByteArrayInputStream. b))))

test/clj_http/client_test.clj

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
(ns clj-http.client-test
22
(:use clojure.test)
3-
(:require [clj-http.client :as client]))
3+
(:require [clj-http.client :as client])
4+
(:require [clj-http.util :as util]))
45

56
(def base-req
67
{:scheme "http"
@@ -72,6 +73,26 @@
7273
(is (= 200 (:status resp)))))
7374

7475

76+
(deftest apply-on-compressed
77+
(let [client (fn [req] {:body (util/gzip (.getBytes "foofoofoo" "UTF-8"))
78+
:headers {"Content-Encoding" "gzip"}})
79+
c-client (client/wrap-decompression client)
80+
resp (c-client {})]
81+
(is (= "foofoofoo" (String. (:body resp) "UTF-8")))))
82+
83+
(deftest apply-on-deflated
84+
(let [client (fn [req] {:body (util/deflate (.getBytes "barbarbar" "UTF-8"))
85+
:headers {"Content-Encoding" "deflate"}})
86+
c-client (client/wrap-decompression client)
87+
resp (c-client {})]
88+
(is (= "barbarbar" (String. (:body resp) "UTF-8")))))
89+
90+
(deftest pass-on-non-compressed
91+
(let [c-client (client/wrap-decompression (fn [req] {:body "foo"}))
92+
resp (c-client {:uri "/foo"})]
93+
(is (= "foo" (:body resp)))))
94+
95+
7596
(deftest apply-on-accept
7697
(is-applied client/wrap-accept
7798
{:accept :json}

0 commit comments

Comments
 (0)