Permalink
Browse files

Merge branch '1.3'

  • Loading branch information...
2 parents c34a7d8 + 2ad2e27 commit fc1bbd0f01a60f0545614b6e4653f7ce7c8cdb57 @ibdknox committed Mar 20, 2012
Showing with 43 additions and 24 deletions.
  1. +24 −13 src/noir/response.clj
  2. +1 −1 src/noir/util/test.clj
  3. +18 −10 test/noir/test/core.clj
View
@@ -4,36 +4,47 @@
(:require [cheshire.core :as json]
[noir.options :as options]))
-(defn xml
- "Wraps the response with the content type for xml and sets the body to the content."
- [content]
- {:headers {"Content-Type" "text/xml"}
- :body content})
+(defn- ->map [c]
+ (if-not (map? c)
+ {:body c}
+ c))
+
+(defn set-headers
+ "Add a map of headers to the given response. Headers must have
+ string keys:
+
+ (set-headers {\"x-csrf\" csrf}
+ (common/layout [:p \"hey\"]))"
+ [headers content]
+ (update-in (->map content) [:headers] merge headers))
(defn content-type
"Wraps the response with the given content type and sets the body to the content."
[ctype content]
- {:headers {"Content-Type" ctype}
- :body content})
+ (set-headers {"Content-Type" ctype} content))
+
+(defn xml
+ "Wraps the response with the content type for xml and sets the body to the content."
+ [content]
+ (content-type "text/xml; charset=utf-8" content))
(defn json
"Wraps the response in the json content type and generates JSON from the content"
[content]
- {:headers {"Content-Type" "application/json"}
- :body (json/generate-string content)})
+ (content-type "application/json; charset=utf-8"
+ (json/generate-string content)))
(defn jsonp
"Generates JSON for the given content and creates a javascript response for calling
func-name with it."
[func-name content]
- {:headers {"Content-Type" "application/javascript"}
- :body (str func-name "(" (json/generate-string content) ");")})
+ (content-type "application/json; charset=utf-8"
+ (str func-name "(" (json/generate-string content) ");")))
(defn status
"Wraps the content in the given status code"
[code content]
- {:status code
- :body content})
+ (assoc (->map content) :status code))
(defn redirect
"A header redirect to a different url"
@@ -7,7 +7,7 @@
[noir.cookies :as cookies]
[noir.options :as options]))
-(def content-types {:json "application/json"
+(def content-types {:json "application/json; charset=utf-8"
:html "text/html"})
(defmacro with-noir
@@ -7,7 +7,6 @@
(:use [clojure.test])
(:require [noir.util.crypt :as crypt]
[noir.server :as server]
- [noir.util.middleware :as middleware]
[noir.session :as session]
[noir.request :as request]
[noir.options :as options]
@@ -88,7 +87,7 @@
(deftest route-dot-test
(-> (send-request "/test.json")
(has-status 200)
- (has-content-type "application/json")
+ (has-content-type "application/json; charset=utf-8")
(has-body "{\"json\":\"text\"}")))
(deftest parsing-defpage
@@ -229,8 +228,8 @@
(has-body "<a href=\"/woohoo/hey\">link</a>"))))
(deftest jsonp
- (-> (resp/jsonp "jsonp245" {:pinot "noir"})
- (has-content-type "application/javascript")
+ (-> (resp/jsonp "jsonp245" {:pinot "noir"})
+ (has-content-type "application/json; charset=utf-8")
(has-body "jsonp245({\"pinot\":\"noir\"});")))
(defpage "/with%20space" []
@@ -259,12 +258,6 @@
(deftest wrap-utf
(-> (send-request "/utf")
(has-content-type "text/html; charset=utf-8")
- (has-body "ąčęė"))
- ;;Technically this middleware is unnecessary now due to some changes in ring.
- ;;but this provides a nice test for custom middleware.
- (server/add-middleware middleware/wrap-utf-8)
- (-> (send-request "/utf")
- (has-content-type "text/html; charset=utf-8; charset=utf-8")
(has-body "ąčęė")))
(deftest valid-emails
@@ -289,3 +282,18 @@
"test"
"test.@domain.com"
"test@com"))
+
+(defpage "/different/content-type" []
+ (resp/content-type "application/vcard+xml" (resp/xml (html [:vcards]))))
+
+(deftest different-content-type
+ (-> (send-request "/different/content-type")
+ (has-content-type "application/vcard+xml")
+ (has-body "<vcards />")))
+
+(defpage "/different/status" []
+ (resp/status 201 "Something was created"))
+
+(deftest different-header
+ (-> (send-request "/different/status")
+ (has-status 201)))

0 comments on commit fc1bbd0

Please sign in to comment.