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

File publisher support & tests #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 9 additions & 1 deletion src/java_http_clj/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
HttpRequest$Builder
HttpResponse
HttpResponse$BodyHandlers]
[java.io File]
[java.nio.file Path]
[java.util.concurrent CompletableFuture]
[java.util.function Function Supplier]))

Expand Down Expand Up @@ -79,7 +81,13 @@
(HttpRequest$BodyPublishers/ofInputStream (input-stream-supplier body))

(instance? byte-array-class body)
(HttpRequest$BodyPublishers/ofByteArray body)))
(HttpRequest$BodyPublishers/ofByteArray body)

(instance? File body)
(HttpRequest$BodyPublishers/ofFile (.toPath ^File body))

(instance? Path body)
(HttpRequest$BodyPublishers/ofFile ^Path body)))

(def ^:private convert-headers-xf
(mapcat
Expand Down
17 changes: 15 additions & 2 deletions test/java_http_clj/integration_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
[java-http-clj.specs]
[mount.core :as mount])
(:import [java.net.http HttpResponse]
[java.io File]
[java.util Arrays]
[java.util.concurrent CompletableFuture]))

Expand Down Expand Up @@ -46,20 +47,32 @@
(is (= :http1.1 version))))

(testing "request-body-types"
(let [send-and-get-body
(let [file-content
"hello"

file
(doto (File/createTempFile "tmp" ".tmp")
(spit file-content))

path
(.toPath file)

send-and-get-body
(fn [body]
(:body (f {:uri (make-url "echo")
:method :post
:body body})))]
(is (= "ROOT" (:body (send (make-url)))))
(is (= s (send-and-get-body s)))
(is (= s (send-and-get-body (.getBytes s))))
(is (= file-content (send-and-get-body file)))
(is (= file-content (send-and-get-body path)))
(is (= s (send-and-get-body (io/input-stream (.getBytes s)))))))

(testing "response-body-types"
(let [send-echo (fn [opts] (f (make-url "echo" {:message s}) opts))]
(is (= s (:body (send-echo {:as :string}))))
(is (Arrays/equals (.getBytes s) (:body (send-echo {:as :byte-array}))))
(is (Arrays/equals (.getBytes s) ^bytes (:body (send-echo {:as :byte-array}))))
(is (= s (-> (send-echo {:as :input-stream}) :body slurp)))))

(testing "raw-opt"
Expand Down
3 changes: 2 additions & 1 deletion test/java_http_clj/test_server.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
(ns ^:skip-test java-http-clj.test-server
(:import org.eclipse.jetty.server.Server)
(:require [clojure.java.io :as io]
[clojure.test :refer :all]
[compojure.core :refer :all]
Expand Down Expand Up @@ -34,4 +35,4 @@
{:port port
:websockets {"/ws" ws-handler}
:join? false}))
:stop (.stop server))
:stop (.stop ^Server server))