Skip to content

Commit

Permalink
Fix #43: when using a string key for Accept header, the value is ov…
Browse files Browse the repository at this point in the history
…erridden by the default (#44)
  • Loading branch information
borkdude authored Sep 4, 2023
1 parent 31362d3 commit ae498b5
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

Babashka [http-client](https://github.com/babashka/http-client): HTTP client for Clojure and babashka built on java.net.http

## Unreleased

- #43: when using a string key for `Accept` header, the value is overridden by the default

## 0.4.14 (2023-08-17)

- [#41](https://github.com/babashka/http-client/issues/41): add `:uri` to response map
Expand Down
1 change: 1 addition & 0 deletions src/babashka/http_client/internal.clj
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@
request-defaults (:request client)
^HttpClient client (or (:client client) client)
req (merge-with merge-opts request-defaults req)
req (update req :headers aux/prefer-string-keys)
request-interceptors (or (:interceptors req)
interceptors/default-interceptors)
req (apply-interceptors req request-interceptors :request)
Expand Down
26 changes: 25 additions & 1 deletion src/babashka/http_client/internal/helpers.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns babashka.http-client.internal.helpers
{:no-doc true})
{:no-doc true}
(:require [clojure.string :as str]))

(defn ->uri [uri]
(cond (string? uri) (java.net.URI/create uri)
Expand All @@ -20,6 +21,23 @@
(-> k str (subs 1))
(str k)))

(defn capitalize-header [hdr]
(str/join "-" (map str/capitalize (str/split hdr #"-"))))

(defn prefer-string-keys
"Dissoc-es keyword header if equivalent string header is available already."
[header-map]
(reduce (fn [m k]
(if (keyword? k)
(let [s (coerce-key k)]
(if (or (clojure.core/get header-map (capitalize-header s))
(clojure.core/get header-map s))
(dissoc m k)
m))
m))
header-map
(keys header-map)))

(defn coerce-headers
[headers]
(mapcat
Expand All @@ -28,3 +46,9 @@
(interleave (repeat (coerce-key k)) v)
[(coerce-key k) v]))
headers))

;;;;

(comment
(prefer-string-keys {:accept 1 "Accept" 2})
)
28 changes: 23 additions & 5 deletions test/babashka/http_client_test.clj
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
(ns babashka.http-client-test
(:require
[babashka.fs :as fs]
[babashka.http-client :as http]
[babashka.http-client.internal.version :as iv]
[borkdude.deflet :refer [deflet]]
[cheshire.core :as json]
[clojure.java.io :as io]
[clojure.string :as str]
[clojure.test :refer [deftest is testing]]
[org.httpkit.server :as server]
[babashka.http-client :as http])
[org.httpkit.server :as server])
(:import
[clojure.lang ExceptionInfo]
[javax.net.ssl SSLContext]))
Expand Down Expand Up @@ -336,6 +336,24 @@
{:headers {:accept "application/json"}})
:body
(json/parse-string true)
:code)))
(is (= 200
(-> (http/get "http://localhost:12233/200"
{:headers {"Accept" "application/json"}})
:body
(json/parse-string true)
:code)))
(is (= 200
(-> (http/get "http://localhost:12233/200"
{:headers {"accept" "application/json"}})
:body
(json/parse-string true)
:code)))
(is (= 200
(-> (http/get "http://localhost:12233/200"
{:accept :json})
:body
(json/parse-string true)
:code))))

(deftest follow-redirects-test
Expand Down Expand Up @@ -408,9 +426,9 @@
(:status resp))}))
(is (= 200 @async-resp))
(def async-resp (http/get "http://localhost:12233/422" {:async true}))
(def ex (is (thrown-with-msg? java.util.concurrent.ExecutionException
#"^clojure.lang.ExceptionInfo: Exceptional status code: 422 "
@async-resp)))
(def _ex (is (thrown-with-msg? java.util.concurrent.ExecutionException
#"^clojure.lang.ExceptionInfo: Exceptional status code: 422 "
@async-resp)))
(def async-resp (http/get "http://localhost:12233/404" {:async true
:async-then (fn [resp]
(:status resp))
Expand Down

0 comments on commit ae498b5

Please sign in to comment.