From 48d4b69378385aa2d67747709c13d2328ea75af3 Mon Sep 17 00:00:00 2001 From: Alex Hammel Date: Tue, 19 Jul 2022 15:51:22 -0700 Subject: [PATCH] Allow overriding of the file name --- src/clj_http/multipart.clj | 12 ++++++------ test/clj_http/test/multipart_test.clj | 11 ++++++++++- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/clj_http/multipart.clj b/src/clj_http/multipart.clj index e4381ace..8abe0def 100644 --- a/src/clj_http/multipart.clj +++ b/src/clj_http/multipart.clj @@ -31,22 +31,22 @@ (defmethod make-multipart-body File ;; Create a FileBody object from the given map, requiring at least :content - [{:keys [^String name ^String mime-type ^File content ^String encoding]}] + [{:keys [^String name ^String mime-type ^File content ^String encoding ^String file-name]}] (cond (and name mime-type content encoding) - (FileBody. content (ContentType/create mime-type encoding) name) + (FileBody. content (ContentType/create mime-type encoding) (or file-name name)) (and mime-type content encoding) - (FileBody. content (ContentType/create mime-type encoding)) + (FileBody. content (ContentType/create mime-type encoding) file-name) (and name mime-type content) - (FileBody. content (ContentType/create mime-type) name) + (FileBody. content (ContentType/create mime-type) (or file-name name)) (and mime-type content) - (FileBody. content (ContentType/create mime-type)) + (FileBody. content (ContentType/create mime-type) file-name) content - (FileBody. content) + (FileBody. content nil file-name) :else (throw (Exception. "Multipart file body must contain at least :content")))) diff --git a/test/clj_http/test/multipart_test.clj b/test/clj_http/test/multipart_test.clj index 7c309cf5..cdedba12 100644 --- a/test/clj_http/test/multipart_test.clj +++ b/test/clj_http/test/multipart_test.clj @@ -172,7 +172,16 @@ (is (= "file-body" (body-mime-type body))) (is (= (Charset/forName "ascii") (body-charset body))) (is (= test-file (.getFile body) )) - (is (= "testname" (.getFilename body))))))) + (is (= "testname" (.getFilename body))))) + + (testing "the :file-name key overrides the :name key" + (let [test-file (File. "testfile") + body (make-multipart-body {:content test-file + :name "testname" + :file-name "foo.txt"})] + (is (instance? FileBody body)) + (is (= test-file (.getFile body) )) + (is (= "foo.txt" (.getFilename body))))))) (deftest test-multipart-content-charset (testing "charset is nil if no multipart-charset is supplied"