Skip to content
Closed
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
14 changes: 11 additions & 3 deletions lib/multipart.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 14 additions & 0 deletions test/multipart_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading