Skip to content

Commit

Permalink
replaced HeaderValue Coll impl with Indexed and Seqable
Browse files Browse the repository at this point in the history
  • Loading branch information
onionpancakes committed Nov 21, 2023
1 parent b7d6b2b commit ba35cdd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
16 changes: 12 additions & 4 deletions src/dev/onionpancakes/hop/request.clj
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,17 @@
;; Headers

(extend-protocol HeaderValue
java.util.Collection
clojure.lang.Indexed
(add-header-to-request-builder [this builder header-name]
(let [add-header-rf #(add-header-to-request-builder %2 % header-name)]
(reduce add-header-rf builder this)))
(loop [i 0 cnt (count this)]
(when (< i cnt)
(-> (nth this i)
(add-header-to-request-builder builder header-name))
(recur (inc i) cnt))))
clojure.lang.Seqable
(add-header-to-request-builder [this builder header-name]
(doseq [value this]
(add-header-to-request-builder value builder header-name)))
String
(add-header-to-request-builder [this builder header-name]
(.header ^HttpRequest$Builder builder header-name this))
Expand All @@ -72,7 +79,8 @@

(defn add-request-builder-header
[builder k value]
(add-header-to-request-builder value builder (name k)))
(add-header-to-request-builder value builder (name k))
builder)

(defn add-request-builder-headers
^HttpRequest$Builder
Expand Down
18 changes: 10 additions & 8 deletions test/dev/onionpancakes/hop/tests/test_request.clj
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,16 @@
(= (.map (.headers obj)) expected))
nil {}
{} {}
{"Foo" nil} {}
{"Foo" []} {}
{:Foo [nil]} {}
{"Foo" "Bar"} {"Foo" ["Bar"]}
{"Foo" ["Bar"]} {"Foo" ["Bar"]}
{:Foo ["Bar"]} {"Foo" ["Bar"]}
{"Foo" 0} {"Foo" ["0"]}
{"Foo" [0]} {"Foo" ["0"]}
{"foo" nil} {}
{"foo" []} {}
{:foo [nil]} {}
{"foo" "bar"} {"foo" ["bar"]}
{"foo" ["bar"]} {"foo" ["bar"]}
{:foo ["bar"]} {"foo" ["bar"]}
{:foo ["bar"]} {"foo" ["bar"]}
{:foo '("bar")} {"foo" ["bar"]}
{"foo" 0} {"foo" ["0"]}
{"foo" [0]} {"foo" ["0"]}

;; Mixed
{:Foo ["Bar" "Baz"] :Qux "Mux"} {"Foo" ["Bar" "Baz"]
Expand Down

0 comments on commit ba35cdd

Please sign in to comment.