Permalink
Please sign in to comment.
Showing
with
35 additions
and 0 deletions.
17
ring-core/src/ring/middleware/resource.clj
| @@ -0,0 +1,17 @@ | ||
| +(ns ring.middleware.resource | ||
| + "Middleware for serving static resources." | ||
| + (require [ring.util.codec :as codec] | ||
| + [ring.util.response :as response])) | ||
| + | ||
| +(defn wrap-resource | ||
| + "Middleware that first checks to see whether the request map matches a static | ||
| + resource. If it does, the resource is returned in a response map, otherwise | ||
| + the request map is passed onto the handler. The root-path argument will be | ||
| + added to the beginning of the resource path." | ||
| + [handler root-path] | ||
| + (fn [request] | ||
| + (if-not (= :get (:request-method request)) | ||
| + (handler request) | ||
| + (let [path (.substring (codec/url-decode (:uri request)) 1)] | ||
| + (or (response/resource-response path {:root root-path}) | ||
| + (handler request)))))) |
18
ring-core/test/ring/middleware/resource_test.clj
| @@ -0,0 +1,18 @@ | ||
| +(ns ring.middleware.resource-test | ||
| + (:use clojure.test | ||
| + ring.util.test | ||
| + ring.middleware.resource)) | ||
| + | ||
| +(defn test-handler [request] | ||
| + {:status 200 | ||
| + :headers {} | ||
| + :body (string-input-stream "handler")}) | ||
| + | ||
| +(deftest resource-test | ||
| + (let [handler (wrap-resource test-handler "/ring/assets")] | ||
| + (are [request body] (= (slurp (:body (handler request))) body) | ||
| + {:request-method :get, :uri "/foo.html"} "foo" | ||
| + {:request-method :get, :uri "/index.html"} "index" | ||
| + {:request-method :get, :uri "/bars/foo.html"} "foo" | ||
| + {:request-method :get, :uri "/handler"} "handler" | ||
| + {:request-method :post, :uri "/foo.html"} "handler"))) |
0 comments on commit
e89404c