Permalink
Browse files

More response building utility functions

  • Loading branch information...
1 parent 13c0999 commit f132606b0d23e78bcf24c7b7c99b279fe66a10bd @weavejester weavejester committed Oct 22, 2011
Showing with 29 additions and 0 deletions.
  1. +13 −0 ring-core/src/ring/util/response.clj
  2. +16 −0 ring-core/test/ring/util/test/response.clj
View
13 ring-core/src/ring/util/response.clj
@@ -18,6 +18,13 @@
:headers {"Location" url}
:body ""})
+(defn not-found
+ "Returns a 404 'not found' response."
+ [body]
+ {:status 404
+ :headers {}
+ :body body})
+
(defn response
"Returns a skeletal Ring response with the given body, status of 200, and no
headers."
@@ -115,3 +122,9 @@
to the given content-type."
[resp content-type]
(header resp "Content-Type" content-type))
+
+(defn set-cookie
+ "Sets a cookie on the response. Requires the handler to be wrapped in the
+ wrap-cookies middleware."
+ [resp name value & [opts]]
+ (assoc-in resp [:cookies name] (merge {:value value} opts)))
View
16 ring-core/test/ring/util/test/response.clj
@@ -12,6 +12,10 @@
(is (= {:status 303 :headers {"Location" "http://example.com"} :body ""}
(redirect-after-post "http://example.com"))))
+(deftest test-not-found
+ (is (= {:status 404 :headers {} :body "Not found"}
+ (not-found "Not found"))))
+
(deftest test-response
(is (= {:status 200 :headers {} :body "foobar"}
(response "foobar"))))
@@ -24,6 +28,10 @@
(content-type {:status 200 :headers {"Content-Length" "10"}}
"text/html"))))
+(deftest test-header
+ (is (= {:status 200 :headers {"X-Foo" "Bar"}}
+ (header {:status 200 :headers {}} "X-Foo" "Bar"))))
+
(defmacro with-classloader
"Temporarily replaces the current context classloader with one that
includes everything in dir"
@@ -77,3 +85,11 @@
(.getAbsoluteFile (File. "test/ring/assets/hello world.txt"))))
(is (= (slurp (:body resp))
"Hello World\n")))))
+
+(deftest test-set-cookie
+ (is (= {:status 200 :headers {} :cookies {"Foo" {:value "Bar"}}}
+ (set-cookie {:status 200 :headers {}}
+ "Foo" "Bar")))
+ (is (= {:status 200 :headers {} :cookies {"Foo" {:value "Bar" :http-only true}}}
+ (set-cookie {:status 200 :headers {}}
+ "Foo" "Bar" {:http-only true}))))

0 comments on commit f132606

Please sign in to comment.