Skip to content

Commit 05b02ff

Browse files
committed
Fix wrap-content-length when given nil response
1 parent 31cbb9c commit 05b02ff

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

ring-core/src/ring/middleware/content_length.clj

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,12 @@
3333
"Adds a Content-Length header to the response. See: wrap-content-length."
3434
{:added "1.15"}
3535
[response]
36-
(if (resp/get-header response "content-length")
37-
response
38-
(if-let [size (body-size-in-bytes (:body response) response)]
39-
(-> response (resp/header "Content-Length" (str size)))
40-
response)))
36+
(when response
37+
(if (resp/get-header response "content-length")
38+
response
39+
(if-let [size (body-size-in-bytes (:body response) response)]
40+
(-> response (resp/header "Content-Length" (str size)))
41+
response))))
4142

4243
(defn wrap-content-length
4344
"Middleware that adds a Content-Length header to the response, if the

ring-core/test/ring/middleware/test/content_length.clj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@
2828
:headers {}
2929
:body (java.io.File. "test/ring/assets/plain.txt")})
3030
(get-in [:headers "Content-Length"])))))
31-
(testing "nil"
31+
(testing "nil body"
3232
(is (= "0" (-> (content-length-response
3333
{:status 200, :headers {}, :body nil})
3434
(get-in [:headers "Content-Length"])))))
35+
(testing "nil response"
36+
(is (nil? (content-length-response nil))))
3537
(testing "other data"
3638
(is (nil? (-> (content-length-response
3739
{:status 200

0 commit comments

Comments
 (0)