From fad590542fc92aa22af31b99f5fecab224c3c894 Mon Sep 17 00:00:00 2001 From: Ivan Grishaev Date: Tue, 24 Dec 2024 12:48:08 +0300 Subject: [PATCH 1/2] File publisher support & tests --- src/java_http_clj/core.clj | 10 +++++++++- test/java_http_clj/integration_test.clj | 12 ++++++++++-- test/java_http_clj/test_server.clj | 3 ++- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/java_http_clj/core.clj b/src/java_http_clj/core.clj index c78e8a6..747d169 100644 --- a/src/java_http_clj/core.clj +++ b/src/java_http_clj/core.clj @@ -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])) @@ -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 diff --git a/test/java_http_clj/integration_test.clj b/test/java_http_clj/integration_test.clj index 2d46058..020638b 100644 --- a/test/java_http_clj/integration_test.clj +++ b/test/java_http_clj/integration_test.clj @@ -46,7 +46,13 @@ (is (= :http1.1 version)))) (testing "request-body-types" - (let [send-and-get-body + (let [file + (io/file "project.clj") + + path + (.toPath file) + + send-and-get-body (fn [body] (:body (f {:uri (make-url "echo") :method :post @@ -54,12 +60,14 @@ (is (= "ROOT" (:body (send (make-url))))) (is (= s (send-and-get-body s))) (is (= s (send-and-get-body (.getBytes s)))) + (is (= (slurp file) (send-and-get-body file))) + (is (= (slurp file) (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" diff --git a/test/java_http_clj/test_server.clj b/test/java_http_clj/test_server.clj index 1e434dc..41e23e9 100644 --- a/test/java_http_clj/test_server.clj +++ b/test/java_http_clj/test_server.clj @@ -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] @@ -34,4 +35,4 @@ {:port port :websockets {"/ws" ws-handler} :join? false})) - :stop (.stop server)) + :stop (.stop ^Server server)) From 6644ed34f134608ebbae2006bf636a9cf6a35f1e Mon Sep 17 00:00:00 2001 From: Ivan Grishaev Date: Fri, 27 Dec 2024 10:50:04 +0300 Subject: [PATCH 2/2] use a temp file in integration tests --- test/java_http_clj/integration_test.clj | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/test/java_http_clj/integration_test.clj b/test/java_http_clj/integration_test.clj index 020638b..0ca1ea2 100644 --- a/test/java_http_clj/integration_test.clj +++ b/test/java_http_clj/integration_test.clj @@ -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])) @@ -46,8 +47,12 @@ (is (= :http1.1 version)))) (testing "request-body-types" - (let [file - (io/file "project.clj") + (let [file-content + "hello" + + file + (doto (File/createTempFile "tmp" ".tmp") + (spit file-content)) path (.toPath file) @@ -60,8 +65,8 @@ (is (= "ROOT" (:body (send (make-url))))) (is (= s (send-and-get-body s))) (is (= s (send-and-get-body (.getBytes s)))) - (is (= (slurp file) (send-and-get-body file))) - (is (= (slurp file) (send-and-get-body path))) + (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"