Skip to content

Commit

Permalink
Added ring.util.response/charset function (fixes #67)
Browse files Browse the repository at this point in the history
  • Loading branch information
weavejester committed Apr 6, 2012
1 parent cdee546 commit 0415c59
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
10 changes: 10 additions & 0 deletions ring-core/src/ring/util/response.clj
Expand Up @@ -123,6 +123,16 @@
[resp content-type]
(header resp "Content-Type" content-type))

(defn charset
"Returns an updated Ring response with the supplied charset added to the
Content-Type header."
[resp charset]
(update-in resp [:headers "Content-Type"]
(fn [content-type]
(-> (or content-type "text/plain")
(str/replace #";\s*charset=[^;]*" "")
(str "; charset=" charset)))))

(defn set-cookie
"Sets a cookie on the response. Requires the handler to be wrapped in the
wrap-cookies middleware."
Expand Down
12 changes: 12 additions & 0 deletions ring-core/test/ring/util/test/response.clj
Expand Up @@ -28,6 +28,18 @@
(content-type {:status 200 :headers {"Content-Length" "10"}}
"text/html"))))

(deftest test-charset
(testing "add charset"
(is (= (charset {:status 200 :headers {"Content-Type" "text/html"}} "UTF-8")
{:status 200 :headers {"Content-Type" "text/html; charset=UTF-8"}})))
(testing "replace existing charset"
(is (= (charset {:status 200 :headers {"Content-Type" "text/html; charset=UTF-16"}}
"UTF-8")
{:status 200 :headers {"Content-Type" "text/html; charset=UTF-8"}})))
(testing "default content-type"
(is (= (charset {:status 200 :headers {}} "UTF-8")
{:status 200 :headers {"Content-Type" "text/plain; charset=UTF-8"}}))))

(deftest test-header
(is (= {:status 200 :headers {"X-Foo" "Bar"}}
(header {:status 200 :headers {}} "X-Foo" "Bar"))))
Expand Down

0 comments on commit 0415c59

Please sign in to comment.