Skip to content

Commit

Permalink
Check first byte before gunzip (#549)
Browse files Browse the repository at this point in the history
This is a regression in the :integration suite. The tests have broken since #521.
  • Loading branch information
rymndhng committed Jul 29, 2020
1 parent 244f3b6 commit ff7dacb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/clj_http/util.clj
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@
(when b
(cond
(instance? InputStream b)
(GZIPInputStream. b)
(let [^PushbackInputStream b (PushbackInputStream. b)
first-byte (int (try (.read b) (catch EOFException _ -1)))]
(case first-byte
-1 b
(do (.unread b first-byte)
(GZIPInputStream. b))))
:else
(IOUtils/toByteArray (GZIPInputStream. (ByteArrayInputStream. b))))))

Expand Down
9 changes: 9 additions & 0 deletions test/clj_http/test/util_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,12 @@
;; coerce to seq to force byte-by-byte comparison
(is (= (seq (IOUtils/toByteArray (io/input-stream jpg-path)))
(seq (force-byte-array (io/input-stream jpg-path))))))))

(deftest test-gunzip
(testing "with input streams"
(testing "with empty stream, does not apply gunzip stream"
(is (= "" (slurp (gunzip (force-stream (byte-array 0)))))))
(testing "with non-empty stream, gunzip decompresses data"
(let [data "hello world"]
(is (= data
(slurp (gunzip (force-stream (gzip (.getBytes data)))))))))))

0 comments on commit ff7dacb

Please sign in to comment.