Skip to content

Commit

Permalink
Support :as
Browse files Browse the repository at this point in the history
Add support for {:as ...} for streams, text, and byte arrays
  • Loading branch information
icambron committed Nov 20, 2015
1 parent 7b272c3 commit fb093ce
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 16 deletions.
44 changes: 34 additions & 10 deletions src/org/httpkit/fake.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
(ns org.httpkit.fake
"Library to fake HTTP traffic with org.httpkit.client."
(:use [robert.hooke :only [with-scope add-hook]])
(:require org.httpkit.client))
(:require org.httpkit.client
[clojure.java.io :as io]))

(defn regex?
[obj]
Expand All @@ -11,6 +12,27 @@
[obj]
(instance? clojure.lang.IDeref obj))

(defn coerce-body
"Coerce the body into the type specified by `(:as opts)`, one of :stream, :byte-array,
:text (:auto is not supported). See
http://www.http-kit.org/client.html#coercion for more."
[resp opts]
(if-let [typ (:as opts)]
(assoc resp :body
(let [body (:body resp)
streamed (io/input-stream (cond
(nil? body) (byte-array 0)
(instance? String body) (.getBytes body)
:else body))]
(case typ
:stream streamed
:byte-array (with-open [out (java.io.ByteArrayOutputStream.)]
(io/copy streamed out)
(.toByteArray out))
:text (slurp streamed)
body)))
resp))

(defn handle-unmatched
"The default handler function that is applied in the case a request sent to
#'org.httpkit.client/request is not matched by any other handlers.
Expand All @@ -30,15 +52,17 @@
This function merges in some defaults."
[opts res-spec]
(merge
{:opts opts
:status 200
:headers {:content-type "text/html"
:server "org.httpkit.fake"}}
(cond
(string? res-spec) {:body res-spec}
(number? res-spec) {:status res-spec}
:else res-spec)))
(->
(merge
{:opts opts
:status 200
:headers {:content-type "text/html"
:server "org.httpkit.fake"}}
(cond
(string? res-spec) {:body res-spec}
(number? res-spec) {:status res-spec}
:else res-spec))
(coerce-body opts)))

(defn responder-wrapper
"Wraps the provided responder to add missing boilerplate code."
Expand Down
33 changes: 27 additions & 6 deletions test/org/httpkit/fake_test.clj
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
(ns org.httpkit.fake-test
(:use org.httpkit.fake
robert.hooke
clojure.test)
(:require [org.httpkit.client :as http]))
(:use clojure.test
org.httpkit.fake
robert.hooke)
(:require [clojure.java.io :as io]
[org.httpkit.client :as http]))

(deftest fake-test
(testing "org.httpkit.fake/with-fake-http"
Expand Down Expand Up @@ -178,7 +179,7 @@
(testing "allows matched urls"
(is (= "ok"
(:body @(http/get "http://bar.co/")))))))

(testing "http-kit/request works correctly when callback-function is defined"
(with-fake-http ["http://foo.com/" "ok"]
(is (= "ok"
Expand All @@ -188,4 +189,24 @@
(testing "http-kit/request works correctly without callback function. "
(with-fake-http ["http://foo.com/" "ok"]
(is (= "ok"
(:body @(http/request {:url "http://foo.com/" :method :get}))))))))
(:body @(http/request {:url "http://foo.com/" :method :get}))))))

(testing "using :as :auto doesn't break anything"
(with-fake-http ["http://foo.com/" "ok"]
(is (= "ok"
(:body @(http/request {:url "http://foo.com/" :method :get :as :auto}))))))

(testing "each combination of inputs and coercions provides the right thing"
(doseq [[typ value-fn] {:text (constantly "hello")
:byte-array #(.getBytes "hello")
:stream #(io/input-stream (.getBytes "hello"))}
[as expected-type] {:text String
:stream java.io.InputStream
:byte-array (Class/forName "[B")}]

(testing (str typ "->" as)
(with-fake-http ["http://foo.com/" (fn [_ _ _] {:body (value-fn)})]
(let [r @(http/request {:method :get :url "http://foo.com/" :as as})]
(is (instance? expected-type (:body r)))
(is (= "hello"
(:body (org.httpkit.fake/coerce-body r {:as :text})))))))))))

0 comments on commit fb093ce

Please sign in to comment.