Skip to content

Instantly share code, notes, and snippets.

@jmorton
Last active October 8, 2015 05:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmorton/7db7ebdd795b6e8606dd to your computer and use it in GitHub Desktop.
Save jmorton/7db7ebdd795b6e8606dd to your computer and use it in GitHub Desktop.
accept.clj
(defrecord Accept [media-range quality accept-extension])
(defn accept [str]
"Parse a single accept string into a map"
; according to RFC2616, the "q" parameter must precede the accept-extension
(let [pattern #"([^;]+)\s*(?:;q=([0-9+\.]+))?\s*(;.+)*"
matches (re-find pattern str)
[_ media-range qvalue accept-extension] matches
quality (java.lang.Double/parseDouble (or qvalue "1"))]
(Accept. media-range quality accept-extension)))
(ns accept.core-test
(:require [clojure.test :refer :all]
[accept.core :refer :all]
:reload))
(deftest accept-parser-tests
(is (= (accept "text/html")
(map->Accept {:media-range "text/html" :quality 1.0 :accept-extension nil} )))
(is (= (accept "text/html;q=0.9")
(map->Accept {:media-range "text/html" :quality 0.9 :accept-extension nil} ))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment