Skip to content

Commit

Permalink
Fix #55: allow :body be a java.net.http.HttpRequest$BodyPublisher ins…
Browse files Browse the repository at this point in the history
…tance (#56)
  • Loading branch information
lotuc authored Apr 23, 2024
1 parent 199406e commit ab6b31f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
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

- [#55](https://github.com/babashka/http-client/issues/55): allow `:body` be `java.net.http.HttpRequest$BodyPublisher`

## 0.4.18 (2024-04-18)

- Support a Clojure function as `:client` option, mostly useful for testing
Expand Down
5 changes: 4 additions & 1 deletion src/babashka/http_client/internal.clj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
HttpClient$Redirect
HttpClient$Version
HttpRequest
HttpRequest$BodyPublisher
HttpRequest$BodyPublishers
HttpRequest$Builder
HttpResponse
Expand Down Expand Up @@ -52,7 +53,6 @@
(doto (KeyStore/getInstance store-type)
(.load kss (char-array store-pass))))))


(def has-extended? (resolve 'javax.net.ssl.X509ExtendedTrustManager))

(defmacro if-has-extended [then else]
Expand Down Expand Up @@ -221,6 +221,9 @@
(let [^java.nio.file.Path path body]
(HttpRequest$BodyPublishers/ofFile path))

(instance? HttpRequest$BodyPublisher body)
body

:else
(throw (ex-info (str "Don't know how to convert " (type body) "to body")
{:body body}))))
Expand Down
14 changes: 14 additions & 0 deletions test/babashka/http_client_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
[org.httpkit.server :as server])
(:import
[clojure.lang ExceptionInfo]
[java.net.http HttpRequest$BodyPublishers]
[javax.net.ssl SSLContext]))

(def !server (atom nil))
Expand Down Expand Up @@ -132,6 +133,19 @@
(:body (http/post "https://postman-echo.com/post"
{:body (io/input-stream "README.md")}))
"babashka")))
(testing "HttpRequest$BodyPublisher body"
(is (not (str/includes?
(:body (http/post "https://postman-echo.com/post"
{:body (io/input-stream "README.md")}))
"content-length")))
(is (str/includes?
(:body (http/post "https://postman-echo.com/post"
{:body (HttpRequest$BodyPublishers/fromPublisher
(HttpRequest$BodyPublishers/ofInputStream
(reify java.util.function.Supplier
(get [_this] (io/input-stream "README.md"))))
(.length (io/file "README.md")))}))
"content-length")))
(testing "form-params"
(let [body (:body (http/post "https://postman-echo.com/post"
{:form-params {"name" "Michiel Borkent"
Expand Down

0 comments on commit ab6b31f

Please sign in to comment.