Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #55: allow :body be a java.net.http.HttpRequest$BodyPublisher ins… #56

Merged
merged 1 commit into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading