Skip to content

Commit

Permalink
added from-map and to-map headers fns; response to use headers parse fn
Browse files Browse the repository at this point in the history
  • Loading branch information
onionpancakes committed Oct 14, 2023
1 parent e33f4c5 commit fd5c905
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
12 changes: 10 additions & 2 deletions src/dev/onionpancakes/hop/headers.clj
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,17 @@

;; Headers

(def headers-map-xf
(def to-map-xf
(map (juxt key (comp vec val))))

(defn headers-map
(defn to-map
[^HttpHeaders headers]
(into {} headers-map-xf (.map headers)))

(def from-map-bi-predicate
(reify java.util.function.BiPredicate
(test [_ _ _] true)))

(defn from-map
[m]
(HttpHeaders/of m from-map-bi-predicate))
6 changes: 3 additions & 3 deletions src/dev/onionpancakes/hop/response.clj
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@
:media-type (.. response
(headers)
(firstValue "content-type")
(map util/parse-media-type-function)
(map h/parse-media-type-function)
(orElse nil))
:character-encoding (.. response
(headers)
(firstValue "content-type")
(map util/parse-character-encoding-function)
(map h/parse-character-encoding-function)
(orElse nil))
:ssl-session (.. response (sslSession) (orElse nil))
:previous-response (.. response
Expand All @@ -72,7 +72,7 @@

(defn response-proxy
[^HttpResponse response]
(let [headers (delay (h/headers-map (.headers response)))]
(let [headers (delay (h/to-map (.headers response)))]
(ResponseProxy. response headers)))

(def ^java.util.function.Function response-proxy-function
Expand Down
7 changes: 3 additions & 4 deletions test/dev/onionpancakes/hop/test_response.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns dev.onionpancakes.hop.test-response
(:require [clojure.test :refer [deftest is are]]
[dev.onionpancakes.hop.headers :as headers]
[dev.onionpancakes.hop.request :as request]
[dev.onionpancakes.hop.response :as response])
(:import [java.net.http HttpResponse HttpHeaders HttpRequest
Expand All @@ -12,10 +13,8 @@
(body [this]
"Body")
(headers [this]
(-> {"content-encoding" ["gzip"]
"content-type" ["text/plain;charset=utf-8"]}
(HttpHeaders/of (reify BiPredicate
(test [_ _ _] true)))))
(headers/from-map {"content-encoding" ["gzip"]
"content-type" ["text/plain;charset=utf-8"]}))
(previousResponse [this]
(Optional/ofNullable nil))
(request [this]
Expand Down

0 comments on commit fd5c905

Please sign in to comment.