File tree Expand file tree Collapse file tree 2 files changed +39
-3
lines changed Expand file tree Collapse file tree 2 files changed +39
-3
lines changed Original file line number Diff line number Diff line change 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
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))))
Original file line number Diff line number Diff line change 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"
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 }
You can’t perform that action at this time.
0 commit comments