Skip to content

Commit

Permalink
Copy clj-http multipart streams, so reading one doesn't break the other
Browse files Browse the repository at this point in the history
  • Loading branch information
KingMob committed Oct 26, 2022
1 parent a94bc5f commit 3822a76
Showing 1 changed file with 38 additions and 15 deletions.
53 changes: 38 additions & 15 deletions test/aleph/http/clj_http/util.clj
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,29 @@
symbol))
middleware-list)))

(defn build-aleph-ring-map
"Constructs an aleph ring map, based on the clj-http ring map.
Adds corresponding middleware, and copies multipart ByteArrayInputStreams,
since they can't be read more than once by default."
[clj-http-ring-map clj-http-middleware]
(let [middleware-ring-map (merge clj-http-ring-map {:pool (aleph-test-conn-pool clj-http-middleware)})]
(if (contains? clj-http-ring-map :multipart)
(update-in middleware-ring-map
[:multipart]
(fn [parts]
(into []
(map (fn [part]
(if (= ByteArrayInputStream (-> part :content class))
(let [bais (:content part)
_ (.mark bais 0)
aleph-part (assoc part :content (ByteArrayInputStream. (.readAllBytes bais)))]
(.reset bais)
aleph-part)
part)))
parts)))
middleware-ring-map)))

(defn make-request
"Need to switch between clj-http's core/request and client/request.
Expand All @@ -248,7 +271,7 @@
;;_ (prn clj-http-ring-map)
clj-http-middleware (if using-middleware? clj-http.client/*current-middleware* [])
;;_ (print-middleware-list clj-http.client/*current-middleware*)
aleph-ring-map (merge base-req req {:pool (aleph-test-conn-pool clj-http-middleware)})
aleph-ring-map (build-aleph-ring-map clj-http-ring-map clj-http-middleware)
;;_ (prn aleph-ring-map)
is-multipart (contains? clj-http-ring-map :multipart)
clj-http-resp (clj-http-request clj-http-ring-map)
Expand Down Expand Up @@ -280,24 +303,24 @@
;;(prn aleph-resp)
;;(println)

(do
(println "clj-http req:")
(prn clj-http-ring-map)
(println)
(println "clj-http resp:")
(prn clj-http-resp)
(println)
(println)
(println "aleph req:")
(prn aleph-ring-map)
(println)
(println "aleph resp:")
(prn aleph-resp))
;;(do
;; (println "clj-http req:")
;; (prn clj-http-ring-map)
;; (println)
;; (println "clj-http resp:")
;; (prn clj-http-resp)
;; (println)
;; (println)
;; (println "aleph req:")
;; (prn aleph-ring-map)
;; (println)
;; (println "aleph resp:")
;; (prn aleph-resp))

(is-headers= (apply dissoc (:headers clj-http-resp) multipart-related-headers)
(apply dissoc (:headers aleph-resp) multipart-related-headers))
(assoc clj-http-resp :body (multipart-resp= clj-http-resp aleph-resp)))
(do
(is-headers= (:headers clj-http-resp) (:headers aleph-resp))
(let [new-clj-http-body (bodies= (:body clj-http-resp) (:body aleph-resp) is-multipart)]
(let [new-clj-http-body (bodies= (:body clj-http-resp) (:body aleph-resp))]
(assoc clj-http-resp :body new-clj-http-body)))))))))

0 comments on commit 3822a76

Please sign in to comment.