Permalink
Browse files

Added to test for util/optimal-media-type and modified function to pass.

Now handles wild card media types in Accept header: "*" and "*/*". Does not
yet handle wild card forms like "text/*".
  • Loading branch information...
1 parent d87aae6 commit 95480dd42fb19a7646afc7d72b88525ce228db17 @tmciver committed Feb 1, 2013
Showing with 16 additions and 11 deletions.
  1. +10 −9 src/hottop/util.clj
  2. +6 −2 test/hottop/test/proc.clj
View
@@ -59,18 +59,19 @@ HTML format (the Accept header contains one or both of 'text/html' or
if there isn't one. See RFC 2046 (http://tools.ietf.org/html/rfc2046) or
http://en.wikipedia.org/wiki/MIME_type for examples of media type strings.
- WARNING! This function is broken. Specifically, */* and type/* media types in
+ WARNING! This function is broken. Specifically, type/* media types in
the Accept header are not handled."
[request resource]
(let [ct-provided (set (keys (:content-types-provided resource)))
- accept-maps (parse-accept-header (get-in request [:headers "accept"]))
- accept-maps (filter #(not (= 0 (:q %))) accept-maps)
- ct-accepted (set (map :type accept-maps))
- intersection (set/intersection ct-provided ct-accepted)
- type (->> (filter #(intersection (:type %)) accept-maps)
- (sort-by :q >)
- first
- :type)]
+ accept-maps (->> (parse-accept-header (get-in request [:headers "accept"]))
+ (filter #(not (= 0 (:q %))))
+ (sort-by :q >))
+ type (some #(let [type (:type %)]
+ (if (or (= type "*")
+ (= type "*/*"))
+ (first ct-provided)
+ (some #{type} ct-provided)))
+ accept-maps)]
type))
(defn response?
@@ -48,16 +48,20 @@
(header "Accept" "text/html"))
request2 (-> (request :get "/test")
(header "Accept" "text/csv"))
+ request3 (-> (request :get "/test")
+ (header "Accept" "*/*"))
resource1 (create-readonly-html-resource (constantly "hello!"))
resource2 (-> base-resource
(assoc-in [:methods :get] (constantly "hello."))
(assoc-in [:content-types-provided "text/csv"] identity))
response1 ((validate-accept (fn [r _] r)) request1 resource1)
response2 ((validate-accept (constantly :handler2)) request2 resource1)
- response3 ((validate-accept (fn [r _] r)) request2 resource2)]
+ response3 ((validate-accept (fn [r _] r)) request2 resource2)
+ response4 ((validate-accept (fn [r _] r)) request3 resource1)]
(is (= (:optimal-ct response1) "text/html"))
(is (= response2 (response/code 406)))
- (is (= (:optimal-ct response3) "text/csv"))))
+ (is (= (:optimal-ct response3) "text/csv"))
+ (is (= (:optimal-ct response4) "text/html"))))
(testing "Process GET"
(let [request1 (-> (request :get "/test")

0 comments on commit 95480dd

Please sign in to comment.