Skip to content

Commit 718fcce

Browse files
committed
Copy body and multipart content streams
Copy streams used in request maps, so consuming one doesn't break the other
1 parent a94bc5f commit 718fcce

File tree

1 file changed

+50
-15
lines changed

1 file changed

+50
-15
lines changed

test/aleph/http/clj_http/util.clj

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,41 @@
229229
symbol))
230230
middleware-list)))
231231

232+
(defn- bais-clone
233+
"Clones a ByteArrayInputStream and resets the original's pos, so it can be read again"
234+
[^ByteArrayInputStream bais]
235+
(.mark bais 0)
236+
(let [new-bais (ByteArrayInputStream. (.readAllBytes bais))]
237+
(.reset bais)
238+
new-bais))
239+
240+
(defn build-aleph-ring-map
241+
"Constructs an aleph ring map, based on the clj-http ring map.
242+
243+
Adds corresponding middleware, and copies request ByteArrayInputStreams,
244+
since they can't be read more than once by default."
245+
[clj-http-ring-map clj-http-middleware]
246+
(let [clone-bais-val (fn [m k]
247+
(if (= ByteArrayInputStream (-> m k class))
248+
(assoc m k (bais-clone (k m)))
249+
m))
250+
middleware-ring-map (merge clj-http-ring-map {:pool (aleph-test-conn-pool clj-http-middleware)})]
251+
(cond-> middleware-ring-map
252+
253+
(contains? clj-http-ring-map :body)
254+
(clone-bais-val :body)
255+
256+
(contains? clj-http-ring-map :multipart)
257+
(update-in [:multipart]
258+
(fn [parts]
259+
(into []
260+
(map #(clone-bais-val % :content)
261+
#_(fn [part]
262+
(if (= ByteArrayInputStream (-> part :content class))
263+
(assoc part :content (bais-clone (:content part)))
264+
part)))
265+
parts))))))
266+
232267
(defn make-request
233268
"Need to switch between clj-http's core/request and client/request.
234269
@@ -248,7 +283,7 @@
248283
;;_ (prn clj-http-ring-map)
249284
clj-http-middleware (if using-middleware? clj-http.client/*current-middleware* [])
250285
;;_ (print-middleware-list clj-http.client/*current-middleware*)
251-
aleph-ring-map (merge base-req req {:pool (aleph-test-conn-pool clj-http-middleware)})
286+
aleph-ring-map (build-aleph-ring-map clj-http-ring-map clj-http-middleware)
252287
;;_ (prn aleph-ring-map)
253288
is-multipart (contains? clj-http-ring-map :multipart)
254289
clj-http-resp (clj-http-request clj-http-ring-map)
@@ -280,24 +315,24 @@
280315
;;(prn aleph-resp)
281316
;;(println)
282317

283-
(do
284-
(println "clj-http req:")
285-
(prn clj-http-ring-map)
286-
(println)
287-
(println "clj-http resp:")
288-
(prn clj-http-resp)
289-
(println)
290-
(println)
291-
(println "aleph req:")
292-
(prn aleph-ring-map)
293-
(println)
294-
(println "aleph resp:")
295-
(prn aleph-resp))
318+
;;(do
319+
;; (println "clj-http req:")
320+
;; (prn clj-http-ring-map)
321+
;; (println)
322+
;; (println "clj-http resp:")
323+
;; (prn clj-http-resp)
324+
;; (println)
325+
;; (println)
326+
;; (println "aleph req:")
327+
;; (prn aleph-ring-map)
328+
;; (println)
329+
;; (println "aleph resp:")
330+
;; (prn aleph-resp))
296331

297332
(is-headers= (apply dissoc (:headers clj-http-resp) multipart-related-headers)
298333
(apply dissoc (:headers aleph-resp) multipart-related-headers))
299334
(assoc clj-http-resp :body (multipart-resp= clj-http-resp aleph-resp)))
300335
(do
301336
(is-headers= (:headers clj-http-resp) (:headers aleph-resp))
302-
(let [new-clj-http-body (bodies= (:body clj-http-resp) (:body aleph-resp) is-multipart)]
337+
(let [new-clj-http-body (bodies= (:body clj-http-resp) (:body aleph-resp))]
303338
(assoc clj-http-resp :body new-clj-http-body)))))))))

0 commit comments

Comments
 (0)