Skip to content

Commit

Permalink
Added tests for accept, fixed nil header bug
Browse files Browse the repository at this point in the history
  • Loading branch information
weavejester committed Jan 15, 2012
1 parent 1d75558 commit 3b8ad97
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
8 changes: 4 additions & 4 deletions ring-core/src/ring/util/request.clj
Expand Up @@ -22,13 +22,13 @@
(assoc :value value))))

(defn accept
"Parse the accept header and returns an ordered seq of maps for each media
type. Each map will contain at least a :value and a :q key.
"Parse the accept header and returns a seq of maps for each media type.
Each map will contain at least a :value and a :q key.
e.g. (accept {:headers {\"accept\": \"text/html, text/xml;q=0.8\"})
=> ({:value \"text/html\", :q 1.0}
{:value \"text/xml\", :q 0.8})"
[request]
(->> (header-seq request "accept")
(map parse-accept-value)))
(if-let [values (header-seq request "accept")]
(map parse-accept-value values)))
11 changes: 11 additions & 0 deletions ring-core/test/ring/util/test/request.clj
Expand Up @@ -7,3 +7,14 @@
"accept" "text/html, text/xml" ["text/html" "text/xml"]
"content-type" "text/html" ["text/html"]
"x-foo" nil nil))

(deftest test-accept
(are [a b] (= (accept {:headers {"accept" a}}) b)
"text/html, text/xml"
[{:value "text/html" :q 1.0} {:value "text/xml" :q 1.0}]
"text/html"
[{:value "text/html" :q 1.0}]
"text/xml;q=0.5, application/json"
[{:value "text/xml" :q 0.5} {:value "application/json" :q 1.0}]
nil
nil))

0 comments on commit 3b8ad97

Please sign in to comment.