Permalink
Browse files
More response building utility functions
- Loading branch information...
|
|
@@ -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)))
|
|
|
@@ -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