Permalink
Browse files

Routes for files and resources now guess content-type

  • Loading branch information...
weavejester committed Mar 5, 2011
1 parent faf19c4 commit c65a9932b64650763ab64014d29c0ef432810f49
Showing with 25 additions and 12 deletions.
  1. +1 −1 project.clj
  2. +11 −7 src/compojure/route.clj
  3. +13 −4 test/compojure/test/route.clj
View
@@ -5,7 +5,7 @@
[[org.clojure/clojure "1.2.0"]
[org.clojure/clojure-contrib "1.2.0"]
[clout "0.4.0"]
- [ring/ring-core "0.3.5"]]
+ [ring/ring-core "0.3.7"]]
:dev-dependencies
[[ring-mock "0.1.1"]
[autodoc "0.7.1" :exclusions [org.clojure/clojure
View
@@ -2,7 +2,9 @@
(:use compojure.core
[compojure.response :only (resource)]
[ring.util.response :only (file-response)]
- [ring.util.codec :only (url-decode)]))
+ [ring.util.codec :only (url-decode)]
+ ring.middleware.content-type
+ ring.middleware.file-info))
(defn- add-wildcard
"Add a wildcard to the end of a route path."
@@ -14,18 +16,20 @@
keys:
:root - the root path where the files are stored. Defaults to 'public'."
[path & [options]]
- (GET (add-wildcard path) {{file-path :*} :route-params}
- (let [options (merge {:root "public"} options)]
- (file-response file-path options))))
+ (-> (GET (add-wildcard path) {{file-path :*} :route-params}
+ (let [options (merge {:root "public"} options)]
+ (file-response file-path options)))
+ (wrap-file-info (:mime-types options))))
(defn resources
"A route for serving resources on the classpath. Accepts the following
keys:
:root - the root prefix to get the resources from. Defaults to 'public'."
[path & [options]]
- (GET (add-wildcard path) {{resource-path :*} :route-params}
- (let [root (:root options "public")]
- (resource (str root "/" resource-path)))))
+ (-> (GET (add-wildcard path) {{resource-path :*} :route-params}
+ (let [root (:root options "public")]
+ (resource (str root "/" resource-path))))
+ (wrap-content-type options)))
(defn not-found
"A route that returns a 404 not found response, with its argument as the
@@ -1,7 +1,6 @@
(ns compojure.test.route
(:use clojure.test
- ring.mock.request
- [clojure.contrib.io :only (slurp*)])
+ ring.mock.request)
(:require [compojure.route :as route]))
(deftest not-found-route
@@ -10,7 +9,17 @@
(is (= (:body response) "foo"))))
(deftest resources-route
- (let [route (route/resources "/foo" {:root "/resources"})
+ (let [route (route/resources "/foo" {:root "resources"})
response (route (request :get "/foo/test.txt"))]
(is (= (:status response) 200))
- (is (= (slurp* (:body response)) "foobar\n"))))
+ (is (= (slurp (:body response)) "foobar\n"))
+ (is (= (get-in response [:headers "Content-Type"])
+ "text/plain"))))
+
+(deftest files-route
+ (let [route (route/files "/foo" {:root "test/resources"})
+ response (route (request :get "/foo/test.txt"))]
+ (is (= (:status response) 200))
+ (is (= (slurp (:body response)) "foobar\n"))
+ (is (= (get-in response [:headers "Content-Type"])
+ "text/plain"))))

0 comments on commit c65a993

Please sign in to comment.