Skip to content

Commit

Permalink
Added Multipart.add_file_with_name
Browse files Browse the repository at this point in the history
  • Loading branch information
secretworry committed Apr 22, 2017
1 parent 3788e6a commit d90c427
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/maxwell/multipart.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ defmodule Maxwell.Multipart do
append_part(multipart, {:file, path, disposition, extra_headers})
end

@spec add_file_with_name(t, Path.t, String.t) :: t
@spec add_file_with_name(t, Path.t, String.t, headers_t) :: t
def add_file_with_name(multipart, path, name, extra_headers \\ []) do
filename = Path.basename(path)
disposition = {"form-data", [{"name", name}, {"filename", filename}]}
append_part(multipart, {:file, path, disposition, extra_headers})
end

@spec add_field(t, String.t, binary) :: t
def add_field(multipart, name, value) when is_binary(name) and is_binary(value) do
append_part(multipart, {name, value})
Expand Down
11 changes: 11 additions & 0 deletions test/maxwell/multipart_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,17 @@ defmodule Maxwell.MultipartTest do
== {:multipart, [{:file, "test.png", disposition, headers}]}
end

test "add_file_with_name/3 should add a file with name" do
assert Multipart.new |> Multipart.add_file_with_name("test.png", "media")
== {:multipart, [{:file, "test.png", {"form-data", [{"name", "media"}, {"filename", "test.png"}]}, []}]}
end

test "add_file_with_name/4 should add a file with name and headers" do
headers = [{"content-type", "image/png"}]
assert Multipart.new |> Multipart.add_file_with_name("test.png", "media", headers)
== {:multipart, [{:file, "test.png", {"form-data", [{"name", "media"}, {"filename", "test.png"}]}, headers}]}
end

test "add_field/3 should add a data part" do
assert Multipart.new |> Multipart.add_field("key", "value")
== {:multipart, [{"key", "value"}]}
Expand Down

0 comments on commit d90c427

Please sign in to comment.