diff --git a/ring-core/src/ring/util/request.clj b/ring-core/src/ring/util/request.clj index d434b962..67778dc9 100644 --- a/ring-core/src/ring/util/request.clj +++ b/ring-core/src/ring/util/request.clj @@ -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))) diff --git a/ring-core/test/ring/util/test/request.clj b/ring-core/test/ring/util/test/request.clj index a0373d16..839e8c99 100644 --- a/ring-core/test/ring/util/test/request.clj +++ b/ring-core/test/ring/util/test/request.clj @@ -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))