Skip to content

Commit

Permalink
v0.1.8
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude committed Mar 18, 2023
1 parent 58dd30b commit 40dcf9c
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 7 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

## 0.1.8

- Fix binary file uploads

## 0.1.7

- Add `:async-then` and `:async-catch` callbacks that go together with `:async`
Expand Down
6 changes: 4 additions & 2 deletions deps.edn
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
{:deps {}
:aliases
{:neil {:project {:name org.babashka/http-client
:version "0.1.7"}}
:version "0.1.8"}}
:repl {:extra-deps {cheshire/cheshire {:mvn/version "5.11.0"}
io.github.borkdude/deflet {:mvn/version "0.1.0"}}}
io.github.borkdude/deflet {:mvn/version "0.1.0"}
babashka/fs {:mvn/version "0.2.16"}}}
:test ;; added by neil
{:extra-paths ["test"]
:extra-deps {cheshire/cheshire {:mvn/version "5.11.0"}
io.github.borkdude/deflet {:mvn/version "0.1.0"}
babashka/fs {:mvn/version "0.2.16"}
io.github.cognitect-labs/test-runner
{:git/tag "v0.5.0" :git/sha "b3fd0d2"}
http-kit/http-kit {:mvn/version "2.6.0"}}
Expand Down
Binary file added icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 8 additions & 2 deletions src/babashka/http_client/internal.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
(:require
[babashka.http-client.interceptors :as interceptors]
[babashka.http-client.internal.version :as iv]
[clojure.string :as str])
[clojure.string :as str]
[clojure.java.io :as io])
(:import
[java.net URI URLEncoder]
[java.net.http
Expand Down Expand Up @@ -115,7 +116,12 @@
(HttpRequest$BodyPublishers/ofByteArray body)

(instance? java.io.File body)
(HttpRequest$BodyPublishers/ofString (slurp body))
(let [^java.nio.file.Path path (.toPath (io/file body))]
(HttpRequest$BodyPublishers/ofFile path))

(instance? java.nio.file.Path body)
(let [^java.nio.file.Path path body]
(HttpRequest$BodyPublishers/ofFile path))

:else
(throw (ex-info (str "Don't know how to convert " (type body) "to body")
Expand Down
18 changes: 15 additions & 3 deletions test/babashka/http_client_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
(:require
[babashka.http-client.internal.version :as iv]
[cheshire.core :as json]
[babashka.fs :as fs]
[clojure.java.io :as io]
[clojure.string :as str]
[clojure.test :refer [deftest is testing]]
Expand All @@ -15,13 +16,14 @@
(defn run-server []
(let [server
(server/run-server
(fn [{:keys [uri] :as req}]
(fn [{:keys [uri body] :as req}]
(let [status (parse-long (subs uri 1))
json? (some-> req :headers (get "accept") (str/includes? "application/json"))]
(case status
200 (let [body (if json?
(json/generate-string {:code 200})
"200 OK")]
(if body body
"200 OK"))]
{:status 200
:body body})
404 {:status 404
Expand Down Expand Up @@ -92,11 +94,21 @@
(:body (http/post "https://postman-echo.com/post"
{:body "From Clojure"}))
"From Clojure"))
(testing "file body"
(testing "text file body"
(is (str/includes?
(:body (http/post "https://postman-echo.com/post"
{:body (io/file "README.md")}))
"babashka")))
(testing "binary file body"
(let [file-bytes (fs/read-all-bytes (io/file "icon.png"))
body1 (:body (http/post "http://localhost:12233/200"
{:body (io/file "icon.png")
:as :bytes}))
body2 (:body (http/post "http://localhost:12233/200"
{:body (fs/path "icon.png")
:as :bytes}))]
(is (java.util.Arrays/equals file-bytes body1))
(is (java.util.Arrays/equals file-bytes body2))))
(testing "JSON body"
(let [response (http/post "https://postman-echo.com/post"
{:headers {"Content-Type" "application/json"}
Expand Down

0 comments on commit 40dcf9c

Please sign in to comment.