Skip to content

Commit

Permalink
Update changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
ordnungswidrig committed Dec 20, 2017
1 parent 272fad7 commit 218febd
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 57 deletions.
7 changes: 5 additions & 2 deletions CHANGES.markdown
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# Changelog

# Unreleased
# Unreleased

* Removed javax.xml.ws dependency
## Bugs fixed

* Log sequence could grow beyond limit (#295)
* Removed javax.xml.ws dependency (#290)

# New in 0.15.1

Expand Down
16 changes: 8 additions & 8 deletions src/liberator/representation.clj
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,10 @@

(defn render-as-clojure [data]
(binding [*print-dup* true]
(with-out-str (pr data))))
(pr-str data)))

(defn render-as-edn [data]
(binding [*print-dup* false]
(with-out-str (pr data))))
(pr-str data))

(defmethod render-map-generic "application/clojure" [data context]
(render-as-clojure data))
Expand Down Expand Up @@ -138,12 +137,13 @@
:keys [dictionary fields] :or {dictionary default-dictionary
fields (keys (first data))}
:as context} sep]
(with-out-str
(csv/write-csv *out* [(map #(or (dictionary % language)
(default-dictionary % language)) fields)]
(let [sw (java.io.StringWriter.)]
(csv/write-csv sw [(map #(or (dictionary % language)
(default-dictionary % language)) fields)]
:newline :cr+lf :separator sep)
(csv/write-csv sw (map (apply juxt (map (fn [x] (fn [m] (get m x))) fields)) data)
:newline :cr+lf :separator sep)
(csv/write-csv *out* (map (apply juxt (map (fn [x] (fn [m] (get m x))) fields)) data)
:newline :cr+lf :separator sep)))
(str sw)))

(defmethod render-seq-generic "text/csv" [data context]
(render-seq-csv data context \,))
Expand Down
113 changes: 66 additions & 47 deletions test/test_representation.clj
Original file line number Diff line number Diff line change
Expand Up @@ -31,56 +31,75 @@
"application/edn" (pr-str entity))))

(facts "Can produce representations from a seq of maps"
(let [entity [(sorted-map :foo 1 :bar 2) (sorted-map :foo 2 :bar 3)]]
(tabular "Various media types are supported"
(as-response entity {:representation {:media-type ?media-type :charset "UTF-8"}})
=> {:body ?body :headers { "Content-Type" (str ?media-type ";charset=UTF-8")}}
?media-type ?body
"text/csv" "bar,foo\r\n2,1\r\n3,2\r\n"
"text/tab-separated-values" "bar\tfoo\r\n2\t1\r\n3\t2\r\n"
"text/plain" "bar=2\r\nfoo=1\r\n\r\nbar=3\r\nfoo=2"
"text/html" (str "<div><table>"
"<thead><tr><th>bar</th><th>foo</th></tr></thead>"
"<tbody>"
"<tr><td>2</td><td>1</td></tr>"
"<tr><td>3</td><td>2</td></tr>"
"</tbody>"
"</table></div>")
"application/json" (clojure.data.json/write-str entity)
"application/clojure" (pr-str-dup entity)
"application/edn" (pr-str entity))))
(let [entity [(sorted-map :foo 1 :bar 2) (sorted-map :foo 2 :bar 3)]]
(tabular "Various media types are supported"
(as-response entity {:representation {:media-type ?media-type :charset "UTF-8"}})
=> {:body ?body :headers { "Content-Type" (str ?media-type ";charset=UTF-8")}}
?media-type ?body
"text/csv" "bar,foo\r\n2,1\r\n3,2\r\n"
"text/tab-separated-values" "bar\tfoo\r\n2\t1\r\n3\t2\r\n"
"text/plain" "bar=2\r\nfoo=1\r\n\r\nbar=3\r\nfoo=2"
"text/html" (str "<div><table>"
"<thead><tr><th>bar</th><th>foo</th></tr></thead>"
"<tbody>"
"<tr><td>2</td><td>1</td></tr>"
"<tr><td>3</td><td>2</td></tr>"
"</tbody>"
"</table></div>")
"application/json" (clojure.data.json/write-str entity)
"application/clojure" (pr-str-dup entity)
"application/edn" (pr-str entity))))


(facts "Using print in layz-seqs does not side-effect with response generation"
(print "Expecting test output <<<")
(let [entity (map #(do (print "BAD")
{:foo (inc %)})
(range 100))]
(tabular "Various media types are supported"
(as-response entity {:representation {:media-type ?media-type :charset "UTF-8"}})
=not=> (contains {:body (contains "BAD")})

?media-type
"text/csv"
"text/tab-separated-values"
"text/plain"
"text/html"
"application/json"
"application/clojure"
"application/edn"))
(println ">>> Test output complete."))

(facts "Can give ring response map to override response values"
(facts "returns single ring response unchanged"
(let [response {:status 123
:headers {"Content-Type" "application/json;charset=UTF-8"
"X-Foo" "Bar"}
:body "123" }]
(as-response (ring-response response) {}) => response))
(facts "delegates to default response generation when value is given"
(fact "for strings"
(as-response (ring-response "foo" {}) {}) => (as-response "foo" {}))
(fact "for maps"
(let [ctx {:representation {:media-type "application/json"}}]
(as-response (ring-response {:a 1} {}) ctx)
=> (as-response {:a 1} ctx))))
(facts "lets override response attributes"
(fact "all attributes"
(let [overidden {:body "body"
:headers ["Content-Type" "application/foo"]
:status 999}]
(as-response (ring-response "foo" overidden)
{:status 200}) => overidden))
(facts "some attributes"
(facts "status"
(as-response (ring-response "foo" {:status 999}) {:status 200})
=> (contains {:status 999}))
(facts "header merged"
(as-response (ring-response "foo" {:headers {"X-Foo" "bar"}})
{:status 200})
=> (contains {:headers {"X-Foo" "bar"
"Content-Type" "text/plain;charset=UTF-8"}})))))
(facts "returns single ring response unchanged"
(let [response {:status 123
:headers {"Content-Type" "application/json;charset=UTF-8"
"X-Foo" "Bar"}
:body "123" }]
(as-response (ring-response response) {}) => response))
(facts "delegates to default response generation when value is given"
(fact "for strings"
(as-response (ring-response "foo" {}) {}) => (as-response "foo" {}))
(fact "for maps"
(let [ctx {:representation {:media-type "application/json"}}]
(as-response (ring-response {:a 1} {}) ctx)
=> (as-response {:a 1} ctx))))
(facts "lets override response attributes"
(fact "all attributes"
(let [overidden {:body "body"
:headers ["Content-Type" "application/foo"]
:status 999}]
(as-response (ring-response "foo" overidden)
{:status 200}) => overidden))
(facts "some attributes"
(facts "status"
(as-response (ring-response "foo" {:status 999}) {:status 200})
=> (contains {:status 999}))
(facts "header merged"
(as-response (ring-response "foo" {:headers {"X-Foo" "bar"}})
{:status 200})
=> (contains {:headers {"X-Foo" "bar"
"Content-Type" "text/plain;charset=UTF-8"}})))))
(facts "about entity parsing"

(fact "it parses a json entity"
Expand Down

0 comments on commit 218febd

Please sign in to comment.