Skip to content

Commit

Permalink
allow all the noir.response/* functions to be nested and add utf-8 to
Browse files Browse the repository at this point in the history
all generated content types

Signed-off-by: Chris Granger <ibdknox@gmail.com>
  • Loading branch information
ibdknox committed Mar 20, 2012
1 parent 8a42f3d commit 2ad2e27
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 17 deletions.
37 changes: 24 additions & 13 deletions src/noir/response.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/noir/util/test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 18 additions & 3 deletions test/noir/test/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -87,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
Expand Down Expand Up @@ -221,8 +221,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" []
Expand Down Expand Up @@ -275,3 +275,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 2ad2e27

Please sign in to comment.