Skip to content
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
25 changes: 24 additions & 1 deletion lib/reverse_proxy/runner.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,30 @@ defmodule ReverseProxy.Runner do
method = conn.method |> String.downcase |> String.to_atom
url = "#{conn.scheme}://#{server}#{conn.request_path}?#{conn.query_string}"
headers = conn.req_headers
{:ok, body, _conn} = Plug.Conn.read_body(conn)
body = case Plug.Conn.read_body(conn) do
{:ok, body, _conn} ->
body
{:more, body, conn} ->
{:stream,
Stream.resource(
fn -> {body, conn} end,
fn
{body, conn} ->
{[body], conn}
nil ->
{:halt, nil}
conn ->
case Plug.Conn.read_body(conn) do
{:ok, body, _conn} ->
{[body], nil}
{:more, body, conn} ->
{[body], conn}
end
end,
fn _ -> end
)
}
end

{method, url, body, headers}
end
Expand Down
19 changes: 19 additions & 0 deletions test/fixtures/body_length.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
defmodule ReverseProxyTest.BodyLength do
def request(_method, _url, body, _headers, _opts \\ []) do
body_length = case body do
{:stream, body} ->
body
|> Enum.join("")
|> byte_size
body ->
body
|> byte_size
end

{:ok, %{
:headers => [],
:status_code => 200,
:body => "#{body_length}"
}}
end
end
12 changes: 12 additions & 0 deletions test/reverse_proxy/runner_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ defmodule ReverseProxy.RunnerTest do
assert conn.resp_body == "success"
end

test "retrieve/3 - partial body" do
conn =
conn(:post, "/", String.duplicate("_", 8_000_000 + 1))
|> put_req_header("content-type", "application/json")
|> ReverseProxy.Runner.retreive(
["localhost"],
ReverseProxyTest.BodyLength
)

assert conn.resp_body == "8000001"
end

test "retreive/3 - http - success with response headers" do
conn = conn(:get, "/")
headers = ReverseProxyTest.SuccessHTTP.headers
Expand Down