Page MenuHomePhorge

No OneTemporary

Size
3 KB
Referenced Files
None
Subscribers
None
diff --git a/lib/tesla/adapter/hackney.ex b/lib/tesla/adapter/hackney.ex
index c8e9bc2..9bbaf96 100644
--- a/lib/tesla/adapter/hackney.ex
+++ b/lib/tesla/adapter/hackney.ex
@@ -1,20 +1,32 @@
defmodule Tesla.Adapter.Hackney do
def call(env) do
{:ok, status, headers, ref} = request(env)
{:ok, body} = :hackney.body(ref)
format_response(env, status, headers, body)
end
defp format_response(env, status, headers, body) do
headers = Enum.into(headers, %{})
%{env | status: status,
headers: headers,
body: body}
end
defp request(env) do
- :hackney.request(env.method, to_char_list(env.url), Enum.into(env.headers, []), env.body || '')
+ request(env.method, to_char_list(env.url), Enum.into(env.headers, []), env.body)
+ end
+ defp request(method, url, headers, %Stream{} = body), do: request_stream(method, url, headers, body)
+ defp request(method, url, headers, body) when is_function(body), do: request_stream(method, url, headers, body)
+ defp request(method, url, headers, body) do
+ :hackney.request(method, url, headers, body || '')
+ end
+
+
+ defp request_stream(method, url, headers, body) do
+ {:ok, ref} = :hackney.request(method, url, headers, :stream)
+ for data <- body, do: :ok = :hackney.send_body(ref, data)
+ :hackney.start_response(ref)
end
end
diff --git a/test/tesla/adapter/hackney_test.exs b/test/tesla/adapter/hackney_test.exs
index 21d2c86..ce54a5e 100644
--- a/test/tesla/adapter/hackney_test.exs
+++ b/test/tesla/adapter/hackney_test.exs
@@ -1,17 +1,18 @@
defmodule HackneyTest do
use ExUnit.Case
use Tesla.Adapter.TestCase.Basic, client: HackneyTest.Client
+ use Tesla.Adapter.TestCase.StreamRequestBody, client: HackneyTest.Client
defmodule Client do
use Tesla.Builder
adapter :hackney
end
setup do
case Application.ensure_started(:hackney) do
{:error, _} -> :hackney.start
:ok -> :ok
end
end
end
diff --git a/test/tesla/adapter/test_case.ex b/test/tesla/adapter/test_case.ex
index 54b78b9..d0b65ab 100644
--- a/test/tesla/adapter/test_case.ex
+++ b/test/tesla/adapter/test_case.ex
@@ -1,16 +1,38 @@
defmodule Tesla.Adapter.TestCase.Basic do
defmacro __using__([client: client]) do
quote do
test "basic get request" do
response = unquote(client).get("http://httpbin.org/ip")
assert response.status == 200
end
test "basic post request" do
response = unquote(client).post("http://httpbin.org/post", "some-post-data")
assert response.status == 200
assert Regex.match?(~r/some-post-data/, to_string(response.body))
end
end
end
end
+
+defmodule Tesla.Adapter.TestCase.StreamRequestBody do
+ defmacro __using__([client: client]) do
+ quote do
+ test "stream request body: Stream.map" do
+ body = (1..5) |> Stream.map(&to_string/1)
+ response = unquote(client).post("http://httpbin.org/post", body)
+ assert response.status == 200
+ assert Regex.match?(~r/12345/, to_string(response.body))
+ end
+
+ test "stream request body: Stream.unfold" do
+ body = Stream.unfold(5, fn 0 -> nil; n -> {n,n-1} end)
+ |> Stream.map(&to_string/1)
+ response = unquote(client).post("http://httpbin.org/post", body)
+
+ assert response.status == 200
+ assert Regex.match?(~r/54321/, to_string(response.body))
+ end
+ end
+ end
+end

File Metadata

Mime Type
text/x-diff
Expires
Tue, Jan 21, 8:47 PM (1 d, 12 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
55754
Default Alt Text
(3 KB)

Event Timeline