Permalink
Please sign in to comment.
Showing
with
30 additions
and 0 deletions.
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)))) |
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"]))))) |
0 comments on commit
d094e7d