Skip to content

Commit

Permalink
resource-response returns File instance when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
weavejester committed Mar 20, 2011
1 parent 5f12de6 commit 9e7781e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 35 deletions.
5 changes: 4 additions & 1 deletion ring-core/src/ring/util/response.clj
Expand Up @@ -65,7 +65,10 @@
(.replace "//" "/")
(.replaceAll "^/" ""))]
(if-let [resource (io/resource path)]
(response (io/input-stream resource)))))
(response
(if (= "file" (.getProtocol resource))
(io/as-file resource)
(io/input-stream resource))))))

(defn status
"Returns an updated Ring response with the given status."
Expand Down
78 changes: 44 additions & 34 deletions ring-core/test/ring/util/response_test.clj
@@ -1,6 +1,24 @@
(ns ring.util.response-test
(:use clojure.test
ring.util.response))
ring.util.response)
(:import [java.io File InputStream]
org.apache.commons.io.FileUtils))

(deftest test-redirect
(is (= {:status 302 :headers {"Location" "http://google.com"} :body ""}
(redirect "http://google.com"))))

(deftest test-response
(is (= {:status 200 :headers {} :body "foobar"}
(response "foobar"))))

(deftest test-status
(is (= {:status 200 :body ""} (status {:status nil :body ""} 200))))

(deftest test-content-type
(is (= {:status 200 :headers {"Content-Type" "text/html" "Content-Length" "10"}}
(content-type {:status 200 :headers {"Content-Length" "10"}}
"text/html"))))

(defmacro with-classloader
"Temporarily replaces the current context classloader with one that
Expand All @@ -16,40 +34,32 @@
(finally
(.setContextClassLoader current-thread# original-loader#)))))

(deftest test-redirect
(is (= {:status 302 :headers {"Location" "http://google.com"} :body ""}
(redirect "http://google.com"))))
(deftest test-resource-response
(testing "response map"
(let [resp (resource-response "/ring/assets/foo.html")]
(is (= (resp :status) 200))
(is (= (resp :headers) {}))
(is (= (slurp (resp :body)) "foo"))))

(deftest test-response
(is (= {:status 200 :headers {} :body "foobar"}
(response "foobar"))))
(testing "with root option"
(let [resp (resource-response "/foo.html" {:root "/ring/assets"})]
(is (= (slurp (resp :body)) "foo"))))

(deftest test-status
(is (= {:status 200 :body ""} (status {:status nil :body ""} 200))))
(testing "with child class-loader"
(let [resource (File/createTempFile "response_test" nil)]
(FileUtils/writeStringToFile resource "just testing")
(with-classloader [(.getParentFile resource)]
(let [resp (resource-response (.getName resource))]
(is (= (slurp (resp :body))
"just testing"))))))

(deftest test-content-type
(is (= {:status 200 :headers {"Content-Type" "text/html" "Content-Length" "10"}}
(content-type {:status 200 :headers {"Content-Length" "10"}} "text/html"))))
(testing "missing resource"
(is (nil? (resource-response "/missing/resource.clj"))))

(deftest test-resource-response
(let [resp (resource-response "/ring/util/response_test.clj")]
(is (= (resp :status) 200))
(is (= (resp :headers) {}))
(is (.startsWith (slurp (resp :body))
"(ns ring.util.response-test"))))

(deftest test-resource-with-root
(let [resp (resource-response "response_test.clj" {:root "/ring/util"})]
(is (.startsWith (slurp (resp :body))
"(ns ring.util.response-test"))))

(deftest test-resource-with-child-classloader
(let [resource (java.io.File/createTempFile "response_test" nil)]
(org.apache.commons.io.FileUtils/writeStringToFile resource "just testing")
(with-classloader [(.getParentFile resource)]
(let [resp (resource-response (.getName resource))]
(is (= (slurp (resp :body))
"just testing"))))))

(deftest test-missing-resource
(is (nil? (resource-response "/missing/resource.clj"))))
(testing "response body type"
(let [body (:body (resource-response "ring/util/response.clj"))]
(is (instance? File body))
(is (.startsWith (slurp body) "(ns ring.util.response")))
(let [body (:body (resource-response "clojure/java/io.clj"))]
(is (instance? InputStream body))
(is (.contains (slurp body) "clojure.java.io")))))

0 comments on commit 9e7781e

Please sign in to comment.