Skip to content

Commit

Permalink
Merge pull request #57 from vincentjames501/multi-value-http-headers
Browse files Browse the repository at this point in the history
Allow a header's value to be a seq of src to match clj-http and multi-valued header responses
  • Loading branch information
gnarroway authored Jun 26, 2024
2 parents d574034 + 278abe9 commit c2abf3a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ request and returns a response. Convenience wrappers are provided for the http v
will depend on `:content`. When `:content` is a `String`, it will be `text/plain; charset=UTF-8` and when `:content`
is a `File`, it will attempt to guess the best content type or fallback to `application/octet-stream`.

`headers` Map of lower case strings to header values, concatenated with ',' when multiple values for a key.
This is presently a slight incompatibility with clj-http, which accepts keyword keys and list values.
`headers` Map of lower case strings to a header value. A header's value may be a string or a sequence of strings when
there are multiple values for a given header.

`basic-auth` Performs basic authentication (sending `Basic` authorization header). Accepts `{:user "user" :pass "pass"}`
Note that basic auth can also be passed via the `url` (e.g. `http://user:[email protected]`)
Expand Down
10 changes: 8 additions & 2 deletions src/hato/client.clj
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,14 @@
(defn- with-headers
^HttpRequest$Builder [builder headers]
(reduce-kv
(fn [^HttpRequest$Builder b ^String hk ^String hv]
(.header b hk hv))
(fn [^HttpRequest$Builder b ^String hk hv]
(if (sequential? hv)
(reduce
(fn [^HttpRequest$Builder acc-b shv]
(.header acc-b hk (str shv)))
b
hv)
(.header b hk (str hv))))
builder
headers))

Expand Down
25 changes: 24 additions & 1 deletion test/hato/client_test.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns hato.client-test
(:refer-clojure :exclude [get])
(:require [clojure.test :refer :all]
(:require [clojure.string :as str]
[clojure.test :refer :all]
[hato.client :refer :all]
[clojure.java.io :as io]
[org.httpkit.server :as http-kit]
Expand Down Expand Up @@ -339,3 +340,25 @@
(fn [req]
::response))]})]
(is (= ::response r))))

(deftest multi-value-headers
(testing "We can send and receive multi-value headers"
(with-server (fn app [req]
{:status 200
:headers {"foo" ["bar" "baz"]
"content-type" "application/json"}
:body (json/generate-string
(-> (:headers req)
(select-keys ["a" "b" "c" "d"])
;; ring spec will take multi value headers as a comma separated string.
(update "b" #(str/split % #","))))})
(let [response (get "http://localhost:1234" {:headers {"a" "single"
"b" ["1" "2"]
"c" "1,2"
"d" 123}
:as :json})]
(is (= {"content-type" "application/json"
"foo" ["bar" "baz"]}
(select-keys (:headers response) ["foo" "content-type"])))
(is (= {:a "single" :b ["1" "2"] :c "1,2" :d "123"}
(:body response)))))))

0 comments on commit c2abf3a

Please sign in to comment.