diff --git a/ring-core/src/ring/middleware/not_modified.clj b/ring-core/src/ring/middleware/not_modified.clj index 99a1d09e..ed2a2bfb 100644 --- a/ring-core/src/ring/middleware/not_modified.clj +++ b/ring-core/src/ring/middleware/not_modified.clj @@ -2,7 +2,7 @@ "Middleware that returns a 304 Not Modified response for responses with Last-Modified headers." (:require [ring.util.time :refer [parse-date]] - [ring.util.response :refer [get-header header]] + [ring.util.response :refer [get-header find-header header]] [ring.util.io :refer [close!]])) (defn- etag-match? [request response] @@ -35,6 +35,11 @@ (or (not-modified-since? request response) (etag-match? request response))))) +(defn- dissoc-header [response header] + (if-some [[k _] (find-header response header)] + (update response :headers dissoc k) + response)) + (defn not-modified-response "Returns 304 or original response based on response and request. See: wrap-not-modified." @@ -46,7 +51,7 @@ (do (close! (:body response)) (-> response (assoc :status 304) - (header "Content-Length" 0) + (dissoc-header "Content-Length") (assoc :body nil))) response)) diff --git a/ring-core/test/ring/middleware/test/not_modified.clj b/ring-core/test/ring/middleware/test/not_modified.clj index 6e3576c7..2d1e30f6 100644 --- a/ring-core/test/ring/middleware/test/not_modified.clj +++ b/ring-core/test/ring/middleware/test/not_modified.clj @@ -71,8 +71,9 @@ last-modified "Sun, 23 Sep 2012 11:00:00 GMT" h-resp {:status 200 :headers {"Last-Modified" last-modified} :body ""} resp (not-modified-response h-resp (req last-modified))] + (is (= 304 (:status resp))) (is (nil? (:body resp))) - (is (= (get-in resp [:headers "Content-Length"]) "0")))) + (is (nil? (get-in resp [:headers "Content-Length"]))))) (testing "no modification info" (let [response {:status 200 :headers {} :body ""}] @@ -111,4 +112,16 @@ 200 304 302 302 404 404 - 500 500)))) + 500 500))) + + (testing "content-type header is removed" + (let [req #(hash-map :request-method :get :headers {"if-modified-since" %}) + last-modified "Sun, 23 Sep 2012 11:00:00 GMT" + h-resp {:status 200 + :headers {"Last-Modified" last-modified + "Content-Length" "11"} + :body "hello world"} + resp (not-modified-response h-resp (req last-modified))] + (is (= 304 (:status resp))) + (is (nil? (:body resp))) + (is (nil? (get-in resp [:headers "Content-Length"]))))))