|
229 | 229 | symbol))
|
230 | 230 | middleware-list)))
|
231 | 231 |
|
| 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 | + |
232 | 267 | (defn make-request
|
233 | 268 | "Need to switch between clj-http's core/request and client/request.
|
234 | 269 |
|
|
248 | 283 | ;;_ (prn clj-http-ring-map)
|
249 | 284 | clj-http-middleware (if using-middleware? clj-http.client/*current-middleware* [])
|
250 | 285 | ;;_ (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) |
252 | 287 | ;;_ (prn aleph-ring-map)
|
253 | 288 | is-multipart (contains? clj-http-ring-map :multipart)
|
254 | 289 | clj-http-resp (clj-http-request clj-http-ring-map)
|
|
280 | 315 | ;;(prn aleph-resp)
|
281 | 316 | ;;(println)
|
282 | 317 |
|
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)) |
296 | 331 |
|
297 | 332 | (is-headers= (apply dissoc (:headers clj-http-resp) multipart-related-headers)
|
298 | 333 | (apply dissoc (:headers aleph-resp) multipart-related-headers))
|
299 | 334 | (assoc clj-http-resp :body (multipart-resp= clj-http-resp aleph-resp)))
|
300 | 335 | (do
|
301 | 336 | (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))] |
303 | 338 | (assoc clj-http-resp :body new-clj-http-body)))))))))
|
0 commit comments