http-kit-fake
http-kit-fake copied to clipboard
Test :body of a :post
My base.clj use http-kit to make a HTTP POST request with a :body supplied. How am I to test if the :body supplied to a fake request confines to what I expect it to be? I tried the following
(testing "create"
(with-fake-http [{:url "https://mydomain.com/account/organizations"
:method :post
:body {:name "test"}}
{:body create-resp}]
(let [resp (create "test")]
(is (= "test" (:name resp))))))
where create-resp is simply a faked response map that looks like
{:name "test",
:updated_at "2015-05-22T14:26:39.537Z",
:created_at "2015-05-22T14:26:39.537Z"}
If I do this I get java.lang.IllegalArgumentException: Attempted to perform POST on unregistered URL https://mydomain.com/account/organizations and real HTTP requests are disabled.
Changing my "fake" request to
{:url "https://mydomain.com/account/organizations"
:method :post}
causes the test to pass, but there is not way to verify if the :body supplied is as I expect it to be.
@looselytyped I solve this by creating picky endpoints:
(testing
(let [url "http://somewhere.com"]
(fakit/with-fake-http [{:url url :method :post}
(fn [_ {body :body} _]
(if (= "test" body) "ok" "rejected"))]
(kit/post url {:body "test"} #(is (= "ok" (:body %))))))) ;;=> fails