|
29 | 29 | (defn accepting [ctype]
|
30 | 30 | {:headers {"accept-encoding" ctype}})
|
31 | 31 |
|
| 32 | +(defn set-encoding |
| 33 | + ([resp] (set-encoding resp false)) |
| 34 | + ([resp caps?] |
| 35 | + (let [content-encoding (if caps? "Content-Encoding" "content-encoding")] |
| 36 | + (assoc-in resp [:headers content-encoding] "text")))) |
| 37 | + |
32 | 38 | (deftest test-basic-gzip
|
33 | 39 | (let [resp (app (accepting "gzip"))]
|
34 | 40 | (is (= 200 (:status resp)))
|
35 | 41 | (is (= "gzip" (encoding resp)))
|
36 | 42 | (is (Arrays/equals (unzip (resp :body)) (.getBytes output)))))
|
37 | 43 |
|
38 | 44 | (deftest test-basic-gzip-async
|
39 |
| - "middleware should work with 3-arg async handlers as well" |
40 |
| - (let [app (wrap-gzip |
41 |
| - (fn [request respond raise] |
42 |
| - (respond {:status 200 |
43 |
| - :body output |
44 |
| - :headers {}}))) |
45 |
| - resp (app (accepting "gzip") identity identity)] |
46 |
| - (is (= 200 (:status resp))) |
47 |
| - (is (= "gzip" (encoding resp))) |
48 |
| - (is (Arrays/equals (unzip (resp :body)) (.getBytes output))))) |
| 45 | + (testing "middleware should work with 3-arg async handlers as well" |
| 46 | + (let [app (wrap-gzip |
| 47 | + (fn [request respond raise] |
| 48 | + (respond {:status 200 |
| 49 | + :body output |
| 50 | + :headers {}}))) |
| 51 | + resp (app (accepting "gzip") identity identity)] |
| 52 | + (is (= 200 (:status resp))) |
| 53 | + (is (= "gzip" (encoding resp))) |
| 54 | + (is (Arrays/equals (unzip (resp :body)) (.getBytes output)))))) |
49 | 55 |
|
50 | 56 | (deftest test-inputstream-gzip
|
51 | 57 | (let [app (wrap-gzip (fn [req] {:status 200
|
|
75 | 81 | (is (= seq-body (resp :body)))))))
|
76 | 82 |
|
77 | 83 | (deftest test-accepts
|
78 |
| - (doseq [ctype ["gzip" "*" "gzip,deflate" "gzip,deflate,sdch" |
79 |
| - "gzip, deflate" "gzip;q=1" "deflate,gzip" |
80 |
| - "deflate,gzip,sdch" "deflate,gzip;q=1" |
81 |
| - "deflate,gzip;q=1,sdch" |
82 |
| - "gzip;q=0.5"]] |
83 |
| - (is (= "gzip" (encoding (app (accepting ctype)))))) |
84 |
| - (doseq [ctype ["" "gzip;q=0" "deflate" "deflate,sdch" |
85 |
| - "deflate,gzip;q=0" "deflate,gzip;q=0,sdch" |
86 |
| - "gzip;q=0,deflate" "*;q=0"]] |
87 |
| - (is (nil? (encoding (app (accepting ctype))))))) |
| 84 | + (testing "appropriate requests will be zipped" |
| 85 | + (doseq [ctype ["gzip" "*" "gzip,deflate" "gzip,deflate,sdch" |
| 86 | + "gzip, deflate" "gzip;q=1" "deflate,gzip" |
| 87 | + "deflate,gzip,sdch" "deflate,gzip;q=1" |
| 88 | + "deflate,gzip;q=1,sdch" |
| 89 | + "gzip;q=0.5"]] |
| 90 | + (is (= "gzip" (encoding (app (accepting ctype))))) |
| 91 | + (is (accepts-gzip? (accepting ctype))))) |
| 92 | + (testing "requests that ask for a zip, but not the supported type of zip are not zipped" |
| 93 | + (doseq [ctype ["" "gzip;q=0" "deflate" "deflate,sdch" |
| 94 | + "deflate,gzip;q=0" "deflate,gzip;q=0,sdch" |
| 95 | + "gzip;q=0,deflate" "*;q=0"]] |
| 96 | + (is (nil? (encoding (app (accepting ctype)))))))) |
88 | 97 |
|
89 | 98 | (deftest test-min-length
|
90 |
| - "don't compress string bodies less than 200 characters long" |
91 |
| - (let [output (apply str (repeat 10 "a")) |
92 |
| - app (wrap-gzip (fn [req] {:status 200 |
93 |
| - :body output |
94 |
| - :headers {}})) |
95 |
| - resp (app (accepting "gzip"))] |
96 |
| - (is (nil? (encoding (app (accepting "gzip"))))))) |
| 99 | + (testing "Compress string bodies greater than the min-length (200) characters long" |
| 100 | + (let [output (apply str (repeat (inc min-length) "a")) |
| 101 | + resp {:status 200 |
| 102 | + :body output |
| 103 | + :headers {}} |
| 104 | + app (wrap-gzip (fn [req] resp))] |
| 105 | + (is (= "gzip" (encoding (app (accepting "gzip"))))) |
| 106 | + (is (supported-response? resp)) |
| 107 | + (testing ", but not string bodies at or below min-length" |
| 108 | + (let [resp (update resp :body subs 1) |
| 109 | + app (wrap-gzip (fn [req] resp))] |
| 110 | + (is (nil? (encoding (app (accepting "gzip"))))) |
| 111 | + (is (not (supported-response? resp)))))))) |
97 | 112 |
|
98 | 113 | (deftest test-wrapped-encoding
|
99 |
| - "don't compress responses which already have a content-encoding header" |
100 |
| - (let [app (wrap-gzip (fn [req] {:status 200 |
101 |
| - :body output |
102 |
| - :headers {"Content-Encoding" "text"}})) |
103 |
| - resp (app (accepting "gzip"))] |
104 |
| - (is (= "text" (encoding resp))) |
105 |
| - (is (= output (:body resp))))) |
| 114 | + (testing "don't compress responses which already have a content-encoding header" |
| 115 | + (let [response {:status 200 |
| 116 | + :body output |
| 117 | + :headers {"Content-Encoding" "text"}} |
| 118 | + app (wrap-gzip (fn [req] response)) |
| 119 | + resp (app (accepting "gzip"))] |
| 120 | + (is (= "text" (encoding resp))) |
| 121 | + (is (= output (:body resp)))))) |
| 122 | + |
| 123 | +(deftest test-supported |
| 124 | + (testing "responses that already have an encoding cannot be zipped" |
| 125 | + (doseq [ctype ["gzip" "*" "gzip,deflate" "gzip,deflate,sdch" |
| 126 | + "gzip, deflate" "gzip;q=1" "deflate,gzip" |
| 127 | + "deflate,gzip,sdch" "deflate,gzip;q=1" |
| 128 | + "deflate,gzip;q=1,sdch" |
| 129 | + "gzip;q=0.5" "" "gzip;q=0" "deflate" "deflate,sdch" |
| 130 | + "deflate,gzip;q=0" "deflate,gzip;q=0,sdch" |
| 131 | + "gzip;q=0,deflate" "*;q=0"]] |
| 132 | + (is (not (supported-response? (set-encoding (accepting ctype))))) |
| 133 | + (is (not (supported-response? (set-encoding (accepting ctype) true))))))) |
106 | 134 |
|
107 | 135 | (deftest test-status
|
108 |
| - "don't compress non-200 responses" |
109 |
| - (let [app (wrap-gzip (fn [req] {:status 404 |
110 |
| - :body output |
111 |
| - :headers {}})) |
112 |
| - resp (app (accepting "gzip"))] |
113 |
| - (is (nil? (encoding resp))) |
114 |
| - (is (= output (:body resp))))) |
| 136 | + (testing "don't compress non-2xx responses" |
| 137 | + (let [app (wrap-gzip (fn [req] {:status 404 |
| 138 | + :body output |
| 139 | + :headers {}})) |
| 140 | + resp (app (accepting "gzip"))] |
| 141 | + (is (nil? (encoding resp))) |
| 142 | + (is (= output (:body resp)))))) |
| 143 | + |
| 144 | +(deftest test-setting-headers |
| 145 | + (testing "updating the headers of a response to indicate that they have been gziped" |
| 146 | + (is (= {"Content-Encoding" "gzip"} (set-response-headers {"Content-Length" 201}))) |
| 147 | + (is (= {"Content-Encoding" "gzip" "Age" 24} (set-response-headers {"Age" 24}))))) |
0 commit comments