-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathgzip_test.clj
106 lines (92 loc) · 3.84 KB
/
gzip_test.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
(ns ring.middleware.gzip-test
(:use clojure.test
ring.middleware.gzip)
(:require [clojure.java.io :as io])
(:import (java.util Arrays))
(:import (java.io StringBufferInputStream ByteArrayOutputStream))
(:import (java.util.zip GZIPInputStream)))
(defn- to-byte-array [inputstream]
(let [buffer (ByteArrayOutputStream.)]
(io/copy inputstream buffer)
(.toByteArray buffer)))
(defn unzip [in]
(let [in (GZIPInputStream. in)
bytes (to-byte-array in)]
(.close in)
bytes))
(defn encoding [resp]
((:headers resp) "Content-Encoding"))
(def output (apply str (repeat 300 "a")))
(def app (wrap-gzip (fn [req] {:status 200
:body output
:headers {}})))
(defn accepting [ctype]
{:headers {"accept-encoding" ctype}})
(deftest test-basic-gzip
(let [resp (app (accepting "gzip"))]
(is (= 200 (:status resp)))
(is (= "gzip" (encoding resp)))
(is (Arrays/equals (unzip (resp :body)) (.getBytes output)))))
(deftest test-inputstream-gzip
(let [app (wrap-gzip (fn [req] {:status 200
:body (StringBufferInputStream. output)
:headers {}}))
resp (app (accepting "gzip"))]
(is (= 200 (:status resp)))
(is (= "gzip" (encoding resp)))
(is (Arrays/equals (unzip (resp :body)) (.getBytes output)))))
(deftest test-string-seq-gzip
(let [seq-body (->> (partition-all 20 output)
(map (partial apply str)))
app (wrap-gzip (fn [req] {:status 200
:body seq-body
:headers {}}))
resp (app (accepting "gzip"))]
(is (= 200 (:status resp)))
(if @@#'ring.middleware.gzip/flushable-gzip?
(do
(println "Running on JDK7+, testing gzipping of seq response bodies.")
(is (= "gzip" (encoding resp)))
(is (Arrays/equals (unzip (resp :body)) (.getBytes output))))
(do
(println "Running on <=JDK6, testing non-gzipping of seq response bodies.")
(is (nil? (encoding resp)))
(is (= seq-body (resp :body)))))))
(deftest test-accepts
(doseq [ctype ["gzip" "*" "gzip,deflate" "gzip,deflate,sdch"
"gzip, deflate" "gzip;q=1" "deflate,gzip"
"deflate,gzip,sdch" "deflate,gzip;q=1"
"deflate,gzip;q=1,sdch"
"gzip;q=0.5"]]
(is (= "gzip" (encoding (app (accepting ctype))))))
(doseq [ctype ["" "gzip;q=0" "deflate" "deflate,sdch"
"deflate,gzip;q=0" "deflate,gzip;q=0,sdch"
"gzip;q=0,deflate" "*;q=0"]]
(is (nil? (encoding (app (accepting ctype)))))))
(deftest test-multiple-accepts
(is (= "gzip" (encoding (app (accepting ["gzip,deflate" "deflate"])))))
(is (nil? (encoding (app (accepting ["deflate" "sdch"]))))))
(deftest test-min-length
"don't compress string bodies less than 200 characters long"
(let [output (apply str (repeat 10 "a"))
app (wrap-gzip (fn [req] {:status 200
:body output
:headers {}}))
resp (app (accepting "gzip"))]
(is (nil? (encoding (app (accepting "gzip")))))))
(deftest test-wrapped-encoding
"don't compress responses which already have a content-encoding header"
(let [app (wrap-gzip (fn [req] {:status 200
:body output
:headers {"Content-Encoding" "text"}}))
resp (app (accepting "gzip"))]
(is (= "text" (encoding resp)))
(is (= output (:body resp)))))
(deftest test-status
"don't compress non-200 responses"
(let [app (wrap-gzip (fn [req] {:status 404
:body output
:headers {}}))
resp (app (accepting "gzip"))]
(is (nil? (encoding resp)))
(is (= output (:body resp)))))