dakrone / clj-http

An idiomatic clojure http client wrapping the apache client. Officially supported version.
http://clojars.org/clj-http
MIT License
1.78k stars 410 forks source link

Incorporate clj-http-fake #484

Open dAnjou opened 5 years ago

dAnjou commented 5 years ago

Hi,

I'm trying to test that my custom middleware is applied. For that I had a look at clj-http-fake because it'd give me the chance to look at the produced request. However, it's 2 years old which I'd consider this unmaintained, it's might even be out of sync with clj-http.

I think it'd make sense for clj-http to ship with something like this, otherwise it's kinda hard to test a highly customized HTTP client.

dakrone commented 5 years ago

I think it'd make sense for clj-http to ship with something like this, otherwise it's kinda hard to test a highly customized HTTP client.

You can write your own client and then wrap with the middleware (this is basically what clj-http-fake does). Hopefully that will let you test what you want. I do this in the tests:

(let [client (fn [req] ;; An example client function that returns whatever you need
               (if (= "example.com" (:server-name req))
                 {:status 302
                  :headers {"location" "http://example.net/bat"}}
                 {:status 200
                  :req req}))
      ;; Wrap the client with whatever middleware you want to test
      r-client (-> client client/wrap-url client/wrap-redirects)
      ;; Use the wrapped client with the regular parameters you would usually use
      resp (r-client {:server-name "example.com" :url "http://example.com"
                      :request-method :get})]

You should be able to test your middleware the same way, does that work for you?

However, it's 2 years old which I'd consider this un-maintained, it's might even be out of sync with clj-http.

I don't know how out of date it is (I haven't used it in a while). I would say right now that I don't think we should add it in to clj-http since it only increases the maintenance burden.