Skip to content
Browse files

Added route/resources function and unit tests

  • Loading branch information...
1 parent e593cd5 commit bcafbeddaffe227bea1812354426eea088b77648 @weavejester committed Sep 13, 2010
Showing with 31 additions and 3 deletions.
  1. +15 −3 src/compojure/route.clj
  2. +15 −0 test/compojure/test/route.clj
  3. +1 −0 test/resources/test.txt
View
18 src/compojure/route.clj
@@ -1,6 +1,6 @@
(ns compojure.route
(:use compojure.core
- [ring.util.response :only (file-response)]
+ [ring.util.response :only (file-response resource-response)]
[ring.util.codec :only (url-decode)]))
(defn- add-wildcard
@@ -9,13 +9,25 @@
(str path (if (.endsWith path "/") "*" "/*")))
(defn files
- "A route for serving static files from a directory."
+ "A route for serving static files from a directory. Accepts the following
+ keys:
+ :root - the root path where the files are stored. Defaults to 'public'."
[path & [options]]
(GET (add-wildcard path) {{file-path "*"} :params}
(let [options (merge {:root "public"} options)]
(file-response file-path 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 "*"} :params}
+ (let [options (merge {:root "public"} options)]
+ (resource-response resource-path options))))
+
(defn not-found
- "A route that returns a 404 not found response."
+ "A route that returns a 404 not found response, with its argument as the
+ response body."
[body]
(ANY "*" [] {:status 404, :body body}))
View
15 test/compojure/test/route.clj
@@ -0,0 +1,15 @@
+(ns compojure.test.route
+ (:use clojure.test
+ [clojure.contrib.duck-streams :only (slurp*)])
+ (:require [compojure.route :as route]))
+
+(deftest not-found-route
+ (let [response ((route/not-found "foo") {})]
+ (is (= (:status response) 404))
+ (is (= (:body response) "foo"))))
+
+(deftest resources-route
+ (let [route (route/resources "/foo" {:root "resources"})
+ response (route {:request-method :get, :uri "/foo/test.txt"})]
+ (is (= (:status response) 200))
+ (is (= (slurp* (:body response)) "foobar\n"))))
View
1 test/resources/test.txt
@@ -0,0 +1 @@
+foobar

0 comments on commit bcafbed

Please sign in to comment.
Something went wrong with that request. Please try again.