Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added wrap-head middleware (fixes #60)

  • Loading branch information...
commit d094e7d5b26c96bcce11ae933d2fd7273626c379 1 parent 18db74b
@weavejester weavejester authored
View
14 ring-core/src/ring/middleware/head.clj
@@ -0,0 +1,14 @@
+(ns ring.middleware.head
+ "Middleware to simplify replying to HEAD requests.")
+
+(defn wrap-head
+ "Middleware that turns any HEAD request into a GET, and then sets the response
+ body to nil."
+ [handler]
+ (fn [request]
+ (if (= :head (:request-method request))
+ (-> request
+ (assoc :request-method :get)
+ (handler)
+ (assoc :body nil))
+ (handler request))))
View
16 ring-core/test/ring/middleware/test/head.clj
@@ -0,0 +1,16 @@
+(ns ring.middleware.test.head
+ (:use clojure.test
+ ring.middleware.head))
+
+(defn- handler [req]
+ {:status 200
+ :headers {"X-method" (name (:request-method req))}
+ :body "Foobar"})
+
+(deftest test-wrap-head
+ (let [resp ((wrap-head handler) {:request-method :head})]
+ (is (nil? (:body resp)))
+ (is (= "get" (get-in resp [:headers "X-method"]))))
+ (let [resp ((wrap-head handler) {:request-method :post})]
+ (is (= (:body resp) "Foobar"))
+ (is (= "post" (get-in resp [:headers "X-method"])))))
Please sign in to comment.
Something went wrong with that request. Please try again.