Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3822a76

Browse files
committedOct 26, 2022
Copy clj-http multipart streams, so reading one doesn't break the other
1 parent a94bc5f commit 3822a76

File tree

1 file changed

+38
-15
lines changed

1 file changed

+38
-15
lines changed
 

‎test/aleph/http/clj_http/util.clj

+38-15
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,29 @@
229229
symbol))
230230
middleware-list)))
231231

232+
(defn build-aleph-ring-map
233+
"Constructs an aleph ring map, based on the clj-http ring map.
234+
235+
Adds corresponding middleware, and copies multipart ByteArrayInputStreams,
236+
since they can't be read more than once by default."
237+
[clj-http-ring-map clj-http-middleware]
238+
(let [middleware-ring-map (merge clj-http-ring-map {:pool (aleph-test-conn-pool clj-http-middleware)})]
239+
(if (contains? clj-http-ring-map :multipart)
240+
(update-in middleware-ring-map
241+
[:multipart]
242+
(fn [parts]
243+
(into []
244+
(map (fn [part]
245+
(if (= ByteArrayInputStream (-> part :content class))
246+
(let [bais (:content part)
247+
_ (.mark bais 0)
248+
aleph-part (assoc part :content (ByteArrayInputStream. (.readAllBytes bais)))]
249+
(.reset bais)
250+
aleph-part)
251+
part)))
252+
parts)))
253+
middleware-ring-map)))
254+
232255
(defn make-request
233256
"Need to switch between clj-http's core/request and client/request.
234257
@@ -248,7 +271,7 @@
248271
;;_ (prn clj-http-ring-map)
249272
clj-http-middleware (if using-middleware? clj-http.client/*current-middleware* [])
250273
;;_ (print-middleware-list clj-http.client/*current-middleware*)
251-
aleph-ring-map (merge base-req req {:pool (aleph-test-conn-pool clj-http-middleware)})
274+
aleph-ring-map (build-aleph-ring-map clj-http-ring-map clj-http-middleware)
252275
;;_ (prn aleph-ring-map)
253276
is-multipart (contains? clj-http-ring-map :multipart)
254277
clj-http-resp (clj-http-request clj-http-ring-map)
@@ -280,24 +303,24 @@
280303
;;(prn aleph-resp)
281304
;;(println)
282305

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))
306+
;;(do
307+
;; (println "clj-http req:")
308+
;; (prn clj-http-ring-map)
309+
;; (println)
310+
;; (println "clj-http resp:")
311+
;; (prn clj-http-resp)
312+
;; (println)
313+
;; (println)
314+
;; (println "aleph req:")
315+
;; (prn aleph-ring-map)
316+
;; (println)
317+
;; (println "aleph resp:")
318+
;; (prn aleph-resp))
296319

297320
(is-headers= (apply dissoc (:headers clj-http-resp) multipart-related-headers)
298321
(apply dissoc (:headers aleph-resp) multipart-related-headers))
299322
(assoc clj-http-resp :body (multipart-resp= clj-http-resp aleph-resp)))
300323
(do
301324
(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)]
325+
(let [new-clj-http-body (bodies= (:body clj-http-resp) (:body aleph-resp))]
303326
(assoc clj-http-resp :body new-clj-http-body)))))))))

0 commit comments

Comments
 (0)
Please sign in to comment.