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

Add ability to set custom boundary for multipart entity #628

Open
wants to merge 1 commit into
base: 3.x
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
3 changes: 2 additions & 1 deletion src/clj_http/multipart.clj
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,13 @@
(defn create-multipart-entity
"Takes a multipart vector of maps and creates a MultipartEntity with each
map added as a part, depending on the type of content."
[multipart {:keys [mime-subtype multipart-mode multipart-charset]
[multipart {:keys [mime-subtype multipart-mode multipart-charset boundary]
:or {mime-subtype "form-data"
multipart-mode HttpMultipartMode/STRICT}}]
(let [mp-entity (doto (MultipartEntityBuilder/create)
(.setMode multipart-mode)
(.setMimeSubtype mime-subtype))]
(when boundary (.setBoundary mp-entity boundary))
(when multipart-charset
(.setCharset mp-entity (encoding-to-charset multipart-charset)))
(doseq [m multipart]
Expand Down
9 changes: 9 additions & 0 deletions test/clj_http/test/multipart_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,12 @@
(testing "charset is set if a multipart-charset is supplied"
(let [mp-entity (create-multipart-entity [] {:multipart-charset "UTF-8"})]
(is (= "UTF-8" (EntityUtils/getContentCharSet mp-entity))))))

(deftest test-multipart-boundary
(testing "default random boundary is set when no custom boundary is supplied"
(let [mp-entity (create-multipart-entity [] {})]
(is (re-matches #"--.+--\r\n" (-> mp-entity .getContent slurp)))))

(testing "custom boundary is set when custom boundary is supplied"
(let [mp-entity (create-multipart-entity [] {:boundary "CustomMultipartBoundary"})]
(is (re-matches #"--CustomMultipartBoundary--\r\n" (-> mp-entity .getContent slurp))))))