Permalink
Browse files

Removed handling of servlet-resources in favor of Ring's resource han…

…dling
  • Loading branch information...
1 parent 72fdc10 commit b43716be375b98307bc05229d31386050c7e3ee6 @weavejester committed May 28, 2011
Showing with 49 additions and 57 deletions.
  1. +17 −34 src/compojure/response.clj
  2. +3 −3 src/compojure/route.clj
  3. +28 −19 test/compojure/test/response.clj
  4. +1 −1 test/compojure/test/route.clj
View
@@ -4,6 +4,7 @@
[ring.util.response :only (response content-type)])
(:require [clojure.java.io :as io])
(:import [java.io File InputStream]
+ [java.net URL]
[clojure.lang APersistentMap IDeref IFn ISeq]))
(defprotocol Renderable
@@ -14,44 +15,26 @@
nil
(render [_ _] nil)
String
- (render [this _]
- (-> (response this)
+ (render [body _]
+ (-> (response body)
(content-type "text/html")))
APersistentMap
- (render [this _]
- (merge (response "") this))
+ (render [resp-map _]
+ (merge (response "") resp-map))
IFn
- (render [this request]
- (render (this request) request))
+ (render [func request]
+ (render (func request) request))
IDeref
- (render [this request]
- (render (deref this) request))
+ (render [ref request]
+ (render (deref ref) request))
File
- (render [this _] (response this))
+ (render [file _] (response file))
ISeq
- (render [this _] (response this))
+ (render [coll _] (response coll))
InputStream
- (render [this _] (response this)))
-
-(defn- servlet-resource-stream [path request]
- (-?> (:servlet-context request)
- (.getResourceAsStream (str "/" path))))
-
-(defn- classpath-resource-stream [path]
- (-?> (io/resource path)
- (io/input-stream)))
-
-(defn- resource-stream [path request]
- (or (classpath-resource-stream path)
- (servlet-resource-stream path request)))
-
-(deftype Resource [path]
- Renderable
- (render [_ request]
- (-?> (resource-stream path request)
- (response))))
-
-(defn resource
- "Create a resource response."
- [^String path]
- (Resource. path))
+ (render [stream _] (response stream))
+ URL
+ (render [url _]
+ (if (= "file" (.getProtocol url))
+ (response (io/as-file url))
+ (response (io/input-stream url)))))
View
@@ -1,7 +1,6 @@
(ns compojure.route
(:use compojure.core
- [compojure.response :only (resource)]
- [ring.util.response :only (file-response)]
+ [ring.util.response :only (file-response resource-response)]
[ring.util.codec :only (url-decode)]
ring.middleware.content-type
ring.middleware.file-info))
@@ -28,7 +27,8 @@
[path & [options]]
(-> (GET (add-wildcard path) {{resource-path :*} :route-params}
(let [root (:root options "public")]
- (resource (str root "/" resource-path))))
+ (resource-response (str root "/" resource-path))))
+ (wrap-file-info (:mime-types options))
(wrap-content-type options)))
(defn not-found
@@ -1,28 +1,37 @@
(ns compojure.test.response
- (:use [clojure.test] :reload)
- (:require [compojure.response :as response]))
+ (:use clojure.test)
+ (:require [clojure.java.io :as io])
+ (:require [compojure.response :as response] :reload)
+ (:import [java.io File InputStream]
+ java.net.URL))
-(deftest response-with-nil
- (is (nil? (response/render nil {}))))
-
-(def test-response
+(def expected-response
{:status 200
:headers {"Content-Type" "text/html"}
:body "<h1>Foo</h1>"})
-(deftest response-with-string
- (is (= (response/render "<h1>Foo</h1>" {})
- test-response)))
+(deftest response-test
+ (testing "with nil"
+ (is (nil? (response/render nil {}))))
+
+ (testing "with string"
+ (is (= (response/render "<h1>Foo</h1>" {})
+ expected-response)))
+
+ (testing "with handler function"
+ (is (= (response/render (constantly expected-response) {})
+ expected-response)))
-(deftest response-with-fn
- (is (= (response/render (constantly test-response) {})
- test-response)))
+ (testing "with deref-able"
+ (is (= (response/render (future expected-response) {})
+ expected-response)))
-(deftest response-with-deref
- (is (= (response/render (future test-response) {})
- test-response)))
+ (testing "with file URL"
+ (let [response (response/render (io/resource "resources/test.txt") {})]
+ (is (instance? File (:body response)))
+ (is (= (slurp (:body response)) "foobar\n"))))
-(deftest response-resource
- (let [resource (response/resource "resources/test.txt")
- response (response/render resource {})]
- (is (= (slurp (:body response)) "foobar\n"))))
+ (testing "with stream URL"
+ (let [response (response/render (io/resource "ring/util/response.clj") {})]
+ (is (instance? InputStream (:body response)))
+ (is (.contains (slurp (:body response)) "(ns ring.util.response")))))
@@ -1,7 +1,7 @@
(ns compojure.test.route
(:use clojure.test
ring.mock.request)
- (:require [compojure.route :as route]))
+ (:require [compojure.route :as route] :reload))
(deftest not-found-route
(let [response ((route/not-found "foo") (request :get "/"))]

0 comments on commit b43716b

Please sign in to comment.