Skip to content

Commit

Permalink
make base request properly extensible
Browse files Browse the repository at this point in the history
  • Loading branch information
flyingmachine committed Apr 5, 2020
1 parent 1214d68 commit afbef30
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 30 deletions.
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject sweet-tooth/sweet-tooth-endpoint "0.7.4"
(defproject sweet-tooth/sweet-tooth-endpoint "0.7.5"
:description "Utilities for working with liberator-based endpoints"
:url "https://github.com/sweet-tooth-clojure/sweet-tooth-endpoint"
:scm {:url "https://github.com/sweet-tooth-clojure/sweet-tooth-endpoint"}
Expand Down
61 changes: 36 additions & 25 deletions src/sweet_tooth/endpoint/test/harness.clj
Original file line number Diff line number Diff line change
Expand Up @@ -40,44 +40,55 @@
(transit/write (transit/writer out :json) data)
(java.io.ByteArrayInputStream. (.toByteArray out))))

(defn base-request
[method url params]
(defn headers
"Add all headers"
[req headers]
(reduce-kv mock/header req headers))

(defmulti base-request*
(fn [_method _url _params content-type]
content-type))

(defmethod base-request* :transit
[method url params _]
(-> (mock/request method url)
(mock/header :content-type "application/transit+json")
(mock/header :accept "application/transit+json")
(assoc :body (transit-in params))))

(defn req
[method url & [params]]
((handler) (base-request method url params)))

(defn resp-read-transit
[ring-resp]
(-> ring-resp
:body
(transit/reader :json)
transit/read))

(defn json-request
[method url params]
(defmethod base-request* :json
[method url params _]
(-> (mock/request method url)
(mock/header :content-type "application/json")
(mock/header :accept "application/json")
(assoc :body (json/encode params))))

(defn json-req
[method url & [params]]
((handler) (json-request method url params)))

(defn html-request
[method url]
(-> (mock/request method url)
(defmethod base-request* :html
[method url params _]
(-> (mock/request method url params)
(mock/header :content-type "text/html")
(mock/header :accept "text/html")))

(defn html-req
[method url]
((handler) (html-request method url)))
(defmethod base-request* :default
[method url params _]
(base-request* method url params :transit))

(defn base-request
([method url params]
(base-request* method url params nil))
([method url params content-type]
(base-request* method url params content-type)))

(defn req
[& args]
((handler) (apply base-request args)))

(defn resp-read-transit
[ring-resp]
(-> ring-resp
:body
(transit/reader :json)
transit/read))

(defn contains-entity?
"Request's response data creates entity of type `ent-type` that has
Expand Down
12 changes: 8 additions & 4 deletions test/sweet_tooth/endpoint/test/harness_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,25 @@
(defmethod ig/init-key ::a [_ opts]
opts)

(defmethod ig/init-key ::b [_ opts]
opts)

(defmethod es/config ::test [_]
{::a :b})
{::a (ig/ref ::b)
::b "hi"})

(deftest with-system-test
(is (= {::a :b}
(is (= {::a "hi" ::b "hi"}
(eth/with-system ::test
eth/*system*))))

(deftest with-custom-system-test
(is (= {::a :c}
(is (= {::a :c ::b "hi"}
(eth/with-custom-system ::test
{::a :c}
eth/*system*)))

(is (= {::a :c}
(is (= {::a :c ::b "hi"}
(eth/with-custom-system ::test
(fn [cfg] (assoc cfg ::a :c))
eth/*system*))))

0 comments on commit afbef30

Please sign in to comment.