From 722e102eab10cf00bd092cba5b0d43cc2b38a657 Mon Sep 17 00:00:00 2001 From: Jinkyou Son Date: Fri, 5 Jul 2024 16:08:22 +0900 Subject: [PATCH 1/4] Add quote_boundary opt to content_type/3 --- lib/multipart.ex | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/multipart.ex b/lib/multipart.ex index 4433c6c..873fce2 100644 --- a/lib/multipart.ex +++ b/lib/multipart.ex @@ -70,9 +70,17 @@ defmodule Multipart do iex> Multipart.content_type(multipart, "multipart/mixed") "multipart/mixed; boundary=\\"==abc123==\\"" """ - @spec content_type(Multipart.t(), String.t()) :: String.t() - def content_type(%__MODULE__{boundary: boundary}, mime_type) do - [mime_type, "boundary=\"#{boundary}\""] + @spec content_type(Multipart.t(), String.t(), Keyword.t()) :: String.t() + def content_type(%__MODULE__{boundary: boundary}, mime_type, opts \\ []) do + quote_boundary = opts |> Keyword.get(:quote_boundary, true) + + boundary = + case quote_boundary do + true -> "\"#{boundary}\"" + false -> boundary + end + + [mime_type, "boundary=#{boundary}"] |> Enum.join("; ") end From 8ffd41d61b0ef9ab88bcb03371d3eb737d0842c6 Mon Sep 17 00:00:00 2001 From: Jinkyou Son Date: Tue, 20 Jan 2026 09:11:32 +0900 Subject: [PATCH 2/4] Add unit tests for contet_type/3 --- test/multipart_test.exs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/multipart_test.exs b/test/multipart_test.exs index c4b5311..f2698cd 100644 --- a/test/multipart_test.exs +++ b/test/multipart_test.exs @@ -124,6 +124,20 @@ defmodule MultipartTest do assert output == expected_output end + describe "content_type/3" do + test "returns Content-Type header with quoted boundary by default" do + multipart = Multipart.new("myboundary") + content_type = Multipart.content_type(multipart, "multipart/form-data") + assert content_type == "multipart/form-data; boundary=\"myboundary\"" + end + + test "returns Content-Type header with unquoted boundary when specified" do + multipart = Multipart.new("myboundary") + content_type = Multipart.content_type(multipart, "multipart/form-data", quote_boundary: false) + assert content_type == "multipart/form-data; boundary=myboundary" + end + end + defp file_path(path) do Path.join(__DIR__, path) end From b6571c2fc06b164526685f780778c5d1a8451a00 Mon Sep 17 00:00:00 2001 From: Tom Taylor Date: Tue, 20 Jan 2026 09:24:17 +0000 Subject: [PATCH 3/4] Add docs for disabling boundary quotes --- lib/multipart.ex | 8 ++++++-- test/multipart_test.exs | 5 ++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/multipart.ex b/lib/multipart.ex index 873fce2..21ef651 100644 --- a/lib/multipart.ex +++ b/lib/multipart.ex @@ -66,9 +66,13 @@ defmodule Multipart do @doc """ Returns the Content-Type header for the `Multipart` message. - iex> multipart = Multipart.new("==abc123==") + Pass in `Keyword` option `:quote_boundary` set `false` to disable quoting the boundary. + + iex> multipart = Multipart.new("abc123") iex> Multipart.content_type(multipart, "multipart/mixed") - "multipart/mixed; boundary=\\"==abc123==\\"" + "multipart/mixed; boundary=\\"abc123\\"" + iex> Multipart.content_type(multipart, "multipart/mixed", quote_boundary: false) + "multipart/mixed; boundary=abc123" """ @spec content_type(Multipart.t(), String.t(), Keyword.t()) :: String.t() def content_type(%__MODULE__{boundary: boundary}, mime_type, opts \\ []) do diff --git a/test/multipart_test.exs b/test/multipart_test.exs index f2698cd..3ec12cf 100644 --- a/test/multipart_test.exs +++ b/test/multipart_test.exs @@ -133,7 +133,10 @@ defmodule MultipartTest do test "returns Content-Type header with unquoted boundary when specified" do multipart = Multipart.new("myboundary") - content_type = Multipart.content_type(multipart, "multipart/form-data", quote_boundary: false) + + content_type = + Multipart.content_type(multipart, "multipart/form-data", quote_boundary: false) + assert content_type == "multipart/form-data; boundary=myboundary" end end From fbbbef894e2a8ae3419cefda667d7edd938e3e88 Mon Sep 17 00:00:00 2001 From: Tom Taylor Date: Tue, 20 Jan 2026 09:27:44 +0000 Subject: [PATCH 4/4] Style tweak --- lib/multipart.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/multipart.ex b/lib/multipart.ex index 21ef651..5ad3a97 100644 --- a/lib/multipart.ex +++ b/lib/multipart.ex @@ -76,7 +76,7 @@ defmodule Multipart do """ @spec content_type(Multipart.t(), String.t(), Keyword.t()) :: String.t() def content_type(%__MODULE__{boundary: boundary}, mime_type, opts \\ []) do - quote_boundary = opts |> Keyword.get(:quote_boundary, true) + quote_boundary = Keyword.get(opts, :quote_boundary, true) boundary = case quote_boundary do