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

Support any enumerable in mutlipart 2 #457

Merged
merged 5 commits into from
Feb 21, 2025
Merged
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
37 changes: 35 additions & 2 deletions lib/req/steps.ex
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,19 @@ defmodule Req.Steps do

* `File.Stream`

* `{value, options}` tuple. Supported options are `:filename` and `:content_type`
* `Enumerable`

* `{value, options}` tuple.

`value` can be any of the values mentioned above.

Supported options are: `:filename`, `:content_type`, and `:size`.

When `value` is an `Enumerable`, option `:size` can be set with
the binary size of the `value`. The size will be used to calculate
and send the `content-length` header which might be required for
some servers. There is no need to pass `:size` for `integer`,
`iodata`, and `File.Stream` values as it's automatically calculated.

* `:json` - if set, encodes the request body as JSON (using `Jason.encode_to_iodata!/1`), sets
the `accept` header to `application/json`, and the `content-type` header to `application/json`.
Expand All @@ -419,6 +431,21 @@ defmodule Req.Steps do
iex> resp.body["files"]
%{"b" => "2"}

Encoding streaming form (`multipart/form-data`):

iex> stream = Stream.cycle(["abc"]) |> Stream.take(3)
iex> fields = [file: {stream, filename: "b.txt"}]
iex> resp = Req.post!("https://httpbin.org/anything", form_multipart: fields)
iex> resp.body["files"]
%{"file" => "abcabcabc"}

# with explicit :size
iex> stream = Stream.cycle(["abc"]) |> Stream.take(3)
iex> fields = [file: {stream, filename: "b.txt", size: 9}]
iex> resp = Req.post!("https://httpbin.org/anything", form_multipart: fields)
iex> resp.body["files"]
%{"file" => "abcabcabc"}

Encoding JSON:

iex> Req.post!("https://httpbin.org/post", json: %{a: 2}).body["json"]
Expand All @@ -436,7 +463,7 @@ defmodule Req.Steps do

%{request | body: multipart.body}
|> Req.Request.put_new_header("content-type", multipart.content_type)
|> Req.Request.put_new_header("content-length", Integer.to_string(multipart.size))
|> then(&maybe_put_content_length(&1, multipart.size))

data = request.options[:json] ->
%{request | body: Jason.encode_to_iodata!(data)}
Expand All @@ -448,6 +475,12 @@ defmodule Req.Steps do
end
end

defp maybe_put_content_length(req, nil), do: req

defp maybe_put_content_length(req, size) do
Req.Request.put_new_header(req, "content-length", Integer.to_string(size))
end

@doc """
Uses a templated request path.

Expand Down
15 changes: 12 additions & 3 deletions lib/req/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -493,17 +493,21 @@ defmodule Req.Utils do
}
end

defp add_sizes(_, nil), do: nil
defp add_sizes(nil, _), do: nil
defp add_sizes(size1, size2), do: size1 + size2

defp add_form_parts({parts1, size1}, {parts2, size2})
when is_list(parts1) and is_list(parts2) do
{[parts1, parts2], size1 + size2}
{[parts1, parts2], add_sizes(size1, size2)}
end

defp add_form_parts({parts1, size1}, {parts2, size2}) do
{Stream.concat(parts1, parts2), size1 + size2}
{Stream.concat(parts1, parts2), add_sizes(size1, size2)}
end

defp encode_form_part({name, {value, options}}, boundary) do
options = Keyword.validate!(options, [:filename, :content_type])
options = Keyword.validate!(options, [:filename, :content_type, :size])

{parts, parts_size, options} =
case value do
Expand Down Expand Up @@ -533,6 +537,11 @@ defmodule Req.Utils do
end)

{stream, size, options}

enum ->
size = Keyword.get(options, :size)

{enum, size, options}
end

params =
Expand Down
25 changes: 25 additions & 0 deletions test/req/steps_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,31 @@ defmodule Req.StepsTest do
]
).status == 200
end

test "form_multipart enum without size" do
plug = fn conn ->
conn = Plug.Parsers.call(conn, Plug.Parsers.init(parsers: [:multipart]))

assert Plug.Conn.get_req_header(conn, "content-length") == []
assert %{"a" => "1", "b" => b} = conn.body_params

assert b.filename == "cycle"
assert b.content_type == "application/text"
assert File.read!(b.path) == "abcabc"

Plug.Conn.send_resp(conn, 200, "ok")
end

assert Req.post!(
plug: plug,
form_multipart: [
a: 1,
b:
{Stream.cycle(["a", "b", "c"]) |> Stream.take(6),
filename: "cycle", content_type: "application/text"}
]
).status == 200
end
end

test "put_params" do
Expand Down
56 changes: 56 additions & 0 deletions test/req/utils_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,62 @@ defmodule Req.UtilsTest do
"""
end

test "it works with size" do
%{content_type: content_type, body: body, size: size} =
Req.Utils.encode_form_multipart([field1: {"value", size: 5}], boundary: "foo")

body = IO.iodata_to_binary(body)

assert size == byte_size(body)
assert content_type == "multipart/form-data; boundary=foo"

assert body == """
\r\n\
--foo\r\n\
content-disposition: form-data; name=\"field1\"\r\n\
\r\n\
value\r\n\
--foo--\r\n\
"""
end

test "can accept any enumerable" do
enum = Stream.cycle(["a"]) |> Stream.take(10)

%{body: body, size: size} =
Req.Utils.encode_form_multipart([field1: {enum, size: 10}], boundary: "foo")

body = body |> Enum.to_list() |> IO.iodata_to_binary()

assert size == byte_size(body)
end

test "blindly trust :content_length option" do
enum = Stream.cycle(["a"]) |> Stream.take(10)
advertised_length = 50

%{body: body, size: size} =
Req.Utils.encode_form_multipart([field1: {enum, size: advertised_length}],
boundary: "foo"
)

body = body |> Enum.to_list() |> IO.iodata_to_binary()

assert size ==
byte_size(body) + advertised_length - IO.iodata_length(enum |> Enum.to_list())
end

test "can return nil size" do
enum = Stream.cycle(["a"]) |> Stream.take(10)

%{size: size} =
Req.Utils.encode_form_multipart([field1: {enum, []}],
boundary: "foo"
)

assert size == nil
end

@tag :tmp_dir
test "can return stream", %{tmp_dir: tmp_dir} do
File.write!("#{tmp_dir}/2.txt", "22")
Expand Down
Loading