clj-http-client/test/puppetlabs/http/client/decompress_test.clj
Chris Price 186b226512 (TK-23) Port to apache HttpAsyncClient
* Ports the code over to use the Apache HttpAsyncClient library
  instead of http-kit, as we were getting some weird and inconsistent
  SSL errors from http-kit, and
* Simplify the API (by eliminating a lot of the supported configuration
  options).  Some of these options we'll eventually want to add back in,
  but for now, getting rid of them makes the API less cluttered and
  also gives us the opportunity to do a better job writing tests
  for new options when we do add support for them.

For now it only supports constructing a new client on every request.
I intend to add API for creating a client explicitly, and a protocol
for how to interact with the client.  This will allow users to
re-use an existing client as they see fit... but this hasn't been
implemented yet.
2014-07-03 08:58:46 -07:00

57 lines
No EOL
2.2 KiB
Clojure

(ns puppetlabs.http.client.decompress-test
(:import (java.io ByteArrayOutputStream ByteArrayInputStream)
(java.util.zip GZIPOutputStream DeflaterInputStream)
(org.apache.commons.io IOUtils)
(com.puppetlabs.http.client.impl JavaClient)
(java.util HashMap))
(:require [clojure.test :refer :all]
[puppetlabs.http.client.async :as async]
[schema.test :as schema-test]))
(use-fixtures :once schema-test/validate-schemas)
(def compressible-body (apply str (repeat 1000 "f")))
(defn gzip
[s]
(let [baos (ByteArrayOutputStream.)
gos (GZIPOutputStream. baos)]
(-> s
(.getBytes "UTF-8")
(ByteArrayInputStream.)
(IOUtils/copy gos))
(.close gos)
(ByteArrayInputStream. (.toByteArray baos))))
(defn deflate
[s]
(-> s
(.getBytes "UTF-8")
(ByteArrayInputStream.)
(DeflaterInputStream.)))
(deftest gzip-compress-test
(testing "clojure gzip decompression"
(let [test-response {:headers {"content-encoding" "gzip"}
:body (gzip compressible-body)}
response (async/decompress test-response)]
(is (not (contains? (:headers response) "content-encoding")))
(is (= compressible-body (slurp (:body response))))))
(testing "java gzip decompression"
(let [headers (HashMap. {"content-encoding" "gzip"})
response (JavaClient/decompress (gzip compressible-body) headers)]
(is (not (.containsKey headers "content-encoding")))
(is (= compressible-body (slurp response))))))
(deftest deflate-compress-test
(testing "clojure deflate decompression"
(let [test-response {:headers {"content-encoding" "deflate"}
:body (deflate compressible-body)}
response (async/decompress test-response)]
(is (not (contains? (:headers response) "content-encoding")))
(is (= compressible-body (slurp (:body response))))))
(testing "java gzip decompression"
(let [headers (HashMap. {"content-encoding" "deflate"})
response (JavaClient/decompress (deflate compressible-body) headers)]
(is (not (.containsKey headers "content-encoding")))
(is (= compressible-body (slurp response))))))